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/session-consumed-size-internal.h>
11 #include <lttng/condition/session-rotation-internal.h>
12 #include <common/macros.h>
13 #include <common/error.h>
14 #include <common/dynamic-buffer.h>
15 #include <common/buffer-view.h>
19 enum lttng_condition_type
lttng_condition_get_type(
20 const struct lttng_condition
*condition
)
22 return condition
? condition
->type
: LTTNG_CONDITION_TYPE_UNKNOWN
;
25 void lttng_condition_destroy(struct lttng_condition
*condition
)
27 lttng_condition_put(condition
);
30 static void condition_destroy_ref(struct urcu_ref
*ref
)
32 struct lttng_condition
*condition
=
33 container_of(ref
, struct lttng_condition
, ref
);
35 condition
->destroy(condition
);
39 void lttng_condition_get(struct lttng_condition
*condition
)
41 urcu_ref_get(&condition
->ref
);
45 void lttng_condition_put(struct lttng_condition
*condition
)
51 assert(condition
->destroy
);
52 urcu_ref_put(&condition
->ref
, condition_destroy_ref
);
57 bool lttng_condition_validate(const struct lttng_condition
*condition
)
66 if (!condition
->validate
) {
67 /* Sub-class guarantees that it can never be invalid. */
72 valid
= condition
->validate(condition
);
78 int lttng_condition_serialize(const struct lttng_condition
*condition
,
79 struct lttng_payload
*payload
)
82 struct lttng_condition_comm condition_comm
= {};
89 condition_comm
.condition_type
= (int8_t) condition
->type
;
91 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &condition_comm
,
92 sizeof(condition_comm
));
97 ret
= condition
->serialize(condition
, payload
);
106 bool lttng_condition_is_equal(const struct lttng_condition
*a
,
107 const struct lttng_condition
*b
)
109 bool is_equal
= false;
115 if (a
->type
!= b
->type
) {
124 is_equal
= a
->equal
? a
->equal(a
, b
) : true;
130 ssize_t
lttng_condition_create_from_payload(
131 struct lttng_payload_view
*view
,
132 struct lttng_condition
**condition
)
134 ssize_t ret
, condition_size
= 0;
135 const struct lttng_condition_comm
*condition_comm
;
136 condition_create_from_payload_cb create_from_payload
= NULL
;
138 if (!view
|| !condition
) {
143 DBG("Deserializing condition from buffer");
144 condition_comm
= (typeof(condition_comm
)) view
->buffer
.data
;
145 condition_size
+= sizeof(*condition_comm
);
147 switch ((enum lttng_condition_type
) condition_comm
->condition_type
) {
148 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
149 create_from_payload
= lttng_condition_buffer_usage_low_create_from_payload
;
151 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
152 create_from_payload
= lttng_condition_buffer_usage_high_create_from_payload
;
154 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
155 create_from_payload
= lttng_condition_session_consumed_size_create_from_payload
;
157 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
158 create_from_payload
= lttng_condition_session_rotation_ongoing_create_from_payload
;
160 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
161 create_from_payload
= lttng_condition_session_rotation_completed_create_from_payload
;
164 ERR("Attempted to create condition of unknown type (%i)",
165 (int) condition_comm
->condition_type
);
170 if (create_from_payload
) {
171 struct lttng_payload_view condition_view
=
172 lttng_payload_view_from_view(view
,
173 sizeof(*condition_comm
), -1);
175 ret
= create_from_payload(&condition_view
, condition
);
179 condition_size
+= ret
;
185 ret
= condition_size
;
191 void lttng_condition_init(struct lttng_condition
*condition
,
192 enum lttng_condition_type type
)
194 condition
->type
= type
;
195 urcu_ref_init(&condition
->ref
);