SoW-2019-0002: Dynamic Snapshot
[lttng-tools.git] / src / common / evaluation.c
1 /*
2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include <lttng/condition/evaluation-internal.h>
9 #include <lttng/condition/buffer-usage-internal.h>
10 #include <lttng/condition/session-consumed-size-internal.h>
11 #include <lttng/condition/session-rotation-internal.h>
12 #include <lttng/condition/event-rule-internal.h>
13 #include <common/macros.h>
14 #include <common/error.h>
15 #include <stdbool.h>
16 #include <assert.h>
17
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
25 LTTNG_HIDDEN
26 int lttng_evaluation_serialize(const struct lttng_evaluation *evaluation,
27 struct lttng_dynamic_buffer *buf)
28 {
29 int ret;
30 struct lttng_evaluation_comm evaluation_comm = {
31 .type = (int8_t) evaluation->type
32 };
33
34 ret = lttng_dynamic_buffer_append(buf, &evaluation_comm,
35 sizeof(evaluation_comm));
36 if (ret) {
37 goto end;
38 }
39
40 if (evaluation->serialize) {
41 ret = evaluation->serialize(evaluation, buf);
42 if (ret) {
43 goto end;
44 }
45 }
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;
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;
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;
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;
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 }
This page took 0.032586 seconds and 5 git commands to generate.