• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #852

17 Dec 2022 04:43PM UTC coverage: 85.912% (-0.7%) from 86.568%
#852

push

StellarBot
Merge #6106

6106: Modernizing modules of levels 0 to 5 r=hkaiser a=hkaiser

- flyby: HPX_FORWARD/HPX_MOVE now expand to std::forward and std::move if those are implemented as builtin functions

working towards #5497

Co-authored-by: Hartmut Kaiser <hartmut.kaiser@gmail.com>

87 of 87 new or added lines in 24 files covered. (100.0%)

173152 of 201546 relevant lines covered (85.91%)

1910264.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

60.68
/libs/core/debugging/src/print.cpp
1
//  Copyright (c) 2019-2020 John Biddiscombe
2
//
3
//  SPDX-License-Identifier: BSL-1.0
4
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
5
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6

7
#include <hpx/config.hpp>
8
#include <hpx/debugging/environ.hpp>
9
#include <hpx/debugging/print.hpp>
10

11
#include <algorithm>
12
#include <array>
13
#include <atomic>
14
#include <bitset>
15
#include <chrono>
16
#include <climits>
17
#include <cmath>
18
#include <cstddef>
19
#include <cstdint>
20
#include <cstring>
21
#include <functional>
22
#include <iomanip>
23
#include <iostream>
24
#include <iterator>
25
#include <string>
26
#include <thread>
27
#include <type_traits>
28
#include <utility>
29
#include <vector>
30

31
#if defined(__FreeBSD__)
32
HPX_CORE_EXPORT char** freebsd_environ = nullptr;
33
#endif
34

35
// ------------------------------------------------------------
36
/// \cond NODETAIL
37
namespace hpx::debug {
38

39
    // ------------------------------------------------------------------
40
    // format as zero padded int
41
    // ------------------------------------------------------------------
42
    namespace detail {
43

44
        template <typename Int>
45
        HPX_CORE_EXPORT void print_dec(std::ostream& os, Int const& v, int N)
3,336✔
46
        {
47
            os << std::right << std::setfill('0') << std::setw(N)
3,353✔
48
               << std::noshowbase << std::dec << v;
3,353✔
49
        }
3,353✔
50

51
        template HPX_CORE_EXPORT void print_dec(
52
            std::ostream&, std::int16_t const&, int);
53
        template HPX_CORE_EXPORT void print_dec(
54
            std::ostream&, std::int32_t const&, int);
55
        template HPX_CORE_EXPORT void print_dec(
56
            std::ostream&, std::int64_t const&, int);
57
        template HPX_CORE_EXPORT void print_dec(
58
            std::ostream&, std::uint64_t const&, int);
59

60
        template HPX_CORE_EXPORT void print_dec(
61
            std::ostream&, std::atomic<int> const&, int);
62
        template HPX_CORE_EXPORT void print_dec(
63
            std::ostream&, std::atomic<unsigned int> const&, int);
64
    }    // namespace detail
65

66
    // ------------------------------------------------------------------
67
    // format as pointer
68
    // ------------------------------------------------------------------
69
    ptr::ptr(void const* v) noexcept
65✔
70
      : data_(v)
65✔
71
    {
72
    }
65✔
73

74
    ptr::ptr(std::uintptr_t v) noexcept
×
75
      : data_(reinterpret_cast<void const*>(v))
×
76
    {
77
    }
×
78

79
    std::ostream& operator<<(std::ostream& os, ptr const& d)
65✔
80
    {
81
        os << d.data_;
65✔
82
        return os;
65✔
83
    }
84

85
    // ------------------------------------------------------------------
86
    // format as zero padded hex
87
    // ------------------------------------------------------------------
88
    namespace detail {
89

90
        template <typename Int>
91
        void print_hex(std::ostream& os, Int v, int N)
975✔
92
        {
93
            os << std::right << "0x" << std::setfill('0') << std::setw(N)
975✔
94
               << std::noshowbase << std::hex << v;
975✔
95
        }
975✔
96

97
        template HPX_CORE_EXPORT void print_hex(
98
            std::ostream&, std::thread::id, int);
99
        template HPX_CORE_EXPORT void print_hex(
100
            std::ostream&, unsigned long, int);
101
        template HPX_CORE_EXPORT void print_hex(std::ostream&, int, int);
102

103
        void print_ptr(std::ostream& os, void* v, int N)
290✔
104
        {
105
            os << std::right << std::setw(N) << std::noshowbase << std::hex
290✔
106
               << v;
290✔
107
        }
290✔
108

109
    }    // namespace detail
110

111
    // ------------------------------------------------------------------
112
    // format as binary bits
113
    // ------------------------------------------------------------------
114
    namespace detail {
115

116
        template <typename Int>
117
        HPX_CORE_EXPORT void print_bin(std::ostream& os, Int v, int N)
36✔
118
        {
119
            char const* beg = reinterpret_cast<char const*>(&v);
36✔
120
            char const* end = beg + sizeof(v);
36✔
121

122
            N = (N + CHAR_BIT - 1) / CHAR_BIT;
36✔
123
            while (beg != end && N-- > 0)
72✔
124
            {
125
                os << std::bitset<CHAR_BIT>(*beg++);
36✔
126
            }
127
        }
36✔
128

129
        template HPX_CORE_EXPORT void print_bin(
130
            std::ostream&, std::uint64_t, int);
131

132
#if defined(__APPLE__)
133
        // Explicit instantiation necessary to solve undefined symbol for MacOS
134
        template HPX_CORE_EXPORT void print_bin(
135
            std::ostream&, unsigned long, int);
136
#endif
137
    }    // namespace detail
138

139
    // ------------------------------------------------------------------
140
    // format as padded string
141
    // ------------------------------------------------------------------
142
    namespace detail {
143

144
        void print_str(std::ostream& os, char const* v, int N)
1,967✔
145
        {
146
            os << std::left << std::setfill(' ') << std::setw(N) << v;
1,968✔
147
        }
1,968✔
148
    }    // namespace detail
149

150
    // ------------------------------------------------------------------
151
    // format as ip address
152
    // ------------------------------------------------------------------
153
    ipaddr::ipaddr(void const* a) noexcept
×
154
      : data_(reinterpret_cast<std::uint8_t const*>(a))
×
155
      , ipdata_(0)
×
156
    {
157
    }
×
158

159
    ipaddr::ipaddr(std::uint32_t a) noexcept
×
160
      : data_(reinterpret_cast<const uint8_t*>(&ipdata_))
×
161
      , ipdata_(a)
×
162
    {
163
    }
×
164

165
    std::ostream& operator<<(std::ostream& os, ipaddr const& p)
×
166
    {
167
        os << std::dec << int(p.data_[0]) << "." << int(p.data_[1]) << "."
×
168
           << int(p.data_[2]) << "." << int(p.data_[3]);
×
169
        return os;
×
170
    }
171

172
    // ------------------------------------------------------------------
173
    // helper class for printing time since start
174
    // ------------------------------------------------------------------
175
    namespace detail {
176

177
        std::ostream& operator<<(
996✔
178
            std::ostream& os, current_time_print_helper const&)
179
        {
180
            static std::chrono::steady_clock::time_point log_t_start =
996✔
181
                std::chrono::steady_clock::now();
2✔
182

183
            auto now = std::chrono::steady_clock::now();
992✔
184
            auto nowt = std::chrono::duration_cast<std::chrono::microseconds>(
1,992✔
185
                now - log_t_start)
992✔
186
                            .count();
992✔
187

188
            os << debug::dec<10>(nowt) << " ";
992✔
189
            return os;
992✔
190
        }
191
    }    // namespace detail
192

193
    ///////////////////////////////////////////////////////////////////////////
194
    namespace detail {
195

196
        std::function<void(std::ostream&)> print_info;
1,250✔
197

198
        void register_print_info(void (*printer)(std::ostream&))
1,250✔
199
        {
200
            print_info = printer;
1,250✔
201
        }
1,250✔
202

203
        void generate_prefix(std::ostream& os)
994✔
204
        {
205
            os << detail::current_time_print_helper();
992✔
206
            if (print_info)
992✔
207
            {
208
                print_info(os);
1,004✔
209
            }
1,004✔
210
            os << detail::hostname_print_helper();
1,008✔
211
        }
1,008✔
212
    }    // namespace detail
213

214
    // ------------------------------------------------------------------
215
    // helper function for printing short memory dump and crc32
216
    // useful for debugging corruptions in buffers during
217
    // rma or other transfers
218
    // ------------------------------------------------------------------
219
    mem_crc32::mem_crc32(
×
220
        void const* a, std::size_t len, char const* txt) noexcept
221
      : addr_(reinterpret_cast<const uint64_t*>(a))
×
222
      , len_(len)
×
223
      , txt_(txt)
×
224
    {
225
    }
×
226

227
    std::ostream& operator<<(std::ostream& os, mem_crc32 const& p)
×
228
    {
229
        std::uint64_t const* uintBuf =
×
230
            static_cast<std::uint64_t const*>(p.addr_);
×
231
        os << "Memory:";
×
232
        os << " address " << hpx::debug::ptr(p.addr_) << " length "
×
233
           << hpx::debug::hex<6>(p.len_)
×
234
           << " CRC32:" << hpx::debug::hex<8>(crc32(p.addr_, p.len_)) << "\n";
×
235

236
        for (std::size_t i = 0;
×
237
             i < (std::min)(size_t(std::ceil(p.len_ / 8.0)), std::size_t(128));
×
238
             i++)
×
239
        {
240
            os << hpx::debug::hex<16>(*uintBuf++) << " ";
×
241
        }
×
242
        os << " : " << p.txt_;
×
243
        return os;
×
244
    }
245

246
    namespace detail {
247

248
        // ------------------------------------------------------------------
249
        // helper class for printing time since start
250
        // ------------------------------------------------------------------
251
        char const* hostname_print_helper::get_hostname() const
1,004✔
252
        {
253
            static bool initialized = false;
254
            static char hostname_[20] = {'\0'};
255
            if (!initialized)
1,003✔
256
            {
257
                initialized = true;
2✔
258
#if !defined(__FreeBSD__)
259
                gethostname(hostname_, std::size_t(12));
2✔
260
#endif
261
                int rank = guess_rank();
2✔
262
                if (rank != -1)
2✔
263
                {
264
                    std::string temp = "(" + std::to_string(guess_rank()) + ")";
×
265
                    std::strcat(hostname_, temp.c_str());
×
266
                }
×
267
            }
2✔
268
            return hostname_;
1,004✔
269
        }
×
270

271
        int hostname_print_helper::guess_rank() const
2✔
272
        {
273
#if defined(__FreeBSD__)
274
            char** env = freebsd_environ;
275
#else
276
            char** env = environ;
2✔
277
#endif
278
            std::vector<std::string> env_strings{"_RANK=", "_NODEID="};
4✔
279
            for (char** current = env; *current; ++current)
752✔
280
            {
281
                auto e = std::string(*current);
750✔
282
                for (auto s : env_strings)
2,250✔
283
                {
284
                    auto pos = e.find(s);
1,500✔
285
                    if (pos != std::string::npos)
1,500✔
286
                    {
287
                        //std::cout << "Got a rank string : " << e << std::endl;
288
                        return std::stoi(e.substr(pos + s.size(), 5));
×
289
                    }
290
                }
1,500✔
291
            }
750✔
292
            return -1;
2✔
293
        }
2✔
294

295
        std::ostream& operator<<(
1,005✔
296
            std::ostream& os, hostname_print_helper const& h)
297
        {
298
            os << debug::str<13>(h.get_hostname()) << " ";
1,006✔
299
            return os;
1,006✔
300
        }
301

302
        ///////////////////////////////////////////////////////////////////////
303
        template <typename T>
304
        HPX_CORE_EXPORT void print_array(
×
305
            std::string const& name, T const* data, std::size_t size)
306
        {
307
            std::cout << str<20>(name.c_str()) << ": {" << debug::dec<4>(size)
×
308
                      << "} : ";
×
309
            std::copy(
×
310
                data, data + size, std::ostream_iterator<T>(std::cout, ", "));
×
311
            std::cout << "\n";
×
312
        }
×
313

314
        template HPX_CORE_EXPORT void print_array(
315
            std::string const&, std::uint64_t const*, std::size_t);
316
    }    // namespace detail
317
}    // namespace hpx::debug
318
/// \endcond
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc