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/evaluation-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> |
d3a684ee | 12 | #include <lttng/condition/event-rule-internal.h> |
a58c490f JG |
13 | #include <common/macros.h> |
14 | #include <common/error.h> | |
15 | #include <stdbool.h> | |
16 | #include <assert.h> | |
17 | ||
c19092cd JG |
18 | LTTNG_HIDDEN |
19 | void lttng_evaluation_init(struct lttng_evaluation *evaluation, | |
20 | enum lttng_condition_type type) | |
21 | { | |
22 | evaluation->type = type; | |
23 | } | |
24 | ||
a58c490f | 25 | LTTNG_HIDDEN |
9b63a4aa | 26 | int lttng_evaluation_serialize(const struct lttng_evaluation *evaluation, |
3647288f | 27 | struct lttng_dynamic_buffer *buf) |
a58c490f | 28 | { |
3647288f | 29 | int ret; |
db315d75 JG |
30 | struct lttng_evaluation_comm evaluation_comm = { |
31 | .type = (int8_t) evaluation->type | |
32 | }; | |
a58c490f | 33 | |
3647288f JG |
34 | ret = lttng_dynamic_buffer_append(buf, &evaluation_comm, |
35 | sizeof(evaluation_comm)); | |
36 | if (ret) { | |
37 | goto end; | |
a58c490f | 38 | } |
a58c490f JG |
39 | |
40 | if (evaluation->serialize) { | |
3647288f JG |
41 | ret = evaluation->serialize(evaluation, buf); |
42 | if (ret) { | |
a58c490f JG |
43 | goto end; |
44 | } | |
a58c490f | 45 | } |
a58c490f JG |
46 | end: |
47 | return ret; | |
48 | } | |
49 | ||
50 | LTTNG_HIDDEN | |
51 | ssize_t lttng_evaluation_create_from_buffer( | |
52 | const struct lttng_buffer_view *src_view, | |
53 | struct lttng_evaluation **evaluation) | |
54 | { | |
55 | ssize_t ret, evaluation_size = 0; | |
56 | const struct lttng_evaluation_comm *evaluation_comm; | |
57 | const struct lttng_buffer_view evaluation_view = | |
58 | lttng_buffer_view_from_view(src_view, | |
59 | sizeof(*evaluation_comm), -1); | |
60 | ||
61 | if (!src_view || !evaluation) { | |
62 | ret = -1; | |
63 | goto end; | |
64 | } | |
65 | ||
66 | evaluation_comm = (const struct lttng_evaluation_comm *) src_view->data; | |
67 | evaluation_size += sizeof(*evaluation_comm); | |
68 | ||
69 | switch ((enum lttng_condition_type) evaluation_comm->type) { | |
70 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
71 | ret = lttng_evaluation_buffer_usage_low_create_from_buffer( | |
72 | &evaluation_view, evaluation); | |
73 | if (ret < 0) { | |
74 | goto end; | |
75 | } | |
76 | evaluation_size += ret; | |
77 | break; | |
78 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
79 | ret = lttng_evaluation_buffer_usage_high_create_from_buffer( | |
80 | &evaluation_view, evaluation); | |
81 | if (ret < 0) { | |
82 | goto end; | |
83 | } | |
84 | evaluation_size += ret; | |
85 | break; | |
e8360425 JD |
86 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
87 | ret = lttng_evaluation_session_consumed_size_create_from_buffer( | |
88 | &evaluation_view, evaluation); | |
89 | if (ret < 0) { | |
90 | goto end; | |
91 | } | |
92 | evaluation_size += ret; | |
93 | break; | |
c19092cd JG |
94 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
95 | ret = lttng_evaluation_session_rotation_ongoing_create_from_buffer( | |
96 | &evaluation_view, evaluation); | |
97 | if (ret < 0) { | |
98 | goto end; | |
99 | } | |
100 | evaluation_size += ret; | |
101 | break; | |
102 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
103 | ret = lttng_evaluation_session_rotation_completed_create_from_buffer( | |
104 | &evaluation_view, evaluation); | |
105 | if (ret < 0) { | |
106 | goto end; | |
107 | } | |
108 | evaluation_size += ret; | |
109 | break; | |
d3a684ee JR |
110 | case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT: |
111 | ret = lttng_evaluation_event_rule_create_from_buffer(&evaluation_view, evaluation); | |
112 | if (ret < 0) { | |
113 | goto end; | |
114 | } | |
115 | evaluation_size += ret; | |
116 | break; | |
a58c490f JG |
117 | default: |
118 | ERR("Attempted to create evaluation of unknown type (%i)", | |
119 | (int) evaluation_comm->type); | |
120 | ret = -1; | |
121 | goto end; | |
122 | } | |
123 | ret = evaluation_size; | |
124 | end: | |
125 | return ret; | |
126 | } | |
127 | ||
128 | enum lttng_condition_type lttng_evaluation_get_type( | |
129 | const struct lttng_evaluation *evaluation) | |
130 | { | |
131 | return evaluation ? evaluation->type : LTTNG_CONDITION_TYPE_UNKNOWN; | |
132 | } | |
133 | ||
134 | void lttng_evaluation_destroy(struct lttng_evaluation *evaluation) | |
135 | { | |
136 | if (!evaluation) { | |
137 | return; | |
138 | } | |
139 | ||
140 | assert(evaluation->destroy); | |
141 | evaluation->destroy(evaluation); | |
142 | } |