Commit | Line | Data |
---|---|---|
a58c490f | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
a58c490f | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: LGPL-2.1-only |
a58c490f | 5 | * |
a58c490f JG |
6 | */ |
7 | ||
7c920b63 | 8 | #include <lttng/condition/condition-internal.h> |
a58c490f JG |
9 | #include <lttng/condition/evaluation-internal.h> |
10 | #include <lttng/condition/buffer-usage-internal.h> | |
e8360425 | 11 | #include <lttng/condition/session-consumed-size-internal.h> |
c19092cd | 12 | #include <lttng/condition/session-rotation-internal.h> |
670a26e4 | 13 | #include <lttng/condition/event-rule-matches-internal.h> |
a58c490f JG |
14 | #include <common/macros.h> |
15 | #include <common/error.h> | |
16 | #include <stdbool.h> | |
a58c490f | 17 | |
c19092cd JG |
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 | ||
a58c490f | 25 | LTTNG_HIDDEN |
9b63a4aa | 26 | int lttng_evaluation_serialize(const struct lttng_evaluation *evaluation, |
c0a66c84 | 27 | struct lttng_payload *payload) |
a58c490f | 28 | { |
3647288f | 29 | int ret; |
db315d75 JG |
30 | struct lttng_evaluation_comm evaluation_comm = { |
31 | .type = (int8_t) evaluation->type | |
32 | }; | |
a58c490f | 33 | |
c0a66c84 | 34 | ret = lttng_dynamic_buffer_append(&payload->buffer, &evaluation_comm, |
3647288f JG |
35 | sizeof(evaluation_comm)); |
36 | if (ret) { | |
37 | goto end; | |
a58c490f | 38 | } |
a58c490f JG |
39 | |
40 | if (evaluation->serialize) { | |
c0a66c84 | 41 | ret = evaluation->serialize(evaluation, payload); |
3647288f | 42 | if (ret) { |
a58c490f JG |
43 | goto end; |
44 | } | |
a58c490f | 45 | } |
a58c490f JG |
46 | end: |
47 | return ret; | |
48 | } | |
49 | ||
50 | LTTNG_HIDDEN | |
c0a66c84 | 51 | ssize_t lttng_evaluation_create_from_payload( |
7c920b63 | 52 | const struct lttng_condition *condition, |
c0a66c84 | 53 | struct lttng_payload_view *src_view, |
a58c490f JG |
54 | struct lttng_evaluation **evaluation) |
55 | { | |
56 | ssize_t ret, evaluation_size = 0; | |
57 | const struct lttng_evaluation_comm *evaluation_comm; | |
3e6e0df2 JG |
58 | struct lttng_payload_view evaluation_comm_view = |
59 | lttng_payload_view_from_view( | |
60 | src_view, 0, sizeof(*evaluation_comm)); | |
61 | struct lttng_payload_view evaluation_view = | |
2f571d6f | 62 | lttng_payload_view_from_view(src_view, |
3e6e0df2 | 63 | sizeof(*evaluation_comm), -1); |
a58c490f JG |
64 | |
65 | if (!src_view || !evaluation) { | |
66 | ret = -1; | |
67 | goto end; | |
68 | } | |
69 | ||
3e6e0df2 JG |
70 | if (!lttng_payload_view_is_valid(&evaluation_comm_view)) { |
71 | ret = -1; | |
72 | goto end; | |
73 | } | |
74 | ||
75 | evaluation_comm = (typeof(evaluation_comm)) evaluation_comm_view.buffer.data; | |
a58c490f JG |
76 | evaluation_size += sizeof(*evaluation_comm); |
77 | ||
78 | switch ((enum lttng_condition_type) evaluation_comm->type) { | |
79 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
c0a66c84 | 80 | ret = lttng_evaluation_buffer_usage_low_create_from_payload( |
a58c490f JG |
81 | &evaluation_view, evaluation); |
82 | if (ret < 0) { | |
83 | goto end; | |
84 | } | |
85 | evaluation_size += ret; | |
86 | break; | |
87 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
c0a66c84 | 88 | ret = lttng_evaluation_buffer_usage_high_create_from_payload( |
a58c490f JG |
89 | &evaluation_view, evaluation); |
90 | if (ret < 0) { | |
91 | goto end; | |
92 | } | |
93 | evaluation_size += ret; | |
94 | break; | |
e8360425 | 95 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
c0a66c84 | 96 | ret = lttng_evaluation_session_consumed_size_create_from_payload( |
e8360425 JD |
97 | &evaluation_view, evaluation); |
98 | if (ret < 0) { | |
99 | goto end; | |
100 | } | |
101 | evaluation_size += ret; | |
102 | break; | |
c19092cd | 103 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
c0a66c84 | 104 | ret = lttng_evaluation_session_rotation_ongoing_create_from_payload( |
c19092cd JG |
105 | &evaluation_view, evaluation); |
106 | if (ret < 0) { | |
107 | goto end; | |
108 | } | |
109 | evaluation_size += ret; | |
110 | break; | |
111 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
c0a66c84 | 112 | ret = lttng_evaluation_session_rotation_completed_create_from_payload( |
c19092cd JG |
113 | &evaluation_view, evaluation); |
114 | if (ret < 0) { | |
115 | goto end; | |
116 | } | |
117 | evaluation_size += ret; | |
118 | break; | |
8dbb86b8 | 119 | case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES: |
a0377dfe FD |
120 | LTTNG_ASSERT(condition); |
121 | LTTNG_ASSERT(condition->type == | |
8dbb86b8 JR |
122 | LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES); |
123 | ret = lttng_evaluation_event_rule_matches_create_from_payload( | |
7c920b63 | 124 | container_of(condition, |
8dbb86b8 | 125 | const struct lttng_condition_event_rule_matches, |
7c920b63 PP |
126 | parent), |
127 | &evaluation_view, evaluation); | |
f1446131 JR |
128 | if (ret < 0) { |
129 | goto end; | |
130 | } | |
131 | evaluation_size += ret; | |
132 | break; | |
a58c490f JG |
133 | default: |
134 | ERR("Attempted to create evaluation of unknown type (%i)", | |
135 | (int) evaluation_comm->type); | |
136 | ret = -1; | |
137 | goto end; | |
138 | } | |
c0a66c84 | 139 | |
a58c490f JG |
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 | ||
a0377dfe | 157 | LTTNG_ASSERT(evaluation->destroy); |
a58c490f JG |
158 | evaluation->destroy(evaluation); |
159 | } |