コード例 #1
0
int main(int argc, char* argv[])
{
    size_t size = ( argc>1 ? atoi(argv[1]) : getpagesize() ); 
    printf("size = %ld \n", (long)size);

#if defined(POSIX_SHM)
    int fd = shm_open("./bar", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR );
    if (fd<0) printf("shm_open failed: %d \n", fd);
    else      printf("shm_open succeeded: %d \n", fd);
#elif defined(DEV_SHM)
    int fd = open("/dev/shm/foo", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR );
    if (fd<0) printf("open failed: %d \n", fd);
    else      printf("open succeeded: %d \n", fd);
#else
    int fd = -1;
    printf("no file backing \n");
#endif

    if (fd>=0)
    {
        int rc = ftruncate(fd, size);
        if (rc==0) printf("ftruncate succeeded \n");
        else       printf("ftruncate failed \n");
    }

#if defined(__bgp__) && defined(BGP_SMP_SHM_BROKEN)
    void * ptr = NULL;
    _BGP_Personality_t pers;
    Kernel_GetPersonality(&pers, sizeof(pers));
    if( BGP_Personality_processConfig(&pers) == _BGP_PERS_PROCESSCONFIG_SMP )
    {
        printf("SMP mode => MAP_PRIVATE | MAP_ANONYMOUS \n");
        ptr = mmap( NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, fd, 0 );
    }
    else
        ptr = mmap( NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
#else
    void * ptr = mmap( NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
#endif
    if (ptr==NULL) printf("mmap failed \n");
    else           printf("mmap succeeded \n");
    
    printf("trying memset \n");
    memset(ptr, '\0', size);
    printf("memset succeeded \n");

    if (fd>=0)
    {
        int rc = ftruncate(fd, 0);
        if (rc==0) printf("ftruncate succeeded \n");
        else       printf("ftruncate failed \n");
    }

#if defined(POSIX_SHM)
    if (fd>=0)
    {
        int rc = shm_unlink("./bar");
        if (rc==0) printf("shm_unlink succeeded \n");
        else       printf("shm_unlink failed \n");
    }
#elif defined(DEV_SHM)
    if (fd>=0)
    {
        int rc = close(fd);
        if (rc==0) printf("close succeeded \n");
        else       printf("close failed \n");
    }
#endif

    if (ptr!=NULL)
    {
        int rc = munmap(ptr, size);
        if (rc==0) printf("munmap succeeded \n");
        else       printf("munmap failed \n");
    }

    return 0;
}
コード例 #2
0
ファイル: GPTLget_memusage.c プロジェクト: ACME-Climate/cime
int GPTLget_memusage (int *size, int *rss, int *share, int *text, int *datastack)
{
#if defined (BGP) || defined(BGQ)

  long long alloc;
  struct mallinfo m;
#if defined (BGP)
  Personality pers;
#endif
#if defined (BGQ)
  uint64_t shared_mem_count;
#endif
  long long total;
  int node_config;
 
 /* memory available */
#if defined(BGP)
  Kernel_GetPersonality(&pers, sizeof(pers));
  total = BGP_Personality_DDRSizeMB(&pers);

  node_config  = BGP_Personality_processConfig(&pers);
  if (node_config == _BGP_PERS_PROCESSCONFIG_VNM) total /= 4;
  else if (node_config == _BGP_PERS_PROCESSCONFIG_2x2) total /= 2;
  total *= 1024*1024;

  *size = total;
#endif

#if defined(BGQ)
  Kernel_GetMemorySize(KERNEL_MEMSIZE_SHARED, &shared_mem_count);

  shared_mem_count *= 1024*1024;
  *size = shared_mem_count;

#endif
  /* total memory used  - heap only (not static memory)*/

  m = mallinfo();
  alloc = m.hblkhd + m.uordblks;

  *rss = alloc;
  *share     = -1;
  *text     = -1;
  *datastack = -1;


#elif (defined HAVE_SLASHPROC)
  FILE *fd;                       /* file descriptor for fopen */
  int pid;                        /* process id */
  static char *head = "/proc/";   /* part of path */
  static char *tail = "/statm";   /* part of path */
  char file[19];                  /* full path to file in /proc */
  int dum;                        /* placeholder for unused return arguments */
  int ret;                        /* function return value */

  /*
  ** The file we want to open is /proc/<pid>/statm
  */

  pid = (int) getpid ();
  if (pid > 999999) {
    fprintf (stderr, "get_memusage: pid %d is too large\n", pid);
    return -1;
  }

  sprintf (file, "%s%d%s", head, pid, tail);
  if ((fd = fopen (file, "r")) < 0) {
    fprintf (stderr, "get_memusage: bad attempt to open %s\n", file);
    return -1;
  }

  /*
  ** Read the desired data from the /proc filesystem directly into the output
  ** arguments, close the file and return.
  */

  ret = fscanf (fd, "%d %d %d %d %d %d %d", 
		size, rss, share, text, datastack, &dum, &dum);
  ret = fclose (fd);
  return 0;

#elif (defined __APPLE__)

  FILE *fd;
  char cmd[60];  
  int pid = (int) getpid ();
  
  sprintf (cmd, "ps -o vsz -o rss -o tsiz -p %d | grep -v RSS", pid);
  fd = popen (cmd, "r");

  if (fd) {
    fscanf (fd, "%d %d %d", size, rss, text);
    *share     = -1;
    *datastack = -1;
    (void) pclose (fd);
  }

  return 0;

#else

  struct rusage usage;         /* structure filled in by getrusage */

  if (getrusage (RUSAGE_SELF, &usage) < 0)
    return -1;
  
  *size      = -1;
  *rss       = usage.ru_maxrss;
  *share     = -1;
  *text      = -1;
  *datastack = -1;
#ifdef IRIX64
  *datastack = usage.ru_idrss + usage.ru_isrss;
#endif
  return 0;

#endif
}