2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #include <lttng/condition/condition-internal.h>
19 #include <lttng/condition/buffer-usage-internal.h>
20 #include <common/macros.h>
21 #include <common/error.h>
27 double fixed_to_double(uint64_t val
)
29 return (double) val
/ (double) UINT64_MAX
;
33 uint64_t double_to_fixed(double val
)
35 return (val
* (double) UINT64_MAX
);
39 bool is_usage_condition(struct lttng_condition
*condition
)
41 enum lttng_condition_type type
= lttng_condition_get_type(condition
);
43 return type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
||
44 type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
;
48 bool is_usage_evaluation(struct lttng_evaluation
*evaluation
)
50 enum lttng_condition_type type
= lttng_evaluation_get_type(evaluation
);
52 return type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
||
53 type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
;
57 void lttng_condition_buffer_usage_destroy(struct lttng_condition
*condition
)
59 struct lttng_condition_buffer_usage
*usage
;
61 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
64 free(usage
->session_name
);
65 free(usage
->channel_name
);
70 bool lttng_condition_buffer_usage_validate(struct lttng_condition
*condition
)
73 struct lttng_condition_buffer_usage
*usage
;
79 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
81 if (!usage
->session_name
) {
82 ERR("Invalid buffer condition: a target session name must be set.");
85 if (!usage
->channel_name
) {
86 ERR("Invalid buffer condition: a target channel name must be set.");
89 if (!usage
->threshold_ratio
.set
&& !usage
->threshold_bytes
.set
) {
90 ERR("Invalid buffer condition: a threshold must be set.");
100 ssize_t
lttng_condition_buffer_usage_serialize(struct lttng_condition
*condition
,
103 struct lttng_condition_buffer_usage
*usage
;
105 size_t session_name_len
, channel_name_len
;
107 if (!condition
|| !is_usage_condition(condition
)) {
112 DBG("Serializing buffer usage condition");
113 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
115 size
= sizeof(struct lttng_condition_buffer_usage_comm
);
116 session_name_len
= strlen(usage
->session_name
) + 1;
117 channel_name_len
= strlen(usage
->channel_name
) + 1;
118 size
+= session_name_len
+ channel_name_len
;
120 struct lttng_condition_buffer_usage_comm usage_comm
= {
121 .threshold_set_in_bytes
= usage
->threshold_bytes
.set
? 1 : 0,
122 .session_name_len
= session_name_len
,
123 .channel_name_len
= channel_name_len
,
124 .domain_type
= (int8_t) usage
->domain
.type
,
127 if (usage
->threshold_bytes
.set
) {
128 usage_comm
.threshold
= usage
->threshold_bytes
.value
;
130 usage_comm
.threshold
= double_to_fixed(
131 usage
->threshold_ratio
.value
);
134 memcpy(buf
, &usage_comm
, sizeof(usage_comm
));
135 buf
+= sizeof(usage_comm
);
136 memcpy(buf
, usage
->session_name
, session_name_len
);
137 buf
+= session_name_len
;
138 memcpy(buf
, usage
->channel_name
, channel_name_len
);
139 buf
+= channel_name_len
;
146 bool lttng_condition_buffer_usage_is_equal(struct lttng_condition
*_a
,
147 struct lttng_condition
*_b
)
149 bool is_equal
= false;
150 struct lttng_condition_buffer_usage
*a
, *b
;
152 a
= container_of(_a
, struct lttng_condition_buffer_usage
, parent
);
153 b
= container_of(_b
, struct lttng_condition_buffer_usage
, parent
);
155 if ((a
->threshold_ratio
.set
&& !b
->threshold_ratio
.set
) ||
156 (a
->threshold_bytes
.set
&& !b
->threshold_bytes
.set
)) {
160 if (a
->threshold_ratio
.set
&& b
->threshold_ratio
.set
) {
161 double a_value
, b_value
, diff
;
163 a_value
= a
->threshold_ratio
.value
;
164 b_value
= b
->threshold_ratio
.value
;
165 diff
= fabs(a_value
- b_value
);
167 if (diff
> DBL_EPSILON
) {
170 } else if (a
->threshold_bytes
.set
&& b
->threshold_bytes
.set
) {
171 uint64_t a_value
, b_value
;
173 a_value
= a
->threshold_bytes
.value
;
174 b_value
= b
->threshold_bytes
.value
;
175 if (a_value
!= b_value
) {
180 if ((a
->session_name
&& !b
->session_name
) ||
181 (!a
->session_name
&& b
->session_name
)) {
185 if (a
->channel_name
&& b
->channel_name
) {
186 if (strcmp(a
->channel_name
, b
->channel_name
)) {
189 } if ((a
->channel_name
&& !b
->channel_name
) ||
190 (!a
->channel_name
&& b
->channel_name
)) {
194 if (a
->channel_name
&& b
->channel_name
) {
195 if (strcmp(a
->channel_name
, b
->channel_name
)) {
200 if ((a
->domain
.set
&& !b
->domain
.set
) ||
201 (!a
->domain
.set
&& b
->domain
.set
)) {
205 if (a
->domain
.set
&& b
->domain
.set
) {
206 if (a
->domain
.type
!= b
->domain
.type
) {
216 struct lttng_condition
*lttng_condition_buffer_usage_create(
217 enum lttng_condition_type type
)
219 struct lttng_condition_buffer_usage
*condition
;
221 condition
= zmalloc(sizeof(struct lttng_condition_buffer_usage
));
226 lttng_condition_init(&condition
->parent
, type
);
227 condition
->parent
.validate
= lttng_condition_buffer_usage_validate
;
228 condition
->parent
.serialize
= lttng_condition_buffer_usage_serialize
;
229 condition
->parent
.equal
= lttng_condition_buffer_usage_is_equal
;
230 condition
->parent
.destroy
= lttng_condition_buffer_usage_destroy
;
232 return &condition
->parent
;
235 struct lttng_condition
*lttng_condition_buffer_usage_low_create(void)
237 return lttng_condition_buffer_usage_create(
238 LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
);
241 struct lttng_condition
*lttng_condition_buffer_usage_high_create(void)
243 return lttng_condition_buffer_usage_create(
244 LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
);
248 ssize_t
init_condition_from_buffer(struct lttng_condition
*condition
,
251 ssize_t ret
, condition_size
;
252 enum lttng_condition_status status
;
253 enum lttng_domain_type domain_type
;
254 struct lttng_condition_buffer_usage_comm
*condition_comm
=
255 (struct lttng_condition_buffer_usage_comm
*) buf
;
256 const char *session_name
, *channel_name
;
258 if (condition_comm
->threshold_set_in_bytes
) {
259 status
= lttng_condition_buffer_usage_set_threshold(condition
,
260 condition_comm
->threshold
);
262 status
= lttng_condition_buffer_usage_set_threshold_ratio(
264 fixed_to_double(condition_comm
->threshold
));
266 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
267 ERR("Failed to initialize buffer usage condition threshold");
272 if (condition_comm
->domain_type
<= LTTNG_DOMAIN_NONE
||
273 condition_comm
->domain_type
> LTTNG_DOMAIN_PYTHON
) {
274 /* Invalid domain value. */
275 ERR("Invalid domain type value (%i) found in condition buffer",
276 (int) condition_comm
->domain_type
);
281 domain_type
= (enum lttng_domain_type
) condition_comm
->domain_type
;
282 status
= lttng_condition_buffer_usage_set_domain_type(condition
,
284 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
285 ERR("Failed to set buffer usage condition domain");
290 session_name
= buf
+ sizeof(struct lttng_condition_buffer_usage_comm
);
291 channel_name
= session_name
+ condition_comm
->session_name_len
;
293 status
= lttng_condition_buffer_usage_set_session_name(condition
,
295 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
296 ERR("Failed to set buffer usage session name");
301 status
= lttng_condition_buffer_usage_set_channel_name(condition
,
303 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
304 ERR("Failed to set buffer usage channel name");
309 if (!lttng_condition_validate(condition
)) {
314 condition_size
= sizeof(*condition_comm
);
315 condition_size
+= (ssize_t
) condition_comm
->session_name_len
;
316 condition_size
+= (ssize_t
) condition_comm
->channel_name_len
;
317 ret
= condition_size
;
323 ssize_t
lttng_condition_buffer_usage_low_create_from_buffer(const char *buf
,
324 struct lttng_condition
**_condition
)
327 struct lttng_condition
*condition
=
328 lttng_condition_buffer_usage_low_create();
330 if (!_condition
|| !condition
) {
335 ret
= init_condition_from_buffer(condition
, buf
);
340 *_condition
= condition
;
343 lttng_condition_destroy(condition
);
348 ssize_t
lttng_condition_buffer_usage_high_create_from_buffer(const char *buf
,
349 struct lttng_condition
**_condition
)
352 struct lttng_condition
*condition
=
353 lttng_condition_buffer_usage_high_create();
355 if (!_condition
|| !condition
) {
360 ret
= init_condition_from_buffer(condition
, buf
);
365 *_condition
= condition
;
368 lttng_condition_destroy(condition
);
373 struct lttng_evaluation
*create_evaluation_from_buffer(
374 enum lttng_condition_type type
, const char *buf
)
376 struct lttng_evaluation_buffer_usage_comm
*comm
=
377 (struct lttng_evaluation_buffer_usage_comm
*) buf
;
378 struct lttng_evaluation
*evaluation
;
380 evaluation
= lttng_evaluation_buffer_usage_create(type
,
381 comm
->buffer_use
, comm
->buffer_capacity
);
386 ssize_t
lttng_evaluation_buffer_usage_low_create_from_buffer(const char *buf
,
387 struct lttng_evaluation
**_evaluation
)
390 struct lttng_evaluation
*evaluation
;
397 evaluation
= create_evaluation_from_buffer(
398 LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
, buf
);
403 *_evaluation
= evaluation
;
404 ret
= sizeof(struct lttng_evaluation_buffer_usage_comm
);
407 lttng_evaluation_destroy(evaluation
);
412 ssize_t
lttng_evaluation_buffer_usage_high_create_from_buffer(const char *buf
,
413 struct lttng_evaluation
**_evaluation
)
416 struct lttng_evaluation
*evaluation
;
423 evaluation
= create_evaluation_from_buffer(
424 LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
, buf
);
429 *_evaluation
= evaluation
;
430 ret
= sizeof(struct lttng_evaluation_buffer_usage_comm
);
433 lttng_evaluation_destroy(evaluation
);
437 enum lttng_condition_status
438 lttng_condition_buffer_usage_get_threshold_ratio(
439 struct lttng_condition
*condition
, double *threshold_ratio
)
441 struct lttng_condition_buffer_usage
*usage
;
442 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
444 if (!condition
|| !is_usage_condition(condition
) ||
446 status
= LTTNG_CONDITION_STATUS_INVALID
;
450 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
452 if (!usage
->threshold_ratio
.set
) {
453 status
= LTTNG_CONDITION_STATUS_UNSET
;
456 *threshold_ratio
= usage
->threshold_ratio
.value
;
461 /* threshold_ratio expressed as [0.0, 1.0]. */
462 enum lttng_condition_status
463 lttng_condition_buffer_usage_set_threshold_ratio(
464 struct lttng_condition
*condition
, double threshold_ratio
)
466 struct lttng_condition_buffer_usage
*usage
;
467 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
469 if (!condition
|| !is_usage_condition(condition
) ||
470 threshold_ratio
< 0.0 ||
471 threshold_ratio
> 1.0) {
472 status
= LTTNG_CONDITION_STATUS_INVALID
;
476 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
478 usage
->threshold_ratio
.set
= true;
479 usage
->threshold_bytes
.set
= false;
480 usage
->threshold_ratio
.value
= threshold_ratio
;
485 enum lttng_condition_status
486 lttng_condition_buffer_usage_get_threshold(
487 struct lttng_condition
*condition
, uint64_t *threshold_bytes
)
489 struct lttng_condition_buffer_usage
*usage
;
490 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
492 if (!condition
|| !is_usage_condition(condition
) || !threshold_bytes
) {
493 status
= LTTNG_CONDITION_STATUS_INVALID
;
497 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
499 if (!usage
->threshold_bytes
.set
) {
500 status
= LTTNG_CONDITION_STATUS_UNSET
;
503 *threshold_bytes
= usage
->threshold_bytes
.value
;
508 enum lttng_condition_status
509 lttng_condition_buffer_usage_set_threshold(
510 struct lttng_condition
*condition
, uint64_t threshold_bytes
)
512 struct lttng_condition_buffer_usage
*usage
;
513 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
515 if (!condition
|| !is_usage_condition(condition
)) {
516 status
= LTTNG_CONDITION_STATUS_INVALID
;
520 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
522 usage
->threshold_ratio
.set
= false;
523 usage
->threshold_bytes
.set
= true;
524 usage
->threshold_bytes
.value
= threshold_bytes
;
529 enum lttng_condition_status
530 lttng_condition_buffer_usage_get_session_name(
531 struct lttng_condition
*condition
, const char **session_name
)
533 struct lttng_condition_buffer_usage
*usage
;
534 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
536 if (!condition
|| !is_usage_condition(condition
) || !session_name
) {
537 status
= LTTNG_CONDITION_STATUS_INVALID
;
541 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
543 if (!usage
->session_name
) {
544 status
= LTTNG_CONDITION_STATUS_UNSET
;
547 *session_name
= usage
->session_name
;
552 extern enum lttng_condition_status
553 lttng_condition_buffer_usage_set_session_name(
554 struct lttng_condition
*condition
, const char *session_name
)
556 char *session_name_copy
;
557 struct lttng_condition_buffer_usage
*usage
;
558 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
560 if (!condition
|| !is_usage_condition(condition
) || !session_name
||
561 strlen(session_name
) == 0) {
562 status
= LTTNG_CONDITION_STATUS_INVALID
;
566 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
568 session_name_copy
= strdup(session_name
);
569 if (!session_name_copy
) {
570 status
= LTTNG_CONDITION_STATUS_ERROR
;
574 if (usage
->session_name
) {
575 free(usage
->session_name
);
577 usage
->session_name
= session_name_copy
;
582 enum lttng_condition_status
583 lttng_condition_buffer_usage_get_channel_name(
584 struct lttng_condition
*condition
, const char **channel_name
)
586 struct lttng_condition_buffer_usage
*usage
;
587 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
589 if (!condition
|| !is_usage_condition(condition
) || !channel_name
) {
590 status
= LTTNG_CONDITION_STATUS_INVALID
;
594 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
596 if (!usage
->channel_name
) {
597 status
= LTTNG_CONDITION_STATUS_UNSET
;
600 *channel_name
= usage
->channel_name
;
605 extern enum lttng_condition_status
606 lttng_condition_buffer_usage_set_channel_name(
607 struct lttng_condition
*condition
, const char *channel_name
)
609 char *channel_name_copy
;
610 struct lttng_condition_buffer_usage
*usage
;
611 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
613 if (!condition
|| !is_usage_condition(condition
) || !channel_name
||
614 strlen(channel_name
) == 0) {
615 status
= LTTNG_CONDITION_STATUS_INVALID
;
619 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
621 channel_name_copy
= strdup(channel_name
);
622 if (!channel_name_copy
) {
623 status
= LTTNG_CONDITION_STATUS_ERROR
;
627 if (usage
->channel_name
) {
628 free(usage
->channel_name
);
630 usage
->channel_name
= channel_name_copy
;
635 extern enum lttng_condition_status
636 lttng_condition_buffer_usage_get_domain_type(
637 struct lttng_condition
*condition
, enum lttng_domain_type
*type
)
639 struct lttng_condition_buffer_usage
*usage
;
640 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
642 if (!condition
|| !is_usage_condition(condition
) || !type
) {
643 status
= LTTNG_CONDITION_STATUS_INVALID
;
647 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
649 if (!usage
->domain
.set
) {
650 status
= LTTNG_CONDITION_STATUS_UNSET
;
653 *type
= usage
->domain
.type
;
658 extern enum lttng_condition_status
659 lttng_condition_buffer_usage_set_domain_type(
660 struct lttng_condition
*condition
, enum lttng_domain_type type
)
662 struct lttng_condition_buffer_usage
*usage
;
663 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
665 if (!condition
|| !is_usage_condition(condition
) ||
666 type
== LTTNG_DOMAIN_NONE
) {
667 status
= LTTNG_CONDITION_STATUS_INVALID
;
671 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
673 usage
->domain
.set
= true;
674 usage
->domain
.type
= type
;
680 ssize_t
lttng_evaluation_buffer_usage_serialize(
681 struct lttng_evaluation
*evaluation
, char *buf
)
684 struct lttng_evaluation_buffer_usage
*usage
;
686 usage
= container_of(evaluation
, struct lttng_evaluation_buffer_usage
,
689 struct lttng_evaluation_buffer_usage_comm comm
= {
690 .buffer_use
= usage
->buffer_use
,
691 .buffer_capacity
= usage
->buffer_capacity
,
694 memcpy(buf
, &comm
, sizeof(comm
));
697 ret
= sizeof(struct lttng_evaluation_buffer_usage_comm
);
702 void lttng_evaluation_buffer_usage_destroy(
703 struct lttng_evaluation
*evaluation
)
705 struct lttng_evaluation_buffer_usage
*usage
;
707 usage
= container_of(evaluation
, struct lttng_evaluation_buffer_usage
,
713 struct lttng_evaluation
*lttng_evaluation_buffer_usage_create(
714 enum lttng_condition_type type
, uint64_t use
, uint64_t capacity
)
716 struct lttng_evaluation_buffer_usage
*usage
;
718 usage
= zmalloc(sizeof(struct lttng_evaluation_buffer_usage
));
723 usage
->parent
.type
= type
;
724 usage
->buffer_use
= use
;
725 usage
->buffer_capacity
= capacity
;
726 usage
->parent
.serialize
= lttng_evaluation_buffer_usage_serialize
;
727 usage
->parent
.destroy
= lttng_evaluation_buffer_usage_destroy
;
729 return &usage
->parent
;
733 * Get the sampled buffer usage which caused the associated condition to
734 * evaluate to "true".
736 enum lttng_evaluation_status
737 lttng_evaluation_buffer_usage_get_usage_ratio(
738 struct lttng_evaluation
*evaluation
, double *usage_ratio
)
740 struct lttng_evaluation_buffer_usage
*usage
;
741 enum lttng_evaluation_status status
= LTTNG_EVALUATION_STATUS_OK
;
743 if (!evaluation
|| !is_usage_evaluation(evaluation
) || !usage_ratio
) {
744 status
= LTTNG_EVALUATION_STATUS_INVALID
;
748 usage
= container_of(evaluation
, struct lttng_evaluation_buffer_usage
,
750 *usage_ratio
= (double) usage
->buffer_use
/
751 (double) usage
->buffer_capacity
;
756 enum lttng_evaluation_status
757 lttng_evaluation_buffer_usage_get_usage(struct lttng_evaluation
*evaluation
,
758 uint64_t *usage_bytes
)
760 struct lttng_evaluation_buffer_usage
*usage
;
761 enum lttng_evaluation_status status
= LTTNG_EVALUATION_STATUS_OK
;
763 if (!evaluation
|| !is_usage_evaluation(evaluation
) || !usage_bytes
) {
764 status
= LTTNG_EVALUATION_STATUS_INVALID
;
768 usage
= container_of(evaluation
, struct lttng_evaluation_buffer_usage
,
770 *usage_bytes
= usage
->buffer_use
;