xref: /linux/tools/perf/trace/beauty/sched_policy.c (revision b2128290c29902315e632ea59e0504d6bc9e9b42)
1 // SPDX-License-Identifier: LGPL-2.1
2 #include "trace/beauty/beauty.h"
3 
4 #include <sched.h>
5 
6 /*
7  * Not defined anywhere else, probably, just to make sure we
8  * catch future flags
9  */
10 #define SCHED_POLICY_MASK 0xff
11 
12 #ifndef SCHED_DEADLINE
13 #define SCHED_DEADLINE 6
14 #endif
15 #ifndef SCHED_RESET_ON_FORK
16 #define SCHED_RESET_ON_FORK 0x40000000
17 #endif
18 
19 size_t syscall_arg__scnprintf_sched_policy(char *bf, size_t size,
20 					   struct syscall_arg *arg)
21 {
22 	bool show_prefix = arg->show_string_prefix;
23 	const char *prefix = "SCHED_";
24 	const char *policies[] = {
25 		"NORMAL", "FIFO", "RR", "BATCH", "ISO", "IDLE", "DEADLINE",
26 	};
27 	size_t printed;
28 	int policy = arg->val,
29 	    flags = policy & ~SCHED_POLICY_MASK;
30 
31 	policy &= SCHED_POLICY_MASK;
32 	if (policy <= SCHED_DEADLINE)
33 		printed = scnprintf(bf, size, "%s%s", show_prefix ? prefix : "", policies[policy]);
34 	else
35 		printed = scnprintf(bf, size, "%#x", policy);
36 
37 #define	P_POLICY_FLAG(n) \
38 	if (flags & SCHED_##n) { \
39 		printed += scnprintf(bf + printed, size - printed, "|%s%s", show_prefix ? prefix : "",  #n); \
40 		flags &= ~SCHED_##n; \
41 	}
42 
43 	P_POLICY_FLAG(RESET_ON_FORK);
44 #undef P_POLICY_FLAG
45 
46 	if (flags)
47 		printed += scnprintf(bf + printed, size - printed, "|%#x", flags);
48 
49 	return printed;
50 }
51