stat.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <linux/irqnr.h>
  13. #include <asm/cputime.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. static int show_stat(struct seq_file *p, void *v)
  21. {
  22. int i, j;
  23. unsigned long jif;
  24. cputime64_t user, nice, system, idle, iowait, irq, softirq, steal;
  25. cputime64_t guest;
  26. u64 sum = 0;
  27. struct timespec boottime;
  28. unsigned int per_irq_sum;
  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. sum += kstat_irqs_cpu(j, i);
  46. }
  47. sum += arch_irq_stat_cpu(i);
  48. }
  49. sum += arch_irq_stat();
  50. seq_printf(p, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
  51. (unsigned long long)cputime64_to_clock_t(user),
  52. (unsigned long long)cputime64_to_clock_t(nice),
  53. (unsigned long long)cputime64_to_clock_t(system),
  54. (unsigned long long)cputime64_to_clock_t(idle),
  55. (unsigned long long)cputime64_to_clock_t(iowait),
  56. (unsigned long long)cputime64_to_clock_t(irq),
  57. (unsigned long long)cputime64_to_clock_t(softirq),
  58. (unsigned long long)cputime64_to_clock_t(steal),
  59. (unsigned long long)cputime64_to_clock_t(guest));
  60. for_each_online_cpu(i) {
  61. /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
  62. user = kstat_cpu(i).cpustat.user;
  63. nice = kstat_cpu(i).cpustat.nice;
  64. system = kstat_cpu(i).cpustat.system;
  65. idle = kstat_cpu(i).cpustat.idle;
  66. iowait = kstat_cpu(i).cpustat.iowait;
  67. irq = kstat_cpu(i).cpustat.irq;
  68. softirq = kstat_cpu(i).cpustat.softirq;
  69. steal = kstat_cpu(i).cpustat.steal;
  70. guest = kstat_cpu(i).cpustat.guest;
  71. seq_printf(p,
  72. "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
  73. i,
  74. (unsigned long long)cputime64_to_clock_t(user),
  75. (unsigned long long)cputime64_to_clock_t(nice),
  76. (unsigned long long)cputime64_to_clock_t(system),
  77. (unsigned long long)cputime64_to_clock_t(idle),
  78. (unsigned long long)cputime64_to_clock_t(iowait),
  79. (unsigned long long)cputime64_to_clock_t(irq),
  80. (unsigned long long)cputime64_to_clock_t(softirq),
  81. (unsigned long long)cputime64_to_clock_t(steal),
  82. (unsigned long long)cputime64_to_clock_t(guest));
  83. }
  84. seq_printf(p, "intr %llu", (unsigned long long)sum);
  85. /* sum again ? it could be updated? */
  86. for_each_irq_nr(j) {
  87. per_irq_sum = 0;
  88. for_each_possible_cpu(i)
  89. per_irq_sum += kstat_irqs_cpu(j, i);
  90. seq_printf(p, " %u", per_irq_sum);
  91. }
  92. seq_printf(p,
  93. "\nctxt %llu\n"
  94. "btime %lu\n"
  95. "processes %lu\n"
  96. "procs_running %lu\n"
  97. "procs_blocked %lu\n",
  98. nr_context_switches(),
  99. (unsigned long)jif,
  100. total_forks,
  101. nr_running(),
  102. nr_iowait());
  103. return 0;
  104. }
  105. static int stat_open(struct inode *inode, struct file *file)
  106. {
  107. unsigned size = 4096 * (1 + num_possible_cpus() / 32);
  108. char *buf;
  109. struct seq_file *m;
  110. int res;
  111. /* don't ask for more than the kmalloc() max size, currently 128 KB */
  112. if (size > 128 * 1024)
  113. size = 128 * 1024;
  114. buf = kmalloc(size, GFP_KERNEL);
  115. if (!buf)
  116. return -ENOMEM;
  117. res = single_open(file, show_stat, NULL);
  118. if (!res) {
  119. m = file->private_data;
  120. m->buf = buf;
  121. m->size = size;
  122. } else
  123. kfree(buf);
  124. return res;
  125. }
  126. static const struct file_operations proc_stat_operations = {
  127. .open = stat_open,
  128. .read = seq_read,
  129. .llseek = seq_lseek,
  130. .release = single_release,
  131. };
  132. static int __init proc_stat_init(void)
  133. {
  134. proc_create("stat", 0, NULL, &proc_stat_operations);
  135. return 0;
  136. }
  137. module_init(proc_stat_init);