int uv_resident_set_memory(size_t* rss) { kvm_t *kd = NULL; struct kinfo_proc2 *kinfo = NULL; pid_t pid; int nprocs; int max_size = sizeof(struct kinfo_proc2); int page_size; page_size = getpagesize(); pid = getpid(); kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, "kvm_open"); if (kd == NULL) goto error; kinfo = kvm_getproc2(kd, KERN_PROC_PID, pid, max_size, &nprocs); if (kinfo == NULL) goto error; *rss = kinfo->p_vm_rssize * page_size; kvm_close(kd); return 0; error: if (kd) kvm_close(kd); return -EPERM; }
uv_err_t uv_resident_set_memory(size_t* rss) { kvm_t *kd = NULL; struct kinfo_proc2 *kinfo = NULL; pid_t pid; int nprocs, max_size = sizeof(struct kinfo_proc2); size_t page_size = getpagesize(); pid = getpid(); kd = kvm_open(NULL, _PATH_MEM, NULL, O_RDONLY, "kvm_open"); if (kd == NULL) goto error; kinfo = kvm_getproc2(kd, KERN_PROC_PID, pid, max_size, &nprocs); if (kinfo == NULL) goto error; *rss = kinfo->p_vm_rssize * page_size; kvm_close(kd); return uv_ok_; error: if (kd) kvm_close(kd); return uv__new_sys_error(errno); }
int psutil_get_proc_list(kinfo_proc **procList, size_t *procCount) { // Returns a list of all BSD processes on the system. This routine // allocates the list and puts it in *procList and a count of the // number of entries in *procCount. You are responsible for freeing // this list (use "free" from System framework). // On success, the function returns 0. // On error, the function returns a BSD errno value. kinfo_proc *result; int done; static const int name[] = { CTL_KERN, KERN_PROC, KERN_PROC, 0 }; // Declaring name as const requires us to cast it when passing it to // sysctl because the prototype doesn't include the const modifier. size_t length; char errbuf[_POSIX2_LINE_MAX]; kinfo_proc *x; int cnt; kvm_t *kd; assert( procList != NULL); assert(*procList == NULL); assert(procCount != NULL); kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf); if (kd == NULL) { PyErr_Format(PyExc_RuntimeError, "kvm_openfiles() failed: %s", errbuf); return errno; } result = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(kinfo_proc), &cnt); if (result == NULL) { PyErr_Format(PyExc_RuntimeError, "kvm_getproc2() failed"); kvm_close(kd); return errno; } *procCount = (size_t)cnt; size_t mlen = cnt * sizeof(kinfo_proc); if ((*procList = malloc(mlen)) == NULL) { PyErr_NoMemory(); kvm_close(kd); return errno; } memcpy(*procList, result, mlen); assert(*procList != NULL); kvm_close(kd); return 0; }
void update_total_processes() { /* It's easier to use kvm here than sysctl */ int n_processes; info.procs = 0; if (init_kvm() < 0) return; else kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), &n_processes); info.procs = n_processes; }
void update_running_processes() { struct kinfo_proc2 *p; int n_processes; int i, cnt = 0; kvm_init(); int max_size = sizeof(struct kinfo_proc2); p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes); for (i = 0; i < n_processes; i++) { if (p[i].p_stat == SRUN) { cnt++; } } info.run_procs = cnt; }
//------------------------------------------------------------------------------ // Name: DebugEvent(int s, edb::pid_t p, edb::tid_t t) // Desc: constructor //------------------------------------------------------------------------------ DebugEvent::DebugEvent(int s, edb::pid_t p, edb::tid_t t) : status(s), pid(p), tid(t) { char errbuf[_POSIX2_LINE_MAX]; if(kvm_t *const kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf)) { int rc; struct kinfo_proc2 *const proc = kvm_getproc2(kd, KERN_PROC_PID, pid, sizeof(struct kinfo_proc2), &rc); struct sigacts sigacts; kvm_read(kd, proc->p_sigacts, &sigacts, sizeof(sigacts)); fault_code_ = sigacts.ps_code; fault_address_ = sigacts.ps_sigval.sival_ptr; //printf("ps_sig : %d\n", sigacts.ps_sig); //printf("ps_type : %d\n", sigacts.ps_type); kvm_close(kd); } }
bool ProcessesLocal::Private::readProc(long pid, struct kinfo_proc2 **p, int *num) { int len; int op, arg; if (pid == 0) { op = KERN_PROC_ALL; arg = 0; } else { op = KERN_PROC_PID; arg = pid; } *p = kvm_getproc2(kd, op, arg, sizeof(struct kinfo_proc2), &len); if (len < 1) return false; if (num != NULL) *num = len; return true; }
void update_running_processes() { struct kinfo_proc2 *p; int n_processes; int i, cnt = 0; info.run_procs = 0; if (init_kvm() < 0) return; else { p = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), &n_processes); for (i = 0; i<n_processes; i++) if (p[i].p_stat == LSRUN || p[i].p_stat == LSIDL || p[i].p_stat == LSONPROC) cnt++; } info.run_procs = cnt; }
//------------------------------------------------------------------------------ // Name: sync() // Desc: reads a memory map using the kvm api //------------------------------------------------------------------------------ void MemoryRegions::sync() { QList<MemRegion> regions; if(pid_ != 0) { char err_buf[_POSIX2_LINE_MAX]; kvm_t *const kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, err_buf); if(kd != 0) { int rc; struct kinfo_proc2 *const proc = kvm_getproc2(kd, KERN_PROC_PID, pid_, sizeof(struct kinfo_proc2), &rc); Q_ASSERT(proc != 0); struct vmspace vmspace; kvm_read(kd, proc->p_vmspace, &vmspace, sizeof(vmspace)); struct vm_map_entry *next = vmspace.vm_map.header.next; for(int i = 0; i < vmspace.vm_map.nentries; ++i) { struct vm_map_entry entry; kvm_read(kd, (u_long)next, &entry, sizeof(entry)); MemRegion region; region.start = entry.start; region.end = entry.end; region.base = entry.offset; region.name = QString(); region.permissions_ = ((entry.protection & VM_PROT_READ) ? PROT_READ : 0) | ((entry.protection & VM_PROT_WRITE) ? PROT_WRITE : 0) | ((entry.protection & VM_PROT_EXECUTE) ? PROT_EXEC : 0); regions.push_back(region); next = entry.next; } } kvm_close(kd); } qSwap(regions_, regions); reset(); }
int main(int argc, char *argv[]) { kvm_t *kd; pid_t pid; uid_t uid; int which, many, ch, rc; char errbuf[_POSIX2_LINE_MAX + 1]; struct kinfo_proc2 *kproc; char *kmem, *kernel, *t; gid_t egid; struct kbit kbit, *vmspace; u_long address; egid = getegid(); if (setegid(getgid()) == -1) err(1, "failed to reset privileges"); check_fd(STDIN_FILENO); check_fd(STDOUT_FILENO); check_fd(STDERR_FILENO); pid = -1; which = verbose = debug = 0; print_all = print_map = print_maps = print_solaris = print_ddb = 0; recurse = 0; kmem = kernel = NULL; address = 0; vmspace = &kbit; while ((ch = getopt(argc, argv, "A:aD:dE:lM:mN:Pp:RrS:sV:vx")) != -1) { switch (ch) { case 'A': case 'E': case 'S': case 'V': if (which != 0) errx(1, "use only one of -A, -E, -S, or -V"); errno = 0; address = strtoul(optarg, &t, 0); if (*t != '\0') errx(1, "%s is not a valid address", optarg); if (errno != 0) err(1, "%s is not a valid address", optarg); switch (ch) { case 'A': which = AMAP_ADDRESS; break; case 'E': which = VM_MAP_ENTRY_ADDRESS; break; case 'S': which = VMSPACE_ADDRESS; break; case 'V': which = VM_MAP_ADDRESS; break; } break; case 'a': print_all = 1; break; case 'd': print_ddb = 1; break; case 'D': errno = 0; debug = strtoul(optarg, &t, 0); if (*t != '\0') errx(1, "%s is not a valid number", optarg); if (errno != 0) err(1, "%s is not a valid number", optarg); break; case 'l': print_maps = 1; break; case 'm': print_map = 1; break; case 'M': kmem = optarg; break; case 'N': kernel = optarg; break; case 'p': errno = 0; pid = strtol(optarg, &t, 0); if (pid < 0) errno = EINVAL; if (*t != '\0') errx(1, "%s is not a valid pid", optarg); if (errno != 0) err(1, "%s is not a valid pid", optarg); break; case 'P': pid = getpid(); break; case 'R': recurse = 1; break; case 's': print_solaris = 1; break; case 'v': verbose++; break; case 'r': case 'x': errx(1, "-%c option not implemented, sorry", optopt); /*NOTREACHED*/ case '?': default: fprintf(stderr, "usage: %s [-adlmPRsv] [-A address] " "[-D number] [-E address] [-M core]\n" "\t[-N system] [-p pid] [-S address] " "[-V address] [pid ...]\n", getprogname()); exit(1); } } argc -= optind; argv += optind; /* more than one "process" to dump? */ many = (argc > 1 - (pid == -1 ? 0 : 1)) ? 1 : 0; /* apply default */ if (print_all + print_map + print_maps + print_solaris + print_ddb == 0) print_solaris = 1; /* get privs back if it appears to be safe, otherwise toss them */ if (kernel == NULL && kmem == NULL && address == 0) rc = setegid(egid); else rc = setgid(getgid()); if (rc == -1) err(1, "failed to reset privileges"); /* start by opening libkvm */ kd = kvm_openfiles(kernel, kmem, NULL, O_RDONLY, errbuf); /* we're completely done with privileges now */ rc = setgid(getgid()); if (rc == -1) err(1, "failed to reset privileges"); /* print the kvm_open error, if any */ errbuf[_POSIX2_LINE_MAX] = '\0'; if (kd == NULL) errx(1, "%s", errbuf); /* get "bootstrap" addresses from kernel */ load_symbols(kd); if (address) { struct kbit kbit2, *at = &kbit2; memset(vmspace, 0, sizeof(*vmspace)); A(at) = address; S(at) = (size_t)-1; switch (which) { case VMSPACE_ADDRESS: /* (kd, kproc, vmspace, thing) */ (*process_map)(kd, NULL, at, "vm_map"); break; case VM_MAP_ADDRESS: /* (kd, proc, vmspace, vm_map, thing) */ (*dump_vm_map)(kd, NULL, vmspace, at, "vm_map"); break; case VM_MAP_ENTRY_ADDRESS: /* (kd, proc, vmspace, vm_map_entry, 0) */ (*dump_vm_map_entry)(kd, NULL, vmspace, at, 0); break; case AMAP_ADDRESS: /* (kd, amap) */ (*dump_amap)(kd, at); break; } exit(0); } uid = getuid(); do { if (pid == -1) { if (argc == 0) pid = getppid(); else { errno = 0; pid = strtol(argv[0], &t, 0); if (pid < 0) errno = EINVAL; if (*t != '\0') errx(1, "%s is not a valid pid", argv[0]); if (errno != 0) err(1, "%s is not a valid pid", argv[0]); argv++; argc--; } } errno = 0; /* find the process id */ if (pid == 0) { kproc = NULL; if (uid != 0) { /* only root can print kernel mappings */ errno = EPERM; } } else { kproc = kvm_getproc2(kd, KERN_PROC_PID, pid, sizeof(struct kinfo_proc2), &rc); if (kproc == NULL || rc == 0) { errno = ESRCH; } else if (uid != 0 && uid != kproc->p_uid) { /* * only the real owner of the process and * root can print process mappings */ errno = EPERM; } } if (errno != 0) { warn("%d", pid); pid = -1; continue; } /* dump it */ if (many) { if (kproc != NULL) printf("process %d:\n", kproc->p_pid); else printf("kernel:\n"); } (*process_map)(kd, kproc, vmspace, NULL); pid = -1; } while (argc > 0); /* done. go away. */ rc = kvm_close(kd); if (rc == -1) err(1, "kvm_close"); return (0); }
void get_memusage(uint64_t * rss, uint64_t * vsz) { #ifdef UNBIT *vsz = syscall(356); #elif defined(__linux__) FILE *procfile; int i; procfile = fopen("/proc/self/stat", "r"); if (procfile) { i = fscanf(procfile, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %llu %lld", (unsigned long long *) vsz, (unsigned long long *) rss); if (i != 2) { uwsgi_log("warning: invalid record in /proc/self/stat\n"); } fclose(procfile); } *rss = *rss * uwsgi.page_size; #elif defined (__sun__) psinfo_t info; int procfd; procfd = open("/proc/self/psinfo", O_RDONLY); if (procfd >= 0) { if (read(procfd, (char *) &info, sizeof(info)) > 0) { *rss = (uint64_t) info.pr_rssize * 1024; *vsz = (uint64_t) info.pr_size * 1024; } close(procfd); } #elif defined(__APPLE__) /* darwin documentation says that the value are in pages, but they are bytes !!! */ struct task_basic_info t_info; mach_msg_type_number_t t_size = sizeof(struct task_basic_info); if (task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t) & t_info, &t_size) == KERN_SUCCESS) { *rss = t_info.resident_size; *vsz = t_info.virtual_size; } #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__OpenBSD__) kvm_t *kv; int cnt; #if defined(__FreeBSD__) kv = kvm_open(NULL, "/dev/null", NULL, O_RDONLY, NULL); #elif defined(__NetBSD__) || defined(__OpenBSD__) kv = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL); #else kv = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL); #endif if (kv) { #if defined(__FreeBSD__) || defined(__DragonFly__) struct kinfo_proc *kproc; kproc = kvm_getprocs(kv, KERN_PROC_PID, uwsgi.mypid, &cnt); if (kproc && cnt > 0) { *vsz = kproc->ki_size; *rss = kproc->ki_rssize * uwsgi.page_size; } #elif defined(UWSGI_NEW_OPENBSD) struct kinfo_proc *kproc; kproc = kvm_getprocs(kv, KERN_PROC_PID, uwsgi.mypid, sizeof(struct kinfo_proc), &cnt); if (kproc && cnt > 0) { *vsz = (kproc->p_vm_dsize + kproc->p_vm_ssize + kproc->p_vm_tsize) * uwsgi.page_size; *rss = kproc->p_vm_rssize * uwsgi.page_size; } #elif defined(__NetBSD__) || defined(__OpenBSD__) struct kinfo_proc2 *kproc2; kproc2 = kvm_getproc2(kv, KERN_PROC_PID, uwsgi.mypid, sizeof(struct kinfo_proc2), &cnt); if (kproc2 && cnt > 0) { #ifdef __OpenBSD__ *vsz = (kproc2->p_vm_dsize + kproc2->p_vm_ssize + kproc2->p_vm_tsize) * uwsgi.page_size; #else *vsz = kproc2->p_vm_msize * uwsgi.page_size; #endif *rss = kproc2->p_vm_rssize * uwsgi.page_size; } #endif kvm_close(kv); } #elif defined(__HAIKU__) area_info ai; int32 cookie; *vsz = 0; *rss = 0; while (get_next_area_info(0, &cookie, &ai) == B_OK) { *vsz += ai.ram_size; if ((ai.protection & B_WRITE_AREA) != 0) { *rss += ai.ram_size; } } #endif }
int PROC_MEM(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { char procname[MAX_STRING_LEN], buffer[MAX_STRING_LEN], proccomm[MAX_STRING_LEN], *args; int do_task, pagesize, count, i, proc_ok, comm_ok, op, arg; double value = 0.0, memsize = 0; int proccount = 0; size_t sz; struct kinfo_proc2 *proc, *pproc; struct passwd *usrinfo; if (num_param(param) > 4) return SYSINFO_RET_FAIL; if (0 != get_param(param, 1, procname, sizeof(procname))) *procname = '\0'; else if (strlen(procname) > MAXCOMLEN) procname[MAXCOMLEN] = '\0'; if (0 != get_param(param, 2, buffer, sizeof(buffer))) *buffer = '\0'; if (*buffer != '\0') { usrinfo = getpwnam(buffer); if (usrinfo == NULL) /* incorrect user name */ return SYSINFO_RET_FAIL; } else usrinfo = NULL; if (0 != get_param(param, 3, buffer, sizeof(buffer))) *buffer = '\0'; if (*buffer != '\0') { if (0 == strcmp(buffer, "avg")) do_task = DO_AVG; else if (0 == strcmp(buffer, "max")) do_task = DO_MAX; else if (0 == strcmp(buffer, "min")) do_task = DO_MIN; else if (0 == strcmp(buffer, "sum")) do_task = DO_SUM; else return SYSINFO_RET_FAIL; } else do_task = DO_SUM; if (0 != get_param(param, 4, proccomm, sizeof(proccomm))) *proccomm = '\0'; pagesize = getpagesize(); if (NULL == kd && NULL == (kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL))) return SYSINFO_RET_FAIL; if (NULL != usrinfo) { op = KERN_PROC_UID; arg = (int)usrinfo->pw_uid; } else { op = KERN_PROC_ALL; arg = 0; } if (NULL == (proc = kvm_getproc2(kd, op, arg, sizeof(struct kinfo_proc2), &count))) return SYSINFO_RET_FAIL; for (pproc = proc, i = 0; i < count; pproc++, i++) { proc_ok = 0; comm_ok = 0; if (*procname == '\0' || 0 == strcmp(procname, pproc->p_comm)) proc_ok = 1; if (*proccomm != '\0') { if (NULL != (args = proc_argv(pproc->p_pid))) { if (NULL != zbx_regexp_match(args, proccomm, NULL)) comm_ok = 1; } } else comm_ok = 1; if (proc_ok && comm_ok) { value = pproc->p_vm_tsize + pproc->p_vm_dsize + pproc->p_vm_ssize; value *= pagesize; if (0 == proccount++) memsize = value; else { if (do_task == DO_MAX) memsize = MAX(memsize, value); else if (do_task == DO_MIN) memsize = MIN(memsize, value); else memsize += value; } } } if (do_task == DO_AVG) SET_DBL_RESULT(result, proccount == 0 ? 0 : memsize / proccount); else SET_UI64_RESULT(result, memsize); return SYSINFO_RET_OK; }
int main(int argc, char *argv[]) { extern char *optarg; extern int optind; struct passwd *passwd; struct kinfo_proc2 *p, *plast; int arg, ch, what; char *memf, *nlistf; char buf[_POSIX2_LINE_MAX]; int cnt; arg = 0; what = KERN_PROC_ALL; nlistf = memf = NULL; oflg = 0; while ((ch = getopt(argc, argv, "fnop:u:vN:M:")) != -1) switch((char)ch) { case 'f': fsflg = 1; break; case 'M': memf = optarg; break; case 'N': nlistf = optarg; break; case 'n': nflg = 1; break; case 'o': oflg = 1; break; case 'p': if (pflg++) usage(); if (!isdigit(*optarg)) { warnx( "-p requires a process id"); usage(); } what = KERN_PROC_PID; arg = atoi(optarg); break; case 'u': if (uflg++) usage(); if (!(passwd = getpwnam(optarg))) errx(1, "%s: unknown uid", optarg); what = KERN_PROC_UID; arg = passwd->pw_uid; break; case 'v': vflg = 1; break; default: usage(); } /* * Discard setgid privileges if not the running kernel so that bad * guys can't print interesting stuff from kernel memory. */ if (nlistf != NULL || memf != NULL) { setegid(getgid()); setgid(getgid()); } if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL) errx(1, "%s", buf); setegid(getgid()); setgid(getgid()); if (*(argv += optind)) { for (; *argv; ++argv) { if (getfname(*argv)) checkfile = 1; } if (!checkfile) /* file(s) specified, but none accessible */ exit(1); } ALLOC_OFILES(256); /* reserve space for file pointers */ if (fsflg && !checkfile) { /* -f with no files means use wd */ if (getfname(".") == 0) exit(1); checkfile = 1; } if ((p = kvm_getproc2(kd, what, arg, sizeof(*p), &cnt)) == NULL) errx(1, "%s", kvm_geterr(kd)); if (nflg) printf("%s", "USER CMD PID FD DEV INUM MODE R/W DV|SZ"); else printf("%s", "USER CMD PID FD MOUNT INUM MODE R/W DV|SZ"); if (oflg) printf("%s", ":OFFSET "); if (checkfile && fsflg == 0) printf(" NAME\n"); else putchar('\n'); for (plast = &p[cnt]; p < plast; ++p) { if (p->p_stat == SZOMB) continue; dofiles(p); } exit(0); }
RC_PIDLIST * rc_find_pids(const char *exec, const char *const *argv, uid_t uid, pid_t pid) { static kvm_t *kd = NULL; char errbuf[_POSIX2_LINE_MAX]; struct _KINFO_PROC *kp; int i; int processes = 0; int pargc = 0; char **pargv; RC_PIDLIST *pids = NULL; RC_PID *pi; pid_t p; const char *const *arg; int match; if ((kd = kvm_openfiles(_KVM_PATH, _KVM_PATH, NULL, _KVM_FLAGS, errbuf)) == NULL) { fprintf(stderr, "kvm_open: %s\n", errbuf); return NULL; } #ifdef _KVM_GETPROC2 kp = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(*kp), &processes); #else kp = kvm_getprocs(kd, KERN_PROC_PROC, 0, &processes); #endif if ((kp == NULL && processes > 0) || (kp != NULL && processes < 0)) { fprintf(stderr, "kvm_getprocs: %s\n", kvm_geterr(kd)); kvm_close(kd); return NULL; } if (exec) exec = basename_c(exec); for (i = 0; i < processes; i++) { p = _GET_KINFO_PID(kp[i]); if (pid != 0 && pid != p) continue; if (uid != 0 && uid != _GET_KINFO_UID(kp[i])) continue; if (exec) { if (!_GET_KINFO_COMM(kp[i]) || strcmp(exec, _GET_KINFO_COMM(kp[i])) != 0) continue; } if (argv && *argv) { pargv = _KVM_GETARGV(kd, &kp[i], pargc); if (!pargv || !*pargv) continue; arg = argv; match = 1; while (*arg && *pargv) if (strcmp(*arg++, *pargv++) != 0) { match = 0; break; } if (!match) continue; } if (!pids) { pids = xmalloc(sizeof(*pids)); LIST_INIT(pids); } pi = xmalloc(sizeof(*pi)); pi->pid = p; LIST_INSERT_HEAD(pids, pi, entries); } kvm_close(kd); return pids; }
inline void proc_find_top(struct process **cpu, struct process **mem) { struct kinfo_proc2 *p; int n_processes; int i, j = 0; struct process *processes; int mib[2]; u_int total_pages; int64_t usermem; int pagesize = getpagesize(); /* we get total pages count again to be sure it is up to date */ mib[0] = CTL_HW; mib[1] = HW_USERMEM64; size_t size = sizeof(usermem); if (sysctl(mib, 2, &usermem, &size, NULL, 0) == -1) { NORM_ERR("error reading usermem"); } /* translate bytes into page count */ total_pages = usermem / pagesize; int max_size = sizeof(struct kinfo_proc2); p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes); processes = malloc(n_processes * sizeof(struct process)); for (i = 0; i < n_processes; i++) { if (!((p[i].p_flag & P_SYSTEM)) && p[i].p_comm != NULL) { processes[j].pid = p[i].p_pid; processes[j].name = strndup(p[i].p_comm, text_buffer_size); processes[j].amount = 100.0 * p[i].p_pctcpu / FSCALE; j++; } } qsort(processes, j - 1, sizeof(struct process), comparemem); for (i = 0; i < 10; i++) { struct process *tmp, *ttmp; tmp = malloc(sizeof(struct process)); tmp->pid = processes[i].pid; tmp->amount = processes[i].amount; tmp->name = strndup(processes[i].name, text_buffer_size); ttmp = mem[i]; mem[i] = tmp; if (ttmp != NULL) { free(ttmp->name); free(ttmp); } } qsort(processes, j - 1, sizeof(struct process), comparecpu); for (i = 0; i < 10; i++) { struct process *tmp, *ttmp; tmp = malloc(sizeof(struct process)); tmp->pid = processes[i].pid; tmp->amount = processes[i].amount; tmp->name = strndup(processes[i].name, text_buffer_size); ttmp = cpu[i]; cpu[i] = tmp; if (ttmp != NULL) { free(ttmp->name); free(ttmp); } } for (i = 0; i < j; i++) { free(processes[i].name); } free(processes); }
void get_memusage() { #ifdef UNBIT uwsgi.workers[uwsgi.mywid].vsz_size = syscall(356); #elif defined(__linux__) FILE *procfile; int i; procfile = fopen("/proc/self/stat", "r"); if (procfile) { i = fscanf(procfile, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %llu %lld", &uwsgi.workers[uwsgi.mywid].vsz_size, &uwsgi.workers[uwsgi.mywid].rss_size); if (i != 2) { fprintf(stderr, "warning: invalid record in /proc/self/stat\n"); } fclose(procfile); } uwsgi.workers[uwsgi.mywid].rss_size = uwsgi.workers[uwsgi.mywid].rss_size * uwsgi.page_size; #elif defined (__sun__) psinfo_t info; int procfd ; procfd = open("/proc/self/psinfo", O_RDONLY); if (procfd >= 0) { if ( read(procfd, (char *) &info, sizeof(info)) > 0) { uwsgi.workers[uwsgi.mywid].rss_size = (uint64_t) info.pr_rssize * 1024 ; uwsgi.workers[uwsgi.mywid].vsz_size = (uint64_t) info.pr_size * 1024 ; } close(procfd); } #elif defined( __APPLE__) /* darwin documentation says that the value are in pages, but they are bytes !!! */ struct task_basic_info t_info; mach_msg_type_number_t t_size = sizeof(struct task_basic_info); if (task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t) & t_info, &t_size) == KERN_SUCCESS) { uwsgi.workers[uwsgi.mywid].rss_size = t_info.resident_size; uwsgi.workers[uwsgi.mywid].vsz_size = t_info.virtual_size; } #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__OpenBSD__) kvm_t *kv; int cnt; kv = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL); if (kv) { #if defined(__FreeBSD__) || defined(__DragonFly__) struct kinfo_proc *kproc; kproc = kvm_getprocs(kv, KERN_PROC_PID, uwsgi.mypid, &cnt); if (kproc && cnt > 0) { uwsgi.workers[uwsgi.mywid].vsz_size = kproc->ki_size; uwsgi.workers[uwsgi.mywid].rss_size = kproc->ki_rssize * uwsgi.page_size; } #elif defined(__NetBSD__) struct kinfo_proc2 *kproc2; kproc2 = kvm_getproc2(kv, KERN_PROC_PID, uwsgi.mypid, sizeof(struct kinfo_proc2), &cnt); if (kproc2 && cnt > 0) { uwsgi.workers[uwsgi.mywid].vsz_size = kproc2->p_vm_msize * uwsgi.page_size; uwsgi.workers[uwsgi.mywid].rss_size = kproc2->p_vm_rssize * uwsgi.page_size; } #endif kvm_close(kv); } #endif }
int PROC_NUM(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { char procname[MAX_STRING_LEN], buffer[MAX_STRING_LEN], proccomm[MAX_STRING_LEN], *args; int zbx_proc_stat, count, i, proc_ok, stat_ok, comm_ok, op, arg; int proccount = 0; size_t sz; struct kinfo_proc2 *proc, *pproc; struct passwd *usrinfo; if (num_param(param) > 4) return SYSINFO_RET_FAIL; if (0 != get_param(param, 1, procname, sizeof(procname))) *procname = '\0'; else if (strlen(procname) > MAXCOMLEN) procname[MAXCOMLEN] = '\0'; if (0 != get_param(param, 2, buffer, sizeof(buffer))) *buffer = '\0'; if (*buffer != '\0') { usrinfo = getpwnam(buffer); if (usrinfo == NULL) /* incorrect user name */ return SYSINFO_RET_FAIL; } else usrinfo = NULL; if (0 != get_param(param, 3, buffer, sizeof(buffer))) *buffer = '\0'; if (*buffer != '\0') { if (0 == strcmp(buffer, "run")) zbx_proc_stat = ZBX_PROC_STAT_RUN; else if (0 == strcmp(buffer, "sleep")) zbx_proc_stat = ZBX_PROC_STAT_SLEEP; else if (0 == strcmp(buffer, "zomb")) zbx_proc_stat = ZBX_PROC_STAT_ZOMB; else if (0 == strcmp(buffer, "all")) zbx_proc_stat = ZBX_PROC_STAT_ALL; else return SYSINFO_RET_FAIL; } else zbx_proc_stat = ZBX_PROC_STAT_ALL; if (0 != get_param(param, 4, proccomm, sizeof(proccomm))) *proccomm = '\0'; if (NULL == kd && NULL == (kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL))) return SYSINFO_RET_FAIL; if (NULL != usrinfo) { op = KERN_PROC_UID; arg = (int)usrinfo->pw_uid; } else { op = KERN_PROC_ALL; arg = 0; } if (NULL == (proc = kvm_getproc2(kd, op, arg, sizeof(struct kinfo_proc2), &count))) return SYSINFO_RET_FAIL; for (pproc = proc, i = 0; i < count; pproc++, i++) { proc_ok = 0; stat_ok = 0; comm_ok = 0; if (*procname == '\0' || 0 == strcmp(procname, pproc->p_comm)) proc_ok = 1; if (zbx_proc_stat != ZBX_PROC_STAT_ALL) { switch (zbx_proc_stat) { case ZBX_PROC_STAT_RUN: if (pproc->p_stat == LSRUN || pproc->p_stat == LSONPROC) stat_ok = 1; break; case ZBX_PROC_STAT_SLEEP: if (pproc->p_stat == LSSLEEP) stat_ok = 1; break; case ZBX_PROC_STAT_ZOMB: if (pproc->p_stat == SZOMB || pproc->p_stat == LSDEAD) stat_ok = 1; break; } } else stat_ok = 1; if (*proccomm != '\0') { if (NULL != (args = proc_argv(pproc->p_pid))) { if (zbx_regexp_match(args, proccomm, NULL) != NULL) comm_ok = 1; } } else comm_ok = 1; if (proc_ok && stat_ok && comm_ok) proccount++; } SET_UI64_RESULT(result, proccount); return SYSINFO_RET_OK; }