stat.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. unsigned long jif;
  67. u64 user, nice, system, idle, iowait, irq, softirq, steal;
  68. u64 guest, guest_nice;
  69. u64 sum = 0;
  70. u64 sum_softirq = 0;
  71. unsigned int per_softirq_sums[NR_SOFTIRQS] = {0};
  72. struct timespec boottime;
  73. user = nice = system = idle = iowait =
  74. irq = softirq = steal = 0;
  75. guest = guest_nice = 0;
  76. getboottime(&boottime);
  77. jif = boottime.tv_sec;
  78. for_each_possible_cpu(i) {
  79. user += kcpustat_cpu(i).cpustat[CPUTIME_USER];
  80. nice += kcpustat_cpu(i).cpustat[CPUTIME_NICE];
  81. system += kcpustat_cpu(i).cpustat[CPUTIME_SYSTEM];
  82. idle += get_idle_time(i);
  83. iowait += get_iowait_time(i);
  84. irq += kcpustat_cpu(i).cpustat[CPUTIME_IRQ];
  85. softirq += kcpustat_cpu(i).cpustat[CPUTIME_SOFTIRQ];
  86. steal += kcpustat_cpu(i).cpustat[CPUTIME_STEAL];
  87. guest += kcpustat_cpu(i).cpustat[CPUTIME_GUEST];
  88. guest_nice += kcpustat_cpu(i).cpustat[CPUTIME_GUEST_NICE];
  89. sum += kstat_cpu_irqs_sum(i);
  90. sum += arch_irq_stat_cpu(i);
  91. for (j = 0; j < NR_SOFTIRQS; j++) {
  92. unsigned int softirq_stat = kstat_softirqs_cpu(j, i);
  93. per_softirq_sums[j] += softirq_stat;
  94. sum_softirq += softirq_stat;
  95. }
  96. }
  97. sum += arch_irq_stat();
  98. seq_puts(p, "cpu ");
  99. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(user));
  100. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(nice));
  101. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(system));
  102. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(idle));
  103. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(iowait));
  104. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(irq));
  105. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(softirq));
  106. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(steal));
  107. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(guest));
  108. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(guest_nice));
  109. seq_putc(p, '\n');
  110. for_each_online_cpu(i) {
  111. /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
  112. user = kcpustat_cpu(i).cpustat[CPUTIME_USER];
  113. nice = kcpustat_cpu(i).cpustat[CPUTIME_NICE];
  114. system = kcpustat_cpu(i).cpustat[CPUTIME_SYSTEM];
  115. idle = get_idle_time(i);
  116. iowait = get_iowait_time(i);
  117. irq = kcpustat_cpu(i).cpustat[CPUTIME_IRQ];
  118. softirq = kcpustat_cpu(i).cpustat[CPUTIME_SOFTIRQ];
  119. steal = kcpustat_cpu(i).cpustat[CPUTIME_STEAL];
  120. guest = kcpustat_cpu(i).cpustat[CPUTIME_GUEST];
  121. guest_nice = kcpustat_cpu(i).cpustat[CPUTIME_GUEST_NICE];
  122. seq_printf(p, "cpu%d", i);
  123. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(user));
  124. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(nice));
  125. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(system));
  126. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(idle));
  127. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(iowait));
  128. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(irq));
  129. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(softirq));
  130. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(steal));
  131. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(guest));
  132. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(guest_nice));
  133. seq_putc(p, '\n');
  134. }
  135. seq_printf(p, "intr %llu", (unsigned long long)sum);
  136. /* sum again ? it could be updated? */
  137. for_each_irq_nr(j)
  138. seq_put_decimal_ull(p, ' ', kstat_irqs_usr(j));
  139. seq_printf(p,
  140. "\nctxt %llu\n"
  141. "btime %lu\n"
  142. "processes %lu\n"
  143. "procs_running %lu\n"
  144. "procs_blocked %lu\n",
  145. nr_context_switches(),
  146. (unsigned long)jif,
  147. total_forks,
  148. nr_running(),
  149. nr_iowait());
  150. seq_printf(p, "softirq %llu", (unsigned long long)sum_softirq);
  151. for (i = 0; i < NR_SOFTIRQS; i++)
  152. seq_put_decimal_ull(p, ' ', per_softirq_sums[i]);
  153. seq_putc(p, '\n');
  154. return 0;
  155. }
  156. static int stat_open(struct inode *inode, struct file *file)
  157. {
  158. size_t size = 1024 + 128 * num_online_cpus();
  159. /* minimum size to display an interrupt count : 2 bytes */
  160. size += 2 * nr_irqs;
  161. return single_open_size(file, show_stat, NULL, size);
  162. }
  163. static const struct file_operations proc_stat_operations = {
  164. .open = stat_open,
  165. .read = seq_read,
  166. .llseek = seq_lseek,
  167. .release = single_release,
  168. };
  169. static int __init proc_stat_init(void)
  170. {
  171. proc_create("stat", 0, NULL, &proc_stat_operations);
  172. return 0;
  173. }
  174. fs_initcall(proc_stat_init);