1 #ifndef __RES_COUNTER_H__
2 #define __RES_COUNTER_H__
6 * Contain common data types and routines for resource accounting
8 * Copyright 2007 OpenVZ SWsoft Inc
10 * Author: Pavel Emelianov <xemul@openvz.org>
12 * See Documentation/cgroups/resource_counter.txt for more
13 * info about what this counter is.
16 #include <linux/cgroup.h>
19 * The core object. the cgroup that wishes to account for some
20 * resource may include this counter into its structures and use
21 * the helpers described beyond
26 * the current resource consumption level
28 unsigned long long usage;
30 * the maximal value of the usage from the counter creation
32 unsigned long long max_usage;
34 * the limit that usage cannot exceed
36 unsigned long long limit;
38 * the limit that usage can be exceed
40 unsigned long long soft_limit;
42 * the number of unsuccessful attempts to consume the resource
44 unsigned long long failcnt;
46 * the lock to protect all of the above.
47 * the routines below consider this to be IRQ-safe
51 * Parent counter, used for hierarchial resource accounting
53 struct res_counter *parent;
56 #define RESOURCE_MAX (unsigned long long)LLONG_MAX
59 * Helpers to interact with userspace
60 * res_counter_read_u64() - returns the value of the specified member.
61 * res_counter_read/_write - put/get the specified fields from the
62 * res_counter struct to/from the user
64 * @counter: the counter in question
65 * @member: the field to work with (see RES_xxx below)
66 * @buf: the buffer to opeate on,...
67 * @nbytes: its size...
68 * @pos: and the offset.
71 u64 res_counter_read_u64(struct res_counter *counter, int member);
73 ssize_t res_counter_read(struct res_counter *counter, int member,
74 const char __user *buf, size_t nbytes, loff_t *pos,
75 int (*read_strategy)(unsigned long long val, char *s));
77 typedef int (*write_strategy_fn)(const char *buf, unsigned long long *val);
79 int res_counter_memparse_write_strategy(const char *buf,
80 unsigned long long *res);
82 int res_counter_write(struct res_counter *counter, int member,
83 const char *buffer, write_strategy_fn write_strategy);
85 void res_counter_write_u64(struct res_counter *counter, int member, u64 val);
87 void res_counter_inherit(struct res_counter *counter, int member);
90 * the field descriptors. one for each member of res_counter
102 * helpers for accounting
105 void res_counter_init(struct res_counter *counter, struct res_counter *parent);
108 * charge - try to consume more resource.
110 * @counter: the counter
111 * @val: the amount of the resource. each controller defines its own
112 * units, e.g. numbers, bytes, Kbytes, etc
114 * returns 0 on success and <0 if the counter->usage will exceed the
115 * counter->limit _locked call expects the counter->lock to be taken
118 int __must_check res_counter_charge_locked(struct res_counter *counter,
120 int __must_check res_counter_charge_until(struct res_counter *counter,
121 struct res_counter *limit,
123 struct res_counter **limit_fail_at);
124 static inline int __must_check
125 res_counter_charge(struct res_counter *counter, unsigned long val,
126 struct res_counter **limit_fail_at)
128 return res_counter_charge_until(counter, NULL, val, limit_fail_at);
132 * uncharge - tell that some portion of the resource is released
134 * @counter: the counter
135 * @val: the amount of the resource
137 * these calls check for usage underflow and show a warning on the console
138 * _locked call expects the counter->lock to be taken
141 void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val);
142 void res_counter_uncharge_until(struct res_counter *counter,
143 struct res_counter *limit, unsigned long val);
144 static inline void res_counter_uncharge(struct res_counter *counter,
147 res_counter_uncharge_until(counter, NULL, val);
150 struct res_counter *res_counter_common_ancestor(struct res_counter *l,
151 struct res_counter *r);
154 * res_counter_margin - calculate chargeable space of a counter
157 * Returns the difference between the hard limit and the current usage
158 * of resource counter @cnt.
160 static inline unsigned long long res_counter_margin(struct res_counter *cnt)
162 unsigned long long margin;
165 spin_lock_irqsave(&cnt->lock, flags);
166 margin = cnt->limit - cnt->usage;
167 spin_unlock_irqrestore(&cnt->lock, flags);
172 * Get the difference between the usage and the soft limit
175 * Returns 0 if usage is less than or equal to soft limit
176 * The difference between usage and soft limit, otherwise.
178 static inline unsigned long long
179 res_counter_soft_limit_excess(struct res_counter *cnt)
181 unsigned long long excess;
184 spin_lock_irqsave(&cnt->lock, flags);
185 if (cnt->usage <= cnt->soft_limit)
188 excess = cnt->usage - cnt->soft_limit;
189 spin_unlock_irqrestore(&cnt->lock, flags);
193 static inline void res_counter_reset_max(struct res_counter *cnt)
197 spin_lock_irqsave(&cnt->lock, flags);
198 cnt->max_usage = cnt->usage;
199 spin_unlock_irqrestore(&cnt->lock, flags);
202 static inline void res_counter_reset_failcnt(struct res_counter *cnt)
206 spin_lock_irqsave(&cnt->lock, flags);
208 spin_unlock_irqrestore(&cnt->lock, flags);
211 static inline int res_counter_set_limit(struct res_counter *cnt,
212 unsigned long long limit)
217 spin_lock_irqsave(&cnt->lock, flags);
218 if (cnt->usage <= limit) {
222 spin_unlock_irqrestore(&cnt->lock, flags);
227 res_counter_set_soft_limit(struct res_counter *cnt,
228 unsigned long long soft_limit)
232 spin_lock_irqsave(&cnt->lock, flags);
233 cnt->soft_limit = soft_limit;
234 spin_unlock_irqrestore(&cnt->lock, flags);