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