xref: /linux/Documentation/scheduler/sched-pelt.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 /*
2  * The following program is used to generate the constants for
3  * computing sched averages.
4  *
5  * ==============================================================
6  *		C program (compile with -lm)
7  * ==============================================================
8  */
9 
10 #include <math.h>
11 #include <stdio.h>
12 
13 #define HALFLIFE 32
14 #define SHIFT 32
15 
16 double y;
17 
18 void calc_runnable_avg_yN_inv(void)
19 {
20 	int i;
21 	unsigned int x;
22 
23 	/* To silence -Wunused-but-set-variable warnings. */
24 	printf("static const u32 runnable_avg_yN_inv[] __maybe_unused = {");
25 	for (i = 0; i < HALFLIFE; i++) {
26 		x = ((1UL<<32)-1)*pow(y, i);
27 
28 		if (i % 6 == 0)
29 			printf("\n\t");
30 		printf("0x%8x, ", x);
31 	}
32 	printf("\n};\n\n");
33 }
34 
35 int sum = 1024;
36 
37 void calc_runnable_avg_yN_sum(void)
38 {
39 	int i;
40 
41 	printf("static const u32 runnable_avg_yN_sum[] = {\n\t    0,");
42 	for (i = 1; i <= HALFLIFE; i++) {
43 		if (i == 1)
44 			sum *= y;
45 		else
46 			sum = sum*y + 1024*y;
47 
48 		if (i % 11 == 0)
49 			printf("\n\t");
50 
51 		printf("%5d,", sum);
52 	}
53 	printf("\n};\n\n");
54 }
55 
56 int n = -1;
57 /* first period */
58 long max = 1024;
59 
60 void calc_converged_max(void)
61 {
62 	long last = 0, y_inv = ((1UL<<32)-1)*y;
63 
64 	for (; ; n++) {
65 		if (n > -1)
66 			max = ((max*y_inv)>>SHIFT) + 1024;
67 			/*
68 			 * This is the same as:
69 			 * max = max*y + 1024;
70 			 */
71 
72 		if (last == max)
73 			break;
74 
75 		last = max;
76 	}
77 	n--;
78 	printf("#define LOAD_AVG_PERIOD %d\n", HALFLIFE);
79 	printf("#define LOAD_AVG_MAX %ld\n", max);
80 //	printf("#define LOAD_AVG_MAX_N %d\n\n", n);
81 }
82 
83 void calc_accumulated_sum_32(void)
84 {
85 	int i, x = sum;
86 
87 	printf("static const u32 __accumulated_sum_N32[] = {\n\t     0,");
88 	for (i = 1; i <= n/HALFLIFE+1; i++) {
89 		if (i > 1)
90 			x = x/2 + sum;
91 
92 		if (i % 6 == 0)
93 			printf("\n\t");
94 
95 		printf("%6d,", x);
96 	}
97 	printf("\n};\n\n");
98 }
99 
100 void main(void)
101 {
102 	printf("/* Generated by Documentation/scheduler/sched-pelt; do not modify. */\n\n");
103 
104 	y = pow(0.5, 1/(double)HALFLIFE);
105 
106 	calc_runnable_avg_yN_inv();
107 //	calc_runnable_avg_yN_sum();
108 	calc_converged_max();
109 //	calc_accumulated_sum_32();
110 }
111