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