1 /* ckrm_numtasks.c - "Number of tasks" resource controller for CKRM
3 * Copyright (C) Chandra Seetharaman, IBM Corp. 2003
5 * Latest version, more details at http://ckrm.sf.net
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
16 * 31 Mar 2004: Created
21 * Code Description: TBD
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <asm/errno.h>
28 #include <asm/div64.h>
29 #include <linux/list.h>
30 #include <linux/spinlock.h>
31 #include <linux/parser.h>
32 #include <linux/ckrm_rc.h>
33 #include <linux/ckrm_tc.h>
34 #include <linux/ckrm_tsk.h>
36 #define DEF_TOTAL_NUM_TASKS (131072) // 128 K
37 #define DEF_FORKRATE (1000000) // 1 million tasks
38 #define DEF_FORKRATE_INTERVAL (3600) // per hour
39 #define NUMTASKS_DEBUG
40 #define NUMTASKS_NAME "numtasks"
41 #define SYS_TOTAL_TASKS "sys_total_tasks"
42 #define FORKRATE "forkrate"
43 #define FORKRATE_INTERVAL "forkrate_interval"
45 static int total_numtasks = DEF_TOTAL_NUM_TASKS;
46 static int total_cnt_alloc = 0;
47 static int forkrate = DEF_FORKRATE;
48 static int forkrate_interval = DEF_FORKRATE_INTERVAL;
49 static ckrm_core_class_t *root_core;
51 typedef struct ckrm_numtasks {
52 struct ckrm_core_class *core; // the core i am part of...
53 struct ckrm_core_class *parent; // parent of the core above.
54 struct ckrm_shares shares;
55 spinlock_t cnt_lock; // always grab parent's lock before child's
56 int cnt_guarantee; // num_tasks guarantee in local units
57 int cnt_unused; // has to borrow if more than this is needed
58 int cnt_limit; // no tasks over this limit.
59 atomic_t cnt_cur_alloc; // current alloc from self
60 atomic_t cnt_borrowed; // borrowed from the parent
62 int over_guarantee; // turn on/off when cur_alloc goes
63 // over/under guarantee
65 // internally maintained statictics to compare with max numbers
66 int limit_failures; // # failures as request was over the limit
67 int borrow_sucesses; // # successful borrows
68 int borrow_failures; // # borrow failures
70 // Maximum the specific statictics has reached.
71 int max_limit_failures;
72 int max_borrow_sucesses;
73 int max_borrow_failures;
75 // Total number of specific statistics
76 int tot_limit_failures;
77 int tot_borrow_sucesses;
78 int tot_borrow_failures;
82 unsigned long period_start;
85 struct ckrm_res_ctlr numtasks_rcbs;
87 /* Initialize rescls values
88 * May be called on each rcfs unmount or as part of error recovery
89 * to make share values sane.
90 * Does not traverse hierarchy reinitializing children.
92 static void numtasks_res_initcls_one(ckrm_numtasks_t * res)
94 res->shares.my_guarantee = CKRM_SHARE_DONTCARE;
95 res->shares.my_limit = CKRM_SHARE_DONTCARE;
96 res->shares.total_guarantee = CKRM_SHARE_DFLT_TOTAL_GUARANTEE;
97 res->shares.max_limit = CKRM_SHARE_DFLT_MAX_LIMIT;
98 res->shares.unused_guarantee = CKRM_SHARE_DFLT_TOTAL_GUARANTEE;
99 res->shares.cur_max_limit = 0;
101 res->cnt_guarantee = CKRM_SHARE_DONTCARE;
102 res->cnt_unused = CKRM_SHARE_DONTCARE;
103 res->cnt_limit = CKRM_SHARE_DONTCARE;
105 res->over_guarantee = 0;
107 res->limit_failures = 0;
108 res->borrow_sucesses = 0;
109 res->borrow_failures = 0;
111 res->max_limit_failures = 0;
112 res->max_borrow_sucesses = 0;
113 res->max_borrow_failures = 0;
115 res->tot_limit_failures = 0;
116 res->tot_borrow_sucesses = 0;
117 res->tot_borrow_failures = 0;
119 res->forks_in_period = 0;
120 res->period_start = jiffies;
122 atomic_set(&res->cnt_cur_alloc, 0);
123 atomic_set(&res->cnt_borrowed, 0);
128 static void numtasks_res_initcls(void *my_res)
130 ckrm_numtasks_t *res = my_res;
132 /* Write a version which propagates values all the way down
133 and replace rcbs callback with that version */
138 static int numtasks_get_ref_local(void *arg, int force)
140 int rc, resid = numtasks_rcbs.resid, borrowed = 0;
141 unsigned long now = jiffies, chg_at;
142 ckrm_numtasks_t *res;
143 ckrm_core_class_t *core = arg;
145 if ((resid < 0) || (core == NULL))
148 res = ckrm_get_res_class(core, resid, ckrm_numtasks_t);
152 // force is not associated with fork. So, if force is specified
153 // we don't have to bother about forkrate.
155 // Take care of wraparound situation
156 chg_at = res->period_start + forkrate_interval * HZ;
157 if (chg_at < res->period_start) {
158 chg_at += forkrate_interval * HZ;
159 now += forkrate_interval * HZ;
162 res->period_start = now;
163 res->forks_in_period = 0;
166 if (res->forks_in_period >= forkrate) {
171 atomic_inc(&res->cnt_cur_alloc);
174 if (((res->parent) && (res->cnt_unused == CKRM_SHARE_DONTCARE)) ||
175 (atomic_read(&res->cnt_cur_alloc) > res->cnt_unused)) {
178 if (!force && (res->cnt_limit != CKRM_SHARE_DONTCARE) &&
179 (atomic_read(&res->cnt_cur_alloc) > res->cnt_limit)) {
180 res->limit_failures++;
181 res->tot_limit_failures++;
182 } else if (res->parent != NULL) {
184 numtasks_get_ref_local(res->parent, force)) == 1) {
185 atomic_inc(&res->cnt_borrowed);
186 res->borrow_sucesses++;
187 res->tot_borrow_sucesses++;
188 res->over_guarantee = 1;
191 res->borrow_failures++;
192 res->tot_borrow_failures++;
197 } else if (res->over_guarantee) {
198 res->over_guarantee = 0;
200 if (res->max_limit_failures < res->limit_failures) {
201 res->max_limit_failures = res->limit_failures;
203 if (res->max_borrow_sucesses < res->borrow_sucesses) {
204 res->max_borrow_sucesses = res->borrow_sucesses;
206 if (res->max_borrow_failures < res->borrow_failures) {
207 res->max_borrow_failures = res->borrow_failures;
209 res->limit_failures = 0;
210 res->borrow_sucesses = 0;
211 res->borrow_failures = 0;
215 atomic_dec(&res->cnt_cur_alloc);
216 } else if (!borrowed) {
218 if (!force) { // force is not associated with a real fork.
219 res->forks_in_period++;
225 static void numtasks_put_ref_local(void *arg)
227 int resid = numtasks_rcbs.resid;
228 ckrm_numtasks_t *res;
229 ckrm_core_class_t *core = arg;
231 if ((resid == -1) || (core == NULL)) {
235 res = ckrm_get_res_class(core, resid, ckrm_numtasks_t);
238 if (unlikely(atomic_read(&res->cnt_cur_alloc) == 0)) {
239 printk(KERN_WARNING "numtasks_put_ref: Trying to decrement "
240 "counter below 0\n");
243 atomic_dec(&res->cnt_cur_alloc);
244 if (atomic_read(&res->cnt_borrowed) > 0) {
245 atomic_dec(&res->cnt_borrowed);
246 numtasks_put_ref_local(res->parent);
254 static void *numtasks_res_alloc(struct ckrm_core_class *core,
255 struct ckrm_core_class *parent)
257 ckrm_numtasks_t *res;
259 res = kmalloc(sizeof(ckrm_numtasks_t), GFP_ATOMIC);
262 memset(res, 0, sizeof(ckrm_numtasks_t));
264 res->parent = parent;
265 numtasks_res_initcls_one(res);
266 res->cnt_lock = SPIN_LOCK_UNLOCKED;
267 if (parent == NULL) {
268 // I am part of root class. So set the max tasks
269 // to available default
270 res->cnt_guarantee = total_numtasks;
271 res->cnt_unused = total_numtasks;
272 res->cnt_limit = total_numtasks;
273 root_core = core; // store the root core.
275 try_module_get(THIS_MODULE);
278 "numtasks_res_alloc: failed GFP_ATOMIC alloc\n");
284 * No locking of this resource class object necessary as we are not
285 * supposed to be assigned (or used) when/after this function is called.
287 static void numtasks_res_free(void *my_res)
289 ckrm_numtasks_t *res = my_res, *parres, *childres;
290 ckrm_core_class_t *child = NULL;
291 int i, borrowed, maxlimit, resid = numtasks_rcbs.resid;
296 // Assuming there will be no children when this function is called
298 parres = ckrm_get_res_class(res->parent, resid, ckrm_numtasks_t);
300 if (unlikely(atomic_read(&res->cnt_cur_alloc) < 0)) {
301 printk(KERN_WARNING "numtasks_res: counter below 0\n");
303 if (unlikely(atomic_read(&res->cnt_cur_alloc) > 0 ||
304 atomic_read(&res->cnt_borrowed) > 0)) {
305 printk(KERN_WARNING "numtasks_res_free: resource still "
306 "alloc'd %p\n", res);
307 if ((borrowed = atomic_read(&res->cnt_borrowed)) > 0) {
308 for (i = 0; i < borrowed; i++) {
309 numtasks_put_ref_local(parres->core);
313 // return child's limit/guarantee to parent node
314 spin_lock(&parres->cnt_lock);
315 child_guarantee_changed(&parres->shares, res->shares.my_guarantee, 0);
317 // run thru parent's children and get the new max_limit of the parent
318 ckrm_lock_hier(parres->core);
320 while ((child = ckrm_get_next_child(parres->core, child)) != NULL) {
321 childres = ckrm_get_res_class(child, resid, ckrm_numtasks_t);
322 if (maxlimit < childres->shares.my_limit) {
323 maxlimit = childres->shares.my_limit;
326 ckrm_unlock_hier(parres->core);
327 if (parres->shares.cur_max_limit < maxlimit) {
328 parres->shares.cur_max_limit = maxlimit;
331 spin_unlock(&parres->cnt_lock);
333 module_put(THIS_MODULE);
339 * Recalculate the guarantee and limit in real units... and propagate the
341 * Caller is responsible for protecting res and for the integrity of parres
344 recalc_and_propagate(ckrm_numtasks_t * res, ckrm_numtasks_t * parres)
346 ckrm_core_class_t *child = NULL;
347 ckrm_numtasks_t *childres;
348 int resid = numtasks_rcbs.resid;
351 struct ckrm_shares *par = &parres->shares;
352 struct ckrm_shares *self = &res->shares;
354 // calculate cnt_guarantee and cnt_limit
356 if (parres->cnt_guarantee == CKRM_SHARE_DONTCARE) {
357 res->cnt_guarantee = CKRM_SHARE_DONTCARE;
358 } else if (par->total_guarantee) {
359 u64 temp = (u64) self->my_guarantee * parres->cnt_guarantee;
360 do_div(temp, par->total_guarantee);
361 res->cnt_guarantee = (int) temp;
363 res->cnt_guarantee = 0;
366 if (parres->cnt_limit == CKRM_SHARE_DONTCARE) {
367 res->cnt_limit = CKRM_SHARE_DONTCARE;
368 } else if (par->max_limit) {
369 u64 temp = (u64) self->my_limit * parres->cnt_limit;
370 do_div(temp, par->max_limit);
371 res->cnt_limit = (int) temp;
376 // Calculate unused units
377 if (res->cnt_guarantee == CKRM_SHARE_DONTCARE) {
378 res->cnt_unused = CKRM_SHARE_DONTCARE;
379 } else if (self->total_guarantee) {
380 u64 temp = (u64) self->unused_guarantee * res->cnt_guarantee;
381 do_div(temp, self->total_guarantee);
382 res->cnt_unused = (int) temp;
387 // propagate to children
388 ckrm_lock_hier(res->core);
389 while ((child = ckrm_get_next_child(res->core, child)) != NULL) {
390 childres = ckrm_get_res_class(child, resid, ckrm_numtasks_t);
392 spin_lock(&childres->cnt_lock);
393 recalc_and_propagate(childres, res);
394 spin_unlock(&childres->cnt_lock);
396 printk(KERN_ERR "%s: numtasks resclass missing\n",__FUNCTION__);
399 ckrm_unlock_hier(res->core);
403 static int numtasks_set_share_values(void *my_res, struct ckrm_shares *new)
405 ckrm_numtasks_t *parres, *res = my_res;
406 struct ckrm_shares *cur = &res->shares, *par;
407 int rc = -EINVAL, resid = numtasks_rcbs.resid;
414 ckrm_get_res_class(res->parent, resid, ckrm_numtasks_t);
415 spin_lock(&parres->cnt_lock);
416 spin_lock(&res->cnt_lock);
417 par = &parres->shares;
419 spin_lock(&res->cnt_lock);
424 rc = set_shares(new, cur, par);
426 if ((rc == 0) && parres) {
427 // Calculate parent's unused units
428 if (parres->cnt_guarantee == CKRM_SHARE_DONTCARE) {
429 parres->cnt_unused = CKRM_SHARE_DONTCARE;
430 } else if (par->total_guarantee) {
431 u64 temp = (u64) par->unused_guarantee * parres->cnt_guarantee;
432 do_div(temp, par->total_guarantee);
433 parres->cnt_unused = (int) temp;
435 parres->cnt_unused = 0;
437 recalc_and_propagate(res, parres);
439 spin_unlock(&res->cnt_lock);
441 spin_unlock(&parres->cnt_lock);
446 static int numtasks_get_share_values(void *my_res, struct ckrm_shares *shares)
448 ckrm_numtasks_t *res = my_res;
452 *shares = res->shares;
456 static int numtasks_get_stats(void *my_res, struct seq_file *sfile)
458 ckrm_numtasks_t *res = my_res;
463 seq_printf(sfile, "Number of tasks resource:\n");
464 seq_printf(sfile, "Total Over limit failures: %d\n",
465 res->tot_limit_failures);
466 seq_printf(sfile, "Total Over guarantee sucesses: %d\n",
467 res->tot_borrow_sucesses);
468 seq_printf(sfile, "Total Over guarantee failures: %d\n",
469 res->tot_borrow_failures);
471 seq_printf(sfile, "Maximum Over limit failures: %d\n",
472 res->max_limit_failures);
473 seq_printf(sfile, "Maximum Over guarantee sucesses: %d\n",
474 res->max_borrow_sucesses);
475 seq_printf(sfile, "Maximum Over guarantee failures: %d\n",
476 res->max_borrow_failures);
477 #ifdef NUMTASKS_DEBUG
479 "cur_alloc %d; borrowed %d; cnt_guar %d; cnt_limit %d "
480 "cnt_unused %d, unused_guarantee %d, cur_max_limit %d\n",
481 atomic_read(&res->cnt_cur_alloc),
482 atomic_read(&res->cnt_borrowed), res->cnt_guarantee,
483 res->cnt_limit, res->cnt_unused,
484 res->shares.unused_guarantee,
485 res->shares.cur_max_limit);
491 static int numtasks_show_config(void *my_res, struct seq_file *sfile)
493 ckrm_numtasks_t *res = my_res;
498 seq_printf(sfile, "res=%s,%s=%d,%s=%d,%s=%d\n", NUMTASKS_NAME,
499 SYS_TOTAL_TASKS, total_numtasks,
501 FORKRATE_INTERVAL, forkrate_interval);
505 enum numtasks_token_t {
506 numtasks_token_total,
507 numtasks_token_forkrate,
508 numtasks_token_interval,
512 static match_table_t numtasks_tokens = {
513 {numtasks_token_total, SYS_TOTAL_TASKS "=%d"},
514 {numtasks_token_forkrate, FORKRATE "=%d"},
515 {numtasks_token_interval, FORKRATE_INTERVAL "=%d"},
516 {numtasks_token_err, NULL},
519 static void reset_forkrates(ckrm_core_class_t *parent, unsigned long now)
521 ckrm_numtasks_t *parres;
522 ckrm_core_class_t *child = NULL;
524 parres = ckrm_get_res_class(parent, numtasks_rcbs.resid,
529 parres->forks_in_period = 0;
530 parres->period_start = now;
532 ckrm_lock_hier(parent);
533 while ((child = ckrm_get_next_child(parent, child)) != NULL) {
534 reset_forkrates(child, now);
536 ckrm_unlock_hier(parent);
539 static int numtasks_set_config(void *my_res, const char *cfgstr)
542 ckrm_numtasks_t *res = my_res;
543 int new_total, fr = 0, itvl = 0, err = 0;
548 while ((p = strsep((char**)&cfgstr, ",")) != NULL) {
549 substring_t args[MAX_OPT_ARGS];
554 token = match_token(p, numtasks_tokens, args);
556 case numtasks_token_total:
557 if (match_int(args, &new_total) ||
558 (new_total < total_cnt_alloc)) {
561 total_numtasks = new_total;
563 // res is the default class, as config is present only
565 spin_lock(&res->cnt_lock);
566 res->cnt_guarantee = total_numtasks;
567 res->cnt_unused = total_numtasks;
568 res->cnt_limit = total_numtasks;
569 recalc_and_propagate(res, NULL);
570 spin_unlock(&res->cnt_lock);
573 case numtasks_token_forkrate:
574 if (match_int(args, &fr) || (fr <= 0)) {
580 case numtasks_token_interval:
581 if (match_int(args, &itvl) || (itvl <= 0)) {
584 forkrate_interval = itvl;
591 if ((fr > 0) || (itvl > 0)) {
592 reset_forkrates(root_core, jiffies);
597 static void numtasks_change_resclass(void *task, void *old, void *new)
599 ckrm_numtasks_t *oldres = old;
600 ckrm_numtasks_t *newres = new;
602 if (oldres != (void *)-1) {
603 struct task_struct *tsk = task;
605 struct ckrm_core_class *old_core =
606 &(tsk->parent->taskclass->core);
608 ckrm_get_res_class(old_core, numtasks_rcbs.resid,
611 numtasks_put_ref_local(oldres->core);
614 (void)numtasks_get_ref_local(newres->core, 1);
618 struct ckrm_res_ctlr numtasks_rcbs = {
619 .res_name = NUMTASKS_NAME,
622 .res_alloc = numtasks_res_alloc,
623 .res_free = numtasks_res_free,
624 .set_share_values = numtasks_set_share_values,
625 .get_share_values = numtasks_get_share_values,
626 .get_stats = numtasks_get_stats,
627 .show_config = numtasks_show_config,
628 .set_config = numtasks_set_config,
629 .change_resclass = numtasks_change_resclass,
632 int __init init_ckrm_numtasks_res(void)
634 struct ckrm_classtype *clstype;
635 int resid = numtasks_rcbs.resid;
637 clstype = ckrm_find_classtype_by_name("taskclass");
638 if (clstype == NULL) {
639 printk(KERN_INFO " Unknown ckrm classtype<taskclass>");
644 resid = ckrm_register_res_ctlr(clstype, &numtasks_rcbs);
645 printk(KERN_DEBUG "........init_ckrm_numtasks_res -> %d\n", resid);
647 ckrm_numtasks_register(numtasks_get_ref_local,
648 numtasks_put_ref_local);
649 numtasks_rcbs.classtype = clstype;
655 void __exit exit_ckrm_numtasks_res(void)
657 if (numtasks_rcbs.resid != -1) {
658 ckrm_numtasks_register(NULL, NULL);
660 ckrm_unregister_res_ctlr(&numtasks_rcbs);
661 numtasks_rcbs.resid = -1;
664 module_init(init_ckrm_numtasks_res)
665 module_exit(exit_ckrm_numtasks_res)
667 MODULE_LICENSE("GPL");