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

STEllAR-GROUP / hpx / #882

31 Aug 2023 07:44PM UTC coverage: 41.798% (-44.7%) from 86.546%
#882

push

19442 of 46514 relevant lines covered (41.8%)

126375.38 hits per line

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

9.38
/libs/full/performance_counters/src/server/arithmetics_counter_extended.cpp
1
//  Copyright (c) 2007-2025 Hartmut Kaiser
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/modules/async_base.hpp>
9
#include <hpx/modules/errors.hpp>
10
#include <hpx/modules/runtime_local.hpp>
11
#include <hpx/modules/string_util.hpp>
12
#include <hpx/performance_counters/counter_creators.hpp>
13
#include <hpx/performance_counters/counters.hpp>
14
#include <hpx/performance_counters/performance_counter.hpp>
15
#include <hpx/performance_counters/server/arithmetics_counter_extended.hpp>
16
#include <hpx/runtime_components/derived_component_factory.hpp>
17

18
#include <boost/accumulators/accumulators.hpp>
19
#include <boost/accumulators/statistics/count.hpp>
20
#include <boost/accumulators/statistics/max.hpp>
21
#include <boost/accumulators/statistics/mean.hpp>
22
#include <boost/accumulators/statistics/median.hpp>
23
#include <boost/accumulators/statistics/min.hpp>
24
#include <boost/accumulators/statistics/stats.hpp>
25
#include <boost/accumulators/statistics/variance.hpp>
26

27
#include <cstddef>
28
#include <cstdint>
29
#include <mutex>
30
#include <string>
31
#include <utility>
32
#include <vector>
33

34
#include <hpx/config/warnings_prefix.hpp>
35

36
///////////////////////////////////////////////////////////////////////////////
37
namespace hpx::performance_counters::server {
38

×
39
    ///////////////////////////////////////////////////////////////////////////
40
    template <typename Statistic>
41
    arithmetics_counter_extended<Statistic>::arithmetics_counter_extended() =
42
        default;
×
43

44
    template <typename Statistic>
45
    arithmetics_counter_extended<Statistic>::arithmetics_counter_extended(
46
        counter_info const& info,
×
47
        std::vector<std::string> const& base_counter_names)
48
      : base_type_holder(info)
×
49
      , counters_(base_counter_names)
50
    {
×
51
        if (info.type_ != counter_type::aggregating)
52
        {
53
            HPX_THROW_EXCEPTION(hpx::error::bad_parameter,
54
                "arithmetics_counter_extended<Statistic>::"
55
                "arithmetics_counter_extended",
×
56
                "unexpected counter type specified");
57
        }
58
    }
59

60
    namespace detail {
61
        template <typename Statistic>
62
        struct statistic_get_value;
63

64
        template <>
65
        struct statistic_get_value<boost::accumulators::tag::mean>
66
        {
67
            template <typename Accumulator>
68
            static double call(Accumulator const& accum)
69
            {
70
                return boost::accumulators::mean(accum);
71
            }
72
        };
73

74
        template <>
75
        struct statistic_get_value<boost::accumulators::tag::variance>
76
        {
77
            template <typename Accumulator>
78
            static double call(Accumulator const& accum)
79
            {
80
                return boost::accumulators::variance(accum);
81
            }
82
        };
83

84
        template <>
85
        struct statistic_get_value<boost::accumulators::tag::median>
86
        {
87
            template <typename Accumulator>
88
            static double call(Accumulator const& accum)
89
            {
90
                return boost::accumulators::median(accum);
91
            }
92
        };
93

94
        template <>
95
        struct statistic_get_value<boost::accumulators::tag::min>
96
        {
97
            template <typename Accumulator>
98
            static double call(Accumulator const& accum)
99
            {
100
                return (boost::accumulators::min) (accum);
101
            }
102
        };
103

104
        template <>
105
        struct statistic_get_value<boost::accumulators::tag::max>
106
        {
107
            template <typename Accumulator>
108
            static double call(Accumulator const& accum)
109
            {
110
                return (boost::accumulators::max) (accum);
111
            }
112
        };
113

114
        template <>
115
        struct statistic_get_value<boost::accumulators::tag::count>
116
        {
117
            template <typename Accumulator>
×
118
            static double call(Accumulator const& accum)
119
            {
120
                return static_cast<double>(boost::accumulators::count(accum));
121
            }
122
        };
123
    }    // namespace detail
124

×
125
    template <typename Statistic>
126
    hpx::performance_counters::counter_value
×
127
    arithmetics_counter_extended<Statistic>::get_counter_value(bool /* reset */)
128
    {
129
        std::vector<counter_value> base_values =
130
            counters_.get_counter_values(hpx::launch::sync);
131

132
        // apply arithmetic Statistic
133
        using accumulator_type = boost::accumulators::accumulator_set<double,
134
            boost::accumulators::stats<Statistic>>;
135

×
136
        accumulator_type accum;
137
        for (counter_value const& base_value : base_values)
×
138
        {
139
            accum(base_value.get_value<double>());
140
        }
141
        double value = detail::statistic_get_value<Statistic>::call(accum);
×
142

×
143
        if (base_values[0].scale_inverse_ &&
144
            static_cast<double>(base_values[0].scaling_) != 1.0)    //-V550
×
145
        {
×
146
            base_values[0].value_ = static_cast<std::int64_t>(
147
                value * static_cast<double>(base_values[0].scaling_));
148
        }
149
        else
×
150
        {
×
151
            base_values[0].value_ = static_cast<std::int64_t>(
152
                value / static_cast<double>(base_values[0].scaling_));
153
        }
×
154

×
155
        base_values[0].time_ =
×
156
            static_cast<std::int64_t>(hpx::get_system_uptime());
157
        base_values[0].count_ = counters_.get_invocation_count();
×
158

159
        return base_values[0];
160
    }
161

×
162
    template <typename Statistic>
163
    bool arithmetics_counter_extended<Statistic>::start()
×
164
    {
165
        return counters_.start(hpx::launch::sync);
166
    }
167

×
168
    template <typename Statistic>
169
    bool arithmetics_counter_extended<Statistic>::stop()
×
170
    {
171
        return counters_.stop(hpx::launch::sync);
172
    }
173

×
174
    template <typename Statistic>
175
    void arithmetics_counter_extended<Statistic>::reset_counter_value()
×
176
    {
×
177
        counters_.reset(hpx::launch::sync);
178
    }
179

×
180
    template <typename Statistic>
181
    void arithmetics_counter_extended<Statistic>::finalize()
182
    {
183
        base_performance_counter::finalize();
×
184
        base_type::finalize();
185
    }
186

187
    template <typename Statistic>
×
188
    naming::address
189
    arithmetics_counter_extended<Statistic>::get_current_address() const
190
    {
191
        return naming::address(
192
            naming::get_gid_from_locality_id(agas::get_locality_id()),
×
193
            components::get_component_type<arithmetics_counter_extended>(),
194
            const_cast<arithmetics_counter_extended*>(this));
195
    }
196
}    // namespace hpx::performance_counters::server
197

198
///////////////////////////////////////////////////////////////////////////////
199
template class HPX_EXPORT hpx::performance_counters::server::
200
    arithmetics_counter_extended<boost::accumulators::tag::mean>;
201
template class HPX_EXPORT hpx::performance_counters::server::
202
    arithmetics_counter_extended<boost::accumulators::tag::variance>;
203
template class HPX_EXPORT hpx::performance_counters::server::
204
    arithmetics_counter_extended<boost::accumulators::tag::median>;
205
template class HPX_EXPORT hpx::performance_counters::server::
206
    arithmetics_counter_extended<boost::accumulators::tag::min>;
207
template class HPX_EXPORT hpx::performance_counters::server::
208
    arithmetics_counter_extended<boost::accumulators::tag::max>;
209
template class HPX_EXPORT hpx::performance_counters::server::
210
    arithmetics_counter_extended<boost::accumulators::tag::count>;
211

212
///////////////////////////////////////////////////////////////////////////////
213
// /arithmetic/mean
214
using mean_arithmetics_counter_type =
215
    hpx::components::component<hpx::performance_counters::server::
216
            arithmetics_counter_extended<boost::accumulators::tag::mean>>;
×
217

218
HPX_REGISTER_DERIVED_COMPONENT_FACTORY(mean_arithmetics_counter_type,
219
    mean_arithmetics_counter, "base_performance_counter",
32✔
220
    hpx::components::factory_state::enabled)
221
HPX_DEFINE_GET_COMPONENT_TYPE(mean_arithmetics_counter_type::wrapped_type)
222

223
///////////////////////////////////////////////////////////////////////////////
224
// /arithmetic/variance
225
using variance_arithmetics_counter_type =
226
    hpx::components::component<hpx::performance_counters::server::
227
            arithmetics_counter_extended<boost::accumulators::tag::variance>>;
×
228

229
HPX_REGISTER_DERIVED_COMPONENT_FACTORY(variance_arithmetics_counter_type,
230
    variance_arithmetics_counter, "base_performance_counter",
32✔
231
    hpx::components::factory_state::enabled)
232
HPX_DEFINE_GET_COMPONENT_TYPE(variance_arithmetics_counter_type::wrapped_type)
233

234
///////////////////////////////////////////////////////////////////////////////
235
// /arithmetic/median
236
using median_arithmetics_counter_type =
237
    hpx::components::component<hpx::performance_counters::server::
238
            arithmetics_counter_extended<boost::accumulators::tag::median>>;
×
239

240
HPX_REGISTER_DERIVED_COMPONENT_FACTORY(median_arithmetics_counter_type,
241
    median_arithmetics_counter, "base_performance_counter",
32✔
242
    hpx::components::factory_state::enabled)
243
HPX_DEFINE_GET_COMPONENT_TYPE(median_arithmetics_counter_type::wrapped_type)
244

245
///////////////////////////////////////////////////////////////////////////////
246
// /arithmetic/min
247
using min_arithmetics_counter_type =
248
    hpx::components::component<hpx::performance_counters::server::
249
            arithmetics_counter_extended<boost::accumulators::tag::min>>;
×
250

251
HPX_REGISTER_DERIVED_COMPONENT_FACTORY(min_arithmetics_counter_type,
252
    min_arithmetics_counter, "base_performance_counter",
32✔
253
    hpx::components::factory_state::enabled)
254
HPX_DEFINE_GET_COMPONENT_TYPE(min_arithmetics_counter_type::wrapped_type)
255

256
///////////////////////////////////////////////////////////////////////////////
257
// /arithmetic/max
258
using max_arithmetics_counter_type =
259
    hpx::components::component<hpx::performance_counters::server::
260
            arithmetics_counter_extended<boost::accumulators::tag::max>>;
×
261

262
HPX_REGISTER_DERIVED_COMPONENT_FACTORY(max_arithmetics_counter_type,
263
    max_arithmetics_counter, "base_performance_counter",
32✔
264
    hpx::components::factory_state::enabled)
265
HPX_DEFINE_GET_COMPONENT_TYPE(max_arithmetics_counter_type::wrapped_type)
266

267
///////////////////////////////////////////////////////////////////////////////
268
// /arithmetic/count
269
using count_arithmetics_counter_type =
270
    hpx::components::component<hpx::performance_counters::server::
271
            arithmetics_counter_extended<boost::accumulators::tag::count>>;
×
272

273
HPX_REGISTER_DERIVED_COMPONENT_FACTORY(count_arithmetics_counter_type,
274
    count_arithmetics_counter, "base_performance_counter",
32✔
275
    hpx::components::factory_state::enabled)
276
HPX_DEFINE_GET_COMPONENT_TYPE(count_arithmetics_counter_type::wrapped_type)
277

278
///////////////////////////////////////////////////////////////////////////////
279
namespace hpx::performance_counters::detail {
280

×
281
    // Creation function for aggregating performance counters to be registered
282
    // with the counter types.
283
    naming::gid_type arithmetics_counter_extended_creator(
×
284
        counter_info const& info, error_code& ec)
285
    {
286
        if (info.type_ != counter_type::aggregating)
287
        {
288
            HPX_THROWS_IF(ec, hpx::error::bad_parameter,
×
289
                "arithmetics_counter_extended_creator",
×
290
                "invalid counter type requested");
291
            return naming::invalid_gid;
292
        }
×
293

294
        counter_path_elements paths;
295
        get_counter_path_elements(info.fullname_, paths, ec);
296
        if (ec)
×
297
            return naming::invalid_gid;
×
298

×
299
        if (!paths.parameters_.empty())
300
        {
×
301
            // try to interpret the additional parameter as a list of
302
            // two performance counter names
×
303
            std::vector<std::string> names;
304
            hpx::string_util::split(
305
                names, paths.parameters_, hpx::string_util::is_any_of(","));
306

307
            if (names.empty())
308
            {
309
                HPX_THROWS_IF(ec, hpx::error::bad_parameter,
310
                    "arithmetics_counter_extended_creator",
×
311
                    "the parameter specification for an arithmetic counter "
312
                    "has to expand to at least one counter name: {}",
313
                    paths.parameters_);
314
                return naming::invalid_gid;
×
315
            }
316

317
            for (std::string const& name : names)
×
318
            {
319
                counter_path_elements paths;
320
                if (counter_status::valid_data !=
321
                        get_counter_path_elements(name, paths, ec) ||
322
                    ec)
323
                {
324
                    HPX_THROWS_IF(ec, hpx::error::bad_parameter,
×
325
                        "arithmetics_counter_extended_creator",
326
                        "the given (expanded) counter name is not "
×
327
                        "a validly formed performance counter name: {}",
×
328
                        name);
329
                    return naming::invalid_gid;
330
                }
×
331
            }
332

333
            return create_arithmetics_counter_extended(info, names, ec);
334
        }
335

336
        HPX_THROWS_IF(ec, hpx::error::bad_parameter,
337
            "arithmetics_counter_extended_creator",
×
338
            "the parameter specification for an arithmetic counter "
339
            "has to be a comma separated list of performance "
340
            "counter names, none is given: {}",
341
            remove_counter_prefix(info.fullname_));
×
342
        return naming::invalid_gid;
343
    }
344
}    // namespace hpx::performance_counters::detail
×
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

© 2026 Coveralls, Inc