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