stat.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include <linux/cpumask.h>
  2. #include <linux/fs.h>
  3. #include <linux/init.h>
  4. #include <linux/interrupt.h>
  5. #include <linux/kernel_stat.h>
  6. #include <linux/proc_fs.h>
  7. #include <linux/sched.h>
  8. #include <linux/seq_file.h>
  9. #include <linux/slab.h>
  10. #include <linux/time.h>
  11. #include <linux/irqnr.h>
  12. #include <linux/cputime.h>
  13. #include <linux/tick.h>
  14. #ifndef arch_irq_stat_cpu
  15. #define arch_irq_stat_cpu(cpu) 0
  16. #endif
  17. #ifndef arch_irq_stat
  18. #define arch_irq_stat() 0
  19. #endif
  20. #ifdef arch_idle_time
  21. static cputime64_t get_idle_time(int cpu)
  22. {
  23. cputime64_t idle;
  24. idle = kcpustat_cpu(cpu).cpustat[CPUTIME_IDLE];
  25. if (cpu_online(cpu) && !nr_iowait_cpu(cpu))
  26. idle += arch_idle_time(cpu);
  27. return idle;
  28. }
  29. static cputime64_t get_iowait_time(int cpu)
  30. {
  31. cputime64_t iowait;
  32. iowait = kcpustat_cpu(cpu).cpustat[CPUTIME_IOWAIT];
  33. if (cpu_online(cpu) && nr_iowait_cpu(cpu))
  34. iowait += arch_idle_time(cpu);
  35. return iowait;
  36. }
  37. #else
  38. static u64 get_idle_time(int cpu)
  39. {
  40. u64 idle, idle_time = -1ULL;
  41. if (cpu_online(cpu))
  42. idle_time = get_cpu_idle_time_us(cpu, NULL);
  43. if (idle_time == -1ULL)
  44. /* !NO_HZ or cpu offline so we can rely on cpustat.idle */
  45. idle = kcpustat_cpu(cpu).cpustat[CPUTIME_IDLE];
  46. else
  47. idle = usecs_to_cputime64(idle_time);
  48. return idle;
  49. }
  50. static u64 get_iowait_time(int cpu)
  51. {
  52. u64 iowait, iowait_time = -1ULL;
  53. if (cpu_online(cpu))
  54. iowait_time = get_cpu_iowait_time_us(cpu, NULL);
  55. if (iowait_time == -1ULL)
  56. /* !NO_HZ or cpu offline so we can rely on cpustat.iowait */
  57. iowait = kcpustat_cpu(cpu).cpustat[CPUTIME_IOWAIT];
  58. else
  59. iowait = usecs_to_cputime64(iowait_time);
  60. return iowait;
  61. }
  62. #endif
  63. static int show_stat(struct seq_file *p, void *v)
  64. {
  65. int i, j;
  66. u64 user, nice, system, idle, iowait, irq, softirq, steal;
  67. u64 guest, guest_nice;
  68. u64 sum = 0;
  69. u64 sum_softirq = 0;
  70. unsigned int per_softirq_sums[NR_SOFTIRQS] = {0};
  71. struct timespec64 boottime;
  72. user = nice = system = idle = iowait =
  73. irq = softirq = steal = 0;
  74. guest = guest_nice = 0;
  75. getboottime64(&boottime);
  76. for_each_possible_cpu(i) {
  77. user += kcpustat_cpu(i).cpustat[CPUTIME_USER];
  78. nice += kcpustat_cpu(i).cpustat[CPUTIME_NICE];
  79. system += kcpustat_cpu(i).cpustat[CPUTIME_SYSTEM];
  80. idle += get_idle_time(i);
  81. iowait += get_iowait_time(i);
  82. irq += kcpustat_cpu(i).cpustat[CPUTIME_IRQ];
  83. softirq += kcpustat_cpu(i).cpustat[CPUTIME_SOFTIRQ];
  84. steal += kcpustat_cpu(i).cpustat[CPUTIME_STEAL];
  85. guest += kcpustat_cpu(i).cpustat[CPUTIME_GUEST];
  86. guest_nice += kcpustat_cpu(i).cpustat[CPUTIME_GUEST_NICE];
  87. sum += kstat_cpu_irqs_sum(i);
  88. sum += arch_irq_stat_cpu(i);
  89. for (j = 0; j < NR_SOFTIRQS; j++) {
  90. unsigned int softirq_stat = kstat_softirqs_cpu(j, i);
  91. per_softirq_sums[j] += softirq_stat;
  92. sum_softirq += softirq_stat;
  93. }
  94. }
  95. sum += arch_irq_stat();
  96. seq_put_decimal_ull(p, "cpu ", cputime64_to_clock_t(user));
  97. seq_put_decimal_ull(p, " ", cputime64_to_clock_t(nice));
  98. seq_put_decimal_ull(p, " ", cputime64_to_clock_t(system));
  99. seq_put_decimal_ull(p, " ", cputime64_to_clock_t(idle));
  100. seq_put_decimal_ull(p, " ", cputime64_to_clock_t(iowait));
  101. seq_put_decimal_ull(p, " ", cputime64_to_clock_t(irq));
  102. seq_put_decimal_ull(p, " ", cputime64_to_clock_t(softirq));
  103. seq_put_decimal_ull(p, " ", cputime64_to_clock_t(steal));
  104. seq_put_decimal_ull(p, " ", cputime64_to_clock_t(guest));
  105. seq_put_decimal_ull(p, " ", cputime64_to_clock_t(guest_nice));
  106. seq_putc(p, '\n');
  107. for_each_online_cpu(i) {
  108. /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
  109. user = kcpustat_cpu(i).cpustat[CPUTIME_USER];
  110. nice = kcpustat_cpu(i).cpustat[CPUTIME_NICE];
  111. system = kcpustat_cpu(i).cpustat[CPUTIME_SYSTEM];
  112. idle = get_idle_time(i);
  113. iowait = get_iowait_time(i);
  114. irq = kcpustat_cpu(i).cpustat[CPUTIME_IRQ];
  115. softirq = kcpustat_cpu(i).cpustat[CPUTIME_SOFTIRQ];
  116. steal = kcpustat_cpu(i).cpustat[CPUTIME_STEAL];
  117. guest = kcpustat_cpu(i).cpustat[CPUTIME_GUEST];
  118. guest_nice = kcpustat_cpu(i).cpustat[CPUTIME_GUEST_NICE];
  119. seq_printf(p, "cpu%d", i);
  120. seq_put_decimal_ull(p, " ", cputime64_to_clock_t(user));
  121. seq_put_decimal_ull(p, " ", cputime64_to_clock_t(nice));
  122. seq_put_decimal_ull(p, " ", cputime64_to_clock_t(system));
  123. seq_put_decimal_ull(p, " ", cputime64_to_clock_t(idle));
  124. seq_put_decimal_ull(p, " ", cputime64_to_clock_t(iowait));
  125. seq_put_decimal_ull(p, " ", cputime64_to_clock_t(irq));
  126. seq_put_decimal_ull(p, " ", cputime64_to_clock_t(softirq));
  127. seq_put_decimal_ull(p, " ", cputime64_to_clock_t(steal));
  128. seq_put_decimal_ull(p, " ", cputime64_to_clock_t(guest));
  129. seq_put_decimal_ull(p, " ", cputime64_to_clock_t(guest_nice));
  130. seq_putc(p, '\n');
  131. }
  132. seq_put_decimal_ull(p, "intr ", (unsigned long long)sum);
  133. /* sum again ? it could be updated? */
  134. for_each_irq_nr(j)
  135. seq_put_decimal_ull(p, " ", kstat_irqs_usr(j));
  136. seq_printf(p,
  137. "\nctxt %llu\n"
  138. "btime %llu\n"
  139. "processes %lu\n"
  140. "procs_running %lu\n"
  141. "procs_blocked %lu\n",
  142. nr_context_switches(),
  143. (unsigned long long)boottime.tv_sec,
  144. total_forks,
  145. nr_running(),
  146. nr_iowait());
  147. seq_put_decimal_ull(p, "softirq ", (unsigned long long)sum_softirq);
  148. for (i = 0; i < NR_SOFTIRQS; i++)
  149. seq_put_decimal_ull(p, " ", per_softirq_sums[i]);
  150. seq_putc(p, '\n');
  151. return 0;
  152. }
  153. static int stat_open(struct inode *inode, struct file *file)
  154. {
  155. size_t size = 1024 + 128 * num_online_cpus();
  156. /* minimum size to display an interrupt count : 2 bytes */
  157. size += 2 * nr_irqs;
  158. return single_open_size(file, show_stat, NULL, size);
  159. }
  160. static const struct file_operations proc_stat_operations = {
  161. .open = stat_open,
  162. .read = seq_read,
  163. .llseek = seq_lseek,
  164. .release = single_release,
  165. };
  166. static int __init proc_stat_init(void)
  167. {
  168. proc_create("stat", 0, NULL, &proc_stat_operations);
  169. return 0;
  170. }
  171. fs_initcall(proc_stat_init);