1 /* kernel/ckrm/ckrm_cpu_class.c - CPU Class resource controller for CKRM
3 * Copyright (C) Haoqiang Zheng, IBM Corp. 2004
4 * (C) Hubertus Franke, IBM Corp. 2004
6 * Latest version, more details at http://ckrm.sf.net
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <asm/errno.h>
18 #include <linux/sched.h>
19 #include <linux/ckrm.h>
20 #include <linux/ckrm_rc.h>
21 #include <linux/ckrm_tc.h>
22 #include <linux/ckrm_sched.h>
23 #include <linux/ckrm_classqueue.h>
24 #include <linux/seq_file.h>
26 struct ckrm_res_ctlr cpu_rcbs;
29 * insert_cpu_class - insert a class to active_cpu_class list
31 * insert the class in decreasing order of class weight
33 static inline void insert_cpu_class(struct ckrm_cpu_class *cls)
35 list_add(&cls->links,&active_cpu_classes);
39 * initialize a class object and its local queues
41 void init_cpu_class(struct ckrm_cpu_class *cls,ckrm_shares_t* shares)
47 cls->shares = *shares;
48 cls->cnt_lock = SPIN_LOCK_UNLOCKED;
49 ckrm_cpu_stat_init(&cls->stat);
50 cls->magic = CKRM_CPU_CLASS_MAGIC;
52 for (i = 0 ; i < NR_CPUS ; i++) {
53 queue = &cls->local_queues[i];
54 queue->active = queue->arrays;
55 queue->expired = queue->arrays+1;
57 for (j = 0; j < 2; j++) {
58 array = queue->arrays + j;
59 for (k = 0; k < MAX_PRIO; k++) {
60 INIT_LIST_HEAD(array->queue + k);
61 __clear_bit(k, array->bitmap);
63 // delimiter for bitsearch
64 __set_bit(MAX_PRIO, array->bitmap);
68 queue->expired_timestamp = 0;
70 queue->cpu_class = cls;
71 queue->classqueue = get_cpu_classqueue(i);
72 queue->top_priority = MAX_PRIO;
73 cq_node_init(&queue->classqueue_linkobj);
74 queue->local_cvt = CVT_INTERACTIVE_BONUS;
76 queue->local_weight = cpu_class_weight(cls);
77 queue->uncounted_ns = 0;
78 queue->magic = 0x43FF43D7;
82 write_lock(&class_list_lock);
83 insert_cpu_class(cls);
84 write_unlock(&class_list_lock);
87 static inline void set_default_share(ckrm_shares_t *shares)
89 shares->my_guarantee = 0;
90 shares->total_guarantee = CKRM_SHARE_DFLT_TOTAL_GUARANTEE;
91 shares->unused_guarantee = CKRM_SHARE_DFLT_TOTAL_GUARANTEE;
92 shares->my_limit = CKRM_SHARE_DFLT_MAX_LIMIT;
93 shares->max_limit = CKRM_SHARE_DFLT_MAX_LIMIT;
94 shares->cur_max_limit = 0;
97 struct ckrm_cpu_class * ckrm_get_cpu_class(struct ckrm_core_class *core)
99 struct ckrm_cpu_class * cls;
100 cls = ckrm_get_res_class(core, cpu_rcbs.resid, struct ckrm_cpu_class);
101 if (valid_cpu_class(cls))
108 void* ckrm_alloc_cpu_class(struct ckrm_core_class *core, struct ckrm_core_class *parent)
110 struct ckrm_cpu_class *cls;
112 if (! parent) /*root class*/
113 cls = get_default_cpu_class();
115 cls = (struct ckrm_cpu_class *) kmalloc(sizeof(struct ckrm_cpu_class),GFP_ATOMIC);
118 ckrm_shares_t shares;
119 if ((! parent) && (core)) {
121 * the default class is already initialized
122 * so only update the core structure
126 set_default_share(&shares);
127 init_cpu_class(cls,&shares);
129 cls->parent = parent;
132 printk(KERN_ERR"alloc_cpu_class failed\n");
138 * hzheng: this is not a stable implementation
139 * need to check race condition issue here
141 static void ckrm_free_cpu_class(void *my_res)
143 struct ckrm_cpu_class *cls = my_res, *parres, *childres;
144 ckrm_core_class_t *child = NULL;
150 /*the default class can't be freed*/
151 if (cls == get_default_cpu_class())
154 // Assuming there will be no children when this function is called
155 parres = ckrm_get_cpu_class(cls->parent);
157 // return child's limit/guarantee to parent node
158 spin_lock(&parres->cnt_lock);
159 child_guarantee_changed(&parres->shares, cls->shares.my_guarantee, 0);
160 // run thru parent's children and get the new max_limit of the parent
161 ckrm_lock_hier(parres->core);
163 while ((child = ckrm_get_next_child(parres->core, child)) != NULL) {
164 childres = ckrm_get_cpu_class(child);
165 if (maxlimit < childres->shares.my_limit) {
166 maxlimit = childres->shares.my_limit;
169 ckrm_unlock_hier(parres->core);
170 if (parres->shares.cur_max_limit < maxlimit) {
171 parres->shares.cur_max_limit = maxlimit;
174 spin_unlock(&parres->cnt_lock);
176 write_lock(&class_list_lock);
177 list_del(&cls->links);
178 write_unlock(&class_list_lock);
184 * the system will adjust to the new share automatically
186 int ckrm_cpu_set_share(void *my_res, struct ckrm_shares *new_share)
188 struct ckrm_cpu_class *parres, *cls = my_res;
189 struct ckrm_shares *cur = &cls->shares, *par;
196 parres = ckrm_get_cpu_class(cls->parent);
197 spin_lock(&parres->cnt_lock);
198 spin_lock(&cls->cnt_lock);
199 par = &parres->shares;
201 spin_lock(&cls->cnt_lock);
207 * hzheng: CKRM_SHARE_DONTCARE should be handled
209 if (new_share->my_guarantee == CKRM_SHARE_DONTCARE)
210 new_share->my_guarantee = 0;
212 rc = set_shares(new_share, cur, par);
213 if (cur->my_limit == CKRM_SHARE_DONTCARE)
214 cur->my_limit = cur->max_limit;
217 spin_unlock(&cls->cnt_lock);
219 spin_unlock(&parres->cnt_lock);
224 static int ckrm_cpu_get_share(void *my_res,
225 struct ckrm_shares *shares)
227 struct ckrm_cpu_class *cls = my_res;
231 *shares = cls->shares;
235 int ckrm_cpu_get_stats(void *my_res, struct seq_file * sfile)
237 struct ckrm_cpu_class *cls = my_res;
238 struct ckrm_cpu_class_stat* stat = &cls->stat;
245 seq_printf(sfile, "-------- CPU Class Status Start---------\n");
246 seq_printf(sfile, "Share:\n\tgrt= %d limit= %d total_grt= %d max_limit= %d\n",
247 cls->shares.my_guarantee,
248 cls->shares.my_limit,
249 cls->shares.total_guarantee,
250 cls->shares.max_limit);
251 seq_printf(sfile, "\tunused_grt= %d cur_max_limit= %d\n",
252 cls->shares.unused_guarantee,
253 cls->shares.cur_max_limit);
255 seq_printf(sfile, "Effective:\n\tegrt= %d\n",stat->egrt);
256 seq_printf(sfile, "\tmegrt= %d\n",stat->megrt);
257 seq_printf(sfile, "\tehl= %d\n",stat->ehl);
258 seq_printf(sfile, "\tmehl= %d\n",stat->mehl);
259 seq_printf(sfile, "\teshare= %d\n",stat->eshare);
260 seq_printf(sfile, "\tmeshare= %d\n",cpu_class_weight(cls));
261 seq_printf(sfile, "\ttotal_ns= %llu\n",stat->total_ns);
262 seq_printf(sfile, "\tmax_demand= %lu\n",stat->max_demand);
263 for_each_online_cpu(i) {
264 lrq = get_ckrm_lrq(cls,i);
265 seq_printf(sfile, "\tlrq %d demand= %lu weight= %d lrq_load= %lu cvt= %llu\n",i,stat->local_stats[i].cpu_demand,local_class_weight(lrq),lrq->lrq_load,lrq->local_cvt);
268 seq_printf(sfile, "-------- CPU Class Status END ---------\n");
274 * task will remain in the same cpu but on a different local runqueue
276 void ckrm_cpu_change_class(void *task, void *old, void *new)
278 struct task_struct *tsk = task;
279 struct ckrm_cpu_class *newcls = new;
282 if (!task || ! old || !new)
285 _ckrm_cpu_change_class(tsk,newcls);
288 /*dummy function, not used*/
289 static int ckrm_cpu_show_config(void *my_res, struct seq_file *sfile)
291 struct ckrm_cpu_class *cls = my_res;
296 seq_printf(sfile, "cls=%s,parameter=somevalue\n","ckrm_cpu class");
300 /*dummy function, not used*/
301 static int ckrm_cpu_set_config(void *my_res, const char *cfgstr)
303 struct ckrm_cpu_class *cls = my_res;
307 printk("ckrm_cpu config='%s'\n",cfgstr);
311 struct ckrm_res_ctlr cpu_rcbs = {
315 .res_alloc = ckrm_alloc_cpu_class,
316 .res_free = ckrm_free_cpu_class,
317 .set_share_values = ckrm_cpu_set_share,
318 .get_share_values = ckrm_cpu_get_share,
319 .get_stats = ckrm_cpu_get_stats,
320 .show_config = ckrm_cpu_show_config,
321 .set_config = ckrm_cpu_set_config,
322 .change_resclass = ckrm_cpu_change_class,
325 int __init init_ckrm_sched_res(void)
327 struct ckrm_classtype *clstype;
328 int resid = cpu_rcbs.resid;
330 clstype = ckrm_find_classtype_by_name("taskclass");
331 if (clstype == NULL) {
332 printk(KERN_INFO" Unknown ckrm classtype<taskclass>");
336 if (resid == -1) { /*not registered */
337 resid = ckrm_register_res_ctlr(clstype,&cpu_rcbs);
338 printk("........init_ckrm_sched_res , resid= %d\n",resid);
344 * initialize the class structure
345 * add the default class: class 0
347 void init_cpu_classes(void)
351 //init classqueues for each processor
352 for (i=0; i < NR_CPUS; i++)
353 classqueue_init(get_cpu_classqueue(i));
356 * hzheng: initialize the default cpu class
357 * required for E14/E15 since ckrm_init is called after sched_init
359 ckrm_alloc_cpu_class(NULL,NULL);
363 EXPORT_SYMBOL(ckrm_get_cpu_class);