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