]>
Commit | Line | Data |
---|---|---|
e552b661 PE |
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 | */ | |
0eea1030 | 26 | unsigned long long usage; |
e552b661 PE |
27 | /* |
28 | * the limit that usage cannot exceed | |
29 | */ | |
0eea1030 | 30 | unsigned long long limit; |
e552b661 PE |
31 | /* |
32 | * the number of unsuccessful attempts to consume the resource | |
33 | */ | |
0eea1030 | 34 | unsigned long long failcnt; |
e552b661 PE |
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/_write - put/get the specified fields from the | |
45 | * res_counter struct to/from the user | |
46 | * | |
47 | * @counter: the counter in question | |
48 | * @member: the field to work with (see RES_xxx below) | |
49 | * @buf: the buffer to opeate on,... | |
50 | * @nbytes: its size... | |
51 | * @pos: and the offset. | |
52 | */ | |
53 | ||
54 | ssize_t res_counter_read(struct res_counter *counter, int member, | |
0eea1030 BS |
55 | const char __user *buf, size_t nbytes, loff_t *pos, |
56 | int (*read_strategy)(unsigned long long val, char *s)); | |
e552b661 | 57 | ssize_t res_counter_write(struct res_counter *counter, int member, |
0eea1030 BS |
58 | const char __user *buf, size_t nbytes, loff_t *pos, |
59 | int (*write_strategy)(char *buf, unsigned long long *val)); | |
e552b661 PE |
60 | |
61 | /* | |
62 | * the field descriptors. one for each member of res_counter | |
63 | */ | |
64 | ||
65 | enum { | |
66 | RES_USAGE, | |
67 | RES_LIMIT, | |
68 | RES_FAILCNT, | |
69 | }; | |
70 | ||
71 | /* | |
72 | * helpers for accounting | |
73 | */ | |
74 | ||
75 | void res_counter_init(struct res_counter *counter); | |
76 | ||
77 | /* | |
78 | * charge - try to consume more resource. | |
79 | * | |
80 | * @counter: the counter | |
81 | * @val: the amount of the resource. each controller defines its own | |
82 | * units, e.g. numbers, bytes, Kbytes, etc | |
83 | * | |
84 | * returns 0 on success and <0 if the counter->usage will exceed the | |
85 | * counter->limit _locked call expects the counter->lock to be taken | |
86 | */ | |
87 | ||
88 | int res_counter_charge_locked(struct res_counter *counter, unsigned long val); | |
89 | int res_counter_charge(struct res_counter *counter, unsigned long val); | |
90 | ||
91 | /* | |
92 | * uncharge - tell that some portion of the resource is released | |
93 | * | |
94 | * @counter: the counter | |
95 | * @val: the amount of the resource | |
96 | * | |
97 | * these calls check for usage underflow and show a warning on the console | |
98 | * _locked call expects the counter->lock to be taken | |
99 | */ | |
100 | ||
101 | void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val); | |
102 | void res_counter_uncharge(struct res_counter *counter, unsigned long val); | |
103 | ||
66e1707b BS |
104 | static inline bool res_counter_limit_check_locked(struct res_counter *cnt) |
105 | { | |
106 | if (cnt->usage < cnt->limit) | |
107 | return true; | |
108 | ||
109 | return false; | |
110 | } | |
111 | ||
112 | /* | |
113 | * Helper function to detect if the cgroup is within it's limit or | |
114 | * not. It's currently called from cgroup_rss_prepare() | |
115 | */ | |
116 | static inline bool res_counter_check_under_limit(struct res_counter *cnt) | |
117 | { | |
118 | bool ret; | |
119 | unsigned long flags; | |
120 | ||
121 | spin_lock_irqsave(&cnt->lock, flags); | |
122 | ret = res_counter_limit_check_locked(cnt); | |
123 | spin_unlock_irqrestore(&cnt->lock, flags); | |
124 | return ret; | |
125 | } | |
126 | ||
e552b661 | 127 | #endif |