stats.c 2.9 KB

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