| 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.hpp> |
| 9 | #include <lttng/condition/evaluation-internal.hpp> |
| 10 | #include <lttng/condition/buffer-usage-internal.hpp> |
| 11 | #include <lttng/condition/session-consumed-size-internal.hpp> |
| 12 | #include <lttng/condition/session-rotation-internal.hpp> |
| 13 | #include <lttng/condition/event-rule-matches-internal.hpp> |
| 14 | #include <common/macros.hpp> |
| 15 | #include <common/error.hpp> |
| 16 | #include <stdbool.h> |
| 17 | |
| 18 | void lttng_evaluation_init(struct lttng_evaluation *evaluation, |
| 19 | enum lttng_condition_type type) |
| 20 | { |
| 21 | evaluation->type = type; |
| 22 | } |
| 23 | |
| 24 | int lttng_evaluation_serialize(const struct lttng_evaluation *evaluation, |
| 25 | struct lttng_payload *payload) |
| 26 | { |
| 27 | int ret; |
| 28 | struct lttng_evaluation_comm evaluation_comm; |
| 29 | |
| 30 | evaluation_comm.type = (int8_t) evaluation->type; |
| 31 | |
| 32 | ret = lttng_dynamic_buffer_append(&payload->buffer, &evaluation_comm, |
| 33 | sizeof(evaluation_comm)); |
| 34 | if (ret) { |
| 35 | goto end; |
| 36 | } |
| 37 | |
| 38 | if (evaluation->serialize) { |
| 39 | ret = evaluation->serialize(evaluation, payload); |
| 40 | if (ret) { |
| 41 | goto end; |
| 42 | } |
| 43 | } |
| 44 | end: |
| 45 | return ret; |
| 46 | } |
| 47 | |
| 48 | ssize_t lttng_evaluation_create_from_payload( |
| 49 | const struct lttng_condition *condition, |
| 50 | struct lttng_payload_view *src_view, |
| 51 | struct lttng_evaluation **evaluation) |
| 52 | { |
| 53 | ssize_t ret, evaluation_size = 0; |
| 54 | const struct lttng_evaluation_comm *evaluation_comm; |
| 55 | struct lttng_payload_view evaluation_comm_view = |
| 56 | lttng_payload_view_from_view( |
| 57 | src_view, 0, sizeof(*evaluation_comm)); |
| 58 | struct lttng_payload_view evaluation_view = |
| 59 | lttng_payload_view_from_view(src_view, |
| 60 | sizeof(*evaluation_comm), -1); |
| 61 | |
| 62 | if (!src_view || !evaluation) { |
| 63 | ret = -1; |
| 64 | goto end; |
| 65 | } |
| 66 | |
| 67 | if (!lttng_payload_view_is_valid(&evaluation_comm_view)) { |
| 68 | ret = -1; |
| 69 | goto end; |
| 70 | } |
| 71 | |
| 72 | evaluation_comm = (typeof(evaluation_comm)) evaluation_comm_view.buffer.data; |
| 73 | evaluation_size += sizeof(*evaluation_comm); |
| 74 | |
| 75 | switch ((enum lttng_condition_type) evaluation_comm->type) { |
| 76 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: |
| 77 | ret = lttng_evaluation_buffer_usage_low_create_from_payload( |
| 78 | &evaluation_view, evaluation); |
| 79 | if (ret < 0) { |
| 80 | goto end; |
| 81 | } |
| 82 | evaluation_size += ret; |
| 83 | break; |
| 84 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: |
| 85 | ret = lttng_evaluation_buffer_usage_high_create_from_payload( |
| 86 | &evaluation_view, evaluation); |
| 87 | if (ret < 0) { |
| 88 | goto end; |
| 89 | } |
| 90 | evaluation_size += ret; |
| 91 | break; |
| 92 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
| 93 | ret = lttng_evaluation_session_consumed_size_create_from_payload( |
| 94 | &evaluation_view, evaluation); |
| 95 | if (ret < 0) { |
| 96 | goto end; |
| 97 | } |
| 98 | evaluation_size += ret; |
| 99 | break; |
| 100 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
| 101 | ret = lttng_evaluation_session_rotation_ongoing_create_from_payload( |
| 102 | &evaluation_view, evaluation); |
| 103 | if (ret < 0) { |
| 104 | goto end; |
| 105 | } |
| 106 | evaluation_size += ret; |
| 107 | break; |
| 108 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: |
| 109 | ret = lttng_evaluation_session_rotation_completed_create_from_payload( |
| 110 | &evaluation_view, evaluation); |
| 111 | if (ret < 0) { |
| 112 | goto end; |
| 113 | } |
| 114 | evaluation_size += ret; |
| 115 | break; |
| 116 | case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES: |
| 117 | LTTNG_ASSERT(condition); |
| 118 | LTTNG_ASSERT(condition->type == |
| 119 | LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES); |
| 120 | ret = lttng_evaluation_event_rule_matches_create_from_payload( |
| 121 | lttng::utils::container_of(condition, |
| 122 | <tng_condition_event_rule_matches::parent), |
| 123 | &evaluation_view, evaluation); |
| 124 | if (ret < 0) { |
| 125 | goto end; |
| 126 | } |
| 127 | evaluation_size += ret; |
| 128 | break; |
| 129 | default: |
| 130 | ERR("Attempted to create evaluation of unknown type (%i)", |
| 131 | (int) evaluation_comm->type); |
| 132 | ret = -1; |
| 133 | goto end; |
| 134 | } |
| 135 | |
| 136 | ret = evaluation_size; |
| 137 | end: |
| 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | enum lttng_condition_type lttng_evaluation_get_type( |
| 142 | const struct lttng_evaluation *evaluation) |
| 143 | { |
| 144 | return evaluation ? evaluation->type : LTTNG_CONDITION_TYPE_UNKNOWN; |
| 145 | } |
| 146 | |
| 147 | void lttng_evaluation_destroy(struct lttng_evaluation *evaluation) |
| 148 | { |
| 149 | if (!evaluation) { |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | LTTNG_ASSERT(evaluation->destroy); |
| 154 | evaluation->destroy(evaluation); |
| 155 | } |