stat.c 5.4 KB

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