This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / include / linux / ckrm_rc.h
1 /* ckrm_rc.h - Header file to be used by Resource controllers of CKRM
2  *
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
7  * 
8  * Provides data structures, macros and kernel API of CKRM for 
9  * resource controllers.
10  *
11  * Latest version, more details at http://ckrm.sf.net
12  * 
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.
17  *
18  */
19
20 /* Changes
21  *
22  * 12 Nov 2003
23  *        Created.
24  */
25
26 #ifndef _LINUX_CKRM_RC_H
27 #define _LINUX_CKRM_RC_H
28
29 #ifdef __KERNEL__
30
31 #ifdef CONFIG_CKRM
32
33 #include <linux/list.h>
34 #include <linux/ckrm.h>
35 #include <linux/ckrm_ce.h>
36 #include <linux/seq_file.h>
37
38 /* maximum number of class types */
39 #define CKRM_MAX_CLASSTYPES         32
40 /* maximum classtype name length */
41 #define CKRM_MAX_CLASSTYPE_NAME     32
42
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
47
48 struct ckrm_core_class;
49 struct ckrm_classtype;
50
51 /*****************************************************************************
52  * Share specifications
53  *****************************************************************************/
54
55 typedef struct ckrm_shares {
56         int my_guarantee;
57         int my_limit;
58         int total_guarantee;
59         int max_limit;
60         int unused_guarantee;   // not used as parameters
61         int cur_max_limit;      // not used as parameters
62 } ckrm_shares_t;
63
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)  
68
69 /******************************************************************************
70  * RESOURCE CONTROLLERS
71  *****************************************************************************/
72
73 /* resource controller callback structure */
74
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
80
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 *);
85
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);
89
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);
95
96         void (*change_resclass) (void *, void *, void *);
97
98 } ckrm_res_ctlr_t;
99
100 /******************************************************************************
101  * CKRM_CLASSTYPE
102  *
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
105  * this dimension
106  *****************************************************************************/
107
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 */
112
113 #define CKRM_MAX_TYPENAME_LEN       32
114
115 typedef struct ckrm_classtype {
116
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];
125
126
127         /* state about my classes */
128
129         struct ckrm_core_class *default_class;  
130         struct list_head classes;  // link all classes of this classtype
131         int num_classes;         
132
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
138
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 
145
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
150
151         /* functions to be called on any class type by external API's */
152
153         struct ckrm_core_class *(*alloc) (struct ckrm_core_class * parent, 
154                                           const char *name);    
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 *);
160
161         int (*reset_stats) (struct ckrm_core_class *, const char *resname,
162                             const char *);
163         int (*set_config) (struct ckrm_core_class *, const char *resname,
164                            const char *cfgstr);
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 *);
168
169         /* functions to be called on a class type by ckrm internals */
170
171         /* class initialization for new RC */
172         void (*add_resctrl) (struct ckrm_core_class *, int resid);      
173
174 } ckrm_classtype_t;
175
176 /******************************************************************************
177  * CKRM CORE CLASS
178  *      common part to any class structure (i.e. instance of a classtype)
179  ******************************************************************************/
180
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
183  */
184
185 #define CKRM_CORE_MAGIC         0xBADCAFFE
186
187 typedef struct ckrm_hnode {
188         struct ckrm_core_class *parent;
189         struct list_head siblings;      
190         struct list_head children;      
191 } ckrm_hnode_t;
192
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 
197
198         
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
202         int magic;
203
204         struct ckrm_hnode hnode;                // hierarchy
205         rwlock_t hnode_rwlock;                  // protects hnode above.
206         atomic_t refcnt;
207         const char *name;
208         int delayed;                            // core deletion delayed 
209                                                 // because of race conditions
210 } ckrm_core_class_t;
211
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)
220
221 /******************************************************************************
222  * OTHER
223  ******************************************************************************/
224
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))
229
230
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 *);
233
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,
238                                 const char *name);
239 extern int ckrm_release_core_class(struct ckrm_core_class *);   
240
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 *);
245
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 *);
250
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);
255
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,
266                                  const char *resname,
267                                  struct ckrm_shares *shares);
268 extern int ckrm_class_reset_stats(struct ckrm_core_class *core,
269                                   const char *resname, const char *unused);
270
271 static inline void ckrm_core_grab(struct ckrm_core_class *core)
272 {
273         if (core)
274                 atomic_inc(&core->refcnt);
275 }
276
277 static inline void ckrm_core_drop(struct ckrm_core_class *core)
278 {
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);
283 }
284
285 static inline unsigned int ckrm_is_core_valid(ckrm_core_class_t * core)
286 {
287         return (core && (core->magic == CKRM_CORE_MAGIC));
288 }
289
290 // iterate through all associate resource controllers:
291 // requires following arguments (ckrm_core_class *cls, 
292 //                               ckrm_res_ctrl   *ctlr,
293 //                               void            *robj,
294 //                               int              bmap)
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]))); });   \
301            )
302
303 extern struct ckrm_classtype *ckrm_classtypes[];        
304
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  *-----------------------------------------------------------------------------*/
312
313 struct ckrm_event_spec {
314         enum ckrm_event ev;
315         struct ckrm_hook_cb cb;
316 };
317 #define CKRM_EVENT_SPEC(EV,FCT) { CKRM_EVENT_##EV, \
318                                         { (ckrm_event_cb)FCT, NULL } }
319
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);
324
325 /******************************************************************************
326  * CE Invocation interface
327  ******************************************************************************/
328
329 #define ce_protect(ctype)      (atomic_inc(&((ctype)->ce_nr_users)))
330 #define ce_release(ctype)      (atomic_dec(&((ctype)->ce_nr_users)))
331
332 // CE Classification callbacks with 
333
334 #define CE_CLASSIFY_NORET(ctype, event, objs_to_classify...)            \
335 do {                                                                    \
336         if ((ctype)->ce_cb_active                                       \
337             && (test_bit(event,&(ctype)->ce_callbacks.c_interest)))     \
338                 (*(ctype)->ce_callbacks.classify)(event,                \
339                                                   objs_to_classify);    \
340 } while (0)
341
342 #define CE_CLASSIFY_RET(ret, ctype, event, objs_to_classify...)         \
343 do {                                                                    \
344         if ((ctype)->ce_cb_active                                       \
345             && (test_bit(event,&(ctype)->ce_callbacks.c_interest)))     \
346                 ret = (*(ctype)->ce_callbacks.classify)(event,          \
347                                                         objs_to_classify);\
348 } while (0)
349
350 #define CE_NOTIFY(ctype, event, cls, objs_to_classify)                  \
351 do {                                                                    \
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);  \
356 } while (0)
357
358 /***************
359  * RCFS related 
360  ***************/
361
362 /* vars needed by other modules/core */
363
364 extern int rcfs_mounted;
365 extern int rcfs_engine_regd;
366
367 #endif                          // CONFIG_CKRM
368
369 #endif                          // __KERNEL__
370
371 #endif                          // _LINUX_CKRM_RC_H