xref: /spdk/module/bdev/ocf/stats.c (revision 88e3ffd7b6c5ec1ea1a660354d25f02c766092e1)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "ctx.h"
35 #include "stats.h"
36 
37 int
38 vbdev_ocf_stats_get(ocf_cache_t cache, char *core_name, struct vbdev_ocf_stats *stats)
39 {
40 	int status;
41 	ocf_core_t core;
42 
43 	status = ocf_core_get_by_name(cache, core_name, strlen(core_name), &core);
44 	if (status) {
45 		return status;
46 	}
47 
48 	return ocf_stats_collect_core(core, &stats->usage, &stats->reqs, &stats->blocks, &stats->errors);
49 }
50 
51 #define WJSON_STAT(w, stats, group, field, units) \
52 	spdk_json_write_named_object_begin(w, #field); \
53 	spdk_json_write_named_uint64(w, "count", stats->group.field.value); \
54 	spdk_json_write_named_string_fmt(w, "percentage", "%lu.%lu", \
55 		stats->group.field.fraction / 100, stats->group.field.fraction % 100); \
56 	spdk_json_write_named_string(w, "units", units); \
57 	spdk_json_write_object_end(w);
58 
59 void
60 vbdev_ocf_stats_write_json(struct spdk_json_write_ctx *w, struct vbdev_ocf_stats *stats)
61 {
62 	spdk_json_write_object_begin(w);
63 
64 	spdk_json_write_named_object_begin(w, "usage");
65 	WJSON_STAT(w, stats, usage, occupancy, "4KiB blocks");
66 	WJSON_STAT(w, stats, usage, free, "4KiB blocks");
67 	WJSON_STAT(w, stats, usage, clean, "4KiB blocks");
68 	WJSON_STAT(w, stats, usage, dirty, "4KiB blocks");
69 	spdk_json_write_object_end(w);
70 
71 	spdk_json_write_named_object_begin(w, "requests");
72 	WJSON_STAT(w, stats, reqs, rd_hits, "Requests");
73 	WJSON_STAT(w, stats, reqs, rd_partial_misses, "Requests");
74 	WJSON_STAT(w, stats, reqs, rd_full_misses, "Requests");
75 	WJSON_STAT(w, stats, reqs, rd_total, "Requests");
76 	WJSON_STAT(w, stats, reqs, wr_hits, "Requests");
77 	WJSON_STAT(w, stats, reqs, wr_partial_misses, "Requests");
78 	WJSON_STAT(w, stats, reqs, wr_full_misses, "Requests");
79 	WJSON_STAT(w, stats, reqs, wr_total, "Requests");
80 	WJSON_STAT(w, stats, reqs, rd_pt, "Requests");
81 	WJSON_STAT(w, stats, reqs, wr_pt, "Requests");
82 	WJSON_STAT(w, stats, reqs, serviced, "Requests");
83 	WJSON_STAT(w, stats, reqs, total, "Requests");
84 	spdk_json_write_object_end(w);
85 
86 	spdk_json_write_named_object_begin(w, "blocks");
87 	WJSON_STAT(w, stats, blocks, core_volume_rd, "4KiB blocks");
88 	WJSON_STAT(w, stats, blocks, core_volume_wr, "4KiB blocks");
89 	WJSON_STAT(w, stats, blocks, core_volume_total, "4KiB blocks");
90 	WJSON_STAT(w, stats, blocks, cache_volume_rd, "4KiB blocks");
91 	WJSON_STAT(w, stats, blocks, cache_volume_wr, "4KiB blocks");
92 	WJSON_STAT(w, stats, blocks, cache_volume_total, "4KiB blocks");
93 	WJSON_STAT(w, stats, blocks, volume_rd, "4KiB blocks");
94 	WJSON_STAT(w, stats, blocks, volume_wr, "4KiB blocks");
95 	WJSON_STAT(w, stats, blocks, volume_total, "4KiB blocks");
96 	spdk_json_write_object_end(w);
97 
98 	spdk_json_write_named_object_begin(w, "errors");
99 	WJSON_STAT(w, stats, errors, core_volume_rd, "Requests");
100 	WJSON_STAT(w, stats, errors, core_volume_wr, "Requests");
101 	WJSON_STAT(w, stats, errors, core_volume_total, "Requests");
102 	WJSON_STAT(w, stats, errors, cache_volume_rd, "Requests");
103 	WJSON_STAT(w, stats, errors, cache_volume_wr, "Requests");
104 	WJSON_STAT(w, stats, errors, cache_volume_total, "Requests");
105 	WJSON_STAT(w, stats, errors, total, "Requests");
106 	spdk_json_write_object_end(w);
107 
108 	spdk_json_write_object_end(w);
109 }
110