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/event-rule-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 const struct lttng_condition_comm
*condition_comm
;
137 condition_create_from_payload_cb create_from_payload
= NULL
;
139 if (!view
|| !condition
) {
144 DBG("Deserializing condition from buffer");
145 condition_comm
= (typeof(condition_comm
)) view
->buffer
.data
;
146 condition_size
+= sizeof(*condition_comm
);
148 switch ((enum lttng_condition_type
) condition_comm
->condition_type
) {
149 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
150 create_from_payload
= lttng_condition_buffer_usage_low_create_from_payload
;
152 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
153 create_from_payload
= lttng_condition_buffer_usage_high_create_from_payload
;
155 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
156 create_from_payload
= lttng_condition_session_consumed_size_create_from_payload
;
158 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
159 create_from_payload
= lttng_condition_session_rotation_ongoing_create_from_payload
;
161 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
162 create_from_payload
= lttng_condition_session_rotation_completed_create_from_payload
;
164 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
165 create_from_payload
= lttng_condition_event_rule_create_from_payload
;
168 ERR("Attempted to create condition of unknown type (%i)",
169 (int) condition_comm
->condition_type
);
174 if (create_from_payload
) {
175 struct lttng_payload_view condition_view
=
176 lttng_payload_view_from_view(view
,
177 sizeof(*condition_comm
), -1);
179 ret
= create_from_payload(&condition_view
, condition
);
183 condition_size
+= ret
;
189 ret
= condition_size
;
195 void lttng_condition_init(struct lttng_condition
*condition
,
196 enum lttng_condition_type type
)
198 condition
->type
= type
;
199 urcu_ref_init(&condition
->ref
);
203 const char *lttng_condition_type_str(enum lttng_condition_type type
)
206 case LTTNG_CONDITION_TYPE_UNKNOWN
:
209 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
210 return "session consumed size";
212 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
213 return "buffer usage high";
215 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
216 return "buffer usage low";
218 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
219 return "session rotation ongoing";
221 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
222 return "session rotation completed";
224 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
225 return "event rule hit";