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 | ||
8 | #include <lttng/condition/condition-internal.h> | |
9 | #include <lttng/condition/buffer-usage-internal.h> | |
670a26e4 | 10 | #include <lttng/condition/event-rule-matches-internal.h> |
e8360425 | 11 | #include <lttng/condition/session-consumed-size-internal.h> |
c19092cd | 12 | #include <lttng/condition/session-rotation-internal.h> |
a58c490f JG |
13 | #include <common/macros.h> |
14 | #include <common/error.h> | |
15 | #include <common/dynamic-buffer.h> | |
16 | #include <common/buffer-view.h> | |
17 | #include <stdbool.h> | |
18 | #include <assert.h> | |
19 | ||
20 | enum lttng_condition_type lttng_condition_get_type( | |
21 | const struct lttng_condition *condition) | |
22 | { | |
23 | return condition ? condition->type : LTTNG_CONDITION_TYPE_UNKNOWN; | |
24 | } | |
25 | ||
26 | void lttng_condition_destroy(struct lttng_condition *condition) | |
4655b864 JR |
27 | { |
28 | lttng_condition_put(condition); | |
29 | } | |
30 | ||
31 | static void condition_destroy_ref(struct urcu_ref *ref) | |
32 | { | |
33 | struct lttng_condition *condition = | |
34 | container_of(ref, struct lttng_condition, ref); | |
35 | ||
36 | condition->destroy(condition); | |
37 | } | |
38 | ||
39 | LTTNG_HIDDEN | |
40 | void lttng_condition_get(struct lttng_condition *condition) | |
41 | { | |
42 | urcu_ref_get(&condition->ref); | |
43 | } | |
44 | ||
45 | LTTNG_HIDDEN | |
46 | void lttng_condition_put(struct lttng_condition *condition) | |
a58c490f JG |
47 | { |
48 | if (!condition) { | |
49 | return; | |
50 | } | |
51 | ||
52 | assert(condition->destroy); | |
4655b864 | 53 | urcu_ref_put(&condition->ref, condition_destroy_ref); |
a58c490f JG |
54 | } |
55 | ||
4655b864 | 56 | |
a58c490f JG |
57 | LTTNG_HIDDEN |
58 | bool lttng_condition_validate(const struct lttng_condition *condition) | |
59 | { | |
60 | bool valid; | |
61 | ||
62 | if (!condition) { | |
63 | valid = false; | |
64 | goto end; | |
65 | } | |
66 | ||
67 | if (!condition->validate) { | |
68 | /* Sub-class guarantees that it can never be invalid. */ | |
69 | valid = true; | |
70 | goto end; | |
71 | } | |
72 | ||
73 | valid = condition->validate(condition); | |
74 | end: | |
75 | return valid; | |
76 | } | |
77 | ||
78 | LTTNG_HIDDEN | |
3647288f | 79 | int lttng_condition_serialize(const struct lttng_condition *condition, |
c0a66c84 | 80 | struct lttng_payload *payload) |
a58c490f | 81 | { |
3647288f | 82 | int ret; |
c0a66c84 | 83 | struct lttng_condition_comm condition_comm = {}; |
a58c490f JG |
84 | |
85 | if (!condition) { | |
86 | ret = -1; | |
87 | goto end; | |
88 | } | |
89 | ||
3647288f JG |
90 | condition_comm.condition_type = (int8_t) condition->type; |
91 | ||
c0a66c84 | 92 | ret = lttng_dynamic_buffer_append(&payload->buffer, &condition_comm, |
3647288f JG |
93 | sizeof(condition_comm)); |
94 | if (ret) { | |
95 | goto end; | |
a58c490f JG |
96 | } |
97 | ||
c0a66c84 | 98 | ret = condition->serialize(condition, payload); |
3647288f | 99 | if (ret) { |
a58c490f JG |
100 | goto end; |
101 | } | |
a58c490f JG |
102 | end: |
103 | return ret; | |
104 | } | |
105 | ||
106 | LTTNG_HIDDEN | |
107 | bool lttng_condition_is_equal(const struct lttng_condition *a, | |
108 | const struct lttng_condition *b) | |
109 | { | |
110 | bool is_equal = false; | |
111 | ||
112 | if (!a || !b) { | |
113 | goto end; | |
114 | } | |
115 | ||
116 | if (a->type != b->type) { | |
117 | goto end; | |
118 | } | |
119 | ||
6c2fe319 JG |
120 | if (a == b) { |
121 | is_equal = true; | |
122 | goto end; | |
123 | } | |
124 | ||
a58c490f JG |
125 | is_equal = a->equal ? a->equal(a, b) : true; |
126 | end: | |
127 | return is_equal; | |
128 | } | |
129 | ||
130 | LTTNG_HIDDEN | |
c0a66c84 JG |
131 | ssize_t lttng_condition_create_from_payload( |
132 | struct lttng_payload_view *view, | |
a58c490f JG |
133 | struct lttng_condition **condition) |
134 | { | |
135 | ssize_t ret, condition_size = 0; | |
c0a66c84 | 136 | condition_create_from_payload_cb create_from_payload = NULL; |
3e6e0df2 JG |
137 | const struct lttng_condition_comm *condition_comm; |
138 | const struct lttng_payload_view condition_comm_view = | |
139 | lttng_payload_view_from_view( | |
140 | view, 0, sizeof(*condition_comm)); | |
a58c490f | 141 | |
c0a66c84 | 142 | if (!view || !condition) { |
a58c490f JG |
143 | ret = -1; |
144 | goto end; | |
145 | } | |
146 | ||
3e6e0df2 JG |
147 | if (!lttng_payload_view_is_valid(&condition_comm_view)) { |
148 | /* Payload not large enough to contain the header. */ | |
149 | ret = -1; | |
150 | goto end; | |
151 | } | |
152 | ||
a58c490f | 153 | DBG("Deserializing condition from buffer"); |
3e6e0df2 | 154 | condition_comm = (typeof(condition_comm)) condition_comm_view.buffer.data; |
a58c490f JG |
155 | condition_size += sizeof(*condition_comm); |
156 | ||
157 | switch ((enum lttng_condition_type) condition_comm->condition_type) { | |
158 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
c0a66c84 | 159 | create_from_payload = lttng_condition_buffer_usage_low_create_from_payload; |
a58c490f JG |
160 | break; |
161 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
c0a66c84 | 162 | create_from_payload = lttng_condition_buffer_usage_high_create_from_payload; |
a58c490f | 163 | break; |
e8360425 | 164 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
c0a66c84 | 165 | create_from_payload = lttng_condition_session_consumed_size_create_from_payload; |
e8360425 | 166 | break; |
c19092cd | 167 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
c0a66c84 | 168 | create_from_payload = lttng_condition_session_rotation_ongoing_create_from_payload; |
c19092cd JG |
169 | break; |
170 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
c0a66c84 | 171 | create_from_payload = lttng_condition_session_rotation_completed_create_from_payload; |
c19092cd | 172 | break; |
8dbb86b8 JR |
173 | case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES: |
174 | create_from_payload = | |
175 | lttng_condition_event_rule_matches_create_from_payload; | |
683d081a | 176 | break; |
a58c490f JG |
177 | default: |
178 | ERR("Attempted to create condition of unknown type (%i)", | |
179 | (int) condition_comm->condition_type); | |
180 | ret = -1; | |
181 | goto end; | |
182 | } | |
183 | ||
c0a66c84 JG |
184 | if (create_from_payload) { |
185 | struct lttng_payload_view condition_view = | |
186 | lttng_payload_view_from_view(view, | |
a58c490f JG |
187 | sizeof(*condition_comm), -1); |
188 | ||
c0a66c84 | 189 | ret = create_from_payload(&condition_view, condition); |
a58c490f JG |
190 | if (ret < 0) { |
191 | goto end; | |
192 | } | |
193 | condition_size += ret; | |
194 | ||
195 | } else { | |
196 | abort(); | |
197 | } | |
198 | ||
199 | ret = condition_size; | |
200 | end: | |
201 | return ret; | |
202 | } | |
203 | ||
204 | LTTNG_HIDDEN | |
205 | void lttng_condition_init(struct lttng_condition *condition, | |
206 | enum lttng_condition_type type) | |
207 | { | |
208 | condition->type = type; | |
4655b864 | 209 | urcu_ref_init(&condition->ref); |
a58c490f | 210 | } |
0de2479d SM |
211 | |
212 | LTTNG_HIDDEN | |
213 | const char *lttng_condition_type_str(enum lttng_condition_type type) | |
214 | { | |
215 | switch (type) { | |
216 | case LTTNG_CONDITION_TYPE_UNKNOWN: | |
217 | return "unknown"; | |
218 | ||
219 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
220 | return "session consumed size"; | |
221 | ||
222 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
223 | return "buffer usage high"; | |
224 | ||
225 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
226 | return "buffer usage low"; | |
227 | ||
228 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: | |
229 | return "session rotation ongoing"; | |
230 | ||
231 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
232 | return "session rotation completed"; | |
233 | ||
8dbb86b8 | 234 | case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES: |
0de2479d SM |
235 | return "event rule hit"; |
236 | ||
237 | default: | |
238 | return "???"; | |
239 | } | |
240 | } |