2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <common/error.hpp>
9 #include <common/macros.hpp>
10 #include <common/mi-lttng.hpp>
12 #include <lttng/condition/condition-internal.hpp>
13 #include <lttng/condition/session-consumed-size-internal.hpp>
14 #include <lttng/constant.h>
18 #define IS_CONSUMED_SIZE_CONDITION(condition) ( \
19 lttng_condition_get_type(condition) == LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE \
22 #define IS_CONSUMED_SIZE_EVALUATION(evaluation) ( \
23 lttng_evaluation_get_type(evaluation) == LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE \
27 void lttng_condition_session_consumed_size_destroy(struct lttng_condition
*condition
)
29 struct lttng_condition_session_consumed_size
*consumed_size
;
31 consumed_size
= container_of(condition
,
32 struct lttng_condition_session_consumed_size
, parent
);
34 free(consumed_size
->session_name
);
39 bool lttng_condition_session_consumed_size_validate(
40 const struct lttng_condition
*condition
)
43 struct lttng_condition_session_consumed_size
*consumed
;
49 consumed
= container_of(condition
, struct lttng_condition_session_consumed_size
,
51 if (!consumed
->session_name
) {
52 ERR("Invalid session consumed size condition: a target session name must be set.");
55 if (!consumed
->consumed_threshold_bytes
.set
) {
56 ERR("Invalid session consumed size condition: a threshold must be set.");
66 int lttng_condition_session_consumed_size_serialize(
67 const struct lttng_condition
*condition
,
68 struct lttng_payload
*payload
)
71 size_t session_name_len
;
72 struct lttng_condition_session_consumed_size
*consumed
;
73 struct lttng_condition_session_consumed_size_comm consumed_comm
;
75 if (!condition
|| !IS_CONSUMED_SIZE_CONDITION(condition
)) {
80 DBG("Serializing session consumed size condition");
81 consumed
= container_of(condition
,
82 struct lttng_condition_session_consumed_size
,
85 session_name_len
= strlen(consumed
->session_name
) + 1;
86 if (session_name_len
> LTTNG_NAME_MAX
) {
91 consumed_comm
.consumed_threshold_bytes
=
92 consumed
->consumed_threshold_bytes
.value
;
93 consumed_comm
.session_name_len
= (uint32_t) session_name_len
;
95 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &consumed_comm
,
96 sizeof(consumed_comm
));
101 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, consumed
->session_name
,
111 bool lttng_condition_session_consumed_size_is_equal(const struct lttng_condition
*_a
,
112 const struct lttng_condition
*_b
)
114 bool is_equal
= false;
115 struct lttng_condition_session_consumed_size
*a
, *b
;
117 a
= container_of(_a
, struct lttng_condition_session_consumed_size
, parent
);
118 b
= container_of(_b
, struct lttng_condition_session_consumed_size
, parent
);
120 if (a
->consumed_threshold_bytes
.set
&& b
->consumed_threshold_bytes
.set
) {
121 uint64_t a_value
, b_value
;
123 a_value
= a
->consumed_threshold_bytes
.value
;
124 b_value
= b
->consumed_threshold_bytes
.value
;
125 if (a_value
!= b_value
) {
130 LTTNG_ASSERT(a
->session_name
);
131 LTTNG_ASSERT(b
->session_name
);
132 if (strcmp(a
->session_name
, b
->session_name
)) {
142 enum lttng_error_code
lttng_condition_session_consumed_size_mi_serialize(
143 const struct lttng_condition
*condition
,
144 struct mi_writer
*writer
)
147 enum lttng_error_code ret_code
;
148 enum lttng_condition_status status
;
149 const char *session_name
= NULL
;
150 uint64_t threshold_bytes
;
152 LTTNG_ASSERT(condition
);
153 LTTNG_ASSERT(writer
);
154 LTTNG_ASSERT(IS_CONSUMED_SIZE_CONDITION(condition
));
156 status
= lttng_condition_session_consumed_size_get_session_name(
157 condition
, &session_name
);
158 LTTNG_ASSERT(status
== LTTNG_CONDITION_STATUS_OK
);
159 LTTNG_ASSERT(session_name
);
161 status
= lttng_condition_session_consumed_size_get_threshold(
162 condition
, &threshold_bytes
);
163 LTTNG_ASSERT(status
== LTTNG_CONDITION_STATUS_OK
);
165 /* Open condition session consumed size element. */
166 ret
= mi_lttng_writer_open_element(writer
,
167 mi_lttng_element_condition_session_consumed_size
);
173 ret
= mi_lttng_writer_write_element_string(
174 writer
, mi_lttng_element_session_name
, session_name
);
179 /* Threshold in bytes. */
180 ret
= mi_lttng_writer_write_element_unsigned_int(writer
,
181 mi_lttng_element_condition_threshold_bytes
,
187 /* Close condition session consumed size element. */
188 ret
= mi_lttng_writer_close_element(writer
);
197 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
202 struct lttng_condition
*lttng_condition_session_consumed_size_create(void)
204 struct lttng_condition_session_consumed_size
*condition
;
206 condition
= zmalloc
<lttng_condition_session_consumed_size
>();
211 lttng_condition_init(&condition
->parent
, LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
);
212 condition
->parent
.validate
= lttng_condition_session_consumed_size_validate
;
213 condition
->parent
.serialize
= lttng_condition_session_consumed_size_serialize
;
214 condition
->parent
.equal
= lttng_condition_session_consumed_size_is_equal
;
215 condition
->parent
.destroy
= lttng_condition_session_consumed_size_destroy
;
216 condition
->parent
.mi_serialize
= lttng_condition_session_consumed_size_mi_serialize
;
217 return &condition
->parent
;
221 ssize_t
init_condition_from_payload(struct lttng_condition
*condition
,
222 struct lttng_payload_view
*src_view
)
224 ssize_t ret
, condition_size
;
225 enum lttng_condition_status status
;
226 const char *session_name
;
227 struct lttng_buffer_view session_name_view
;
228 const struct lttng_condition_session_consumed_size_comm
*condition_comm
;
229 struct lttng_payload_view condition_comm_view
= lttng_payload_view_from_view(
230 src_view
, 0, sizeof(*condition_comm
));
232 if (!lttng_payload_view_is_valid(&condition_comm_view
)) {
233 ERR("Failed to initialize from malformed condition buffer: buffer too short to contain header");
238 condition_comm
= (typeof(condition_comm
)) condition_comm_view
.buffer
.data
;
239 session_name_view
= lttng_buffer_view_from_view(&src_view
->buffer
,
240 sizeof(*condition_comm
), condition_comm
->session_name_len
);
242 if (condition_comm
->session_name_len
> LTTNG_NAME_MAX
) {
243 ERR("Failed to initialize from malformed condition buffer: name exceeds LTTNG_MAX_NAME");
248 if (!lttng_buffer_view_is_valid(&session_name_view
)) {
249 ERR("Failed to initialize from malformed condition buffer: buffer too short to contain element names");
254 status
= lttng_condition_session_consumed_size_set_threshold(condition
,
255 condition_comm
->consumed_threshold_bytes
);
256 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
257 ERR("Failed to initialize session consumed size condition threshold");
262 session_name
= session_name_view
.data
;
263 if (*(session_name
+ condition_comm
->session_name_len
- 1) != '\0') {
264 ERR("Malformed session name encountered in condition buffer");
269 status
= lttng_condition_session_consumed_size_set_session_name(condition
,
271 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
272 ERR("Failed to set session consumed size condition's session name");
277 if (!lttng_condition_validate(condition
)) {
282 condition_size
= sizeof(*condition_comm
) +
283 (ssize_t
) condition_comm
->session_name_len
;
284 ret
= condition_size
;
289 ssize_t
lttng_condition_session_consumed_size_create_from_payload(
290 struct lttng_payload_view
*view
,
291 struct lttng_condition
**_condition
)
294 struct lttng_condition
*condition
=
295 lttng_condition_session_consumed_size_create();
297 if (!_condition
|| !condition
) {
302 ret
= init_condition_from_payload(condition
, view
);
307 *_condition
= condition
;
310 lttng_condition_destroy(condition
);
315 struct lttng_evaluation
*create_evaluation_from_payload(
316 const struct lttng_payload_view
*view
)
318 const struct lttng_evaluation_session_consumed_size_comm
*comm
=
319 (typeof(comm
)) view
->buffer
.data
;
320 struct lttng_evaluation
*evaluation
= NULL
;
322 if (view
->buffer
.size
< sizeof(*comm
)) {
326 evaluation
= lttng_evaluation_session_consumed_size_create(
327 comm
->session_consumed
);
332 ssize_t
lttng_evaluation_session_consumed_size_create_from_payload(
333 struct lttng_payload_view
*view
,
334 struct lttng_evaluation
**_evaluation
)
337 struct lttng_evaluation
*evaluation
= NULL
;
344 evaluation
= create_evaluation_from_payload(view
);
350 *_evaluation
= evaluation
;
351 ret
= sizeof(struct lttng_evaluation_session_consumed_size_comm
);
354 lttng_evaluation_destroy(evaluation
);
358 enum lttng_condition_status
359 lttng_condition_session_consumed_size_get_threshold(
360 const struct lttng_condition
*condition
,
361 uint64_t *consumed_threshold_bytes
)
363 struct lttng_condition_session_consumed_size
*consumed
;
364 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
366 if (!condition
|| !IS_CONSUMED_SIZE_CONDITION(condition
) || !consumed_threshold_bytes
) {
367 status
= LTTNG_CONDITION_STATUS_INVALID
;
371 consumed
= container_of(condition
, struct lttng_condition_session_consumed_size
,
373 if (!consumed
->consumed_threshold_bytes
.set
) {
374 status
= LTTNG_CONDITION_STATUS_UNSET
;
377 *consumed_threshold_bytes
= consumed
->consumed_threshold_bytes
.value
;
382 enum lttng_condition_status
383 lttng_condition_session_consumed_size_set_threshold(
384 struct lttng_condition
*condition
, uint64_t consumed_threshold_bytes
)
386 struct lttng_condition_session_consumed_size
*consumed
;
387 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
389 if (!condition
|| !IS_CONSUMED_SIZE_CONDITION(condition
)) {
390 status
= LTTNG_CONDITION_STATUS_INVALID
;
394 consumed
= container_of(condition
, struct lttng_condition_session_consumed_size
,
396 consumed
->consumed_threshold_bytes
.set
= true;
397 consumed
->consumed_threshold_bytes
.value
= consumed_threshold_bytes
;
402 enum lttng_condition_status
403 lttng_condition_session_consumed_size_get_session_name(
404 const struct lttng_condition
*condition
,
405 const char **session_name
)
407 struct lttng_condition_session_consumed_size
*consumed
;
408 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
410 if (!condition
|| !IS_CONSUMED_SIZE_CONDITION(condition
) || !session_name
) {
411 status
= LTTNG_CONDITION_STATUS_INVALID
;
415 consumed
= container_of(condition
, struct lttng_condition_session_consumed_size
,
417 if (!consumed
->session_name
) {
418 status
= LTTNG_CONDITION_STATUS_UNSET
;
421 *session_name
= consumed
->session_name
;
426 enum lttng_condition_status
427 lttng_condition_session_consumed_size_set_session_name(
428 struct lttng_condition
*condition
, const char *session_name
)
430 char *session_name_copy
;
431 struct lttng_condition_session_consumed_size
*consumed
;
432 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
434 if (!condition
|| !IS_CONSUMED_SIZE_CONDITION(condition
) ||
435 !session_name
|| strlen(session_name
) == 0) {
436 status
= LTTNG_CONDITION_STATUS_INVALID
;
440 consumed
= container_of(condition
, struct lttng_condition_session_consumed_size
,
442 session_name_copy
= strdup(session_name
);
443 if (!session_name_copy
) {
444 status
= LTTNG_CONDITION_STATUS_ERROR
;
448 if (consumed
->session_name
) {
449 free(consumed
->session_name
);
451 consumed
->session_name
= session_name_copy
;
457 int lttng_evaluation_session_consumed_size_serialize(
458 const struct lttng_evaluation
*evaluation
,
459 struct lttng_payload
*payload
)
461 struct lttng_evaluation_session_consumed_size
*consumed
;
462 struct lttng_evaluation_session_consumed_size_comm comm
;
464 consumed
= container_of(evaluation
,
465 struct lttng_evaluation_session_consumed_size
, parent
);
466 comm
.session_consumed
= consumed
->session_consumed
;
467 return lttng_dynamic_buffer_append(
468 &payload
->buffer
, &comm
, sizeof(comm
));
472 void lttng_evaluation_session_consumed_size_destroy(
473 struct lttng_evaluation
*evaluation
)
475 struct lttng_evaluation_session_consumed_size
*consumed
;
477 consumed
= container_of(evaluation
, struct lttng_evaluation_session_consumed_size
,
482 struct lttng_evaluation
*lttng_evaluation_session_consumed_size_create(
485 struct lttng_evaluation_session_consumed_size
*consumed_eval
;
487 consumed_eval
= zmalloc
<lttng_evaluation_session_consumed_size
>();
488 if (!consumed_eval
) {
492 consumed_eval
->parent
.type
= LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
;
493 consumed_eval
->session_consumed
= consumed
;
494 consumed_eval
->parent
.serialize
= lttng_evaluation_session_consumed_size_serialize
;
495 consumed_eval
->parent
.destroy
= lttng_evaluation_session_consumed_size_destroy
;
497 return &consumed_eval
->parent
;
500 enum lttng_evaluation_status
501 lttng_evaluation_session_consumed_size_get_consumed_size(
502 const struct lttng_evaluation
*evaluation
,
503 uint64_t *session_consumed
)
505 struct lttng_evaluation_session_consumed_size
*consumed
;
506 enum lttng_evaluation_status status
= LTTNG_EVALUATION_STATUS_OK
;
508 if (!evaluation
|| !IS_CONSUMED_SIZE_EVALUATION(evaluation
) ||
510 status
= LTTNG_EVALUATION_STATUS_INVALID
;
514 consumed
= container_of(evaluation
, struct lttng_evaluation_session_consumed_size
,
516 *session_consumed
= consumed
->session_consumed
;