Annotation of OpenXM/src/util/total_rusage.c, Revision 1.1
1.1 ! noro 1: /* $OpenXM$ */
! 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:
! 26: int compare_pi(struct proc_info *a, struct proc_info *b)
! 27: {
! 28: if ( a->pid < b->pid ) return 1;
! 29: else if ( a->pid >= b->pid ) return -1;
! 30: else return 0;
! 31: }
! 32:
! 33: main(int argc, char **argv)
! 34: {
! 35: char *nlistf,*memf;
! 36: char errbuf[BUFSIZ];
! 37: int rootid,n,i,j,pid,ppid;
! 38: kvm_t *kd;
! 39: struct kinfo_proc *kp;
! 40: struct proc_info *pi;
! 41:
! 42: if ( argc != 2 ) {
! 43: fprintf(stderr,"total_rusage <process id>\n");
! 44: exit(0);
! 45: }
! 46: rootid = atoi(argv[1]);
! 47: nlistf = memf = _PATH_DEVNULL;
! 48: kd = kvm_openfiles(nlistf,memf,NULL,O_RDONLY,errbuf);
! 49: if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &n)) == 0) {
! 50: fprintf(stderr,"%s\n",kvm_geterr(kd));
! 51: exit(0);
! 52: }
! 53: pi = (struct proc_info *)calloc(n,sizeof(struct proc_info));
! 54: for ( i = 0; i < n; i++, kp++ ) {
! 55: pi[i].pid = kp->kp_proc.p_pid;
! 56: pi[i].ppid = kp->kp_eproc.e_ppid;
! 57: pi[i].vsize = kp->kp_eproc.e_vm.vm_map.size/1024;
! 58: pi[i].time = (double) (kp->kp_proc.p_uu+kp->kp_proc.p_su)/(double)1e6;
! 59: }
! 60: qsort(pi,n,sizeof(struct proc_info),
! 61: (int (*)(const void *,const void *))compare_pi);
! 62: for ( i = 0; i < n; i++ ) {
! 63: pid = pi[i].pid;
! 64: if ( pid == rootid ) {
! 65: printf("%d %f\n",pi[i].vsize,pi[i].time);
! 66: exit(0);
! 67: } else {
! 68: /* find the parent */
! 69: ppid = pi[i].ppid;
! 70: for ( j = i+1; j < n; j++ )
! 71: if ( pi[j].pid == ppid )
! 72: break;
! 73: if ( j < n ) {
! 74: pi[j].vsize += pi[i].vsize;
! 75: pi[j].time += pi[i].time;
! 76: }
! 77: }
! 78: }
! 79: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>