stat.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 <asm/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. #ifndef arch_idle_time
  21. #define arch_idle_time(cpu) 0
  22. #endif
  23. static u64 get_idle_time(int cpu)
  24. {
  25. u64 idle, idle_time = get_cpu_idle_time_us(cpu, NULL);
  26. if (idle_time == -1ULL) {
  27. /* !NO_HZ so we can rely on cpustat.idle */
  28. idle = kcpustat_cpu(cpu).cpustat[CPUTIME_IDLE];
  29. idle += arch_idle_time(cpu);
  30. } else
  31. idle = usecs_to_cputime64(idle_time);
  32. return idle;
  33. }
  34. static u64 get_iowait_time(int cpu)
  35. {
  36. u64 iowait, iowait_time = get_cpu_iowait_time_us(cpu, NULL);
  37. if (iowait_time == -1ULL)
  38. /* !NO_HZ so we can rely on cpustat.iowait */
  39. iowait = kcpustat_cpu(cpu).cpustat[CPUTIME_IOWAIT];
  40. else
  41. iowait = usecs_to_cputime64(iowait_time);
  42. return iowait;
  43. }
  44. static int show_stat(struct seq_file *p, void *v)
  45. {
  46. int i, j;
  47. unsigned long jif;
  48. u64 user, nice, system, idle, iowait, irq, softirq, steal;
  49. u64 guest, guest_nice;
  50. u64 sum = 0;
  51. u64 sum_softirq = 0;
  52. unsigned int per_softirq_sums[NR_SOFTIRQS] = {0};
  53. struct timespec boottime;
  54. user = nice = system = idle = iowait =
  55. irq = softirq = steal = 0;
  56. guest = guest_nice = 0;
  57. getboottime(&boottime);
  58. jif = boottime.tv_sec;
  59. for_each_possible_cpu(i) {
  60. user += kcpustat_cpu(i).cpustat[CPUTIME_USER];
  61. nice += kcpustat_cpu(i).cpustat[CPUTIME_NICE];
  62. system += kcpustat_cpu(i).cpustat[CPUTIME_SYSTEM];
  63. idle += get_idle_time(i);
  64. iowait += get_iowait_time(i);
  65. irq += kcpustat_cpu(i).cpustat[CPUTIME_IRQ];
  66. softirq += kcpustat_cpu(i).cpustat[CPUTIME_SOFTIRQ];
  67. steal += kcpustat_cpu(i).cpustat[CPUTIME_STEAL];
  68. guest += kcpustat_cpu(i).cpustat[CPUTIME_GUEST];
  69. guest_nice += kcpustat_cpu(i).cpustat[CPUTIME_GUEST_NICE];
  70. sum += kstat_cpu_irqs_sum(i);
  71. sum += arch_irq_stat_cpu(i);
  72. for (j = 0; j < NR_SOFTIRQS; j++) {
  73. unsigned int softirq_stat = kstat_softirqs_cpu(j, i);
  74. per_softirq_sums[j] += softirq_stat;
  75. sum_softirq += softirq_stat;
  76. }
  77. }
  78. sum += arch_irq_stat();
  79. seq_puts(p, "cpu ");
  80. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(user));
  81. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(nice));
  82. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(system));
  83. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(idle));
  84. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(iowait));
  85. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(irq));
  86. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(softirq));
  87. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(steal));
  88. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(guest));
  89. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(guest_nice));
  90. seq_putc(p, '\n');
  91. for_each_online_cpu(i) {
  92. /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
  93. user = kcpustat_cpu(i).cpustat[CPUTIME_USER];
  94. nice = kcpustat_cpu(i).cpustat[CPUTIME_NICE];
  95. system = kcpustat_cpu(i).cpustat[CPUTIME_SYSTEM];
  96. idle = get_idle_time(i);
  97. iowait = get_iowait_time(i);
  98. irq = kcpustat_cpu(i).cpustat[CPUTIME_IRQ];
  99. softirq = kcpustat_cpu(i).cpustat[CPUTIME_SOFTIRQ];
  100. steal = kcpustat_cpu(i).cpustat[CPUTIME_STEAL];
  101. guest = kcpustat_cpu(i).cpustat[CPUTIME_GUEST];
  102. guest_nice = kcpustat_cpu(i).cpustat[CPUTIME_GUEST_NICE];
  103. seq_printf(p, "cpu%d", i);
  104. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(user));
  105. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(nice));
  106. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(system));
  107. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(idle));
  108. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(iowait));
  109. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(irq));
  110. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(softirq));
  111. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(steal));
  112. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(guest));
  113. seq_put_decimal_ull(p, ' ', cputime64_to_clock_t(guest_nice));
  114. seq_putc(p, '\n');
  115. }
  116. seq_printf(p, "intr %llu", (unsigned long long)sum);
  117. /* sum again ? it could be updated? */
  118. for_each_irq_nr(j)
  119. seq_put_decimal_ull(p, ' ', kstat_irqs(j));
  120. seq_printf(p,
  121. "\nctxt %llu\n"
  122. "btime %lu\n"
  123. "processes %lu\n"
  124. "procs_running %lu\n"
  125. "procs_blocked %lu\n",
  126. nr_context_switches(),
  127. (unsigned long)jif,
  128. total_forks,
  129. nr_running(),
  130. nr_iowait());
  131. seq_printf(p, "softirq %llu", (unsigned long long)sum_softirq);
  132. for (i = 0; i < NR_SOFTIRQS; i++)
  133. seq_put_decimal_ull(p, ' ', per_softirq_sums[i]);
  134. seq_putc(p, '\n');
  135. return 0;
  136. }
  137. static int stat_open(struct inode *inode, struct file *file)
  138. {
  139. unsigned size = 1024 + 128 * num_possible_cpus();
  140. char *buf;
  141. struct seq_file *m;
  142. int res;
  143. /* minimum size to display an interrupt count : 2 bytes */
  144. size += 2 * nr_irqs;
  145. /* don't ask for more than the kmalloc() max size */
  146. if (size > KMALLOC_MAX_SIZE)
  147. size = KMALLOC_MAX_SIZE;
  148. buf = kmalloc(size, GFP_KERNEL);
  149. if (!buf)
  150. return -ENOMEM;
  151. res = single_open(file, show_stat, NULL);
  152. if (!res) {
  153. m = file->private_data;
  154. m->buf = buf;
  155. m->size = ksize(buf);
  156. } else
  157. kfree(buf);
  158. return res;
  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. module_init(proc_stat_init);