blob: df8085acba16017f6e1a5da5852d21b0f936cccc [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 *
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
22struct res_counter {
23 /*
24 * the current resource consumption level
25 */
Balbir Singh0eea1032008-02-07 00:13:57 -080026 unsigned long long usage;
Pavel Emelianove552b662008-02-07 00:13:49 -080027 /*
Pavel Emelyanovc84872e2008-04-29 01:00:17 -070028 * the maximal value of the usage from the counter creation
29 */
30 unsigned long long max_usage;
31 /*
Pavel Emelianove552b662008-02-07 00:13:49 -080032 * the limit that usage cannot exceed
33 */
Balbir Singh0eea1032008-02-07 00:13:57 -080034 unsigned long long limit;
Pavel Emelianove552b662008-02-07 00:13:49 -080035 /*
36 * the number of unsuccessful attempts to consume the resource
37 */
Balbir Singh0eea1032008-02-07 00:13:57 -080038 unsigned long long failcnt;
Pavel Emelianove552b662008-02-07 00:13:49 -080039 /*
40 * the lock to protect all of the above.
41 * the routines below consider this to be IRQ-safe
42 */
43 spinlock_t lock;
44};
45
Paul Menage2c7eabf2008-04-29 00:59:58 -070046/**
Pavel Emelianove552b662008-02-07 00:13:49 -080047 * Helpers to interact with userspace
Paul Menage2c7eabf2008-04-29 00:59:58 -070048 * res_counter_read_u64() - returns the value of the specified member.
Pavel Emelianove552b662008-02-07 00:13:49 -080049 * res_counter_read/_write - put/get the specified fields from the
50 * res_counter struct to/from the user
51 *
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.
57 */
58
Paul Menage2c7eabf2008-04-29 00:59:58 -070059u64 res_counter_read_u64(struct res_counter *counter, int member);
60
Pavel Emelianove552b662008-02-07 00:13:49 -080061ssize_t res_counter_read(struct res_counter *counter, int member,
Balbir Singh0eea1032008-02-07 00:13:57 -080062 const char __user *buf, size_t nbytes, loff_t *pos,
63 int (*read_strategy)(unsigned long long val, char *s));
Pavel Emelianove552b662008-02-07 00:13:49 -080064ssize_t res_counter_write(struct res_counter *counter, int member,
Balbir Singh0eea1032008-02-07 00:13:57 -080065 const char __user *buf, size_t nbytes, loff_t *pos,
66 int (*write_strategy)(char *buf, unsigned long long *val));
Pavel Emelianove552b662008-02-07 00:13:49 -080067
68/*
69 * the field descriptors. one for each member of res_counter
70 */
71
72enum {
73 RES_USAGE,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -070074 RES_MAX_USAGE,
Pavel Emelianove552b662008-02-07 00:13:49 -080075 RES_LIMIT,
76 RES_FAILCNT,
77};
78
79/*
80 * helpers for accounting
81 */
82
83void res_counter_init(struct res_counter *counter);
84
85/*
86 * charge - try to consume more resource.
87 *
88 * @counter: the counter
89 * @val: the amount of the resource. each controller defines its own
90 * units, e.g. numbers, bytes, Kbytes, etc
91 *
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
94 */
95
96int res_counter_charge_locked(struct res_counter *counter, unsigned long val);
97int res_counter_charge(struct res_counter *counter, unsigned long val);
98
99/*
100 * uncharge - tell that some portion of the resource is released
101 *
102 * @counter: the counter
103 * @val: the amount of the resource
104 *
105 * these calls check for usage underflow and show a warning on the console
106 * _locked call expects the counter->lock to be taken
107 */
108
109void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val);
110void res_counter_uncharge(struct res_counter *counter, unsigned long val);
111
Balbir Singh66e17072008-02-07 00:13:56 -0800112static inline bool res_counter_limit_check_locked(struct res_counter *cnt)
113{
114 if (cnt->usage < cnt->limit)
115 return true;
116
117 return false;
118}
119
120/*
121 * Helper function to detect if the cgroup is within it's limit or
122 * not. It's currently called from cgroup_rss_prepare()
123 */
124static inline bool res_counter_check_under_limit(struct res_counter *cnt)
125{
126 bool ret;
127 unsigned long flags;
128
129 spin_lock_irqsave(&cnt->lock, flags);
130 ret = res_counter_limit_check_locked(cnt);
131 spin_unlock_irqrestore(&cnt->lock, flags);
132 return ret;
133}
134
Pavel Emelyanovc84872e2008-04-29 01:00:17 -0700135static inline void res_counter_reset_max(struct res_counter *cnt)
136{
137 unsigned long flags;
138
139 spin_lock_irqsave(&cnt->lock, flags);
140 cnt->max_usage = cnt->usage;
141 spin_unlock_irqrestore(&cnt->lock, flags);
142}
143
Pavel Emelianove552b662008-02-07 00:13:49 -0800144#endif