Commit | Line | Data |
---|---|---|
e98a976a FD |
1 | /* |
2 | * Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: LGPL-2.1-only | |
5 | * | |
6 | */ | |
7 | ||
c9e313bc SM |
8 | #include <common/hashtable/utils.hpp> |
9 | #include <common/hashtable/hashtable.hpp> | |
e98a976a FD |
10 | |
11 | #include <lttng/condition/condition.h> | |
c9e313bc SM |
12 | #include <lttng/condition/condition-internal.hpp> |
13 | #include <lttng/condition/buffer-usage-internal.hpp> | |
14 | #include <lttng/condition/session-consumed-size-internal.hpp> | |
15 | #include <lttng/condition/session-rotation-internal.hpp> | |
16 | #include <lttng/condition/event-rule-matches-internal.hpp> | |
670a26e4 | 17 | #include <lttng/condition/event-rule-matches.h> |
c9e313bc SM |
18 | #include <lttng/event-rule/event-rule-internal.hpp> |
19 | #include <lttng/condition/event-rule-matches-internal.hpp> | |
20 | #include "condition-internal.hpp" | |
e98a976a FD |
21 | |
22 | static | |
23 | unsigned long lttng_condition_buffer_usage_hash( | |
24 | const struct lttng_condition *_condition) | |
25 | { | |
26 | unsigned long hash; | |
27 | unsigned long condition_type; | |
28 | struct lttng_condition_buffer_usage *condition; | |
29 | ||
30 | condition = container_of(_condition, | |
31 | struct lttng_condition_buffer_usage, parent); | |
32 | ||
33 | condition_type = (unsigned long) condition->parent.type; | |
34 | hash = hash_key_ulong((void *) condition_type, lttng_ht_seed); | |
35 | if (condition->session_name) { | |
36 | hash ^= hash_key_str(condition->session_name, lttng_ht_seed); | |
37 | } | |
38 | if (condition->channel_name) { | |
39 | hash ^= hash_key_str(condition->channel_name, lttng_ht_seed); | |
40 | } | |
41 | if (condition->domain.set) { | |
42 | hash ^= hash_key_ulong( | |
43 | (void *) condition->domain.type, | |
44 | lttng_ht_seed); | |
45 | } | |
46 | if (condition->threshold_ratio.set) { | |
f66473ac | 47 | hash ^= hash_key_u64(&condition->threshold_ratio.value, lttng_ht_seed); |
e98a976a FD |
48 | } else if (condition->threshold_bytes.set) { |
49 | uint64_t val; | |
50 | ||
51 | val = condition->threshold_bytes.value; | |
52 | hash ^= hash_key_u64(&val, lttng_ht_seed); | |
53 | } | |
54 | return hash; | |
55 | } | |
56 | ||
57 | static | |
58 | unsigned long lttng_condition_session_consumed_size_hash( | |
59 | const struct lttng_condition *_condition) | |
60 | { | |
61 | unsigned long hash; | |
62 | unsigned long condition_type = | |
63 | (unsigned long) LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE; | |
64 | struct lttng_condition_session_consumed_size *condition; | |
65 | uint64_t val; | |
66 | ||
67 | condition = container_of(_condition, | |
68 | struct lttng_condition_session_consumed_size, parent); | |
69 | ||
70 | hash = hash_key_ulong((void *) condition_type, lttng_ht_seed); | |
71 | if (condition->session_name) { | |
72 | hash ^= hash_key_str(condition->session_name, lttng_ht_seed); | |
73 | } | |
74 | val = condition->consumed_threshold_bytes.value; | |
75 | hash ^= hash_key_u64(&val, lttng_ht_seed); | |
76 | return hash; | |
77 | } | |
78 | ||
79 | static | |
80 | unsigned long lttng_condition_session_rotation_hash( | |
81 | const struct lttng_condition *_condition) | |
82 | { | |
83 | unsigned long hash, condition_type; | |
84 | struct lttng_condition_session_rotation *condition; | |
85 | ||
86 | condition = container_of(_condition, | |
87 | struct lttng_condition_session_rotation, parent); | |
88 | condition_type = (unsigned long) condition->parent.type; | |
89 | hash = hash_key_ulong((void *) condition_type, lttng_ht_seed); | |
a0377dfe | 90 | LTTNG_ASSERT(condition->session_name); |
e98a976a FD |
91 | hash ^= hash_key_str(condition->session_name, lttng_ht_seed); |
92 | return hash; | |
93 | } | |
94 | ||
8dbb86b8 JR |
95 | static unsigned long lttng_condition_event_rule_matches_hash( |
96 | const struct lttng_condition *condition) | |
e98a976a FD |
97 | { |
98 | unsigned long hash, condition_type; | |
99 | enum lttng_condition_status condition_status; | |
100 | const struct lttng_event_rule *event_rule; | |
101 | ||
102 | condition_type = (unsigned long) condition->type; | |
8dbb86b8 JR |
103 | condition_status = lttng_condition_event_rule_matches_get_rule( |
104 | condition, &event_rule); | |
a0377dfe | 105 | LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK); |
e98a976a FD |
106 | |
107 | hash = hash_key_ulong((void *) condition_type, lttng_ht_seed); | |
108 | return hash ^ lttng_event_rule_hash(event_rule); | |
109 | } | |
110 | ||
111 | /* | |
112 | * The lttng_condition hashing code is kept in this file (rather than | |
113 | * condition.c) since it makes use of GPLv2 code (hashtable utils), which we | |
114 | * don't want to link in liblttng-ctl. | |
115 | */ | |
116 | unsigned long lttng_condition_hash(const struct lttng_condition *condition) | |
117 | { | |
118 | switch (condition->type) { | |
119 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
120 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
121 | return lttng_condition_buffer_usage_hash(condition); | |
122 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
123 | return lttng_condition_session_consumed_size_hash(condition); | |
124 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: | |
125 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
126 | return lttng_condition_session_rotation_hash(condition); | |
8dbb86b8 JR |
127 | case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES: |
128 | return lttng_condition_event_rule_matches_hash(condition); | |
e98a976a | 129 | default: |
e98a976a FD |
130 | abort(); |
131 | } | |
132 | } | |
091fa780 | 133 | |
091fa780 FD |
134 | struct lttng_condition *lttng_condition_copy(const struct lttng_condition *condition) |
135 | { | |
136 | int ret; | |
137 | struct lttng_payload copy_buffer; | |
138 | struct lttng_condition *copy = NULL; | |
139 | ||
140 | lttng_payload_init(©_buffer); | |
141 | ||
142 | ret = lttng_condition_serialize(condition, ©_buffer); | |
143 | if (ret < 0) { | |
144 | goto end; | |
145 | } | |
146 | ||
147 | { | |
148 | struct lttng_payload_view view = | |
149 | lttng_payload_view_from_payload( | |
150 | ©_buffer, 0, -1); | |
151 | ||
152 | ret = lttng_condition_create_from_payload( | |
153 | &view, ©); | |
154 | if (ret < 0) { | |
155 | copy = NULL; | |
156 | goto end; | |
157 | } | |
158 | } | |
159 | ||
160 | end: | |
161 | lttng_payload_reset(©_buffer); | |
162 | return copy; | |
163 | } |