2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <lttng/condition/condition-internal.h>
9 #include <lttng/condition/session-consumed-size-internal.h>
10 #include <lttng/constant.h>
11 #include <common/macros.h>
12 #include <common/error.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 assert(a
->session_name
);
131 assert(b
->session_name
);
132 if (strcmp(a
->session_name
, b
->session_name
)) {
141 struct lttng_condition
*lttng_condition_session_consumed_size_create(void)
143 struct lttng_condition_session_consumed_size
*condition
;
145 condition
= zmalloc(sizeof(struct lttng_condition_session_consumed_size
));
150 lttng_condition_init(&condition
->parent
, LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
);
151 condition
->parent
.validate
= lttng_condition_session_consumed_size_validate
;
152 condition
->parent
.serialize
= lttng_condition_session_consumed_size_serialize
;
153 condition
->parent
.equal
= lttng_condition_session_consumed_size_is_equal
;
154 condition
->parent
.destroy
= lttng_condition_session_consumed_size_destroy
;
155 return &condition
->parent
;
159 ssize_t
init_condition_from_payload(struct lttng_condition
*condition
,
160 struct lttng_payload_view
*src_view
)
162 ssize_t ret
, condition_size
;
163 enum lttng_condition_status status
;
164 const char *session_name
;
165 struct lttng_buffer_view session_name_view
;
166 const struct lttng_condition_session_consumed_size_comm
*condition_comm
;
167 struct lttng_payload_view condition_comm_view
= lttng_payload_view_from_view(
168 src_view
, 0, sizeof(*condition_comm
));
170 if (!lttng_payload_view_is_valid(&condition_comm_view
)) {
171 ERR("Failed to initialize from malformed condition buffer: buffer too short to contain header");
176 condition_comm
= (typeof(condition_comm
)) condition_comm_view
.buffer
.data
;
177 session_name_view
= lttng_buffer_view_from_view(&src_view
->buffer
,
178 sizeof(*condition_comm
), condition_comm
->session_name_len
);
180 if (condition_comm
->session_name_len
> LTTNG_NAME_MAX
) {
181 ERR("Failed to initialize from malformed condition buffer: name exceeds LTTNG_MAX_NAME");
186 if (!lttng_buffer_view_is_valid(&session_name_view
)) {
187 ERR("Failed to initialize from malformed condition buffer: buffer too short to contain element names");
192 status
= lttng_condition_session_consumed_size_set_threshold(condition
,
193 condition_comm
->consumed_threshold_bytes
);
194 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
195 ERR("Failed to initialize session consumed size condition threshold");
200 session_name
= session_name_view
.data
;
201 if (*(session_name
+ condition_comm
->session_name_len
- 1) != '\0') {
202 ERR("Malformed session name encountered in condition buffer");
207 status
= lttng_condition_session_consumed_size_set_session_name(condition
,
209 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
210 ERR("Failed to set session consumed size condition's session name");
215 if (!lttng_condition_validate(condition
)) {
220 condition_size
= sizeof(*condition_comm
) +
221 (ssize_t
) condition_comm
->session_name_len
;
222 ret
= condition_size
;
228 ssize_t
lttng_condition_session_consumed_size_create_from_payload(
229 struct lttng_payload_view
*view
,
230 struct lttng_condition
**_condition
)
233 struct lttng_condition
*condition
=
234 lttng_condition_session_consumed_size_create();
236 if (!_condition
|| !condition
) {
241 ret
= init_condition_from_payload(condition
, view
);
246 *_condition
= condition
;
249 lttng_condition_destroy(condition
);
254 struct lttng_evaluation
*create_evaluation_from_payload(
255 const struct lttng_payload_view
*view
)
257 const struct lttng_evaluation_session_consumed_size_comm
*comm
=
258 (typeof(comm
)) view
->buffer
.data
;
259 struct lttng_evaluation
*evaluation
= NULL
;
261 if (view
->buffer
.size
< sizeof(*comm
)) {
265 evaluation
= lttng_evaluation_session_consumed_size_create(
266 comm
->session_consumed
);
272 ssize_t
lttng_evaluation_session_consumed_size_create_from_payload(
273 struct lttng_payload_view
*view
,
274 struct lttng_evaluation
**_evaluation
)
277 struct lttng_evaluation
*evaluation
= NULL
;
284 evaluation
= create_evaluation_from_payload(view
);
290 *_evaluation
= evaluation
;
291 ret
= sizeof(struct lttng_evaluation_session_consumed_size_comm
);
294 lttng_evaluation_destroy(evaluation
);
298 enum lttng_condition_status
299 lttng_condition_session_consumed_size_get_threshold(
300 const struct lttng_condition
*condition
,
301 uint64_t *consumed_threshold_bytes
)
303 struct lttng_condition_session_consumed_size
*consumed
;
304 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
306 if (!condition
|| !IS_CONSUMED_SIZE_CONDITION(condition
) || !consumed_threshold_bytes
) {
307 status
= LTTNG_CONDITION_STATUS_INVALID
;
311 consumed
= container_of(condition
, struct lttng_condition_session_consumed_size
,
313 if (!consumed
->consumed_threshold_bytes
.set
) {
314 status
= LTTNG_CONDITION_STATUS_UNSET
;
317 *consumed_threshold_bytes
= consumed
->consumed_threshold_bytes
.value
;
322 enum lttng_condition_status
323 lttng_condition_session_consumed_size_set_threshold(
324 struct lttng_condition
*condition
, uint64_t consumed_threshold_bytes
)
326 struct lttng_condition_session_consumed_size
*consumed
;
327 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
329 if (!condition
|| !IS_CONSUMED_SIZE_CONDITION(condition
)) {
330 status
= LTTNG_CONDITION_STATUS_INVALID
;
334 consumed
= container_of(condition
, struct lttng_condition_session_consumed_size
,
336 consumed
->consumed_threshold_bytes
.set
= true;
337 consumed
->consumed_threshold_bytes
.value
= consumed_threshold_bytes
;
342 enum lttng_condition_status
343 lttng_condition_session_consumed_size_get_session_name(
344 const struct lttng_condition
*condition
,
345 const char **session_name
)
347 struct lttng_condition_session_consumed_size
*consumed
;
348 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
350 if (!condition
|| !IS_CONSUMED_SIZE_CONDITION(condition
) || !session_name
) {
351 status
= LTTNG_CONDITION_STATUS_INVALID
;
355 consumed
= container_of(condition
, struct lttng_condition_session_consumed_size
,
357 if (!consumed
->session_name
) {
358 status
= LTTNG_CONDITION_STATUS_UNSET
;
361 *session_name
= consumed
->session_name
;
366 enum lttng_condition_status
367 lttng_condition_session_consumed_size_set_session_name(
368 struct lttng_condition
*condition
, const char *session_name
)
370 char *session_name_copy
;
371 struct lttng_condition_session_consumed_size
*consumed
;
372 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
374 if (!condition
|| !IS_CONSUMED_SIZE_CONDITION(condition
) ||
375 !session_name
|| strlen(session_name
) == 0) {
376 status
= LTTNG_CONDITION_STATUS_INVALID
;
380 consumed
= container_of(condition
, struct lttng_condition_session_consumed_size
,
382 session_name_copy
= strdup(session_name
);
383 if (!session_name_copy
) {
384 status
= LTTNG_CONDITION_STATUS_ERROR
;
388 if (consumed
->session_name
) {
389 free(consumed
->session_name
);
391 consumed
->session_name
= session_name_copy
;
397 int lttng_evaluation_session_consumed_size_serialize(
398 const struct lttng_evaluation
*evaluation
,
399 struct lttng_payload
*payload
)
401 struct lttng_evaluation_session_consumed_size
*consumed
;
402 struct lttng_evaluation_session_consumed_size_comm comm
;
404 consumed
= container_of(evaluation
,
405 struct lttng_evaluation_session_consumed_size
, parent
);
406 comm
.session_consumed
= consumed
->session_consumed
;
407 return lttng_dynamic_buffer_append(
408 &payload
->buffer
, &comm
, sizeof(comm
));
412 void lttng_evaluation_session_consumed_size_destroy(
413 struct lttng_evaluation
*evaluation
)
415 struct lttng_evaluation_session_consumed_size
*consumed
;
417 consumed
= container_of(evaluation
, struct lttng_evaluation_session_consumed_size
,
423 struct lttng_evaluation
*lttng_evaluation_session_consumed_size_create(
426 struct lttng_evaluation_session_consumed_size
*consumed_eval
;
428 consumed_eval
= zmalloc(sizeof(struct lttng_evaluation_session_consumed_size
));
429 if (!consumed_eval
) {
433 consumed_eval
->parent
.type
= LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
;
434 consumed_eval
->session_consumed
= consumed
;
435 consumed_eval
->parent
.serialize
= lttng_evaluation_session_consumed_size_serialize
;
436 consumed_eval
->parent
.destroy
= lttng_evaluation_session_consumed_size_destroy
;
438 return &consumed_eval
->parent
;
441 enum lttng_evaluation_status
442 lttng_evaluation_session_consumed_size_get_consumed_size(
443 const struct lttng_evaluation
*evaluation
,
444 uint64_t *session_consumed
)
446 struct lttng_evaluation_session_consumed_size
*consumed
;
447 enum lttng_evaluation_status status
= LTTNG_EVALUATION_STATUS_OK
;
449 if (!evaluation
|| !IS_CONSUMED_SIZE_EVALUATION(evaluation
) ||
451 status
= LTTNG_EVALUATION_STATUS_INVALID
;
455 consumed
= container_of(evaluation
, struct lttng_evaluation_session_consumed_size
,
457 *session_consumed
= consumed
->session_consumed
;