stats.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include <linux/slab.h>
  2. #include <linux/fs.h>
  3. #include <linux/seq_file.h>
  4. #include <linux/proc_fs.h>
  5. #include "sched.h"
  6. /*
  7. * bump this up when changing the output format or the meaning of an existing
  8. * format, so that tools can adapt (or abort)
  9. */
  10. #define SCHEDSTAT_VERSION 15
  11. static int show_schedstat(struct seq_file *seq, void *v)
  12. {
  13. int cpu;
  14. int mask_len = DIV_ROUND_UP(NR_CPUS, 32) * 9;
  15. char *mask_str = kmalloc(mask_len, GFP_KERNEL);
  16. if (mask_str == NULL)
  17. return -ENOMEM;
  18. if (v == (void *)1) {
  19. seq_printf(seq, "version %d\n", SCHEDSTAT_VERSION);
  20. seq_printf(seq, "timestamp %lu\n", jiffies);
  21. } else {
  22. struct rq *rq;
  23. #ifdef CONFIG_SMP
  24. struct sched_domain *sd;
  25. int dcount = 0;
  26. #endif
  27. cpu = (unsigned long)(v - 2);
  28. rq = cpu_rq(cpu);
  29. /* runqueue-specific stats */
  30. seq_printf(seq,
  31. "cpu%d %u 0 %u %u %u %u %llu %llu %lu",
  32. cpu, rq->yld_count,
  33. rq->sched_count, rq->sched_goidle,
  34. rq->ttwu_count, rq->ttwu_local,
  35. rq->rq_cpu_time,
  36. rq->rq_sched_info.run_delay, rq->rq_sched_info.pcount);
  37. seq_printf(seq, "\n");
  38. #ifdef CONFIG_SMP
  39. /* domain-specific stats */
  40. rcu_read_lock();
  41. for_each_domain(cpu, sd) {
  42. enum cpu_idle_type itype;
  43. cpumask_scnprintf(mask_str, mask_len,
  44. sched_domain_span(sd));
  45. seq_printf(seq, "domain%d %s", dcount++, mask_str);
  46. for (itype = CPU_IDLE; itype < CPU_MAX_IDLE_TYPES;
  47. itype++) {
  48. seq_printf(seq, " %u %u %u %u %u %u %u %u",
  49. sd->lb_count[itype],
  50. sd->lb_balanced[itype],
  51. sd->lb_failed[itype],
  52. sd->lb_imbalance[itype],
  53. sd->lb_gained[itype],
  54. sd->lb_hot_gained[itype],
  55. sd->lb_nobusyq[itype],
  56. sd->lb_nobusyg[itype]);
  57. }
  58. seq_printf(seq,
  59. " %u %u %u %u %u %u %u %u %u %u %u %u\n",
  60. sd->alb_count, sd->alb_failed, sd->alb_pushed,
  61. sd->sbe_count, sd->sbe_balanced, sd->sbe_pushed,
  62. sd->sbf_count, sd->sbf_balanced, sd->sbf_pushed,
  63. sd->ttwu_wake_remote, sd->ttwu_move_affine,
  64. sd->ttwu_move_balance);
  65. }
  66. rcu_read_unlock();
  67. #endif
  68. }
  69. kfree(mask_str);
  70. return 0;
  71. }
  72. /*
  73. * This itererator needs some explanation.
  74. * It returns 1 for the header position.
  75. * This means 2 is cpu 0.
  76. * In a hotplugged system some cpus, including cpu 0, may be missing so we have
  77. * to use cpumask_* to iterate over the cpus.
  78. */
  79. static void *schedstat_start(struct seq_file *file, loff_t *offset)
  80. {
  81. unsigned long n = *offset;
  82. if (n == 0)
  83. return (void *) 1;
  84. n--;
  85. if (n > 0)
  86. n = cpumask_next(n - 1, cpu_online_mask);
  87. else
  88. n = cpumask_first(cpu_online_mask);
  89. *offset = n + 1;
  90. if (n < nr_cpu_ids)
  91. return (void *)(unsigned long)(n + 2);
  92. return NULL;
  93. }
  94. static void *schedstat_next(struct seq_file *file, void *data, loff_t *offset)
  95. {
  96. (*offset)++;
  97. return schedstat_start(file, offset);
  98. }
  99. static void schedstat_stop(struct seq_file *file, void *data)
  100. {
  101. }
  102. static const struct seq_operations schedstat_sops = {
  103. .start = schedstat_start,
  104. .next = schedstat_next,
  105. .stop = schedstat_stop,
  106. .show = show_schedstat,
  107. };
  108. static int schedstat_open(struct inode *inode, struct file *file)
  109. {
  110. return seq_open(file, &schedstat_sops);
  111. }
  112. static int schedstat_release(struct inode *inode, struct file *file)
  113. {
  114. return 0;
  115. };
  116. static const struct file_operations proc_schedstat_operations = {
  117. .open = schedstat_open,
  118. .read = seq_read,
  119. .llseek = seq_lseek,
  120. .release = schedstat_release,
  121. };
  122. static int __init proc_schedstat_init(void)
  123. {
  124. proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
  125. return 0;
  126. }
  127. module_init(proc_schedstat_init);