blob: 271c1c2c9f6f741016f0917c360469ae5ee872a5 [file] [log] [blame]
Pavel Emelianove552b662008-02-07 00:13:49 -08001#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 *
Pavel Emelyanovfaebe9f2008-04-29 01:00:18 -070012 * See Documentation/controllers/resource_counter.txt for more
13 * info about what this counter is.
Pavel Emelianove552b662008-02-07 00:13:49 -080014 */
15
16#include <linux/cgroup.h>
17
18/*
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
22 */
23
24struct res_counter {
25 /*
26 * the current resource consumption level
27 */
Balbir Singh0eea1032008-02-07 00:13:57 -080028 unsigned long long usage;
Pavel Emelianove552b662008-02-07 00:13:49 -080029 /*
Pavel Emelyanovc84872e2008-04-29 01:00:17 -070030 * the maximal value of the usage from the counter creation
31 */
32 unsigned long long max_usage;
33 /*
Pavel Emelianove552b662008-02-07 00:13:49 -080034 * the limit that usage cannot exceed
35 */
Balbir Singh0eea1032008-02-07 00:13:57 -080036 unsigned long long limit;
Pavel Emelianove552b662008-02-07 00:13:49 -080037 /*
38 * the number of unsuccessful attempts to consume the resource
39 */
Balbir Singh0eea1032008-02-07 00:13:57 -080040 unsigned long long failcnt;
Pavel Emelianove552b662008-02-07 00:13:49 -080041 /*
42 * the lock to protect all of the above.
43 * the routines below consider this to be IRQ-safe
44 */
45 spinlock_t lock;
46};
47
Paul Menage2c7eabf2008-04-29 00:59:58 -070048/**
Pavel Emelianove552b662008-02-07 00:13:49 -080049 * Helpers to interact with userspace
Paul Menage2c7eabf2008-04-29 00:59:58 -070050 * res_counter_read_u64() - returns the value of the specified member.
Pavel Emelianove552b662008-02-07 00:13:49 -080051 * res_counter_read/_write - put/get the specified fields from the
52 * res_counter struct to/from the user
53 *
54 * @counter: the counter in question
55 * @member: the field to work with (see RES_xxx below)
56 * @buf: the buffer to opeate on,...
57 * @nbytes: its size...
58 * @pos: and the offset.
59 */
60
Paul Menage2c7eabf2008-04-29 00:59:58 -070061u64 res_counter_read_u64(struct res_counter *counter, int member);
62
Pavel Emelianove552b662008-02-07 00:13:49 -080063ssize_t res_counter_read(struct res_counter *counter, int member,
Balbir Singh0eea1032008-02-07 00:13:57 -080064 const char __user *buf, size_t nbytes, loff_t *pos,
65 int (*read_strategy)(unsigned long long val, char *s));
Paul Menage856c13a2008-07-25 01:47:04 -070066
67typedef int (*write_strategy_fn)(const char *buf, unsigned long long *val);
68
69int res_counter_memparse_write_strategy(const char *buf,
70 unsigned long long *res);
71
72int res_counter_write(struct res_counter *counter, int member,
73 const char *buffer, write_strategy_fn write_strategy);
Pavel Emelianove552b662008-02-07 00:13:49 -080074
75/*
76 * the field descriptors. one for each member of res_counter
77 */
78
79enum {
80 RES_USAGE,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -070081 RES_MAX_USAGE,
Pavel Emelianove552b662008-02-07 00:13:49 -080082 RES_LIMIT,
83 RES_FAILCNT,
84};
85
86/*
87 * helpers for accounting
88 */
89
90void res_counter_init(struct res_counter *counter);
91
92/*
93 * charge - try to consume more resource.
94 *
95 * @counter: the counter
96 * @val: the amount of the resource. each controller defines its own
97 * units, e.g. numbers, bytes, Kbytes, etc
98 *
99 * returns 0 on success and <0 if the counter->usage will exceed the
100 * counter->limit _locked call expects the counter->lock to be taken
101 */
102
Pavel Emelyanovf2992db2008-07-25 01:46:55 -0700103int __must_check res_counter_charge_locked(struct res_counter *counter,
104 unsigned long val);
105int __must_check res_counter_charge(struct res_counter *counter,
106 unsigned long val);
Pavel Emelianove552b662008-02-07 00:13:49 -0800107
108/*
109 * uncharge - tell that some portion of the resource is released
110 *
111 * @counter: the counter
112 * @val: the amount of the resource
113 *
114 * these calls check for usage underflow and show a warning on the console
115 * _locked call expects the counter->lock to be taken
116 */
117
118void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val);
119void res_counter_uncharge(struct res_counter *counter, unsigned long val);
120
Balbir Singh66e17072008-02-07 00:13:56 -0800121static inline bool res_counter_limit_check_locked(struct res_counter *cnt)
122{
123 if (cnt->usage < cnt->limit)
124 return true;
125
126 return false;
127}
128
129/*
130 * Helper function to detect if the cgroup is within it's limit or
131 * not. It's currently called from cgroup_rss_prepare()
132 */
133static inline bool res_counter_check_under_limit(struct res_counter *cnt)
134{
135 bool ret;
136 unsigned long flags;
137
138 spin_lock_irqsave(&cnt->lock, flags);
139 ret = res_counter_limit_check_locked(cnt);
140 spin_unlock_irqrestore(&cnt->lock, flags);
141 return ret;
142}
143
Pavel Emelyanovc84872e2008-04-29 01:00:17 -0700144static inline void res_counter_reset_max(struct res_counter *cnt)
145{
146 unsigned long flags;
147
148 spin_lock_irqsave(&cnt->lock, flags);
149 cnt->max_usage = cnt->usage;
150 spin_unlock_irqrestore(&cnt->lock, flags);
151}
152
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -0700153static inline void res_counter_reset_failcnt(struct res_counter *cnt)
154{
155 unsigned long flags;
156
157 spin_lock_irqsave(&cnt->lock, flags);
158 cnt->failcnt = 0;
159 spin_unlock_irqrestore(&cnt->lock, flags);
160}
KAMEZAWA Hiroyuki12b98042008-07-25 01:47:19 -0700161
162static inline int res_counter_set_limit(struct res_counter *cnt,
163 unsigned long long limit)
164{
165 unsigned long flags;
166 int ret = -EBUSY;
167
168 spin_lock_irqsave(&cnt->lock, flags);
Li Zefan11d55d22008-09-05 14:00:18 -0700169 if (cnt->usage <= limit) {
KAMEZAWA Hiroyuki12b98042008-07-25 01:47:19 -0700170 cnt->limit = limit;
171 ret = 0;
172 }
173 spin_unlock_irqrestore(&cnt->lock, flags);
174 return ret;
175}
176
Pavel Emelianove552b662008-02-07 00:13:49 -0800177#endif