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, 16, 8 }
14 #define SHIFT 32
15
16 double y;
17
calc_runnable_avg_yN_inv(const int halflife)18 void calc_runnable_avg_yN_inv(const int halflife)
19 {
20 int i;
21 unsigned int x;
22
23 printf("static const u32 runnable_avg_yN_inv[] = {");
24 for (i = 0; i < halflife; i++) {
25 x = ((1UL<<32)-1)*pow(y, i);
26
27 if (i % 4 == 0) printf("\n\t");
28 printf("0x%8x,", x);
29 }
30 printf("\n};\n\n");
31 }
32
33 int sum;
34
calc_runnable_avg_yN_sum(const int halflife)35 void calc_runnable_avg_yN_sum(const int halflife)
36 {
37 int i;
38
39 printf("static const u32 runnable_avg_yN_sum[] = {\n\t 0,");
40 for (i = 1; i <= halflife; i++) {
41 if (i == 1)
42 sum *= y;
43 else
44 sum = sum*y + 1024*y;
45
46 if (i % 11 == 0)
47 printf("\n\t");
48
49 printf("%5d,", sum);
50 }
51 printf("\n};\n\n");
52 }
53
54 int n;
55 long max;
56
calc_converged_max(const int halflife)57 void calc_converged_max(const int halflife)
58 {
59 long last = 0, y_inv = ((1UL<<32)-1)*y;
60
61 for (; ; n++) {
62 if (n > -1)
63 max = ((max*y_inv)>>SHIFT) + 1024;
64 /*
65 * This is the same as:
66 * max = max*y + 1024;
67 */
68
69 if (last == max)
70 break;
71
72 last = max;
73 }
74 n--;
75 printf("#define LOAD_AVG_PERIOD %d\n", halflife);
76 printf("#define LOAD_AVG_MAX %ld\n", max);
77 printf("#define LOAD_AVG_MAX_N %d\n\n", n);
78 }
79
calc_accumulated_sum_32(const int halflife)80 void calc_accumulated_sum_32(const int halflife)
81 {
82 int i, x = sum;
83
84 printf("static const u32 __accumulated_sum_N32[] = {\n\t 0,");
85 for (i = 1; i <= n/halflife+1; i++) {
86 if (i > 1)
87 x = x/2 + sum;
88
89 if (i % 6 == 0)
90 printf("\n\t");
91
92 printf("%6d,", x);
93 }
94 printf("\n};\n\n");
95 }
96
main(void)97 void main(void)
98 {
99 int hl_value[] = HALFLIFE;
100 int hl_count = sizeof(hl_value) / sizeof(int);
101 int hl_idx, halflife;
102
103 printf("/* SPDX-License-Identifier: GPL-2.0 */\n");
104 printf("/* Generated by Documentation/scheduler/sched-pelt; do not modify. */\n");
105
106 for (hl_idx = 0; hl_idx < hl_count; ++hl_idx) {
107 halflife = hl_value[hl_idx];
108
109 y = pow(0.5, 1/(double)halflife);
110 sum = 1024;
111 /* first period */
112 max = 1024;
113 n = -1;
114
115 printf("\n#ifdef CONFIG_PELT_UTIL_HALFLIFE_%d\n", halflife);
116 calc_runnable_avg_yN_inv(halflife);
117 calc_runnable_avg_yN_sum(halflife);
118 calc_converged_max(halflife);
119 /*
120 * calc_accumulated_sum_32(halflife) precomputed load sum table of half-life,
121 * not used yet.
122 */
123 printf("#endif\n");
124 }
125 }
126