stat.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include <linux/cpumask.h>
  2. #include <linux/fs.h>
  3. #include <linux/gfp.h>
  4. #include <linux/init.h>
  5. #include <linux/interrupt.h>
  6. #include <linux/kernel_stat.h>
  7. #include <linux/proc_fs.h>
  8. #include <linux/sched.h>
  9. #include <linux/seq_file.h>
  10. #include <linux/slab.h>
  11. #include <linux/time.h>
  12. #include <asm/cputime.h>
  13. #ifndef arch_irq_stat_cpu
  14. #define arch_irq_stat_cpu(cpu) 0
  15. #endif
  16. #ifndef arch_irq_stat
  17. #define arch_irq_stat() 0
  18. #endif
  19. static int show_stat(struct seq_file *p, void *v)
  20. {
  21. int i, j;
  22. unsigned long jif;
  23. cputime64_t user, nice, system, idle, iowait, irq, softirq, steal;
  24. cputime64_t guest;
  25. u64 sum = 0;
  26. struct timespec boottime;
  27. unsigned int per_irq_sum;
  28. struct irq_desc *desc;
  29. user = nice = system = idle = iowait =
  30. irq = softirq = steal = cputime64_zero;
  31. guest = cputime64_zero;
  32. getboottime(&boottime);
  33. jif = boottime.tv_sec;
  34. for_each_possible_cpu(i) {
  35. user = cputime64_add(user, kstat_cpu(i).cpustat.user);
  36. nice = cputime64_add(nice, kstat_cpu(i).cpustat.nice);
  37. system = cputime64_add(system, kstat_cpu(i).cpustat.system);
  38. idle = cputime64_add(idle, kstat_cpu(i).cpustat.idle);
  39. iowait = cputime64_add(iowait, kstat_cpu(i).cpustat.iowait);
  40. irq = cputime64_add(irq, kstat_cpu(i).cpustat.irq);
  41. softirq = cputime64_add(softirq, kstat_cpu(i).cpustat.softirq);
  42. steal = cputime64_add(steal, kstat_cpu(i).cpustat.steal);
  43. guest = cputime64_add(guest, kstat_cpu(i).cpustat.guest);
  44. for_each_irq_nr(j) {
  45. #ifdef CONFIG_SPARSE_IRQ
  46. desc = irq_to_desc(j);
  47. if (!desc)
  48. continue;
  49. #endif
  50. sum += kstat_irqs_cpu(j, i);
  51. }
  52. sum += arch_irq_stat_cpu(i);
  53. }
  54. sum += arch_irq_stat();
  55. seq_printf(p, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
  56. (unsigned long long)cputime64_to_clock_t(user),
  57. (unsigned long long)cputime64_to_clock_t(nice),
  58. (unsigned long long)cputime64_to_clock_t(system),
  59. (unsigned long long)cputime64_to_clock_t(idle),
  60. (unsigned long long)cputime64_to_clock_t(iowait),
  61. (unsigned long long)cputime64_to_clock_t(irq),
  62. (unsigned long long)cputime64_to_clock_t(softirq),
  63. (unsigned long long)cputime64_to_clock_t(steal),
  64. (unsigned long long)cputime64_to_clock_t(guest));
  65. for_each_online_cpu(i) {
  66. /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
  67. user = kstat_cpu(i).cpustat.user;
  68. nice = kstat_cpu(i).cpustat.nice;
  69. system = kstat_cpu(i).cpustat.system;
  70. idle = kstat_cpu(i).cpustat.idle;
  71. iowait = kstat_cpu(i).cpustat.iowait;
  72. irq = kstat_cpu(i).cpustat.irq;
  73. softirq = kstat_cpu(i).cpustat.softirq;
  74. steal = kstat_cpu(i).cpustat.steal;
  75. guest = kstat_cpu(i).cpustat.guest;
  76. seq_printf(p,
  77. "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
  78. i,
  79. (unsigned long long)cputime64_to_clock_t(user),
  80. (unsigned long long)cputime64_to_clock_t(nice),
  81. (unsigned long long)cputime64_to_clock_t(system),
  82. (unsigned long long)cputime64_to_clock_t(idle),
  83. (unsigned long long)cputime64_to_clock_t(iowait),
  84. (unsigned long long)cputime64_to_clock_t(irq),
  85. (unsigned long long)cputime64_to_clock_t(softirq),
  86. (unsigned long long)cputime64_to_clock_t(steal),
  87. (unsigned long long)cputime64_to_clock_t(guest));
  88. }
  89. seq_printf(p, "intr %llu", (unsigned long long)sum);
  90. /* sum again ? it could be updated? */
  91. for_each_irq_nr(j) {
  92. per_irq_sum = 0;
  93. #ifdef CONFIG_SPARSE_IRQ
  94. desc = irq_to_desc(j);
  95. if (!desc) {
  96. seq_printf(p, " %u", per_irq_sum);
  97. continue;
  98. }
  99. #endif
  100. for_each_possible_cpu(i)
  101. per_irq_sum += kstat_irqs_cpu(j, i);
  102. seq_printf(p, " %u", per_irq_sum);
  103. }
  104. seq_printf(p,
  105. "\nctxt %llu\n"
  106. "btime %lu\n"
  107. "processes %lu\n"
  108. "procs_running %lu\n"
  109. "procs_blocked %lu\n",
  110. nr_context_switches(),
  111. (unsigned long)jif,
  112. total_forks,
  113. nr_running(),
  114. nr_iowait());
  115. return 0;
  116. }
  117. static int stat_open(struct inode *inode, struct file *file)
  118. {
  119. unsigned size = 4096 * (1 + num_possible_cpus() / 32);
  120. char *buf;
  121. struct seq_file *m;
  122. int res;
  123. /* don't ask for more than the kmalloc() max size, currently 128 KB */
  124. if (size > 128 * 1024)
  125. size = 128 * 1024;
  126. buf = kmalloc(size, GFP_KERNEL);
  127. if (!buf)
  128. return -ENOMEM;
  129. res = single_open(file, show_stat, NULL);
  130. if (!res) {
  131. m = file->private_data;
  132. m->buf = buf;
  133. m->size = size;
  134. } else
  135. kfree(buf);
  136. return res;
  137. }
  138. static const struct file_operations proc_stat_operations = {
  139. .open = stat_open,
  140. .read = seq_read,
  141. .llseek = seq_lseek,
  142. .release = single_release,
  143. };
  144. static int __init proc_stat_init(void)
  145. {
  146. proc_create("stat", 0, NULL, &proc_stat_operations);
  147. return 0;
  148. }
  149. module_init(proc_stat_init);