/* * vmem_init -- initialization for vmem * * Called automatically by the run-time loader or on the first use of vmem. */ void vmem_init(void) { static bool initialized = false; static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; int oerrno; if (initialized) return; if ((errno = pthread_mutex_lock(&lock))) FATAL("!pthread_mutex_lock"); if (!initialized) { out_init(VMEM_LOG_PREFIX, VMEM_LOG_LEVEL_VAR, VMEM_LOG_FILE_VAR, VMEM_MAJOR_VERSION, VMEM_MINOR_VERSION); out_set_vsnprintf_func(je_vmem_navsnprintf); LOG(3, NULL); util_init(); Header_size = roundup(sizeof (VMEM), Pagesize); /* Set up jemalloc messages to a custom print function */ je_vmem_malloc_message = print_jemalloc_messages; initialized = true; } oerrno = errno; if ((errno = pthread_mutex_unlock(&lock))) ERR("!pthread_mutex_unlock"); errno = oerrno; }
int main(int argc, char *argv[]) { START(argc, argv, "traces_custom_function"); if (argc != 2) FATAL("usage: %s [v|p]", argv[0]); out_set_print_func(print_custom_function); out_init(LOG_PREFIX, LOG_LEVEL_VAR, LOG_FILE_VAR, MAJOR_VERSION, MINOR_VERSION); switch (argv[1][0]) { case 'p': { LOG(0, "Log level NONE"); LOG(1, "Log level ERROR"); LOG(2, "Log level WARNING"); LOG(3, "Log level INFO"); LOG(4, "Log level DEBUG"); } break; case 'v': out_set_vsnprintf_func(vsnprintf_custom_function); LOG(0, "no format"); LOG(0, "pointer: %p", (void *)0x12345678); LOG(0, "string: %s", "Hello world!"); LOG(0, "number: %u", 12345678); errno = EINVAL; LOG(0, "!error"); break; default: FATAL("usage: %s [v|p]", argv[0]); } /* Cleanup */ out_fini(); DONE(NULL); }
static void libvmmalloc_init(void) { char *env_str; size_t size; /* * Register fork handlers before jemalloc initialization. * This provides the correct order of fork handlers execution. * Note that the first malloc() will trigger jemalloc init, so we * have to register fork handlers before the call to out_init(), * as it may indirectly call malloc() when opening the log file. */ if (pthread_atfork(libvmmalloc_prefork, libvmmalloc_postfork_parent, libvmmalloc_postfork_child) != 0) { perror("Error (libvmmalloc): pthread_atfork"); abort(); } out_init(VMMALLOC_LOG_PREFIX, VMMALLOC_LOG_LEVEL_VAR, VMMALLOC_LOG_FILE_VAR, VMMALLOC_MAJOR_VERSION, VMMALLOC_MINOR_VERSION); out_set_vsnprintf_func(je_vmem_navsnprintf); LOG(3, NULL); util_init(); /* set up jemalloc messages to a custom print function */ je_vmem_malloc_message = print_jemalloc_messages; Header_size = roundup(sizeof (VMEM), Pagesize); if ((Dir = getenv(VMMALLOC_POOL_DIR_VAR)) == NULL) { out_log(NULL, 0, NULL, 0, "Error (libvmmalloc): " "environment variable %s not specified", VMMALLOC_POOL_DIR_VAR); abort(); } if ((env_str = getenv(VMMALLOC_POOL_SIZE_VAR)) == NULL) { out_log(NULL, 0, NULL, 0, "Error (libvmmalloc): " "environment variable %s not specified", VMMALLOC_POOL_SIZE_VAR); abort(); } else { long long int v = atoll(env_str); if (v < 0) { out_log(NULL, 0, NULL, 0, "Error (libvmmalloc): negative %s", VMMALLOC_POOL_SIZE_VAR); abort(); } size = (size_t)v; } if (size < VMMALLOC_MIN_POOL) { out_log(NULL, 0, NULL, 0, "Error (libvmmalloc): " "%s value is less than minimum (%zu < %zu)", VMMALLOC_POOL_SIZE_VAR, size, VMMALLOC_MIN_POOL); abort(); } if ((env_str = getenv(VMMALLOC_FORK_VAR)) != NULL) { Forkopt = atoi(env_str); if (Forkopt < 0 || Forkopt > 3) { out_log(NULL, 0, NULL, 0, "Error (libvmmalloc): " "incorrect %s value (%d)", VMMALLOC_FORK_VAR, Forkopt); abort(); } LOG(4, "Fork action %d", Forkopt); } /* * XXX - vmem_create() could be used here, but then we need to * link vmem.o, including all the vmem API. */ Vmp = libvmmalloc_create(Dir, size); if (Vmp == NULL) { out_log(NULL, 0, NULL, 0, "!Error (libvmmalloc): " "vmem pool creation failed"); abort(); } LOG(2, "initialization completed"); }