Skip to content

Commit 60758d6

Browse files
Davidlohr Buesoacmel
authored andcommitted
perf bench futex: Sanitize numeric parameters
This gets rid of oddities such as: perf bench futex hash -t -4 perf: calloc: Cannot allocate memory Runtime (and many more) are equally busted, i.e. run for bogus amounts of time. Just use the abs, instead of, for example errorring out. Committer note: After the patch: $ perf bench futex hash -t -4 # Running 'futex/hash' benchmark: Run summary [PID 10178]: 4 threads, each operating on 1024 [private] futexes for 10 secs. [thread 0] futexes: 0x34f9fa0 ... 0x34faf9c [ 4702208 ops/sec ] [thread 1] futexes: 0x34fb140 ... 0x34fc13c [ 4707020 ops/sec ] [thread 2] futexes: 0x34fc2e0 ... 0x34fd2dc [ 4711526 ops/sec ] [thread 3] futexes: 0x34fd480 ... 0x34fe47c [ 4709683 ops/sec ] Averaged 4707609 operations/sec (+- 0.04%), total secs = 10 $ Signed-off-by: Davidlohr Bueso <[email protected]> Tested-by: Arnaldo Carvalho de Melo <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent e2e1680 commit 60758d6

File tree

6 files changed

+20
-0
lines changed

6 files changed

+20
-0
lines changed

tools/perf/bench/futex-hash.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,17 @@ int bench_futex_hash(int argc, const char **argv,
130130
}
131131

132132
ncpus = sysconf(_SC_NPROCESSORS_ONLN);
133+
nsecs = futexbench_sanitize_numeric(nsecs);
134+
nfutexes = futexbench_sanitize_numeric(nfutexes);
133135

134136
sigfillset(&act.sa_mask);
135137
act.sa_sigaction = toggle_done;
136138
sigaction(SIGINT, &act, NULL);
137139

138140
if (!nthreads) /* default to the number of CPUs */
139141
nthreads = ncpus;
142+
else
143+
nthreads = futexbench_sanitize_numeric(nthreads);
140144

141145
worker = calloc(nthreads, sizeof(*worker));
142146
if (!worker)

tools/perf/bench/futex-lock-pi.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,16 @@ int bench_futex_lock_pi(int argc, const char **argv,
152152
goto err;
153153

154154
ncpus = sysconf(_SC_NPROCESSORS_ONLN);
155+
nsecs = futexbench_sanitize_numeric(nsecs);
155156

156157
sigfillset(&act.sa_mask);
157158
act.sa_sigaction = toggle_done;
158159
sigaction(SIGINT, &act, NULL);
159160

160161
if (!nthreads)
161162
nthreads = ncpus;
163+
else
164+
nthreads = futexbench_sanitize_numeric(nthreads);
162165

163166
worker = calloc(nthreads, sizeof(*worker));
164167
if (!worker)

tools/perf/bench/futex-requeue.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ int bench_futex_requeue(int argc, const char **argv,
128128

129129
if (!nthreads)
130130
nthreads = ncpus;
131+
else
132+
nthreads = futexbench_sanitize_numeric(nthreads);
131133

132134
worker = calloc(nthreads, sizeof(*worker));
133135
if (!worker)

tools/perf/bench/futex-wake-parallel.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,12 @@ int bench_futex_wake_parallel(int argc, const char **argv,
217217
sigaction(SIGINT, &act, NULL);
218218

219219
ncpus = sysconf(_SC_NPROCESSORS_ONLN);
220+
nwaking_threads = futexbench_sanitize_numeric(nwaking_threads);
221+
220222
if (!nblocked_threads)
221223
nblocked_threads = ncpus;
224+
else
225+
nblocked_threads = futexbench_sanitize_numeric(nblocked_threads);
222226

223227
/* some sanity checks */
224228
if (nwaking_threads > nblocked_threads || !nwaking_threads)

tools/perf/bench/futex-wake.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,16 @@ int bench_futex_wake(int argc, const char **argv,
129129
}
130130

131131
ncpus = sysconf(_SC_NPROCESSORS_ONLN);
132+
nwakes = futexbench_sanitize_numeric(nwakes);
132133

133134
sigfillset(&act.sa_mask);
134135
act.sa_sigaction = toggle_done;
135136
sigaction(SIGINT, &act, NULL);
136137

137138
if (!nthreads)
138139
nthreads = ncpus;
140+
else
141+
nthreads = futexbench_sanitize_numeric(nthreads);
139142

140143
worker = calloc(nthreads, sizeof(*worker));
141144
if (!worker)

tools/perf/bench/futex.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#ifndef _FUTEX_H
88
#define _FUTEX_H
99

10+
#include <stdlib.h>
1011
#include <unistd.h>
1112
#include <sys/syscall.h>
1213
#include <sys/types.h>
@@ -99,4 +100,7 @@ static inline int pthread_attr_setaffinity_np(pthread_attr_t *attr,
99100
}
100101
#endif
101102

103+
/* User input sanitation */
104+
#define futexbench_sanitize_numeric(__n) abs((__n))
105+
102106
#endif /* _FUTEX_H */

0 commit comments

Comments
 (0)