[BACK]Return to total_rusage.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / util

Annotation of OpenXM/src/util/total_rusage.c, Revision 1.2

1.2     ! noro        1: /* $OpenXM: OpenXM/src/util/total_rusage.c,v 1.1 2002/10/22 08:59:42 noro Exp $ */
1.1       noro        2:
                      3: /* outputs total virtual memory size and total cputime consumed by
                      4:    whole processes which have the specified process as their ancestor */
                      5:
                      6: #include <sys/param.h>
                      7: #include <sys/user.h>
                      8: #include <sys/time.h>
                      9: #include <sys/resource.h>
                     10: #include <sys/stat.h>
                     11: #include <sys/ioctl.h>
                     12: #include <sys/sysctl.h>
                     13:
                     14: #include <fcntl.h>
                     15: #include <kvm.h>
                     16: #include <paths.h>
                     17: #include <stdio.h>
                     18: #include <stdlib.h>
                     19:
                     20: struct proc_info {
                     21:        int pid,ppid;
                     22:        int vsize;    /* kbytes */
                     23:        double time;  /* second */
                     24: };
                     25:
1.2     ! noro       26: void total_resource(struct proc_info *pi,int n,int rootid,int *vsize,double *time);
        !            27:
1.1       noro       28: int compare_pi(struct proc_info *a, struct proc_info *b)
                     29: {
                     30:         if ( a->pid < b->pid ) return 1;
                     31:         else if ( a->pid >= b->pid ) return -1;
                     32:         else return 0;
                     33: }
                     34:
                     35: main(int argc, char **argv)
                     36: {
                     37:        char *nlistf,*memf;
                     38:        char errbuf[BUFSIZ];
1.2     ! noro       39:        int rootid,n,i,j,pid,ppid,total_vsize;
        !            40:        double total_time;
1.1       noro       41:        kvm_t *kd;
                     42:        struct kinfo_proc *kp;
                     43:        struct proc_info *pi;
                     44:
                     45:        if ( argc != 2 ) {
                     46:                fprintf(stderr,"total_rusage <process id>\n");
                     47:                exit(0);
                     48:        }
                     49:        rootid = atoi(argv[1]);
                     50:        nlistf = memf = _PATH_DEVNULL;
                     51:        kd = kvm_openfiles(nlistf,memf,NULL,O_RDONLY,errbuf);
                     52:        if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &n)) == 0) {
                     53:                fprintf(stderr,"%s\n",kvm_geterr(kd));
                     54:                exit(0);
                     55:        }
                     56:        pi = (struct proc_info *)calloc(n,sizeof(struct proc_info));
                     57:        for ( i = 0; i < n; i++, kp++ ) {
                     58:                pi[i].pid = kp->kp_proc.p_pid;
                     59:                pi[i].ppid = kp->kp_eproc.e_ppid;
                     60:                pi[i].vsize = kp->kp_eproc.e_vm.vm_map.size/1024;
                     61:                pi[i].time = (double) (kp->kp_proc.p_uu+kp->kp_proc.p_su)/(double)1e6;
                     62:        }
                     63:        qsort(pi,n,sizeof(struct proc_info),
                     64:                (int (*)(const void *,const void *))compare_pi);
1.2     ! noro       65:        total_resource(pi,n,rootid,&total_vsize,&total_time);
        !            66:        printf("%d %f\n",total_vsize,total_time);
        !            67: }
        !            68:
        !            69: void total_resource(struct proc_info *pi,int n,int rootid,int *vsize,double *time)
        !            70: {
        !            71:        int vsize0,vsize1,i;
        !            72:        double time0,time1;
        !            73:
        !            74:        vsize0 = 0;
        !            75:        time0 = 0;
1.1       noro       76:        for ( i = 0; i < n; i++ ) {
1.2     ! noro       77:                if ( pi[i].ppid == rootid ) {
        !            78:                        if ( pi[i].pid ) {
        !            79:                                total_resource(pi,n,pi[i].pid,&vsize1,&time1);
        !            80:                                vsize0 += vsize1;
        !            81:                                time0 += time1;
1.1       noro       82:                        }
1.2     ! noro       83:                } else if ( pi[i].pid == rootid ) {
        !            84:                        vsize0 += pi[i].vsize;
        !            85:                        time0 += pi[i].time;
1.1       noro       86:                }
                     87:        }
1.2     ! noro       88:        *vsize = vsize0;
        !            89:        *time = time0;
1.1       noro       90: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>