stat.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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_printf(p, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu "
  80. "%llu\n",
  81. (unsigned long long)cputime64_to_clock_t(user),
  82. (unsigned long long)cputime64_to_clock_t(nice),
  83. (unsigned long long)cputime64_to_clock_t(system),
  84. (unsigned long long)cputime64_to_clock_t(idle),
  85. (unsigned long long)cputime64_to_clock_t(iowait),
  86. (unsigned long long)cputime64_to_clock_t(irq),
  87. (unsigned long long)cputime64_to_clock_t(softirq),
  88. (unsigned long long)cputime64_to_clock_t(steal),
  89. (unsigned long long)cputime64_to_clock_t(guest),
  90. (unsigned long long)cputime64_to_clock_t(guest_nice));
  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,
  104. "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu "
  105. "%llu\n",
  106. i,
  107. (unsigned long long)cputime64_to_clock_t(user),
  108. (unsigned long long)cputime64_to_clock_t(nice),
  109. (unsigned long long)cputime64_to_clock_t(system),
  110. (unsigned long long)cputime64_to_clock_t(idle),
  111. (unsigned long long)cputime64_to_clock_t(iowait),
  112. (unsigned long long)cputime64_to_clock_t(irq),
  113. (unsigned long long)cputime64_to_clock_t(softirq),
  114. (unsigned long long)cputime64_to_clock_t(steal),
  115. (unsigned long long)cputime64_to_clock_t(guest),
  116. (unsigned long long)cputime64_to_clock_t(guest_nice));
  117. }
  118. seq_printf(p, "intr %llu", (unsigned long long)sum);
  119. /* sum again ? it could be updated? */
  120. for_each_irq_nr(j)
  121. seq_printf(p, " %u", kstat_irqs(j));
  122. seq_printf(p,
  123. "\nctxt %llu\n"
  124. "btime %lu\n"
  125. "processes %lu\n"
  126. "procs_running %lu\n"
  127. "procs_blocked %lu\n",
  128. nr_context_switches(),
  129. (unsigned long)jif,
  130. total_forks,
  131. nr_running(),
  132. nr_iowait());
  133. seq_printf(p, "softirq %llu", (unsigned long long)sum_softirq);
  134. for (i = 0; i < NR_SOFTIRQS; i++)
  135. seq_printf(p, " %u", per_softirq_sums[i]);
  136. seq_putc(p, '\n');
  137. return 0;
  138. }
  139. static int stat_open(struct inode *inode, struct file *file)
  140. {
  141. unsigned size = 4096 * (1 + num_possible_cpus() / 32);
  142. char *buf;
  143. struct seq_file *m;
  144. int res;
  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 = size;
  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);