2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #include <lttng/condition/condition-internal.h>
19 #include <lttng/condition/buffer-usage-internal.h>
20 #include <common/macros.h>
24 bool is_usage_condition(struct lttng_condition
*condition
)
26 enum lttng_condition_type type
= lttng_condition_get_type(condition
);
28 return type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
||
29 type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
;
33 bool is_usage_evaluation(struct lttng_evaluation
*evaluation
)
35 enum lttng_condition_type type
= lttng_evaluation_get_type(evaluation
);
37 return type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
||
38 type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
;
42 void lttng_condition_buffer_usage_destroy(struct lttng_condition
*condition
)
44 struct lttng_condition_buffer_usage
*usage
;
46 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
49 free(usage
->session_name
);
50 free(usage
->channel_name
);
55 bool lttng_condition_buffer_usage_validate(struct lttng_condition
*condition
)
58 struct lttng_condition_buffer_usage
*usage
;
64 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
66 if (!usage
->session_name
) {
69 if (!usage
->channel_name
) {
72 if (!usage
->threshold_percent
.set
&& !usage
->threshold_bytes
.set
) {
82 struct lttng_condition
*lttng_condition_buffer_usage_create(
83 enum lttng_condition_type type
)
85 struct lttng_condition_buffer_usage
*condition
;
87 condition
= zmalloc(sizeof(struct lttng_condition_buffer_usage
));
92 condition
->parent
.type
= type
;
93 condition
->parent
.validate
= lttng_condition_buffer_usage_validate
;
94 condition
->parent
.destroy
= lttng_condition_buffer_usage_destroy
;
96 return &condition
->parent
;
99 struct lttng_condition
*lttng_condition_buffer_usage_low_create(void)
101 return lttng_condition_buffer_usage_create(
102 LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
);
105 struct lttng_condition
*lttng_condition_buffer_usage_high_create(void)
107 return lttng_condition_buffer_usage_create(
108 LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
);
111 enum lttng_condition_status
112 lttng_condition_buffer_usage_get_threshold_percentage(
113 struct lttng_condition
*condition
, double *threshold_percent
)
115 struct lttng_condition_buffer_usage
*usage
;
116 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
118 if (!condition
|| !is_usage_condition(condition
) ||
119 !threshold_percent
) {
120 status
= LTTNG_CONDITION_STATUS_INVALID
;
124 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
126 if (!usage
->threshold_percent
.set
) {
127 status
= LTTNG_CONDITION_STATUS_UNSET
;
130 *threshold_percent
= usage
->threshold_percent
.value
;
135 /* threshold_percent expressed as [0.0, 1.0]. */
136 enum lttng_condition_status
137 lttng_condition_buffer_usage_set_threshold_percentage(
138 struct lttng_condition
*condition
, double threshold_percent
)
140 struct lttng_condition_buffer_usage
*usage
;
141 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
143 if (!condition
|| !is_usage_condition(condition
) ||
144 threshold_percent
< 0.0 || threshold_percent
> 1.0) {
145 status
= LTTNG_CONDITION_STATUS_INVALID
;
149 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
151 usage
->threshold_percent
.set
= true;
152 usage
->threshold_bytes
.set
= false;
153 usage
->threshold_percent
.value
= threshold_percent
;
158 enum lttng_condition_status
159 lttng_condition_buffer_usage_get_threshold(
160 struct lttng_condition
*condition
, uint64_t *threshold_bytes
)
162 struct lttng_condition_buffer_usage
*usage
;
163 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
165 if (!condition
|| !is_usage_condition(condition
) || !threshold_bytes
) {
166 status
= LTTNG_CONDITION_STATUS_INVALID
;
170 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
172 if (!usage
->threshold_bytes
.set
) {
173 status
= LTTNG_CONDITION_STATUS_UNSET
;
176 *threshold_bytes
= usage
->threshold_bytes
.value
;
181 enum lttng_condition_status
182 lttng_condition_buffer_usage_set_threshold(
183 struct lttng_condition
*condition
, uint64_t threshold_bytes
)
185 struct lttng_condition_buffer_usage
*usage
;
186 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
188 if (!condition
|| !is_usage_condition(condition
)) {
189 status
= LTTNG_CONDITION_STATUS_INVALID
;
193 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
195 usage
->threshold_percent
.set
= false;
196 usage
->threshold_bytes
.set
= true;
197 usage
->threshold_bytes
.value
= threshold_bytes
;
202 enum lttng_condition_status
203 lttng_condition_buffer_usage_get_session_name(
204 struct lttng_condition
*condition
, const char **session_name
)
206 struct lttng_condition_buffer_usage
*usage
;
207 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
209 if (!condition
|| !is_usage_condition(condition
) || !session_name
) {
210 status
= LTTNG_CONDITION_STATUS_INVALID
;
214 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
216 if (!usage
->session_name
) {
217 status
= LTTNG_CONDITION_STATUS_UNSET
;
220 *session_name
= usage
->session_name
;
225 extern enum lttng_condition_status
226 lttng_condition_buffer_usage_set_session_name(
227 struct lttng_condition
*condition
, const char *session_name
)
229 struct lttng_condition_buffer_usage
*usage
;
230 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
232 if (!condition
|| !is_usage_condition(condition
) || !session_name
||
233 strlen(session_name
) == 0) {
234 status
= LTTNG_CONDITION_STATUS_INVALID
;
238 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
240 usage
->session_name
= strdup(session_name
);
241 if (!usage
->session_name
) {
242 status
= LTTNG_CONDITION_STATUS_ERROR
;
249 enum lttng_condition_status
250 lttng_condition_buffer_usage_get_channel_name(
251 struct lttng_condition
*condition
, const char **channel_name
)
253 struct lttng_condition_buffer_usage
*usage
;
254 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
256 if (!condition
|| !is_usage_condition(condition
) || !channel_name
) {
257 status
= LTTNG_CONDITION_STATUS_INVALID
;
261 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
263 if (!usage
->channel_name
) {
264 status
= LTTNG_CONDITION_STATUS_UNSET
;
267 *channel_name
= usage
->channel_name
;
272 extern enum lttng_condition_status
273 lttng_condition_buffer_usage_set_channel_name(
274 struct lttng_condition
*condition
, const char *channel_name
)
276 struct lttng_condition_buffer_usage
*usage
;
277 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
279 if (!condition
|| !is_usage_condition(condition
) || !channel_name
||
280 strlen(channel_name
) == 0) {
281 status
= LTTNG_CONDITION_STATUS_INVALID
;
285 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
287 usage
->channel_name
= strdup(channel_name
);
288 if (!usage
->channel_name
) {
289 status
= LTTNG_CONDITION_STATUS_ERROR
;
296 extern enum lttng_condition_status
297 lttng_condition_buffer_usage_get_domain_type(
298 struct lttng_condition
*condition
, enum lttng_domain_type
*type
)
300 struct lttng_condition_buffer_usage
*usage
;
301 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
303 if (!condition
|| !is_usage_condition(condition
) || !type
) {
304 status
= LTTNG_CONDITION_STATUS_INVALID
;
308 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
310 if (!usage
->domain
.set
) {
311 status
= LTTNG_CONDITION_STATUS_UNSET
;
314 *type
= usage
->domain
.type
;
319 extern enum lttng_condition_status
320 lttng_condition_buffer_usage_set_domain_type(
321 struct lttng_condition
*condition
, enum lttng_domain_type type
)
323 struct lttng_condition_buffer_usage
*usage
;
324 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
326 if (!condition
|| !is_usage_condition(condition
) ||
327 type
== LTTNG_DOMAIN_NONE
) {
328 status
= LTTNG_CONDITION_STATUS_INVALID
;
332 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
334 usage
->domain
.set
= true;
335 usage
->domain
.type
= type
;
341 void lttng_evaluation_buffer_usage_destroy(
342 struct lttng_evaluation
*evaluation
)
344 struct lttng_evaluation_buffer_usage
*usage
;
346 usage
= container_of(evaluation
, struct lttng_evaluation_buffer_usage
,
352 struct lttng_evaluation
*lttng_evaluation_buffer_usage_create(uint64_t use
,
355 struct lttng_evaluation_buffer_usage
*usage
;
357 usage
= zmalloc(sizeof(struct lttng_evaluation_buffer_usage
));
362 usage
->buffer_use
= use
;
363 usage
->buffer_capacity
= capacity
;
364 usage
->parent
.destroy
= lttng_evaluation_buffer_usage_destroy
;
366 return &usage
->parent
;
370 * Get the sampled buffer usage which caused the associated condition to
371 * evaluate to "true".
373 enum lttng_evaluation_status
374 lttng_evaluation_buffer_usage_get_usage_percentage(
375 struct lttng_evaluation
*evaluation
, double *usage_percent
)
377 struct lttng_evaluation_buffer_usage
*usage
;
378 enum lttng_evaluation_status status
= LTTNG_EVALUATION_STATUS_OK
;
380 if (!evaluation
|| !is_usage_evaluation(evaluation
) || !usage_percent
) {
381 status
= LTTNG_EVALUATION_STATUS_INVALID
;
385 usage
= container_of(evaluation
, struct lttng_evaluation_buffer_usage
,
387 *usage_percent
= (double) usage
->buffer_use
/
388 (double) usage
->buffer_capacity
;
393 enum lttng_evaluation_status
394 lttng_evaluation_buffer_usage_get_usage(struct lttng_evaluation
*evaluation
,
395 uint64_t *usage_bytes
)
397 struct lttng_evaluation_buffer_usage
*usage
;
398 enum lttng_evaluation_status status
= LTTNG_EVALUATION_STATUS_OK
;
400 if (!evaluation
|| !is_usage_evaluation(evaluation
) || !usage_bytes
) {
401 status
= LTTNG_EVALUATION_STATUS_INVALID
;
405 usage
= container_of(evaluation
, struct lttng_evaluation_buffer_usage
,
407 *usage_bytes
= usage
->buffer_use
;