Skip to content

Commit 5624a4e

Browse files
rhvgoyalJens Axboe
authored andcommitted
blk-throttle: Make dispatch stats per cpu
Currently we take blkg_stat lock for even updating the stats. So even if a group has no throttling rules (common case for root group), we end up taking blkg_lock, for updating the stats. Make dispatch stats per cpu so that these can be updated without taking blkg lock. If cpu goes offline, these stats simply disappear. No protection has been provided for that yet. Do we really need anything for that? Signed-off-by: Vivek Goyal <vgoyal@redhat.com> Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
1 parent 4843c69 commit 5624a4e

4 files changed

Lines changed: 147 additions & 42 deletions

File tree

‎block/blk-cgroup.c‎

Lines changed: 100 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -392,20 +392,22 @@ void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time,
392392
}
393393
EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
394394

395+
/*
396+
* should be called under rcu read lock or queue lock to make sure blkg pointer
397+
* is valid.
398+
*/
395399
void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
396400
uint64_t bytes, bool direction, bool sync)
397401
{
398-
struct blkio_group_stats *stats;
399-
unsigned long flags;
402+
struct blkio_group_stats_cpu *stats_cpu;
400403

401-
spin_lock_irqsave(&blkg->stats_lock, flags);
402-
stats = &blkg->stats;
403-
stats->sectors += bytes >> 9;
404-
blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICED], 1, direction,
405-
sync);
406-
blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_BYTES], bytes,
407-
direction, sync);
408-
spin_unlock_irqrestore(&blkg->stats_lock, flags);
404+
stats_cpu = this_cpu_ptr(blkg->stats_cpu);
405+
406+
stats_cpu->sectors += bytes >> 9;
407+
blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICED],
408+
1, direction, sync);
409+
blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICE_BYTES],
410+
bytes, direction, sync);
409411
}
410412
EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
411413

@@ -440,6 +442,20 @@ void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
440442
}
441443
EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
442444

445+
/*
446+
* This function allocates the per cpu stats for blkio_group. Should be called
447+
* from sleepable context as alloc_per_cpu() requires that.
448+
*/
449+
int blkio_alloc_blkg_stats(struct blkio_group *blkg)
450+
{
451+
/* Allocate memory for per cpu stats */
452+
blkg->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
453+
if (!blkg->stats_cpu)
454+
return -ENOMEM;
455+
return 0;
456+
}
457+
EXPORT_SYMBOL_GPL(blkio_alloc_blkg_stats);
458+
443459
void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
444460
struct blkio_group *blkg, void *key, dev_t dev,
445461
enum blkio_policy_id plid)
@@ -600,6 +616,53 @@ static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
600616
return val;
601617
}
602618

619+
620+
static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg,
621+
enum stat_type_cpu type, enum stat_sub_type sub_type)
622+
{
623+
int cpu;
624+
struct blkio_group_stats_cpu *stats_cpu;
625+
uint64_t val = 0;
626+
627+
for_each_possible_cpu(cpu) {
628+
stats_cpu = per_cpu_ptr(blkg->stats_cpu, cpu);
629+
630+
if (type == BLKIO_STAT_CPU_SECTORS)
631+
val += stats_cpu->sectors;
632+
else
633+
val += stats_cpu->stat_arr_cpu[type][sub_type];
634+
}
635+
636+
return val;
637+
}
638+
639+
static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg,
640+
struct cgroup_map_cb *cb, dev_t dev, enum stat_type_cpu type)
641+
{
642+
uint64_t disk_total, val;
643+
char key_str[MAX_KEY_LEN];
644+
enum stat_sub_type sub_type;
645+
646+
if (type == BLKIO_STAT_CPU_SECTORS) {
647+
val = blkio_read_stat_cpu(blkg, type, 0);
648+
return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, val, cb, dev);
649+
}
650+
651+
for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
652+
sub_type++) {
653+
blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
654+
val = blkio_read_stat_cpu(blkg, type, sub_type);
655+
cb->fill(cb, key_str, val);
656+
}
657+
658+
disk_total = blkio_read_stat_cpu(blkg, type, BLKIO_STAT_READ) +
659+
blkio_read_stat_cpu(blkg, type, BLKIO_STAT_WRITE);
660+
661+
blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
662+
cb->fill(cb, key_str, disk_total);
663+
return disk_total;
664+
}
665+
603666
/* This should be called with blkg->stats_lock held */
604667
static uint64_t blkio_get_stat(struct blkio_group *blkg,
605668
struct cgroup_map_cb *cb, dev_t dev, enum stat_type type)
@@ -611,9 +674,6 @@ static uint64_t blkio_get_stat(struct blkio_group *blkg,
611674
if (type == BLKIO_STAT_TIME)
612675
return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
613676
blkg->stats.time, cb, dev);
614-
if (type == BLKIO_STAT_SECTORS)
615-
return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
616-
blkg->stats.sectors, cb, dev);
617677
#ifdef CONFIG_DEBUG_BLK_CGROUP
618678
if (type == BLKIO_STAT_UNACCOUNTED_TIME)
619679
return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
@@ -1077,8 +1137,8 @@ static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
10771137
}
10781138

10791139
static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
1080-
struct cftype *cft, struct cgroup_map_cb *cb, enum stat_type type,
1081-
bool show_total)
1140+
struct cftype *cft, struct cgroup_map_cb *cb,
1141+
enum stat_type type, bool show_total, bool pcpu)
10821142
{
10831143
struct blkio_group *blkg;
10841144
struct hlist_node *n;
@@ -1089,10 +1149,15 @@ static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
10891149
if (blkg->dev) {
10901150
if (!cftype_blkg_same_policy(cft, blkg))
10911151
continue;
1092-
spin_lock_irq(&blkg->stats_lock);
1093-
cgroup_total += blkio_get_stat(blkg, cb, blkg->dev,
1094-
type);
1095-
spin_unlock_irq(&blkg->stats_lock);
1152+
if (pcpu)
1153+
cgroup_total += blkio_get_stat_cpu(blkg, cb,
1154+
blkg->dev, type);
1155+
else {
1156+
spin_lock_irq(&blkg->stats_lock);
1157+
cgroup_total += blkio_get_stat(blkg, cb,
1158+
blkg->dev, type);
1159+
spin_unlock_irq(&blkg->stats_lock);
1160+
}
10961161
}
10971162
}
10981163
if (show_total)
@@ -1116,47 +1181,47 @@ static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
11161181
switch(name) {
11171182
case BLKIO_PROP_time:
11181183
return blkio_read_blkg_stats(blkcg, cft, cb,
1119-
BLKIO_STAT_TIME, 0);
1184+
BLKIO_STAT_TIME, 0, 0);
11201185
case BLKIO_PROP_sectors:
11211186
return blkio_read_blkg_stats(blkcg, cft, cb,
1122-
BLKIO_STAT_SECTORS, 0);
1187+
BLKIO_STAT_CPU_SECTORS, 0, 1);
11231188
case BLKIO_PROP_io_service_bytes:
11241189
return blkio_read_blkg_stats(blkcg, cft, cb,
1125-
BLKIO_STAT_SERVICE_BYTES, 1);
1190+
BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
11261191
case BLKIO_PROP_io_serviced:
11271192
return blkio_read_blkg_stats(blkcg, cft, cb,
1128-
BLKIO_STAT_SERVICED, 1);
1193+
BLKIO_STAT_CPU_SERVICED, 1, 1);
11291194
case BLKIO_PROP_io_service_time:
11301195
return blkio_read_blkg_stats(blkcg, cft, cb,
1131-
BLKIO_STAT_SERVICE_TIME, 1);
1196+
BLKIO_STAT_SERVICE_TIME, 1, 0);
11321197
case BLKIO_PROP_io_wait_time:
11331198
return blkio_read_blkg_stats(blkcg, cft, cb,
1134-
BLKIO_STAT_WAIT_TIME, 1);
1199+
BLKIO_STAT_WAIT_TIME, 1, 0);
11351200
case BLKIO_PROP_io_merged:
11361201
return blkio_read_blkg_stats(blkcg, cft, cb,
1137-
BLKIO_STAT_MERGED, 1);
1202+
BLKIO_STAT_MERGED, 1, 0);
11381203
case BLKIO_PROP_io_queued:
11391204
return blkio_read_blkg_stats(blkcg, cft, cb,
1140-
BLKIO_STAT_QUEUED, 1);
1205+
BLKIO_STAT_QUEUED, 1, 0);
11411206
#ifdef CONFIG_DEBUG_BLK_CGROUP
11421207
case BLKIO_PROP_unaccounted_time:
11431208
return blkio_read_blkg_stats(blkcg, cft, cb,
1144-
BLKIO_STAT_UNACCOUNTED_TIME, 0);
1209+
BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
11451210
case BLKIO_PROP_dequeue:
11461211
return blkio_read_blkg_stats(blkcg, cft, cb,
1147-
BLKIO_STAT_DEQUEUE, 0);
1212+
BLKIO_STAT_DEQUEUE, 0, 0);
11481213
case BLKIO_PROP_avg_queue_size:
11491214
return blkio_read_blkg_stats(blkcg, cft, cb,
1150-
BLKIO_STAT_AVG_QUEUE_SIZE, 0);
1215+
BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
11511216
case BLKIO_PROP_group_wait_time:
11521217
return blkio_read_blkg_stats(blkcg, cft, cb,
1153-
BLKIO_STAT_GROUP_WAIT_TIME, 0);
1218+
BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
11541219
case BLKIO_PROP_idle_time:
11551220
return blkio_read_blkg_stats(blkcg, cft, cb,
1156-
BLKIO_STAT_IDLE_TIME, 0);
1221+
BLKIO_STAT_IDLE_TIME, 0, 0);
11571222
case BLKIO_PROP_empty_time:
11581223
return blkio_read_blkg_stats(blkcg, cft, cb,
1159-
BLKIO_STAT_EMPTY_TIME, 0);
1224+
BLKIO_STAT_EMPTY_TIME, 0, 0);
11601225
#endif
11611226
default:
11621227
BUG();
@@ -1166,10 +1231,10 @@ static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
11661231
switch(name){
11671232
case BLKIO_THROTL_io_service_bytes:
11681233
return blkio_read_blkg_stats(blkcg, cft, cb,
1169-
BLKIO_STAT_SERVICE_BYTES, 1);
1234+
BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
11701235
case BLKIO_THROTL_io_serviced:
11711236
return blkio_read_blkg_stats(blkcg, cft, cb,
1172-
BLKIO_STAT_SERVICED, 1);
1237+
BLKIO_STAT_CPU_SERVICED, 1, 1);
11731238
default:
11741239
BUG();
11751240
}

‎block/blk-cgroup.h‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ enum stat_type {
3636
* request completion for IOs doen by this cgroup. This may not be
3737
* accurate when NCQ is turned on. */
3838
BLKIO_STAT_SERVICE_TIME = 0,
39-
/* Total bytes transferred */
40-
BLKIO_STAT_SERVICE_BYTES,
41-
/* Total IOs serviced, post merge */
42-
BLKIO_STAT_SERVICED,
4339
/* Total time spent waiting in scheduler queue in ns */
4440
BLKIO_STAT_WAIT_TIME,
4541
/* Number of IOs merged */
@@ -48,7 +44,6 @@ enum stat_type {
4844
BLKIO_STAT_QUEUED,
4945
/* All the single valued stats go below this */
5046
BLKIO_STAT_TIME,
51-
BLKIO_STAT_SECTORS,
5247
#ifdef CONFIG_DEBUG_BLK_CGROUP
5348
/* Time not charged to this cgroup */
5449
BLKIO_STAT_UNACCOUNTED_TIME,
@@ -60,6 +55,16 @@ enum stat_type {
6055
#endif
6156
};
6257

58+
/* Per cpu stats */
59+
enum stat_type_cpu {
60+
BLKIO_STAT_CPU_SECTORS,
61+
/* Total bytes transferred */
62+
BLKIO_STAT_CPU_SERVICE_BYTES,
63+
/* Total IOs serviced, post merge */
64+
BLKIO_STAT_CPU_SERVICED,
65+
BLKIO_STAT_CPU_NR
66+
};
67+
6368
enum stat_sub_type {
6469
BLKIO_STAT_READ = 0,
6570
BLKIO_STAT_WRITE,
@@ -116,7 +121,6 @@ struct blkio_cgroup {
116121
struct blkio_group_stats {
117122
/* total disk time and nr sectors dispatched by this group */
118123
uint64_t time;
119-
uint64_t sectors;
120124
uint64_t stat_arr[BLKIO_STAT_QUEUED + 1][BLKIO_STAT_TOTAL];
121125
#ifdef CONFIG_DEBUG_BLK_CGROUP
122126
/* Time not charged to this cgroup */
@@ -146,6 +150,12 @@ struct blkio_group_stats {
146150
#endif
147151
};
148152

153+
/* Per cpu blkio group stats */
154+
struct blkio_group_stats_cpu {
155+
uint64_t sectors;
156+
uint64_t stat_arr_cpu[BLKIO_STAT_CPU_NR][BLKIO_STAT_TOTAL];
157+
};
158+
149159
struct blkio_group {
150160
/* An rcu protected unique identifier for the group */
151161
void *key;
@@ -161,6 +171,8 @@ struct blkio_group {
161171
/* Need to serialize the stats in the case of reset/update */
162172
spinlock_t stats_lock;
163173
struct blkio_group_stats stats;
174+
/* Per cpu stats pointer */
175+
struct blkio_group_stats_cpu __percpu *stats_cpu;
164176
};
165177

166178
struct blkio_policy_node {
@@ -296,6 +308,7 @@ extern struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk);
296308
extern void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
297309
struct blkio_group *blkg, void *key, dev_t dev,
298310
enum blkio_policy_id plid);
311+
extern int blkio_alloc_blkg_stats(struct blkio_group *blkg);
299312
extern int blkiocg_del_blkio_group(struct blkio_group *blkg);
300313
extern struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg,
301314
void *key);
@@ -323,6 +336,8 @@ static inline void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
323336
struct blkio_group *blkg, void *key, dev_t dev,
324337
enum blkio_policy_id plid) {}
325338

339+
static inline int blkio_alloc_blkg_stats(struct blkio_group *blkg) { return 0; }
340+
326341
static inline int
327342
blkiocg_del_blkio_group(struct blkio_group *blkg) { return 0; }
328343

‎block/blk-throttle.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ static void throtl_free_tg(struct rcu_head *head)
158158
struct throtl_grp *tg;
159159

160160
tg = container_of(head, struct throtl_grp, rcu_head);
161+
free_percpu(tg->blkg.stats_cpu);
161162
kfree(tg);
162163
}
163164

@@ -249,11 +250,19 @@ static void throtl_init_add_tg_lists(struct throtl_data *td,
249250
static struct throtl_grp *throtl_alloc_tg(struct throtl_data *td)
250251
{
251252
struct throtl_grp *tg = NULL;
253+
int ret;
252254

253255
tg = kzalloc_node(sizeof(*tg), GFP_ATOMIC, td->queue->node);
254256
if (!tg)
255257
return NULL;
256258

259+
ret = blkio_alloc_blkg_stats(&tg->blkg);
260+
261+
if (ret) {
262+
kfree(tg);
263+
return NULL;
264+
}
265+
257266
throtl_init_group(tg);
258267
return tg;
259268
}

‎block/cfq-iosched.c‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,7 @@ static void cfq_init_add_cfqg_lists(struct cfq_data *cfqd,
10511051
static struct cfq_group * cfq_alloc_cfqg(struct cfq_data *cfqd)
10521052
{
10531053
struct cfq_group *cfqg = NULL;
1054-
int i, j;
1054+
int i, j, ret;
10551055
struct cfq_rb_root *st;
10561056

10571057
cfqg = kzalloc_node(sizeof(*cfqg), GFP_ATOMIC, cfqd->queue->node);
@@ -1069,6 +1069,13 @@ static struct cfq_group * cfq_alloc_cfqg(struct cfq_data *cfqd)
10691069
* or cgroup deletion path depending on who is exiting first.
10701070
*/
10711071
cfqg->ref = 1;
1072+
1073+
ret = blkio_alloc_blkg_stats(&cfqg->blkg);
1074+
if (ret) {
1075+
kfree(cfqg);
1076+
return NULL;
1077+
}
1078+
10721079
return cfqg;
10731080
}
10741081

@@ -1183,6 +1190,7 @@ static void cfq_put_cfqg(struct cfq_group *cfqg)
11831190
return;
11841191
for_each_cfqg_st(cfqg, i, j, st)
11851192
BUG_ON(!RB_EMPTY_ROOT(&st->rb));
1193+
free_percpu(cfqg->blkg.stats_cpu);
11861194
kfree(cfqg);
11871195
}
11881196

@@ -3995,7 +4003,15 @@ static void *cfq_init_queue(struct request_queue *q)
39954003
* throtl_data goes away.
39964004
*/
39974005
cfqg->ref = 2;
4006+
4007+
if (blkio_alloc_blkg_stats(&cfqg->blkg)) {
4008+
kfree(cfqg);
4009+
kfree(cfqd);
4010+
return NULL;
4011+
}
4012+
39984013
rcu_read_lock();
4014+
39994015
cfq_blkiocg_add_blkio_group(&blkio_root_cgroup, &cfqg->blkg,
40004016
(void *)cfqd, 0);
40014017
rcu_read_unlock();

0 commit comments

Comments
 (0)