Coverage Report

Created: 2024-02-25 06:37

/src/ntopng/include/GenericHash.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *
3
 * (C) 2013-24 - ntop.org
4
 *
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software Foundation,
18
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
 *
20
 */
21
22
#ifndef _GENERIC_HASH_H_
23
#define _GENERIC_HASH_H_
24
25
#include "ntop_includes.h"
26
27
class GenericHashEntry;
28
29
/** @defgroup MonitoringData Monitoring Data
30
 * This is the group that contains all classes and datastructures that handle
31
 * monitoring data.
32
 */
33
34
/** @class GenericHash
35
 *  @brief Base hash class.
36
 *  @details Defined the base hash class for ntopng.
37
 *
38
 *  @ingroup MonitoringData
39
 *
40
 */
41
class GenericHash {
42
 protected:
43
  GenericHashEntry *
44
      *table; /**< Entry table. It is used for maintain an update history */
45
  char *name;
46
  u_int32_t num_hashes;    /**< Number of hash */
47
  u_int32_t current_size;  /**< Current size of hash (including idle or
48
                              ready-to-purge elements) */
49
  u_int32_t max_hash_size; /**< Max size of hash */
50
  u_int32_t
51
      upper_num_visited_entries; /**< Max number of entries to purge per run */
52
  u_int32_t hash_mask;
53
  RwLock **locks;
54
  NetworkInterface
55
      *iface; /**< Pointer of network interface for this generic hash */
56
  u_int32_t last_purged_hash; /**< Index of last purged hash */
57
  u_int32_t last_entry_id;    /**< An uniue identifier assigned to each entry in
58
                                 the hash table */
59
  u_int purge_step;
60
  u_int walk_idle_start_hash_id; /**< The id of the hash bucket from which to
61
                                    start walkIdle hash table walk */
62
  struct {
63
    u_int64_t num_idle_transitions;
64
    u_int64_t num_purged;
65
  } entry_state_transition_counters;
66
67
  vector<GenericHashEntry *>
68
      *idle_entries_in_use; /**< Vector used by the offline thread in charge to
69
                               hold idle entries but still in use */
70
  vector<GenericHashEntry *>
71
      *idle_entries; /**< Vector used by the offline thread in charge of
72
                        deleting hash table entries */
73
  vector<GenericHashEntry *>
74
      *idle_entries_shadow; /**< Vector prepared by the purgeIdle and
75
                               periodically swapped to idle_entries */
76
77
 public:
78
  /**
79
   * @brief A Constructor
80
   * @details Creating a new GenericHash.
81
   *
82
   * @param _iface Network interface pointer for the new hash.
83
   * @param _num_hashes Number of hashes.
84
   * @param _max_hash_size Max size of new hash.
85
   * @param _name Hash name (debug)
86
   * @return A new Instance of GenericHash.
87
   */
88
  GenericHash(NetworkInterface *_iface, u_int _num_hashes, u_int _max_hash_size,
89
              const char *_name);
90
91
  /**
92
   * @brief A Destructor
93
   */
94
  virtual ~GenericHash();
95
96
  /**
97
   * @brief Get number of entries.
98
   * @details Inline method.
99
   *
100
   * @return Current size of hash.
101
   */
102
188k
  inline u_int32_t getNumEntries() { return (current_size); };
103
104
  /**
105
   * @brief Get number of idle entries, that is, entries no longer in the hash
106
   * table but still to be purged.
107
   * @details Inline method.
108
   *
109
   * @return The number of idle entries.
110
   */
111
  u_int32_t getNumIdleEntries() const;
112
113
  /**
114
   * @brief Add new entry to generic hash.
115
   * @details If current_size < max_hash_size, this method calculate a new hash
116
   * key for the new entry, add it and update the current_size value.
117
   *
118
   * @param h Pointer of new entry to add.
119
   * @param h whether the bucket should be locked before addin the entry to the
120
   * linked list.
121
   * @return True if the entry has been added successfully,
122
   *         False otherwise.
123
   *
124
   */
125
  bool add(GenericHashEntry *h, bool do_lock);
126
127
  /**
128
   * @brief Generic hash table walker
129
   * @details This method traverses all the non-idle entries of the hash table,
130
   * calling the walker function on each of them. Function idle() is called for
131
   * each entry to evaluate its state, determine if the entry is idle, and
132
   * possibly call the walker.
133
   *
134
   * @param begin_slot begin hash slot. Use 0 to walk all slots
135
   * @param walk_all true = walk all hash, false, walk only one (non NULL) slot
136
   * @param walker A pointer to the comparison function.
137
   * @param user_data Value to be compared with the values of hash.
138
   */
139
  bool walk(u_int32_t *begin_slot, bool walk_all,
140
            bool (*walker)(GenericHashEntry *h, void *user_data,
141
                           bool *entryMatched),
142
            void *user_data);
143
144
  /**
145
   * @brief Purge idle entries that have been previous idled by purgeIdle() via
146
   * periodic calls
147
   * @return The number of purged entries
148
   */
149
  u_int64_t purgeQueuedIdleEntries();
150
151
  /**
152
   * @brief Purge idle hash entries.
153
   *
154
   * @param tv Timestamp of the current purge
155
   * @param force_idle Forcefully marks all hash_entry_state_active entries to
156
   * hash_entry_state_idle
157
   * @param full_scan Force a full scan to purge all idle entries in one shot
158
   *
159
   * @return Numbers of purged entry, 0 otherwise.
160
   */
161
  u_int purgeIdle(const struct timeval *tv, bool force_idle, bool full_scan);
162
163
  /**
164
   * @brief Purge all hash entries.
165
   *
166
   */
167
  void cleanup();
168
169
  /**
170
   * @brief Return the network interface instance associated with the hash.
171
   * @details Inline method.
172
   *
173
   * @return Pointer of network interface instance.
174
   */
175
0
  inline NetworkInterface *getInterface() { return (iface); };
176
177
  /**
178
   * @brief Return the name associated with the hash.
179
   * @details Inline method.
180
   *
181
   * @return Pointer to the name
182
   */
183
0
  inline const char *getName() const { return name; };
184
185
  /**
186
   * @brief Check whether the hash has empty space
187
   *
188
   * @return true if there is space left, or false if the hash is full
189
   */
190
  bool hasEmptyRoom();
191
192
  /**
193
   * @brief Populates a lua table with hash table stats, including
194
   * the state transitions
195
   *
196
   * @param vm A lua VM
197
   *
198
   * @return Current size of hash.
199
   */
200
  void lua(lua_State *vm);
201
};
202
203
#endif /* _GENERIC_HASH_H_ */