4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/param.h>
45 const int primes
[] = {
46 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283,
47 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359,
48 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431,
49 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491,
50 499, 503, 509, 521, 523, 541,
58 pthread_barrier_t barrier
;
60 volatile sig_atomic_t stop
;
67 char buf
[MAXPATHLEN
], *p
;
71 fp
= popen("/usr/bin/locate x", "r");
77 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
86 if (sacnt
== sasize
) {
88 sa
= realloc(sa
, sizeof(*sa
) * sasize
);
95 if (sacnt
== MAXFILES
) {
108 for (c
= 0, p
= 0; !stop
; c
++) {
113 (void)access(sa
[p
], F_OK
);
117 pthread_mutex_lock(&lock
);
119 pthread_mutex_unlock(&lock
);
126 (void)pthread_barrier_wait(&barrier
);
127 if (clock_gettime(CLOCK_MONOTONIC
, &sts
)) {
128 perror("clock_gettime");
137 if (clock_gettime(CLOCK_MONOTONIC
, &ets
)) {
138 perror("clock_gettime");
141 (void)pthread_barrier_wait(&barrier
);
149 lookups((int)(uintptr_t)cookie
);
159 stop
= (sig_atomic_t)1;
170 if (pthread_barrier_init(&barrier
, NULL
, nt
+ 1)) {
171 fprintf(stderr
, "pthread_barrier_init\n");
177 for (i
= 0; i
< nt
; i
++) {
178 if (pthread_create(&pt
, NULL
, thread
, (void *)(uintptr_t)i
)) {
179 fprintf(stderr
, "pthread_create\n");
186 us
= (long)(ets
.tv_sec
* (uint64_t)1000000 + ets
.tv_nsec
/ 1000);
187 us
-= (long)(sts
.tv_sec
* (uint64_t)1000000 + sts
.tv_nsec
/ 1000);
188 c
= nlookups
* 1000000.0 / us
;
192 printf("%d\t%d\t%.0f\t%.0f\n", sacnt
, nt
, c
, ideal
* nt
);
194 if (pthread_barrier_destroy(&barrier
)) {
195 fprintf(stderr
, "pthread_barrier_destroy\n");
201 main(int argc
, char **argv
)
205 (void)setvbuf(stdout
, NULL
, _IOLBF
, BUFSIZ
);
207 mt
= sysconf(_SC_NPROCESSORS_ONLN
);
214 if (pthread_mutex_init(&lock
, NULL
)) {
215 fprintf(stderr
, "pthread_mutex_init\n");
219 (void)signal(SIGALRM
, sigalrm
);
220 printf("# nname\tnthr\tpersec\tideal\n");
221 for (i
= 1; i
<= mt
; i
++) {