docs: Add supported versions and fix-backport policy
[lttng-tools.git] / src / bin / lttng-sessiond / condition-internal.cpp
CommitLineData
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
28ab034a
JG
8#include "condition-internal.hpp"
9
c9e313bc 10#include <common/hashtable/hashtable.hpp>
28ab034a 11#include <common/hashtable/utils.hpp>
e98a976a 12
c9e313bc 13#include <lttng/condition/buffer-usage-internal.hpp>
28ab034a
JG
14#include <lttng/condition/condition-internal.hpp>
15#include <lttng/condition/condition.h>
c9e313bc 16#include <lttng/condition/event-rule-matches-internal.hpp>
670a26e4 17#include <lttng/condition/event-rule-matches.h>
28ab034a
JG
18#include <lttng/condition/session-consumed-size-internal.hpp>
19#include <lttng/condition/session-rotation-internal.hpp>
c9e313bc 20#include <lttng/event-rule/event-rule-internal.hpp>
e98a976a 21
28ab034a 22static unsigned long lttng_condition_buffer_usage_hash(const struct lttng_condition *_condition)
e98a976a
FD
23{
24 unsigned long hash;
25 unsigned long condition_type;
26 struct lttng_condition_buffer_usage *condition;
27
28ab034a 28 condition = lttng::utils::container_of(_condition, &lttng_condition_buffer_usage::parent);
e98a976a
FD
29
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);
34 }
35 if (condition->channel_name) {
36 hash ^= hash_key_str(condition->channel_name, lttng_ht_seed);
37 }
38 if (condition->domain.set) {
28ab034a 39 hash ^= hash_key_ulong((void *) condition->domain.type, lttng_ht_seed);
e98a976a
FD
40 }
41 if (condition->threshold_ratio.set) {
f66473ac 42 hash ^= hash_key_u64(&condition->threshold_ratio.value, lttng_ht_seed);
e98a976a
FD
43 } else if (condition->threshold_bytes.set) {
44 uint64_t val;
45
46 val = condition->threshold_bytes.value;
47 hash ^= hash_key_u64(&val, lttng_ht_seed);
48 }
49 return hash;
50}
51
28ab034a
JG
52static unsigned long
53lttng_condition_session_consumed_size_hash(const struct lttng_condition *_condition)
e98a976a
FD
54{
55 unsigned long hash;
28ab034a 56 unsigned long condition_type = (unsigned long) LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE;
e98a976a
FD
57 struct lttng_condition_session_consumed_size *condition;
58 uint64_t val;
59
0114db0e 60 condition = lttng::utils::container_of(_condition,
28ab034a 61 &lttng_condition_session_consumed_size::parent);
e98a976a
FD
62
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);
66 }
67 val = condition->consumed_threshold_bytes.value;
68 hash ^= hash_key_u64(&val, lttng_ht_seed);
69 return hash;
70}
71
28ab034a 72static unsigned long lttng_condition_session_rotation_hash(const struct lttng_condition *_condition)
e98a976a
FD
73{
74 unsigned long hash, condition_type;
75 struct lttng_condition_session_rotation *condition;
76
28ab034a
JG
77 condition =
78 lttng::utils::container_of(_condition, &lttng_condition_session_rotation::parent);
e98a976a
FD
79 condition_type = (unsigned long) condition->parent.type;
80 hash = hash_key_ulong((void *) condition_type, lttng_ht_seed);
a0377dfe 81 LTTNG_ASSERT(condition->session_name);
e98a976a
FD
82 hash ^= hash_key_str(condition->session_name, lttng_ht_seed);
83 return hash;
84}
85
28ab034a
JG
86static unsigned long
87lttng_condition_event_rule_matches_hash(const struct lttng_condition *condition)
e98a976a
FD
88{
89 unsigned long hash, condition_type;
90 enum lttng_condition_status condition_status;
91 const struct lttng_event_rule *event_rule;
92
93 condition_type = (unsigned long) condition->type;
28ab034a 94 condition_status = lttng_condition_event_rule_matches_get_rule(condition, &event_rule);
a0377dfe 95 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
e98a976a
FD
96
97 hash = hash_key_ulong((void *) condition_type, lttng_ht_seed);
98 return hash ^ lttng_event_rule_hash(event_rule);
99}
100
101/*
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.
105 */
106unsigned long lttng_condition_hash(const struct lttng_condition *condition)
107{
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);
8dbb86b8
JR
117 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES:
118 return lttng_condition_event_rule_matches_hash(condition);
e98a976a 119 default:
e98a976a
FD
120 abort();
121 }
122}
091fa780 123
091fa780
FD
124struct lttng_condition *lttng_condition_copy(const struct lttng_condition *condition)
125{
126 int ret;
127 struct lttng_payload copy_buffer;
cd9adb8b 128 struct lttng_condition *copy = nullptr;
091fa780
FD
129
130 lttng_payload_init(&copy_buffer);
131
132 ret = lttng_condition_serialize(condition, &copy_buffer);
133 if (ret < 0) {
134 goto end;
135 }
136
137 {
138 struct lttng_payload_view view =
28ab034a 139 lttng_payload_view_from_payload(&copy_buffer, 0, -1);
091fa780 140
28ab034a 141 ret = lttng_condition_create_from_payload(&view, &copy);
091fa780 142 if (ret < 0) {
cd9adb8b 143 copy = nullptr;
091fa780
FD
144 goto end;
145 }
146 }
147
148end:
149 lttng_payload_reset(&copy_buffer);
150 return copy;
151}
This page took 0.056892 seconds and 4 git commands to generate.