xref: /spdk/lib/trace/trace_rpc.c (revision 927f1fd57bd004df581518466ec4c1b8083e5d23)
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 "spdk/rpc.h"
35 #include "spdk/util.h"
36 #include "spdk/trace.h"
37 #include "spdk/log.h"
38 
39 struct rpc_tpoint_group {
40 	char *name;
41 	uint64_t tpoint_mask;
42 };
43 
44 static void
45 free_rpc_tpoint_group(struct rpc_tpoint_group *p)
46 {
47 	free(p->name);
48 }
49 
50 static const struct spdk_json_object_decoder rpc_tpoint_mask_decoders[] = {
51 	{"name", offsetof(struct rpc_tpoint_group, name), spdk_json_decode_string},
52 	{"tpoint_mask", offsetof(struct rpc_tpoint_group, tpoint_mask), spdk_json_decode_uint64, true},
53 };
54 
55 static void
56 rpc_trace_set_tpoint_mask(struct spdk_jsonrpc_request *request,
57 			  const struct spdk_json_val *params)
58 {
59 	struct rpc_tpoint_group req = {};
60 	uint64_t tpoint_group_mask = 0;
61 
62 	if (spdk_json_decode_object(params, rpc_tpoint_mask_decoders,
63 				    SPDK_COUNTOF(rpc_tpoint_mask_decoders), &req)) {
64 		SPDK_DEBUGLOG(trace, "spdk_json_decode_object failed\n");
65 		goto invalid;
66 	}
67 
68 	if (req.name == NULL) {
69 		SPDK_DEBUGLOG(trace, "flag was NULL\n");
70 		goto invalid;
71 	}
72 
73 	tpoint_group_mask = spdk_trace_create_tpoint_group_mask(req.name);
74 	if (tpoint_group_mask == 0) {
75 		goto invalid;
76 	}
77 
78 	spdk_trace_set_tpoints(spdk_u64log2(tpoint_group_mask), req.tpoint_mask);
79 
80 	free_rpc_tpoint_group(&req);
81 
82 	spdk_jsonrpc_send_bool_response(request, true);
83 	return;
84 
85 invalid:
86 	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
87 	free_rpc_tpoint_group(&req);
88 }
89 SPDK_RPC_REGISTER("trace_set_tpoint_mask", rpc_trace_set_tpoint_mask,
90 		  SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
91 
92 static void
93 rpc_trace_clear_tpoint_mask(struct spdk_jsonrpc_request *request,
94 			    const struct spdk_json_val *params)
95 {
96 	struct rpc_tpoint_group req = {};
97 	uint64_t tpoint_group_mask = 0;
98 
99 	if (spdk_json_decode_object(params, rpc_tpoint_mask_decoders,
100 				    SPDK_COUNTOF(rpc_tpoint_mask_decoders), &req)) {
101 		SPDK_DEBUGLOG(trace, "spdk_json_decode_object failed\n");
102 		goto invalid;
103 	}
104 
105 	if (req.name == NULL) {
106 		SPDK_DEBUGLOG(trace, "flag was NULL\n");
107 		goto invalid;
108 	}
109 
110 	tpoint_group_mask = spdk_trace_create_tpoint_group_mask(req.name);
111 	if (tpoint_group_mask == 0) {
112 		goto invalid;
113 	}
114 
115 	spdk_trace_clear_tpoints(spdk_u64log2(tpoint_group_mask), req.tpoint_mask);
116 
117 	free_rpc_tpoint_group(&req);
118 
119 	spdk_jsonrpc_send_bool_response(request, true);
120 	return;
121 
122 invalid:
123 	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
124 	free_rpc_tpoint_group(&req);
125 }
126 SPDK_RPC_REGISTER("trace_clear_tpoint_mask", rpc_trace_clear_tpoint_mask,
127 		  SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
128 
129 static const struct spdk_json_object_decoder rpc_tpoint_group_decoders[] = {
130 	{"name", offsetof(struct rpc_tpoint_group, name), spdk_json_decode_string},
131 };
132 
133 static void
134 rpc_trace_enable_tpoint_group(struct spdk_jsonrpc_request *request,
135 			      const struct spdk_json_val *params)
136 {
137 	struct rpc_tpoint_group req = {};
138 
139 	if (spdk_json_decode_object(params, rpc_tpoint_group_decoders,
140 				    SPDK_COUNTOF(rpc_tpoint_group_decoders), &req)) {
141 		SPDK_DEBUGLOG(trace, "spdk_json_decode_object failed\n");
142 		goto invalid;
143 	}
144 
145 	if (req.name == NULL) {
146 		SPDK_DEBUGLOG(trace, "flag was NULL\n");
147 		goto invalid;
148 	}
149 
150 	if (spdk_trace_enable_tpoint_group(req.name)) {
151 		goto invalid;
152 	}
153 
154 	free_rpc_tpoint_group(&req);
155 
156 	spdk_jsonrpc_send_bool_response(request, true);
157 	return;
158 
159 invalid:
160 	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
161 	free_rpc_tpoint_group(&req);
162 }
163 SPDK_RPC_REGISTER("trace_enable_tpoint_group", rpc_trace_enable_tpoint_group,
164 		  SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
165 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(trace_enable_tpoint_group, enable_tpoint_group)
166 
167 static void
168 rpc_trace_disable_tpoint_group(struct spdk_jsonrpc_request *request,
169 			       const struct spdk_json_val *params)
170 {
171 	struct rpc_tpoint_group req = {};
172 
173 	if (spdk_json_decode_object(params, rpc_tpoint_group_decoders,
174 				    SPDK_COUNTOF(rpc_tpoint_group_decoders), &req)) {
175 		SPDK_DEBUGLOG(trace, "spdk_json_decode_object failed\n");
176 		goto invalid;
177 	}
178 
179 	if (req.name == NULL) {
180 		SPDK_DEBUGLOG(trace, "flag was NULL\n");
181 		goto invalid;
182 	}
183 
184 	if (spdk_trace_disable_tpoint_group(req.name)) {
185 		goto invalid;
186 	}
187 
188 	free_rpc_tpoint_group(&req);
189 
190 	spdk_jsonrpc_send_bool_response(request, true);
191 	return;
192 
193 invalid:
194 	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
195 	free_rpc_tpoint_group(&req);
196 }
197 SPDK_RPC_REGISTER("trace_disable_tpoint_group", rpc_trace_disable_tpoint_group,
198 		  SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
199 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(trace_disable_tpoint_group, disable_tpoint_group)
200 
201 static void
202 rpc_trace_get_tpoint_group_mask(struct spdk_jsonrpc_request *request,
203 				const struct spdk_json_val *params)
204 {
205 	uint64_t tpoint_group_mask;
206 	char mask_str[20];
207 	bool enabled;
208 	struct spdk_json_write_ctx *w;
209 	struct spdk_trace_register_fn *register_fn;
210 
211 	if (params != NULL) {
212 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
213 						 "trace_get_tpoint_group_mask requires no parameters");
214 		return;
215 	}
216 
217 	w = spdk_jsonrpc_begin_result(request);
218 	tpoint_group_mask = spdk_trace_get_tpoint_group_mask();
219 
220 	spdk_json_write_object_begin(w);
221 
222 	snprintf(mask_str, sizeof(mask_str), "0x%" PRIx64, tpoint_group_mask);
223 	spdk_json_write_named_string(w, "tpoint_group_mask", mask_str);
224 
225 	register_fn = spdk_trace_get_first_register_fn();
226 	while (register_fn) {
227 		enabled = spdk_trace_get_tpoint_mask(register_fn->tgroup_id) != 0;
228 
229 		spdk_json_write_named_object_begin(w, register_fn->name);
230 		spdk_json_write_named_bool(w, "enabled", enabled);
231 
232 		snprintf(mask_str, sizeof(mask_str), "0x%lx", (1UL << register_fn->tgroup_id));
233 		spdk_json_write_named_string(w, "mask", mask_str);
234 		spdk_json_write_object_end(w);
235 
236 		register_fn = spdk_trace_get_next_register_fn(register_fn);
237 	}
238 
239 	spdk_json_write_object_end(w);
240 	spdk_jsonrpc_end_result(request, w);
241 }
242 SPDK_RPC_REGISTER("trace_get_tpoint_group_mask", rpc_trace_get_tpoint_group_mask,
243 		  SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
244 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(trace_get_tpoint_group_mask, get_tpoint_group_mask)
245