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>
14 #include <linux/cgroup.h>
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
24 * the current resource consumption level
26 unsigned long long usage;
28 * the maximal value of the usage from the counter creation
30 unsigned long long max_usage;
32 * the limit that usage cannot exceed
34 unsigned long long limit;
36 * the number of unsuccessful attempts to consume the resource
38 unsigned long long failcnt;
40 * the lock to protect all of the above.
41 * the routines below consider this to be IRQ-safe
47 * Helpers to interact with userspace
48 * res_counter_read_u64() - returns the value of the specified member.
49 * res_counter_read/_write - put/get the specified fields from the
50 * res_counter struct to/from the user
52 * @counter: the counter in question
53 * @member: the field to work with (see RES_xxx below)
54 * @buf: the buffer to opeate on,...
55 * @nbytes: its size...
56 * @pos: and the offset.
59 u64 res_counter_read_u64(struct res_counter *counter, int member);
61 ssize_t res_counter_read(struct res_counter *counter, int member,
62 const char __user *buf, size_t nbytes, loff_t *pos,
63 int (*read_strategy)(unsigned long long val, char *s));
64 ssize_t res_counter_write(struct res_counter *counter, int member,
65 const char __user *buf, size_t nbytes, loff_t *pos,
66 int (*write_strategy)(char *buf, unsigned long long *val));
69 * the field descriptors. one for each member of res_counter
80 * helpers for accounting
83 void res_counter_init(struct res_counter *counter);
86 * charge - try to consume more resource.
88 * @counter: the counter
89 * @val: the amount of the resource. each controller defines its own
90 * units, e.g. numbers, bytes, Kbytes, etc
92 * returns 0 on success and <0 if the counter->usage will exceed the
93 * counter->limit _locked call expects the counter->lock to be taken
96 int res_counter_charge_locked(struct res_counter *counter, unsigned long val);
97 int res_counter_charge(struct res_counter *counter, unsigned long val);
100 * uncharge - tell that some portion of the resource is released
102 * @counter: the counter
103 * @val: the amount of the resource
105 * these calls check for usage underflow and show a warning on the console
106 * _locked call expects the counter->lock to be taken
109 void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val);
110 void res_counter_uncharge(struct res_counter *counter, unsigned long val);
112 static inline bool res_counter_limit_check_locked(struct res_counter *cnt)
114 if (cnt->usage < cnt->limit)
121 * Helper function to detect if the cgroup is within it's limit or
122 * not. It's currently called from cgroup_rss_prepare()
124 static inline bool res_counter_check_under_limit(struct res_counter *cnt)
129 spin_lock_irqsave(&cnt->lock, flags);
130 ret = res_counter_limit_check_locked(cnt);
131 spin_unlock_irqrestore(&cnt->lock, flags);
135 static inline void res_counter_reset_max(struct res_counter *cnt)
139 spin_lock_irqsave(&cnt->lock, flags);
140 cnt->max_usage = cnt->usage;
141 spin_unlock_irqrestore(&cnt->lock, flags);