suricata
util-pool-thread.c
Go to the documentation of this file.
1 /* Copyright (C) 2013 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \defgroup utilpool Pool
20  *
21  * @{
22  */
23 
24 /**
25  * \file
26  *
27  * \author Victor Julien <victor@inliniac.net>
28  *
29  * Pool utility functions
30  */
31 
32 #include "suricata-common.h"
33 #include "util-pool.h"
34 #include "util-pool-thread.h"
35 #include "util-unittest.h"
36 #include "util-debug.h"
37 #include "util-validate.h"
38 
39 /**
40  * \brief per thread Pool, initialization function
41  * \param thread number of threads this is for. Can start with 1 and be expanded.
42  * Other params are as for PoolInit()
43  */
44 PoolThread *PoolThreadInit(int threads, uint32_t size, uint32_t prealloc_size, uint32_t elt_size,
45  void *(*Alloc)(void), int (*Init)(void *), void (*Cleanup)(void *))
46 {
47  sc_errno = SC_OK;
48 
49  if (threads <= 0) {
50  SCLogDebug("error");
52  return NULL;
53  }
54 
55  PoolThread *pt = SCCalloc(1, sizeof(*pt));
56  if (unlikely(pt == NULL)) {
57  SCLogDebug("memory alloc error");
59  goto error;
60  }
61 
62  SCLogDebug("size %d", threads);
63  pt->array = SCMalloc(threads * sizeof(PoolThreadElement));
64  if (pt->array == NULL) {
65  SCLogDebug("memory alloc error");
67  goto error;
68  }
69  pt->size = threads;
70 
71  for (int i = 0; i < threads; i++) {
72  PoolThreadElement *e = &pt->array[i];
73 
74  SCMutexInit(&e->lock, NULL);
75  SCMutexLock(&e->lock);
76 // SCLogDebug("size %u prealloc_size %u elt_size %u Alloc %p Init %p InitData %p Cleanup %p Free %p",
77 // size, prealloc_size, elt_size,
78 // Alloc, Init, InitData, Cleanup, Free);
79  e->pool = PoolInit(size, prealloc_size, elt_size, Alloc, Init, Cleanup);
80  SCMutexUnlock(&e->lock);
81  if (e->pool == NULL) {
82  SCLogDebug("error");
83  goto error;
84  }
85  }
86 
87  return pt;
88 error:
89  if (pt != NULL)
90  PoolThreadFree(pt);
91  return NULL;
92 }
93 
94 /** \brief expand pool by one for a new thread
95  * \retval -1 or pool thread id
96  */
98 {
99  if (pt == NULL || pt->array == NULL || pt->size == 0) {
100  SCLogError("pool grow failed");
101  return -1;
102  }
103 
104  size_t newsize = pt->size + 1;
105  SCLogDebug("newsize %"PRIuMAX, (uintmax_t)newsize);
106 
107  void *ptmp = SCRealloc(pt->array, (newsize * sizeof(PoolThreadElement)));
108  if (ptmp == NULL) {
109  SCFree(pt->array);
110  pt->array = NULL;
111  SCLogError("pool grow failed");
112  return -1;
113  }
114  pt->array = ptmp;
115  pt->size = newsize;
116 
117  /* copy settings from first thread that registered the pool */
118  Pool settings;
119  memset(&settings, 0x0, sizeof(settings));
120  PoolThreadElement *e = &pt->array[0];
121  SCMutexLock(&e->lock);
122  settings.max_buckets = e->pool->max_buckets;
123  settings.preallocated = e->pool->preallocated;
124  settings.elt_size = e->pool->elt_size;
125  settings.Alloc = e->pool->Alloc;
126  settings.Init = e->pool->Init;
127  settings.Cleanup = e->pool->Cleanup;
128  SCMutexUnlock(&e->lock);
129 
130  e = &pt->array[newsize - 1];
131  memset(e, 0x00, sizeof(*e));
132  SCMutexInit(&e->lock, NULL);
133  SCMutexLock(&e->lock);
134  e->pool = PoolInit(settings.max_buckets, settings.preallocated, settings.elt_size,
135  settings.Alloc, settings.Init, settings.Cleanup);
136  SCMutexUnlock(&e->lock);
137  if (e->pool == NULL) {
138  SCLogError("pool grow failed");
139  return -1;
140  }
141 
142  return (int)(newsize - 1);
143 }
144 
146 {
147  if (pt == NULL)
148  return -1;
149  return (int)pt->size;
150 }
151 
153 {
154  if (pt == NULL)
155  return;
156 
157  if (pt->array != NULL) {
158  for (int i = 0; i < (int)pt->size; i++) {
159  PoolThreadElement *e = &pt->array[i];
160  SCMutexLock(&e->lock);
161  PoolFree(e->pool);
162  SCMutexUnlock(&e->lock);
163  SCMutexDestroy(&e->lock);
164  }
165  SCFree(pt->array);
166  }
167  SCFree(pt);
168 }
169 
170 void *PoolThreadGetById(PoolThread *pt, uint16_t id)
171 {
172  void *data = NULL;
173 
174  if (pt == NULL || id >= pt->size)
175  return NULL;
176 
177  PoolThreadElement *e = &pt->array[id];
178  SCMutexLock(&e->lock);
179  data = PoolGet(e->pool);
180  SCMutexUnlock(&e->lock);
181  if (data) {
182  PoolThreadId *did = data;
183  *did = id;
184  }
185 
186  return data;
187 }
188 
189 void PoolThreadReturn(PoolThread *pt, void *data)
190 {
191  PoolThreadId *id = data;
192 
193  if (pt == NULL || *id >= pt->size)
194  return;
195 
196  SCLogDebug("returning to id %u", *id);
197 
198  PoolThreadElement *e = &pt->array[*id];
199  SCMutexLock(&e->lock);
200  PoolReturn(e->pool, data);
201  SCMutexUnlock(&e->lock);
202 }
203 
205 {
206  DEBUG_VALIDATE_BUG_ON(pt == NULL || id >= pt->size);
207  PoolThreadElement *e = &pt->array[id];
208  SCMutexLock(&e->lock);
209 }
210 
211 void PoolThreadReturnRaw(PoolThread *pt, PoolThreadId id, void *data)
212 {
213  DEBUG_VALIDATE_BUG_ON(pt == NULL || id >= pt->size);
214  PoolThreadElement *e = &pt->array[id];
215  PoolReturn(e->pool, data);
216 }
217 
219 {
220  DEBUG_VALIDATE_BUG_ON(pt == NULL || id >= pt->size);
221  PoolThreadElement *e = &pt->array[id];
222  SCMutexUnlock(&e->lock);
223 }
224 
225 #ifdef UNITTESTS
228  int abc;
229 };
230 
231 static void *PoolThreadTestAlloc(void)
232 {
233  void *data = SCMalloc(sizeof(struct PoolThreadTestData));
234  return data;
235 }
236 
237 static
238 void PoolThreadTestFree(void *data)
239 {
240 }
241 
242 static int PoolThreadTestInit01(void)
243 {
244  PoolThread *pt = PoolThreadInit(4, /* threads */
245  10, 5, 10, PoolThreadTestAlloc, NULL, NULL);
246  FAIL_IF(pt == NULL);
247  PoolThreadFree(pt);
248  PASS;
249 }
250 
251 static int PoolThreadTestInit02(void)
252 {
253  PoolThread *pt = PoolThreadInit(4, /* threads */
254  10, 5, 10, PoolThreadTestAlloc, NULL, PoolThreadTestFree);
255  FAIL_IF(pt == NULL);
256  PoolThreadFree(pt);
257  PASS;
258 }
259 
260 static int PoolThreadTestGet01(void)
261 {
262  PoolThread *pt = PoolThreadInit(4, /* threads */
263  10, 5, 10, PoolThreadTestAlloc, NULL, NULL);
264  FAIL_IF(pt == NULL);
265 
266  void *data = PoolThreadGetById(pt, 3);
267  FAIL_IF_NULL(data);
268 
269  struct PoolThreadTestData *pdata = data;
270  FAIL_IF(pdata->res != 3);
271 
272  PoolThreadFree(pt);
273  PASS;
274 }
275 
276 static int PoolThreadTestGet02(void)
277 {
278  PoolThread *pt = PoolThreadInit(4, /* threads */
279  10, 5, 10, PoolThreadTestAlloc, NULL, PoolThreadTestFree);
280  FAIL_IF_NULL(pt);
281 
282  void *data = PoolThreadGetById(pt, 3);
283  FAIL_IF_NULL(data);
284 
285  struct PoolThreadTestData *pdata = data;
286  FAIL_IF_NOT (pdata->res == 3);
287 
288  PoolThreadFree(pt);
289  PASS;
290 }
291 
292 static int PoolThreadTestReturn01(void)
293 {
294  PoolThread *pt = PoolThreadInit(4, /* threads */
295  10, 5, 10, PoolThreadTestAlloc, NULL, PoolThreadTestFree);
296  FAIL_IF_NULL(pt);
297 
298  void *data = PoolThreadGetById(pt, 3);
299  FAIL_IF_NULL(data);
300 
301  struct PoolThreadTestData *pdata = data;
302  FAIL_IF_NOT (pdata->res == 3);
303 
304  FAIL_IF_NOT (pt->array[3].pool->outstanding == 1);
305 
306  PoolThreadReturn(pt, data);
307 
308  FAIL_IF_NOT (pt->array[3].pool->outstanding == 0);
309 
310  PoolThreadFree(pt);
311  PASS;
312 }
313 
314 static int PoolThreadTestGrow01(void)
315 {
316  PoolThread *pt = PoolThreadInit(4, /* threads */
317  10, 5, 10, PoolThreadTestAlloc, NULL, NULL);
318  FAIL_IF_NULL(pt);
319  FAIL_IF(PoolThreadExpand(pt) < 0);
320 
321  PoolThreadFree(pt);
322  PASS;
323 }
324 
325 static int PoolThreadTestGrow02(void)
326 {
327  PoolThread *pt = PoolThreadInit(4, /* threads */
328  10, 5, 10, PoolThreadTestAlloc, NULL, PoolThreadTestFree);
329  FAIL_IF_NULL(pt);
330  FAIL_IF(PoolThreadExpand(pt) < 0);
331 
332  PoolThreadFree(pt);
333  PASS;
334 }
335 
336 static int PoolThreadTestGrow03(void)
337 {
338  PoolThread *pt = PoolThreadInit(4, /* threads */
339  10, 5, 10, PoolThreadTestAlloc, NULL, PoolThreadTestFree);
340  FAIL_IF_NULL(pt);
341  FAIL_IF(PoolThreadExpand(pt) < 0);
342 
343  void *data = PoolThreadGetById(pt, 4);
344  FAIL_IF_NULL(data);
345 
346  struct PoolThreadTestData *pdata = data;
347  FAIL_IF_NOT(pdata->res == 4);
348 
349  FAIL_IF_NOT(pt->array[4].pool->outstanding == 1);
350 
351  PoolThreadReturn(pt, data);
352 
353  FAIL_IF_NOT(pt->array[4].pool->outstanding == 0);
354 
355  PoolThreadFree(pt);
356  PASS;
357 }
358 
359 #endif
360 
362 {
363 #ifdef UNITTESTS
364  UtRegisterTest("PoolThreadTestInit01", PoolThreadTestInit01);
365  UtRegisterTest("PoolThreadTestInit02", PoolThreadTestInit02);
366 
367  UtRegisterTest("PoolThreadTestGet01", PoolThreadTestGet01);
368  UtRegisterTest("PoolThreadTestGet02", PoolThreadTestGet02);
369 
370  UtRegisterTest("PoolThreadTestReturn01", PoolThreadTestReturn01);
371 
372  UtRegisterTest("PoolThreadTestGrow01", PoolThreadTestGrow01);
373  UtRegisterTest("PoolThreadTestGrow02", PoolThreadTestGrow02);
374  UtRegisterTest("PoolThreadTestGrow03", PoolThreadTestGrow03);
375 #endif
376 }
377 
378 /**
379  * @}
380  */
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
PoolThreadTestData::abc
int abc
Definition: util-pool-thread.c:228
PoolThreadExpand
int PoolThreadExpand(PoolThread *pt)
expand pool by one for a new thread
Definition: util-pool-thread.c:97
PoolThreadElement_::pool
Pool * pool
Definition: util-pool-thread.h:47
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
PoolThread_::size
size_t size
Definition: util-pool-thread.h:54
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
PoolThreadReturnRaw
void PoolThreadReturnRaw(PoolThread *pt, PoolThreadId id, void *data)
Definition: util-pool-thread.c:211
util-pool-thread.h
PoolThreadElement_
Definition: util-pool-thread.h:45
SC_EINVAL
@ SC_EINVAL
Definition: util-error.h:30
PoolThreadTestData
Definition: util-pool-thread.c:226
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
Pool_
Definition: util-pool.h:43
PoolThreadElement_::lock
SCMutex lock
Definition: util-pool-thread.h:46
PoolThreadFree
void PoolThreadFree(PoolThread *pt)
destroy the thread pool
Definition: util-pool-thread.c:152
Pool_::Cleanup
void(* Cleanup)(void *)
Definition: util-pool.h:62
util-unittest.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
SC_ENOMEM
@ SC_ENOMEM
Definition: util-error.h:29
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:120
Pool_::outstanding
uint32_t outstanding
Definition: util-pool.h:65
PoolFree
void PoolFree(Pool *p)
Definition: util-pool.c:205
PoolThreadReturn
void PoolThreadReturn(PoolThread *pt, void *data)
return data to thread pool
Definition: util-pool-thread.c:189
SC_OK
@ SC_OK
Definition: util-error.h:27
SCMutexInit
#define SCMutexInit(mut, mutattrs)
Definition: threads-debug.h:116
PoolThreadLock
void PoolThreadLock(PoolThread *pt, PoolThreadId id)
Definition: util-pool-thread.c:204
SCRealloc
#define SCRealloc(ptr, sz)
Definition: util-mem.h:50
PoolReturn
void PoolReturn(Pool *p, void *data)
Definition: util-pool.c:306
PoolThreadInit
PoolThread * PoolThreadInit(int threads, uint32_t size, uint32_t prealloc_size, uint32_t elt_size, void *(*Alloc)(void), int(*Init)(void *), void(*Cleanup)(void *))
per thread Pool, initialization function
Definition: util-pool-thread.c:44
Pool_::Init
int(* Init)(void *)
Definition: util-pool.h:61
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
Pool_::max_buckets
uint32_t max_buckets
Definition: util-pool.h:44
PoolThreadRegisterTests
void PoolThreadRegisterTests(void)
Definition: util-pool-thread.c:361
suricata-common.h
PoolThreadTestData::res
PoolThreadId res
Definition: util-pool-thread.c:227
Pool_::Alloc
void *(* Alloc)(void)
Definition: util-pool.h:60
Pool_::preallocated
uint32_t preallocated
Definition: util-pool.h:45
util-validate.h
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
PoolGet
void * PoolGet(Pool *p)
Definition: util-pool.c:251
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
SCFree
#define SCFree(p)
Definition: util-mem.h:61
PoolThread_
Definition: util-pool-thread.h:53
sc_errno
thread_local SCError sc_errno
Definition: util-error.c:31
PoolInit
Pool * PoolInit(const uint32_t size, const uint32_t prealloc_size, const uint32_t elt_size, void *(*Alloc)(void), int(*Init)(void *), void(*Cleanup)(void *))
Init a Pool.
Definition: util-pool.c:82
PoolThreadId
uint16_t PoolThreadId
Definition: util-pool-thread.h:60
PoolThreadGetById
void * PoolThreadGetById(PoolThread *pt, uint16_t id)
get data from thread pool by thread id
Definition: util-pool-thread.c:170
Pool_::elt_size
uint32_t elt_size
Definition: util-pool.h:64
PoolThread_::array
PoolThreadElement * array
Definition: util-pool-thread.h:55
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
PoolThreadSize
int PoolThreadSize(PoolThread *pt)
get size of PoolThread (number of 'threads', so array elements)
Definition: util-pool-thread.c:145
util-pool.h
SCMutexDestroy
#define SCMutexDestroy
Definition: threads-debug.h:121
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109
PoolThreadUnlock
void PoolThreadUnlock(PoolThread *pt, PoolThreadId id)
Definition: util-pool-thread.c:218