blob: 673d274578db4d01cd35f3f6dc063f5bc121a890 [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" */
Willy Tarreaud2d33482019-04-25 17:09:07 +020032#define HA_PROF_TASKS_OFF 0x00000000 /* per-task CPU profiling forced disabled */
33#define HA_PROF_TASKS_AUTO 0x00000001 /* per-task CPU profiling automatic */
34#define HA_PROF_TASKS_ON 0x00000002 /* per-task CPU profiling forced enabled */
35#define HA_PROF_TASKS_MASK 0x00000003 /* per-task CPU profiling mask */
Willy Tarreau75c62c22018-11-22 11:02:09 +010036
37extern unsigned int profiling;
Willy Tarreaud9add3a2019-04-25 08:57:41 +020038extern unsigned long task_profiling_mask;
Willy Tarreau609aad92018-11-22 08:31:09 +010039extern struct activity activity[MAX_THREADS];
40
41
42void report_stolen_time(uint64_t stolen);
43
44/* Collect date and time information before calling poll(). This will be used
45 * to count the run time of the past loop and the sleep time of the next poll.
Willy Tarreaubaba82f2018-11-22 08:42:42 +010046 * It also makes use of the just updated before_poll timer to count the loop's
47 * run time and feed the average loop time metric (in microseconds).
Willy Tarreau609aad92018-11-22 08:31:09 +010048 */
49static inline void activity_count_runtime()
50{
51 uint64_t new_mono_time;
52 uint64_t new_cpu_time;
53 int64_t stolen;
Willy Tarreaubaba82f2018-11-22 08:42:42 +010054 uint32_t run_time;
Willy Tarreaud2d33482019-04-25 17:09:07 +020055 uint32_t up, down;
56
57 /* 1 millisecond per loop on average over last 1024 iterations is
58 * enough to turn on profiling.
59 */
60 up = 1000;
61 down = up * 99 / 100;
Willy Tarreau609aad92018-11-22 08:31:09 +010062
63 new_cpu_time = now_cpu_time();
64 new_mono_time = now_mono_time();
65
Willy Tarreau81036f22019-05-20 19:24:50 +020066 if (ti->prev_cpu_time && ti->prev_mono_time) {
67 new_cpu_time -= ti->prev_cpu_time;
68 new_mono_time -= ti->prev_mono_time;
Willy Tarreau609aad92018-11-22 08:31:09 +010069 stolen = new_mono_time - new_cpu_time;
70 if (unlikely(stolen >= 500000)) {
71 stolen /= 500000;
72 /* more than half a millisecond difference might
73 * indicate an undesired preemption.
74 */
75 report_stolen_time(stolen);
76 }
77 }
Willy Tarreaubaba82f2018-11-22 08:42:42 +010078
79 run_time = (before_poll.tv_sec - after_poll.tv_sec) * 1000000U + (before_poll.tv_usec - after_poll.tv_usec);
80 swrate_add(&activity[tid].avg_loop_us, TIME_STATS_SAMPLES, run_time);
Willy Tarreaud9add3a2019-04-25 08:57:41 +020081
Willy Tarreaud2d33482019-04-25 17:09:07 +020082 /* reaching the "up" threshold on average switches profiling to "on"
83 * when automatic, and going back below the "down" threshold switches
84 * to off.
85 */
Willy Tarreaud9add3a2019-04-25 08:57:41 +020086 if (!(task_profiling_mask & tid_bit)) {
Willy Tarreaud2d33482019-04-25 17:09:07 +020087 if (unlikely((profiling & HA_PROF_TASKS_MASK) == HA_PROF_TASKS_ON ||
88 ((profiling & HA_PROF_TASKS_MASK) == HA_PROF_TASKS_AUTO && run_time >= up))) {
89 if (swrate_avg(activity[tid].avg_loop_us, TIME_STATS_SAMPLES) >= up)
90 _HA_ATOMIC_OR(&task_profiling_mask, tid_bit);
91 }
Willy Tarreaud9add3a2019-04-25 08:57:41 +020092 } else {
Willy Tarreaud2d33482019-04-25 17:09:07 +020093 if (unlikely((profiling & HA_PROF_TASKS_MASK) == HA_PROF_TASKS_OFF ||
94 ((profiling & HA_PROF_TASKS_MASK) == HA_PROF_TASKS_AUTO && run_time <= down))) {
95 if (swrate_avg(activity[tid].avg_loop_us, TIME_STATS_SAMPLES) <= down)
96 _HA_ATOMIC_AND(&task_profiling_mask, ~tid_bit);
97 }
Willy Tarreaud9add3a2019-04-25 08:57:41 +020098 }
Willy Tarreau609aad92018-11-22 08:31:09 +010099}
100
101
102#endif /* _PROTO_ACTIVITY_H */
103
104/*
105 * Local variables:
106 * c-indent-level: 8
107 * c-basic-offset: 8
108 * End:
109 */