| 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/evaluation-internal.h> |
| 9 | #include <lttng/condition/buffer-usage-internal.h> |
| 10 | #include <lttng/condition/session-consumed-size-internal.h> |
| 11 | #include <lttng/condition/session-rotation-internal.h> |
| 12 | #include <common/macros.h> |
| 13 | #include <common/error.h> |
| 14 | #include <stdbool.h> |
| 15 | #include <assert.h> |
| 16 | |
| 17 | LTTNG_HIDDEN |
| 18 | void lttng_evaluation_init(struct lttng_evaluation *evaluation, |
| 19 | enum lttng_condition_type type) |
| 20 | { |
| 21 | evaluation->type = type; |
| 22 | } |
| 23 | |
| 24 | LTTNG_HIDDEN |
| 25 | int lttng_evaluation_serialize(const struct lttng_evaluation *evaluation, |
| 26 | struct lttng_dynamic_buffer *buf) |
| 27 | { |
| 28 | int ret; |
| 29 | struct lttng_evaluation_comm evaluation_comm = { |
| 30 | .type = (int8_t) evaluation->type |
| 31 | }; |
| 32 | |
| 33 | ret = lttng_dynamic_buffer_append(buf, &evaluation_comm, |
| 34 | sizeof(evaluation_comm)); |
| 35 | if (ret) { |
| 36 | goto end; |
| 37 | } |
| 38 | |
| 39 | if (evaluation->serialize) { |
| 40 | ret = evaluation->serialize(evaluation, buf); |
| 41 | if (ret) { |
| 42 | goto end; |
| 43 | } |
| 44 | } |
| 45 | end: |
| 46 | return ret; |
| 47 | } |
| 48 | |
| 49 | LTTNG_HIDDEN |
| 50 | ssize_t lttng_evaluation_create_from_buffer( |
| 51 | const struct lttng_buffer_view *src_view, |
| 52 | struct lttng_evaluation **evaluation) |
| 53 | { |
| 54 | ssize_t ret, evaluation_size = 0; |
| 55 | const struct lttng_evaluation_comm *evaluation_comm; |
| 56 | const struct lttng_buffer_view evaluation_view = |
| 57 | lttng_buffer_view_from_view(src_view, |
| 58 | sizeof(*evaluation_comm), -1); |
| 59 | |
| 60 | if (!src_view || !evaluation) { |
| 61 | ret = -1; |
| 62 | goto end; |
| 63 | } |
| 64 | |
| 65 | evaluation_comm = (const struct lttng_evaluation_comm *) src_view->data; |
| 66 | evaluation_size += sizeof(*evaluation_comm); |
| 67 | |
| 68 | switch ((enum lttng_condition_type) evaluation_comm->type) { |
| 69 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: |
| 70 | ret = lttng_evaluation_buffer_usage_low_create_from_buffer( |
| 71 | &evaluation_view, evaluation); |
| 72 | if (ret < 0) { |
| 73 | goto end; |
| 74 | } |
| 75 | evaluation_size += ret; |
| 76 | break; |
| 77 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: |
| 78 | ret = lttng_evaluation_buffer_usage_high_create_from_buffer( |
| 79 | &evaluation_view, evaluation); |
| 80 | if (ret < 0) { |
| 81 | goto end; |
| 82 | } |
| 83 | evaluation_size += ret; |
| 84 | break; |
| 85 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
| 86 | ret = lttng_evaluation_session_consumed_size_create_from_buffer( |
| 87 | &evaluation_view, evaluation); |
| 88 | if (ret < 0) { |
| 89 | goto end; |
| 90 | } |
| 91 | evaluation_size += ret; |
| 92 | break; |
| 93 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
| 94 | ret = lttng_evaluation_session_rotation_ongoing_create_from_buffer( |
| 95 | &evaluation_view, evaluation); |
| 96 | if (ret < 0) { |
| 97 | goto end; |
| 98 | } |
| 99 | evaluation_size += ret; |
| 100 | break; |
| 101 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: |
| 102 | ret = lttng_evaluation_session_rotation_completed_create_from_buffer( |
| 103 | &evaluation_view, evaluation); |
| 104 | if (ret < 0) { |
| 105 | goto end; |
| 106 | } |
| 107 | evaluation_size += ret; |
| 108 | break; |
| 109 | default: |
| 110 | ERR("Attempted to create evaluation of unknown type (%i)", |
| 111 | (int) evaluation_comm->type); |
| 112 | ret = -1; |
| 113 | goto end; |
| 114 | } |
| 115 | ret = evaluation_size; |
| 116 | end: |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | enum lttng_condition_type lttng_evaluation_get_type( |
| 121 | const struct lttng_evaluation *evaluation) |
| 122 | { |
| 123 | return evaluation ? evaluation->type : LTTNG_CONDITION_TYPE_UNKNOWN; |
| 124 | } |
| 125 | |
| 126 | void lttng_evaluation_destroy(struct lttng_evaluation *evaluation) |
| 127 | { |
| 128 | if (!evaluation) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | assert(evaluation->destroy); |
| 133 | evaluation->destroy(evaluation); |
| 134 | } |