common: compile libcommon as C++
[lttng-tools.git] / src / common / actions / action.cpp
CommitLineData
a58c490f 1/*
ab5be9fa 2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
a58c490f 3 *
ab5be9fa 4 * SPDX-License-Identifier: LGPL-2.1-only
a58c490f 5 *
a58c490f
JG
6 */
7
14ec7e87 8#include <common/error.h>
6a751b95 9#include <common/mi-lttng.h>
a58c490f 10#include <lttng/action/action-internal.h>
ad63a966 11#include <lttng/action/list-internal.h>
a58c490f 12#include <lttng/action/notify-internal.h>
7f4d5b07 13#include <lttng/action/rate-policy-internal.h>
bfb2ec6a 14#include <lttng/action/rotate-session-internal.h>
757c48a2 15#include <lttng/action/snapshot-session-internal.h>
58397d0d 16#include <lttng/action/start-session-internal.h>
931bdbaa 17#include <lttng/action/stop-session-internal.h>
588c4b0d 18#include <lttng/error-query-internal.h>
a58c490f 19
10615eee 20const char *lttng_action_type_string(enum lttng_action_type action_type)
2666d352
SM
21{
22 switch (action_type) {
23 case LTTNG_ACTION_TYPE_UNKNOWN:
24 return "UNKNOWN";
7c2fae7c
JG
25 case LTTNG_ACTION_TYPE_LIST:
26 return "LIST";
2666d352
SM
27 case LTTNG_ACTION_TYPE_NOTIFY:
28 return "NOTIFY";
bfb2ec6a
SM
29 case LTTNG_ACTION_TYPE_ROTATE_SESSION:
30 return "ROTATE_SESSION";
757c48a2
SM
31 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION:
32 return "SNAPSHOT_SESSION";
58397d0d
SM
33 case LTTNG_ACTION_TYPE_START_SESSION:
34 return "START_SESSION";
931bdbaa
SM
35 case LTTNG_ACTION_TYPE_STOP_SESSION:
36 return "STOP_SESSION";
2666d352
SM
37 default:
38 return "???";
39 }
40}
41
17182cfd 42enum lttng_action_type lttng_action_get_type(const struct lttng_action *action)
a58c490f
JG
43{
44 return action ? action->type : LTTNG_ACTION_TYPE_UNKNOWN;
45}
46
2d57482c 47void lttng_action_init(struct lttng_action *action,
6acb3f46
SM
48 enum lttng_action_type type,
49 action_validate_cb validate,
50 action_serialize_cb serialize,
3dd04a6a 51 action_equal_cb equal,
2d57482c 52 action_destroy_cb destroy,
588c4b0d 53 action_get_rate_policy_cb get_rate_policy,
6a751b95
JR
54 action_add_error_query_results_cb add_error_query_results,
55 action_mi_serialize_cb mi)
6acb3f46 56{
c852ce4e 57 urcu_ref_init(&action->ref);
6acb3f46
SM
58 action->type = type;
59 action->validate = validate;
60 action->serialize = serialize;
3dd04a6a 61 action->equal = equal;
6acb3f46 62 action->destroy = destroy;
7f4d5b07 63 action->get_rate_policy = get_rate_policy;
588c4b0d 64 action->add_error_query_results = add_error_query_results;
6a751b95 65 action->mi_serialize = mi;
2d57482c
JR
66
67 action->execution_request_counter = 0;
68 action->execution_counter = 0;
69 action->execution_failure_counter = 0;
6acb3f46
SM
70}
71
c852ce4e
JG
72static
73void action_destroy_ref(struct urcu_ref *ref)
74{
75 struct lttng_action *action =
76 container_of(ref, struct lttng_action, ref);
77
78 action->destroy(action);
79}
80
c852ce4e
JG
81void lttng_action_get(struct lttng_action *action)
82{
83 urcu_ref_get(&action->ref);
84}
85
c852ce4e 86void lttng_action_put(struct lttng_action *action)
a58c490f
JG
87{
88 if (!action) {
89 return;
90 }
91
a0377dfe 92 LTTNG_ASSERT(action->destroy);
c852ce4e
JG
93 urcu_ref_put(&action->ref, action_destroy_ref);
94}
95
96void lttng_action_destroy(struct lttng_action *action)
97{
98 lttng_action_put(action);
a58c490f
JG
99}
100
a58c490f
JG
101bool lttng_action_validate(struct lttng_action *action)
102{
103 bool valid;
104
105 if (!action) {
106 valid = false;
107 goto end;
108 }
109
110 if (!action->validate) {
111 /* Sub-class guarantees that it can never be invalid. */
112 valid = true;
113 goto end;
114 }
115
116 valid = action->validate(action);
117end:
118 return valid;
119}
120
3647288f 121int lttng_action_serialize(struct lttng_action *action,
c0a66c84 122 struct lttng_payload *payload)
a58c490f 123{
3647288f
JG
124 int ret;
125 struct lttng_action_comm action_comm = {
126 .action_type = (int8_t) action->type,
127 };
128
c0a66c84 129 ret = lttng_dynamic_buffer_append(&payload->buffer, &action_comm,
3647288f
JG
130 sizeof(action_comm));
131 if (ret) {
a58c490f
JG
132 goto end;
133 }
134
c0a66c84 135 ret = action->serialize(action, payload);
3647288f 136 if (ret) {
a58c490f
JG
137 goto end;
138 }
a58c490f
JG
139end:
140 return ret;
141}
142
c0a66c84 143ssize_t lttng_action_create_from_payload(struct lttng_payload_view *view,
869a3c2d 144 struct lttng_action **action)
a58c490f 145{
869a3c2d 146 ssize_t consumed_len, specific_action_consumed_len;
c0a66c84 147 action_create_from_payload_cb create_from_payload_cb;
3e6e0df2
JG
148 const struct lttng_action_comm *action_comm;
149 const struct lttng_payload_view action_comm_view =
150 lttng_payload_view_from_view(
151 view, 0, sizeof(*action_comm));
a58c490f 152
869a3c2d
SM
153 if (!view || !action) {
154 consumed_len = -1;
a58c490f
JG
155 goto end;
156 }
157
3e6e0df2
JG
158 if (!lttng_payload_view_is_valid(&action_comm_view)) {
159 /* Payload not large enough to contain the header. */
160 consumed_len = -1;
161 goto end;
162 }
163
164 action_comm = (const struct lttng_action_comm *) action_comm_view.buffer.data;
869a3c2d 165
c0a66c84 166 DBG("Create action from payload: action-type=%s",
a6bc4ca9 167 lttng_action_type_string((lttng_action_type) action_comm->action_type));
2666d352 168
a58c490f
JG
169 switch (action_comm->action_type) {
170 case LTTNG_ACTION_TYPE_NOTIFY:
c0a66c84 171 create_from_payload_cb = lttng_action_notify_create_from_payload;
a58c490f 172 break;
bfb2ec6a 173 case LTTNG_ACTION_TYPE_ROTATE_SESSION:
c0a66c84
JG
174 create_from_payload_cb =
175 lttng_action_rotate_session_create_from_payload;
bfb2ec6a 176 break;
757c48a2
SM
177 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION:
178 create_from_payload_cb =
179 lttng_action_snapshot_session_create_from_payload;
180 break;
58397d0d 181 case LTTNG_ACTION_TYPE_START_SESSION:
c0a66c84
JG
182 create_from_payload_cb =
183 lttng_action_start_session_create_from_payload;
58397d0d 184 break;
931bdbaa 185 case LTTNG_ACTION_TYPE_STOP_SESSION:
c0a66c84
JG
186 create_from_payload_cb =
187 lttng_action_stop_session_create_from_payload;
931bdbaa 188 break;
7c2fae7c 189 case LTTNG_ACTION_TYPE_LIST:
702f26c8 190 create_from_payload_cb = lttng_action_list_create_from_payload;
0c51e8f3 191 break;
a58c490f 192 default:
c0a66c84 193 ERR("Failed to create action from payload, unhandled action type: action-type=%u (%s)",
2666d352
SM
194 action_comm->action_type,
195 lttng_action_type_string(
a6bc4ca9 196 (lttng_action_type) action_comm->action_type));
869a3c2d 197 consumed_len = -1;
a58c490f
JG
198 goto end;
199 }
200
c0a66c84
JG
201 {
202 /* Create buffer view for the action-type-specific data. */
203 struct lttng_payload_view specific_action_view =
204 lttng_payload_view_from_view(view,
205 sizeof(struct lttng_action_comm),
206 -1);
869a3c2d 207
c0a66c84
JG
208 specific_action_consumed_len = create_from_payload_cb(
209 &specific_action_view, action);
210 }
869a3c2d
SM
211 if (specific_action_consumed_len < 0) {
212 ERR("Failed to create specific action from buffer.");
213 consumed_len = -1;
a58c490f
JG
214 goto end;
215 }
869a3c2d 216
a0377dfe 217 LTTNG_ASSERT(*action);
869a3c2d
SM
218
219 consumed_len = sizeof(struct lttng_action_comm) +
220 specific_action_consumed_len;
221
a58c490f 222end:
869a3c2d 223 return consumed_len;
a58c490f 224}
3dd04a6a 225
3dd04a6a
JR
226bool lttng_action_is_equal(const struct lttng_action *a,
227 const struct lttng_action *b)
228{
229 bool is_equal = false;
230
231 if (!a || !b) {
232 goto end;
233 }
234
235 if (a->type != b->type) {
236 goto end;
237 }
238
239 if (a == b) {
240 is_equal = true;
241 goto end;
242 }
243
a0377dfe 244 LTTNG_ASSERT(a->equal);
3dd04a6a
JR
245 is_equal = a->equal(a, b);
246end:
247 return is_equal;
248}
2d57482c 249
2d57482c
JR
250void lttng_action_increase_execution_request_count(struct lttng_action *action)
251{
252 action->execution_request_counter++;
253}
254
2d57482c
JR
255void lttng_action_increase_execution_count(struct lttng_action *action)
256{
257 action->execution_counter++;
258}
259
2d57482c
JR
260void lttng_action_increase_execution_failure_count(struct lttng_action *action)
261{
588c4b0d 262 uatomic_inc(&action->execution_failure_counter);
2d57482c
JR
263}
264
2d57482c
JR
265bool lttng_action_should_execute(const struct lttng_action *action)
266{
7f4d5b07 267 const struct lttng_rate_policy *policy = NULL;
2d57482c
JR
268 bool execute = false;
269
7f4d5b07 270 if (action->get_rate_policy == NULL) {
2d57482c
JR
271 execute = true;
272 goto end;
273 }
274
7f4d5b07 275 policy = action->get_rate_policy(action);
2d57482c
JR
276 if (policy == NULL) {
277 execute = true;
278 goto end;
279 }
280
7f4d5b07 281 execute = lttng_rate_policy_should_execute(
2d57482c
JR
282 policy, action->execution_request_counter);
283end:
284 return execute;
285}
588c4b0d 286
588c4b0d
JG
287enum lttng_action_status lttng_action_add_error_query_results(
288 const struct lttng_action *action,
289 struct lttng_error_query_results *results)
290{
291 return action->add_error_query_results(action, results);
292}
293
588c4b0d
JG
294enum lttng_action_status lttng_action_generic_add_error_query_results(
295 const struct lttng_action *action,
296 struct lttng_error_query_results *results)
297{
298 enum lttng_action_status action_status;
299 struct lttng_error_query_result *error_counter = NULL;
300 const uint64_t execution_failure_counter =
301 uatomic_read(&action->execution_failure_counter);
302
303 error_counter = lttng_error_query_result_counter_create(
304 "total execution failures",
305 "Aggregated count of errors encountered when executing the action",
306 execution_failure_counter);
307 if (!error_counter) {
308 action_status = LTTNG_ACTION_STATUS_ERROR;
309 goto end;
310 }
311
312 if (lttng_error_query_results_add_result(
313 results, error_counter)) {
314 action_status = LTTNG_ACTION_STATUS_ERROR;
315 goto end;
316 }
317
318 /* Ownership transferred to the results. */
319 error_counter = NULL;
320 action_status = LTTNG_ACTION_STATUS_OK;
321end:
322 lttng_error_query_result_destroy(error_counter);
323 return action_status;
324}
6a751b95 325
6a751b95
JR
326enum lttng_error_code lttng_action_mi_serialize(const struct lttng_trigger *trigger,
327 const struct lttng_action *action,
328 struct mi_writer *writer,
329 const struct mi_lttng_error_query_callbacks
330 *error_query_callbacks,
331 struct lttng_dynamic_array *action_path_indexes)
332{
333 int ret;
334 enum lttng_error_code ret_code;
335 struct lttng_action_path *action_path = NULL;
336 struct lttng_error_query_results *error_query_results = NULL;
337
a0377dfe
FD
338 LTTNG_ASSERT(action);
339 LTTNG_ASSERT(writer);
6a751b95
JR
340
341 /* Open action. */
342 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_action);
343 if (ret) {
344 goto mi_error;
345 }
346
347 if (action->type == LTTNG_ACTION_TYPE_LIST) {
348 /*
349 * Recursion is safe since action lists can't be nested for
350 * the moment.
351 */
352 ret_code = lttng_action_list_mi_serialize(trigger, action, writer,
353 error_query_callbacks, action_path_indexes);
354 if (ret_code != LTTNG_OK) {
355 goto end;
356 }
357
358 /* Nothing else to do. */
359 goto close_action_element;
360 }
361
a0377dfe 362 LTTNG_ASSERT(action->mi_serialize);
6a751b95
JR
363 ret_code = action->mi_serialize(action, writer);
364 if (ret_code != LTTNG_OK) {
365 goto end;
366 }
367
368 /* Error query for the action. */
369 if (error_query_callbacks && error_query_callbacks->action_cb) {
370 const uint64_t *action_path_indexes_raw_pointer = NULL;
371 const size_t action_path_indexes_size =
372 lttng_dynamic_array_get_count(
373 action_path_indexes);
374
375 if (action_path_indexes_size != 0) {
376 action_path_indexes_raw_pointer =
377 (const uint64_t *) action_path_indexes
378 ->buffer.data;
379 }
380
381 action_path = lttng_action_path_create(
382 action_path_indexes_raw_pointer,
383 action_path_indexes_size);
a0377dfe 384 LTTNG_ASSERT(action_path);
6a751b95
JR
385
386 ret_code = error_query_callbacks->action_cb(
387 trigger, action_path, &error_query_results);
388 if (ret_code != LTTNG_OK) {
389 goto end;
390 }
391
392 /* Serialize the error query results. */
393 ret_code = lttng_error_query_results_mi_serialize(
394 error_query_results, writer);
395 if (ret_code != LTTNG_OK) {
396 goto end;
397 }
398 }
399
400close_action_element:
401 /* Close action. */
402 ret = mi_lttng_writer_close_element(writer);
403 if (ret) {
404 goto mi_error;
405 }
406
407 ret_code = LTTNG_OK;
408 goto end;
409
410mi_error:
411 ret_code = LTTNG_ERR_MI_IO_FAIL;
412end:
413 lttng_action_path_destroy(action_path);
414 lttng_error_query_results_destroy(error_query_results);
415 return ret_code;
416}
This page took 0.057985 seconds and 4 git commands to generate.