xref: /titanic_50/usr/src/lib/libmtmalloc/tests/exhaust.c (revision c77a61a72b5ecdc507d6cf104142edd371a16c84)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1998-2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include "mtmalloc.h"
31 #include <unistd.h>
32 #include <thread.h>
33 #include <sys/types.h>
34 #include <sys/mman.h>
35 #include <fcntl.h>
36 #include <errno.h>
37 
38 /*
39  * This file tests for swap space exhaustion
40  *
41  * cc -O -o exhaust exhaust.c -lmtmalloc
42  */
43 
44 void * be_thread(void *);
45 int iwin = 0;
46 
47 main(int argc, char ** argv)
48 {
49 	int ncpus;
50 	thread_t tid[512];
51 	int fd;
52 	caddr_t stacks[512];
53 
54 	srand(getpid());
55 	ncpus = sysconf(_SC_NPROCESSORS_CONF);
56 
57 	fd = open("/dev/zero", O_RDONLY);
58 
59 	if (fd < 0) {
60 		perror("open");
61 		exit(-1);
62 	}
63 
64 	while (ncpus--)
65 		stacks[ncpus] = mmap(0, 1<<23, PROT_READ|PROT_WRITE,
66 			MAP_PRIVATE, fd, 0);
67 
68 	close(fd);
69 
70 	mallocctl(MTCHUNKSIZE, 150);
71 
72 	ncpus = sysconf(_SC_NPROCESSORS_CONF);
73 	while (ncpus--)
74 		thr_create(stacks[ncpus], 1<<23, be_thread, NULL, THR_BOUND,
75 			&tid[ncpus]);
76 
77 	while (thr_join(NULL, NULL, NULL) == 0);
78 
79 	exit(0);
80 }
81 
82 /* ARGSUSED */
83 void *
84 be_thread(void *foo)
85 {
86 	char *p;
87 
88 	if (iwin) {
89 		printf("why am I here\n");
90 		return;
91 	}
92 
93 	if ((p = malloc(rand())) == NULL) {
94 		iwin = 1;
95 		fprintf(stderr, "Errno is %d\n", errno);
96 		perror("malloc");
97 	} else {
98 		be_thread(NULL);
99 		printf("free %p\n", p);
100 		free(p);
101 	}
102 
103 }
104