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" */ |
| 32 | #define HA_PROF_TASKS 0x00000001 /* enable per-task CPU profiling */ |
| 33 | |
| 34 | extern unsigned int profiling; |
Willy Tarreau | 609aad9 | 2018-11-22 08:31:09 +0100 | [diff] [blame] | 35 | extern struct activity activity[MAX_THREADS]; |
| 36 | |
| 37 | |
| 38 | void report_stolen_time(uint64_t stolen); |
| 39 | |
| 40 | /* Collect date and time information before calling poll(). This will be used |
| 41 | * 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] | 42 | * It also makes use of the just updated before_poll timer to count the loop's |
| 43 | * run time and feed the average loop time metric (in microseconds). |
Willy Tarreau | 609aad9 | 2018-11-22 08:31:09 +0100 | [diff] [blame] | 44 | */ |
| 45 | static inline void activity_count_runtime() |
| 46 | { |
| 47 | uint64_t new_mono_time; |
| 48 | uint64_t new_cpu_time; |
| 49 | int64_t stolen; |
Willy Tarreau | baba82f | 2018-11-22 08:42:42 +0100 | [diff] [blame] | 50 | uint32_t run_time; |
Willy Tarreau | 609aad9 | 2018-11-22 08:31:09 +0100 | [diff] [blame] | 51 | |
| 52 | new_cpu_time = now_cpu_time(); |
| 53 | new_mono_time = now_mono_time(); |
| 54 | |
| 55 | if (prev_cpu_time && prev_mono_time) { |
| 56 | new_cpu_time -= prev_cpu_time; |
| 57 | new_mono_time -= prev_mono_time; |
| 58 | stolen = new_mono_time - new_cpu_time; |
| 59 | if (unlikely(stolen >= 500000)) { |
| 60 | stolen /= 500000; |
| 61 | /* more than half a millisecond difference might |
| 62 | * indicate an undesired preemption. |
| 63 | */ |
| 64 | report_stolen_time(stolen); |
| 65 | } |
| 66 | } |
Willy Tarreau | baba82f | 2018-11-22 08:42:42 +0100 | [diff] [blame] | 67 | |
| 68 | run_time = (before_poll.tv_sec - after_poll.tv_sec) * 1000000U + (before_poll.tv_usec - after_poll.tv_usec); |
| 69 | swrate_add(&activity[tid].avg_loop_us, TIME_STATS_SAMPLES, run_time); |
Willy Tarreau | 609aad9 | 2018-11-22 08:31:09 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | |
| 73 | #endif /* _PROTO_ACTIVITY_H */ |
| 74 | |
| 75 | /* |
| 76 | * Local variables: |
| 77 | * c-indent-level: 8 |
| 78 | * c-basic-offset: 8 |
| 79 | * End: |
| 80 | */ |