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