1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (C) 2018 Intel Corporation.
3 * All rights reserved.
4 */
5
6 #include "ctx.h"
7 #include "stats.h"
8
9 int
vbdev_ocf_stats_get(ocf_cache_t cache,char * core_name,struct vbdev_ocf_stats * stats)10 vbdev_ocf_stats_get(ocf_cache_t cache, char *core_name, struct vbdev_ocf_stats *stats)
11 {
12 int status;
13 ocf_core_t core;
14
15 status = ocf_core_get_by_name(cache, core_name, strlen(core_name), &core);
16 if (status) {
17 return status;
18 }
19
20 return ocf_stats_collect_core(core, &stats->usage, &stats->reqs, &stats->blocks, &stats->errors);
21 }
22
23 int
vbdev_ocf_stats_reset(ocf_cache_t cache,char * core_name)24 vbdev_ocf_stats_reset(ocf_cache_t cache, char *core_name)
25 {
26 int status;
27 ocf_core_t core;
28
29 status = ocf_core_get_by_name(cache, core_name, strlen(core_name), &core);
30 if (status) {
31 return status;
32 }
33
34 ocf_core_stats_initialize(core);
35
36 return 0;
37 }
38
39 #define WJSON_STAT(w, stats, group, field, units) \
40 spdk_json_write_named_object_begin(w, #field); \
41 spdk_json_write_named_uint64(w, "count", stats->group.field.value); \
42 spdk_json_write_named_string_fmt(w, "percentage", "%lu.%lu", \
43 stats->group.field.fraction / 100, stats->group.field.fraction % 100); \
44 spdk_json_write_named_string(w, "units", units); \
45 spdk_json_write_object_end(w);
46
47 void
vbdev_ocf_stats_write_json(struct spdk_json_write_ctx * w,struct vbdev_ocf_stats * stats)48 vbdev_ocf_stats_write_json(struct spdk_json_write_ctx *w, struct vbdev_ocf_stats *stats)
49 {
50 spdk_json_write_object_begin(w);
51
52 spdk_json_write_named_object_begin(w, "usage");
53 WJSON_STAT(w, stats, usage, occupancy, "4KiB blocks");
54 WJSON_STAT(w, stats, usage, free, "4KiB blocks");
55 WJSON_STAT(w, stats, usage, clean, "4KiB blocks");
56 WJSON_STAT(w, stats, usage, dirty, "4KiB blocks");
57 spdk_json_write_object_end(w);
58
59 spdk_json_write_named_object_begin(w, "requests");
60 WJSON_STAT(w, stats, reqs, rd_hits, "Requests");
61 WJSON_STAT(w, stats, reqs, rd_partial_misses, "Requests");
62 WJSON_STAT(w, stats, reqs, rd_full_misses, "Requests");
63 WJSON_STAT(w, stats, reqs, rd_total, "Requests");
64 WJSON_STAT(w, stats, reqs, wr_hits, "Requests");
65 WJSON_STAT(w, stats, reqs, wr_partial_misses, "Requests");
66 WJSON_STAT(w, stats, reqs, wr_full_misses, "Requests");
67 WJSON_STAT(w, stats, reqs, wr_total, "Requests");
68 WJSON_STAT(w, stats, reqs, rd_pt, "Requests");
69 WJSON_STAT(w, stats, reqs, wr_pt, "Requests");
70 WJSON_STAT(w, stats, reqs, serviced, "Requests");
71 WJSON_STAT(w, stats, reqs, total, "Requests");
72 spdk_json_write_object_end(w);
73
74 spdk_json_write_named_object_begin(w, "blocks");
75 WJSON_STAT(w, stats, blocks, core_volume_rd, "4KiB blocks");
76 WJSON_STAT(w, stats, blocks, core_volume_wr, "4KiB blocks");
77 WJSON_STAT(w, stats, blocks, core_volume_total, "4KiB blocks");
78 WJSON_STAT(w, stats, blocks, cache_volume_rd, "4KiB blocks");
79 WJSON_STAT(w, stats, blocks, cache_volume_wr, "4KiB blocks");
80 WJSON_STAT(w, stats, blocks, cache_volume_total, "4KiB blocks");
81 WJSON_STAT(w, stats, blocks, volume_rd, "4KiB blocks");
82 WJSON_STAT(w, stats, blocks, volume_wr, "4KiB blocks");
83 WJSON_STAT(w, stats, blocks, volume_total, "4KiB blocks");
84 spdk_json_write_object_end(w);
85
86 spdk_json_write_named_object_begin(w, "errors");
87 WJSON_STAT(w, stats, errors, core_volume_rd, "Requests");
88 WJSON_STAT(w, stats, errors, core_volume_wr, "Requests");
89 WJSON_STAT(w, stats, errors, core_volume_total, "Requests");
90 WJSON_STAT(w, stats, errors, cache_volume_rd, "Requests");
91 WJSON_STAT(w, stats, errors, cache_volume_wr, "Requests");
92 WJSON_STAT(w, stats, errors, cache_volume_total, "Requests");
93 WJSON_STAT(w, stats, errors, total, "Requests");
94 spdk_json_write_object_end(w);
95
96 spdk_json_write_object_end(w);
97 }
98