📄 mom_mach.c
字号:
intgetprocs(){ static unsigned int lastproc = 0; char *id = "getprocs"; caddr_t *kernel_proc; int i, len; if (lastproc == reqnum) /* don't need new proc table */ return 1; if (mom_get_sample() != PBSE_NONE) return 0; lastproc = reqnum; return 1;}char *cput_job(jobid)pid_t jobid;{ char *id = "cput_job"; double ses_time; int i; register struct user *uarea; unsigned long cputime; if (getprocs() == 0) { rm_errno = RM_ERR_SYSTEM; return NULL; } cputime = 0; for (i=0; i<nproc; i++) { register struct proc *pp = &proc_tbl[i]; if (pp->p_stat==0) continue; if (jobid != sess_tbl[i]) continue; if (pp->p_ru == NULL) { if ((uarea = kvm_getu(kd, pp)) == NULL) continue; else { cputime += tv(uarea->u_ru.ru_utime) + tv(uarea->u_ru.ru_stime) + tv(uarea->u_cru.ru_utime) + tv(uarea->u_cru.ru_stime); } } else { struct rusage ru; if (kvm_read(kd, (unsigned long)pp->p_ru, (char *)&ru, sizeof(ru)) != sizeof(ru)) { log_err(errno, id, "kvm_read(session)"); continue; } cputime += tv(ru.ru_utime) + tv(ru.ru_stime); } DBPRT(("%s: ses %d pid %d cputime %d\n", id, jobid, pp->p_pid, cputime)) } sprintf(ret_string, "%.2f", (double)cputime * cputfactor); return ret_string;}char *cput_proc(pid)pid_t pid;{ char *id = "cput_proc"; register struct proc *pp; register struct user *uarea; uint cputime; if (kd == NULL) { log_err(-1, id, nokernel); rm_errno = RM_ERR_SYSTEM; return NULL; } if ((pp = kvm_getproc(kd, pid)) == NULL) { sprintf(log_buffer, noproc, pid); log_err(-1, id, log_buffer); rm_errno = RM_ERR_EXIST; return NULL; } if ((uarea = kvm_getu(kd, pp)) == NULL) { if (errno) { log_err(errno, id, "kvm_getu"); rm_errno = RM_ERR_SYSTEM; return NULL; } cputime = 0; } else { cputime = tv(uarea->u_ru.ru_utime) + tv(uarea->u_ru.ru_stime) + tv(uarea->u_cru.ru_utime) + tv(uarea->u_cru.ru_stime); } sprintf(ret_string, "%.2f", (double)cputime * cputfactor); return ret_string;}char *cput(attrib)struct rm_attribute *attrib;{ char *id = "cput"; int value; if (attrib == NULL) { log_err(-1, id, no_parm); rm_errno = RM_ERR_NOPARAM; return NULL; } if ((value = atoi(attrib->a_value)) == 0) { sprintf(log_buffer, "bad param: %s", attrib->a_value); log_err(-1, id, log_buffer); rm_errno = RM_ERR_BADPARAM; return NULL; } if (momgetattr(NULL)) { log_err(-1, id, extra_parm); rm_errno = RM_ERR_BADPARAM; return NULL; } if (strcmp(attrib->a_qualifier, "session") == 0) return (cput_job((pid_t)value)); else if (strcmp(attrib->a_qualifier, "proc") == 0) return (cput_proc((pid_t)value)); else { rm_errno = RM_ERR_BADPARAM; return NULL; }}char *mem_job(jobid)pid_t jobid;{ char *id = "mem_job"; int i; int memsize, addmem; int found = 0; if (getprocs() == 0) { rm_errno = RM_ERR_SYSTEM; return NULL; } memsize = 0; for (i=0; i<nproc; i++) { register struct proc *pp = &proc_tbl[i]; if (pp->p_stat==0) continue; if (jobid != sess_tbl[i]) continue; found = 1; addmem = pp->p_tsize + pp->p_dsize + pp->p_ssize; memsize += addmem; } if (found) { sprintf(ret_string, "%ukb", ctob(memsize) >> 10); /* KB */ return ret_string; } rm_errno = RM_ERR_EXIST; return NULL;}char *mem_proc(pid)pid_t pid;{ char *id = "mem_proc"; register struct proc *pp; int memsize; if (kd == NULL) { log_err(-1, id, nokernel); rm_errno = RM_ERR_SYSTEM; return NULL; } if ((pp = kvm_getproc(kd, pid)) == NULL) { sprintf(log_buffer, noproc, pid); log_err(-1, id, log_buffer); rm_errno = RM_ERR_EXIST; return NULL; } memsize = pp->p_tsize + pp->p_dsize + pp->p_ssize; sprintf(ret_string, "%ukb", ctob(memsize) >> 10); /* KB */ return ret_string;}char *mem(attrib)struct rm_attribute *attrib;{ char *id = "mem"; int value; if (attrib == NULL) { log_err(-1, id, no_parm); rm_errno = RM_ERR_NOPARAM; return NULL; } if ((value = atoi(attrib->a_value)) == 0) { sprintf(log_buffer, "bad param: %s", attrib->a_value); log_err(-1, id, log_buffer); rm_errno = RM_ERR_BADPARAM; return NULL; } if (momgetattr(NULL)) { log_err(-1, id, extra_parm); rm_errno = RM_ERR_BADPARAM; return NULL; } if (strcmp(attrib->a_qualifier, "session") == 0) return (mem_job((pid_t)value)); else if (strcmp(attrib->a_qualifier, "proc") == 0) return (mem_proc((pid_t)value)); else { rm_errno = RM_ERR_BADPARAM; return NULL; }}static char *resi_job(jobid)pid_t jobid;{ char *id = "resi_job"; int i; int resisize; int found = 0; if (getprocs() == 0) { rm_errno = RM_ERR_SYSTEM; return NULL; } resisize = 0; for (i=0; i<nproc; i++) { register struct proc *pp = &proc_tbl[i]; if (pp->p_stat==0) continue; if (jobid != sess_tbl[i]) continue; found = 1; resisize += pp->p_rssize; } if (found) { sprintf(ret_string, "%ukb", ctob(resisize) >> 10); /* KB */ return ret_string; } rm_errno = RM_ERR_EXIST; return NULL;}static char *resi_proc(pid)pid_t pid;{ char *id = "resi_proc"; register struct proc *pp; if (kd == NULL) { log_err(-1, id, nokernel); rm_errno = RM_ERR_SYSTEM; return NULL; } if ((pp = kvm_getproc(kd, pid)) == NULL) { sprintf(log_buffer, noproc, pid); log_err(-1, id, log_buffer); rm_errno = RM_ERR_EXIST; return NULL; } sprintf(ret_string, "%ukb", ctob(pp->p_rssize) >> 10); /* KB */ return ret_string;}static char *resi(attrib)struct rm_attribute *attrib;{ char *id = "resi"; int value; if (attrib == NULL) { log_err(-1, id, no_parm); rm_errno = RM_ERR_NOPARAM; return NULL; } if ((value = atoi(attrib->a_value)) == 0) { sprintf(log_buffer, "bad param: %s", attrib->a_value); log_err(-1, id, log_buffer); rm_errno = RM_ERR_BADPARAM; return NULL; } if (momgetattr(NULL)) { log_err(-1, id, extra_parm); rm_errno = RM_ERR_BADPARAM; return NULL; } if (strcmp(attrib->a_qualifier, "session") == 0) return (resi_job((pid_t)value)); else if (strcmp(attrib->a_qualifier, "proc") == 0) return (resi_proc((pid_t)value)); else { rm_errno = RM_ERR_BADPARAM; return NULL; }}char *sessions(attrib)struct rm_attribute *attrib;{ char *id = "sessions"; int i, j; char *fmt; int njids = 0; pid_t *jids, jobid; if (attrib) { log_err(-1, id, extra_parm); rm_errno = RM_ERR_BADPARAM; return NULL; } if (getprocs() == 0) { rm_errno = RM_ERR_SYSTEM; return NULL; } if ((jids = (pid_t *)calloc(nproc, sizeof(pid_t))) == NULL) { log_err(errno, id, "no memory"); rm_errno = RM_ERR_SYSTEM; return NULL; } /* ** Search for job */ for (i=0; i<nproc; i++) { register struct proc *pp = &proc_tbl[i]; if (pp->p_stat == 0) continue; if (pp->p_suid == 0) continue; jobid = sess_tbl[i]; DBPRT(("%s: pid %d sid %u\n", id, (int)pp->p_pid, jobid)) for (j=0; j<njids; j++) { if (jids[j] == jobid) break; } if (j == njids) /* not found */ jids[njids++] = jobid; /* so add it to list */ } fmt = ret_string; for (j=0; j<njids; j++) { checkret(&fmt, 100); sprintf(fmt, " %d", (int)jids[j]); fmt += strlen(fmt); } free(jids); return ret_string;}char *nsessions(attrib)struct rm_attribute *attrib;{ char *result, *ch; int num = 0; if ((result = sessions(attrib)) == NULL) return result; for (ch=result; *ch; ch++) { if (*ch == ' ') /* count blanks */ num++; } sprintf(ret_string, "%d", num); return ret_string;}char *pids(attrib)struct rm_attribute *attrib;{ char *id = "pids"; pid_t jobid; int i; char *fmt; int num_pids = 0; if (attrib == NULL) { log_err(-1, id, no_parm); rm_errno = RM_ERR_NOPARAM; return NULL; } if ((jobid = (pid_t)atoi(attrib->a_value)) == 0) { sprintf(log_buffer, "bad param: %s", attrib->a_value); log_err(-1, id, log_buffer); rm_errno = RM_ERR_BADPARAM; return NULL; } if (momgetattr(NULL)) { log_err(-1, id, extra_parm); rm_errno = RM_ERR_BADPARAM; return NULL; } if (strcmp(attrib->a_qualifier, "session") != 0) { rm_errno = RM_ERR_BADPARAM; return NULL; } if (getprocs() == 0) { rm_errno = RM_ERR_SYSTEM; return NULL; } /* ** Search for members of session */ fmt = ret_string; for (i=0; i<nproc; i++) { register struct proc *pp = &proc_tbl[i]; if (pp->p_stat==0) continue; DBPRT(("%s[%d]: pid %d sid %u\n", id, num_pids, pp->p_pid, sess_tbl[i])) if (jobid != sess_tbl[i]) continue; checkret(&fmt, 100); sprintf(fmt, " %d", pp->p_pid); fmt += strlen(fmt); num_pids++; } if (num_pids == 0) { rm_errno = RM_ERR_EXIST; return NULL; } return ret_string;}char *nusers(attrib)struct rm_attribute *attrib;{ char *id = "nusers"; int i, j; int nuids = 0; uid_t *uids, uid; if (attrib) { log_err(-1, id, extra_parm); rm_errno = RM_ERR_BADPARAM; return NULL; } if (getprocs() == 0) { rm_errno = RM_ERR_SYSTEM; return NULL; } if ((uids = (uid_t *)calloc(nproc, sizeof(uid_t))) == NULL) { log_err(errno, id, "no memory"); rm_errno = RM_ERR_SYSTEM; return NULL; } for (i=0; i<nproc; i++) { register struct proc *pp = &proc_tbl[i]; if (pp->p_stat==0) continue; if ((uid = pp->p_suid) == 0) continue; DBPRT(("%s: pid %d uid %u\n", id, (int)pp->p_pid, uid)) for (j=0; j<nuids; j++) { if (uids[j] == uid) break; } if (j == nuids) /* not found */ uids[nuids++] = uid; /* so add it to list */ } sprintf(ret_string, "%d", nuids); free(uids); return ret_string;}struct anoninfo ai;intgetanon(id)char *id;{ static unsigned int lastai = 0; if (lastai == reqnum) /* already have anoninfo */ return 0; if (kd == NULL) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -