Debian Codegraph @

src/util/metrics.c

raw
1 /*
2 * Metrics Helper
3 *
4 * The metrics object is used to compute the min/max/avg/std deviation of samples of
5 * CPU time, in fixed size and without memory allocations.
6 *
7 * The values of min/max/avg are meant to be read out of the struct directly, whereas
8 * the standard deviation can only be accessed using a helper function (as it is not
9 * actually stored directly, but computed on-demand).
10 *
11 * Only one sample may be active at any point in time, and every sample that is started,
12 * must be stopped.
13 *
14 * See `Note on a Method for Calculating Corrected Sums of Squares and Products' by
15 * W. P. Welford, 1962.
16 */
17
18 #include <c-stdaux.h>
19 #include <math.h>
20 #include <stdlib.h>
21 #include <time.h>
22 #include "util/metrics.h"
23
24 void metrics_init(Metrics *metrics, clockid_t id) {
25 *metrics = (Metrics)METRICS_INIT(id);
26 }
27
28 void metrics_deinit(Metrics *metrics) {
29 c_assert(metrics->timestamp == METRICS_TIMESTAMP_INVALID);
30 metrics_init(metrics, metrics->id);
31 }
32
33 /**
34 * metrics_get_time() - get the current thread CPU time
35 *
36 * Read the current thread CPU time to be used to record samples.
37 *
38 * Return: the timestamp in nano seconds.
39 */
40 uint64_t metrics_get_time(Metrics *metrics) {
41 struct timespec ts;
42 int r;
43
44 r = clock_gettime(metrics->id, &ts);
45 c_assert(r >= 0);
46
47 return ts.tv_sec * UINT64_C(1000000000) + ts.tv_nsec;
48 }
49
50 /**
51 * metrics_sample_add() - add one sample
52 * @metrics: object to operate on
53 * @timestamp: time the sample was started
54 *
55 * Update the internal state with a new sample, started at @timestamp
56 * and ending at the time the function is called.
57 */
58 void metrics_sample_add(Metrics *metrics, uint64_t timestamp) {
59 uint64_t sample, average_old;
60
61 sample = metrics_get_time(metrics) - timestamp;
62
63 metrics->count ++;
64 metrics->sum += sample;
65
66 average_old = metrics->average;
67 metrics->average = metrics->sum / metrics->count;
68 metrics->sum_of_squares += (sample - average_old) * (sample - metrics->average);
69
70 if (metrics->minimum > sample)
71 metrics->minimum = sample;
72
73 if (metrics->maximum < sample)
74 metrics->maximum = sample;
75 }
76
77 /**
78 * metrics_sample_start() - start a new sample
79 * @metrics: object to operate on
80 *
81 * Start a new sample by recording the current timestamp, verifying that
82 * a sample is not currently running.
83 */
84 void metrics_sample_start(Metrics *metrics) {
85 c_assert(metrics->timestamp == METRICS_TIMESTAMP_INVALID);
86 metrics->timestamp = metrics_get_time(metrics);
87 }
88
89 /**
90 * metrics_sample_end() - end a running sample
91 * @metrics: object to operate on
92 *
93 * End a currently running sample, and update the internal state.
94 */
95 void metrics_sample_end(Metrics *metrics) {
96 c_assert(metrics->timestamp != METRICS_TIMESTAMP_INVALID);
97
98 metrics_sample_add(metrics, metrics->timestamp);
99
100 metrics->timestamp = METRICS_TIMESTAMP_INVALID;
101 }
102
103 /**
104 * metrics_read_standard_deviation() - read out the current standard deviation
105 * @metrics: objcet to operate on
106 *
107 * This computes and returns the standard deviation of the samples recorded
108 * so far. The standard devitaion is not stored internally, but computed
109 * on-demand.
110 *
111 * If the standard deviation is not defined (no samples were taken), then
112 * zero is returned.
113 *
114 * Return: the standard deviation, or 0 if not defined.
115 */
116 double metrics_read_standard_deviation(Metrics *metrics) {
117 if (!metrics->count)
118 return 0;
119
120 return sqrt(metrics->sum_of_squares / metrics->count);
121 }