accounting.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * QEMU System Emulator block accounting
  3. *
  4. * Copyright (c) 2011 Christoph Hellwig
  5. * Copyright (c) 2015 Igalia, S.L.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "block/accounting.h"
  27. #include "block/block_int.h"
  28. #include "qemu/timer.h"
  29. #include "system/qtest.h"
  30. static QEMUClockType clock_type = QEMU_CLOCK_REALTIME;
  31. static const int qtest_latency_ns = NANOSECONDS_PER_SECOND / 1000;
  32. void block_acct_init(BlockAcctStats *stats)
  33. {
  34. qemu_mutex_init(&stats->lock);
  35. if (qtest_enabled()) {
  36. clock_type = QEMU_CLOCK_VIRTUAL;
  37. }
  38. stats->account_invalid = true;
  39. stats->account_failed = true;
  40. }
  41. static bool bool_from_onoffauto(OnOffAuto val, bool def)
  42. {
  43. switch (val) {
  44. case ON_OFF_AUTO_AUTO:
  45. return def;
  46. case ON_OFF_AUTO_ON:
  47. return true;
  48. case ON_OFF_AUTO_OFF:
  49. return false;
  50. default:
  51. abort();
  52. }
  53. }
  54. void block_acct_setup(BlockAcctStats *stats, enum OnOffAuto account_invalid,
  55. enum OnOffAuto account_failed)
  56. {
  57. stats->account_invalid = bool_from_onoffauto(account_invalid,
  58. stats->account_invalid);
  59. stats->account_failed = bool_from_onoffauto(account_failed,
  60. stats->account_failed);
  61. }
  62. void block_acct_cleanup(BlockAcctStats *stats)
  63. {
  64. BlockAcctTimedStats *s, *next;
  65. QSLIST_FOREACH_SAFE(s, &stats->intervals, entries, next) {
  66. g_free(s);
  67. }
  68. qemu_mutex_destroy(&stats->lock);
  69. }
  70. void block_acct_add_interval(BlockAcctStats *stats, unsigned interval_length)
  71. {
  72. BlockAcctTimedStats *s;
  73. unsigned i;
  74. s = g_new0(BlockAcctTimedStats, 1);
  75. s->interval_length = interval_length;
  76. s->stats = stats;
  77. qemu_mutex_lock(&stats->lock);
  78. QSLIST_INSERT_HEAD(&stats->intervals, s, entries);
  79. for (i = 0; i < BLOCK_MAX_IOTYPE; i++) {
  80. timed_average_init(&s->latency[i], clock_type,
  81. (uint64_t) interval_length * NANOSECONDS_PER_SECOND);
  82. }
  83. qemu_mutex_unlock(&stats->lock);
  84. }
  85. BlockAcctTimedStats *block_acct_interval_next(BlockAcctStats *stats,
  86. BlockAcctTimedStats *s)
  87. {
  88. if (s == NULL) {
  89. return QSLIST_FIRST(&stats->intervals);
  90. } else {
  91. return QSLIST_NEXT(s, entries);
  92. }
  93. }
  94. void block_acct_start(BlockAcctStats *stats, BlockAcctCookie *cookie,
  95. int64_t bytes, enum BlockAcctType type)
  96. {
  97. assert(type < BLOCK_MAX_IOTYPE);
  98. cookie->bytes = bytes;
  99. cookie->start_time_ns = qemu_clock_get_ns(clock_type);
  100. cookie->type = type;
  101. }
  102. /* block_latency_histogram_compare_func:
  103. * Compare @key with interval [@it[0], @it[1]).
  104. * Return: -1 if @key < @it[0]
  105. * 0 if @key in [@it[0], @it[1])
  106. * +1 if @key >= @it[1]
  107. */
  108. static int block_latency_histogram_compare_func(const void *key, const void *it)
  109. {
  110. uint64_t k = *(uint64_t *)key;
  111. uint64_t a = ((uint64_t *)it)[0];
  112. uint64_t b = ((uint64_t *)it)[1];
  113. return k < a ? -1 : (k < b ? 0 : 1);
  114. }
  115. static void block_latency_histogram_account(BlockLatencyHistogram *hist,
  116. int64_t latency_ns)
  117. {
  118. uint64_t *pos;
  119. if (hist->bins == NULL) {
  120. /* histogram disabled */
  121. return;
  122. }
  123. if (latency_ns < hist->boundaries[0]) {
  124. hist->bins[0]++;
  125. return;
  126. }
  127. if (latency_ns >= hist->boundaries[hist->nbins - 2]) {
  128. hist->bins[hist->nbins - 1]++;
  129. return;
  130. }
  131. pos = bsearch(&latency_ns, hist->boundaries, hist->nbins - 2,
  132. sizeof(hist->boundaries[0]),
  133. block_latency_histogram_compare_func);
  134. assert(pos != NULL);
  135. hist->bins[pos - hist->boundaries + 1]++;
  136. }
  137. int block_latency_histogram_set(BlockAcctStats *stats, enum BlockAcctType type,
  138. uint64List *boundaries)
  139. {
  140. BlockLatencyHistogram *hist = &stats->latency_histogram[type];
  141. uint64List *entry;
  142. uint64_t *ptr;
  143. uint64_t prev = 0;
  144. int new_nbins = 1;
  145. for (entry = boundaries; entry; entry = entry->next) {
  146. if (entry->value <= prev) {
  147. return -EINVAL;
  148. }
  149. new_nbins++;
  150. prev = entry->value;
  151. }
  152. hist->nbins = new_nbins;
  153. g_free(hist->boundaries);
  154. hist->boundaries = g_new(uint64_t, hist->nbins - 1);
  155. for (entry = boundaries, ptr = hist->boundaries; entry;
  156. entry = entry->next, ptr++)
  157. {
  158. *ptr = entry->value;
  159. }
  160. g_free(hist->bins);
  161. hist->bins = g_new0(uint64_t, hist->nbins);
  162. return 0;
  163. }
  164. void block_latency_histograms_clear(BlockAcctStats *stats)
  165. {
  166. int i;
  167. for (i = 0; i < BLOCK_MAX_IOTYPE; i++) {
  168. BlockLatencyHistogram *hist = &stats->latency_histogram[i];
  169. g_free(hist->bins);
  170. g_free(hist->boundaries);
  171. memset(hist, 0, sizeof(*hist));
  172. }
  173. }
  174. static void block_account_one_io(BlockAcctStats *stats, BlockAcctCookie *cookie,
  175. bool failed)
  176. {
  177. BlockAcctTimedStats *s;
  178. int64_t time_ns = qemu_clock_get_ns(clock_type);
  179. int64_t latency_ns = time_ns - cookie->start_time_ns;
  180. if (qtest_enabled()) {
  181. latency_ns = qtest_latency_ns;
  182. }
  183. assert(cookie->type < BLOCK_MAX_IOTYPE);
  184. if (cookie->type == BLOCK_ACCT_NONE) {
  185. return;
  186. }
  187. WITH_QEMU_LOCK_GUARD(&stats->lock) {
  188. if (failed) {
  189. stats->failed_ops[cookie->type]++;
  190. } else {
  191. stats->nr_bytes[cookie->type] += cookie->bytes;
  192. stats->nr_ops[cookie->type]++;
  193. }
  194. block_latency_histogram_account(&stats->latency_histogram[cookie->type],
  195. latency_ns);
  196. if (!failed || stats->account_failed) {
  197. stats->total_time_ns[cookie->type] += latency_ns;
  198. stats->last_access_time_ns = time_ns;
  199. QSLIST_FOREACH(s, &stats->intervals, entries) {
  200. timed_average_account(&s->latency[cookie->type], latency_ns);
  201. }
  202. }
  203. }
  204. cookie->type = BLOCK_ACCT_NONE;
  205. }
  206. void block_acct_done(BlockAcctStats *stats, BlockAcctCookie *cookie)
  207. {
  208. block_account_one_io(stats, cookie, false);
  209. }
  210. void block_acct_failed(BlockAcctStats *stats, BlockAcctCookie *cookie)
  211. {
  212. block_account_one_io(stats, cookie, true);
  213. }
  214. void block_acct_invalid(BlockAcctStats *stats, enum BlockAcctType type)
  215. {
  216. assert(type < BLOCK_MAX_IOTYPE);
  217. /* block_account_one_io() updates total_time_ns[], but this one does
  218. * not. The reason is that invalid requests are accounted during their
  219. * submission, therefore there's no actual I/O involved.
  220. */
  221. qemu_mutex_lock(&stats->lock);
  222. stats->invalid_ops[type]++;
  223. if (stats->account_invalid) {
  224. stats->last_access_time_ns = qemu_clock_get_ns(clock_type);
  225. }
  226. qemu_mutex_unlock(&stats->lock);
  227. }
  228. void block_acct_merge_done(BlockAcctStats *stats, enum BlockAcctType type,
  229. int num_requests)
  230. {
  231. assert(type < BLOCK_MAX_IOTYPE);
  232. qemu_mutex_lock(&stats->lock);
  233. stats->merged[type] += num_requests;
  234. qemu_mutex_unlock(&stats->lock);
  235. }
  236. int64_t block_acct_idle_time_ns(BlockAcctStats *stats)
  237. {
  238. return qemu_clock_get_ns(clock_type) - stats->last_access_time_ns;
  239. }
  240. double block_acct_queue_depth(BlockAcctTimedStats *stats,
  241. enum BlockAcctType type)
  242. {
  243. uint64_t sum, elapsed;
  244. assert(type < BLOCK_MAX_IOTYPE);
  245. qemu_mutex_lock(&stats->stats->lock);
  246. sum = timed_average_sum(&stats->latency[type], &elapsed);
  247. qemu_mutex_unlock(&stats->stats->lock);
  248. return (double) sum / elapsed;
  249. }