1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
/*
* Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/edp.h>
#include "edp_internal.h"
static struct list_head hilist;
static struct list_head lolist_hash[10];
static void init_hash(void)
{
int i;
INIT_LIST_HEAD(&hilist);
for (i = 0; i < ARRAY_SIZE(lolist_hash); i++)
INIT_LIST_HEAD(lolist_hash + i);
}
/* Return the minimum state that we must approve */
static unsigned int min_ai(struct edp_client *c)
{
unsigned int ri = req_index(c);
if (ri >= c->e0_index)
return ri;
return min(cur_index(c), c->e0_index);
}
static struct edp_client *lolist_hi_entry(int hash)
{
int i;
for (i = hash; i < ARRAY_SIZE(lolist_hash); i++) {
if (!list_empty(lolist_hash + i))
return list_first_entry(lolist_hash + i,
struct edp_client, glnk);
}
return NULL;
}
static struct edp_client *lolist_li_entry(int hash)
{
int i;
for (i = hash; i >= 0; i--) {
if (!list_empty(lolist_hash + i))
return list_first_entry(lolist_hash + i,
struct edp_client, glnk);
}
return NULL;
}
static struct edp_client *lolist_entry(int hash, bool hifirst)
{
struct edp_client *c;
if (hifirst)
c = lolist_hi_entry(hash) ?: lolist_li_entry(hash - 1);
else
c = lolist_li_entry(hash) ?: lolist_hi_entry(hash + 1);
return c;
}
/*
* Use hashing to fasten up the lookup for bestfit (we might have to do
* multiple passes). If a single client can supply the minimum
* requirement, put them into hilist. Otherwise, compute a simple hash
* from the ratio of (cur - E0) to the minimum requirement and add to
* the corresponding lolist queue. Entries are added to the list head
* so that lower priority clients are throttled first.
*/
static void prep_throttle_hash(struct edp_client *client, unsigned int mn)
{
struct edp_manager *m = client->manager;
struct edp_client *c;
unsigned int i;
unsigned int more;
init_hash();
list_for_each_entry(c, &m->clients, link) {
if (c == client || cur_level(c) <= e0_level(c))
continue;
more = cur_level(c) - e0_level(c);
if (more + m->remaining < mn) {
i = more * ARRAY_SIZE(lolist_hash) / mn;
list_add(&c->glnk, lolist_hash + i);
} else {
list_add(&c->glnk, &hilist);
}
}
}
/*
* Find the bestfit point between the requesting client and a potential
* throttle-victim. Choose the one with lowest remaining current.
*/
static unsigned int bestfit_point(struct edp_client *rc, struct edp_client *c,
unsigned int mn, unsigned int *opt_bal)
{
unsigned int ai = cur_index(rc);
unsigned int ri = req_index(rc);
unsigned int cl = cur_level(rc);
unsigned int step;
unsigned int bal;
unsigned int i;
unsigned int j;
*opt_bal = rc->manager->imax;
for (i = cur_index(c) + 1; i <= c->e0_index && ai > ri; i++) {
step = rc->manager->remaining + *c->cur - c->states[i];
if (step < mn)
continue;
j = edp_promotion_point(rc, step);
bal = step - (rc->states[j] - cl);
if (bal < *opt_bal) {
*opt_bal = bal;
c->gwt = i;
ai = j;
}
}
return ai;
}
static struct edp_client *throttle_bestfit_hi(struct edp_client *rc,
unsigned int mn, unsigned int *bfi, unsigned int *opt_bal)
{
struct edp_client *c;
struct edp_client *opt_c;
unsigned int bal;
unsigned int i;
if (list_empty(&hilist))
return NULL;
opt_c = NULL;
*opt_bal = rc->manager->imax;
list_for_each_entry(c, &hilist, glnk) {
i = bestfit_point(rc, c, mn, &bal);
if (bal < *opt_bal) {
*bfi = i;
*opt_bal = bal;
opt_c = c;
}
}
WARN_ON(!opt_c);
return opt_c;
}
static unsigned int throttle_recover(struct edp_manager *m, unsigned int mn)
{
struct edp_client *c;
unsigned int i;
unsigned int tp;
unsigned int step;
unsigned int rsum = m->remaining;
while (rsum < mn) {
i = ((mn - rsum) * ARRAY_SIZE(lolist_hash) + mn - 1) / mn;
c = lolist_entry(i, true);
if (!c)
break;
list_del(&c->glnk);
step = min(cur_level(c) - e0_level(c), mn - rsum);
tp = edp_throttling_point(c, step);
if (tp == cur_index(c))
continue;
rsum += cur_level(c) - c->states[tp];
c->throttle(tp);
if (c->cur == c->req)
m->num_denied++;
c->cur = c->states + tp;
}
WARN_ON(rsum < mn);
return rsum;
}
static void throttle(struct edp_client *client)
{
struct edp_manager *m = client->manager;
unsigned int ai;
unsigned int mn;
struct edp_client *c;
unsigned int balance;
ai = min_ai(client);
mn = client->states[ai] - cur_level(client);
if (mn <= m->remaining) {
ai = edp_promotion_point(client, m->remaining);
m->remaining -= client->states[ai] - cur_level(client);
client->cur = client->states + ai;
return;
}
prep_throttle_hash(client, mn);
c = throttle_bestfit_hi(client, mn, &ai, &balance);
if (c) {
c->throttle(c->gwt);
if (c->cur == c->req)
m->num_denied++;
m->remaining = balance;
c->cur = c->states + c->gwt;
client->cur = client->states + ai;
return;
}
balance = throttle_recover(m, mn);
WARN_ON(balance < mn);
ai = edp_promotion_point(client, balance);
m->remaining = balance - (client->states[ai] - cur_level(client));
client->cur = client->states + ai;
}
static void bestfit_update_request(struct edp_client *client,
const unsigned int *req)
{
edp_default_update_request(client, req, throttle);
}
static void prep_promotion_hash(struct edp_manager *m)
{
unsigned int balance = m->remaining;
struct edp_client *c;
unsigned int step;
unsigned int i;
init_hash();
list_for_each_entry(c, &m->clients, link) {
if (req_level(c) <= cur_level(c) || !c->notify_promotion)
continue;
step = req_level(c) - cur_level(c);
/*
* Add to the list tail so that higher priority clients
* are promoted first
*/
if (step < balance) {
i = step * ARRAY_SIZE(lolist_hash) / balance;
list_add_tail(&c->glnk, lolist_hash + i);
} else {
list_add_tail(&c->glnk, &hilist);
}
}
}
static struct edp_client *promotion_bestfit_hi(unsigned int balance)
{
struct edp_client *c;
unsigned int i;
unsigned int step;
struct edp_client *opt_c = NULL;
unsigned int opt_bal = balance;
list_for_each_entry(c, &hilist, glnk) {
i = edp_promotion_point(c, balance);
step = c->states[i] - cur_level(c);
if (balance - step < opt_bal) {
c->gwt = i;
opt_c = c;
}
}
return opt_c;
}
static void bestfit_promote(struct edp_manager *mgr)
{
unsigned int balance = mgr->remaining;
struct edp_client *c;
unsigned int i;
prep_promotion_hash(mgr);
c = promotion_bestfit_hi(balance);
if (c) {
balance -= c->states[c->gwt] - cur_level(c);
c->cur = c->states + c->gwt;
if (c->cur == c->req)
mgr->num_denied--;
c->notify_promotion(c->gwt);
}
while (balance && mgr->num_denied) {
i = balance * ARRAY_SIZE(lolist_hash) / mgr->remaining;
if (i)
i--;
c = lolist_entry(i, false);
if (!c)
break;
list_del(&c->glnk);
c->gwt = edp_promotion_point(c, balance);
if (c->gwt == cur_index(c))
continue;
balance -= c->states[c->gwt] - cur_level(c);
c->cur = c->states + c->gwt;
if (c->cur == c->req)
mgr->num_denied--;
c->notify_promotion(c->gwt);
}
mgr->remaining = balance;
}
static struct edp_governor bestfit_governor = {
.name = "bestfit",
.owner = THIS_MODULE,
.update_request = bestfit_update_request,
.update_loans = edp_default_update_loans,
.promote = bestfit_promote
};
static int __init bestfit_init(void)
{
return edp_register_governor(&bestfit_governor);
}
postcore_initcall(bestfit_init);
|