blob: 922f493924b97eebebcd7ef9fa22c6cf71d00aa0 [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
Willy Tarreau75c62c22018-11-22 11:02:09 +010031/* bit fields for "profiling" */
32#define HA_PROF_TASKS 0x00000001 /* enable per-task CPU profiling */
33
34extern unsigned int profiling;
Willy Tarreaud9add3a2019-04-25 08:57:41 +020035extern unsigned long task_profiling_mask;
Willy Tarreau609aad92018-11-22 08:31:09 +010036extern struct activity activity[MAX_THREADS];
37
38
39void report_stolen_time(uint64_t stolen);
40
41/* Collect date and time information before calling poll(). This will be used
42 * to count the run time of the past loop and the sleep time of the next poll.
Willy Tarreaubaba82f2018-11-22 08:42:42 +010043 * It also makes use of the just updated before_poll timer to count the loop's
44 * run time and feed the average loop time metric (in microseconds).
Willy Tarreau609aad92018-11-22 08:31:09 +010045 */
46static inline void activity_count_runtime()
47{
48 uint64_t new_mono_time;
49 uint64_t new_cpu_time;
50 int64_t stolen;
Willy Tarreaubaba82f2018-11-22 08:42:42 +010051 uint32_t run_time;
Willy Tarreau609aad92018-11-22 08:31:09 +010052
53 new_cpu_time = now_cpu_time();
54 new_mono_time = now_mono_time();
55
56 if (prev_cpu_time && prev_mono_time) {
57 new_cpu_time -= prev_cpu_time;
58 new_mono_time -= prev_mono_time;
59 stolen = new_mono_time - new_cpu_time;
60 if (unlikely(stolen >= 500000)) {
61 stolen /= 500000;
62 /* more than half a millisecond difference might
63 * indicate an undesired preemption.
64 */
65 report_stolen_time(stolen);
66 }
67 }
Willy Tarreaubaba82f2018-11-22 08:42:42 +010068
69 run_time = (before_poll.tv_sec - after_poll.tv_sec) * 1000000U + (before_poll.tv_usec - after_poll.tv_usec);
70 swrate_add(&activity[tid].avg_loop_us, TIME_STATS_SAMPLES, run_time);
Willy Tarreaud9add3a2019-04-25 08:57:41 +020071
72 if (!(task_profiling_mask & tid_bit)) {
73 if (unlikely(profiling & HA_PROF_TASKS))
74 _HA_ATOMIC_OR(&task_profiling_mask, tid_bit);
75 } else {
76 if (unlikely(!(profiling & HA_PROF_TASKS)))
77 _HA_ATOMIC_AND(&task_profiling_mask, ~tid_bit);
78 }
Willy Tarreau609aad92018-11-22 08:31:09 +010079}
80
81
82#endif /* _PROTO_ACTIVITY_H */
83
84/*
85 * Local variables:
86 * c-indent-level: 8
87 * c-basic-offset: 8
88 * End:
89 */