forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stress-hsearch.c
136 lines (120 loc) · 3.53 KB
/
stress-hsearch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Copyright (C) 2013-2017 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* This code is a complete clean re-write of the stress tool by
* Colin Ian King <colin.king@canonical.com> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <apw@rossby.metr.ou.edu> but has more stress tests and more
* functionality.
*
*/
#include "stress-ng.h"
#include <search.h>
/*
* stress_set_hsearch_size()
* set hsearch size from given option string
*/
void stress_set_hsearch_size(const char *opt)
{
uint64_t hsearch_size;
hsearch_size = get_uint64(opt);
check_range("hsearch-size", hsearch_size,
MIN_TSEARCH_SIZE, MAX_TSEARCH_SIZE);
set_setting("hsearch-size", TYPE_ID_UINT64, &hsearch_size);
}
/*
* stress_hsearch()
* stress hsearch
*/
int stress_hsearch(const args_t *args)
{
uint64_t hsearch_size = DEFAULT_HSEARCH_SIZE;
size_t i, max;
int ret = EXIT_FAILURE;
char **keys;
if (!get_setting("hsearch-size", &hsearch_size)) {
if (g_opt_flags & OPT_FLAGS_MAXIMIZE)
hsearch_size = MAX_HSEARCH_SIZE;
if (g_opt_flags & OPT_FLAGS_MINIMIZE)
hsearch_size = MIN_HSEARCH_SIZE;
}
max = (size_t)hsearch_size;
/* Make hash table with 25% slack */
if (!hcreate(max + (max / 4))) {
pr_fail_err("hcreate");
return EXIT_FAILURE;
}
if ((keys = calloc(max, sizeof(char *))) == NULL) {
pr_err("%s: cannot allocate keys\n", args->name);
goto free_hash;
}
/* Populate hash, make it 100% full for worst performance */
for (i = 0; i < max; i++) {
char buffer[32];
ENTRY e;
(void)snprintf(buffer, sizeof(buffer), "%zu", i);
keys[i] = strdup(buffer);
if (!keys[i]) {
pr_err("%s: cannot allocate key\n", args->name);
goto free_all;
}
e.key = keys[i];
e.data = (void *)i;
if (hsearch(e, ENTER) == NULL) {
pr_err("%s: cannot allocate new hash item\n", args->name);
goto free_all;
}
}
do {
for (i = 0; g_keep_stressing_flag && i < max; i++) {
ENTRY e, *ep;
e.key = keys[i];
e.data = NULL; /* Keep Coverity quiet */
ep = hsearch(e, FIND);
if (g_opt_flags & OPT_FLAGS_VERIFY) {
if (ep == NULL) {
pr_fail("%s: cannot find key %s\n", args->name, keys[i]);
} else {
if (i != (size_t)ep->data) {
pr_fail("%s: hash returned incorrect data %zd\n", args->name, i);
}
}
}
}
inc_counter(args);
} while (keep_stressing());
ret = EXIT_SUCCESS;
free_all:
/*
* The sematics to hdestroy are rather varied from
* system to system. OpenBSD will free the keys,
* where as NetBSD provides traditional functionaly
* that does not free them, plus hdestroy1 where
* one can provide a free'ing callback. Linux
* currently does not destroy them. It's a mess,
* so for now, don't free them and just let it
* leak, the exit() will clean up the heap for us
*/
/*
for (i = 0; i < max; i++)
free(keys[i]);
*/
free(keys);
free_hash:
hdestroy();
return ret;
}