1 /* ckrm_rc.h - Header file to be used by Resource controllers of CKRM
3 * Copyright (C) Hubertus Franke, IBM Corp. 2003
4 * (C) Shailabh Nagar, IBM Corp. 2003
5 * (C) Chandra Seetharaman, IBM Corp. 2003
6 * (C) Vivek Kashyap , IBM Corp. 2004
8 * Provides data structures, macros and kernel API of CKRM for
9 * resource controllers.
11 * Latest version, more details at http://ckrm.sf.net
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
26 #ifndef _LINUX_CKRM_RC_H
27 #define _LINUX_CKRM_RC_H
33 #include <linux/list.h>
34 #include <linux/ckrm.h>
35 #include <linux/ckrm_ce.h>
36 #include <linux/seq_file.h>
38 /* maximum number of class types */
39 #define CKRM_MAX_CLASSTYPES 32
40 /* maximum classtype name length */
41 #define CKRM_MAX_CLASSTYPE_NAME 32
43 /* maximum resource controllers per classtype */
44 #define CKRM_MAX_RES_CTLRS 8
45 /* maximum resource controller name length */
46 #define CKRM_MAX_RES_NAME 128
48 struct ckrm_core_class;
49 struct ckrm_classtype;
51 /*****************************************************************************
52 * Share specifications
53 *****************************************************************************/
55 typedef struct ckrm_shares {
60 int unused_guarantee; // not used as parameters
61 int cur_max_limit; // not used as parameters
64 #define CKRM_SHARE_UNCHANGED (-1)
65 #define CKRM_SHARE_DONTCARE (-2)
66 #define CKRM_SHARE_DFLT_TOTAL_GUARANTEE (100)
67 #define CKRM_SHARE_DFLT_MAX_LIMIT (100)
69 /******************************************************************************
70 * RESOURCE CONTROLLERS
71 *****************************************************************************/
73 /* resource controller callback structure */
75 typedef struct ckrm_res_ctlr {
76 char res_name[CKRM_MAX_RES_NAME];
77 int res_hdepth; // maximum hierarchy
78 int resid; // (for now) same as the enum resid
79 struct ckrm_classtype *classtype; // classtype owning this res ctlr
81 /* allocate/free new resource class object for resource controller */
82 void *(*res_alloc) (struct ckrm_core_class * this,
83 struct ckrm_core_class * parent);
84 void (*res_free) (void *);
86 /* set/get limits/guarantees for a resource controller class */
87 int (*set_share_values) (void *, struct ckrm_shares * shares);
88 int (*get_share_values) (void *, struct ckrm_shares * shares);
90 /* statistics and configuration access */
91 int (*get_stats) (void *, struct seq_file *);
92 int (*reset_stats) (void *);
93 int (*show_config) (void *, struct seq_file *);
94 int (*set_config) (void *, const char *cfgstr);
96 void (*change_resclass) (void *, void *, void *);
100 /******************************************************************************
103 * A <struct ckrm_classtype> object describes a dimension for CKRM to classify
104 * along. Need to provide methods to create and manipulate class objects in
106 *****************************************************************************/
108 /* list of predefined class types, we always recognize */
109 #define CKRM_CLASSTYPE_TASK_CLASS 0
110 #define CKRM_CLASSTYPE_SOCKET_CLASS 1
111 #define CKRM_RESV_CLASSTYPES 2 /* always +1 of last known type */
113 #define CKRM_MAX_TYPENAME_LEN 32
115 typedef struct ckrm_classtype {
117 /* resource controllers */
118 spinlock_t res_ctlrs_lock; // protect res ctlr related data
119 int max_res_ctlrs; // max number of res ctlrs allowed
120 int max_resid; // max resid used
121 int resid_reserved; // max number of reserved controllers
122 long bit_res_ctlrs; // bitmap of resource ID used
123 atomic_t nr_resusers[CKRM_MAX_RES_CTLRS];
124 ckrm_res_ctlr_t *res_ctlrs[CKRM_MAX_RES_CTLRS];
127 /* state about my classes */
129 struct ckrm_core_class *default_class;
130 struct list_head classes; // link all classes of this classtype
133 /* state about my ce interaction */
134 atomic_t ce_regd; // if CE registered
135 int ce_cb_active; // if Callbacks active
136 atomic_t ce_nr_users; // number of active transient calls
137 struct ckrm_eng_callback ce_callbacks; // callback engine
139 // Begin classtype-rcfs private data. No rcfs/fs specific types used.
140 int mfidx; // Index into genmfdesc array used to initialize
141 void *mfdesc; // Array of descriptors of root and magic files
142 int mfcount; // length of above array
143 void *rootde; // root dentry created by rcfs
144 // End rcfs private data
146 char name[CKRM_MAX_TYPENAME_LEN]; // currently same as mfdesc[0]->name
147 // but could be different
148 int typeID; // unique TypeID
149 int maxdepth; // maximum depth supported
151 /* functions to be called on any class type by external API's */
153 struct ckrm_core_class *(*alloc) (struct ckrm_core_class * parent,
155 int (*free) (struct ckrm_core_class * cls);
156 int (*show_members) (struct ckrm_core_class *, struct seq_file *);
157 int (*show_stats) (struct ckrm_core_class *, struct seq_file *);
158 int (*show_config) (struct ckrm_core_class *, struct seq_file *);
159 int (*show_shares) (struct ckrm_core_class *, struct seq_file *);
161 int (*reset_stats) (struct ckrm_core_class *, const char *resname,
163 int (*set_config) (struct ckrm_core_class *, const char *resname,
165 int (*set_shares) (struct ckrm_core_class *, const char *resname,
166 struct ckrm_shares * shares);
167 int (*forced_reclassify) (struct ckrm_core_class *, const char *);
169 /* functions to be called on a class type by ckrm internals */
171 /* class initialization for new RC */
172 void (*add_resctrl) (struct ckrm_core_class *, int resid);
176 /******************************************************************************
178 * common part to any class structure (i.e. instance of a classtype)
179 ******************************************************************************/
181 /* basic definition of a hierarchy that is to be used by the the CORE classes
182 * and can be used by the resource class objects
185 #define CKRM_CORE_MAGIC 0xBADCAFFE
187 typedef struct ckrm_hnode {
188 struct ckrm_core_class *parent;
189 struct list_head siblings;
190 struct list_head children;
193 typedef struct ckrm_core_class {
194 struct ckrm_classtype *classtype;
195 void *res_class[CKRM_MAX_RES_CTLRS]; // resource classes
196 spinlock_t class_lock; // protects list,array above
199 struct list_head objlist; // generic object list
200 struct list_head clslist; // peer classtype classes
201 struct dentry *dentry; // dentry of inode in the RCFS
204 struct ckrm_hnode hnode; // hierarchy
205 rwlock_t hnode_rwlock; // protects hnode above.
208 int delayed; // core deletion delayed
209 // because of race conditions
212 /* type coerce between derived class types and ckrm core class type */
213 #define class_type(type,coreptr) container_of(coreptr,type,core)
214 #define class_core(clsptr) (&(clsptr)->core)
215 /* locking classes */
216 #define class_lock(coreptr) spin_lock(&(coreptr)->class_lock)
217 #define class_unlock(coreptr) spin_unlock(&(coreptr)->class_lock)
218 /* what type is a class of ISA */
219 #define class_isa(clsptr) (class_core(clsptr)->classtype)
221 /******************************************************************************
223 ******************************************************************************/
225 #define ckrm_get_res_class(rescls, resid, type) \
226 ((type*) (((resid != -1) && ((rescls) != NULL) \
227 && ((rescls) != (void *)-1)) ? \
228 ((struct ckrm_core_class *)(rescls))->res_class[resid] : NULL))
231 extern int ckrm_register_res_ctlr(struct ckrm_classtype *, ckrm_res_ctlr_t *);
232 extern int ckrm_unregister_res_ctlr(ckrm_res_ctlr_t *);
234 extern int ckrm_validate_and_grab_core(struct ckrm_core_class *core);
235 extern int ckrm_init_core_class(struct ckrm_classtype *clstype,
236 struct ckrm_core_class *dcore,
237 struct ckrm_core_class *parent,
239 extern int ckrm_release_core_class(struct ckrm_core_class *);
241 extern void ckrm_lock_hier(struct ckrm_core_class *);
242 extern void ckrm_unlock_hier(struct ckrm_core_class *);
243 extern struct ckrm_core_class *ckrm_get_next_child(struct ckrm_core_class *,
244 struct ckrm_core_class *);
246 extern void child_guarantee_changed(struct ckrm_shares *, int, int);
247 extern void child_maxlimit_changed(struct ckrm_shares *, int);
248 extern int set_shares(struct ckrm_shares *, struct ckrm_shares *,
249 struct ckrm_shares *);
251 /* classtype registration and lookup */
252 extern int ckrm_register_classtype(struct ckrm_classtype *clstype);
253 extern int ckrm_unregister_classtype(struct ckrm_classtype *clstype);
254 extern struct ckrm_classtype *ckrm_find_classtype_by_name(const char *name);
256 /* default functions that can be used in classtypes's function table */
257 extern int ckrm_class_show_shares(struct ckrm_core_class *core,
258 struct seq_file *seq);
259 extern int ckrm_class_show_stats(struct ckrm_core_class *core,
260 struct seq_file *seq);
261 extern int ckrm_class_show_config(struct ckrm_core_class *core,
262 struct seq_file *seq);
263 extern int ckrm_class_set_config(struct ckrm_core_class *core,
264 const char *resname, const char *cfgstr);
265 extern int ckrm_class_set_shares(struct ckrm_core_class *core,
267 struct ckrm_shares *shares);
268 extern int ckrm_class_reset_stats(struct ckrm_core_class *core,
269 const char *resname, const char *unused);
271 static inline void ckrm_core_grab(struct ckrm_core_class *core)
274 atomic_inc(&core->refcnt);
277 static inline void ckrm_core_drop(struct ckrm_core_class *core)
279 // only make definition available in this context
280 extern void ckrm_free_core_class(struct ckrm_core_class *core);
281 if (core && (atomic_dec_and_test(&core->refcnt)))
282 ckrm_free_core_class(core);
285 static inline unsigned int ckrm_is_core_valid(ckrm_core_class_t * core)
287 return (core && (core->magic == CKRM_CORE_MAGIC));
290 // iterate through all associate resource controllers:
291 // requires following arguments (ckrm_core_class *cls,
292 // ckrm_res_ctrl *ctlr,
295 #define forall_class_resobjs(cls,rcbs,robj,bmap) \
296 for ( bmap=((cls->classtype)->bit_res_ctlrs) ; \
297 ({ int rid; ((rid=ffs(bmap)-1) >= 0) && \
298 (bmap &= ~(1<<rid), \
299 ((rcbs=cls->classtype->res_ctlrs[rid]) \
300 && (robj=cls->res_class[rid]))); }); \
303 extern struct ckrm_classtype *ckrm_classtypes[];
305 /*-----------------------------------------------------------------------------
306 * CKRM event callback specification for the classtypes or resource controllers
307 * typically an array is specified using CKRM_EVENT_SPEC terminated with
308 * CKRM_EVENT_SPEC_LAST and then that array is registered using
309 * ckrm_register_event_set.
310 * Individual registration of event_cb is also possible
311 *-----------------------------------------------------------------------------*/
313 struct ckrm_event_spec {
315 struct ckrm_hook_cb cb;
317 #define CKRM_EVENT_SPEC(EV,FCT) { CKRM_EVENT_##EV, \
318 { (ckrm_event_cb)FCT, NULL } }
320 int ckrm_register_event_set(struct ckrm_event_spec especs[]);
321 int ckrm_unregister_event_set(struct ckrm_event_spec especs[]);
322 int ckrm_register_event_cb(enum ckrm_event ev, struct ckrm_hook_cb *cb);
323 int ckrm_unregister_event_cb(enum ckrm_event ev, struct ckrm_hook_cb *cb);
325 /******************************************************************************
326 * CE Invocation interface
327 ******************************************************************************/
329 #define ce_protect(ctype) (atomic_inc(&((ctype)->ce_nr_users)))
330 #define ce_release(ctype) (atomic_dec(&((ctype)->ce_nr_users)))
332 // CE Classification callbacks with
334 #define CE_CLASSIFY_NORET(ctype, event, objs_to_classify...) \
336 if ((ctype)->ce_cb_active \
337 && (test_bit(event,&(ctype)->ce_callbacks.c_interest))) \
338 (*(ctype)->ce_callbacks.classify)(event, \
342 #define CE_CLASSIFY_RET(ret, ctype, event, objs_to_classify...) \
344 if ((ctype)->ce_cb_active \
345 && (test_bit(event,&(ctype)->ce_callbacks.c_interest))) \
346 ret = (*(ctype)->ce_callbacks.classify)(event, \
350 #define CE_NOTIFY(ctype, event, cls, objs_to_classify) \
352 if ((ctype)->ce_cb_active \
353 && (test_bit(event,&(ctype)->ce_callbacks.n_interest))) \
354 (*(ctype)->ce_callbacks.notify)(event, \
355 cls,objs_to_classify); \
362 /* vars needed by other modules/core */
364 extern int rcfs_mounted;
365 extern int rcfs_engine_regd;
367 #endif // CONFIG_CKRM
371 #endif // _LINUX_CKRM_RC_H