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> | |
2463b787 | 10 | #include <lttng/condition/event-rule-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; | |
136 | const struct lttng_condition_comm *condition_comm; | |
c0a66c84 | 137 | condition_create_from_payload_cb create_from_payload = NULL; |
a58c490f | 138 | |
c0a66c84 | 139 | if (!view || !condition) { |
a58c490f JG |
140 | ret = -1; |
141 | goto end; | |
142 | } | |
143 | ||
144 | DBG("Deserializing condition from buffer"); | |
c0a66c84 | 145 | condition_comm = (typeof(condition_comm)) view->buffer.data; |
a58c490f JG |
146 | condition_size += sizeof(*condition_comm); |
147 | ||
148 | switch ((enum lttng_condition_type) condition_comm->condition_type) { | |
149 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
c0a66c84 | 150 | create_from_payload = lttng_condition_buffer_usage_low_create_from_payload; |
a58c490f JG |
151 | break; |
152 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
c0a66c84 | 153 | create_from_payload = lttng_condition_buffer_usage_high_create_from_payload; |
a58c490f | 154 | break; |
e8360425 | 155 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
c0a66c84 | 156 | create_from_payload = lttng_condition_session_consumed_size_create_from_payload; |
e8360425 | 157 | break; |
c19092cd | 158 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
c0a66c84 | 159 | create_from_payload = lttng_condition_session_rotation_ongoing_create_from_payload; |
c19092cd JG |
160 | break; |
161 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
c0a66c84 | 162 | create_from_payload = lttng_condition_session_rotation_completed_create_from_payload; |
c19092cd | 163 | break; |
2463b787 JR |
164 | case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT: |
165 | create_from_payload = lttng_condition_event_rule_create_from_payload; | |
166 | break; | |
a58c490f JG |
167 | default: |
168 | ERR("Attempted to create condition of unknown type (%i)", | |
169 | (int) condition_comm->condition_type); | |
170 | ret = -1; | |
171 | goto end; | |
172 | } | |
173 | ||
c0a66c84 JG |
174 | if (create_from_payload) { |
175 | struct lttng_payload_view condition_view = | |
176 | lttng_payload_view_from_view(view, | |
a58c490f JG |
177 | sizeof(*condition_comm), -1); |
178 | ||
c0a66c84 | 179 | ret = create_from_payload(&condition_view, condition); |
a58c490f JG |
180 | if (ret < 0) { |
181 | goto end; | |
182 | } | |
183 | condition_size += ret; | |
184 | ||
185 | } else { | |
186 | abort(); | |
187 | } | |
188 | ||
189 | ret = condition_size; | |
190 | end: | |
191 | return ret; | |
192 | } | |
193 | ||
194 | LTTNG_HIDDEN | |
195 | void lttng_condition_init(struct lttng_condition *condition, | |
196 | enum lttng_condition_type type) | |
197 | { | |
198 | condition->type = type; | |
4655b864 | 199 | urcu_ref_init(&condition->ref); |
a58c490f | 200 | } |
2463b787 JR |
201 | |
202 | LTTNG_HIDDEN | |
203 | const char *lttng_condition_type_str(enum lttng_condition_type type) | |
204 | { | |
205 | switch (type) { | |
206 | case LTTNG_CONDITION_TYPE_UNKNOWN: | |
207 | return "unknown"; | |
208 | ||
209 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
210 | return "session consumed size"; | |
211 | ||
212 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
213 | return "buffer usage high"; | |
214 | ||
215 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
216 | return "buffer usage low"; | |
217 | ||
218 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: | |
219 | return "session rotation ongoing"; | |
220 | ||
221 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
222 | return "session rotation completed"; | |
223 | ||
224 | case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT: | |
225 | return "event rule hit"; | |
226 | ||
227 | default: | |
228 | return "???"; | |
229 | } | |
230 | } |