xref: /spdk/module/bdev/ocf/stats.c (revision 7392cdeff7d8d8d30ec1d8f24a3eebe6f762cde4)
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, ocf_core_id_t core_id, struct vbdev_ocf_stats *stats)
39 {
40 	int status;
41 	ocf_core_t core;
42 
43 	if (cache == NULL) {
44 		assert(false);
45 		return -EFAULT;
46 	}
47 
48 	status = ocf_mngt_cache_read_lock(cache);
49 	if (status) {
50 		return status;
51 	}
52 
53 	status = ocf_core_get(cache, core_id, &core);
54 	if (status) {
55 		return status;
56 	}
57 
58 	status = ocf_stats_collect_core(core, &stats->usage, &stats->reqs, &stats->blocks, &stats->errors);
59 	ocf_mngt_cache_read_unlock(cache);
60 	if (status) {
61 		return status;
62 	}
63 
64 	return 0;
65 }
66 
67 #define WJSON_STAT(w, stats, group, field, units) \
68 	spdk_json_write_named_object_begin(w, #field); \
69 	spdk_json_write_named_uint64(w, "count", stats->group.field.value); \
70 	spdk_json_write_named_string_fmt(w, "percentage", "%lu.%lu", \
71 		stats->group.field.percent / 10, stats->group.field.percent % 10); \
72 	spdk_json_write_named_string(w, "units", units); \
73 	spdk_json_write_object_end(w);
74 
75 void
76 vbdev_ocf_stats_write_json(struct spdk_json_write_ctx *w, struct vbdev_ocf_stats *stats)
77 {
78 	spdk_json_write_object_begin(w);
79 
80 	spdk_json_write_named_object_begin(w, "usage");
81 	WJSON_STAT(w, stats, usage, occupancy, "4KiB blocks");
82 	WJSON_STAT(w, stats, usage, free, "4KiB blocks");
83 	WJSON_STAT(w, stats, usage, clean, "4KiB blocks");
84 	WJSON_STAT(w, stats, usage, dirty, "4KiB blocks");
85 	spdk_json_write_object_end(w);
86 
87 	spdk_json_write_named_object_begin(w, "requests");
88 	WJSON_STAT(w, stats, reqs, rd_hits, "Requests");
89 	WJSON_STAT(w, stats, reqs, rd_partial_misses, "Requests");
90 	WJSON_STAT(w, stats, reqs, rd_full_misses, "Requests");
91 	WJSON_STAT(w, stats, reqs, rd_total, "Requests");
92 	WJSON_STAT(w, stats, reqs, wr_hits, "Requests");
93 	WJSON_STAT(w, stats, reqs, wr_partial_misses, "Requests");
94 	WJSON_STAT(w, stats, reqs, wr_full_misses, "Requests");
95 	WJSON_STAT(w, stats, reqs, wr_total, "Requests");
96 	WJSON_STAT(w, stats, reqs, rd_pt, "Requests");
97 	WJSON_STAT(w, stats, reqs, wr_pt, "Requests");
98 	WJSON_STAT(w, stats, reqs, serviced, "Requests");
99 	WJSON_STAT(w, stats, reqs, total, "Requests");
100 	spdk_json_write_object_end(w);
101 
102 	spdk_json_write_named_object_begin(w, "blocks");
103 	WJSON_STAT(w, stats, blocks, core_volume_rd, "4KiB blocks");
104 	WJSON_STAT(w, stats, blocks, core_volume_wr, "4KiB blocks");
105 	WJSON_STAT(w, stats, blocks, core_volume_total, "4KiB blocks");
106 	WJSON_STAT(w, stats, blocks, cache_volume_rd, "4KiB blocks");
107 	WJSON_STAT(w, stats, blocks, cache_volume_wr, "4KiB blocks");
108 	WJSON_STAT(w, stats, blocks, cache_volume_total, "4KiB blocks");
109 	WJSON_STAT(w, stats, blocks, volume_rd, "4KiB blocks");
110 	WJSON_STAT(w, stats, blocks, volume_wr, "4KiB blocks");
111 	WJSON_STAT(w, stats, blocks, volume_total, "4KiB blocks");
112 	spdk_json_write_object_end(w);
113 
114 	spdk_json_write_named_object_begin(w, "errors");
115 	WJSON_STAT(w, stats, errors, core_volume_rd, "Requests");
116 	WJSON_STAT(w, stats, errors, core_volume_wr, "Requests");
117 	WJSON_STAT(w, stats, errors, core_volume_total, "Requests");
118 	WJSON_STAT(w, stats, errors, cache_volume_rd, "Requests");
119 	WJSON_STAT(w, stats, errors, cache_volume_wr, "Requests");
120 	WJSON_STAT(w, stats, errors, cache_volume_total, "Requests");
121 	WJSON_STAT(w, stats, errors, total, "Requests");
122 	spdk_json_write_object_end(w);
123 
124 	spdk_json_write_object_end(w);
125 }
126