Fix: consumerd: consumed size miscomputed during statistics sampling
[lttng-tools.git] / src / common / actions / start-session.cpp
1 /*
2 * Copyright (C) 2019 Simon Marchi <simon.marchi@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include <common/error.hpp>
9 #include <common/macros.hpp>
10 #include <common/mi-lttng.hpp>
11
12 #include <lttng/action/action-internal.hpp>
13 #include <lttng/action/rate-policy-internal.hpp>
14 #include <lttng/action/rate-policy.h>
15 #include <lttng/action/start-session-internal.hpp>
16 #include <lttng/action/start-session.h>
17
18 #define IS_START_SESSION_ACTION(action) \
19 (lttng_action_get_type(action) == LTTNG_ACTION_TYPE_START_SESSION)
20
21 namespace {
22 struct lttng_action_start_session {
23 struct lttng_action parent;
24
25 /* Owned by this. */
26 char *session_name;
27 struct lttng_rate_policy *policy;
28 };
29
30 struct lttng_action_start_session_comm {
31 /* Includes the trailing \0. */
32 uint32_t session_name_len;
33
34 /*
35 * Variable data:
36 *
37 * - session name (null terminated)
38 * - policy
39 */
40 char data[];
41 } LTTNG_PACKED;
42 } /* namespace */
43
44 static const struct lttng_rate_policy *
45 lttng_action_start_session_internal_get_rate_policy(const struct lttng_action *action);
46
47 static struct lttng_action_start_session *
48 action_start_session_from_action(struct lttng_action *action)
49 {
50 LTTNG_ASSERT(action);
51
52 return lttng::utils::container_of(action, &lttng_action_start_session::parent);
53 }
54
55 static const struct lttng_action_start_session *
56 action_start_session_from_action_const(const struct lttng_action *action)
57 {
58 LTTNG_ASSERT(action);
59
60 return lttng::utils::container_of(action, &lttng_action_start_session::parent);
61 }
62
63 static bool lttng_action_start_session_validate(struct lttng_action *action)
64 {
65 bool valid;
66 struct lttng_action_start_session *action_start_session;
67
68 if (!action) {
69 valid = false;
70 goto end;
71 }
72
73 action_start_session = action_start_session_from_action(action);
74
75 /* A non-empty session name is mandatory. */
76 if (!action_start_session->session_name ||
77 strlen(action_start_session->session_name) == 0) {
78 valid = false;
79 goto end;
80 }
81
82 valid = true;
83 end:
84 return valid;
85 }
86
87 static bool lttng_action_start_session_is_equal(const struct lttng_action *_a,
88 const struct lttng_action *_b)
89 {
90 bool is_equal = false;
91 struct lttng_action_start_session *a, *b;
92
93 a = lttng::utils::container_of(_a, &lttng_action_start_session::parent);
94 b = lttng::utils::container_of(_b, &lttng_action_start_session::parent);
95
96 /* Action is not valid if this is not true. */
97 LTTNG_ASSERT(a->session_name);
98 LTTNG_ASSERT(b->session_name);
99 if (strcmp(a->session_name, b->session_name) != 0) {
100 goto end;
101 }
102
103 is_equal = lttng_rate_policy_is_equal(a->policy, b->policy);
104 end:
105 return is_equal;
106 }
107
108 static int lttng_action_start_session_serialize(struct lttng_action *action,
109 struct lttng_payload *payload)
110 {
111 struct lttng_action_start_session *action_start_session;
112 struct lttng_action_start_session_comm comm;
113 size_t session_name_len;
114 int ret;
115
116 LTTNG_ASSERT(action);
117 LTTNG_ASSERT(payload);
118
119 action_start_session = action_start_session_from_action(action);
120
121 LTTNG_ASSERT(action_start_session->session_name);
122
123 DBG("Serializing start session action: session-name: %s",
124 action_start_session->session_name);
125
126 session_name_len = strlen(action_start_session->session_name) + 1;
127 comm.session_name_len = session_name_len;
128
129 ret = lttng_dynamic_buffer_append(&payload->buffer, &comm, sizeof(comm));
130 if (ret) {
131 ret = -1;
132 goto end;
133 }
134
135 ret = lttng_dynamic_buffer_append(
136 &payload->buffer, action_start_session->session_name, session_name_len);
137 if (ret) {
138 ret = -1;
139 goto end;
140 }
141
142 ret = lttng_rate_policy_serialize(action_start_session->policy, payload);
143 if (ret) {
144 ret = -1;
145 goto end;
146 }
147
148 ret = 0;
149 end:
150 return ret;
151 }
152
153 static void lttng_action_start_session_destroy(struct lttng_action *action)
154 {
155 struct lttng_action_start_session *action_start_session;
156
157 if (!action) {
158 goto end;
159 }
160
161 action_start_session = action_start_session_from_action(action);
162
163 lttng_rate_policy_destroy(action_start_session->policy);
164 free(action_start_session->session_name);
165 free(action_start_session);
166
167 end:
168 return;
169 }
170
171 ssize_t lttng_action_start_session_create_from_payload(struct lttng_payload_view *view,
172 struct lttng_action **p_action)
173 {
174 ssize_t consumed_len, ret;
175 const struct lttng_action_start_session_comm *comm;
176 const char *session_name;
177 struct lttng_action *action = nullptr;
178 enum lttng_action_status status;
179 struct lttng_rate_policy *policy = nullptr;
180
181 comm = (typeof(comm)) view->buffer.data;
182 session_name = (const char *) &comm->data;
183
184 /* Session name */
185 if (!lttng_buffer_view_contains_string(
186 &view->buffer, session_name, comm->session_name_len)) {
187 consumed_len = -1;
188 goto end;
189 }
190 consumed_len = sizeof(*comm) + comm->session_name_len;
191
192 /* Rate policy. */
193 {
194 struct lttng_payload_view policy_view =
195 lttng_payload_view_from_view(view, consumed_len, -1);
196 ret = lttng_rate_policy_create_from_payload(&policy_view, &policy);
197 if (ret < 0) {
198 consumed_len = -1;
199 goto end;
200 }
201 consumed_len += ret;
202 }
203
204 action = lttng_action_start_session_create();
205 if (!action) {
206 consumed_len = -1;
207 goto end;
208 }
209
210 status = lttng_action_start_session_set_session_name(action, session_name);
211 if (status != LTTNG_ACTION_STATUS_OK) {
212 consumed_len = -1;
213 goto end;
214 }
215
216 LTTNG_ASSERT(policy);
217 status = lttng_action_start_session_set_rate_policy(action, policy);
218 if (status != LTTNG_ACTION_STATUS_OK) {
219 consumed_len = -1;
220 goto end;
221 }
222
223 *p_action = action;
224 action = nullptr;
225
226 end:
227 lttng_rate_policy_destroy(policy);
228 lttng_action_start_session_destroy(action);
229
230 return consumed_len;
231 }
232
233 static enum lttng_error_code
234 lttng_action_start_session_mi_serialize(const struct lttng_action *action, struct mi_writer *writer)
235 {
236 int ret;
237 enum lttng_error_code ret_code;
238 enum lttng_action_status status;
239 const char *session_name = nullptr;
240 const struct lttng_rate_policy *policy = nullptr;
241
242 LTTNG_ASSERT(action);
243 LTTNG_ASSERT(IS_START_SESSION_ACTION(action));
244
245 status = lttng_action_start_session_get_session_name(action, &session_name);
246 LTTNG_ASSERT(status == LTTNG_ACTION_STATUS_OK);
247 LTTNG_ASSERT(session_name != nullptr);
248
249 status = lttng_action_start_session_get_rate_policy(action, &policy);
250 LTTNG_ASSERT(status == LTTNG_ACTION_STATUS_OK);
251 LTTNG_ASSERT(policy != nullptr);
252
253 /* Open action start session element. */
254 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_action_start_session);
255 if (ret) {
256 goto mi_error;
257 }
258
259 /* Session name. */
260 ret = mi_lttng_writer_write_element_string(
261 writer, mi_lttng_element_session_name, session_name);
262 if (ret) {
263 goto mi_error;
264 }
265
266 /* Rate policy. */
267 ret_code = lttng_rate_policy_mi_serialize(policy, writer);
268 if (ret_code != LTTNG_OK) {
269 goto end;
270 }
271
272 /* Close action start session element. */
273 ret = mi_lttng_writer_close_element(writer);
274 if (ret) {
275 goto mi_error;
276 }
277
278 ret_code = LTTNG_OK;
279 goto end;
280
281 mi_error:
282 ret_code = LTTNG_ERR_MI_IO_FAIL;
283 end:
284 return ret_code;
285 }
286
287 struct lttng_action *lttng_action_start_session_create(void)
288 {
289 struct lttng_action_start_session *action_start = nullptr;
290 struct lttng_rate_policy *policy = nullptr;
291 enum lttng_action_status status;
292
293 /* Create a every N = 1 rate policy. */
294 policy = lttng_rate_policy_every_n_create(1);
295 if (!policy) {
296 goto end;
297 }
298
299 action_start = zmalloc<lttng_action_start_session>();
300 if (!action_start) {
301 goto end;
302 }
303
304 lttng_action_init(&action_start->parent,
305 LTTNG_ACTION_TYPE_START_SESSION,
306 lttng_action_start_session_validate,
307 lttng_action_start_session_serialize,
308 lttng_action_start_session_is_equal,
309 lttng_action_start_session_destroy,
310 lttng_action_start_session_internal_get_rate_policy,
311 lttng_action_generic_add_error_query_results,
312 lttng_action_start_session_mi_serialize);
313
314 status = lttng_action_start_session_set_rate_policy(&action_start->parent, policy);
315 if (status != LTTNG_ACTION_STATUS_OK) {
316 lttng_action_destroy(&action_start->parent);
317 action_start = nullptr;
318 goto end;
319 }
320
321 end:
322 lttng_rate_policy_destroy(policy);
323 return &action_start->parent;
324 }
325
326 enum lttng_action_status lttng_action_start_session_set_session_name(struct lttng_action *action,
327 const char *session_name)
328 {
329 struct lttng_action_start_session *action_start_session;
330 enum lttng_action_status status;
331
332 if (!action || !IS_START_SESSION_ACTION(action) || !session_name ||
333 strlen(session_name) == 0) {
334 status = LTTNG_ACTION_STATUS_INVALID;
335 goto end;
336 }
337
338 action_start_session = action_start_session_from_action(action);
339
340 free(action_start_session->session_name);
341
342 action_start_session->session_name = strdup(session_name);
343 if (!action_start_session->session_name) {
344 status = LTTNG_ACTION_STATUS_ERROR;
345 goto end;
346 }
347
348 status = LTTNG_ACTION_STATUS_OK;
349 end:
350 return status;
351 }
352
353 enum lttng_action_status
354 lttng_action_start_session_get_session_name(const struct lttng_action *action,
355 const char **session_name)
356 {
357 const struct lttng_action_start_session *action_start_session;
358 enum lttng_action_status status;
359
360 if (!action || !IS_START_SESSION_ACTION(action) || !session_name) {
361 status = LTTNG_ACTION_STATUS_INVALID;
362 goto end;
363 }
364
365 action_start_session = action_start_session_from_action_const(action);
366
367 *session_name = action_start_session->session_name;
368
369 status = LTTNG_ACTION_STATUS_OK;
370 end:
371 return status;
372 }
373
374 enum lttng_action_status
375 lttng_action_start_session_set_rate_policy(struct lttng_action *action,
376 const struct lttng_rate_policy *policy)
377 {
378 enum lttng_action_status status;
379 struct lttng_action_start_session *start_session_action;
380 struct lttng_rate_policy *copy = nullptr;
381
382 if (!action || !policy || !IS_START_SESSION_ACTION(action)) {
383 status = LTTNG_ACTION_STATUS_INVALID;
384 goto end;
385 }
386
387 copy = lttng_rate_policy_copy(policy);
388 if (!copy) {
389 status = LTTNG_ACTION_STATUS_ERROR;
390 goto end;
391 }
392
393 start_session_action = action_start_session_from_action(action);
394
395 /* Release the previous rate policy .*/
396 lttng_rate_policy_destroy(start_session_action->policy);
397
398 /* Assign the policy. */
399 start_session_action->policy = copy;
400 status = LTTNG_ACTION_STATUS_OK;
401 copy = nullptr;
402
403 end:
404 lttng_rate_policy_destroy(copy);
405 return status;
406 }
407
408 enum lttng_action_status
409 lttng_action_start_session_get_rate_policy(const struct lttng_action *action,
410 const struct lttng_rate_policy **policy)
411 {
412 enum lttng_action_status status;
413 const struct lttng_action_start_session *start_session_action;
414
415 if (!action || !policy || !IS_START_SESSION_ACTION(action)) {
416 status = LTTNG_ACTION_STATUS_INVALID;
417 goto end;
418 }
419
420 start_session_action = action_start_session_from_action_const(action);
421
422 *policy = start_session_action->policy;
423 status = LTTNG_ACTION_STATUS_OK;
424 end:
425 return status;
426 }
427
428 static const struct lttng_rate_policy *
429 lttng_action_start_session_internal_get_rate_policy(const struct lttng_action *action)
430 {
431 const struct lttng_action_start_session *_action;
432 _action = action_start_session_from_action_const(action);
433
434 return _action->policy;
435 }
This page took 0.038284 seconds and 4 git commands to generate.