• 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

0.0
/examples/performance_counters/sine/sine_client.cpp
1
//  Copyright (c) 2007-2012 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
#if !defined(HPX_COMPUTE_DEVICE_CODE)
9
#include <hpx/hpx.hpp>
10
#include <hpx/hpx_init.hpp>
11
#include <hpx/include/util.hpp>
12

13
#include <hpx/modules/program_options.hpp>
14

15
#include <cstdint>
16
#include <iostream>
17
#include <string>
18
#include <vector>
19

20
///////////////////////////////////////////////////////////////////////////////
21
// This example demonstrates the creation and use of different types of
22
// performance counters. It utilizes the sine component, which implements two
23
// of the demonstrated counters.
24
//
25
// The example prints the values of the counters after every fixed amount of
26
// time. This time is configurable by a command line option --pause=N, where
27
// N is the amount of milliseconds to wait in between the sample points. The
28
// default value is 500ms.
29
//
30
// Here is a short description of each of the counters:
31
//
32
// /sine{locality#0/instance#0}/immediate/explicit
33
//    This is a custom performance counter fully implemented in the sine
34
//    component. It evaluates a new value every 1000ms (1s) and delivers this
35
//    value whenever it is queried.
36
//
37
// /sine{locality#0/total}/immediate/implicit
38
//    This is an immediate counter implemented by registering a function with
39
//    the counter framework. This function will be called whenever the counter
40
//    value is queried. It calculates the current value of a sine based on the
41
//    uptime of the counter.
42
//
43
// /statistics{/sine{locality#0/instance#1}/immediate/explicit}/average#100
44
//    This is an aggregating counter calculating the average value of a given
45
//    base counter (in this case /sine{locality#0/instance#1}/immediate/explicit,
46
//    i.e. a second instance of the explicit counter). The base counter is
47
//    evaluated every 100ms (as specified by the trailing parameter in the
48
//    counter name). No special code in the sine example is needed for this
49
//    counter as it reuses the explicit counter and the predefined averaging
50
//    performance counter implemented in HPX.
51
//
52
// Additionally, this example demonstrates starting and stopping performance
53
// counters. It stops the evaluation of the first explicit counter instance
54
// every 5 seconds, restarting it after a while.
55

56
///////////////////////////////////////////////////////////////////////////////
57
int monitor(std::uint64_t pause, std::uint64_t values)
×
58
{
59
    // Create the performances counters using their symbolic name.
60
    std::uint32_t const prefix = hpx::get_locality_id();
×
61

62
    using hpx::performance_counters::performance_counter;
63
    performance_counter sine_explicit(hpx::util::format(
×
64
        "/sine{{locality#{}/instance#{}}}/immediate/explicit", prefix, 0));
×
65
    performance_counter sine_implicit(hpx::util::format(
×
66
        "/sine{{locality#{}/total}}/immediate/implicit", prefix));
×
67
    performance_counter sine_average(
68
        hpx::util::format("/statistics{{/sine{{locality#{}/instance#{}}}/"
×
69
                          "immediate/explicit}}/average@100",
70
            prefix, 1));
×
71

72
    // We need to explicitly start all counters before we can use them. For
73
    // certain counters this could be a no-op, in which case start will return
74
    // 'false'.
75
    sine_explicit.start();
×
76
    sine_implicit.start();
×
77
    sine_average.start();
×
78

79
    // retrieve the counter values
80
    std::uint64_t start_time = 0;
81
    bool started = true;
82
    while (values-- > 0)
×
83
    {
84
        // Query the performance counter.
85
        using hpx::performance_counters::counter_value;
86
        using hpx::performance_counters::status_is_valid;
87

88
        counter_value value1 =
89
            sine_explicit.get_counter_value(hpx::launch::sync);
×
90
        counter_value value2 =
91
            sine_implicit.get_counter_value(hpx::launch::sync);
×
92
        counter_value value3 =
93
            sine_average.get_counter_value(hpx::launch::sync);
×
94

95
        if (status_is_valid(value1.status_))
×
96
        {
97
            if (!start_time)
×
98
                start_time = value2.time_;
×
99

100
            hpx::util::format_to(std::cout, "{:.3}: {:.4}, {:.4}, {:.4}\n",
×
101
                static_cast<double>(value2.time_ - start_time) * 1e-9,
×
102
                value1.get_value<double>(),
×
103
                value2.get_value<double>() / 100000.,
×
104
                value3.get_value<double>());
×
105
        }
106

107
        // stop/restart the sine_explicit counter after every 5 seconds of
108
        // evaluation
109
        bool should_run =
×
110
            (int(static_cast<double>(value2.time_ - start_time) * 1e-9) / 5) %
×
111
                2 !=
112
            0;
113
        if (should_run == started)
×
114
        {
115
            if (started)
×
116
            {
117
                sine_explicit.stop();
×
118
                started = false;
119
            }
120
            else
121
            {
122
                sine_explicit.start();
×
123
                started = true;
124
            }
125
        }
126

127
        // give up control to the thread manager, we will be resumed after
128
        // 'pause' ms
129
        hpx::this_thread::suspend(std::chrono::milliseconds(pause));
×
130
    }
131
    return 0;
×
132
}
133

134
///////////////////////////////////////////////////////////////////////////////
135
int hpx_main(hpx::program_options::variables_map& vm)
×
136
{
137
    // retrieve the command line arguments
138
    std::uint64_t const pause = vm["pause"].as<std::uint64_t>();
×
139
    std::uint64_t const values = vm["values"].as<std::uint64_t>();
×
140

141
    // do main work, i.e. query the performance counters
142
    std::cout << "starting sine monitoring..." << std::endl;
143

144
    int result = 0;
145
    try
146
    {
147
        result = monitor(pause, values);
×
148
    }
149
    catch (hpx::exception const& e)
×
150
    {
151
        std::cerr << "sine_client: caught exception: " << e.what() << std::endl;
×
152
        std::cerr << "Have you specified the command line option "
153
                     "--sine to enable the sine component?"
154
                  << std::endl;
155
    }
×
156

157
    // Initiate shutdown of the runtime system.
158
    hpx::finalize();
159
    return result;
×
160
}
161

162
///////////////////////////////////////////////////////////////////////////////
163
int main(int argc, char* argv[])
×
164
{
165
    using hpx::program_options::options_description;
166
    using hpx::program_options::value;
167

168
    // Configure application-specific options.
169
    options_description desc_commandline("usage: sine_client [options]");
×
170
    desc_commandline.add_options()("pause",
×
171
        value<std::uint64_t>()->default_value(500),
×
172
        "milliseconds between each performance counter query")("values",
×
173
        value<std::uint64_t>()->default_value(100),
×
174
        "number of performance counter queries to perform");
175

176
    std::vector<std::string> cfg = {"hpx.components.sine.enabled! = 1"};
×
177

178
    // Initialize and run HPX.
179
    hpx::init_params init_args;
×
180
    init_args.desc_cmdline = desc_commandline;
×
181
    init_args.cfg = cfg;
×
182

183
    return hpx::init(argc, argv, init_args);
×
184
}
×
185
#endif
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