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
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.
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]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 * Portions Copyright 2006 OmniTI, Inc.
30 /* #pragma ident "@(#)envvar.c 1.5 05/06/08 SMI" */
42 #include "umem_base.h"
43 #include "vmem_base.h"
46 * A umem environment variable, like UMEM_DEBUG, is set to a series
47 * of items, seperated by ',':
49 * UMEM_DEBUG="audit=10,guards,firewall=512"
51 * This structure describes items. Each item has a name, type, and
52 * description. During processing, an item read from the user may
53 * be either "valid" or "invalid".
55 * A valid item has an argument, if required, and it is of the right
56 * form (doesn't overflow, doesn't contain any unexpected characters).
58 * If the item is valid, item_flag_target != NULL, and:
59 * type is not CLEARFLAG, then (*item_flag_target) |= item_flag_value
60 * type is CLEARFLAG, then (*item_flag_target) &= ~item_flag_value
63 #define UMEM_ENV_ITEM_MAX 512
67 typedef int arg_process_t(const struct umem_env_item
*item
, const char *value
);
68 #define ARG_SUCCESS 0 /* processing successful */
69 #define ARG_BAD 1 /* argument had a bad value */
71 typedef struct umem_env_item
{
72 const char *item_name
; /* tag in environment variable */
73 const char *item_interface_stability
;
76 ITEM_FLAG
, /* only a flag. No argument allowed */
77 ITEM_CLEARFLAG
, /* only a flag, but clear instead of set */
78 ITEM_OPTUINT
, /* optional integer argument */
79 ITEM_UINT
, /* required integer argument */
80 ITEM_OPTSIZE
, /* optional size_t argument */
81 ITEM_SIZE
, /* required size_t argument */
82 ITEM_SPECIAL
/* special argument processing */
84 const char *item_description
;
85 uint_t
*item_flag_target
; /* the variable containing the flag */
86 uint_t item_flag_value
; /* the value to OR in */
87 uint_t
*item_uint_target
; /* the variable to hold the integer */
88 size_t *item_size_target
;
89 arg_process_t
*item_special
; /* callback for special handling */
92 #ifndef UMEM_STANDALONE
93 static arg_process_t umem_backend_process
;
96 static arg_process_t umem_log_process
;
98 const char *____umem_environ_msg_options
= "-- UMEM_OPTIONS --";
100 static umem_env_item_t umem_options_items
[] = {
101 #ifndef UMEM_STANDALONE
102 { "backend", "Evolving", ITEM_SPECIAL
,
103 "=sbrk for sbrk(2), =mmap for mmap(2)",
105 &umem_backend_process
109 { "concurrency", "Private", ITEM_UINT
,
111 NULL
, 0, &umem_max_ncpus
113 { "max_contention", "Private", ITEM_UINT
,
114 "Maximum contention in a reap interval before the depot is "
116 NULL
, 0, &umem_depot_contention
118 { "nomagazines", "Private", ITEM_FLAG
,
119 "no caches will be multithreaded, and no caching will occur.",
120 &umem_flags
, UMF_NOMAGAZINE
122 { "reap_interval", "Private", ITEM_UINT
,
123 "Minimum time between reaps and updates, in seconds.",
124 NULL
, 0, &umem_reap_interval
128 #ifndef UMEM_STANDALONE
129 { "sbrk_pagesize", "Private", ITEM_SIZE
,
130 "The preferred page size for the sbrk(2) heap.",
131 NULL
, 0, NULL
, &vmem_sbrk_pagesize
136 { NULL
, "-- end of UMEM_OPTIONS --", ITEM_INVALID
}
139 const char *____umem_environ_msg_debug
= "-- UMEM_DEBUG --";
141 static umem_env_item_t umem_debug_items
[] = {
142 { "default", "Unstable", ITEM_FLAG
,
143 "audit,contents,guards",
145 UMF_AUDIT
| UMF_CONTENTS
| UMF_DEADBEEF
| UMF_REDZONE
147 { "audit", "Unstable", ITEM_OPTUINT
,
148 "Enable auditing. optionally =frames to set the number of "
149 "stored stack frames",
150 &umem_flags
, UMF_AUDIT
, &umem_stack_depth
152 { "contents", "Unstable", ITEM_OPTSIZE
,
153 "Enable contents storing. UMEM_LOGGING=contents also "
154 "required. optionally =bytes to set the number of stored "
156 &umem_flags
, UMF_CONTENTS
, NULL
, &umem_content_maxsave
158 { "guards", "Unstable", ITEM_FLAG
,
159 "Enables guards and special patterns",
160 &umem_flags
, UMF_DEADBEEF
| UMF_REDZONE
162 { "verbose", "Unstable", ITEM_FLAG
,
163 "Enables writing error messages to stderr",
167 { "nosignal", "Private", ITEM_FLAG
,
168 "Abort if called from a signal handler. Turns on 'audit'. "
169 "Note that this is not always a bug.",
170 &umem_flags
, UMF_AUDIT
| UMF_CHECKSIGNAL
172 { "firewall", "Private", ITEM_SIZE
,
173 "=minbytes. Every object >= minbytes in size will have its "
174 "end against an unmapped page",
175 &umem_flags
, UMF_FIREWALL
, NULL
, &umem_minfirewall
177 { "lite", "Private", ITEM_FLAG
,
179 &umem_flags
, UMF_LITE
181 { "maxverify", "Private", ITEM_SIZE
,
182 "=maxbytes, Maximum bytes to check when 'guards' is active. "
183 "Normally all bytes are checked.",
184 NULL
, 0, NULL
, &umem_maxverify
186 { "noabort", "Private", ITEM_CLEARFLAG
,
187 "umem will not abort when a recoverable error occurs "
188 "(i.e. double frees, certain kinds of corruption)",
191 { "mtbf", "Private", ITEM_UINT
,
192 "=mtbf, the mean time between injected failures. Works best "
196 { "random", "Private", ITEM_FLAG
,
197 "randomize flags on a per-cache basis",
198 &umem_flags
, UMF_RANDOMIZE
200 { "allverbose", "Private", ITEM_FLAG
,
201 "Enables writing all logged messages to stderr",
205 { NULL
, "-- end of UMEM_DEBUG --", ITEM_INVALID
}
208 const char *____umem_environ_msg_logging
= "-- UMEM_LOGGING --";
210 static umem_env_item_t umem_logging_items
[] = {
211 { "transaction", "Unstable", ITEM_SPECIAL
,
212 "If 'audit' is set in UMEM_DEBUG, the audit structures "
213 "from previous transactions are entered into this log.",
215 &umem_transaction_log_size
, &umem_log_process
217 { "contents", "Unstable", ITEM_SPECIAL
,
218 "If 'audit' is set in UMEM_DEBUG, the contents of objects "
219 "are recorded in this log as they are freed. If the "
220 "'contents' option is not set in UMEM_DEBUG, the first "
221 "256 bytes of each freed buffer will be saved.",
222 &umem_flags
, UMF_CONTENTS
, NULL
,
223 &umem_content_log_size
, &umem_log_process
225 { "fail", "Unstable", ITEM_SPECIAL
,
226 "Records are entered into this log for every failed "
229 &umem_failure_log_size
, &umem_log_process
232 { "slab", "Private", ITEM_SPECIAL
,
233 "Every slab created will be entered into this log.",
235 &umem_slab_log_size
, &umem_log_process
238 { NULL
, "-- end of UMEM_LOGGING --", ITEM_INVALID
}
241 typedef struct umem_envvar
{
242 const char *env_name
;
243 const char *env_func
;
244 umem_env_item_t
*env_item_list
;
245 const char *env_getenv_result
;
246 const char *env_func_result
;
249 static umem_envvar_t umem_envvars
[] = {
250 { "UMEM_DEBUG", "_umem_debug_init", umem_debug_items
},
251 { "UMEM_OPTIONS", "_umem_options_init", umem_options_items
},
252 { "UMEM_LOGGING", "_umem_logging_init", umem_logging_items
},
256 static umem_envvar_t
*env_current
;
257 #define CURRENT (env_current->env_name)
260 empty(const char *str
)
264 while ((c
= *str
) != '\0' && isspace(c
))
267 return (*str
== '\0');
271 item_uint_process(const umem_env_item_t
*item
, const char *item_arg
)
280 if (empty(item_arg
)) {
284 result
= strtoul(item_arg
, &endptr
, 10);
286 if (result
== ULONG_MAX
&& errno
== ERANGE
) {
294 if ((uint_t
)result
!= result
)
297 (*item
->item_uint_target
) = (uint_t
)result
;
298 return (ARG_SUCCESS
);
301 log_message("%s: %s: not a number\n", CURRENT
, item
->item_name
);
305 log_message("%s: %s: overflowed\n", CURRENT
, item
->item_name
);
310 item_size_process(const umem_env_item_t
*item
, const char *item_arg
)
323 result_arg
= strtoul(item_arg
, &endptr
, 10);
325 if (result_arg
== ULONG_MAX
&& errno
== ERANGE
) {
337 if (result
< result_arg
)
343 if (result
< result_arg
)
349 if (result
< result_arg
)
355 if (result
< result_arg
)
357 endptr
++; /* skip over the size character */
360 break; /* handled later */
366 (*item
->item_size_target
) = result
;
367 return (ARG_SUCCESS
);
370 log_message("%s: %s: not a number\n", CURRENT
, item
->item_name
);
374 log_message("%s: %s: overflowed\n", CURRENT
, item
->item_name
);
379 umem_log_process(const umem_env_item_t
*item
, const char *item_arg
)
381 if (item_arg
!= NULL
) {
383 ret
= item_size_process(item
, item_arg
);
384 if (ret
!= ARG_SUCCESS
)
387 if (*item
->item_size_target
== 0)
388 return (ARG_SUCCESS
);
390 *item
->item_size_target
= 64*1024;
393 return (ARG_SUCCESS
);
396 #ifndef UMEM_STANDALONE
398 umem_backend_process(const umem_env_item_t
*item
, const char *item_arg
)
400 const char *name
= item
->item_name
;
402 if (item_arg
== NULL
)
405 if (strcmp(item_arg
, "sbrk") == 0)
406 vmem_backend
|= VMEM_BACKEND_SBRK
;
407 else if (strcmp(item_arg
, "mmap") == 0)
408 vmem_backend
|= VMEM_BACKEND_MMAP
;
412 return (ARG_SUCCESS
);
415 log_message("%s: %s: must be %s=sbrk or %s=mmap\n",
416 CURRENT
, name
, name
, name
);
422 process_item(const umem_env_item_t
*item
, const char *item_arg
)
424 int arg_required
= 0;
425 arg_process_t
*processor
;
427 switch (item
->item_type
) {
442 log_message("%s: %s: Invalid type. Ignored\n",
443 CURRENT
, item
->item_name
);
447 switch (item
->item_type
) {
450 if (item_arg
!= NULL
) {
451 log_message("%s: %s: does not take a value. ignored\n",
452 CURRENT
, item
->item_name
);
460 processor
= item_uint_process
;
465 processor
= item_size_process
;
469 processor
= item
->item_special
;
473 log_message("%s: %s: Invalid type. Ignored\n",
474 CURRENT
, item
->item_name
);
478 if (arg_required
&& item_arg
== NULL
) {
479 log_message("%s: %s: Required value missing\n",
480 CURRENT
, item
->item_name
);
484 if (item_arg
!= NULL
|| item
->item_type
== ITEM_SPECIAL
) {
485 if (processor(item
, item_arg
) != ARG_SUCCESS
)
489 if (item
->item_flag_target
) {
490 if (item
->item_type
== ITEM_CLEARFLAG
)
491 (*item
->item_flag_target
) &= ~item
->item_flag_value
;
493 (*item
->item_flag_target
) |= item
->item_flag_value
;
501 #define ENV_SHORT_BYTES 10 /* bytes to print on error */
503 umem_process_value(umem_env_item_t
*item_list
, const char *beg
, const char *end
)
505 char buf
[UMEM_ENV_ITEM_MAX
];
510 while (beg
< end
&& isspace(*beg
))
513 while (beg
< end
&& isspace(*(end
- 1)))
517 log_message("%s: empty option\n", CURRENT
);
523 if (count
+ 1 > sizeof (buf
)) {
524 char outbuf
[ENV_SHORT_BYTES
+ 1];
526 * Have to do this, since sprintf("%10s",...) calls malloc()
528 (void) strncpy(outbuf
, beg
, ENV_SHORT_BYTES
);
529 outbuf
[ENV_SHORT_BYTES
] = 0;
531 log_message("%s: argument \"%s...\" too long\n", CURRENT
,
536 (void) strncpy(buf
, beg
, count
);
539 argptr
= strchr(buf
, '=');
544 for (; item_list
->item_name
!= NULL
; item_list
++) {
545 if (strcmp(buf
, item_list
->item_name
) == 0) {
546 (void) process_item(item_list
, argptr
);
550 log_message("%s: '%s' not recognized\n", CURRENT
, buf
);
555 umem_setup_envvars(int invalid
)
557 umem_envvar_t
*cur_env
;
558 static volatile enum {
564 } state
= STATE_START
;
565 #ifndef UMEM_STANDALONE
572 * One of the calls below invoked malloc() recursively. We
573 * remove any partial results and return.
578 where
= "before getenv(3C) calls -- "
579 "getenv(3C) results ignored.";
582 where
= "during getenv(3C) calls -- "
583 "getenv(3C) results ignored.";
586 where
= "during dlsym(3C) call -- "
587 "_umem_*() results ignored.";
590 where
= "during _umem_*() call -- "
591 "_umem_*() results ignored.";
594 where
= "after dlsym() or _umem_*() calls.";
597 where
= "at unknown point -- "
598 "_umem_*() results ignored.";
602 log_message("recursive allocation %s\n", where
);
604 for (cur_env
= umem_envvars
; cur_env
->env_name
!= NULL
;
606 if (state
== STATE_GETENV
)
607 cur_env
->env_getenv_result
= NULL
;
608 if (state
!= STATE_DONE
)
609 cur_env
->env_func_result
= NULL
;
616 state
= STATE_GETENV
;
618 for (cur_env
= umem_envvars
; cur_env
->env_name
!= NULL
; cur_env
++) {
619 cur_env
->env_getenv_result
= getenv(cur_env
->env_name
);
620 if (state
== STATE_DONE
)
621 return; /* recursed */
624 #ifndef UMEM_STANDALONE
626 # define dlopen(a, b) GetModuleHandle(NULL)
627 # define dlsym(a, b) GetProcAddress((HANDLE)a, b)
628 # define dlclose(a) 0
631 /* get a handle to the "a.out" object */
632 if ((h
= dlopen(0, RTLD_FIRST
| RTLD_LAZY
)) != NULL
) {
633 for (cur_env
= umem_envvars
; cur_env
->env_name
!= NULL
;
635 const char *(*func
)(void);
639 func
= (const char *(*)(void))dlsym(h
,
642 if (state
== STATE_DONE
)
643 break; /* recursed */
648 if (state
== STATE_DONE
)
649 break; /* recursed */
650 cur_env
->env_func_result
= value
;
655 (void) dlerror(); /* snarf dlerror() */
657 #endif /* UMEM_STANDALONE */
663 * Process the environment variables.
666 umem_process_envvars(void)
669 const char *end
, *next
;
670 umem_envvar_t
*cur_env
;
672 for (cur_env
= umem_envvars
; cur_env
->env_name
!= NULL
; cur_env
++) {
673 env_current
= cur_env
;
675 value
= cur_env
->env_getenv_result
;
677 value
= cur_env
->env_func_result
;
679 /* ignore if missing or empty */
683 for (end
= value
; *end
!= '\0'; value
= next
) {
684 end
= strchr(value
, ',');
686 next
= end
+ 1; /* skip the comma */
688 next
= end
= value
+ strlen(value
);
690 umem_process_value(cur_env
->env_item_list
, value
, end
);