kernel_stat.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. NR_STATS,
  29. };
  30. struct kernel_cpustat {
  31. u64 cpustat[NR_STATS];
  32. };
  33. struct kernel_stat {
  34. unsigned long irqs_sum;
  35. unsigned int softirqs[NR_SOFTIRQS];
  36. };
  37. DECLARE_PER_CPU(struct kernel_stat, kstat);
  38. DECLARE_PER_CPU(struct kernel_cpustat, kernel_cpustat);
  39. /* Must have preemption disabled for this to be meaningful. */
  40. #define kstat_this_cpu this_cpu_ptr(&kstat)
  41. #define kcpustat_this_cpu this_cpu_ptr(&kernel_cpustat)
  42. #define kstat_cpu(cpu) per_cpu(kstat, cpu)
  43. #define kcpustat_cpu(cpu) per_cpu(kernel_cpustat, cpu)
  44. extern unsigned long long nr_context_switches(void);
  45. extern unsigned int kstat_irqs_cpu(unsigned int irq, int cpu);
  46. extern void kstat_incr_irq_this_cpu(unsigned int irq);
  47. static inline void kstat_incr_softirqs_this_cpu(unsigned int irq)
  48. {
  49. __this_cpu_inc(kstat.softirqs[irq]);
  50. }
  51. static inline unsigned int kstat_softirqs_cpu(unsigned int irq, int cpu)
  52. {
  53. return kstat_cpu(cpu).softirqs[irq];
  54. }
  55. /*
  56. * Number of interrupts per specific IRQ source, since bootup
  57. */
  58. extern unsigned int kstat_irqs(unsigned int irq);
  59. extern unsigned int kstat_irqs_usr(unsigned int irq);
  60. /*
  61. * Number of interrupts per cpu, since bootup
  62. */
  63. static inline unsigned int kstat_cpu_irqs_sum(unsigned int cpu)
  64. {
  65. return kstat_cpu(cpu).irqs_sum;
  66. }
  67. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  68. extern u64 kcpustat_field(struct kernel_cpustat *kcpustat,
  69. enum cpu_usage_stat usage, int cpu);
  70. extern void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu);
  71. #else
  72. static inline u64 kcpustat_field(struct kernel_cpustat *kcpustat,
  73. enum cpu_usage_stat usage, int cpu)
  74. {
  75. return kcpustat->cpustat[usage];
  76. }
  77. static inline void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu)
  78. {
  79. *dst = kcpustat_cpu(cpu);
  80. }
  81. #endif
  82. extern void account_user_time(struct task_struct *, u64);
  83. extern void account_guest_time(struct task_struct *, u64);
  84. extern void account_system_time(struct task_struct *, int, u64);
  85. extern void account_system_index_time(struct task_struct *, u64,
  86. enum cpu_usage_stat);
  87. extern void account_steal_time(u64);
  88. extern void account_idle_time(u64);
  89. extern u64 get_idle_time(struct kernel_cpustat *kcs, int cpu);
  90. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  91. static inline void account_process_tick(struct task_struct *tsk, int user)
  92. {
  93. vtime_flush(tsk);
  94. }
  95. #else
  96. extern void account_process_tick(struct task_struct *, int user);
  97. #endif
  98. extern void account_idle_ticks(unsigned long ticks);
  99. #endif /* _LINUX_KERNEL_STAT_H */