sched: Move /proc/sched_debug to debugfs
[linux-2.6-microblaze.git] / kernel / sched / debug.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * kernel/sched/debug.c
4  *
5  * Print the CFS rbtree and other debugging details
6  *
7  * Copyright(C) 2007, Red Hat, Inc., Ingo Molnar
8  */
9 #include "sched.h"
10
11 static DEFINE_SPINLOCK(sched_debug_lock);
12
13 /*
14  * This allows printing both to /proc/sched_debug and
15  * to the console
16  */
17 #define SEQ_printf(m, x...)                     \
18  do {                                           \
19         if (m)                                  \
20                 seq_printf(m, x);               \
21         else                                    \
22                 pr_cont(x);                     \
23  } while (0)
24
25 /*
26  * Ease the printing of nsec fields:
27  */
28 static long long nsec_high(unsigned long long nsec)
29 {
30         if ((long long)nsec < 0) {
31                 nsec = -nsec;
32                 do_div(nsec, 1000000);
33                 return -nsec;
34         }
35         do_div(nsec, 1000000);
36
37         return nsec;
38 }
39
40 static unsigned long nsec_low(unsigned long long nsec)
41 {
42         if ((long long)nsec < 0)
43                 nsec = -nsec;
44
45         return do_div(nsec, 1000000);
46 }
47
48 #define SPLIT_NS(x) nsec_high(x), nsec_low(x)
49
50 #define SCHED_FEAT(name, enabled)       \
51         #name ,
52
53 static const char * const sched_feat_names[] = {
54 #include "features.h"
55 };
56
57 #undef SCHED_FEAT
58
59 static int sched_feat_show(struct seq_file *m, void *v)
60 {
61         int i;
62
63         for (i = 0; i < __SCHED_FEAT_NR; i++) {
64                 if (!(sysctl_sched_features & (1UL << i)))
65                         seq_puts(m, "NO_");
66                 seq_printf(m, "%s ", sched_feat_names[i]);
67         }
68         seq_puts(m, "\n");
69
70         return 0;
71 }
72
73 #ifdef CONFIG_JUMP_LABEL
74
75 #define jump_label_key__true  STATIC_KEY_INIT_TRUE
76 #define jump_label_key__false STATIC_KEY_INIT_FALSE
77
78 #define SCHED_FEAT(name, enabled)       \
79         jump_label_key__##enabled ,
80
81 struct static_key sched_feat_keys[__SCHED_FEAT_NR] = {
82 #include "features.h"
83 };
84
85 #undef SCHED_FEAT
86
87 static void sched_feat_disable(int i)
88 {
89         static_key_disable_cpuslocked(&sched_feat_keys[i]);
90 }
91
92 static void sched_feat_enable(int i)
93 {
94         static_key_enable_cpuslocked(&sched_feat_keys[i]);
95 }
96 #else
97 static void sched_feat_disable(int i) { };
98 static void sched_feat_enable(int i) { };
99 #endif /* CONFIG_JUMP_LABEL */
100
101 static int sched_feat_set(char *cmp)
102 {
103         int i;
104         int neg = 0;
105
106         if (strncmp(cmp, "NO_", 3) == 0) {
107                 neg = 1;
108                 cmp += 3;
109         }
110
111         i = match_string(sched_feat_names, __SCHED_FEAT_NR, cmp);
112         if (i < 0)
113                 return i;
114
115         if (neg) {
116                 sysctl_sched_features &= ~(1UL << i);
117                 sched_feat_disable(i);
118         } else {
119                 sysctl_sched_features |= (1UL << i);
120                 sched_feat_enable(i);
121         }
122
123         return 0;
124 }
125
126 static ssize_t
127 sched_feat_write(struct file *filp, const char __user *ubuf,
128                 size_t cnt, loff_t *ppos)
129 {
130         char buf[64];
131         char *cmp;
132         int ret;
133         struct inode *inode;
134
135         if (cnt > 63)
136                 cnt = 63;
137
138         if (copy_from_user(&buf, ubuf, cnt))
139                 return -EFAULT;
140
141         buf[cnt] = 0;
142         cmp = strstrip(buf);
143
144         /* Ensure the static_key remains in a consistent state */
145         inode = file_inode(filp);
146         cpus_read_lock();
147         inode_lock(inode);
148         ret = sched_feat_set(cmp);
149         inode_unlock(inode);
150         cpus_read_unlock();
151         if (ret < 0)
152                 return ret;
153
154         *ppos += cnt;
155
156         return cnt;
157 }
158
159 static int sched_feat_open(struct inode *inode, struct file *filp)
160 {
161         return single_open(filp, sched_feat_show, NULL);
162 }
163
164 static const struct file_operations sched_feat_fops = {
165         .open           = sched_feat_open,
166         .write          = sched_feat_write,
167         .read           = seq_read,
168         .llseek         = seq_lseek,
169         .release        = single_release,
170 };
171
172 #ifdef CONFIG_SMP
173
174 static ssize_t sched_scaling_write(struct file *filp, const char __user *ubuf,
175                                    size_t cnt, loff_t *ppos)
176 {
177         char buf[16];
178
179         if (cnt > 15)
180                 cnt = 15;
181
182         if (copy_from_user(&buf, ubuf, cnt))
183                 return -EFAULT;
184
185         if (kstrtouint(buf, 10, &sysctl_sched_tunable_scaling))
186                 return -EINVAL;
187
188         if (sched_update_scaling())
189                 return -EINVAL;
190
191         *ppos += cnt;
192         return cnt;
193 }
194
195 static int sched_scaling_show(struct seq_file *m, void *v)
196 {
197         seq_printf(m, "%d\n", sysctl_sched_tunable_scaling);
198         return 0;
199 }
200
201 static int sched_scaling_open(struct inode *inode, struct file *filp)
202 {
203         return single_open(filp, sched_scaling_show, NULL);
204 }
205
206 static const struct file_operations sched_scaling_fops = {
207         .open           = sched_scaling_open,
208         .write          = sched_scaling_write,
209         .read           = seq_read,
210         .llseek         = seq_lseek,
211         .release        = single_release,
212 };
213
214 #endif /* SMP */
215
216 #ifdef CONFIG_PREEMPT_DYNAMIC
217
218 static ssize_t sched_dynamic_write(struct file *filp, const char __user *ubuf,
219                                    size_t cnt, loff_t *ppos)
220 {
221         char buf[16];
222         int mode;
223
224         if (cnt > 15)
225                 cnt = 15;
226
227         if (copy_from_user(&buf, ubuf, cnt))
228                 return -EFAULT;
229
230         buf[cnt] = 0;
231         mode = sched_dynamic_mode(strstrip(buf));
232         if (mode < 0)
233                 return mode;
234
235         sched_dynamic_update(mode);
236
237         *ppos += cnt;
238
239         return cnt;
240 }
241
242 static int sched_dynamic_show(struct seq_file *m, void *v)
243 {
244         static const char * preempt_modes[] = {
245                 "none", "voluntary", "full"
246         };
247         int i;
248
249         for (i = 0; i < ARRAY_SIZE(preempt_modes); i++) {
250                 if (preempt_dynamic_mode == i)
251                         seq_puts(m, "(");
252                 seq_puts(m, preempt_modes[i]);
253                 if (preempt_dynamic_mode == i)
254                         seq_puts(m, ")");
255
256                 seq_puts(m, " ");
257         }
258
259         seq_puts(m, "\n");
260         return 0;
261 }
262
263 static int sched_dynamic_open(struct inode *inode, struct file *filp)
264 {
265         return single_open(filp, sched_dynamic_show, NULL);
266 }
267
268 static const struct file_operations sched_dynamic_fops = {
269         .open           = sched_dynamic_open,
270         .write          = sched_dynamic_write,
271         .read           = seq_read,
272         .llseek         = seq_lseek,
273         .release        = single_release,
274 };
275
276 #endif /* CONFIG_PREEMPT_DYNAMIC */
277
278 __read_mostly bool sched_debug_enabled;
279
280 static const struct seq_operations sched_debug_sops;
281
282 static int sched_debug_open(struct inode *inode, struct file *filp)
283 {
284         return seq_open(filp, &sched_debug_sops);
285 }
286
287 static const struct file_operations sched_debug_fops = {
288         .open           = sched_debug_open,
289         .read           = seq_read,
290         .llseek         = seq_lseek,
291         .release        = seq_release,
292 };
293
294 static struct dentry *debugfs_sched;
295
296 static __init int sched_init_debug(void)
297 {
298         struct dentry __maybe_unused *numa;
299
300         debugfs_sched = debugfs_create_dir("sched", NULL);
301
302         debugfs_create_file("features", 0644, debugfs_sched, NULL, &sched_feat_fops);
303         debugfs_create_bool("debug_enabled", 0644, debugfs_sched, &sched_debug_enabled);
304 #ifdef CONFIG_PREEMPT_DYNAMIC
305         debugfs_create_file("preempt", 0644, debugfs_sched, NULL, &sched_dynamic_fops);
306 #endif
307
308         debugfs_create_u32("latency_ns", 0644, debugfs_sched, &sysctl_sched_latency);
309         debugfs_create_u32("min_granularity_ns", 0644, debugfs_sched, &sysctl_sched_min_granularity);
310         debugfs_create_u32("wakeup_granularity_ns", 0644, debugfs_sched, &sysctl_sched_wakeup_granularity);
311
312 #ifdef CONFIG_SMP
313         debugfs_create_file("tunable_scaling", 0644, debugfs_sched, NULL, &sched_scaling_fops);
314         debugfs_create_u32("migration_cost_ns", 0644, debugfs_sched, &sysctl_sched_migration_cost);
315         debugfs_create_u32("nr_migrate", 0644, debugfs_sched, &sysctl_sched_nr_migrate);
316
317         mutex_lock(&sched_domains_mutex);
318         update_sched_domain_debugfs();
319         mutex_unlock(&sched_domains_mutex);
320 #endif
321
322 #ifdef CONFIG_NUMA_BALANCING
323         numa = debugfs_create_dir("numa_balancing", debugfs_sched);
324
325         debugfs_create_u32("scan_delay_ms", 0644, numa, &sysctl_numa_balancing_scan_delay);
326         debugfs_create_u32("scan_period_min_ms", 0644, numa, &sysctl_numa_balancing_scan_period_min);
327         debugfs_create_u32("scan_period_max_ms", 0644, numa, &sysctl_numa_balancing_scan_period_max);
328         debugfs_create_u32("scan_size_mb", 0644, numa, &sysctl_numa_balancing_scan_size);
329 #endif
330
331         debugfs_create_file("debug", 0444, debugfs_sched, NULL, &sched_debug_fops);
332
333         return 0;
334 }
335 late_initcall(sched_init_debug);
336
337 #ifdef CONFIG_SMP
338
339 static cpumask_var_t            sd_sysctl_cpus;
340 static struct dentry            *sd_dentry;
341
342 static int sd_flags_show(struct seq_file *m, void *v)
343 {
344         unsigned long flags = *(unsigned int *)m->private;
345         int idx;
346
347         for_each_set_bit(idx, &flags, __SD_FLAG_CNT) {
348                 seq_puts(m, sd_flag_debug[idx].name);
349                 seq_puts(m, " ");
350         }
351         seq_puts(m, "\n");
352
353         return 0;
354 }
355
356 static int sd_flags_open(struct inode *inode, struct file *file)
357 {
358         return single_open(file, sd_flags_show, inode->i_private);
359 }
360
361 static const struct file_operations sd_flags_fops = {
362         .open           = sd_flags_open,
363         .read           = seq_read,
364         .llseek         = seq_lseek,
365         .release        = single_release,
366 };
367
368 static void register_sd(struct sched_domain *sd, struct dentry *parent)
369 {
370 #define SDM(type, mode, member) \
371         debugfs_create_##type(#member, mode, parent, &sd->member)
372
373         SDM(ulong, 0644, min_interval);
374         SDM(ulong, 0644, max_interval);
375         SDM(u64,   0644, max_newidle_lb_cost);
376         SDM(u32,   0644, busy_factor);
377         SDM(u32,   0644, imbalance_pct);
378         SDM(u32,   0644, cache_nice_tries);
379         SDM(str,   0444, name);
380
381 #undef SDM
382
383         debugfs_create_file("flags", 0444, parent, &sd->flags, &sd_flags_fops);
384 }
385
386 void update_sched_domain_debugfs(void)
387 {
388         int cpu, i;
389
390         if (!cpumask_available(sd_sysctl_cpus)) {
391                 if (!alloc_cpumask_var(&sd_sysctl_cpus, GFP_KERNEL))
392                         return;
393                 cpumask_copy(sd_sysctl_cpus, cpu_possible_mask);
394         }
395
396         if (!sd_dentry)
397                 sd_dentry = debugfs_create_dir("domains", debugfs_sched);
398
399         for_each_cpu(cpu, sd_sysctl_cpus) {
400                 struct sched_domain *sd;
401                 struct dentry *d_cpu;
402                 char buf[32];
403
404                 snprintf(buf, sizeof(buf), "cpu%d", cpu);
405                 debugfs_remove(debugfs_lookup(buf, sd_dentry));
406                 d_cpu = debugfs_create_dir(buf, sd_dentry);
407
408                 i = 0;
409                 for_each_domain(cpu, sd) {
410                         struct dentry *d_sd;
411
412                         snprintf(buf, sizeof(buf), "domain%d", i);
413                         d_sd = debugfs_create_dir(buf, d_cpu);
414
415                         register_sd(sd, d_sd);
416                         i++;
417                 }
418
419                 __cpumask_clear_cpu(cpu, sd_sysctl_cpus);
420         }
421 }
422
423 void dirty_sched_domain_sysctl(int cpu)
424 {
425         if (cpumask_available(sd_sysctl_cpus))
426                 __cpumask_set_cpu(cpu, sd_sysctl_cpus);
427 }
428
429 #endif /* CONFIG_SMP */
430
431 #ifdef CONFIG_FAIR_GROUP_SCHED
432 static void print_cfs_group_stats(struct seq_file *m, int cpu, struct task_group *tg)
433 {
434         struct sched_entity *se = tg->se[cpu];
435
436 #define P(F)            SEQ_printf(m, "  .%-30s: %lld\n",       #F, (long long)F)
437 #define P_SCHEDSTAT(F)  SEQ_printf(m, "  .%-30s: %lld\n",       #F, (long long)schedstat_val(F))
438 #define PN(F)           SEQ_printf(m, "  .%-30s: %lld.%06ld\n", #F, SPLIT_NS((long long)F))
439 #define PN_SCHEDSTAT(F) SEQ_printf(m, "  .%-30s: %lld.%06ld\n", #F, SPLIT_NS((long long)schedstat_val(F)))
440
441         if (!se)
442                 return;
443
444         PN(se->exec_start);
445         PN(se->vruntime);
446         PN(se->sum_exec_runtime);
447
448         if (schedstat_enabled()) {
449                 PN_SCHEDSTAT(se->statistics.wait_start);
450                 PN_SCHEDSTAT(se->statistics.sleep_start);
451                 PN_SCHEDSTAT(se->statistics.block_start);
452                 PN_SCHEDSTAT(se->statistics.sleep_max);
453                 PN_SCHEDSTAT(se->statistics.block_max);
454                 PN_SCHEDSTAT(se->statistics.exec_max);
455                 PN_SCHEDSTAT(se->statistics.slice_max);
456                 PN_SCHEDSTAT(se->statistics.wait_max);
457                 PN_SCHEDSTAT(se->statistics.wait_sum);
458                 P_SCHEDSTAT(se->statistics.wait_count);
459         }
460
461         P(se->load.weight);
462 #ifdef CONFIG_SMP
463         P(se->avg.load_avg);
464         P(se->avg.util_avg);
465         P(se->avg.runnable_avg);
466 #endif
467
468 #undef PN_SCHEDSTAT
469 #undef PN
470 #undef P_SCHEDSTAT
471 #undef P
472 }
473 #endif
474
475 #ifdef CONFIG_CGROUP_SCHED
476 static char group_path[PATH_MAX];
477
478 static char *task_group_path(struct task_group *tg)
479 {
480         if (autogroup_path(tg, group_path, PATH_MAX))
481                 return group_path;
482
483         cgroup_path(tg->css.cgroup, group_path, PATH_MAX);
484
485         return group_path;
486 }
487 #endif
488
489 static void
490 print_task(struct seq_file *m, struct rq *rq, struct task_struct *p)
491 {
492         if (task_current(rq, p))
493                 SEQ_printf(m, ">R");
494         else
495                 SEQ_printf(m, " %c", task_state_to_char(p));
496
497         SEQ_printf(m, " %15s %5d %9Ld.%06ld %9Ld %5d ",
498                 p->comm, task_pid_nr(p),
499                 SPLIT_NS(p->se.vruntime),
500                 (long long)(p->nvcsw + p->nivcsw),
501                 p->prio);
502
503         SEQ_printf(m, "%9Ld.%06ld %9Ld.%06ld %9Ld.%06ld",
504                 SPLIT_NS(schedstat_val_or_zero(p->se.statistics.wait_sum)),
505                 SPLIT_NS(p->se.sum_exec_runtime),
506                 SPLIT_NS(schedstat_val_or_zero(p->se.statistics.sum_sleep_runtime)));
507
508 #ifdef CONFIG_NUMA_BALANCING
509         SEQ_printf(m, " %d %d", task_node(p), task_numa_group_id(p));
510 #endif
511 #ifdef CONFIG_CGROUP_SCHED
512         SEQ_printf(m, " %s", task_group_path(task_group(p)));
513 #endif
514
515         SEQ_printf(m, "\n");
516 }
517
518 static void print_rq(struct seq_file *m, struct rq *rq, int rq_cpu)
519 {
520         struct task_struct *g, *p;
521
522         SEQ_printf(m, "\n");
523         SEQ_printf(m, "runnable tasks:\n");
524         SEQ_printf(m, " S            task   PID         tree-key  switches  prio"
525                    "     wait-time             sum-exec        sum-sleep\n");
526         SEQ_printf(m, "-------------------------------------------------------"
527                    "------------------------------------------------------\n");
528
529         rcu_read_lock();
530         for_each_process_thread(g, p) {
531                 if (task_cpu(p) != rq_cpu)
532                         continue;
533
534                 print_task(m, rq, p);
535         }
536         rcu_read_unlock();
537 }
538
539 void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq)
540 {
541         s64 MIN_vruntime = -1, min_vruntime, max_vruntime = -1,
542                 spread, rq0_min_vruntime, spread0;
543         struct rq *rq = cpu_rq(cpu);
544         struct sched_entity *last;
545         unsigned long flags;
546
547 #ifdef CONFIG_FAIR_GROUP_SCHED
548         SEQ_printf(m, "\n");
549         SEQ_printf(m, "cfs_rq[%d]:%s\n", cpu, task_group_path(cfs_rq->tg));
550 #else
551         SEQ_printf(m, "\n");
552         SEQ_printf(m, "cfs_rq[%d]:\n", cpu);
553 #endif
554         SEQ_printf(m, "  .%-30s: %Ld.%06ld\n", "exec_clock",
555                         SPLIT_NS(cfs_rq->exec_clock));
556
557         raw_spin_lock_irqsave(&rq->lock, flags);
558         if (rb_first_cached(&cfs_rq->tasks_timeline))
559                 MIN_vruntime = (__pick_first_entity(cfs_rq))->vruntime;
560         last = __pick_last_entity(cfs_rq);
561         if (last)
562                 max_vruntime = last->vruntime;
563         min_vruntime = cfs_rq->min_vruntime;
564         rq0_min_vruntime = cpu_rq(0)->cfs.min_vruntime;
565         raw_spin_unlock_irqrestore(&rq->lock, flags);
566         SEQ_printf(m, "  .%-30s: %Ld.%06ld\n", "MIN_vruntime",
567                         SPLIT_NS(MIN_vruntime));
568         SEQ_printf(m, "  .%-30s: %Ld.%06ld\n", "min_vruntime",
569                         SPLIT_NS(min_vruntime));
570         SEQ_printf(m, "  .%-30s: %Ld.%06ld\n", "max_vruntime",
571                         SPLIT_NS(max_vruntime));
572         spread = max_vruntime - MIN_vruntime;
573         SEQ_printf(m, "  .%-30s: %Ld.%06ld\n", "spread",
574                         SPLIT_NS(spread));
575         spread0 = min_vruntime - rq0_min_vruntime;
576         SEQ_printf(m, "  .%-30s: %Ld.%06ld\n", "spread0",
577                         SPLIT_NS(spread0));
578         SEQ_printf(m, "  .%-30s: %d\n", "nr_spread_over",
579                         cfs_rq->nr_spread_over);
580         SEQ_printf(m, "  .%-30s: %d\n", "nr_running", cfs_rq->nr_running);
581         SEQ_printf(m, "  .%-30s: %ld\n", "load", cfs_rq->load.weight);
582 #ifdef CONFIG_SMP
583         SEQ_printf(m, "  .%-30s: %lu\n", "load_avg",
584                         cfs_rq->avg.load_avg);
585         SEQ_printf(m, "  .%-30s: %lu\n", "runnable_avg",
586                         cfs_rq->avg.runnable_avg);
587         SEQ_printf(m, "  .%-30s: %lu\n", "util_avg",
588                         cfs_rq->avg.util_avg);
589         SEQ_printf(m, "  .%-30s: %u\n", "util_est_enqueued",
590                         cfs_rq->avg.util_est.enqueued);
591         SEQ_printf(m, "  .%-30s: %ld\n", "removed.load_avg",
592                         cfs_rq->removed.load_avg);
593         SEQ_printf(m, "  .%-30s: %ld\n", "removed.util_avg",
594                         cfs_rq->removed.util_avg);
595         SEQ_printf(m, "  .%-30s: %ld\n", "removed.runnable_avg",
596                         cfs_rq->removed.runnable_avg);
597 #ifdef CONFIG_FAIR_GROUP_SCHED
598         SEQ_printf(m, "  .%-30s: %lu\n", "tg_load_avg_contrib",
599                         cfs_rq->tg_load_avg_contrib);
600         SEQ_printf(m, "  .%-30s: %ld\n", "tg_load_avg",
601                         atomic_long_read(&cfs_rq->tg->load_avg));
602 #endif
603 #endif
604 #ifdef CONFIG_CFS_BANDWIDTH
605         SEQ_printf(m, "  .%-30s: %d\n", "throttled",
606                         cfs_rq->throttled);
607         SEQ_printf(m, "  .%-30s: %d\n", "throttle_count",
608                         cfs_rq->throttle_count);
609 #endif
610
611 #ifdef CONFIG_FAIR_GROUP_SCHED
612         print_cfs_group_stats(m, cpu, cfs_rq->tg);
613 #endif
614 }
615
616 void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq)
617 {
618 #ifdef CONFIG_RT_GROUP_SCHED
619         SEQ_printf(m, "\n");
620         SEQ_printf(m, "rt_rq[%d]:%s\n", cpu, task_group_path(rt_rq->tg));
621 #else
622         SEQ_printf(m, "\n");
623         SEQ_printf(m, "rt_rq[%d]:\n", cpu);
624 #endif
625
626 #define P(x) \
627         SEQ_printf(m, "  .%-30s: %Ld\n", #x, (long long)(rt_rq->x))
628 #define PU(x) \
629         SEQ_printf(m, "  .%-30s: %lu\n", #x, (unsigned long)(rt_rq->x))
630 #define PN(x) \
631         SEQ_printf(m, "  .%-30s: %Ld.%06ld\n", #x, SPLIT_NS(rt_rq->x))
632
633         PU(rt_nr_running);
634 #ifdef CONFIG_SMP
635         PU(rt_nr_migratory);
636 #endif
637         P(rt_throttled);
638         PN(rt_time);
639         PN(rt_runtime);
640
641 #undef PN
642 #undef PU
643 #undef P
644 }
645
646 void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq)
647 {
648         struct dl_bw *dl_bw;
649
650         SEQ_printf(m, "\n");
651         SEQ_printf(m, "dl_rq[%d]:\n", cpu);
652
653 #define PU(x) \
654         SEQ_printf(m, "  .%-30s: %lu\n", #x, (unsigned long)(dl_rq->x))
655
656         PU(dl_nr_running);
657 #ifdef CONFIG_SMP
658         PU(dl_nr_migratory);
659         dl_bw = &cpu_rq(cpu)->rd->dl_bw;
660 #else
661         dl_bw = &dl_rq->dl_bw;
662 #endif
663         SEQ_printf(m, "  .%-30s: %lld\n", "dl_bw->bw", dl_bw->bw);
664         SEQ_printf(m, "  .%-30s: %lld\n", "dl_bw->total_bw", dl_bw->total_bw);
665
666 #undef PU
667 }
668
669 static void print_cpu(struct seq_file *m, int cpu)
670 {
671         struct rq *rq = cpu_rq(cpu);
672         unsigned long flags;
673
674 #ifdef CONFIG_X86
675         {
676                 unsigned int freq = cpu_khz ? : 1;
677
678                 SEQ_printf(m, "cpu#%d, %u.%03u MHz\n",
679                            cpu, freq / 1000, (freq % 1000));
680         }
681 #else
682         SEQ_printf(m, "cpu#%d\n", cpu);
683 #endif
684
685 #define P(x)                                                            \
686 do {                                                                    \
687         if (sizeof(rq->x) == 4)                                         \
688                 SEQ_printf(m, "  .%-30s: %ld\n", #x, (long)(rq->x));    \
689         else                                                            \
690                 SEQ_printf(m, "  .%-30s: %Ld\n", #x, (long long)(rq->x));\
691 } while (0)
692
693 #define PN(x) \
694         SEQ_printf(m, "  .%-30s: %Ld.%06ld\n", #x, SPLIT_NS(rq->x))
695
696         P(nr_running);
697         P(nr_switches);
698         P(nr_uninterruptible);
699         PN(next_balance);
700         SEQ_printf(m, "  .%-30s: %ld\n", "curr->pid", (long)(task_pid_nr(rq->curr)));
701         PN(clock);
702         PN(clock_task);
703 #undef P
704 #undef PN
705
706 #ifdef CONFIG_SMP
707 #define P64(n) SEQ_printf(m, "  .%-30s: %Ld\n", #n, rq->n);
708         P64(avg_idle);
709         P64(max_idle_balance_cost);
710 #undef P64
711 #endif
712
713 #define P(n) SEQ_printf(m, "  .%-30s: %d\n", #n, schedstat_val(rq->n));
714         if (schedstat_enabled()) {
715                 P(yld_count);
716                 P(sched_count);
717                 P(sched_goidle);
718                 P(ttwu_count);
719                 P(ttwu_local);
720         }
721 #undef P
722
723         spin_lock_irqsave(&sched_debug_lock, flags);
724         print_cfs_stats(m, cpu);
725         print_rt_stats(m, cpu);
726         print_dl_stats(m, cpu);
727
728         print_rq(m, rq, cpu);
729         spin_unlock_irqrestore(&sched_debug_lock, flags);
730         SEQ_printf(m, "\n");
731 }
732
733 static const char *sched_tunable_scaling_names[] = {
734         "none",
735         "logarithmic",
736         "linear"
737 };
738
739 static void sched_debug_header(struct seq_file *m)
740 {
741         u64 ktime, sched_clk, cpu_clk;
742         unsigned long flags;
743
744         local_irq_save(flags);
745         ktime = ktime_to_ns(ktime_get());
746         sched_clk = sched_clock();
747         cpu_clk = local_clock();
748         local_irq_restore(flags);
749
750         SEQ_printf(m, "Sched Debug Version: v0.11, %s %.*s\n",
751                 init_utsname()->release,
752                 (int)strcspn(init_utsname()->version, " "),
753                 init_utsname()->version);
754
755 #define P(x) \
756         SEQ_printf(m, "%-40s: %Ld\n", #x, (long long)(x))
757 #define PN(x) \
758         SEQ_printf(m, "%-40s: %Ld.%06ld\n", #x, SPLIT_NS(x))
759         PN(ktime);
760         PN(sched_clk);
761         PN(cpu_clk);
762         P(jiffies);
763 #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
764         P(sched_clock_stable());
765 #endif
766 #undef PN
767 #undef P
768
769         SEQ_printf(m, "\n");
770         SEQ_printf(m, "sysctl_sched\n");
771
772 #define P(x) \
773         SEQ_printf(m, "  .%-40s: %Ld\n", #x, (long long)(x))
774 #define PN(x) \
775         SEQ_printf(m, "  .%-40s: %Ld.%06ld\n", #x, SPLIT_NS(x))
776         PN(sysctl_sched_latency);
777         PN(sysctl_sched_min_granularity);
778         PN(sysctl_sched_wakeup_granularity);
779         P(sysctl_sched_child_runs_first);
780         P(sysctl_sched_features);
781 #undef PN
782 #undef P
783
784         SEQ_printf(m, "  .%-40s: %d (%s)\n",
785                 "sysctl_sched_tunable_scaling",
786                 sysctl_sched_tunable_scaling,
787                 sched_tunable_scaling_names[sysctl_sched_tunable_scaling]);
788         SEQ_printf(m, "\n");
789 }
790
791 static int sched_debug_show(struct seq_file *m, void *v)
792 {
793         int cpu = (unsigned long)(v - 2);
794
795         if (cpu != -1)
796                 print_cpu(m, cpu);
797         else
798                 sched_debug_header(m);
799
800         return 0;
801 }
802
803 void sysrq_sched_debug_show(void)
804 {
805         int cpu;
806
807         sched_debug_header(NULL);
808         for_each_online_cpu(cpu) {
809                 /*
810                  * Need to reset softlockup watchdogs on all CPUs, because
811                  * another CPU might be blocked waiting for us to process
812                  * an IPI or stop_machine.
813                  */
814                 touch_nmi_watchdog();
815                 touch_all_softlockup_watchdogs();
816                 print_cpu(NULL, cpu);
817         }
818 }
819
820 /*
821  * This iterator needs some explanation.
822  * It returns 1 for the header position.
823  * This means 2 is CPU 0.
824  * In a hotplugged system some CPUs, including CPU 0, may be missing so we have
825  * to use cpumask_* to iterate over the CPUs.
826  */
827 static void *sched_debug_start(struct seq_file *file, loff_t *offset)
828 {
829         unsigned long n = *offset;
830
831         if (n == 0)
832                 return (void *) 1;
833
834         n--;
835
836         if (n > 0)
837                 n = cpumask_next(n - 1, cpu_online_mask);
838         else
839                 n = cpumask_first(cpu_online_mask);
840
841         *offset = n + 1;
842
843         if (n < nr_cpu_ids)
844                 return (void *)(unsigned long)(n + 2);
845
846         return NULL;
847 }
848
849 static void *sched_debug_next(struct seq_file *file, void *data, loff_t *offset)
850 {
851         (*offset)++;
852         return sched_debug_start(file, offset);
853 }
854
855 static void sched_debug_stop(struct seq_file *file, void *data)
856 {
857 }
858
859 static const struct seq_operations sched_debug_sops = {
860         .start          = sched_debug_start,
861         .next           = sched_debug_next,
862         .stop           = sched_debug_stop,
863         .show           = sched_debug_show,
864 };
865
866 #define __PS(S, F) SEQ_printf(m, "%-45s:%21Ld\n", S, (long long)(F))
867 #define __P(F) __PS(#F, F)
868 #define   P(F) __PS(#F, p->F)
869 #define __PSN(S, F) SEQ_printf(m, "%-45s:%14Ld.%06ld\n", S, SPLIT_NS((long long)(F)))
870 #define __PN(F) __PSN(#F, F)
871 #define   PN(F) __PSN(#F, p->F)
872
873
874 #ifdef CONFIG_NUMA_BALANCING
875 void print_numa_stats(struct seq_file *m, int node, unsigned long tsf,
876                 unsigned long tpf, unsigned long gsf, unsigned long gpf)
877 {
878         SEQ_printf(m, "numa_faults node=%d ", node);
879         SEQ_printf(m, "task_private=%lu task_shared=%lu ", tpf, tsf);
880         SEQ_printf(m, "group_private=%lu group_shared=%lu\n", gpf, gsf);
881 }
882 #endif
883
884
885 static void sched_show_numa(struct task_struct *p, struct seq_file *m)
886 {
887 #ifdef CONFIG_NUMA_BALANCING
888         struct mempolicy *pol;
889
890         if (p->mm)
891                 P(mm->numa_scan_seq);
892
893         task_lock(p);
894         pol = p->mempolicy;
895         if (pol && !(pol->flags & MPOL_F_MORON))
896                 pol = NULL;
897         mpol_get(pol);
898         task_unlock(p);
899
900         P(numa_pages_migrated);
901         P(numa_preferred_nid);
902         P(total_numa_faults);
903         SEQ_printf(m, "current_node=%d, numa_group_id=%d\n",
904                         task_node(p), task_numa_group_id(p));
905         show_numa_stats(p, m);
906         mpol_put(pol);
907 #endif
908 }
909
910 void proc_sched_show_task(struct task_struct *p, struct pid_namespace *ns,
911                                                   struct seq_file *m)
912 {
913         unsigned long nr_switches;
914
915         SEQ_printf(m, "%s (%d, #threads: %d)\n", p->comm, task_pid_nr_ns(p, ns),
916                                                 get_nr_threads(p));
917         SEQ_printf(m,
918                 "---------------------------------------------------------"
919                 "----------\n");
920
921 #define P_SCHEDSTAT(F)  __PS(#F, schedstat_val(p->F))
922 #define PN_SCHEDSTAT(F) __PSN(#F, schedstat_val(p->F))
923
924         PN(se.exec_start);
925         PN(se.vruntime);
926         PN(se.sum_exec_runtime);
927
928         nr_switches = p->nvcsw + p->nivcsw;
929
930         P(se.nr_migrations);
931
932         if (schedstat_enabled()) {
933                 u64 avg_atom, avg_per_cpu;
934
935                 PN_SCHEDSTAT(se.statistics.sum_sleep_runtime);
936                 PN_SCHEDSTAT(se.statistics.wait_start);
937                 PN_SCHEDSTAT(se.statistics.sleep_start);
938                 PN_SCHEDSTAT(se.statistics.block_start);
939                 PN_SCHEDSTAT(se.statistics.sleep_max);
940                 PN_SCHEDSTAT(se.statistics.block_max);
941                 PN_SCHEDSTAT(se.statistics.exec_max);
942                 PN_SCHEDSTAT(se.statistics.slice_max);
943                 PN_SCHEDSTAT(se.statistics.wait_max);
944                 PN_SCHEDSTAT(se.statistics.wait_sum);
945                 P_SCHEDSTAT(se.statistics.wait_count);
946                 PN_SCHEDSTAT(se.statistics.iowait_sum);
947                 P_SCHEDSTAT(se.statistics.iowait_count);
948                 P_SCHEDSTAT(se.statistics.nr_migrations_cold);
949                 P_SCHEDSTAT(se.statistics.nr_failed_migrations_affine);
950                 P_SCHEDSTAT(se.statistics.nr_failed_migrations_running);
951                 P_SCHEDSTAT(se.statistics.nr_failed_migrations_hot);
952                 P_SCHEDSTAT(se.statistics.nr_forced_migrations);
953                 P_SCHEDSTAT(se.statistics.nr_wakeups);
954                 P_SCHEDSTAT(se.statistics.nr_wakeups_sync);
955                 P_SCHEDSTAT(se.statistics.nr_wakeups_migrate);
956                 P_SCHEDSTAT(se.statistics.nr_wakeups_local);
957                 P_SCHEDSTAT(se.statistics.nr_wakeups_remote);
958                 P_SCHEDSTAT(se.statistics.nr_wakeups_affine);
959                 P_SCHEDSTAT(se.statistics.nr_wakeups_affine_attempts);
960                 P_SCHEDSTAT(se.statistics.nr_wakeups_passive);
961                 P_SCHEDSTAT(se.statistics.nr_wakeups_idle);
962
963                 avg_atom = p->se.sum_exec_runtime;
964                 if (nr_switches)
965                         avg_atom = div64_ul(avg_atom, nr_switches);
966                 else
967                         avg_atom = -1LL;
968
969                 avg_per_cpu = p->se.sum_exec_runtime;
970                 if (p->se.nr_migrations) {
971                         avg_per_cpu = div64_u64(avg_per_cpu,
972                                                 p->se.nr_migrations);
973                 } else {
974                         avg_per_cpu = -1LL;
975                 }
976
977                 __PN(avg_atom);
978                 __PN(avg_per_cpu);
979         }
980
981         __P(nr_switches);
982         __PS("nr_voluntary_switches", p->nvcsw);
983         __PS("nr_involuntary_switches", p->nivcsw);
984
985         P(se.load.weight);
986 #ifdef CONFIG_SMP
987         P(se.avg.load_sum);
988         P(se.avg.runnable_sum);
989         P(se.avg.util_sum);
990         P(se.avg.load_avg);
991         P(se.avg.runnable_avg);
992         P(se.avg.util_avg);
993         P(se.avg.last_update_time);
994         P(se.avg.util_est.ewma);
995         P(se.avg.util_est.enqueued);
996 #endif
997 #ifdef CONFIG_UCLAMP_TASK
998         __PS("uclamp.min", p->uclamp_req[UCLAMP_MIN].value);
999         __PS("uclamp.max", p->uclamp_req[UCLAMP_MAX].value);
1000         __PS("effective uclamp.min", uclamp_eff_value(p, UCLAMP_MIN));
1001         __PS("effective uclamp.max", uclamp_eff_value(p, UCLAMP_MAX));
1002 #endif
1003         P(policy);
1004         P(prio);
1005         if (task_has_dl_policy(p)) {
1006                 P(dl.runtime);
1007                 P(dl.deadline);
1008         }
1009 #undef PN_SCHEDSTAT
1010 #undef P_SCHEDSTAT
1011
1012         {
1013                 unsigned int this_cpu = raw_smp_processor_id();
1014                 u64 t0, t1;
1015
1016                 t0 = cpu_clock(this_cpu);
1017                 t1 = cpu_clock(this_cpu);
1018                 __PS("clock-delta", t1-t0);
1019         }
1020
1021         sched_show_numa(p, m);
1022 }
1023
1024 void proc_sched_set_task(struct task_struct *p)
1025 {
1026 #ifdef CONFIG_SCHEDSTATS
1027         memset(&p->se.statistics, 0, sizeof(p->se.statistics));
1028 #endif
1029 }