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 #include <stdio.h>
28 #include "mtmalloc.h"
29 #include <unistd.h>
30 #include <thread.h>
31 #include <sys/types.h>
32 #include <sys/mman.h>
33 #include <fcntl.h>
34 #include <errno.h>
35
36 /*
37 * This file tests for swap space exhaustion
38 *
39 * cc -O -o exhaust exhaust.c -lmtmalloc
40 */
41
42 void * be_thread(void *);
43 int iwin = 0;
44
main(int argc,char ** argv)45 main(int argc, char ** argv)
46 {
47 int ncpus;
48 thread_t tid[512];
49 int fd;
50 caddr_t stacks[512];
51
52 srand(getpid());
53 ncpus = sysconf(_SC_NPROCESSORS_CONF);
54
55 fd = open("/dev/zero", O_RDONLY);
56
57 if (fd < 0) {
58 perror("open");
59 exit(-1);
60 }
61
62 while (ncpus--)
63 stacks[ncpus] = mmap(0, 1<<23, PROT_READ|PROT_WRITE,
64 MAP_PRIVATE, fd, 0);
65
66 close(fd);
67
68 mallocctl(MTCHUNKSIZE, 150);
69
70 ncpus = sysconf(_SC_NPROCESSORS_CONF);
71 while (ncpus--)
72 thr_create(stacks[ncpus], 1<<23, be_thread, NULL, THR_BOUND,
73 &tid[ncpus]);
74
75 while (thr_join(NULL, NULL, NULL) == 0);
76
77 exit(0);
78 }
79
80 /* ARGSUSED */
81 void *
be_thread(void * foo)82 be_thread(void *foo)
83 {
84 char *p;
85
86 if (iwin) {
87 printf("why am I here\n");
88 return;
89 }
90
91 if ((p = malloc(rand())) == NULL) {
92 iwin = 1;
93 fprintf(stderr, "Errno is %d\n", errno);
94 perror("malloc");
95 } else {
96 be_thread(NULL);
97 printf("free %p\n", p);
98 free(p);
99 }
100
101 }
102