kernel_stat.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_KERNEL_STAT_H
  3. #define _LINUX_KERNEL_STAT_H
  4. #include <linux/smp.h>
  5. #include <linux/threads.h>
  6. #include <linux/percpu.h>
  7. #include <linux/cpumask.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/sched.h>
  10. #include <linux/vtime.h>
  11. #include <asm/irq.h>
  12. /*
  13. * 'kernel_stat.h' contains the definitions needed for doing
  14. * some kernel statistics (CPU usage, context switches ...),
  15. * used by rstatd/perfmeter
  16. */
  17. enum cpu_usage_stat {
  18. CPUTIME_USER,
  19. CPUTIME_NICE,
  20. CPUTIME_SYSTEM,
  21. CPUTIME_SOFTIRQ,
  22. CPUTIME_IRQ,
  23. CPUTIME_IDLE,
  24. CPUTIME_IOWAIT,
  25. CPUTIME_STEAL,
  26. CPUTIME_GUEST,
  27. CPUTIME_GUEST_NICE,
  28. #ifdef CONFIG_SCHED_CORE
  29. CPUTIME_FORCEIDLE,
  30. #endif
  31. NR_STATS,
  32. };
  33. struct kernel_cpustat {
  34. u64 cpustat[NR_STATS];
  35. };
  36. struct kernel_stat {
  37. unsigned long irqs_sum;
  38. unsigned int softirqs[NR_SOFTIRQS];
  39. };
  40. DECLARE_PER_CPU(struct kernel_stat, kstat);
  41. DECLARE_PER_CPU(struct kernel_cpustat, kernel_cpustat);
  42. /* Must have preemption disabled for this to be meaningful. */
  43. #define kstat_this_cpu this_cpu_ptr(&kstat)
  44. #define kcpustat_this_cpu this_cpu_ptr(&kernel_cpustat)
  45. #define kstat_cpu(cpu) per_cpu(kstat, cpu)
  46. #define kcpustat_cpu(cpu) per_cpu(kernel_cpustat, cpu)
  47. extern unsigned long long nr_context_switches(void);
  48. extern unsigned int kstat_irqs_cpu(unsigned int irq, int cpu);
  49. extern void kstat_incr_irq_this_cpu(unsigned int irq);
  50. static inline void kstat_incr_softirqs_this_cpu(unsigned int irq)
  51. {
  52. __this_cpu_inc(kstat.softirqs[irq]);
  53. }
  54. static inline unsigned int kstat_softirqs_cpu(unsigned int irq, int cpu)
  55. {
  56. return kstat_cpu(cpu).softirqs[irq];
  57. }
  58. /*
  59. * Number of interrupts per specific IRQ source, since bootup
  60. */
  61. extern unsigned int kstat_irqs_usr(unsigned int irq);
  62. /*
  63. * Number of interrupts per cpu, since bootup
  64. */
  65. static inline unsigned long kstat_cpu_irqs_sum(unsigned int cpu)
  66. {
  67. return kstat_cpu(cpu).irqs_sum;
  68. }
  69. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  70. extern u64 kcpustat_field(struct kernel_cpustat *kcpustat,
  71. enum cpu_usage_stat usage, int cpu);
  72. extern void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu);
  73. #else
  74. static inline u64 kcpustat_field(struct kernel_cpustat *kcpustat,
  75. enum cpu_usage_stat usage, int cpu)
  76. {
  77. return kcpustat->cpustat[usage];
  78. }
  79. static inline void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu)
  80. {
  81. *dst = kcpustat_cpu(cpu);
  82. }
  83. #endif
  84. extern void account_user_time(struct task_struct *, u64);
  85. extern void account_guest_time(struct task_struct *, u64);
  86. extern void account_system_time(struct task_struct *, int, u64);
  87. extern void account_system_index_time(struct task_struct *, u64,
  88. enum cpu_usage_stat);
  89. extern void account_steal_time(u64);
  90. extern void account_idle_time(u64);
  91. extern u64 get_idle_time(struct kernel_cpustat *kcs, int cpu);
  92. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  93. static inline void account_process_tick(struct task_struct *tsk, int user)
  94. {
  95. vtime_flush(tsk);
  96. }
  97. #else
  98. extern void account_process_tick(struct task_struct *, int user);
  99. #endif
  100. extern void account_idle_ticks(unsigned long ticks);
  101. #ifdef CONFIG_SCHED_CORE
  102. extern void __account_forceidle_time(struct task_struct *tsk, u64 delta);
  103. #endif
  104. #endif /* _LINUX_KERNEL_STAT_H */