Restructure autoconf around ./config directory
[zfs.git] / zfs / lib / libumem / init_lib.c
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 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * Portions Copyright 2006 OmniTI, Inc.
28  */
29
30 /* #pragma ident        "@(#)init_lib.c 1.2     05/06/08 SMI" */
31
32 /*
33  * Initialization routines for the library version of libumem.
34  */
35
36 #include "config.h"
37 #include "umem_base.h"
38 #include "vmem_base.h"
39
40 #if HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43 #if HAVE_DLFCN_H
44 #include <dlfcn.h>
45 #endif
46
47 #include <fcntl.h>
48 #include <string.h>
49
50 #ifdef __FreeBSD__
51 #include <machine/param.h>
52 #endif
53
54 void
55 vmem_heap_init(void)
56 {
57 #ifdef _WIN32
58         vmem_backend = VMEM_BACKEND_MMAP;
59 #else
60 #if 0
61         void *handle = dlopen("libmapmalloc.so.1", RTLD_NOLOAD);
62
63         if (handle != NULL) {
64 #endif
65                 log_message("sbrk backend disabled\n");
66                 vmem_backend = VMEM_BACKEND_MMAP;
67 #if 0
68         }
69 #endif
70 #endif
71
72         if ((vmem_backend & VMEM_BACKEND_MMAP) != 0) {
73                 vmem_backend = VMEM_BACKEND_MMAP;
74                 (void) vmem_mmap_arena(NULL, NULL);
75         } else {
76 #ifndef _WIN32
77                 vmem_backend = VMEM_BACKEND_SBRK;
78                 (void) vmem_sbrk_arena(NULL, NULL);
79 #endif
80         }
81 }
82
83 /*ARGSUSED*/
84 void
85 umem_type_init(caddr_t start, size_t len, size_t pgsize)
86 {
87 #ifdef _WIN32
88         SYSTEM_INFO info;
89         GetSystemInfo(&info);
90         pagesize = info.dwPageSize;
91 #elif !defined(__FreeBSD__)
92         pagesize = _sysconf(_SC_PAGESIZE);
93 #else
94         pagesize = PAGE_SIZE;
95 #endif
96 }
97
98 int
99 umem_get_max_ncpus(void)
100 {
101 #ifdef linux
102   /*
103    * HACK: sysconf() will invoke malloc() on Linux as part of reading
104    * in /proc/stat. To avoid recursion in the malloc replacement
105    * version of libumem, read /proc/stat into a static buffer.
106    */
107   static char proc_stat[8192];
108   int fd;
109   int ncpus = 1;
110
111   fd = open("/proc/stat", O_RDONLY);
112   if (fd >= 0) {
113     const ssize_t n = read(fd, proc_stat, sizeof(proc_stat) - 1);
114     if (n >= 0) {
115       const char *cur;
116       const char *next;
117
118       proc_stat[n] = '\0';
119       cur = proc_stat;
120       while (*cur && (next = strstr(cur + 3, "cpu"))) {
121         cur = next;
122       }
123
124       if (*cur)
125         ncpus = atoi(cur + 3) + 1;
126     }
127
128     close(fd);
129   }
130
131   return ncpus;
132
133 #else /* !linux */
134
135 #if _SC_NPROCESSORS_ONLN
136   return (2 * sysconf(_SC_NPROCESSORS_ONLN));
137 #elif defined(_SC_NPROCESSORS_CONF)
138   return (2 * sysconf(_SC_NPROCESSORS_CONF));
139 #elif defined(_WIN32)
140   SYSTEM_INFO info;
141   GetSystemInfo(&info);
142   return info.dwNumberOfProcessors;
143 #else
144   /* XXX: determine CPU count on other platforms */
145   return (1);
146 #endif
147
148 #endif /* linux */
149 }