5f68608001a43d3559d527541c974e8edc0eef73
[lttng-tools.git] / src / common / buffer-usage.c
1 /*
2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include <lttng/condition/condition-internal.h>
9 #include <lttng/condition/buffer-usage-internal.h>
10 #include <common/macros.h>
11 #include <common/error.h>
12 #include <assert.h>
13 #include <math.h>
14 #include <float.h>
15 #include <time.h>
16
17 #define IS_USAGE_CONDITION(condition) ( \
18 lttng_condition_get_type(condition) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW || \
19 lttng_condition_get_type(condition) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH \
20 )
21
22 static
23 double fixed_to_double(uint32_t val)
24 {
25 return (double) val / (double) UINT32_MAX;
26 }
27
28 static
29 uint64_t double_to_fixed(double val)
30 {
31 return (val * (double) UINT32_MAX);
32 }
33
34 static
35 bool is_usage_evaluation(const struct lttng_evaluation *evaluation)
36 {
37 enum lttng_condition_type type = lttng_evaluation_get_type(evaluation);
38
39 return type == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW ||
40 type == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH;
41 }
42
43 static
44 void lttng_condition_buffer_usage_destroy(struct lttng_condition *condition)
45 {
46 struct lttng_condition_buffer_usage *usage;
47
48 usage = container_of(condition, struct lttng_condition_buffer_usage,
49 parent);
50
51 free(usage->session_name);
52 free(usage->channel_name);
53 free(usage);
54 }
55
56 static
57 bool lttng_condition_buffer_usage_validate(
58 const struct lttng_condition *condition)
59 {
60 bool valid = false;
61 struct lttng_condition_buffer_usage *usage;
62
63 if (!condition) {
64 goto end;
65 }
66
67 usage = container_of(condition, struct lttng_condition_buffer_usage,
68 parent);
69 if (!usage->session_name) {
70 ERR("Invalid buffer condition: a target session name must be set.");
71 goto end;
72 }
73 if (!usage->channel_name) {
74 ERR("Invalid buffer condition: a target channel name must be set.");
75 goto end;
76 }
77 if (!usage->threshold_ratio.set && !usage->threshold_bytes.set) {
78 ERR("Invalid buffer condition: a threshold must be set.");
79 goto end;
80 }
81 if (!usage->domain.set) {
82 ERR("Invalid buffer usage condition: a domain must be set.");
83 goto end;
84 }
85
86 valid = true;
87 end:
88 return valid;
89 }
90
91 static
92 int lttng_condition_buffer_usage_serialize(
93 const struct lttng_condition *condition,
94 struct lttng_payload *payload)
95 {
96 int ret;
97 struct lttng_condition_buffer_usage *usage;
98 size_t session_name_len, channel_name_len;
99 struct lttng_condition_buffer_usage_comm usage_comm;
100
101 if (!condition || !IS_USAGE_CONDITION(condition)) {
102 ret = -1;
103 goto end;
104 }
105
106 DBG("Serializing buffer usage condition");
107 usage = container_of(condition, struct lttng_condition_buffer_usage,
108 parent);
109
110 session_name_len = strlen(usage->session_name) + 1;
111 channel_name_len = strlen(usage->channel_name) + 1;
112 if (session_name_len > LTTNG_NAME_MAX ||
113 channel_name_len > LTTNG_NAME_MAX) {
114 ret = -1;
115 goto end;
116 }
117
118 usage_comm.threshold_set_in_bytes = !!usage->threshold_bytes.set;
119 usage_comm.session_name_len = session_name_len;
120 usage_comm.channel_name_len = channel_name_len;
121 usage_comm.domain_type = (int8_t) usage->domain.type;
122
123 if (usage->threshold_bytes.set) {
124 usage_comm.threshold = usage->threshold_bytes.value;
125 } else {
126 uint64_t val = double_to_fixed(
127 usage->threshold_ratio.value);
128
129 if (val > UINT32_MAX) {
130 /* overflow. */
131 ret = -1;
132 goto end;
133 }
134 usage_comm.threshold = val;
135 }
136
137 ret = lttng_dynamic_buffer_append(&payload->buffer, &usage_comm,
138 sizeof(usage_comm));
139 if (ret) {
140 goto end;
141 }
142
143 ret = lttng_dynamic_buffer_append(&payload->buffer, usage->session_name,
144 session_name_len);
145 if (ret) {
146 goto end;
147 }
148
149 ret = lttng_dynamic_buffer_append(&payload->buffer, usage->channel_name,
150 channel_name_len);
151 if (ret) {
152 goto end;
153 }
154 end:
155 return ret;
156 }
157
158 static
159 bool lttng_condition_buffer_usage_is_equal(const struct lttng_condition *_a,
160 const struct lttng_condition *_b)
161 {
162 bool is_equal = false;
163 struct lttng_condition_buffer_usage *a, *b;
164
165 a = container_of(_a, struct lttng_condition_buffer_usage, parent);
166 b = container_of(_b, struct lttng_condition_buffer_usage, parent);
167
168 if ((a->threshold_ratio.set && !b->threshold_ratio.set) ||
169 (a->threshold_bytes.set && !b->threshold_bytes.set)) {
170 goto end;
171 }
172
173 if (a->threshold_ratio.set && b->threshold_ratio.set) {
174 double a_value, b_value, diff;
175
176 a_value = a->threshold_ratio.value;
177 b_value = b->threshold_ratio.value;
178 diff = fabs(a_value - b_value);
179
180 if (diff > DBL_EPSILON) {
181 goto end;
182 }
183 } else if (a->threshold_bytes.set && b->threshold_bytes.set) {
184 uint64_t a_value, b_value;
185
186 a_value = a->threshold_bytes.value;
187 b_value = b->threshold_bytes.value;
188 if (a_value != b_value) {
189 goto end;
190 }
191 }
192
193 /* Condition is not valid if this is not true. */
194 assert(a->session_name);
195 assert(b->session_name);
196 if (strcmp(a->session_name, b->session_name)) {
197 goto end;
198 }
199
200 assert(a->channel_name);
201 assert(b->channel_name);
202 if (strcmp(a->channel_name, b->channel_name)) {
203 goto end;
204 }
205
206 assert(a->domain.set);
207 assert(b->domain.set);
208 if (a->domain.type != b->domain.type) {
209 goto end;
210 }
211 is_equal = true;
212 end:
213 return is_equal;
214 }
215
216 static
217 struct lttng_condition *lttng_condition_buffer_usage_create(
218 enum lttng_condition_type type)
219 {
220 struct lttng_condition_buffer_usage *condition;
221
222 condition = zmalloc(sizeof(struct lttng_condition_buffer_usage));
223 if (!condition) {
224 return NULL;
225 }
226
227 lttng_condition_init(&condition->parent, type);
228 condition->parent.validate = lttng_condition_buffer_usage_validate;
229 condition->parent.serialize = lttng_condition_buffer_usage_serialize;
230 condition->parent.equal = lttng_condition_buffer_usage_is_equal;
231 condition->parent.destroy = lttng_condition_buffer_usage_destroy;
232 return &condition->parent;
233 }
234
235 struct lttng_condition *lttng_condition_buffer_usage_low_create(void)
236 {
237 return lttng_condition_buffer_usage_create(
238 LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW);
239 }
240
241 struct lttng_condition *lttng_condition_buffer_usage_high_create(void)
242 {
243 return lttng_condition_buffer_usage_create(
244 LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH);
245 }
246
247 static
248 ssize_t init_condition_from_payload(struct lttng_condition *condition,
249 struct lttng_payload_view *src_view)
250 {
251 ssize_t ret, condition_size;
252 enum lttng_condition_status status;
253 enum lttng_domain_type domain_type;
254 const char *session_name, *channel_name;
255 struct lttng_buffer_view names_view;
256 const struct lttng_condition_buffer_usage_comm *condition_comm;
257 const struct lttng_payload_view condition_comm_view =
258 lttng_payload_view_from_view(
259 src_view, 0, sizeof(*condition_comm));
260
261 if (!lttng_payload_view_is_valid(&condition_comm_view)) {
262 ERR("Failed to initialize from malformed condition buffer: buffer too short to contain header");
263 ret = -1;
264 goto end;
265 }
266
267 condition_comm = (typeof(condition_comm)) condition_comm_view.buffer.data;
268 names_view = lttng_buffer_view_from_view(&src_view->buffer,
269 sizeof(*condition_comm), -1);
270
271 if (condition_comm->session_name_len > LTTNG_NAME_MAX ||
272 condition_comm->channel_name_len > LTTNG_NAME_MAX) {
273 ERR("Failed to initialize from malformed condition buffer: name exceeds LTTNG_MAX_NAME");
274 ret = -1;
275 goto end;
276 }
277
278 if (names_view.size <
279 (condition_comm->session_name_len +
280 condition_comm->channel_name_len)) {
281 ERR("Failed to initialize from malformed condition buffer: buffer too short to contain element names");
282 ret = -1;
283 goto end;
284 }
285
286 if (condition_comm->threshold_set_in_bytes) {
287 status = lttng_condition_buffer_usage_set_threshold(condition,
288 condition_comm->threshold);
289 } else {
290 status = lttng_condition_buffer_usage_set_threshold_ratio(
291 condition,
292 fixed_to_double(condition_comm->threshold));
293 }
294
295 if (status != LTTNG_CONDITION_STATUS_OK) {
296 ERR("Failed to initialize buffer usage condition threshold");
297 ret = -1;
298 goto end;
299 }
300
301 if (condition_comm->domain_type <= LTTNG_DOMAIN_NONE ||
302 condition_comm->domain_type > LTTNG_DOMAIN_PYTHON) {
303 /* Invalid domain value. */
304 ERR("Invalid domain type value (%i) found in condition buffer",
305 (int) condition_comm->domain_type);
306 ret = -1;
307 goto end;
308 }
309
310 domain_type = (enum lttng_domain_type) condition_comm->domain_type;
311 status = lttng_condition_buffer_usage_set_domain_type(condition,
312 domain_type);
313 if (status != LTTNG_CONDITION_STATUS_OK) {
314 ERR("Failed to set buffer usage condition domain");
315 ret = -1;
316 goto end;
317 }
318
319 session_name = names_view.data;
320 if (*(session_name + condition_comm->session_name_len - 1) != '\0') {
321 ERR("Malformed session name encountered in condition buffer");
322 ret = -1;
323 goto end;
324 }
325
326 channel_name = session_name + condition_comm->session_name_len;
327 if (*(channel_name + condition_comm->channel_name_len - 1) != '\0') {
328 ERR("Malformed channel name encountered in condition buffer");
329 ret = -1;
330 goto end;
331 }
332
333 status = lttng_condition_buffer_usage_set_session_name(condition,
334 session_name);
335 if (status != LTTNG_CONDITION_STATUS_OK) {
336 ERR("Failed to set buffer usage session name");
337 ret = -1;
338 goto end;
339 }
340
341 status = lttng_condition_buffer_usage_set_channel_name(condition,
342 channel_name);
343 if (status != LTTNG_CONDITION_STATUS_OK) {
344 ERR("Failed to set buffer usage channel name");
345 ret = -1;
346 goto end;
347 }
348
349 if (!lttng_condition_validate(condition)) {
350 ret = -1;
351 goto end;
352 }
353
354 condition_size = sizeof(*condition_comm) +
355 (ssize_t) condition_comm->session_name_len +
356 (ssize_t) condition_comm->channel_name_len;
357 ret = condition_size;
358 end:
359 return ret;
360 }
361
362 LTTNG_HIDDEN
363 ssize_t lttng_condition_buffer_usage_low_create_from_payload(
364 struct lttng_payload_view *view,
365 struct lttng_condition **_condition)
366 {
367 ssize_t ret;
368 struct lttng_condition *condition =
369 lttng_condition_buffer_usage_low_create();
370
371 if (!_condition || !condition) {
372 ret = -1;
373 goto error;
374 }
375
376 ret = init_condition_from_payload(condition, view);
377 if (ret < 0) {
378 goto error;
379 }
380
381 *_condition = condition;
382 return ret;
383 error:
384 lttng_condition_destroy(condition);
385 return ret;
386 }
387
388 LTTNG_HIDDEN
389 ssize_t lttng_condition_buffer_usage_high_create_from_payload(
390 struct lttng_payload_view *view,
391 struct lttng_condition **_condition)
392 {
393 ssize_t ret;
394 struct lttng_condition *condition =
395 lttng_condition_buffer_usage_high_create();
396
397 if (!_condition || !condition) {
398 ret = -1;
399 goto error;
400 }
401
402 ret = init_condition_from_payload(condition, view);
403 if (ret < 0) {
404 goto error;
405 }
406
407 *_condition = condition;
408 return ret;
409 error:
410 lttng_condition_destroy(condition);
411 return ret;
412 }
413
414 static
415 struct lttng_evaluation *create_evaluation_from_payload(
416 enum lttng_condition_type type,
417 struct lttng_payload_view *view)
418 {
419 const struct lttng_evaluation_buffer_usage_comm *comm =
420 (typeof(comm)) view->buffer.data;
421 struct lttng_evaluation *evaluation = NULL;
422
423 if (view->buffer.size < sizeof(*comm)) {
424 goto end;
425 }
426
427 evaluation = lttng_evaluation_buffer_usage_create(type,
428 comm->buffer_use, comm->buffer_capacity);
429 end:
430 return evaluation;
431 }
432
433 LTTNG_HIDDEN
434 ssize_t lttng_evaluation_buffer_usage_low_create_from_payload(
435 struct lttng_payload_view *view,
436 struct lttng_evaluation **_evaluation)
437 {
438 ssize_t ret;
439 struct lttng_evaluation *evaluation = NULL;
440
441 if (!_evaluation) {
442 ret = -1;
443 goto error;
444 }
445
446 evaluation = create_evaluation_from_payload(
447 LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW, view);
448 if (!evaluation) {
449 ret = -1;
450 goto error;
451 }
452
453 *_evaluation = evaluation;
454 ret = sizeof(struct lttng_evaluation_buffer_usage_comm);
455 return ret;
456 error:
457 lttng_evaluation_destroy(evaluation);
458 return ret;
459 }
460
461 LTTNG_HIDDEN
462 ssize_t lttng_evaluation_buffer_usage_high_create_from_payload(
463 struct lttng_payload_view *view,
464 struct lttng_evaluation **_evaluation)
465 {
466 ssize_t ret;
467 struct lttng_evaluation *evaluation = NULL;
468
469 if (!_evaluation) {
470 ret = -1;
471 goto error;
472 }
473
474 evaluation = create_evaluation_from_payload(
475 LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH, view);
476 if (!evaluation) {
477 ret = -1;
478 goto error;
479 }
480
481 *_evaluation = evaluation;
482 ret = sizeof(struct lttng_evaluation_buffer_usage_comm);
483 return ret;
484 error:
485 lttng_evaluation_destroy(evaluation);
486 return ret;
487 }
488
489 enum lttng_condition_status
490 lttng_condition_buffer_usage_get_threshold_ratio(
491 const struct lttng_condition *condition,
492 double *threshold_ratio)
493 {
494 struct lttng_condition_buffer_usage *usage;
495 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
496
497 if (!condition || !IS_USAGE_CONDITION(condition) ||
498 !threshold_ratio) {
499 status = LTTNG_CONDITION_STATUS_INVALID;
500 goto end;
501 }
502
503 usage = container_of(condition, struct lttng_condition_buffer_usage,
504 parent);
505 if (!usage->threshold_ratio.set) {
506 status = LTTNG_CONDITION_STATUS_UNSET;
507 goto end;
508 }
509 *threshold_ratio = usage->threshold_ratio.value;
510 end:
511 return status;
512 }
513
514 /* threshold_ratio expressed as [0.0, 1.0]. */
515 enum lttng_condition_status
516 lttng_condition_buffer_usage_set_threshold_ratio(
517 struct lttng_condition *condition, double threshold_ratio)
518 {
519 struct lttng_condition_buffer_usage *usage;
520 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
521
522 if (!condition || !IS_USAGE_CONDITION(condition) ||
523 threshold_ratio < 0.0 ||
524 threshold_ratio > 1.0) {
525 status = LTTNG_CONDITION_STATUS_INVALID;
526 goto end;
527 }
528
529 usage = container_of(condition, struct lttng_condition_buffer_usage,
530 parent);
531 usage->threshold_ratio.set = true;
532 usage->threshold_bytes.set = false;
533 usage->threshold_ratio.value = threshold_ratio;
534 end:
535 return status;
536 }
537
538 enum lttng_condition_status
539 lttng_condition_buffer_usage_get_threshold(
540 const struct lttng_condition *condition,
541 uint64_t *threshold_bytes)
542 {
543 struct lttng_condition_buffer_usage *usage;
544 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
545
546 if (!condition || !IS_USAGE_CONDITION(condition) || !threshold_bytes) {
547 status = LTTNG_CONDITION_STATUS_INVALID;
548 goto end;
549 }
550
551 usage = container_of(condition, struct lttng_condition_buffer_usage,
552 parent);
553 if (!usage->threshold_bytes.set) {
554 status = LTTNG_CONDITION_STATUS_UNSET;
555 goto end;
556 }
557 *threshold_bytes = usage->threshold_bytes.value;
558 end:
559 return status;
560 }
561
562 enum lttng_condition_status
563 lttng_condition_buffer_usage_set_threshold(
564 struct lttng_condition *condition, uint64_t threshold_bytes)
565 {
566 struct lttng_condition_buffer_usage *usage;
567 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
568
569 if (!condition || !IS_USAGE_CONDITION(condition)) {
570 status = LTTNG_CONDITION_STATUS_INVALID;
571 goto end;
572 }
573
574 usage = container_of(condition, struct lttng_condition_buffer_usage,
575 parent);
576 usage->threshold_ratio.set = false;
577 usage->threshold_bytes.set = true;
578 usage->threshold_bytes.value = threshold_bytes;
579 end:
580 return status;
581 }
582
583 enum lttng_condition_status
584 lttng_condition_buffer_usage_get_session_name(
585 const struct lttng_condition *condition,
586 const char **session_name)
587 {
588 struct lttng_condition_buffer_usage *usage;
589 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
590
591 if (!condition || !IS_USAGE_CONDITION(condition) || !session_name) {
592 status = LTTNG_CONDITION_STATUS_INVALID;
593 goto end;
594 }
595
596 usage = container_of(condition, struct lttng_condition_buffer_usage,
597 parent);
598 if (!usage->session_name) {
599 status = LTTNG_CONDITION_STATUS_UNSET;
600 goto end;
601 }
602 *session_name = usage->session_name;
603 end:
604 return status;
605 }
606
607 enum lttng_condition_status
608 lttng_condition_buffer_usage_set_session_name(
609 struct lttng_condition *condition, const char *session_name)
610 {
611 char *session_name_copy;
612 struct lttng_condition_buffer_usage *usage;
613 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
614
615 if (!condition || !IS_USAGE_CONDITION(condition) || !session_name ||
616 strlen(session_name) == 0) {
617 status = LTTNG_CONDITION_STATUS_INVALID;
618 goto end;
619 }
620
621 usage = container_of(condition, struct lttng_condition_buffer_usage,
622 parent);
623 session_name_copy = strdup(session_name);
624 if (!session_name_copy) {
625 status = LTTNG_CONDITION_STATUS_ERROR;
626 goto end;
627 }
628
629 if (usage->session_name) {
630 free(usage->session_name);
631 }
632 usage->session_name = session_name_copy;
633 end:
634 return status;
635 }
636
637 enum lttng_condition_status
638 lttng_condition_buffer_usage_get_channel_name(
639 const struct lttng_condition *condition,
640 const char **channel_name)
641 {
642 struct lttng_condition_buffer_usage *usage;
643 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
644
645 if (!condition || !IS_USAGE_CONDITION(condition) || !channel_name) {
646 status = LTTNG_CONDITION_STATUS_INVALID;
647 goto end;
648 }
649
650 usage = container_of(condition, struct lttng_condition_buffer_usage,
651 parent);
652 if (!usage->channel_name) {
653 status = LTTNG_CONDITION_STATUS_UNSET;
654 goto end;
655 }
656 *channel_name = usage->channel_name;
657 end:
658 return status;
659 }
660
661 enum lttng_condition_status
662 lttng_condition_buffer_usage_set_channel_name(
663 struct lttng_condition *condition, const char *channel_name)
664 {
665 char *channel_name_copy;
666 struct lttng_condition_buffer_usage *usage;
667 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
668
669 if (!condition || !IS_USAGE_CONDITION(condition) || !channel_name ||
670 strlen(channel_name) == 0) {
671 status = LTTNG_CONDITION_STATUS_INVALID;
672 goto end;
673 }
674
675 usage = container_of(condition, struct lttng_condition_buffer_usage,
676 parent);
677 channel_name_copy = strdup(channel_name);
678 if (!channel_name_copy) {
679 status = LTTNG_CONDITION_STATUS_ERROR;
680 goto end;
681 }
682
683 if (usage->channel_name) {
684 free(usage->channel_name);
685 }
686 usage->channel_name = channel_name_copy;
687 end:
688 return status;
689 }
690
691 enum lttng_condition_status
692 lttng_condition_buffer_usage_get_domain_type(
693 const struct lttng_condition *condition,
694 enum lttng_domain_type *type)
695 {
696 struct lttng_condition_buffer_usage *usage;
697 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
698
699 if (!condition || !IS_USAGE_CONDITION(condition) || !type) {
700 status = LTTNG_CONDITION_STATUS_INVALID;
701 goto end;
702 }
703
704 usage = container_of(condition, struct lttng_condition_buffer_usage,
705 parent);
706 if (!usage->domain.set) {
707 status = LTTNG_CONDITION_STATUS_UNSET;
708 goto end;
709 }
710 *type = usage->domain.type;
711 end:
712 return status;
713 }
714
715 enum lttng_condition_status
716 lttng_condition_buffer_usage_set_domain_type(
717 struct lttng_condition *condition, enum lttng_domain_type type)
718 {
719 struct lttng_condition_buffer_usage *usage;
720 enum lttng_condition_status status = LTTNG_CONDITION_STATUS_OK;
721
722 if (!condition || !IS_USAGE_CONDITION(condition) ||
723 type == LTTNG_DOMAIN_NONE) {
724 status = LTTNG_CONDITION_STATUS_INVALID;
725 goto end;
726 }
727
728 usage = container_of(condition, struct lttng_condition_buffer_usage,
729 parent);
730 usage->domain.set = true;
731 usage->domain.type = type;
732 end:
733 return status;
734 }
735
736 static
737 int lttng_evaluation_buffer_usage_serialize(
738 const struct lttng_evaluation *evaluation,
739 struct lttng_payload *payload)
740 {
741 struct lttng_evaluation_buffer_usage *usage;
742 struct lttng_evaluation_buffer_usage_comm comm;
743
744 usage = container_of(evaluation, struct lttng_evaluation_buffer_usage,
745 parent);
746 comm.buffer_use = usage->buffer_use;
747 comm.buffer_capacity = usage->buffer_capacity;
748
749 return lttng_dynamic_buffer_append(
750 &payload->buffer, &comm, sizeof(comm));
751 }
752
753 static
754 void lttng_evaluation_buffer_usage_destroy(
755 struct lttng_evaluation *evaluation)
756 {
757 struct lttng_evaluation_buffer_usage *usage;
758
759 usage = container_of(evaluation, struct lttng_evaluation_buffer_usage,
760 parent);
761 free(usage);
762 }
763
764 LTTNG_HIDDEN
765 struct lttng_evaluation *lttng_evaluation_buffer_usage_create(
766 enum lttng_condition_type type, uint64_t use, uint64_t capacity)
767 {
768 struct lttng_evaluation_buffer_usage *usage;
769
770 usage = zmalloc(sizeof(struct lttng_evaluation_buffer_usage));
771 if (!usage) {
772 goto end;
773 }
774
775 usage->parent.type = type;
776 usage->buffer_use = use;
777 usage->buffer_capacity = capacity;
778 usage->parent.serialize = lttng_evaluation_buffer_usage_serialize;
779 usage->parent.destroy = lttng_evaluation_buffer_usage_destroy;
780 end:
781 return &usage->parent;
782 }
783
784 /*
785 * Get the sampled buffer usage which caused the associated condition to
786 * evaluate to "true".
787 */
788 enum lttng_evaluation_status
789 lttng_evaluation_buffer_usage_get_usage_ratio(
790 const struct lttng_evaluation *evaluation, double *usage_ratio)
791 {
792 struct lttng_evaluation_buffer_usage *usage;
793 enum lttng_evaluation_status status = LTTNG_EVALUATION_STATUS_OK;
794
795 if (!evaluation || !is_usage_evaluation(evaluation) || !usage_ratio) {
796 status = LTTNG_EVALUATION_STATUS_INVALID;
797 goto end;
798 }
799
800 usage = container_of(evaluation, struct lttng_evaluation_buffer_usage,
801 parent);
802 *usage_ratio = (double) usage->buffer_use /
803 (double) usage->buffer_capacity;
804 end:
805 return status;
806 }
807
808 enum lttng_evaluation_status
809 lttng_evaluation_buffer_usage_get_usage(
810 const struct lttng_evaluation *evaluation,
811 uint64_t *usage_bytes)
812 {
813 struct lttng_evaluation_buffer_usage *usage;
814 enum lttng_evaluation_status status = LTTNG_EVALUATION_STATUS_OK;
815
816 if (!evaluation || !is_usage_evaluation(evaluation) || !usage_bytes) {
817 status = LTTNG_EVALUATION_STATUS_INVALID;
818 goto end;
819 }
820
821 usage = container_of(evaluation, struct lttng_evaluation_buffer_usage,
822 parent);
823 *usage_bytes = usage->buffer_use;
824 end:
825 return status;
826 }
This page took 0.043955 seconds and 3 git commands to generate.