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> | |
e8360425 | 10 | #include <lttng/condition/session-consumed-size-internal.h> |
c19092cd | 11 | #include <lttng/condition/session-rotation-internal.h> |
a58c490f JG |
12 | #include <common/macros.h> |
13 | #include <common/error.h> | |
14 | #include <common/dynamic-buffer.h> | |
15 | #include <common/buffer-view.h> | |
16 | #include <stdbool.h> | |
17 | #include <assert.h> | |
18 | ||
19 | enum lttng_condition_type lttng_condition_get_type( | |
20 | const struct lttng_condition *condition) | |
21 | { | |
22 | return condition ? condition->type : LTTNG_CONDITION_TYPE_UNKNOWN; | |
23 | } | |
24 | ||
25 | void lttng_condition_destroy(struct lttng_condition *condition) | |
4655b864 JR |
26 | { |
27 | lttng_condition_put(condition); | |
28 | } | |
29 | ||
30 | static void condition_destroy_ref(struct urcu_ref *ref) | |
31 | { | |
32 | struct lttng_condition *condition = | |
33 | container_of(ref, struct lttng_condition, ref); | |
34 | ||
35 | condition->destroy(condition); | |
36 | } | |
37 | ||
38 | LTTNG_HIDDEN | |
39 | void lttng_condition_get(struct lttng_condition *condition) | |
40 | { | |
41 | urcu_ref_get(&condition->ref); | |
42 | } | |
43 | ||
44 | LTTNG_HIDDEN | |
45 | void lttng_condition_put(struct lttng_condition *condition) | |
a58c490f JG |
46 | { |
47 | if (!condition) { | |
48 | return; | |
49 | } | |
50 | ||
51 | assert(condition->destroy); | |
4655b864 | 52 | urcu_ref_put(&condition->ref, condition_destroy_ref); |
a58c490f JG |
53 | } |
54 | ||
4655b864 | 55 | |
a58c490f JG |
56 | LTTNG_HIDDEN |
57 | bool lttng_condition_validate(const struct lttng_condition *condition) | |
58 | { | |
59 | bool valid; | |
60 | ||
61 | if (!condition) { | |
62 | valid = false; | |
63 | goto end; | |
64 | } | |
65 | ||
66 | if (!condition->validate) { | |
67 | /* Sub-class guarantees that it can never be invalid. */ | |
68 | valid = true; | |
69 | goto end; | |
70 | } | |
71 | ||
72 | valid = condition->validate(condition); | |
73 | end: | |
74 | return valid; | |
75 | } | |
76 | ||
77 | LTTNG_HIDDEN | |
3647288f | 78 | int lttng_condition_serialize(const struct lttng_condition *condition, |
c0a66c84 | 79 | struct lttng_payload *payload) |
a58c490f | 80 | { |
3647288f | 81 | int ret; |
c0a66c84 | 82 | struct lttng_condition_comm condition_comm = {}; |
a58c490f JG |
83 | |
84 | if (!condition) { | |
85 | ret = -1; | |
86 | goto end; | |
87 | } | |
88 | ||
3647288f JG |
89 | condition_comm.condition_type = (int8_t) condition->type; |
90 | ||
c0a66c84 | 91 | ret = lttng_dynamic_buffer_append(&payload->buffer, &condition_comm, |
3647288f JG |
92 | sizeof(condition_comm)); |
93 | if (ret) { | |
94 | goto end; | |
a58c490f JG |
95 | } |
96 | ||
c0a66c84 | 97 | ret = condition->serialize(condition, payload); |
3647288f | 98 | if (ret) { |
a58c490f JG |
99 | goto end; |
100 | } | |
a58c490f JG |
101 | end: |
102 | return ret; | |
103 | } | |
104 | ||
105 | LTTNG_HIDDEN | |
106 | bool lttng_condition_is_equal(const struct lttng_condition *a, | |
107 | const struct lttng_condition *b) | |
108 | { | |
109 | bool is_equal = false; | |
110 | ||
111 | if (!a || !b) { | |
112 | goto end; | |
113 | } | |
114 | ||
115 | if (a->type != b->type) { | |
116 | goto end; | |
117 | } | |
118 | ||
6c2fe319 JG |
119 | if (a == b) { |
120 | is_equal = true; | |
121 | goto end; | |
122 | } | |
123 | ||
a58c490f JG |
124 | is_equal = a->equal ? a->equal(a, b) : true; |
125 | end: | |
126 | return is_equal; | |
127 | } | |
128 | ||
129 | LTTNG_HIDDEN | |
c0a66c84 JG |
130 | ssize_t lttng_condition_create_from_payload( |
131 | struct lttng_payload_view *view, | |
a58c490f JG |
132 | struct lttng_condition **condition) |
133 | { | |
134 | ssize_t ret, condition_size = 0; | |
135 | const struct lttng_condition_comm *condition_comm; | |
c0a66c84 | 136 | condition_create_from_payload_cb create_from_payload = NULL; |
a58c490f | 137 | |
c0a66c84 | 138 | if (!view || !condition) { |
a58c490f JG |
139 | ret = -1; |
140 | goto end; | |
141 | } | |
142 | ||
143 | DBG("Deserializing condition from buffer"); | |
c0a66c84 | 144 | condition_comm = (typeof(condition_comm)) view->buffer.data; |
a58c490f JG |
145 | condition_size += sizeof(*condition_comm); |
146 | ||
147 | switch ((enum lttng_condition_type) condition_comm->condition_type) { | |
148 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
c0a66c84 | 149 | create_from_payload = lttng_condition_buffer_usage_low_create_from_payload; |
a58c490f JG |
150 | break; |
151 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
c0a66c84 | 152 | create_from_payload = lttng_condition_buffer_usage_high_create_from_payload; |
a58c490f | 153 | break; |
e8360425 | 154 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
c0a66c84 | 155 | create_from_payload = lttng_condition_session_consumed_size_create_from_payload; |
e8360425 | 156 | break; |
c19092cd | 157 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
c0a66c84 | 158 | create_from_payload = lttng_condition_session_rotation_ongoing_create_from_payload; |
c19092cd JG |
159 | break; |
160 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
c0a66c84 | 161 | create_from_payload = lttng_condition_session_rotation_completed_create_from_payload; |
c19092cd | 162 | break; |
a58c490f JG |
163 | default: |
164 | ERR("Attempted to create condition of unknown type (%i)", | |
165 | (int) condition_comm->condition_type); | |
166 | ret = -1; | |
167 | goto end; | |
168 | } | |
169 | ||
c0a66c84 JG |
170 | if (create_from_payload) { |
171 | struct lttng_payload_view condition_view = | |
172 | lttng_payload_view_from_view(view, | |
a58c490f JG |
173 | sizeof(*condition_comm), -1); |
174 | ||
c0a66c84 | 175 | ret = create_from_payload(&condition_view, condition); |
a58c490f JG |
176 | if (ret < 0) { |
177 | goto end; | |
178 | } | |
179 | condition_size += ret; | |
180 | ||
181 | } else { | |
182 | abort(); | |
183 | } | |
184 | ||
185 | ret = condition_size; | |
186 | end: | |
187 | return ret; | |
188 | } | |
189 | ||
190 | LTTNG_HIDDEN | |
191 | void lttng_condition_init(struct lttng_condition *condition, | |
192 | enum lttng_condition_type type) | |
193 | { | |
194 | condition->type = type; | |
4655b864 | 195 | urcu_ref_init(&condition->ref); |
a58c490f | 196 | } |