1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 21da177e4SLinus Torvalds #ifndef _LINUX_KERNEL_STAT_H 31da177e4SLinus Torvalds #define _LINUX_KERNEL_STAT_H 41da177e4SLinus Torvalds 51da177e4SLinus Torvalds #include <linux/smp.h> 61da177e4SLinus Torvalds #include <linux/threads.h> 71da177e4SLinus Torvalds #include <linux/percpu.h> 828ef3584SIngo Molnar #include <linux/cpumask.h> 9aa0ce5bbSKeika Kobayashi #include <linux/interrupt.h> 103292beb3SGlauber Costa #include <linux/sched.h> 11dcbf832eSFrederic Weisbecker #include <linux/vtime.h> 126859a840SAlan Mayer #include <asm/irq.h> 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds /* 151da177e4SLinus Torvalds * 'kernel_stat.h' contains the definitions needed for doing 161da177e4SLinus Torvalds * some kernel statistics (CPU usage, context switches ...), 171da177e4SLinus Torvalds * used by rstatd/perfmeter 181da177e4SLinus Torvalds */ 191da177e4SLinus Torvalds 203292beb3SGlauber Costa enum cpu_usage_stat { 213292beb3SGlauber Costa CPUTIME_USER, 223292beb3SGlauber Costa CPUTIME_NICE, 233292beb3SGlauber Costa CPUTIME_SYSTEM, 243292beb3SGlauber Costa CPUTIME_SOFTIRQ, 253292beb3SGlauber Costa CPUTIME_IRQ, 263292beb3SGlauber Costa CPUTIME_IDLE, 273292beb3SGlauber Costa CPUTIME_IOWAIT, 283292beb3SGlauber Costa CPUTIME_STEAL, 293292beb3SGlauber Costa CPUTIME_GUEST, 303292beb3SGlauber Costa CPUTIME_GUEST_NICE, 311fcf54deSJosh Don #ifdef CONFIG_SCHED_CORE 321fcf54deSJosh Don CPUTIME_FORCEIDLE, 331fcf54deSJosh Don #endif 343292beb3SGlauber Costa NR_STATS, 353292beb3SGlauber Costa }; 363292beb3SGlauber Costa 373292beb3SGlauber Costa struct kernel_cpustat { 383292beb3SGlauber Costa u64 cpustat[NR_STATS]; 391da177e4SLinus Torvalds }; 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds struct kernel_stat { 42f2c66cd8SKAMEZAWA Hiroyuki unsigned long irqs_sum; 43aa0ce5bbSKeika Kobayashi unsigned int softirqs[NR_SOFTIRQS]; 441da177e4SLinus Torvalds }; 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds DECLARE_PER_CPU(struct kernel_stat, kstat); 473292beb3SGlauber Costa DECLARE_PER_CPU(struct kernel_cpustat, kernel_cpustat); 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds /* Must have preemption disabled for this to be meaningful. */ 504a32fea9SChristoph Lameter #define kstat_this_cpu this_cpu_ptr(&kstat) 514a32fea9SChristoph Lameter #define kcpustat_this_cpu this_cpu_ptr(&kernel_cpustat) 523292beb3SGlauber Costa #define kstat_cpu(cpu) per_cpu(kstat, cpu) 533292beb3SGlauber Costa #define kcpustat_cpu(cpu) per_cpu(kernel_cpustat, cpu) 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds extern unsigned long long nr_context_switches(void); 561da177e4SLinus Torvalds 570b8f1efaSYinghai Lu extern unsigned int kstat_irqs_cpu(unsigned int irq, int cpu); 58792d0018SThomas Gleixner extern void kstat_incr_irq_this_cpu(unsigned int irq); 59d52a61c0SYinghai Lu 60aa0ce5bbSKeika Kobayashi static inline void kstat_incr_softirqs_this_cpu(unsigned int irq) 61aa0ce5bbSKeika Kobayashi { 626c9ae009SEric Dumazet __this_cpu_inc(kstat.softirqs[irq]); 63aa0ce5bbSKeika Kobayashi } 64aa0ce5bbSKeika Kobayashi 65aa0ce5bbSKeika Kobayashi static inline unsigned int kstat_softirqs_cpu(unsigned int irq, int cpu) 66aa0ce5bbSKeika Kobayashi { 67aa0ce5bbSKeika Kobayashi return kstat_cpu(cpu).softirqs[irq]; 68aa0ce5bbSKeika Kobayashi } 69aa0ce5bbSKeika Kobayashi 70*3ca0a6eaSZhen Lei static inline unsigned int kstat_cpu_softirqs_sum(int cpu) 71*3ca0a6eaSZhen Lei { 72*3ca0a6eaSZhen Lei int i; 73*3ca0a6eaSZhen Lei unsigned int sum = 0; 74*3ca0a6eaSZhen Lei 75*3ca0a6eaSZhen Lei for (i = 0; i < NR_SOFTIRQS; i++) 76*3ca0a6eaSZhen Lei sum += kstat_softirqs_cpu(i, cpu); 77*3ca0a6eaSZhen Lei 78*3ca0a6eaSZhen Lei return sum; 79*3ca0a6eaSZhen Lei } 80*3ca0a6eaSZhen Lei 811da177e4SLinus Torvalds /* 821da177e4SLinus Torvalds * Number of interrupts per specific IRQ source, since bootup 831da177e4SLinus Torvalds */ 84c291ee62SThomas Gleixner extern unsigned int kstat_irqs_usr(unsigned int irq); 851da177e4SLinus Torvalds 86f2c66cd8SKAMEZAWA Hiroyuki /* 87f2c66cd8SKAMEZAWA Hiroyuki * Number of interrupts per cpu, since bootup 88f2c66cd8SKAMEZAWA Hiroyuki */ 8947904aedSZhen Lei static inline unsigned long kstat_cpu_irqs_sum(unsigned int cpu) 90f2c66cd8SKAMEZAWA Hiroyuki { 91f2c66cd8SKAMEZAWA Hiroyuki return kstat_cpu(cpu).irqs_sum; 92f2c66cd8SKAMEZAWA Hiroyuki } 93aa9c4c0fSIngo Molnar 9464eea63cSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN 9564eea63cSFrederic Weisbecker extern u64 kcpustat_field(struct kernel_cpustat *kcpustat, 9664eea63cSFrederic Weisbecker enum cpu_usage_stat usage, int cpu); 9774722bb2SFrederic Weisbecker extern void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu); 9864eea63cSFrederic Weisbecker #else 9964eea63cSFrederic Weisbecker static inline u64 kcpustat_field(struct kernel_cpustat *kcpustat, 10064eea63cSFrederic Weisbecker enum cpu_usage_stat usage, int cpu) 10164eea63cSFrederic Weisbecker { 10264eea63cSFrederic Weisbecker return kcpustat->cpustat[usage]; 10364eea63cSFrederic Weisbecker } 10474722bb2SFrederic Weisbecker 10574722bb2SFrederic Weisbecker static inline void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu) 10674722bb2SFrederic Weisbecker { 10774722bb2SFrederic Weisbecker *dst = kcpustat_cpu(cpu); 10874722bb2SFrederic Weisbecker } 10974722bb2SFrederic Weisbecker 11064eea63cSFrederic Weisbecker #endif 11164eea63cSFrederic Weisbecker 11223244a5cSFrederic Weisbecker extern void account_user_time(struct task_struct *, u64); 113fb8b049cSFrederic Weisbecker extern void account_guest_time(struct task_struct *, u64); 114fb8b049cSFrederic Weisbecker extern void account_system_time(struct task_struct *, int, u64); 115fb8b049cSFrederic Weisbecker extern void account_system_index_time(struct task_struct *, u64, 116c31cc6a5SFrederic Weisbecker enum cpu_usage_stat); 117be9095edSFrederic Weisbecker extern void account_steal_time(u64); 11818b43a9bSFrederic Weisbecker extern void account_idle_time(u64); 119a130e8fbSJosh Don extern u64 get_idle_time(struct kernel_cpustat *kcs, int cpu); 12079741dd3SMartin Schwidefsky 121abf917cdSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE 122bcebdf84SFrederic Weisbecker static inline void account_process_tick(struct task_struct *tsk, int user) 123bcebdf84SFrederic Weisbecker { 124c8d7dabfSFrederic Weisbecker vtime_flush(tsk); 125bcebdf84SFrederic Weisbecker } 126bcebdf84SFrederic Weisbecker #else 12779741dd3SMartin Schwidefsky extern void account_process_tick(struct task_struct *, int user); 128bcebdf84SFrederic Weisbecker #endif 129bcebdf84SFrederic Weisbecker 13079741dd3SMartin Schwidefsky extern void account_idle_ticks(unsigned long ticks); 1311da177e4SLinus Torvalds 1321fcf54deSJosh Don #ifdef CONFIG_SCHED_CORE 1331fcf54deSJosh Don extern void __account_forceidle_time(struct task_struct *tsk, u64 delta); 1341fcf54deSJosh Don #endif 1351fcf54deSJosh Don 1361da177e4SLinus Torvalds #endif /* _LINUX_KERNEL_STAT_H */ 137