CGroup API files: add res_counter_read_u64()
[safe/jmp/linux-2.6] / include / linux / res_counter.h
1 #ifndef __RES_COUNTER_H__
2 #define __RES_COUNTER_H__
3
4 /*
5  * Resource Counters
6  * Contain common data types and routines for resource accounting
7  *
8  * Copyright 2007 OpenVZ SWsoft Inc
9  *
10  * Author: Pavel Emelianov <xemul@openvz.org>
11  *
12  */
13
14 #include <linux/cgroup.h>
15
16 /*
17  * The core object. the cgroup that wishes to account for some
18  * resource may include this counter into its structures and use
19  * the helpers described beyond
20  */
21
22 struct res_counter {
23         /*
24          * the current resource consumption level
25          */
26         unsigned long long usage;
27         /*
28          * the limit that usage cannot exceed
29          */
30         unsigned long long limit;
31         /*
32          * the number of unsuccessful attempts to consume the resource
33          */
34         unsigned long long failcnt;
35         /*
36          * the lock to protect all of the above.
37          * the routines below consider this to be IRQ-safe
38          */
39         spinlock_t lock;
40 };
41
42 /**
43  * Helpers to interact with userspace
44  * res_counter_read_u64() - returns the value of the specified member.
45  * res_counter_read/_write - put/get the specified fields from the
46  * res_counter struct to/from the user
47  *
48  * @counter:     the counter in question
49  * @member:  the field to work with (see RES_xxx below)
50  * @buf:     the buffer to opeate on,...
51  * @nbytes:  its size...
52  * @pos:     and the offset.
53  */
54
55 u64 res_counter_read_u64(struct res_counter *counter, int member);
56
57 ssize_t res_counter_read(struct res_counter *counter, int member,
58                 const char __user *buf, size_t nbytes, loff_t *pos,
59                 int (*read_strategy)(unsigned long long val, char *s));
60 ssize_t res_counter_write(struct res_counter *counter, int member,
61                 const char __user *buf, size_t nbytes, loff_t *pos,
62                 int (*write_strategy)(char *buf, unsigned long long *val));
63
64 /*
65  * the field descriptors. one for each member of res_counter
66  */
67
68 enum {
69         RES_USAGE,
70         RES_LIMIT,
71         RES_FAILCNT,
72 };
73
74 /*
75  * helpers for accounting
76  */
77
78 void res_counter_init(struct res_counter *counter);
79
80 /*
81  * charge - try to consume more resource.
82  *
83  * @counter: the counter
84  * @val: the amount of the resource. each controller defines its own
85  *       units, e.g. numbers, bytes, Kbytes, etc
86  *
87  * returns 0 on success and <0 if the counter->usage will exceed the
88  * counter->limit _locked call expects the counter->lock to be taken
89  */
90
91 int res_counter_charge_locked(struct res_counter *counter, unsigned long val);
92 int res_counter_charge(struct res_counter *counter, unsigned long val);
93
94 /*
95  * uncharge - tell that some portion of the resource is released
96  *
97  * @counter: the counter
98  * @val: the amount of the resource
99  *
100  * these calls check for usage underflow and show a warning on the console
101  * _locked call expects the counter->lock to be taken
102  */
103
104 void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val);
105 void res_counter_uncharge(struct res_counter *counter, unsigned long val);
106
107 static inline bool res_counter_limit_check_locked(struct res_counter *cnt)
108 {
109         if (cnt->usage < cnt->limit)
110                 return true;
111
112         return false;
113 }
114
115 /*
116  * Helper function to detect if the cgroup is within it's limit or
117  * not. It's currently called from cgroup_rss_prepare()
118  */
119 static inline bool res_counter_check_under_limit(struct res_counter *cnt)
120 {
121         bool ret;
122         unsigned long flags;
123
124         spin_lock_irqsave(&cnt->lock, flags);
125         ret = res_counter_limit_check_locked(cnt);
126         spin_unlock_irqrestore(&cnt->lock, flags);
127         return ret;
128 }
129
130 #endif