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 | ||
8 | #include <common/hashtable/utils.h> | |
9 | #include <common/hashtable/hashtable.h> | |
10 | ||
11 | #include <lttng/condition/condition.h> | |
12 | #include <lttng/condition/condition-internal.h> | |
13 | #include <lttng/condition/buffer-usage-internal.h> | |
14 | #include <lttng/condition/session-consumed-size-internal.h> | |
15 | #include <lttng/condition/session-rotation-internal.h> | |
670a26e4 JR |
16 | #include <lttng/condition/event-rule-matches-internal.h> |
17 | #include <lttng/condition/event-rule-matches.h> | |
e98a976a | 18 | #include <lttng/event-rule/event-rule-internal.h> |
670a26e4 | 19 | #include <lttng/condition/event-rule-matches-internal.h> |
e98a976a FD |
20 | #include "condition-internal.h" |
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); | |
90 | assert(condition->session_name); | |
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); | |
e98a976a FD |
105 | assert(condition_status == LTTNG_CONDITION_STATUS_OK); |
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 FD |
133 | |
134 | LTTNG_HIDDEN | |
135 | struct lttng_condition *lttng_condition_copy(const struct lttng_condition *condition) | |
136 | { | |
137 | int ret; | |
138 | struct lttng_payload copy_buffer; | |
139 | struct lttng_condition *copy = NULL; | |
140 | ||
141 | lttng_payload_init(©_buffer); | |
142 | ||
143 | ret = lttng_condition_serialize(condition, ©_buffer); | |
144 | if (ret < 0) { | |
145 | goto end; | |
146 | } | |
147 | ||
148 | { | |
149 | struct lttng_payload_view view = | |
150 | lttng_payload_view_from_payload( | |
151 | ©_buffer, 0, -1); | |
152 | ||
153 | ret = lttng_condition_create_from_payload( | |
154 | &view, ©); | |
155 | if (ret < 0) { | |
156 | copy = NULL; | |
157 | goto end; | |
158 | } | |
159 | } | |
160 | ||
161 | end: | |
162 | lttng_payload_reset(©_buffer); | |
163 | return copy; | |
164 | } |