stats.c 3.2 KB

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