blob: cd3e1fe7a98ada1cc73fbfd2304bb8c73be55079 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
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
16double y;
17
18void 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
33int sum = 1024;
34
35void 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
54int n = -1;
55/* first period */
56long max = 1024;
57
58void calc_converged_max(const int halflife)
59{
60 long last = 0, y_inv = ((1UL<<32)-1)*y;
61
62 for (; ; n++) {
63 if (n > -1)
64 max = ((max*y_inv)>>SHIFT) + 1024;
65 /*
66 * This is the same as:
67 * max = max*y + 1024;
68 */
69
70 if (last == max)
71 break;
72
73 last = max;
74 }
75 n--;
76 printf("#define LOAD_AVG_PERIOD %d\n", halflife);
77 printf("#define LOAD_AVG_MAX %ld\n", max);
78 printf("#define LOAD_AVG_MAX_N %d\n\n", n);
79}
80
81void calc_accumulated_sum_32(const int halflife)
82{
83 int i, x = sum;
84
85 printf("static const u32 __accumulated_sum_N32[] = {\n\t 0,");
86 for (i = 1; i <= n/halflife+1; i++) {
87 if (i > 1)
88 x = x/2 + sum;
89
90 if (i % 6 == 0)
91 printf("\n\t");
92
93 printf("%6d,", x);
94 }
95 printf("\n};\n\n");
96}
97
98void main(void)
99{
100 int hl_value[] = HALFLIFE;
101 int hl_count = sizeof(hl_value) / sizeof(int);
102 int hl_idx, halflife;
103
104 printf("/* Generated by Documentation/scheduler/sched-pelt; do not modify. */\n\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
111 printf("#if CONFIG_PELT_UTIL_HALFLIFE_%d\n", halflife);
112 calc_runnable_avg_yN_inv(halflife);
113 calc_runnable_avg_yN_sum(halflife);
114 calc_converged_max(halflife);
115 calc_accumulated_sum_32(halflife);
116 printf("#endif\n\n");
117 }
118}