Changeset 56

Show
Ignore:
Timestamp:
04/30/10 21:27:09 (5 years ago)
Author:
wez
Message:
  • allow the sbrk backend to be requested via UMEM_OPTIONS=backend=sbrk, but note that this appears to be broken on non-solaris platforms.
  • make a safer/better check for number of cpus on linux
  • use pthread_once to register the forkhandler, as it is possible to call it twice in some scenarios without this protection
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/init_lib.c

    r49 r56  
    5757#ifdef _WIN32 
    5858        vmem_backend = VMEM_BACKEND_MMAP; 
     59        (void) vmem_sbrk_arena(NULL, NULL); 
    5960#else 
    60 #if 0 
     61# if defined(sun) 
    6162        void *handle = dlopen("libmapmalloc.so.1", RTLD_NOLOAD); 
    6263 
    6364        if (handle != NULL) { 
    64 #endif 
    6565                log_message("sbrk backend disabled\n"); 
    6666                vmem_backend = VMEM_BACKEND_MMAP; 
    67 #if 0 
    6867        } 
    69 #endif 
    70 #endif 
     68# else 
     69        if (vmem_backend == 0) { 
     70                /* prefer mmap, as sbrk() seems to have problems wither working 
     71                 * with other allocators or has some Solaris specific assumptions. */ 
     72                vmem_backend = VMEM_BACKEND_MMAP; 
     73        } 
     74# endif 
    7175 
    7276        if ((vmem_backend & VMEM_BACKEND_MMAP) != 0) { 
     
    7478                (void) vmem_mmap_arena(NULL, NULL); 
    7579        } else { 
    76 #ifndef _WIN32 
    7780                vmem_backend = VMEM_BACKEND_SBRK; 
    7881                (void) vmem_sbrk_arena(NULL, NULL); 
     82        } 
    7983#endif 
    80         } 
    8184} 
    8285 
     
    105108   * version of libumem, read /proc/stat into a static buffer. 
    106109   */ 
    107   static char proc_stat[8192]; 
    108   int fd; 
    109   int ncpus = 1; 
     110  static int ncpus = 0; 
    110111 
    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; 
     112  if (ncpus == 0) { 
     113    char proc_stat[8192]; 
     114    int fd; 
    117115 
    118       proc_stat[n] = '\0'; 
    119       cur = proc_stat; 
    120       while (*cur && (next = strstr(cur + 3, "cpu"))) { 
    121         cur = next; 
     116    ncpus = 1; 
     117    fd = open("/proc/stat", O_RDONLY); 
     118    if (fd >= 0) { 
     119      const ssize_t n = read(fd, proc_stat, sizeof(proc_stat) - 1); 
     120      if (n >= 0) { 
     121        const char *cur; 
     122        const char *next; 
     123 
     124        proc_stat[n] = '\0'; 
     125        cur = proc_stat; 
     126        while (*cur && (next = strstr(cur + 3, "cpu"))) { 
     127          cur = next; 
     128        } 
     129 
     130        if (*cur) 
     131          ncpus = atoi(cur + 3) + 1; 
    122132      } 
    123133 
    124       if (*cur) 
    125         ncpus = atoi(cur + 3) + 1; 
     134      close(fd); 
    126135    } 
    127  
    128     close(fd); 
    129136  } 
    130137 
  • trunk/umem.c

    r52 r56  
    622622caddr_t                 umem_max_stack; 
    623623 
     624#ifndef UMEM_STANDALONE 
     625static pthread_once_t umem_forkhandler_once = PTHREAD_ONCE_INIT; 
     626#endif 
    624627 
    625628/* 
     
    29082911        /* Standalone doesn't fork */ 
    29092912#else 
    2910         umem_forkhandler_init(); /* register the fork handler */ 
     2913        /* register the fork handler */ 
     2914        (void) pthread_once(&umem_forkhandler_once, umem_forkhandler_init); 
    29112915#endif 
    29122916