15e5a94b6SBenoît Canet /* 25e5a94b6SBenoît Canet * QEMU System Emulator block accounting 35e5a94b6SBenoît Canet * 45e5a94b6SBenoît Canet * Copyright (c) 2011 Christoph Hellwig 5aece5edcSAlberto Garcia * Copyright (c) 2015 Igalia, S.L. 65e5a94b6SBenoît Canet * 75e5a94b6SBenoît Canet * Permission is hereby granted, free of charge, to any person obtaining a copy 85e5a94b6SBenoît Canet * of this software and associated documentation files (the "Software"), to deal 95e5a94b6SBenoît Canet * in the Software without restriction, including without limitation the rights 105e5a94b6SBenoît Canet * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 115e5a94b6SBenoît Canet * copies of the Software, and to permit persons to whom the Software is 125e5a94b6SBenoît Canet * furnished to do so, subject to the following conditions: 135e5a94b6SBenoît Canet * 145e5a94b6SBenoît Canet * The above copyright notice and this permission notice shall be included in 155e5a94b6SBenoît Canet * all copies or substantial portions of the Software. 165e5a94b6SBenoît Canet * 175e5a94b6SBenoît Canet * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 185e5a94b6SBenoît Canet * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 195e5a94b6SBenoît Canet * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 205e5a94b6SBenoît Canet * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 215e5a94b6SBenoît Canet * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 225e5a94b6SBenoît Canet * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 235e5a94b6SBenoît Canet * THE SOFTWARE. 245e5a94b6SBenoît Canet */ 255e5a94b6SBenoît Canet #ifndef BLOCK_ACCOUNTING_H 265e5a94b6SBenoît Canet #define BLOCK_ACCOUNTING_H 275e5a94b6SBenoît Canet 28979e9b03SAlberto Garcia #include "qemu/timed-average.h" 295b50bf77SPaolo Bonzini #include "qemu/thread.h" 30b2aaf354SDenis V. Lunev #include "qapi/qapi-types-common.h" 31979e9b03SAlberto Garcia 32979e9b03SAlberto Garcia typedef struct BlockAcctTimedStats BlockAcctTimedStats; 335b50bf77SPaolo Bonzini typedef struct BlockAcctStats BlockAcctStats; 345e5a94b6SBenoît Canet 355e5a94b6SBenoît Canet enum BlockAcctType { 36f3444466SAnton Nefedov BLOCK_ACCT_NONE = 0, 3728298fd3SBenoît Canet BLOCK_ACCT_READ, 3828298fd3SBenoît Canet BLOCK_ACCT_WRITE, 3928298fd3SBenoît Canet BLOCK_ACCT_FLUSH, 40*52eb76f4SSam Li BLOCK_ACCT_ZONE_APPEND, 41159f85ddSAnton Nefedov BLOCK_ACCT_UNMAP, 4228298fd3SBenoît Canet BLOCK_MAX_IOTYPE, 435e5a94b6SBenoît Canet }; 445e5a94b6SBenoît Canet 45979e9b03SAlberto Garcia struct BlockAcctTimedStats { 465b50bf77SPaolo Bonzini BlockAcctStats *stats; 47979e9b03SAlberto Garcia TimedAverage latency[BLOCK_MAX_IOTYPE]; 48979e9b03SAlberto Garcia unsigned interval_length; /* in seconds */ 49979e9b03SAlberto Garcia QSLIST_ENTRY(BlockAcctTimedStats) entries; 50979e9b03SAlberto Garcia }; 51979e9b03SAlberto Garcia 52b741ae74SVladimir Sementsov-Ogievskiy typedef struct BlockLatencyHistogram { 53b741ae74SVladimir Sementsov-Ogievskiy /* The following histogram is represented like this: 54b741ae74SVladimir Sementsov-Ogievskiy * 55b741ae74SVladimir Sementsov-Ogievskiy * 5| * 56b741ae74SVladimir Sementsov-Ogievskiy * 4| * 57b741ae74SVladimir Sementsov-Ogievskiy * 3| * * 58b741ae74SVladimir Sementsov-Ogievskiy * 2| * * * 59b741ae74SVladimir Sementsov-Ogievskiy * 1| * * * * 60b741ae74SVladimir Sementsov-Ogievskiy * +------------------ 61b741ae74SVladimir Sementsov-Ogievskiy * 10 50 100 62b741ae74SVladimir Sementsov-Ogievskiy * 63b741ae74SVladimir Sementsov-Ogievskiy * BlockLatencyHistogram histogram = { 64b741ae74SVladimir Sementsov-Ogievskiy * .nbins = 4, 65b741ae74SVladimir Sementsov-Ogievskiy * .boundaries = {10, 50, 100}, 66b741ae74SVladimir Sementsov-Ogievskiy * .bins = {3, 1, 5, 2}, 67b741ae74SVladimir Sementsov-Ogievskiy * }; 68b741ae74SVladimir Sementsov-Ogievskiy * 69b741ae74SVladimir Sementsov-Ogievskiy * @boundaries array define histogram intervals as follows: 70b741ae74SVladimir Sementsov-Ogievskiy * [0, boundaries[0]), [boundaries[0], boundaries[1]), ... 71b741ae74SVladimir Sementsov-Ogievskiy * [boundaries[nbins-2], +inf) 72b741ae74SVladimir Sementsov-Ogievskiy * 73b741ae74SVladimir Sementsov-Ogievskiy * So, for example above, histogram intervals are: 74b741ae74SVladimir Sementsov-Ogievskiy * [0, 10), [10, 50), [50, 100), [100, +inf) 75b741ae74SVladimir Sementsov-Ogievskiy */ 76b741ae74SVladimir Sementsov-Ogievskiy int nbins; 77b741ae74SVladimir Sementsov-Ogievskiy uint64_t *boundaries; /* @nbins-1 numbers here 78b741ae74SVladimir Sementsov-Ogievskiy (all boundaries, except 0 and +inf) */ 79b741ae74SVladimir Sementsov-Ogievskiy uint64_t *bins; 80b741ae74SVladimir Sementsov-Ogievskiy } BlockLatencyHistogram; 81b741ae74SVladimir Sementsov-Ogievskiy 825b50bf77SPaolo Bonzini struct BlockAcctStats { 835b50bf77SPaolo Bonzini QemuMutex lock; 8428298fd3SBenoît Canet uint64_t nr_bytes[BLOCK_MAX_IOTYPE]; 8528298fd3SBenoît Canet uint64_t nr_ops[BLOCK_MAX_IOTYPE]; 867ee12dafSAlberto Garcia uint64_t invalid_ops[BLOCK_MAX_IOTYPE]; 877ee12dafSAlberto Garcia uint64_t failed_ops[BLOCK_MAX_IOTYPE]; 8828298fd3SBenoît Canet uint64_t total_time_ns[BLOCK_MAX_IOTYPE]; 89f4564d53SPeter Lieven uint64_t merged[BLOCK_MAX_IOTYPE]; 90cb38fffbSAlberto Garcia int64_t last_access_time_ns; 91979e9b03SAlberto Garcia QSLIST_HEAD(, BlockAcctTimedStats) intervals; 92362e9299SAlberto Garcia bool account_invalid; 93362e9299SAlberto Garcia bool account_failed; 94b741ae74SVladimir Sementsov-Ogievskiy BlockLatencyHistogram latency_histogram[BLOCK_MAX_IOTYPE]; 955b50bf77SPaolo Bonzini }; 965e5a94b6SBenoît Canet 975e5a94b6SBenoît Canet typedef struct BlockAcctCookie { 985e5a94b6SBenoît Canet int64_t bytes; 995e5a94b6SBenoît Canet int64_t start_time_ns; 1005e5a94b6SBenoît Canet enum BlockAcctType type; 1015e5a94b6SBenoît Canet } BlockAcctCookie; 1025e5a94b6SBenoît Canet 1039caa6f3dSPaolo Bonzini void block_acct_init(BlockAcctStats *stats); 104b2aaf354SDenis V. Lunev void block_acct_setup(BlockAcctStats *stats, enum OnOffAuto account_invalid, 105b2aaf354SDenis V. Lunev enum OnOffAuto account_failed); 106979e9b03SAlberto Garcia void block_acct_cleanup(BlockAcctStats *stats); 107979e9b03SAlberto Garcia void block_acct_add_interval(BlockAcctStats *stats, unsigned interval_length); 108979e9b03SAlberto Garcia BlockAcctTimedStats *block_acct_interval_next(BlockAcctStats *stats, 109979e9b03SAlberto Garcia BlockAcctTimedStats *s); 1105366d0c8SBenoît Canet void block_acct_start(BlockAcctStats *stats, BlockAcctCookie *cookie, 1115e5a94b6SBenoît Canet int64_t bytes, enum BlockAcctType type); 1125366d0c8SBenoît Canet void block_acct_done(BlockAcctStats *stats, BlockAcctCookie *cookie); 1137ee12dafSAlberto Garcia void block_acct_failed(BlockAcctStats *stats, BlockAcctCookie *cookie); 1147ee12dafSAlberto Garcia void block_acct_invalid(BlockAcctStats *stats, enum BlockAcctType type); 115f4564d53SPeter Lieven void block_acct_merge_done(BlockAcctStats *stats, enum BlockAcctType type, 116f4564d53SPeter Lieven int num_requests); 117cb38fffbSAlberto Garcia int64_t block_acct_idle_time_ns(BlockAcctStats *stats); 11896e4dedaSAlberto Garcia double block_acct_queue_depth(BlockAcctTimedStats *stats, 11996e4dedaSAlberto Garcia enum BlockAcctType type); 120b741ae74SVladimir Sementsov-Ogievskiy int block_latency_histogram_set(BlockAcctStats *stats, enum BlockAcctType type, 121b741ae74SVladimir Sementsov-Ogievskiy uint64List *boundaries); 122b741ae74SVladimir Sementsov-Ogievskiy void block_latency_histograms_clear(BlockAcctStats *stats); 1235e5a94b6SBenoît Canet 1245e5a94b6SBenoît Canet #endif 125