blob: bfce085b19629ab6f47eccb5507987c80a603c90 [file] [log] [blame]
Vivek Goyal31e4c282009-12-03 12:59:42 -05001#ifndef _BLK_CGROUP_H
2#define _BLK_CGROUP_H
3/*
4 * Common Block IO controller cgroup interface
5 *
6 * Based on ideas and code from CFQ, CFS and BFQ:
7 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
8 *
9 * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
10 * Paolo Valente <paolo.valente@unimore.it>
11 *
12 * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
13 * Nauman Rafique <nauman@google.com>
14 */
15
16#include <linux/cgroup.h>
17
Ben Blum67523c42010-03-10 15:22:11 -080018#if defined(CONFIG_BLK_CGROUP) || defined(CONFIG_BLK_CGROUP_MODULE)
19
20#ifndef CONFIG_BLK_CGROUP
21/* When blk-cgroup is a module, its subsys_id isn't a compile-time constant */
22extern struct cgroup_subsys blkio_subsys;
23#define blkio_subsys_id blkio_subsys.subsys_id
24#endif
Jens Axboe2f5ea472009-12-03 21:06:43 +010025
Divyesh Shah84c124d2010-04-09 08:31:19 +020026enum stat_type {
27 /* Total time spent (in ns) between request dispatch to the driver and
28 * request completion for IOs doen by this cgroup. This may not be
29 * accurate when NCQ is turned on. */
30 BLKIO_STAT_SERVICE_TIME = 0,
31 /* Total bytes transferred */
32 BLKIO_STAT_SERVICE_BYTES,
33 /* Total IOs serviced, post merge */
34 BLKIO_STAT_SERVICED,
35 /* Total time spent waiting in scheduler queue in ns */
36 BLKIO_STAT_WAIT_TIME,
Divyesh Shah812d4022010-04-08 21:14:23 -070037 /* Number of IOs merged */
38 BLKIO_STAT_MERGED,
Divyesh Shahcdc11842010-04-08 21:15:10 -070039 /* Number of IOs queued up */
40 BLKIO_STAT_QUEUED,
Divyesh Shah84c124d2010-04-09 08:31:19 +020041 /* All the single valued stats go below this */
42 BLKIO_STAT_TIME,
43 BLKIO_STAT_SECTORS,
44#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -070045 BLKIO_STAT_AVG_QUEUE_SIZE,
Divyesh Shah812df482010-04-08 21:15:35 -070046 BLKIO_STAT_IDLE_TIME,
47 BLKIO_STAT_EMPTY_TIME,
48 BLKIO_STAT_GROUP_WAIT_TIME,
Divyesh Shah84c124d2010-04-09 08:31:19 +020049 BLKIO_STAT_DEQUEUE
50#endif
51};
52
53enum stat_sub_type {
54 BLKIO_STAT_READ = 0,
55 BLKIO_STAT_WRITE,
56 BLKIO_STAT_SYNC,
57 BLKIO_STAT_ASYNC,
58 BLKIO_STAT_TOTAL
Divyesh Shah303a3ac2010-04-01 15:01:24 -070059};
60
Divyesh Shah812df482010-04-08 21:15:35 -070061/* blkg state flags */
62enum blkg_state_flags {
63 BLKG_waiting = 0,
64 BLKG_idling,
65 BLKG_empty,
66};
67
Vivek Goyal31e4c282009-12-03 12:59:42 -050068struct blkio_cgroup {
69 struct cgroup_subsys_state css;
70 unsigned int weight;
71 spinlock_t lock;
72 struct hlist_head blkg_list;
73};
74
Divyesh Shah303a3ac2010-04-01 15:01:24 -070075struct blkio_group_stats {
76 /* total disk time and nr sectors dispatched by this group */
77 uint64_t time;
78 uint64_t sectors;
Divyesh Shahcdc11842010-04-08 21:15:10 -070079 uint64_t stat_arr[BLKIO_STAT_QUEUED + 1][BLKIO_STAT_TOTAL];
Divyesh Shah303a3ac2010-04-01 15:01:24 -070080#ifdef CONFIG_DEBUG_BLK_CGROUP
Divyesh Shahcdc11842010-04-08 21:15:10 -070081 /* Sum of number of IOs queued across all samples */
82 uint64_t avg_queue_size_sum;
83 /* Count of samples taken for average */
84 uint64_t avg_queue_size_samples;
Divyesh Shah303a3ac2010-04-01 15:01:24 -070085 /* How many times this group has been removed from service tree */
86 unsigned long dequeue;
Divyesh Shah812df482010-04-08 21:15:35 -070087
88 /* Total time spent waiting for it to be assigned a timeslice. */
89 uint64_t group_wait_time;
90 uint64_t start_group_wait_time;
91
92 /* Time spent idling for this blkio_group */
93 uint64_t idle_time;
94 uint64_t start_idle_time;
95 /*
96 * Total time when we have requests queued and do not contain the
97 * current active queue.
98 */
99 uint64_t empty_time;
100 uint64_t start_empty_time;
101 uint16_t flags;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700102#endif
103};
104
Vivek Goyal31e4c282009-12-03 12:59:42 -0500105struct blkio_group {
106 /* An rcu protected unique identifier for the group */
107 void *key;
108 struct hlist_node blkcg_node;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500109 unsigned short blkcg_id;
Vivek Goyal2868ef72009-12-03 12:59:48 -0500110#ifdef CONFIG_DEBUG_BLK_CGROUP
111 /* Store cgroup path */
112 char path[128];
113#endif
Vivek Goyal22084192009-12-03 12:59:49 -0500114 /* The device MKDEV(major, minor), this group has been created for */
Divyesh Shah84c124d2010-04-09 08:31:19 +0200115 dev_t dev;
Vivek Goyal22084192009-12-03 12:59:49 -0500116
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700117 /* Need to serialize the stats in the case of reset/update */
118 spinlock_t stats_lock;
119 struct blkio_group_stats stats;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500120};
121
Vivek Goyal3e252062009-12-04 10:36:42 -0500122typedef void (blkio_unlink_group_fn) (void *key, struct blkio_group *blkg);
123typedef void (blkio_update_group_weight_fn) (struct blkio_group *blkg,
124 unsigned int weight);
125
126struct blkio_policy_ops {
127 blkio_unlink_group_fn *blkio_unlink_group_fn;
128 blkio_update_group_weight_fn *blkio_update_group_weight_fn;
129};
130
131struct blkio_policy_type {
132 struct list_head list;
133 struct blkio_policy_ops ops;
134};
135
136/* Blkio controller policy registration */
137extern void blkio_policy_register(struct blkio_policy_type *);
138extern void blkio_policy_unregister(struct blkio_policy_type *);
139
Jens Axboe2f5ea472009-12-03 21:06:43 +0100140#else
141
142struct blkio_group {
143};
144
Vivek Goyal3e252062009-12-04 10:36:42 -0500145struct blkio_policy_type {
146};
147
148static inline void blkio_policy_register(struct blkio_policy_type *blkiop) { }
149static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
150
Jens Axboe2f5ea472009-12-03 21:06:43 +0100151#endif
152
Vivek Goyal31e4c282009-12-03 12:59:42 -0500153#define BLKIO_WEIGHT_MIN 100
154#define BLKIO_WEIGHT_MAX 1000
155#define BLKIO_WEIGHT_DEFAULT 500
156
Vivek Goyal2868ef72009-12-03 12:59:48 -0500157#ifdef CONFIG_DEBUG_BLK_CGROUP
158static inline char *blkg_path(struct blkio_group *blkg)
159{
160 return blkg->path;
161}
Divyesh Shahcdc11842010-04-08 21:15:10 -0700162void blkiocg_update_set_active_queue_stats(struct blkio_group *blkg);
Divyesh Shah91952912010-04-01 15:01:41 -0700163void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
Vivek Goyal22084192009-12-03 12:59:49 -0500164 unsigned long dequeue);
Divyesh Shah812df482010-04-08 21:15:35 -0700165void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg);
166void blkiocg_update_idle_time_stats(struct blkio_group *blkg);
167void blkiocg_set_start_empty_time(struct blkio_group *blkg, bool ignore);
168
169#define BLKG_FLAG_FNS(name) \
170static inline void blkio_mark_blkg_##name( \
171 struct blkio_group_stats *stats) \
172{ \
173 stats->flags |= (1 << BLKG_##name); \
174} \
175static inline void blkio_clear_blkg_##name( \
176 struct blkio_group_stats *stats) \
177{ \
178 stats->flags &= ~(1 << BLKG_##name); \
179} \
180static inline int blkio_blkg_##name(struct blkio_group_stats *stats) \
181{ \
182 return (stats->flags & (1 << BLKG_##name)) != 0; \
183} \
184
185BLKG_FLAG_FNS(waiting)
186BLKG_FLAG_FNS(idling)
187BLKG_FLAG_FNS(empty)
188#undef BLKG_FLAG_FNS
Vivek Goyal2868ef72009-12-03 12:59:48 -0500189#else
190static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
Divyesh Shahcdc11842010-04-08 21:15:10 -0700191static inline void blkiocg_update_set_active_queue_stats(
192 struct blkio_group *blkg) {}
Divyesh Shah91952912010-04-01 15:01:41 -0700193static inline void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
194 unsigned long dequeue) {}
Divyesh Shah812df482010-04-08 21:15:35 -0700195static inline void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg)
196{}
197static inline void blkiocg_update_idle_time_stats(struct blkio_group *blkg) {}
198static inline void blkiocg_set_start_empty_time(struct blkio_group *blkg,
199 bool ignore) {}
Vivek Goyal2868ef72009-12-03 12:59:48 -0500200#endif
201
Ben Blum67523c42010-03-10 15:22:11 -0800202#if defined(CONFIG_BLK_CGROUP) || defined(CONFIG_BLK_CGROUP_MODULE)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500203extern struct blkio_cgroup blkio_root_cgroup;
204extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
205extern void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
Vivek Goyal22084192009-12-03 12:59:49 -0500206 struct blkio_group *blkg, void *key, dev_t dev);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500207extern int blkiocg_del_blkio_group(struct blkio_group *blkg);
208extern struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg,
209 void *key);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200210void blkio_group_init(struct blkio_group *blkg);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700211void blkiocg_update_timeslice_used(struct blkio_group *blkg,
212 unsigned long time);
Divyesh Shah84c124d2010-04-09 08:31:19 +0200213void blkiocg_update_dispatch_stats(struct blkio_group *blkg, uint64_t bytes,
214 bool direction, bool sync);
215void blkiocg_update_completion_stats(struct blkio_group *blkg,
216 uint64_t start_time, uint64_t io_start_time, bool direction, bool sync);
Divyesh Shah812d4022010-04-08 21:14:23 -0700217void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
218 bool sync);
Divyesh Shahcdc11842010-04-08 21:15:10 -0700219void blkiocg_update_request_add_stats(struct blkio_group *blkg,
220 struct blkio_group *curr_blkg, bool direction, bool sync);
221void blkiocg_update_request_remove_stats(struct blkio_group *blkg,
222 bool direction, bool sync);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500223#else
Jens Axboe2f5ea472009-12-03 21:06:43 +0100224struct cgroup;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500225static inline struct blkio_cgroup *
226cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
227
Divyesh Shah84c124d2010-04-09 08:31:19 +0200228static inline void blkio_group_init(struct blkio_group *blkg) {}
Vivek Goyal31e4c282009-12-03 12:59:42 -0500229static inline void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
Divyesh Shah84c124d2010-04-09 08:31:19 +0200230 struct blkio_group *blkg, void *key, dev_t dev) {}
Vivek Goyal31e4c282009-12-03 12:59:42 -0500231
232static inline int
233blkiocg_del_blkio_group(struct blkio_group *blkg) { return 0; }
234
235static inline struct blkio_group *
236blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key) { return NULL; }
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700237static inline void blkiocg_update_timeslice_used(struct blkio_group *blkg,
Divyesh Shah9a0785b2010-04-01 15:01:04 -0700238 unsigned long time) {}
Divyesh Shah84c124d2010-04-09 08:31:19 +0200239static inline void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
240 uint64_t bytes, bool direction, bool sync) {}
241static inline void blkiocg_update_completion_stats(struct blkio_group *blkg,
242 uint64_t start_time, uint64_t io_start_time, bool direction,
243 bool sync) {}
Divyesh Shah812d4022010-04-08 21:14:23 -0700244static inline void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
245 bool direction, bool sync) {}
Divyesh Shahcdc11842010-04-08 21:15:10 -0700246static inline void blkiocg_update_request_add_stats(struct blkio_group *blkg,
247 struct blkio_group *curr_blkg, bool direction, bool sync) {}
248static inline void blkiocg_update_request_remove_stats(struct blkio_group *blkg,
249 bool direction, bool sync) {}
Vivek Goyal31e4c282009-12-03 12:59:42 -0500250#endif
251#endif /* _BLK_CGROUP_H */