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; |
d602bd6a JR |
173 | case LTTNG_CONDITION_TYPE_ON_EVENT: |
174 | create_from_payload = lttng_condition_on_event_create_from_payload; | |
683d081a | 175 | break; |
a58c490f JG |
176 | default: |
177 | ERR("Attempted to create condition of unknown type (%i)", | |
178 | (int) condition_comm->condition_type); | |
179 | ret = -1; | |
180 | goto end; | |
181 | } | |
182 | ||
c0a66c84 JG |
183 | if (create_from_payload) { |
184 | struct lttng_payload_view condition_view = | |
185 | lttng_payload_view_from_view(view, | |
a58c490f JG |
186 | sizeof(*condition_comm), -1); |
187 | ||
c0a66c84 | 188 | ret = create_from_payload(&condition_view, condition); |
a58c490f JG |
189 | if (ret < 0) { |
190 | goto end; | |
191 | } | |
192 | condition_size += ret; | |
193 | ||
194 | } else { | |
195 | abort(); | |
196 | } | |
197 | ||
198 | ret = condition_size; | |
199 | end: | |
200 | return ret; | |
201 | } | |
202 | ||
203 | LTTNG_HIDDEN | |
204 | void lttng_condition_init(struct lttng_condition *condition, | |
205 | enum lttng_condition_type type) | |
206 | { | |
207 | condition->type = type; | |
4655b864 | 208 | urcu_ref_init(&condition->ref); |
a58c490f | 209 | } |
0de2479d SM |
210 | |
211 | LTTNG_HIDDEN | |
212 | const char *lttng_condition_type_str(enum lttng_condition_type type) | |
213 | { | |
214 | switch (type) { | |
215 | case LTTNG_CONDITION_TYPE_UNKNOWN: | |
216 | return "unknown"; | |
217 | ||
218 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
219 | return "session consumed size"; | |
220 | ||
221 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
222 | return "buffer usage high"; | |
223 | ||
224 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
225 | return "buffer usage low"; | |
226 | ||
227 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: | |
228 | return "session rotation ongoing"; | |
229 | ||
230 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
231 | return "session rotation completed"; | |
232 | ||
d602bd6a | 233 | case LTTNG_CONDITION_TYPE_ON_EVENT: |
0de2479d SM |
234 | return "event rule hit"; |
235 | ||
236 | default: | |
237 | return "???"; | |
238 | } | |
239 | } |