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
)
31 assert(condition
->destroy
);
32 condition
->destroy(condition
);
36 bool lttng_condition_validate(const struct lttng_condition
*condition
)
45 if (!condition
->validate
) {
46 /* Sub-class guarantees that it can never be invalid. */
51 valid
= condition
->validate(condition
);
57 int lttng_condition_serialize(const struct lttng_condition
*condition
,
58 struct lttng_payload
*payload
)
61 struct lttng_condition_comm condition_comm
= {};
68 condition_comm
.condition_type
= (int8_t) condition
->type
;
70 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &condition_comm
,
71 sizeof(condition_comm
));
76 ret
= condition
->serialize(condition
, payload
);
85 bool lttng_condition_is_equal(const struct lttng_condition
*a
,
86 const struct lttng_condition
*b
)
88 bool is_equal
= false;
94 if (a
->type
!= b
->type
) {
103 is_equal
= a
->equal
? a
->equal(a
, b
) : true;
109 ssize_t
lttng_condition_create_from_payload(
110 struct lttng_payload_view
*view
,
111 struct lttng_condition
**condition
)
113 ssize_t ret
, condition_size
= 0;
114 const struct lttng_condition_comm
*condition_comm
;
115 condition_create_from_payload_cb create_from_payload
= NULL
;
117 if (!view
|| !condition
) {
122 DBG("Deserializing condition from buffer");
123 condition_comm
= (typeof(condition_comm
)) view
->buffer
.data
;
124 condition_size
+= sizeof(*condition_comm
);
126 switch ((enum lttng_condition_type
) condition_comm
->condition_type
) {
127 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
128 create_from_payload
= lttng_condition_buffer_usage_low_create_from_payload
;
130 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
131 create_from_payload
= lttng_condition_buffer_usage_high_create_from_payload
;
133 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
134 create_from_payload
= lttng_condition_session_consumed_size_create_from_payload
;
136 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
137 create_from_payload
= lttng_condition_session_rotation_ongoing_create_from_payload
;
139 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
140 create_from_payload
= lttng_condition_session_rotation_completed_create_from_payload
;
143 ERR("Attempted to create condition of unknown type (%i)",
144 (int) condition_comm
->condition_type
);
149 if (create_from_payload
) {
150 struct lttng_payload_view condition_view
=
151 lttng_payload_view_from_view(view
,
152 sizeof(*condition_comm
), -1);
154 ret
= create_from_payload(&condition_view
, condition
);
158 condition_size
+= ret
;
164 ret
= condition_size
;
170 void lttng_condition_init(struct lttng_condition
*condition
,
171 enum lttng_condition_type type
)
173 condition
->type
= type
;