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> |
a58c490f JG |
12 | #include <common/macros.h> |
13 | #include <common/error.h> | |
14 | #include <stdbool.h> | |
15 | #include <assert.h> | |
16 | ||
c19092cd JG |
17 | LTTNG_HIDDEN |
18 | void lttng_evaluation_init(struct lttng_evaluation *evaluation, | |
19 | enum lttng_condition_type type) | |
20 | { | |
21 | evaluation->type = type; | |
22 | } | |
23 | ||
a58c490f | 24 | LTTNG_HIDDEN |
9b63a4aa | 25 | int lttng_evaluation_serialize(const struct lttng_evaluation *evaluation, |
c0a66c84 | 26 | struct lttng_payload *payload) |
a58c490f | 27 | { |
3647288f | 28 | int ret; |
db315d75 JG |
29 | struct lttng_evaluation_comm evaluation_comm = { |
30 | .type = (int8_t) evaluation->type | |
31 | }; | |
a58c490f | 32 | |
c0a66c84 | 33 | ret = lttng_dynamic_buffer_append(&payload->buffer, &evaluation_comm, |
3647288f JG |
34 | sizeof(evaluation_comm)); |
35 | if (ret) { | |
36 | goto end; | |
a58c490f | 37 | } |
a58c490f JG |
38 | |
39 | if (evaluation->serialize) { | |
c0a66c84 | 40 | ret = evaluation->serialize(evaluation, payload); |
3647288f | 41 | if (ret) { |
a58c490f JG |
42 | goto end; |
43 | } | |
a58c490f | 44 | } |
a58c490f JG |
45 | end: |
46 | return ret; | |
47 | } | |
48 | ||
49 | LTTNG_HIDDEN | |
c0a66c84 JG |
50 | ssize_t lttng_evaluation_create_from_payload( |
51 | struct lttng_payload_view *src_view, | |
a58c490f JG |
52 | struct lttng_evaluation **evaluation) |
53 | { | |
54 | ssize_t ret, evaluation_size = 0; | |
55 | const struct lttng_evaluation_comm *evaluation_comm; | |
3e6e0df2 JG |
56 | struct lttng_payload_view evaluation_comm_view = |
57 | lttng_payload_view_from_view( | |
58 | src_view, 0, sizeof(*evaluation_comm)); | |
59 | struct lttng_payload_view evaluation_view = | |
2f571d6f | 60 | lttng_payload_view_from_view(src_view, |
3e6e0df2 | 61 | sizeof(*evaluation_comm), -1); |
a58c490f JG |
62 | |
63 | if (!src_view || !evaluation) { | |
64 | ret = -1; | |
65 | goto end; | |
66 | } | |
67 | ||
3e6e0df2 JG |
68 | if (!lttng_payload_view_is_valid(&evaluation_comm_view)) { |
69 | ret = -1; | |
70 | goto end; | |
71 | } | |
72 | ||
73 | evaluation_comm = (typeof(evaluation_comm)) evaluation_comm_view.buffer.data; | |
a58c490f JG |
74 | evaluation_size += sizeof(*evaluation_comm); |
75 | ||
76 | switch ((enum lttng_condition_type) evaluation_comm->type) { | |
77 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
c0a66c84 | 78 | ret = lttng_evaluation_buffer_usage_low_create_from_payload( |
a58c490f JG |
79 | &evaluation_view, evaluation); |
80 | if (ret < 0) { | |
81 | goto end; | |
82 | } | |
83 | evaluation_size += ret; | |
84 | break; | |
85 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
c0a66c84 | 86 | ret = lttng_evaluation_buffer_usage_high_create_from_payload( |
a58c490f JG |
87 | &evaluation_view, evaluation); |
88 | if (ret < 0) { | |
89 | goto end; | |
90 | } | |
91 | evaluation_size += ret; | |
92 | break; | |
e8360425 | 93 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
c0a66c84 | 94 | ret = lttng_evaluation_session_consumed_size_create_from_payload( |
e8360425 JD |
95 | &evaluation_view, evaluation); |
96 | if (ret < 0) { | |
97 | goto end; | |
98 | } | |
99 | evaluation_size += ret; | |
100 | break; | |
c19092cd | 101 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
c0a66c84 | 102 | ret = lttng_evaluation_session_rotation_ongoing_create_from_payload( |
c19092cd JG |
103 | &evaluation_view, evaluation); |
104 | if (ret < 0) { | |
105 | goto end; | |
106 | } | |
107 | evaluation_size += ret; | |
108 | break; | |
109 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
c0a66c84 | 110 | ret = lttng_evaluation_session_rotation_completed_create_from_payload( |
c19092cd JG |
111 | &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 | } | |
c0a66c84 | 123 | |
a58c490f JG |
124 | ret = evaluation_size; |
125 | end: | |
126 | return ret; | |
127 | } | |
128 | ||
129 | enum lttng_condition_type lttng_evaluation_get_type( | |
130 | const struct lttng_evaluation *evaluation) | |
131 | { | |
132 | return evaluation ? evaluation->type : LTTNG_CONDITION_TYPE_UNKNOWN; | |
133 | } | |
134 | ||
135 | void lttng_evaluation_destroy(struct lttng_evaluation *evaluation) | |
136 | { | |
137 | if (!evaluation) { | |
138 | return; | |
139 | } | |
140 | ||
141 | assert(evaluation->destroy); | |
142 | evaluation->destroy(evaluation); | |
143 | } |