1 /* Copyright 1999-2004 The Apache Software Foundation
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 time-sem.c has the basics of the semaphores we use in http_main.c. It's
18 intended for timing differences between various methods on an
19 architecture. In practice we've found many things affect which semaphore
22 - NFS filesystems absolutely suck for fcntl() and flock()
24 - uslock absolutely sucks on single-processor IRIX boxes, but
25 absolutely rocks on multi-processor boxes. The converse
26 is true for fcntl. sysvsem seems a moderate balance.
28 - Under Solaris you can't have too many processes use SEM_UNDO, there
29 might be a tuneable somewhere that increases the limit from 29.
30 We're not sure what the tunable is, so there's a define
31 NO_SEM_UNDO which can be used to simulate us trapping/blocking
32 signals to be able to properly release the semaphore on a clean
33 child death. You'll also need to define NEED_UNION_SEMUN
36 You'll need to define USE_SHMGET_SCOREBOARD if anonymous shared mmap()
37 doesn't work on your system (i.e. linux).
39 argv[1] is the #children, argv[2] is the #iterations per child
41 You should run each over many different #children inputs, and choose
42 #iter such that the program runs for at least a second or so... or even
43 longer depending on your patience.
47 gcc -o time-FCNTL -Wall -O time-sem.c -DUSE_FCNTL_SERIALIZED_ACCEPT
48 gcc -o time-FLOCK -Wall -O time-sem.c -DUSE_FLOCK_SERIALIZED_ACCEPT
49 gcc -o time-SYSVSEM -Wall -O time-sem.c -DUSE_SYSVSEM_SERIALIZED_ACCEPT
50 gcc -o time-SYSVSEM2 -Wall -O time-sem.c -DUSE_SYSVSEM_SERIALIZED_ACCEPT -DNO_SEM_UNDO
51 gcc -o time-PTHREAD -Wall -O time-sem.c -DUSE_PTHREAD_SERIALIZED_ACCEPT -lpthread
52 gcc -o time-USLOCK -Wall -O time-sem.c -DUSE_USLOCK_SERIALIZED_ACCEPT
54 not all versions work on all systems.
63 #include <sys/types.h>
70 #if defined(USE_FCNTL_SERIALIZED_ACCEPT)
72 static struct flock lock_it;
73 static struct flock unlock_it;
75 static int fcntl_fd=-1;
77 #define accept_mutex_child_init()
78 #define accept_mutex_cleanup()
81 * Initialize mutex lock.
82 * Must be safe to call this on a restart.
85 accept_mutex_init(void)
88 lock_it.l_whence = SEEK_SET; /* from current point */
89 lock_it.l_start = 0; /* -"- */
90 lock_it.l_len = 0; /* until end of file */
91 lock_it.l_type = F_WRLCK; /* set exclusive/write lock */
92 lock_it.l_pid = 0; /* pid not actually interesting */
93 unlock_it.l_whence = SEEK_SET; /* from current point */
94 unlock_it.l_start = 0; /* -"- */
95 unlock_it.l_len = 0; /* until end of file */
96 unlock_it.l_type = F_UNLCK; /* set exclusive/write lock */
97 unlock_it.l_pid = 0; /* pid not actually interesting */
99 printf("opening test-lock-thing in current directory\n");
100 fcntl_fd = open("test-lock-thing", O_CREAT | O_WRONLY | O_EXCL, 0644);
104 fprintf (stderr, "Cannot open lock file: %s\n", "test-lock-thing");
107 unlink("test-lock-thing");
110 void accept_mutex_on(void)
114 while ((ret = fcntl(fcntl_fd, F_SETLKW, &lock_it)) < 0 && errno == EINTR)
118 perror ("fcntl lock_it");
123 void accept_mutex_off(void)
125 if (fcntl (fcntl_fd, F_SETLKW, &unlock_it) < 0)
127 perror ("fcntl unlock_it");
132 #elif defined(USE_FLOCK_SERIALIZED_ACCEPT)
134 #include <sys/file.h>
136 static int flock_fd=-1;
138 #define FNAME "test-lock-thing"
141 * Initialize mutex lock.
142 * Must be safe to call this on a restart.
144 void accept_mutex_init(void)
147 printf("opening " FNAME " in current directory\n");
148 flock_fd = open(FNAME, O_CREAT | O_WRONLY | O_EXCL, 0644);
152 fprintf (stderr, "Cannot open lock file: %s\n", "test-lock-thing");
157 void accept_mutex_child_init(void)
159 flock_fd = open(FNAME, O_WRONLY, 0600);
160 if (flock_fd == -1) {
166 void accept_mutex_cleanup(void)
171 void accept_mutex_on(void)
175 while ((ret = flock(flock_fd, LOCK_EX)) < 0 && errno == EINTR)
179 perror ("flock(LOCK_EX)");
184 void accept_mutex_off(void)
186 if (flock (flock_fd, LOCK_UN) < 0)
188 perror ("flock(LOCK_UN)");
193 #elif defined (USE_SYSVSEM_SERIALIZED_ACCEPT)
195 #include <sys/types.h>
199 static int sem_id = -1;
201 static sigset_t accept_block_mask;
202 static sigset_t accept_previous_mask;
205 #define accept_mutex_child_init()
206 #define accept_mutex_cleanup()
208 void accept_mutex_init(void)
210 #ifdef NEED_UNION_SEMUN
211 /* believe it or not, you need to define this under solaris */
214 struct semid_ds *buf;
221 sem_id = semget(999, 1, IPC_CREAT | 0666);
227 if (semctl(sem_id, 0, SETVAL, ick) < 0) {
232 sigfillset(&accept_block_mask);
233 sigdelset(&accept_block_mask, SIGHUP);
234 sigdelset(&accept_block_mask, SIGTERM);
235 sigdelset(&accept_block_mask, SIGUSR1);
239 void accept_mutex_on()
244 if (sigprocmask(SIG_BLOCK, &accept_block_mask, &accept_previous_mask)) {
245 perror("sigprocmask(SIG_BLOCK)");
250 op.sem_flg = SEM_UNDO;
254 if (semop(sem_id, &op, 1) < 0) {
255 perror ("accept_mutex_on");
260 void accept_mutex_off()
269 op.sem_flg = SEM_UNDO;
271 if (semop(sem_id, &op, 1) < 0) {
272 perror ("accept_mutex_off");
276 if (sigprocmask(SIG_SETMASK, &accept_previous_mask, NULL)) {
277 perror("sigprocmask(SIG_SETMASK)");
283 #elif defined (USE_PTHREAD_SERIALIZED_ACCEPT)
285 /* note: pthread mutexes aren't released on child death, hence the
286 * signal goop ... in a real implementation we'd do special things
287 * during hup, term, usr1.
292 static pthread_mutex_t *mutex;
293 static sigset_t accept_block_mask;
294 static sigset_t accept_previous_mask;
296 #define accept_mutex_child_init()
297 #define accept_mutex_cleanup()
299 void accept_mutex_init(void)
301 pthread_mutexattr_t mattr;
304 fd = open ("/dev/zero", O_RDWR);
306 perror ("open(/dev/zero)");
309 mutex = (pthread_mutex_t *)mmap ((caddr_t)0, sizeof (*mutex),
310 PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
311 if (mutex == (void *)(caddr_t)-1) {
316 if (pthread_mutexattr_init(&mattr)) {
317 perror ("pthread_mutexattr_init");
320 if (pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED)) {
321 perror ("pthread_mutexattr_setpshared");
324 if (pthread_mutex_init(mutex, &mattr)) {
325 perror ("pthread_mutex_init");
328 sigfillset(&accept_block_mask);
329 sigdelset(&accept_block_mask, SIGHUP);
330 sigdelset(&accept_block_mask, SIGTERM);
331 sigdelset(&accept_block_mask, SIGUSR1);
334 void accept_mutex_on()
336 if (sigprocmask(SIG_BLOCK, &accept_block_mask, &accept_previous_mask)) {
337 perror("sigprocmask(SIG_BLOCK)");
340 if (pthread_mutex_lock (mutex)) {
341 perror ("pthread_mutex_lock");
346 void accept_mutex_off()
348 if (pthread_mutex_unlock (mutex)) {
349 perror ("pthread_mutex_unlock");
352 if (sigprocmask(SIG_SETMASK, &accept_previous_mask, NULL)) {
353 perror("sigprocmask(SIG_SETMASK)");
358 #elif defined (USE_USLOCK_SERIALIZED_ACCEPT)
362 static usptr_t *us = NULL;
363 static ulock_t uslock = NULL;
365 #define accept_mutex_child_init()
366 #define accept_mutex_cleanup()
368 void accept_mutex_init(void)
372 #define CONF_INITUSERS_MAX 15
373 if ((old = usconfig(CONF_INITUSERS, CONF_INITUSERS_MAX)) == -1) {
377 if ((old = usconfig(CONF_LOCKTYPE, US_NODEBUG)) == -1) {
381 if ((old = usconfig(CONF_ARENATYPE, US_SHAREDONLY)) == -1) {
385 if ((us = usinit("/dev/zero")) == NULL) {
389 if ((uslock = usnewlock(us)) == NULL) {
394 void accept_mutex_on()
396 switch(ussetlock(uslock)) {
401 fprintf(stderr, "didn't get lock\n");
408 void accept_mutex_off()
410 if (usunsetlock(uslock) == -1) {
411 perror("usunsetlock");
418 #ifndef USE_SHMGET_SCOREBOARD
419 static void *get_shared_mem(apr_size_t size)
423 /* allocate shared memory for the shared_counter */
424 result = (unsigned long *)mmap ((caddr_t)0, size,
425 PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1, 0);
426 if (result == (void *)(caddr_t)-1) {
433 #include <sys/types.h>
435 #ifdef HAVE_SYS_MUTEX_H
436 #include <sys/mutex.h>
440 static void *get_shared_mem(apr_size_t size)
442 key_t shmkey = IPC_PRIVATE;
449 if ((shmid = shmget(shmkey, size, IPC_CREAT | SHM_R | SHM_W)) == -1) {
456 * Some SysV systems place the shared segment WAY too close
457 * to the dynamic memory break point (sbrk(0)). This severely
458 * limits the use of malloc/sbrk in the program since sbrk will
459 * refuse to move past that point.
461 * To get around this, we move the break point "way up there",
462 * attach the segment and then move break back down. Ugly
464 if ((obrk = sbrk(MOVEBREAK)) == (char *) -1) {
469 #define BADSHMAT ((void *)(-1))
470 if ((result = shmat(shmid, 0, 0)) == BADSHMAT) {
474 * We must avoid leaving segments in the kernel's
477 if (shmctl(shmid, IPC_RMID, NULL) != 0) {
478 perror("shmctl(IPC_RMID)");
480 if (result == BADSHMAT) /* now bailout */
484 if (obrk == (char *) -1)
485 return; /* nothing else to do */
486 if (sbrk(-(MOVEBREAK)) == (char *) -1) {
494 #ifdef _POSIX_PRIORITY_SCHEDULING
498 #define YIELD sched_yield()
500 #define YIELD do { struct timeval zero; zero.tv_sec = zero.tv_usec = 0; select(0,0,0,0,&zero); } while(0)
503 void main (int argc, char **argv)
508 struct timeval first;
512 unsigned long *shared_counter;
515 fprintf (stderr, "Usage: time-sem num-child num iter\n");
519 num_child = atoi (argv[1]);
520 num_iter = atoi (argv[2]);
522 /* allocate shared memory for the shared_counter */
523 shared_counter = get_shared_mem(sizeof(*shared_counter));
525 /* initialize counter to 0 */
528 accept_mutex_init ();
530 /* parent grabs mutex until done spawning children */
533 for (i = 0; i < num_child; ++i) {
536 /* child, do our thing */
537 accept_mutex_child_init();
538 for (i = 0; i < num_iter; ++i) {
542 tmp = *shared_counter;
544 *shared_counter = tmp + 1;
548 } else if (pid == -1) {
554 /* a quick test to see that nothing is screwed up */
555 if (*shared_counter != 0) {
556 puts ("WTF! shared_counter != 0 before the children have been started!");
560 gettimeofday (&first, NULL);
561 /* launch children into action */
563 for (i = 0; i < num_child; ++i) {
564 if (wait(NULL) == -1) {
568 gettimeofday (&last, NULL);
570 if (*shared_counter != num_child * num_iter) {
571 printf ("WTF! shared_counter != num_child * num_iter!\n"
572 "shared_counter = %lu\nnum_child = %d\nnum_iter=%d\n",
574 num_child, num_iter);
577 last.tv_sec -= first.tv_sec;
578 ms = last.tv_usec - first.tv_usec;
584 printf ("%8lu.%06lu\n", last.tv_sec, last.tv_usec);
586 accept_mutex_cleanup();