| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | /* cpustats.h -- a library for checking the CPU utilization |
| 3 | |
| 4 | This program is free software: you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by |
| 6 | the Free Software Foundation, either version 3 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 16 | |
| 17 | #ifndef _CPUSTATS_H |
| 18 | #define _CPUSTATS_H |
| 19 | |
| 20 | #include <sys/sysinfo.h> |
| 21 | |
| 22 | #ifdef __cplusplus |
| 23 | extern "C" |
| 24 | { |
| 25 | #endif |
| 26 | |
| 27 | typedef unsigned long long jiff; |
| 28 | |
| 29 | struct cpu_time |
| 30 | { |
| 31 | /* The name of the cpu found in /proc/stat: cpu, cpu0, cpu1, ... */ |
| 32 | const char *cpuname; |
| 33 | /* Time spent running non-kernel code. (user time, including nice time) */ |
| 34 | jiff user; |
| 35 | /* Time spent in user mode with low priority (nice) */ |
| 36 | jiff nice; |
| 37 | /* Time spent running kernel code. (system time) */ |
| 38 | jiff system; |
| 39 | /* Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time */ |
| 40 | jiff idle; |
| 41 | /* Time spent waiting for IO. Prior to Linux 2.5.41, included in idle */ |
| 42 | jiff iowait; |
| 43 | /* Time servicing interrupts. (since Linux 2.6.0-test4) */ |
| 44 | jiff irq; |
| 45 | /* Time servicing softirqs.i (since Linux 2.6.0-test4) */ |
| 46 | jiff softirq; |
| 47 | /* Stolen time, which is the time spent in other operating systems |
| 48 | * when running in a virtualized environment. (since Linux 2.6.11) */ |
| 49 | jiff steal; |
| 50 | /* Time spent running a virtual CPU for guest operating systems |
| 51 | * under the control of the Linux kernel. (since Linux 2.6.24) */ |
| 52 | jiff guest; |
| 53 | /* Time spent running a niced guest (virtual CPU for guest |
| 54 | * operating systems under the control of the Linux kernel). |
| 55 | * (since Linux 2.6.33) */ |
| 56 | jiff guestn; |
| 57 | }; |
| 58 | |
| 59 | /* Return the PATH of the proc stat filesystem ("/proc/stat"), or the content |
| 60 | of the environment variable "NPL_TESTING_PATH_PROC_STAT" if set */ |
| 61 | const char *get_path_proc_stat (); |
| 62 | |
| 63 | /* Get the cpu time statistics |
| 64 | * lines = 1 --> 'cpu' only |
| 65 | * lines = 3 --> 'cpu', 'cpu0', 'cpu1' |
| 66 | * and so on */ |
| 67 | void cpu_stats_get_time (struct cpu_time * __restrict cputime, |
| 68 | unsigned int lines); |
| 69 | |
| 70 | /* Get the number of context switches that the system underwent */ |
| 71 | unsigned long long cpu_stats_get_cswch (); |
| 72 | |
| 73 | /* Get the number of interrupts serviced since boot time, for each of the |
| 74 | * possible system interrupts, including unnumbered architecture specific |
| 75 | * interrupts (since Linux 2.6.0-test4) */ |
| 76 | unsigned long long cpu_stats_get_intr (); |
| 77 | |
| 78 | /* Get the total of softirqs the system has experienced |
| 79 | * (since Linux 2.6.0-test4) */ |
| 80 | unsigned long long cpu_stats_get_softirq (); |
| 81 | |
| 82 | #ifdef __cplusplus |
| 83 | } |
| 84 | #endif |
| 85 | |
| 86 | #endif /* _CPUSTATS_H */ |