blob: a08266f618c59240cee6ee35d5aa8f481284a858 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once
#include "time.h"
// Used by various executors to adjust the utilization number reported
// in ExecutorStats. How to use (also see unit test):
//
// (0) Remember that these classes are not thread-safe themselves
//
// (1) Keep one ThreadIdleTracker per worker thread and one
// ExecutorIdleTracker in the executor itself. Note that the
// ExecutorIdleTracker needs the current time as a constructor
// parameter.
//
// (2) Each time a worker thread is blocked; call set_idle with the
// current time right before blocking and call set_active with the
// current time right after waking up again. Pass the result from
// the set_active function to the was_idle function.
//
// (3) Each time stats are sampled, start by sampling the current
// time. Then call ThreadIdleTracker::reset for (at least) all
// blocked worker threads and pass the results to the was_idle
// function. Then call ExecutorIdleTracker::reset with the current
// time and the number of threads as parameters. Subtract this
// result from the utilization of the stats to be reported.
namespace vespalib {
class ThreadIdleTracker {
private:
steady_time _idle_tag = steady_time::min();
public:
bool is_idle() const { return (_idle_tag != steady_time::min()); }
void set_idle(steady_time t) {
if (!is_idle()) {
_idle_tag = t;
}
}
duration set_active(steady_time t) {
if (is_idle()) {
duration how_long_idle = (t - _idle_tag);
_idle_tag = steady_time::min();
return how_long_idle;
} else {
return duration::zero();
}
}
duration reset(steady_time t) {
if (is_idle()) {
duration how_long_idle = (t - _idle_tag);
_idle_tag = t;
return how_long_idle;
} else {
return duration::zero();
}
}
};
class ExecutorIdleTracker {
private:
steady_time _start;
duration _total_idle = duration::zero();
public:
ExecutorIdleTracker(steady_time t) : _start(t) {}
void was_idle(duration how_long_idle) {
_total_idle += how_long_idle;
}
double reset(steady_time t, size_t num_threads) {
double idle = count_ns(_total_idle);
double elapsed = std::max(idle, double(count_ns((t - _start) * num_threads)));
_start = t;
_total_idle = duration::zero();
return (elapsed > 0) ? (idle / elapsed) : 0.0;
}
};
}
|