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

STEllAR-GROUP / hpx / #862

10 Jan 2023 05:30PM UTC coverage: 86.582% (-0.05%) from 86.634%
#862

push

StellarBot
Merge #6130

6130: Remove the mutex lock in the critical path of get_partitioner. r=hkaiser a=JiakunYan

Remove the mutex lock in the critical path of hpx::resource::detail::get_partitioner.

The protected variable `partitioner_ref` is only set once during initialization.

Co-authored-by: Jiakun Yan <jiakunyan1998@gmail.com>

6 of 6 new or added lines in 1 file covered. (100.0%)

174767 of 201851 relevant lines covered (86.58%)

2069816.07 hits per line

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

61.02
/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,135✔
46
        {
47
            os << std::right << std::setfill('0') << std::setw(N)
3,136✔
48
               << std::noshowbase << std::dec << v;
3,136✔
49
        }
3,136✔
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)
952✔
92
        {
93
            os << std::right << "0x" << std::setfill('0') << std::setw(N)
950✔
94
               << std::noshowbase << std::hex << v;
950✔
95
        }
950✔
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,900✔
145
        {
146
            os << std::left << std::setfill(' ') << std::setw(N) << v;
1,900✔
147
        }
1,900✔
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<uint8_t const*>(&ipdata_))    //-V206
×
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<<(std::ostream& os, current_time_print_helper)
954✔
178
        {
179
            static std::chrono::steady_clock::time_point log_t_start =
954✔
180
                std::chrono::steady_clock::now();
2✔
181

182
            auto now = std::chrono::steady_clock::now();    //-V656
952✔
183
            auto nowt = std::chrono::duration_cast<std::chrono::microseconds>(
1,904✔
184
                now - log_t_start)
952✔
185
                            .count();
952✔
186

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

192
    ///////////////////////////////////////////////////////////////////////////
193
    namespace detail {
194

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

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

202
        void generate_prefix(std::ostream& os)
959✔
203
        {
204
            os << detail::current_time_print_helper();
964✔
205
            if (print_info)
964✔
206
            {
207
                print_info(os);
965✔
208
            }
965✔
209
            os << detail::hostname_print_helper();
964✔
210
        }
964✔
211
    }    // namespace detail
212

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

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

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

245
    namespace detail {
246

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

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

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

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

313
        template HPX_CORE_EXPORT void print_array(
314
            std::string const&, std::uint64_t const*, std::size_t);
315
    }    // namespace detail
316
}    // namespace hpx::debug
317
/// \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