#define PAGE_TO_KBYTE_SHIFT PAGE_SHIFT-10 static long mem_kbyte_max; int init_process_info_sysdep(void) { struct stat buf; /* I hope this is okay hack to get the total memsize. (-: */ if ( stat("/proc/kcore", &buf) != 0 ) { return FALSE; } num_cpus= sysconf(_SC_NPROCESSORS_CONF); mem_kbyte_max = buf.st_size>>10; return TRUE; } int get_process_info_sysdep(ProcInfo_T p) { char buf[4096]; char* tmp; char stat_item_state; unsigned long stat_item_utime; unsigned long stat_item_stime; long stat_item_cutime; long stat_item_cstime; long stat_item_rss; if (!read_proc_file(buf,4096, "stat", p->pid)) { return FALSE; } /* Move along the buffer to get past the process name */ tmp = strrchr(buf, ')') + 2; /* This implementation is done by using fs/procfs/array.c as a basis it is also worth looking into the source of the procps utils */ sscanf(tmp,"%c %*d %*d %*d %*d %*d %*u %*u" "%*u %*u %*u %lu %lu %ld %ld %*d %*d %*d " "%*d %*u %*u %ld %*u %*u %*u %*u %*u " "%*u %*u %*u %*u %*u %*u %*u %*u %*d %*d\n", &stat_item_state, &stat_item_utime, &stat_item_stime, &stat_item_cutime, &stat_item_cstime, &stat_item_rss); /* abs to please the compiler... we dont want to shift negatively. why doesn't C understand this??? */ if ( PAGE_TO_KBYTE_SHIFT < 0 ) { p->mem_kbyte= stat_item_rss >> abs(PAGE_TO_KBYTE_SHIFT); } else { p->mem_kbyte= stat_item_rss << abs(PAGE_TO_KBYTE_SHIFT); } p->mem_percent = (int) ((double) p->mem_kbyte * 1000.0 / mem_kbyte_max); /* jiffies -> seconds = 1 / HZ HZ is defined in "asm/param.h" and it is usually 1/100s but on alpha system it is 1/1024s */ p->cputime_prev = p->cputime; p->cputime = ( stat_item_utime + stat_item_stime ) * 10 / HZ; if ( include_children ) { p->cputime += ( stat_item_cutime + stat_item_cstime ) * 10 / HZ; } /* first run ? */ if ( p->time_prev == 0.0 ) { p->cputime_prev = p->cputime; } /* State is Zombie -> then we are a Zombie ... clear or? (-: */ if ( stat_item_state == 'Z' ) { p->status_flag |= PROCESS_ZOMBIE; } return TRUE; }