2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <lttng/condition/condition-internal.h>
9 #include <lttng/condition/buffer-usage-internal.h>
10 #include <lttng/condition/on-event-internal.h>
11 #include <lttng/condition/session-consumed-size-internal.h>
12 #include <lttng/condition/session-rotation-internal.h>
13 #include <common/macros.h>
14 #include <common/error.h>
15 #include <common/dynamic-buffer.h>
16 #include <common/buffer-view.h>
20 enum lttng_condition_type
lttng_condition_get_type(
21 const struct lttng_condition
*condition
)
23 return condition
? condition
->type
: LTTNG_CONDITION_TYPE_UNKNOWN
;
26 void lttng_condition_destroy(struct lttng_condition
*condition
)
28 lttng_condition_put(condition
);
31 static void condition_destroy_ref(struct urcu_ref
*ref
)
33 struct lttng_condition
*condition
=
34 container_of(ref
, struct lttng_condition
, ref
);
36 condition
->destroy(condition
);
40 void lttng_condition_get(struct lttng_condition
*condition
)
42 urcu_ref_get(&condition
->ref
);
46 void lttng_condition_put(struct lttng_condition
*condition
)
52 assert(condition
->destroy
);
53 urcu_ref_put(&condition
->ref
, condition_destroy_ref
);
58 bool lttng_condition_validate(const struct lttng_condition
*condition
)
67 if (!condition
->validate
) {
68 /* Sub-class guarantees that it can never be invalid. */
73 valid
= condition
->validate(condition
);
79 int lttng_condition_serialize(const struct lttng_condition
*condition
,
80 struct lttng_payload
*payload
)
83 struct lttng_condition_comm condition_comm
= {};
90 condition_comm
.condition_type
= (int8_t) condition
->type
;
92 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &condition_comm
,
93 sizeof(condition_comm
));
98 ret
= condition
->serialize(condition
, payload
);
107 bool lttng_condition_is_equal(const struct lttng_condition
*a
,
108 const struct lttng_condition
*b
)
110 bool is_equal
= false;
116 if (a
->type
!= b
->type
) {
125 is_equal
= a
->equal
? a
->equal(a
, b
) : true;
131 ssize_t
lttng_condition_create_from_payload(
132 struct lttng_payload_view
*view
,
133 struct lttng_condition
**condition
)
135 ssize_t ret
, condition_size
= 0;
136 condition_create_from_payload_cb create_from_payload
= NULL
;
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
));
142 if (!view
|| !condition
) {
147 if (!lttng_payload_view_is_valid(&condition_comm_view
)) {
148 /* Payload not large enough to contain the header. */
153 DBG("Deserializing condition from buffer");
154 condition_comm
= (typeof(condition_comm
)) condition_comm_view
.buffer
.data
;
155 condition_size
+= sizeof(*condition_comm
);
157 switch ((enum lttng_condition_type
) condition_comm
->condition_type
) {
158 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
159 create_from_payload
= lttng_condition_buffer_usage_low_create_from_payload
;
161 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
162 create_from_payload
= lttng_condition_buffer_usage_high_create_from_payload
;
164 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
165 create_from_payload
= lttng_condition_session_consumed_size_create_from_payload
;
167 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
168 create_from_payload
= lttng_condition_session_rotation_ongoing_create_from_payload
;
170 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
171 create_from_payload
= lttng_condition_session_rotation_completed_create_from_payload
;
173 case LTTNG_CONDITION_TYPE_ON_EVENT
:
174 create_from_payload
= lttng_condition_on_event_create_from_payload
;
177 ERR("Attempted to create condition of unknown type (%i)",
178 (int) condition_comm
->condition_type
);
183 if (create_from_payload
) {
184 struct lttng_payload_view condition_view
=
185 lttng_payload_view_from_view(view
,
186 sizeof(*condition_comm
), -1);
188 ret
= create_from_payload(&condition_view
, condition
);
192 condition_size
+= ret
;
198 ret
= condition_size
;
204 void lttng_condition_init(struct lttng_condition
*condition
,
205 enum lttng_condition_type type
)
207 condition
->type
= type
;
208 urcu_ref_init(&condition
->ref
);
212 const char *lttng_condition_type_str(enum lttng_condition_type type
)
215 case LTTNG_CONDITION_TYPE_UNKNOWN
:
218 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
219 return "session consumed size";
221 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
222 return "buffer usage high";
224 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
225 return "buffer usage low";
227 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
228 return "session rotation ongoing";
230 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
231 return "session rotation completed";
233 case LTTNG_CONDITION_TYPE_ON_EVENT
:
234 return "event rule hit";