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>
25 bool is_usage_condition(struct lttng_condition
*condition
)
27 enum lttng_condition_type type
= lttng_condition_get_type(condition
);
29 return type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
||
30 type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
;
34 bool is_usage_evaluation(struct lttng_evaluation
*evaluation
)
36 enum lttng_condition_type type
= lttng_evaluation_get_type(evaluation
);
38 return type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
||
39 type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
;
43 void lttng_condition_buffer_usage_destroy(struct lttng_condition
*condition
)
45 struct lttng_condition_buffer_usage
*usage
;
47 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
50 free(usage
->session_name
);
51 free(usage
->channel_name
);
56 bool lttng_condition_buffer_usage_validate(struct lttng_condition
*condition
)
59 struct lttng_condition_buffer_usage
*usage
;
65 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
67 if (!usage
->session_name
) {
68 ERR("Invalid buffer condition: a target session name must be set.");
71 if (!usage
->channel_name
) {
72 ERR("Invalid buffer condition: a target channel name must be set.");
75 if (!usage
->threshold_percent
.set
&& !usage
->threshold_bytes
.set
) {
76 ERR("Invalid buffer condition: a threshold must be set.");
86 ssize_t
lttng_condition_buffer_usage_serialize(struct lttng_condition
*condition
,
89 struct lttng_condition_buffer_usage
*usage
;
91 size_t session_name_len
, channel_name_len
;
93 if (!condition
|| !is_usage_condition(condition
)) {
98 DBG("Serializing buffer usage condition");
99 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
101 size
= sizeof(struct lttng_condition_buffer_usage_comm
);
102 session_name_len
= strlen(usage
->session_name
) + 1;
103 channel_name_len
= strlen(usage
->channel_name
) + 1;
104 size
+= session_name_len
+ channel_name_len
;
106 struct lttng_condition_buffer_usage_comm usage_comm
= {
107 .threshold_set_in_bytes
= usage
->threshold_bytes
.set
? 1 : 0,
108 .session_name_len
= session_name_len
,
109 .channel_name_len
= channel_name_len
,
110 .domain_type
= (int8_t) usage
->domain
.type
,
113 if (usage
->threshold_bytes
.set
) {
114 usage_comm
.threshold
.bytes
=
115 usage
->threshold_bytes
.value
;
117 usage_comm
.threshold
.percent
=
118 usage
->threshold_percent
.value
;
121 memcpy(buf
, &usage_comm
, sizeof(usage_comm
));
122 buf
+= sizeof(usage_comm
);
123 memcpy(buf
, usage
->session_name
, session_name_len
);
124 buf
+= session_name_len
;
125 memcpy(buf
, usage
->channel_name
, channel_name_len
);
126 buf
+= channel_name_len
;
133 struct lttng_condition
*lttng_condition_buffer_usage_create(
134 enum lttng_condition_type type
)
136 struct lttng_condition_buffer_usage
*condition
;
138 condition
= zmalloc(sizeof(struct lttng_condition_buffer_usage
));
143 lttng_condition_init(&condition
->parent
, type
);
144 condition
->parent
.validate
= lttng_condition_buffer_usage_validate
;
145 condition
->parent
.serialize
= lttng_condition_buffer_usage_serialize
;
146 condition
->parent
.destroy
= lttng_condition_buffer_usage_destroy
;
148 return &condition
->parent
;
151 struct lttng_condition
*lttng_condition_buffer_usage_low_create(void)
153 return lttng_condition_buffer_usage_create(
154 LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
);
157 struct lttng_condition
*lttng_condition_buffer_usage_high_create(void)
159 return lttng_condition_buffer_usage_create(
160 LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
);
164 ssize_t
init_from_buffer(struct lttng_condition
*condition
, const char *buf
)
166 ssize_t ret
, condition_size
;
167 enum lttng_condition_status status
;
168 enum lttng_domain_type domain_type
;
169 struct lttng_condition_buffer_usage_comm
*condition_comm
=
170 (struct lttng_condition_buffer_usage_comm
*) buf
;
171 const char *session_name
, *channel_name
;
173 if (condition_comm
->threshold_set_in_bytes
) {
174 status
= lttng_condition_buffer_usage_set_threshold(condition
,
175 condition_comm
->threshold
.bytes
);
177 status
= lttng_condition_buffer_usage_set_threshold_percentage(
178 condition
, condition_comm
->threshold
.percent
);
180 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
181 ERR("Failed to initialize buffer usage condition threshold");
186 if (condition_comm
->domain_type
<= LTTNG_DOMAIN_NONE
||
187 condition_comm
->domain_type
> LTTNG_DOMAIN_PYTHON
) {
188 /* Invalid domain value. */
189 ERR("Invalid domain type value (%i) found in condition buffer",
190 (int) condition_comm
->domain_type
);
195 domain_type
= (enum lttng_domain_type
) condition_comm
->domain_type
;
196 status
= lttng_condition_buffer_usage_set_domain_type(condition
,
198 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
199 ERR("Failed to set buffer usage condition domain");
204 session_name
= buf
+ sizeof(struct lttng_condition_buffer_usage_comm
);
205 channel_name
= session_name
+ condition_comm
->session_name_len
;
207 status
= lttng_condition_buffer_usage_set_session_name(condition
,
209 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
210 ERR("Failed to set buffer usage session name");
215 status
= lttng_condition_buffer_usage_set_channel_name(condition
,
217 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
218 ERR("Failed to set buffer usage channel name");
223 if (!lttng_condition_validate(condition
)) {
228 condition_size
= sizeof(*condition_comm
);
229 condition_size
+= (ssize_t
) condition_comm
->session_name_len
;
230 condition_size
+= (ssize_t
) condition_comm
->channel_name_len
;
231 ret
= condition_size
;
237 ssize_t
lttng_condition_buffer_usage_low_create_from_buffer(const char *buf
,
238 struct lttng_condition
**_condition
)
241 struct lttng_condition
*condition
=
242 lttng_condition_buffer_usage_low_create();
244 if (!_condition
|| !condition
) {
249 DBG("Initializing low buffer usage condition from buffer");
250 ret
= init_from_buffer(condition
, buf
);
255 *_condition
= condition
;
258 lttng_condition_destroy(condition
);
263 ssize_t
lttng_condition_buffer_usage_high_create_from_buffer(const char *buf
,
264 struct lttng_condition
**_condition
)
267 struct lttng_condition
*condition
=
268 lttng_condition_buffer_usage_high_create();
270 if (!_condition
|| !condition
) {
275 DBG("Initializing high buffer usage condition from buffer");
276 ret
= init_from_buffer(condition
, buf
);
281 *_condition
= condition
;
284 lttng_condition_destroy(condition
);
288 enum lttng_condition_status
289 lttng_condition_buffer_usage_get_threshold_percentage(
290 struct lttng_condition
*condition
, double *threshold_percent
)
292 struct lttng_condition_buffer_usage
*usage
;
293 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
295 if (!condition
|| !is_usage_condition(condition
) ||
296 !threshold_percent
) {
297 status
= LTTNG_CONDITION_STATUS_INVALID
;
301 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
303 if (!usage
->threshold_percent
.set
) {
304 status
= LTTNG_CONDITION_STATUS_UNSET
;
307 *threshold_percent
= usage
->threshold_percent
.value
;
312 /* threshold_percent expressed as [0.0, 1.0]. */
313 enum lttng_condition_status
314 lttng_condition_buffer_usage_set_threshold_percentage(
315 struct lttng_condition
*condition
, double threshold_percent
)
317 struct lttng_condition_buffer_usage
*usage
;
318 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
320 if (!condition
|| !is_usage_condition(condition
) ||
321 threshold_percent
< 0.0 || threshold_percent
> 1.0) {
322 status
= LTTNG_CONDITION_STATUS_INVALID
;
326 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
328 usage
->threshold_percent
.set
= true;
329 usage
->threshold_bytes
.set
= false;
330 usage
->threshold_percent
.value
= threshold_percent
;
335 enum lttng_condition_status
336 lttng_condition_buffer_usage_get_threshold(
337 struct lttng_condition
*condition
, uint64_t *threshold_bytes
)
339 struct lttng_condition_buffer_usage
*usage
;
340 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
342 if (!condition
|| !is_usage_condition(condition
) || !threshold_bytes
) {
343 status
= LTTNG_CONDITION_STATUS_INVALID
;
347 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
349 if (!usage
->threshold_bytes
.set
) {
350 status
= LTTNG_CONDITION_STATUS_UNSET
;
353 *threshold_bytes
= usage
->threshold_bytes
.value
;
358 enum lttng_condition_status
359 lttng_condition_buffer_usage_set_threshold(
360 struct lttng_condition
*condition
, uint64_t threshold_bytes
)
362 struct lttng_condition_buffer_usage
*usage
;
363 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
365 if (!condition
|| !is_usage_condition(condition
)) {
366 status
= LTTNG_CONDITION_STATUS_INVALID
;
370 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
372 usage
->threshold_percent
.set
= false;
373 usage
->threshold_bytes
.set
= true;
374 usage
->threshold_bytes
.value
= threshold_bytes
;
379 enum lttng_condition_status
380 lttng_condition_buffer_usage_get_session_name(
381 struct lttng_condition
*condition
, const char **session_name
)
383 struct lttng_condition_buffer_usage
*usage
;
384 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
386 if (!condition
|| !is_usage_condition(condition
) || !session_name
) {
387 status
= LTTNG_CONDITION_STATUS_INVALID
;
391 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
393 if (!usage
->session_name
) {
394 status
= LTTNG_CONDITION_STATUS_UNSET
;
397 *session_name
= usage
->session_name
;
402 extern enum lttng_condition_status
403 lttng_condition_buffer_usage_set_session_name(
404 struct lttng_condition
*condition
, const char *session_name
)
406 struct lttng_condition_buffer_usage
*usage
;
407 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
409 if (!condition
|| !is_usage_condition(condition
) || !session_name
||
410 strlen(session_name
) == 0) {
411 status
= LTTNG_CONDITION_STATUS_INVALID
;
415 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
417 usage
->session_name
= strdup(session_name
);
418 if (!usage
->session_name
) {
419 status
= LTTNG_CONDITION_STATUS_ERROR
;
426 enum lttng_condition_status
427 lttng_condition_buffer_usage_get_channel_name(
428 struct lttng_condition
*condition
, const char **channel_name
)
430 struct lttng_condition_buffer_usage
*usage
;
431 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
433 if (!condition
|| !is_usage_condition(condition
) || !channel_name
) {
434 status
= LTTNG_CONDITION_STATUS_INVALID
;
438 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
440 if (!usage
->channel_name
) {
441 status
= LTTNG_CONDITION_STATUS_UNSET
;
444 *channel_name
= usage
->channel_name
;
449 extern enum lttng_condition_status
450 lttng_condition_buffer_usage_set_channel_name(
451 struct lttng_condition
*condition
, const char *channel_name
)
453 struct lttng_condition_buffer_usage
*usage
;
454 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
456 if (!condition
|| !is_usage_condition(condition
) || !channel_name
||
457 strlen(channel_name
) == 0) {
458 status
= LTTNG_CONDITION_STATUS_INVALID
;
462 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
464 usage
->channel_name
= strdup(channel_name
);
465 if (!usage
->channel_name
) {
466 status
= LTTNG_CONDITION_STATUS_ERROR
;
473 extern enum lttng_condition_status
474 lttng_condition_buffer_usage_get_domain_type(
475 struct lttng_condition
*condition
, enum lttng_domain_type
*type
)
477 struct lttng_condition_buffer_usage
*usage
;
478 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
480 if (!condition
|| !is_usage_condition(condition
) || !type
) {
481 status
= LTTNG_CONDITION_STATUS_INVALID
;
485 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
487 if (!usage
->domain
.set
) {
488 status
= LTTNG_CONDITION_STATUS_UNSET
;
491 *type
= usage
->domain
.type
;
496 extern enum lttng_condition_status
497 lttng_condition_buffer_usage_set_domain_type(
498 struct lttng_condition
*condition
, enum lttng_domain_type type
)
500 struct lttng_condition_buffer_usage
*usage
;
501 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
503 if (!condition
|| !is_usage_condition(condition
) ||
504 type
== LTTNG_DOMAIN_NONE
) {
505 status
= LTTNG_CONDITION_STATUS_INVALID
;
509 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
511 usage
->domain
.set
= true;
512 usage
->domain
.type
= type
;
518 void lttng_evaluation_buffer_usage_destroy(
519 struct lttng_evaluation
*evaluation
)
521 struct lttng_evaluation_buffer_usage
*usage
;
523 usage
= container_of(evaluation
, struct lttng_evaluation_buffer_usage
,
529 struct lttng_evaluation
*lttng_evaluation_buffer_usage_create(uint64_t use
,
532 struct lttng_evaluation_buffer_usage
*usage
;
534 usage
= zmalloc(sizeof(struct lttng_evaluation_buffer_usage
));
539 usage
->buffer_use
= use
;
540 usage
->buffer_capacity
= capacity
;
541 usage
->parent
.destroy
= lttng_evaluation_buffer_usage_destroy
;
543 return &usage
->parent
;
547 * Get the sampled buffer usage which caused the associated condition to
548 * evaluate to "true".
550 enum lttng_evaluation_status
551 lttng_evaluation_buffer_usage_get_usage_percentage(
552 struct lttng_evaluation
*evaluation
, double *usage_percent
)
554 struct lttng_evaluation_buffer_usage
*usage
;
555 enum lttng_evaluation_status status
= LTTNG_EVALUATION_STATUS_OK
;
557 if (!evaluation
|| !is_usage_evaluation(evaluation
) || !usage_percent
) {
558 status
= LTTNG_EVALUATION_STATUS_INVALID
;
562 usage
= container_of(evaluation
, struct lttng_evaluation_buffer_usage
,
564 *usage_percent
= (double) usage
->buffer_use
/
565 (double) usage
->buffer_capacity
;
570 enum lttng_evaluation_status
571 lttng_evaluation_buffer_usage_get_usage(struct lttng_evaluation
*evaluation
,
572 uint64_t *usage_bytes
)
574 struct lttng_evaluation_buffer_usage
*usage
;
575 enum lttng_evaluation_status status
= LTTNG_EVALUATION_STATUS_OK
;
577 if (!evaluation
|| !is_usage_evaluation(evaluation
) || !usage_bytes
) {
578 status
= LTTNG_EVALUATION_STATUS_INVALID
;
582 usage
= container_of(evaluation
, struct lttng_evaluation_buffer_usage
,
584 *usage_bytes
= usage
->buffer_use
;