stat.c 4.2 KB

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