What's the "real size" of a running process? - HP UX

This is a discussion on What's the "real size" of a running process? - HP UX ; I'm looking for help on how how to tell each time the "real size" of the process crosses a user-defined limit. By "real size", I mean the size of the process including all malloc'ed memory minus the size of memory ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: What's the "real size" of a running process?

  1. What's the "real size" of a running process?

    I'm looking for help on how how to tell each time the "real size" of
    the process crosses a user-defined limit. By "real size", I mean the
    size of the process including all malloc'ed memory minus the size of
    memory that was free'd.

    When a process mallocs some memory, the process size increases, as is
    shown by "ps -o vsz". As that memory begins to be used, pages get
    accounted from pst_vdsize/pst_vtsize/pst_vssize elements of the
    pst_status structure to the pst_dsize/pst_tsize/pst_ssize elements.

    If that process then successfully frees the memory that it had
    malloc'ed earlier, my test show that those pst_status elements don't
    change. So how can I find what the "real size" of the process now is?

    I know that the memory is not released back to the system, and so the
    next malloc (of a size not exceeding what was freed) will not increase
    the size of the process, so the "real size" of the process must've come
    down after the free.

    Thanks in advance.


  2. Re: What's the "real size" of a running process?

    I see the same problem with Solaris too.

    This is my test program, foo.c. I compiled it on HP-UX B.11.11 with
    "cc +DA2.0W foo.c" and on SunOS 5.8 with "CC -xarch=v9 foo.c".

    #include
    #include
    #include
    #include
    #include
    #if defined(__sun)
    # include
    # include
    # include
    #elif defined(__hpux)
    # include
    # include
    # include
    # include
    # include
    # include
    #endif
    #include


    void showPageSize()
    {
    #if defined(__sun)
    printf("Page size = %d\n", getpagesize());
    #elif defined(__hpux)
    struct pst_static pst;
    if (pstat_getstatic(&pst, sizeof(pst), (size_t)1, 0) != -1)
    {
    pstat_getstatic(&pst, sizeof(pst), (size_t)1, 0);
    printf("Page size = %d\n", pst.page_size);
    printf("Physical Memory size = %d\n", pst.physical_memory);
    }
    else
    {
    perror("pstat_getstatic");
    }
    #endif
    }

    void getProcSize()
    {
    uint64_t procSize;
    #if defined(__sun)
    int fd = -1;
    char fname[80];

    snprintf(fname, sizeof(fname), "/proc/%d/psinfo", getpid());

    if ((fd = open(fname, O_RDONLY)) >= 0) {
    psinfo_t currproc;

    if (read(fd, &currproc, sizeof(psinfo_t)) == sizeof(psinfo_t)) {
    procSize = currproc.pr_size;
    }

    close (fd);
    }
    #elif defined(__hpux)
    struct pst_status pss;

    if (pstat_getproc(&pss, sizeof(pss), (size_t)0, getpid()) != -1) {
    procSize = ((pss.pst_dsize + pss.pst_tsize + pss.pst_ssize) *
    4096);
    printf("real %d %d %d\n", pss.pst_dsize, pss.pst_tsize,
    pss.pst_ssize);
    printf("virtual %d %d %d\n", pss.pst_vdsize, pss.pst_vtsize,
    pss.pst_vssize);
    }
    else
    {
    perror("pstat_getproc");
    }
    printf("proc size (bytes) = %lld\n", procSize);
    #endif
    }

    void maxMemoryLimit()
    {
    struct rlimit rl;
    #if defined(__hpux)
    uint64_t dssmax;
    #endif

    getrlimit(RLIMIT_AS, &rl);
    if (RLIM_INFINITY != rl.rlim_cur) {
    printf("RLIMIT_AS rl.rlim_cur = %ld\n", rl.rlim_cur);
    }

    getrlimit(RLIMIT_DATA, &rl);
    if (RLIM_INFINITY != rl.rlim_cur) {
    printf("RLIMIT_DATA rl.rlim_cur = %ld\n", rl.rlim_cur);
    }

    #if defined(__hpux)
    gettune("maxdsiz", &dssmax);
    printf("maxdsiz (in bytes) = %lld\n",dssmax);
    #endif
    }

    int main()
    {
    int i, *big;
    const long chunk = 1024*1024*100;
    #if defined(__sun)
    const char * cmd = "ps -o vsz,rss,args | grep a.out | grep -v grep";
    #elif defined(__hpux)
    const char * cmd = "ps -o vsz,args | grep a.out | grep -v grep";
    #endif

    showPageSize();

    maxMemoryLimit();

    for(i = 1; i <= 3; i++)
    {
    printf("\nBefore malloc: ");
    system(cmd);
    getProcSize();

    /* allocate memory */
    big = (int *)malloc(chunk * i);
    if (!big) {
    perror("malloc");
    exit(1);
    }
    printf("\nAfter malloc: ");
    system(cmd);
    getProcSize();

    memset(big, 1, chunk*i); /* "use" the memory */

    /* free memory */
    free(big);
    printf("\nAfter free: ");
    system(cmd);
    getProcSize();
    }

    /* freed 3*chunk, mallocing a chunk now shouldn't change size seen by
    ps */
    big = (int *)malloc(chunk);
    printf("\nAfter redo #1: ");
    system(cmd);
    getProcSize();

    /* mallocing another chunk shouldn't change size */
    big = (int *)malloc(chunk);
    printf("\nAfter redo #2: ");
    system(cmd);
    getProcSize();

    /* mallocing yet another chunk shouldn't change size */
    big = (int *)malloc(chunk);
    printf("\nAfter redo #3: ");
    system(cmd);
    getProcSize();

    /* mallocing one more chunk should change size */
    big = (int *)malloc(chunk);
    printf("\nAfter redo #4: ");
    system(cmd);
    getProcSize();

    free(big);
    exit(0);
    }


  3. Re: What's the "real size" of a running process?

    Ok, mallinfo tells me what I wanted to know - the count of used and
    free blocks varies correctly as I allocate and free memory.


+ Reply to Thread