2 * Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include "condition-internal.hpp"
10 #include <common/hashtable/hashtable.hpp>
11 #include <common/hashtable/utils.hpp>
13 #include <lttng/condition/buffer-usage-internal.hpp>
14 #include <lttng/condition/condition-internal.hpp>
15 #include <lttng/condition/condition.h>
16 #include <lttng/condition/event-rule-matches-internal.hpp>
17 #include <lttng/condition/event-rule-matches.h>
18 #include <lttng/condition/session-consumed-size-internal.hpp>
19 #include <lttng/condition/session-rotation-internal.hpp>
20 #include <lttng/event-rule/event-rule-internal.hpp>
22 static unsigned long lttng_condition_buffer_usage_hash(const struct lttng_condition
*_condition
)
25 unsigned long condition_type
;
26 struct lttng_condition_buffer_usage
*condition
;
28 condition
= lttng::utils::container_of(_condition
, <tng_condition_buffer_usage::parent
);
30 condition_type
= (unsigned long) condition
->parent
.type
;
31 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
32 if (condition
->session_name
) {
33 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
35 if (condition
->channel_name
) {
36 hash
^= hash_key_str(condition
->channel_name
, lttng_ht_seed
);
38 if (condition
->domain
.set
) {
39 hash
^= hash_key_ulong((void *) condition
->domain
.type
, lttng_ht_seed
);
41 if (condition
->threshold_ratio
.set
) {
42 hash
^= hash_key_u64(&condition
->threshold_ratio
.value
, lttng_ht_seed
);
43 } else if (condition
->threshold_bytes
.set
) {
46 val
= condition
->threshold_bytes
.value
;
47 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
53 lttng_condition_session_consumed_size_hash(const struct lttng_condition
*_condition
)
56 unsigned long condition_type
= (unsigned long) LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
;
57 struct lttng_condition_session_consumed_size
*condition
;
60 condition
= lttng::utils::container_of(_condition
,
61 <tng_condition_session_consumed_size::parent
);
63 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
64 if (condition
->session_name
) {
65 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
67 val
= condition
->consumed_threshold_bytes
.value
;
68 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
72 static unsigned long lttng_condition_session_rotation_hash(const struct lttng_condition
*_condition
)
74 unsigned long hash
, condition_type
;
75 struct lttng_condition_session_rotation
*condition
;
78 lttng::utils::container_of(_condition
, <tng_condition_session_rotation::parent
);
79 condition_type
= (unsigned long) condition
->parent
.type
;
80 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
81 LTTNG_ASSERT(condition
->session_name
);
82 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
87 lttng_condition_event_rule_matches_hash(const struct lttng_condition
*condition
)
89 unsigned long hash
, condition_type
;
90 enum lttng_condition_status condition_status
;
91 const struct lttng_event_rule
*event_rule
;
93 condition_type
= (unsigned long) condition
->type
;
94 condition_status
= lttng_condition_event_rule_matches_get_rule(condition
, &event_rule
);
95 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
97 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
98 return hash
^ lttng_event_rule_hash(event_rule
);
102 * The lttng_condition hashing code is kept in this file (rather than
103 * condition.c) since it makes use of GPLv2 code (hashtable utils), which we
104 * don't want to link in liblttng-ctl.
106 unsigned long lttng_condition_hash(const struct lttng_condition
*condition
)
108 switch (condition
->type
) {
109 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
110 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
111 return lttng_condition_buffer_usage_hash(condition
);
112 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
113 return lttng_condition_session_consumed_size_hash(condition
);
114 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
115 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
116 return lttng_condition_session_rotation_hash(condition
);
117 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
118 return lttng_condition_event_rule_matches_hash(condition
);
124 struct lttng_condition
*lttng_condition_copy(const struct lttng_condition
*condition
)
127 struct lttng_payload copy_buffer
;
128 struct lttng_condition
*copy
= nullptr;
130 lttng_payload_init(©_buffer
);
132 ret
= lttng_condition_serialize(condition
, ©_buffer
);
138 struct lttng_payload_view view
=
139 lttng_payload_view_from_payload(©_buffer
, 0, -1);
141 ret
= lttng_condition_create_from_payload(&view
, ©
);
149 lttng_payload_reset(©_buffer
);