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