1cf43e6beSJens Axboe /* 2cf43e6beSJens Axboe * Block stat tracking code 3cf43e6beSJens Axboe * 4cf43e6beSJens Axboe * Copyright (C) 2016 Jens Axboe 5cf43e6beSJens Axboe */ 6cf43e6beSJens Axboe #include <linux/kernel.h> 734dbad5dSOmar Sandoval #include <linux/rculist.h> 8cf43e6beSJens Axboe #include <linux/blk-mq.h> 9cf43e6beSJens Axboe 10cf43e6beSJens Axboe #include "blk-stat.h" 11cf43e6beSJens Axboe #include "blk-mq.h" 12b9147dd1SShaohua Li #include "blk.h" 13cf43e6beSJens Axboe 144875253fSOmar Sandoval #define BLK_RQ_STAT_BATCH 64 154875253fSOmar Sandoval 1634dbad5dSOmar Sandoval struct blk_queue_stats { 1734dbad5dSOmar Sandoval struct list_head callbacks; 1834dbad5dSOmar Sandoval spinlock_t lock; 19b9147dd1SShaohua Li bool enable_accounting; 2034dbad5dSOmar Sandoval }; 2134dbad5dSOmar Sandoval 22*a37244e4SStephen Bates int blk_stat_rq_ddir(const struct request *rq) 2334dbad5dSOmar Sandoval { 2434dbad5dSOmar Sandoval return rq_data_dir(rq); 2534dbad5dSOmar Sandoval } 2634dbad5dSOmar Sandoval EXPORT_SYMBOL_GPL(blk_stat_rq_ddir); 2734dbad5dSOmar Sandoval 2834dbad5dSOmar Sandoval static void blk_stat_init(struct blk_rq_stat *stat) 2934dbad5dSOmar Sandoval { 3034dbad5dSOmar Sandoval stat->min = -1ULL; 3134dbad5dSOmar Sandoval stat->max = stat->nr_samples = stat->mean = 0; 3234dbad5dSOmar Sandoval stat->batch = stat->nr_batch = 0; 3334dbad5dSOmar Sandoval } 3434dbad5dSOmar Sandoval 35cf43e6beSJens Axboe static void blk_stat_flush_batch(struct blk_rq_stat *stat) 36cf43e6beSJens Axboe { 37cf43e6beSJens Axboe const s32 nr_batch = READ_ONCE(stat->nr_batch); 38209200efSShaohua Li const s32 nr_samples = READ_ONCE(stat->nr_samples); 39cf43e6beSJens Axboe 40cf43e6beSJens Axboe if (!nr_batch) 41cf43e6beSJens Axboe return; 42cf43e6beSJens Axboe if (!nr_samples) 43cf43e6beSJens Axboe stat->mean = div64_s64(stat->batch, nr_batch); 44cf43e6beSJens Axboe else { 45cf43e6beSJens Axboe stat->mean = div64_s64((stat->mean * nr_samples) + 46cf43e6beSJens Axboe stat->batch, 47cf43e6beSJens Axboe nr_batch + nr_samples); 48cf43e6beSJens Axboe } 49cf43e6beSJens Axboe 50cf43e6beSJens Axboe stat->nr_samples += nr_batch; 51cf43e6beSJens Axboe stat->nr_batch = stat->batch = 0; 52cf43e6beSJens Axboe } 53cf43e6beSJens Axboe 54cf43e6beSJens Axboe static void blk_stat_sum(struct blk_rq_stat *dst, struct blk_rq_stat *src) 55cf43e6beSJens Axboe { 567d8d0014SOmar Sandoval blk_stat_flush_batch(src); 577d8d0014SOmar Sandoval 58cf43e6beSJens Axboe if (!src->nr_samples) 59cf43e6beSJens Axboe return; 60cf43e6beSJens Axboe 61cf43e6beSJens Axboe dst->min = min(dst->min, src->min); 62cf43e6beSJens Axboe dst->max = max(dst->max, src->max); 63cf43e6beSJens Axboe 64cf43e6beSJens Axboe if (!dst->nr_samples) 65cf43e6beSJens Axboe dst->mean = src->mean; 66cf43e6beSJens Axboe else { 67cf43e6beSJens Axboe dst->mean = div64_s64((src->mean * src->nr_samples) + 68cf43e6beSJens Axboe (dst->mean * dst->nr_samples), 69cf43e6beSJens Axboe dst->nr_samples + src->nr_samples); 70cf43e6beSJens Axboe } 71cf43e6beSJens Axboe dst->nr_samples += src->nr_samples; 72cf43e6beSJens Axboe } 73cf43e6beSJens Axboe 7434dbad5dSOmar Sandoval static void __blk_stat_add(struct blk_rq_stat *stat, u64 value) 75cf43e6beSJens Axboe { 7634dbad5dSOmar Sandoval stat->min = min(stat->min, value); 7734dbad5dSOmar Sandoval stat->max = max(stat->max, value); 78cf43e6beSJens Axboe 79cf43e6beSJens Axboe if (stat->batch + value < stat->batch || 80cf43e6beSJens Axboe stat->nr_batch + 1 == BLK_RQ_STAT_BATCH) 81cf43e6beSJens Axboe blk_stat_flush_batch(stat); 82cf43e6beSJens Axboe 83cf43e6beSJens Axboe stat->batch += value; 84cf43e6beSJens Axboe stat->nr_batch++; 85cf43e6beSJens Axboe } 86cf43e6beSJens Axboe 8734dbad5dSOmar Sandoval void blk_stat_add(struct request *rq) 88cf43e6beSJens Axboe { 8934dbad5dSOmar Sandoval struct request_queue *q = rq->q; 9034dbad5dSOmar Sandoval struct blk_stat_callback *cb; 9134dbad5dSOmar Sandoval struct blk_rq_stat *stat; 9234dbad5dSOmar Sandoval int bucket; 9334dbad5dSOmar Sandoval s64 now, value; 94cf43e6beSJens Axboe 9534dbad5dSOmar Sandoval now = __blk_stat_time(ktime_to_ns(ktime_get())); 9634dbad5dSOmar Sandoval if (now < blk_stat_time(&rq->issue_stat)) 9734dbad5dSOmar Sandoval return; 9834dbad5dSOmar Sandoval 9934dbad5dSOmar Sandoval value = now - blk_stat_time(&rq->issue_stat); 10034dbad5dSOmar Sandoval 101b9147dd1SShaohua Li blk_throtl_stat_add(rq, value); 102b9147dd1SShaohua Li 10334dbad5dSOmar Sandoval rcu_read_lock(); 10434dbad5dSOmar Sandoval list_for_each_entry_rcu(cb, &q->stats->callbacks, list) { 10534dbad5dSOmar Sandoval if (blk_stat_is_active(cb)) { 10634dbad5dSOmar Sandoval bucket = cb->bucket_fn(rq); 107*a37244e4SStephen Bates if (bucket < 0) 108*a37244e4SStephen Bates continue; 10934dbad5dSOmar Sandoval stat = &this_cpu_ptr(cb->cpu_stat)[bucket]; 11034dbad5dSOmar Sandoval __blk_stat_add(stat, value); 111cf43e6beSJens Axboe } 112cf43e6beSJens Axboe } 11334dbad5dSOmar Sandoval rcu_read_unlock(); 11434dbad5dSOmar Sandoval } 11534dbad5dSOmar Sandoval 11634dbad5dSOmar Sandoval static void blk_stat_timer_fn(unsigned long data) 11734dbad5dSOmar Sandoval { 11834dbad5dSOmar Sandoval struct blk_stat_callback *cb = (void *)data; 11934dbad5dSOmar Sandoval unsigned int bucket; 12034dbad5dSOmar Sandoval int cpu; 12134dbad5dSOmar Sandoval 12234dbad5dSOmar Sandoval for (bucket = 0; bucket < cb->buckets; bucket++) 12334dbad5dSOmar Sandoval blk_stat_init(&cb->stat[bucket]); 12434dbad5dSOmar Sandoval 12534dbad5dSOmar Sandoval for_each_online_cpu(cpu) { 12634dbad5dSOmar Sandoval struct blk_rq_stat *cpu_stat; 12734dbad5dSOmar Sandoval 12834dbad5dSOmar Sandoval cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu); 12934dbad5dSOmar Sandoval for (bucket = 0; bucket < cb->buckets; bucket++) { 13034dbad5dSOmar Sandoval blk_stat_sum(&cb->stat[bucket], &cpu_stat[bucket]); 13134dbad5dSOmar Sandoval blk_stat_init(&cpu_stat[bucket]); 132cf43e6beSJens Axboe } 133cf43e6beSJens Axboe } 134cf43e6beSJens Axboe 13534dbad5dSOmar Sandoval cb->timer_fn(cb); 136cf43e6beSJens Axboe } 137cf43e6beSJens Axboe 13834dbad5dSOmar Sandoval struct blk_stat_callback * 13934dbad5dSOmar Sandoval blk_stat_alloc_callback(void (*timer_fn)(struct blk_stat_callback *), 140*a37244e4SStephen Bates int (*bucket_fn)(const struct request *), 14134dbad5dSOmar Sandoval unsigned int buckets, void *data) 142cf43e6beSJens Axboe { 14334dbad5dSOmar Sandoval struct blk_stat_callback *cb; 14434dbad5dSOmar Sandoval 14534dbad5dSOmar Sandoval cb = kmalloc(sizeof(*cb), GFP_KERNEL); 14634dbad5dSOmar Sandoval if (!cb) 14734dbad5dSOmar Sandoval return NULL; 14834dbad5dSOmar Sandoval 14934dbad5dSOmar Sandoval cb->stat = kmalloc_array(buckets, sizeof(struct blk_rq_stat), 15034dbad5dSOmar Sandoval GFP_KERNEL); 15134dbad5dSOmar Sandoval if (!cb->stat) { 15234dbad5dSOmar Sandoval kfree(cb); 15334dbad5dSOmar Sandoval return NULL; 15434dbad5dSOmar Sandoval } 15534dbad5dSOmar Sandoval cb->cpu_stat = __alloc_percpu(buckets * sizeof(struct blk_rq_stat), 15634dbad5dSOmar Sandoval __alignof__(struct blk_rq_stat)); 15734dbad5dSOmar Sandoval if (!cb->cpu_stat) { 15834dbad5dSOmar Sandoval kfree(cb->stat); 15934dbad5dSOmar Sandoval kfree(cb); 16034dbad5dSOmar Sandoval return NULL; 16134dbad5dSOmar Sandoval } 16234dbad5dSOmar Sandoval 16334dbad5dSOmar Sandoval cb->timer_fn = timer_fn; 16434dbad5dSOmar Sandoval cb->bucket_fn = bucket_fn; 16534dbad5dSOmar Sandoval cb->data = data; 16634dbad5dSOmar Sandoval cb->buckets = buckets; 16734dbad5dSOmar Sandoval setup_timer(&cb->timer, blk_stat_timer_fn, (unsigned long)cb); 16834dbad5dSOmar Sandoval 16934dbad5dSOmar Sandoval return cb; 17034dbad5dSOmar Sandoval } 17134dbad5dSOmar Sandoval EXPORT_SYMBOL_GPL(blk_stat_alloc_callback); 17234dbad5dSOmar Sandoval 17334dbad5dSOmar Sandoval void blk_stat_add_callback(struct request_queue *q, 17434dbad5dSOmar Sandoval struct blk_stat_callback *cb) 17534dbad5dSOmar Sandoval { 17634dbad5dSOmar Sandoval unsigned int bucket; 17734dbad5dSOmar Sandoval int cpu; 17834dbad5dSOmar Sandoval 17934dbad5dSOmar Sandoval for_each_possible_cpu(cpu) { 18034dbad5dSOmar Sandoval struct blk_rq_stat *cpu_stat; 18134dbad5dSOmar Sandoval 18234dbad5dSOmar Sandoval cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu); 18334dbad5dSOmar Sandoval for (bucket = 0; bucket < cb->buckets; bucket++) 18434dbad5dSOmar Sandoval blk_stat_init(&cpu_stat[bucket]); 18534dbad5dSOmar Sandoval } 18634dbad5dSOmar Sandoval 18734dbad5dSOmar Sandoval spin_lock(&q->stats->lock); 18834dbad5dSOmar Sandoval list_add_tail_rcu(&cb->list, &q->stats->callbacks); 189cf43e6beSJens Axboe set_bit(QUEUE_FLAG_STATS, &q->queue_flags); 19034dbad5dSOmar Sandoval spin_unlock(&q->stats->lock); 19134dbad5dSOmar Sandoval } 19234dbad5dSOmar Sandoval EXPORT_SYMBOL_GPL(blk_stat_add_callback); 19334dbad5dSOmar Sandoval 19434dbad5dSOmar Sandoval void blk_stat_remove_callback(struct request_queue *q, 19534dbad5dSOmar Sandoval struct blk_stat_callback *cb) 19634dbad5dSOmar Sandoval { 19734dbad5dSOmar Sandoval spin_lock(&q->stats->lock); 19834dbad5dSOmar Sandoval list_del_rcu(&cb->list); 199b9147dd1SShaohua Li if (list_empty(&q->stats->callbacks) && !q->stats->enable_accounting) 20034dbad5dSOmar Sandoval clear_bit(QUEUE_FLAG_STATS, &q->queue_flags); 20134dbad5dSOmar Sandoval spin_unlock(&q->stats->lock); 20234dbad5dSOmar Sandoval 20334dbad5dSOmar Sandoval del_timer_sync(&cb->timer); 20434dbad5dSOmar Sandoval } 20534dbad5dSOmar Sandoval EXPORT_SYMBOL_GPL(blk_stat_remove_callback); 20634dbad5dSOmar Sandoval 20734dbad5dSOmar Sandoval static void blk_stat_free_callback_rcu(struct rcu_head *head) 20834dbad5dSOmar Sandoval { 20934dbad5dSOmar Sandoval struct blk_stat_callback *cb; 21034dbad5dSOmar Sandoval 21134dbad5dSOmar Sandoval cb = container_of(head, struct blk_stat_callback, rcu); 21234dbad5dSOmar Sandoval free_percpu(cb->cpu_stat); 21334dbad5dSOmar Sandoval kfree(cb->stat); 21434dbad5dSOmar Sandoval kfree(cb); 215cf43e6beSJens Axboe } 216cf43e6beSJens Axboe 21734dbad5dSOmar Sandoval void blk_stat_free_callback(struct blk_stat_callback *cb) 21834dbad5dSOmar Sandoval { 219a83b576cSJens Axboe if (cb) 22034dbad5dSOmar Sandoval call_rcu(&cb->rcu, blk_stat_free_callback_rcu); 22134dbad5dSOmar Sandoval } 22234dbad5dSOmar Sandoval EXPORT_SYMBOL_GPL(blk_stat_free_callback); 22334dbad5dSOmar Sandoval 224b9147dd1SShaohua Li void blk_stat_enable_accounting(struct request_queue *q) 225b9147dd1SShaohua Li { 226b9147dd1SShaohua Li spin_lock(&q->stats->lock); 227b9147dd1SShaohua Li q->stats->enable_accounting = true; 228b9147dd1SShaohua Li set_bit(QUEUE_FLAG_STATS, &q->queue_flags); 229b9147dd1SShaohua Li spin_unlock(&q->stats->lock); 230b9147dd1SShaohua Li } 231b9147dd1SShaohua Li 23234dbad5dSOmar Sandoval struct blk_queue_stats *blk_alloc_queue_stats(void) 23334dbad5dSOmar Sandoval { 23434dbad5dSOmar Sandoval struct blk_queue_stats *stats; 23534dbad5dSOmar Sandoval 23634dbad5dSOmar Sandoval stats = kmalloc(sizeof(*stats), GFP_KERNEL); 23734dbad5dSOmar Sandoval if (!stats) 23834dbad5dSOmar Sandoval return NULL; 23934dbad5dSOmar Sandoval 24034dbad5dSOmar Sandoval INIT_LIST_HEAD(&stats->callbacks); 24134dbad5dSOmar Sandoval spin_lock_init(&stats->lock); 242b9147dd1SShaohua Li stats->enable_accounting = false; 24334dbad5dSOmar Sandoval 24434dbad5dSOmar Sandoval return stats; 24534dbad5dSOmar Sandoval } 24634dbad5dSOmar Sandoval 24734dbad5dSOmar Sandoval void blk_free_queue_stats(struct blk_queue_stats *stats) 24834dbad5dSOmar Sandoval { 24934dbad5dSOmar Sandoval if (!stats) 25034dbad5dSOmar Sandoval return; 25134dbad5dSOmar Sandoval 25234dbad5dSOmar Sandoval WARN_ON(!list_empty(&stats->callbacks)); 25334dbad5dSOmar Sandoval 25434dbad5dSOmar Sandoval kfree(stats); 255cf43e6beSJens Axboe } 256