Willy Tarreau | 609aad9 | 2018-11-22 08:31:09 +0100 | [diff] [blame] | 1 | /* |
| 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 Tarreau | baba82f | 2018-11-22 08:42:42 +0100 | [diff] [blame] | 29 | #include <proto/freq_ctr.h> |
Willy Tarreau | 609aad9 | 2018-11-22 08:31:09 +0100 | [diff] [blame] | 30 | |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 31 | /* bit fields for "profiling" */ |
Willy Tarreau | d2d3348 | 2019-04-25 17:09:07 +0200 | [diff] [blame] | 32 | #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 Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 36 | |
| 37 | extern unsigned int profiling; |
Willy Tarreau | d9add3a | 2019-04-25 08:57:41 +0200 | [diff] [blame] | 38 | extern unsigned long task_profiling_mask; |
Willy Tarreau | 609aad9 | 2018-11-22 08:31:09 +0100 | [diff] [blame] | 39 | extern struct activity activity[MAX_THREADS]; |
| 40 | |
| 41 | |
| 42 | void 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 Tarreau | baba82f | 2018-11-22 08:42:42 +0100 | [diff] [blame] | 46 | * 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 Tarreau | 609aad9 | 2018-11-22 08:31:09 +0100 | [diff] [blame] | 48 | */ |
| 49 | static inline void activity_count_runtime() |
| 50 | { |
| 51 | uint64_t new_mono_time; |
| 52 | uint64_t new_cpu_time; |
| 53 | int64_t stolen; |
Willy Tarreau | baba82f | 2018-11-22 08:42:42 +0100 | [diff] [blame] | 54 | uint32_t run_time; |
Willy Tarreau | d2d3348 | 2019-04-25 17:09:07 +0200 | [diff] [blame] | 55 | 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 Tarreau | 609aad9 | 2018-11-22 08:31:09 +0100 | [diff] [blame] | 62 | |
| 63 | new_cpu_time = now_cpu_time(); |
| 64 | new_mono_time = now_mono_time(); |
| 65 | |
| 66 | if (prev_cpu_time && prev_mono_time) { |
| 67 | new_cpu_time -= prev_cpu_time; |
| 68 | new_mono_time -= prev_mono_time; |
| 69 | 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 Tarreau | baba82f | 2018-11-22 08:42:42 +0100 | [diff] [blame] | 78 | |
| 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 Tarreau | d9add3a | 2019-04-25 08:57:41 +0200 | [diff] [blame] | 81 | |
Willy Tarreau | d2d3348 | 2019-04-25 17:09:07 +0200 | [diff] [blame] | 82 | /* 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 Tarreau | d9add3a | 2019-04-25 08:57:41 +0200 | [diff] [blame] | 86 | if (!(task_profiling_mask & tid_bit)) { |
Willy Tarreau | d2d3348 | 2019-04-25 17:09:07 +0200 | [diff] [blame] | 87 | 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 Tarreau | d9add3a | 2019-04-25 08:57:41 +0200 | [diff] [blame] | 92 | } else { |
Willy Tarreau | d2d3348 | 2019-04-25 17:09:07 +0200 | [diff] [blame] | 93 | 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 Tarreau | d9add3a | 2019-04-25 08:57:41 +0200 | [diff] [blame] | 98 | } |
Willy Tarreau | 609aad9 | 2018-11-22 08:31:09 +0100 | [diff] [blame] | 99 | } |
| 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 | */ |