shun_iwasawa a35b8f
#include <stdio.h></stdio.h>
shun_iwasawa a35b8f
#include <stdlib.h></stdlib.h>
shun_iwasawa a35b8f
#include <sys times.h=""></sys>
shun_iwasawa a35b8f
#include <sys types.h=""></sys>
shun_iwasawa a35b8f
#include <unistd.h></unistd.h>
shun_iwasawa a35b8f
shun_iwasawa a35b8f
#include "pstats.h"
shun_iwasawa a35b8f
shun_iwasawa a35b8f
static struct tms tms_beg;
shun_iwasawa a35b8f
static struct tms tms_end;
shun_iwasawa a35b8f
static int has_times = 0;
shun_iwasawa a35b8f
shun_iwasawa a35b8f
shun_iwasawa a35b8f
void pstats_init(void)
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    has_times = times(&tms_beg) != -1;
shun_iwasawa a35b8f
}
shun_iwasawa a35b8f
shun_iwasawa a35b8f
static void tms_report(void)
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    double cputime;
shun_iwasawa a35b8f
    if (! has_times )
shun_iwasawa a35b8f
        return;
shun_iwasawa a35b8f
    times(&tms_end);
shun_iwasawa a35b8f
    cputime = ( ((float)tms_end.tms_utime + tms_end.tms_stime + tms_end.tms_cutime + tms_end.tms_cstime ) -
shun_iwasawa a35b8f
                ((float)tms_beg.tms_utime + tms_beg.tms_stime + tms_beg.tms_cutime + tms_beg.tms_cstime ) )
shun_iwasawa a35b8f
               / sysconf(_SC_CLK_TCK);
shun_iwasawa a35b8f
    fprintf(stderr,"\tcputime=%.3f\n" , cputime);
shun_iwasawa a35b8f
}
shun_iwasawa a35b8f
shun_iwasawa a35b8f
static void ps_report(void)
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    char buf[1024];
shun_iwasawa a35b8f
#ifdef __APPLE__ /*  MAC OS X */
shun_iwasawa a35b8f
    sprintf(buf,"ps -o command,majflt,minflt,rss,pagein,vsz -p %d 1>&2",getpid() );
shun_iwasawa a35b8f
#else /* GNU/Linux */
shun_iwasawa a35b8f
    sprintf(buf,"ps -o comm,majflt,minflt,rss,drs,pagein,sz,trs,vsz %d 1>&2",getpid() );
shun_iwasawa a35b8f
#endif    
shun_iwasawa a35b8f
    if (system( buf )==-1) {
shun_iwasawa a35b8f
        perror("system call to ps failed");
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
}
shun_iwasawa a35b8f
shun_iwasawa a35b8f
void pstats_report()
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    ps_report();
shun_iwasawa a35b8f
    tms_report();
shun_iwasawa a35b8f
}
shun_iwasawa a35b8f