]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - block/blk-cgroup.h
blkcg: restructure blkio_get_stat()
[mirror_ubuntu-zesty-kernel.git] / block / blk-cgroup.h
1 #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 #include <linux/u64_stats_sync.h>
18
19 enum blkio_policy_id {
20 BLKIO_POLICY_PROP = 0, /* Proportional Bandwidth division */
21 BLKIO_POLICY_THROTL, /* Throttling */
22
23 BLKIO_NR_POLICIES,
24 };
25
26 /* Max limits for throttle policy */
27 #define THROTL_IOPS_MAX UINT_MAX
28
29 #ifdef CONFIG_BLK_CGROUP
30
31 enum stat_type {
32 /* Number of IOs merged */
33 BLKIO_STAT_MERGED,
34 /* Total time spent (in ns) between request dispatch to the driver and
35 * request completion for IOs doen by this cgroup. This may not be
36 * accurate when NCQ is turned on. */
37 BLKIO_STAT_SERVICE_TIME,
38 /* Total time spent waiting in scheduler queue in ns */
39 BLKIO_STAT_WAIT_TIME,
40 /* Number of IOs queued up */
41 BLKIO_STAT_QUEUED,
42
43 /* All the single valued stats go below this */
44 BLKIO_STAT_TIME,
45 #ifdef CONFIG_DEBUG_BLK_CGROUP
46 /* Time not charged to this cgroup */
47 BLKIO_STAT_UNACCOUNTED_TIME,
48 BLKIO_STAT_AVG_QUEUE_SIZE,
49 BLKIO_STAT_IDLE_TIME,
50 BLKIO_STAT_EMPTY_TIME,
51 BLKIO_STAT_GROUP_WAIT_TIME,
52 BLKIO_STAT_DEQUEUE
53 #endif
54 };
55
56 /* Types lower than this live in stat_arr and have subtypes */
57 #define BLKIO_STAT_ARR_NR (BLKIO_STAT_QUEUED + 1)
58
59 /* Per cpu stats */
60 enum stat_type_cpu {
61 BLKIO_STAT_CPU_SECTORS,
62 /* Total bytes transferred */
63 BLKIO_STAT_CPU_SERVICE_BYTES,
64 /* Total IOs serviced, post merge */
65 BLKIO_STAT_CPU_SERVICED,
66 BLKIO_STAT_CPU_NR
67 };
68
69 enum stat_sub_type {
70 BLKIO_STAT_READ = 0,
71 BLKIO_STAT_WRITE,
72 BLKIO_STAT_SYNC,
73 BLKIO_STAT_ASYNC,
74 BLKIO_STAT_TOTAL
75 };
76
77 /* blkg state flags */
78 enum blkg_state_flags {
79 BLKG_waiting = 0,
80 BLKG_idling,
81 BLKG_empty,
82 };
83
84 /* cgroup files owned by proportional weight policy */
85 enum blkcg_file_name_prop {
86 BLKIO_PROP_weight = 1,
87 BLKIO_PROP_weight_device,
88 BLKIO_PROP_io_service_bytes,
89 BLKIO_PROP_io_serviced,
90 BLKIO_PROP_time,
91 BLKIO_PROP_sectors,
92 BLKIO_PROP_unaccounted_time,
93 BLKIO_PROP_io_service_time,
94 BLKIO_PROP_io_wait_time,
95 BLKIO_PROP_io_merged,
96 BLKIO_PROP_io_queued,
97 BLKIO_PROP_avg_queue_size,
98 BLKIO_PROP_group_wait_time,
99 BLKIO_PROP_idle_time,
100 BLKIO_PROP_empty_time,
101 BLKIO_PROP_dequeue,
102 };
103
104 /* cgroup files owned by throttle policy */
105 enum blkcg_file_name_throtl {
106 BLKIO_THROTL_read_bps_device,
107 BLKIO_THROTL_write_bps_device,
108 BLKIO_THROTL_read_iops_device,
109 BLKIO_THROTL_write_iops_device,
110 BLKIO_THROTL_io_service_bytes,
111 BLKIO_THROTL_io_serviced,
112 };
113
114 struct blkio_cgroup {
115 struct cgroup_subsys_state css;
116 unsigned int weight;
117 spinlock_t lock;
118 struct hlist_head blkg_list;
119 };
120
121 struct blkio_group_stats {
122 /* total disk time and nr sectors dispatched by this group */
123 uint64_t time;
124 uint64_t stat_arr[BLKIO_STAT_ARR_NR][BLKIO_STAT_TOTAL];
125 #ifdef CONFIG_DEBUG_BLK_CGROUP
126 /* Time not charged to this cgroup */
127 uint64_t unaccounted_time;
128
129 /* Sum of number of IOs queued across all samples */
130 uint64_t avg_queue_size_sum;
131 /* Count of samples taken for average */
132 uint64_t avg_queue_size_samples;
133 /* How many times this group has been removed from service tree */
134 unsigned long dequeue;
135
136 /* Total time spent waiting for it to be assigned a timeslice. */
137 uint64_t group_wait_time;
138
139 /* Time spent idling for this blkio_group */
140 uint64_t idle_time;
141 /*
142 * Total time when we have requests queued and do not contain the
143 * current active queue.
144 */
145 uint64_t empty_time;
146
147 /* fields after this shouldn't be cleared on stat reset */
148 uint64_t start_group_wait_time;
149 uint64_t start_idle_time;
150 uint64_t start_empty_time;
151 uint16_t flags;
152 #endif
153 };
154
155 #ifdef CONFIG_DEBUG_BLK_CGROUP
156 #define BLKG_STATS_DEBUG_CLEAR_START \
157 offsetof(struct blkio_group_stats, unaccounted_time)
158 #define BLKG_STATS_DEBUG_CLEAR_SIZE \
159 (offsetof(struct blkio_group_stats, start_group_wait_time) - \
160 BLKG_STATS_DEBUG_CLEAR_START)
161 #endif
162
163 /* Per cpu blkio group stats */
164 struct blkio_group_stats_cpu {
165 uint64_t sectors;
166 uint64_t stat_arr_cpu[BLKIO_STAT_CPU_NR][BLKIO_STAT_TOTAL];
167 struct u64_stats_sync syncp;
168 };
169
170 struct blkio_group_conf {
171 unsigned int weight;
172 unsigned int iops[2];
173 u64 bps[2];
174 };
175
176 /* per-blkg per-policy data */
177 struct blkg_policy_data {
178 /* the blkg this per-policy data belongs to */
179 struct blkio_group *blkg;
180
181 /* Configuration */
182 struct blkio_group_conf conf;
183
184 struct blkio_group_stats stats;
185 /* Per cpu stats pointer */
186 struct blkio_group_stats_cpu __percpu *stats_cpu;
187
188 /* pol->pdata_size bytes of private data used by policy impl */
189 char pdata[] __aligned(__alignof__(unsigned long long));
190 };
191
192 struct blkio_group {
193 /* Pointer to the associated request_queue */
194 struct request_queue *q;
195 struct list_head q_node;
196 struct hlist_node blkcg_node;
197 struct blkio_cgroup *blkcg;
198 /* Store cgroup path */
199 char path[128];
200 /* reference count */
201 int refcnt;
202
203 /* Need to serialize the stats in the case of reset/update */
204 spinlock_t stats_lock;
205 struct blkg_policy_data *pd[BLKIO_NR_POLICIES];
206
207 /* List of blkg waiting for per cpu stats memory to be allocated */
208 struct list_head alloc_node;
209 struct rcu_head rcu_head;
210 };
211
212 typedef void (blkio_init_group_fn)(struct blkio_group *blkg);
213 typedef void (blkio_update_group_weight_fn)(struct request_queue *q,
214 struct blkio_group *blkg, unsigned int weight);
215 typedef void (blkio_update_group_read_bps_fn)(struct request_queue *q,
216 struct blkio_group *blkg, u64 read_bps);
217 typedef void (blkio_update_group_write_bps_fn)(struct request_queue *q,
218 struct blkio_group *blkg, u64 write_bps);
219 typedef void (blkio_update_group_read_iops_fn)(struct request_queue *q,
220 struct blkio_group *blkg, unsigned int read_iops);
221 typedef void (blkio_update_group_write_iops_fn)(struct request_queue *q,
222 struct blkio_group *blkg, unsigned int write_iops);
223
224 struct blkio_policy_ops {
225 blkio_init_group_fn *blkio_init_group_fn;
226 blkio_update_group_weight_fn *blkio_update_group_weight_fn;
227 blkio_update_group_read_bps_fn *blkio_update_group_read_bps_fn;
228 blkio_update_group_write_bps_fn *blkio_update_group_write_bps_fn;
229 blkio_update_group_read_iops_fn *blkio_update_group_read_iops_fn;
230 blkio_update_group_write_iops_fn *blkio_update_group_write_iops_fn;
231 };
232
233 struct blkio_policy_type {
234 struct list_head list;
235 struct blkio_policy_ops ops;
236 enum blkio_policy_id plid;
237 size_t pdata_size; /* policy specific private data size */
238 };
239
240 extern int blkcg_init_queue(struct request_queue *q);
241 extern void blkcg_drain_queue(struct request_queue *q);
242 extern void blkcg_exit_queue(struct request_queue *q);
243
244 /* Blkio controller policy registration */
245 extern void blkio_policy_register(struct blkio_policy_type *);
246 extern void blkio_policy_unregister(struct blkio_policy_type *);
247 extern void blkg_destroy_all(struct request_queue *q, bool destroy_root);
248 extern void update_root_blkg_pd(struct request_queue *q,
249 enum blkio_policy_id plid);
250
251 /**
252 * blkg_to_pdata - get policy private data
253 * @blkg: blkg of interest
254 * @pol: policy of interest
255 *
256 * Return pointer to private data associated with the @blkg-@pol pair.
257 */
258 static inline void *blkg_to_pdata(struct blkio_group *blkg,
259 struct blkio_policy_type *pol)
260 {
261 return blkg ? blkg->pd[pol->plid]->pdata : NULL;
262 }
263
264 /**
265 * pdata_to_blkg - get blkg associated with policy private data
266 * @pdata: policy private data of interest
267 * @pol: policy @pdata is for
268 *
269 * @pdata is policy private data for @pol. Determine the blkg it's
270 * associated with.
271 */
272 static inline struct blkio_group *pdata_to_blkg(void *pdata,
273 struct blkio_policy_type *pol)
274 {
275 if (pdata) {
276 struct blkg_policy_data *pd =
277 container_of(pdata, struct blkg_policy_data, pdata);
278 return pd->blkg;
279 }
280 return NULL;
281 }
282
283 static inline char *blkg_path(struct blkio_group *blkg)
284 {
285 return blkg->path;
286 }
287
288 /**
289 * blkg_get - get a blkg reference
290 * @blkg: blkg to get
291 *
292 * The caller should be holding queue_lock and an existing reference.
293 */
294 static inline void blkg_get(struct blkio_group *blkg)
295 {
296 lockdep_assert_held(blkg->q->queue_lock);
297 WARN_ON_ONCE(!blkg->refcnt);
298 blkg->refcnt++;
299 }
300
301 void __blkg_release(struct blkio_group *blkg);
302
303 /**
304 * blkg_put - put a blkg reference
305 * @blkg: blkg to put
306 *
307 * The caller should be holding queue_lock.
308 */
309 static inline void blkg_put(struct blkio_group *blkg)
310 {
311 lockdep_assert_held(blkg->q->queue_lock);
312 WARN_ON_ONCE(blkg->refcnt <= 0);
313 if (!--blkg->refcnt)
314 __blkg_release(blkg);
315 }
316
317 #else
318
319 struct blkio_group {
320 };
321
322 struct blkio_policy_type {
323 };
324
325 static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
326 static inline void blkcg_drain_queue(struct request_queue *q) { }
327 static inline void blkcg_exit_queue(struct request_queue *q) { }
328 static inline void blkio_policy_register(struct blkio_policy_type *blkiop) { }
329 static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
330 static inline void blkg_destroy_all(struct request_queue *q,
331 bool destory_root) { }
332 static inline void update_root_blkg_pd(struct request_queue *q,
333 enum blkio_policy_id plid) { }
334
335 static inline void *blkg_to_pdata(struct blkio_group *blkg,
336 struct blkio_policy_type *pol) { return NULL; }
337 static inline struct blkio_group *pdata_to_blkg(void *pdata,
338 struct blkio_policy_type *pol) { return NULL; }
339 static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
340 static inline void blkg_get(struct blkio_group *blkg) { }
341 static inline void blkg_put(struct blkio_group *blkg) { }
342
343 #endif
344
345 #define BLKIO_WEIGHT_MIN 10
346 #define BLKIO_WEIGHT_MAX 1000
347 #define BLKIO_WEIGHT_DEFAULT 500
348
349 #ifdef CONFIG_DEBUG_BLK_CGROUP
350 void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
351 struct blkio_policy_type *pol);
352 void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
353 struct blkio_policy_type *pol,
354 unsigned long dequeue);
355 void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
356 struct blkio_policy_type *pol);
357 void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
358 struct blkio_policy_type *pol);
359 void blkiocg_set_start_empty_time(struct blkio_group *blkg,
360 struct blkio_policy_type *pol);
361
362 #define BLKG_FLAG_FNS(name) \
363 static inline void blkio_mark_blkg_##name( \
364 struct blkio_group_stats *stats) \
365 { \
366 stats->flags |= (1 << BLKG_##name); \
367 } \
368 static inline void blkio_clear_blkg_##name( \
369 struct blkio_group_stats *stats) \
370 { \
371 stats->flags &= ~(1 << BLKG_##name); \
372 } \
373 static inline int blkio_blkg_##name(struct blkio_group_stats *stats) \
374 { \
375 return (stats->flags & (1 << BLKG_##name)) != 0; \
376 } \
377
378 BLKG_FLAG_FNS(waiting)
379 BLKG_FLAG_FNS(idling)
380 BLKG_FLAG_FNS(empty)
381 #undef BLKG_FLAG_FNS
382 #else
383 static inline void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
384 struct blkio_policy_type *pol) { }
385 static inline void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
386 struct blkio_policy_type *pol, unsigned long dequeue) { }
387 static inline void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
388 struct blkio_policy_type *pol) { }
389 static inline void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
390 struct blkio_policy_type *pol) { }
391 static inline void blkiocg_set_start_empty_time(struct blkio_group *blkg,
392 struct blkio_policy_type *pol) { }
393 #endif
394
395 #ifdef CONFIG_BLK_CGROUP
396 extern struct blkio_cgroup blkio_root_cgroup;
397 extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
398 extern struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio);
399 extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
400 struct request_queue *q);
401 struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
402 struct request_queue *q,
403 enum blkio_policy_id plid,
404 bool for_root);
405 void blkiocg_update_timeslice_used(struct blkio_group *blkg,
406 struct blkio_policy_type *pol,
407 unsigned long time,
408 unsigned long unaccounted_time);
409 void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
410 struct blkio_policy_type *pol,
411 uint64_t bytes, bool direction, bool sync);
412 void blkiocg_update_completion_stats(struct blkio_group *blkg,
413 struct blkio_policy_type *pol,
414 uint64_t start_time,
415 uint64_t io_start_time, bool direction,
416 bool sync);
417 void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
418 struct blkio_policy_type *pol,
419 bool direction, bool sync);
420 void blkiocg_update_io_add_stats(struct blkio_group *blkg,
421 struct blkio_policy_type *pol,
422 struct blkio_group *curr_blkg, bool direction,
423 bool sync);
424 void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
425 struct blkio_policy_type *pol,
426 bool direction, bool sync);
427 #else
428 struct cgroup;
429 static inline struct blkio_cgroup *
430 cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
431 static inline struct blkio_cgroup *
432 bio_blkio_cgroup(struct bio *bio) { return NULL; }
433
434 static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
435 void *key) { return NULL; }
436 static inline void blkiocg_update_timeslice_used(struct blkio_group *blkg,
437 struct blkio_policy_type *pol, unsigned long time,
438 unsigned long unaccounted_time) { }
439 static inline void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
440 struct blkio_policy_type *pol, uint64_t bytes,
441 bool direction, bool sync) { }
442 static inline void blkiocg_update_completion_stats(struct blkio_group *blkg,
443 struct blkio_policy_type *pol, uint64_t start_time,
444 uint64_t io_start_time, bool direction, bool sync) { }
445 static inline void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
446 struct blkio_policy_type *pol, bool direction,
447 bool sync) { }
448 static inline void blkiocg_update_io_add_stats(struct blkio_group *blkg,
449 struct blkio_policy_type *pol,
450 struct blkio_group *curr_blkg, bool direction,
451 bool sync) { }
452 static inline void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
453 struct blkio_policy_type *pol, bool direction,
454 bool sync) { }
455 #endif
456 #endif /* _BLK_CGROUP_H */