xref: /spdk/lib/trace/trace_rpc.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 "spdk/rpc.h"
35 #include "spdk/util.h"
36 #include "spdk/trace.h"
37 #include "spdk_internal/log.h"
38 
39 struct rpc_tpoint_group {
40 	char *name;
41 };
42 
43 static void
44 free_rpc_tpoint_group(struct rpc_tpoint_group *p)
45 {
46 	free(p->name);
47 }
48 
49 static const struct spdk_json_object_decoder rpc_tpoint_group_decoders[] = {
50 	{"name", offsetof(struct rpc_tpoint_group, name), spdk_json_decode_string},
51 };
52 
53 static void
54 spdk_rpc_enable_tpoint_group(struct spdk_jsonrpc_request *request,
55 			     const struct spdk_json_val *params)
56 {
57 	struct rpc_tpoint_group req = {};
58 	struct spdk_json_write_ctx *w;
59 
60 	if (spdk_json_decode_object(params, rpc_tpoint_group_decoders,
61 				    SPDK_COUNTOF(rpc_tpoint_group_decoders), &req)) {
62 		SPDK_DEBUGLOG(SPDK_LOG_TRACE, "spdk_json_decode_object failed\n");
63 		goto invalid;
64 	}
65 
66 	if (req.name == NULL) {
67 		SPDK_DEBUGLOG(SPDK_LOG_TRACE, "flag was NULL\n");
68 		goto invalid;
69 	}
70 
71 	if (spdk_trace_enable_tpoint_group(req.name)) {
72 		goto invalid;
73 	}
74 
75 	free_rpc_tpoint_group(&req);
76 
77 	w = spdk_jsonrpc_begin_result(request);
78 	spdk_json_write_bool(w, true);
79 	spdk_jsonrpc_end_result(request, w);
80 	return;
81 
82 invalid:
83 	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
84 	free_rpc_tpoint_group(&req);
85 }
86 SPDK_RPC_REGISTER("enable_tpoint_group", spdk_rpc_enable_tpoint_group,
87 		  SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
88 
89 static void
90 spdk_rpc_disable_tpoint_group(struct spdk_jsonrpc_request *request,
91 			      const struct spdk_json_val *params)
92 {
93 	struct rpc_tpoint_group req = {};
94 	struct spdk_json_write_ctx *w;
95 
96 	if (spdk_json_decode_object(params, rpc_tpoint_group_decoders,
97 				    SPDK_COUNTOF(rpc_tpoint_group_decoders), &req)) {
98 		SPDK_DEBUGLOG(SPDK_LOG_TRACE, "spdk_json_decode_object failed\n");
99 		goto invalid;
100 	}
101 
102 	if (req.name == NULL) {
103 		SPDK_DEBUGLOG(SPDK_LOG_TRACE, "flag was NULL\n");
104 		goto invalid;
105 	}
106 
107 	if (spdk_trace_disable_tpoint_group(req.name)) {
108 		goto invalid;
109 	}
110 
111 	free_rpc_tpoint_group(&req);
112 
113 	w = spdk_jsonrpc_begin_result(request);
114 	spdk_json_write_bool(w, true);
115 	spdk_jsonrpc_end_result(request, w);
116 	return;
117 
118 invalid:
119 	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
120 	free_rpc_tpoint_group(&req);
121 }
122 SPDK_RPC_REGISTER("disable_tpoint_group", spdk_rpc_disable_tpoint_group,
123 		  SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
124 
125 static void
126 spdk_rpc_get_tpoint_group_mask(struct spdk_jsonrpc_request *request,
127 			       const struct spdk_json_val *params)
128 {
129 	uint64_t tpoint_group_mask;
130 	char mask_str[7];
131 	bool enabled;
132 	struct spdk_json_write_ctx *w;
133 	struct spdk_trace_register_fn *register_fn;
134 
135 	if (params != NULL) {
136 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
137 						 "get_tpoint_group_mask requires no parameters");
138 		return;
139 	}
140 
141 	w = spdk_jsonrpc_begin_result(request);
142 	tpoint_group_mask = spdk_trace_get_tpoint_group_mask();
143 
144 	spdk_json_write_object_begin(w);
145 
146 	snprintf(mask_str, sizeof(mask_str), "0x%lx", tpoint_group_mask);
147 	spdk_json_write_named_string(w, "tpoint_group_mask", mask_str);
148 
149 	register_fn = spdk_trace_get_first_register_fn();
150 	while (register_fn) {
151 		enabled = spdk_trace_get_tpoint_mask(register_fn->tgroup_id) != 0;
152 
153 		spdk_json_write_named_object_begin(w, register_fn->name);
154 		spdk_json_write_named_bool(w, "enabled", enabled);
155 
156 		snprintf(mask_str, sizeof(mask_str), "0x%lx", (1UL << register_fn->tgroup_id));
157 		spdk_json_write_named_string(w, "mask", mask_str);
158 		spdk_json_write_object_end(w);
159 
160 		register_fn = spdk_trace_get_next_register_fn(register_fn);
161 	}
162 
163 	spdk_json_write_object_end(w);
164 	spdk_jsonrpc_end_result(request, w);
165 }
166 SPDK_RPC_REGISTER("get_tpoint_group_mask", spdk_rpc_get_tpoint_group_mask,
167 		  SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
168