blob: 717d6678e64ec11b62d2b8962ff0a1ac461f9f9f [file] [log] [blame]
Willy Tarreau609aad92018-11-22 08:31:09 +01001/*
2 * include/proto/activity.h
3 * This file contains macros and inline functions for activity measurements.
4 *
5 * Copyright (C) 2000-2018 Willy Tarreau - w@1wt.eu
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#ifndef _PROTO_ACTIVITY_H
23#define _PROTO_ACTIVITY_H
24
25#include <common/config.h>
26#include <common/hathreads.h>
27#include <common/time.h>
28#include <types/activity.h>
Willy Tarreaubaba82f2018-11-22 08:42:42 +010029#include <proto/freq_ctr.h>
Willy Tarreau609aad92018-11-22 08:31:09 +010030
31extern struct activity activity[MAX_THREADS];
32
33
34void report_stolen_time(uint64_t stolen);
35
36/* Collect date and time information before calling poll(). This will be used
37 * to count the run time of the past loop and the sleep time of the next poll.
Willy Tarreaubaba82f2018-11-22 08:42:42 +010038 * It also makes use of the just updated before_poll timer to count the loop's
39 * run time and feed the average loop time metric (in microseconds).
Willy Tarreau609aad92018-11-22 08:31:09 +010040 */
41static inline void activity_count_runtime()
42{
43 uint64_t new_mono_time;
44 uint64_t new_cpu_time;
45 int64_t stolen;
Willy Tarreaubaba82f2018-11-22 08:42:42 +010046 uint32_t run_time;
Willy Tarreau609aad92018-11-22 08:31:09 +010047
48 new_cpu_time = now_cpu_time();
49 new_mono_time = now_mono_time();
50
51 if (prev_cpu_time && prev_mono_time) {
52 new_cpu_time -= prev_cpu_time;
53 new_mono_time -= prev_mono_time;
54 stolen = new_mono_time - new_cpu_time;
55 if (unlikely(stolen >= 500000)) {
56 stolen /= 500000;
57 /* more than half a millisecond difference might
58 * indicate an undesired preemption.
59 */
60 report_stolen_time(stolen);
61 }
62 }
Willy Tarreaubaba82f2018-11-22 08:42:42 +010063
64 run_time = (before_poll.tv_sec - after_poll.tv_sec) * 1000000U + (before_poll.tv_usec - after_poll.tv_usec);
65 swrate_add(&activity[tid].avg_loop_us, TIME_STATS_SAMPLES, run_time);
Willy Tarreau609aad92018-11-22 08:31:09 +010066}
67
68
69#endif /* _PROTO_ACTIVITY_H */
70
71/*
72 * Local variables:
73 * c-indent-level: 8
74 * c-basic-offset: 8
75 * End:
76 */