2009-07-28 21:44:22 +00:00
|
|
|
/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
|
2009-09-12 10:50:51 +00:00
|
|
|
* vim: ts=4 sw=4 noet ai cindent syntax=c
|
2009-07-28 21:44:22 +00:00
|
|
|
*
|
|
|
|
* Conky, a system monitor, based on torsmo
|
2005-08-05 01:06:17 +00:00
|
|
|
*
|
2007-08-10 19:53:44 +00:00
|
|
|
* Any original torsmo code is licensed under the BSD license
|
|
|
|
*
|
|
|
|
* All code written since the fork of torsmo is licensed under the GPL
|
|
|
|
*
|
|
|
|
* Please see COPYING for details
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005 Adi Zaimi, Dan Piponi <dan@tanelorn.demon.co.uk>,
|
2008-02-20 20:30:45 +00:00
|
|
|
* Dave Clark <clarkd@skynet.ca>
|
2009-03-30 04:55:30 +00:00
|
|
|
* Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
|
2008-02-20 20:30:45 +00:00
|
|
|
* (see AUTHORS)
|
2007-08-10 19:53:44 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2008-02-20 20:30:45 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2005-08-05 01:06:17 +00:00
|
|
|
*
|
2008-12-09 23:35:49 +00:00
|
|
|
*/
|
2005-08-05 01:06:17 +00:00
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
#include "top.h"
|
2009-08-05 22:46:51 +00:00
|
|
|
#include "logging.h"
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2005-11-10 01:48:36 +00:00
|
|
|
static unsigned long g_time = 0;
|
2005-11-27 06:56:35 +00:00
|
|
|
static unsigned long long previous_total = 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
static struct process *first_process = 0;
|
|
|
|
|
2009-09-20 14:09:18 +00:00
|
|
|
/* a simple hash table to speed up find_process() */
|
|
|
|
struct proc_hash_entry {
|
|
|
|
struct proc_hash_entry *next;
|
|
|
|
struct process *proc;
|
|
|
|
};
|
|
|
|
static struct proc_hash_entry proc_hash_table[256];
|
|
|
|
|
|
|
|
static void hash_process(struct process *p)
|
|
|
|
{
|
|
|
|
struct proc_hash_entry *phe;
|
|
|
|
static char first_run = 1;
|
|
|
|
|
|
|
|
/* better make sure all next pointers are zero upon first access */
|
|
|
|
if (first_run) {
|
|
|
|
memset(proc_hash_table, 0, sizeof(struct proc_hash_entry) * 256);
|
|
|
|
first_run = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the bucket head */
|
|
|
|
phe = &proc_hash_table[p->pid % 256];
|
|
|
|
|
|
|
|
/* find the bucket's end */
|
|
|
|
while (phe->next)
|
|
|
|
phe = phe->next;
|
|
|
|
|
|
|
|
/* append process */
|
|
|
|
phe->next = malloc(sizeof(struct proc_hash_entry));
|
|
|
|
memset(phe->next, 0, sizeof(struct proc_hash_entry));
|
|
|
|
phe->next->proc = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unhash_process(struct process *p)
|
|
|
|
{
|
|
|
|
struct proc_hash_entry *phe, *tmp;
|
|
|
|
|
|
|
|
/* get the bucket head */
|
|
|
|
phe = &proc_hash_table[p->pid % 256];
|
|
|
|
|
|
|
|
/* find the entry pointing to p and drop it */
|
|
|
|
while (phe->next) {
|
|
|
|
if (phe->next->proc == p) {
|
|
|
|
tmp = phe->next;
|
|
|
|
phe->next = phe->next->next;
|
|
|
|
free(tmp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
phe = phe->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __unhash_all_processes(struct proc_hash_entry *phe)
|
|
|
|
{
|
|
|
|
if (phe->next)
|
|
|
|
__unhash_all_processes(phe->next);
|
|
|
|
free(phe->next);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unhash_all_processes(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
__unhash_all_processes(&proc_hash_table[i]);
|
|
|
|
proc_hash_table[i].next = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-29 02:01:03 +00:00
|
|
|
struct process *get_first_process(void)
|
2005-11-10 04:19:43 +00:00
|
|
|
{
|
|
|
|
return first_process;
|
|
|
|
}
|
2005-11-10 01:20:19 +00:00
|
|
|
|
2008-03-29 02:01:03 +00:00
|
|
|
void free_all_processes(void)
|
2005-11-10 04:19:43 +00:00
|
|
|
{
|
2005-11-12 03:41:55 +00:00
|
|
|
struct process *next = NULL, *pr = first_process;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2005-11-10 04:19:43 +00:00
|
|
|
while (pr) {
|
|
|
|
next = pr->next;
|
|
|
|
if (pr->name) {
|
|
|
|
free(pr->name);
|
|
|
|
}
|
|
|
|
free(pr);
|
|
|
|
pr = next;
|
|
|
|
}
|
2005-11-12 03:41:55 +00:00
|
|
|
first_process = NULL;
|
2009-09-20 14:09:18 +00:00
|
|
|
|
|
|
|
/* drop the whole hash table */
|
|
|
|
unhash_all_processes();
|
2005-11-10 04:19:43 +00:00
|
|
|
}
|
2005-11-10 01:20:19 +00:00
|
|
|
|
2009-06-19 23:40:40 +00:00
|
|
|
struct process *get_process_by_name(const char *name)
|
|
|
|
{
|
|
|
|
struct process *p = first_process;
|
|
|
|
|
|
|
|
while (p) {
|
2009-11-12 20:32:59 +00:00
|
|
|
if (p->name && !strcmp(p->name, name))
|
2009-06-19 23:40:40 +00:00
|
|
|
return p;
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
static struct process *find_process(pid_t pid)
|
|
|
|
{
|
2009-09-20 14:09:18 +00:00
|
|
|
struct proc_hash_entry *phe;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2009-09-20 14:09:18 +00:00
|
|
|
phe = &proc_hash_table[pid % 256];
|
|
|
|
while (phe->next) {
|
|
|
|
if (phe->next->proc->pid == pid)
|
|
|
|
return phe->next->proc;
|
|
|
|
phe = phe->next;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/* Create a new process object and insert it into the process list */
|
2005-07-20 00:30:40 +00:00
|
|
|
static struct process *new_process(int p)
|
|
|
|
{
|
|
|
|
struct process *process;
|
2008-02-20 20:30:45 +00:00
|
|
|
process = (struct process *) malloc(sizeof(struct process));
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2005-11-09 02:15:13 +00:00
|
|
|
// clean up memory first
|
|
|
|
memset(process, 0, sizeof(struct process));
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/* Do stitching necessary for doubly linked list */
|
2005-07-20 00:30:40 +00:00
|
|
|
process->name = 0;
|
|
|
|
process->previous = 0;
|
|
|
|
process->next = first_process;
|
2008-02-20 20:30:45 +00:00
|
|
|
if (process->next) {
|
2005-07-20 00:30:40 +00:00
|
|
|
process->next->previous = process;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
first_process = process;
|
|
|
|
|
|
|
|
process->pid = p;
|
|
|
|
process->time_stamp = 0;
|
2005-11-10 01:48:36 +00:00
|
|
|
process->previous_user_time = ULONG_MAX;
|
|
|
|
process->previous_kernel_time = ULONG_MAX;
|
2009-06-12 17:08:44 +00:00
|
|
|
#ifdef IOSTATS
|
|
|
|
process->previous_read_bytes = ULLONG_MAX;
|
|
|
|
process->previous_write_bytes = ULLONG_MAX;
|
2009-08-05 20:35:20 +00:00
|
|
|
#endif /* IOSTATS */
|
2005-07-20 00:30:40 +00:00
|
|
|
process->counted = 1;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/* process_find_name(process); */
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2009-09-20 14:09:18 +00:00
|
|
|
/* add the process to the hash table */
|
|
|
|
hash_process(process);
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
return process;
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/******************************************
|
|
|
|
* Functions *
|
|
|
|
******************************************/
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/******************************************
|
|
|
|
* Extract information from /proc *
|
|
|
|
******************************************/
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/* These are the guts that extract information out of /proc.
|
|
|
|
* Anyone hoping to port wmtop should look here first. */
|
2005-07-20 00:30:40 +00:00
|
|
|
static int process_parse_stat(struct process *process)
|
|
|
|
{
|
2007-06-03 09:18:54 +00:00
|
|
|
char line[BUFFER_LEN] = { 0 }, filename[BUFFER_LEN], procname[BUFFER_LEN];
|
2005-07-20 00:30:40 +00:00
|
|
|
int ps;
|
2005-11-09 02:15:13 +00:00
|
|
|
unsigned long user_time = 0;
|
|
|
|
unsigned long kernel_time = 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
int rc;
|
|
|
|
char *r, *q;
|
|
|
|
int endl;
|
|
|
|
int nice_val;
|
2009-06-13 22:02:50 +00:00
|
|
|
char *lparen, *rparen;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
snprintf(filename, sizeof(filename), PROCFS_TEMPLATE, process->pid);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
ps = open(filename, O_RDONLY);
|
2008-02-20 20:30:45 +00:00
|
|
|
if (ps < 0) {
|
|
|
|
/* The process must have finished in the last few jiffies! */
|
2005-07-20 00:30:40 +00:00
|
|
|
return 1;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/* Mark process as up-to-date. */
|
2005-07-20 00:30:40 +00:00
|
|
|
process->time_stamp = g_time;
|
|
|
|
|
|
|
|
rc = read(ps, line, sizeof(line));
|
|
|
|
close(ps);
|
2008-02-20 20:30:45 +00:00
|
|
|
if (rc < 0) {
|
2005-07-20 00:30:40 +00:00
|
|
|
return 1;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/* Extract cpu times from data in /proc filesystem */
|
2009-06-13 22:02:50 +00:00
|
|
|
lparen = strchr(line, '(');
|
|
|
|
rparen = strrchr(line, ')');
|
|
|
|
if(!lparen || !rparen || rparen < lparen)
|
|
|
|
return 1; // this should not happen
|
|
|
|
|
|
|
|
rc = MIN((unsigned)(rparen - lparen - 1), sizeof(procname) - 1);
|
|
|
|
strncpy(procname, lparen + 1, rc);
|
|
|
|
procname[rc] = '\0';
|
|
|
|
rc = sscanf(rparen + 1, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %lu "
|
2009-08-05 20:35:20 +00:00
|
|
|
"%lu %*s %*s %*s %d %*s %*s %*s %u %u", &process->user_time,
|
|
|
|
&process->kernel_time, &nice_val, &process->vsize, &process->rss);
|
2008-02-20 20:30:45 +00:00
|
|
|
if (rc < 5) {
|
2009-09-15 20:59:37 +00:00
|
|
|
NORM_ERR("scaning data for %s failed, got only %d fields", procname, rc);
|
2005-07-20 00:30:40 +00:00
|
|
|
return 1;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
/* remove any "kdeinit: " */
|
2009-06-13 22:02:50 +00:00
|
|
|
if (procname == strstr(procname, "kdeinit")) {
|
2008-02-20 20:30:45 +00:00
|
|
|
snprintf(filename, sizeof(filename), PROCFS_CMDLINE_TEMPLATE,
|
2009-08-05 20:35:20 +00:00
|
|
|
process->pid);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
ps = open(filename, O_RDONLY);
|
2008-02-20 20:30:45 +00:00
|
|
|
if (ps < 0) {
|
|
|
|
/* The process must have finished in the last few jiffies! */
|
2005-07-20 00:30:40 +00:00
|
|
|
return 1;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
endl = read(ps, line, sizeof(line));
|
|
|
|
close(ps);
|
|
|
|
|
|
|
|
/* null terminate the input */
|
|
|
|
line[endl] = 0;
|
|
|
|
/* account for "kdeinit: " */
|
2008-02-20 20:30:45 +00:00
|
|
|
if ((char *) line == strstr(line, "kdeinit: ")) {
|
2005-07-20 00:30:40 +00:00
|
|
|
r = ((char *) line) + 9;
|
2008-02-20 20:30:45 +00:00
|
|
|
} else {
|
2005-07-20 00:30:40 +00:00
|
|
|
r = (char *) line;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2009-06-13 22:02:50 +00:00
|
|
|
q = procname;
|
2005-07-20 00:30:40 +00:00
|
|
|
/* stop at space */
|
2008-02-20 20:30:45 +00:00
|
|
|
while (*r && *r != ' ') {
|
2005-07-20 00:30:40 +00:00
|
|
|
*q++ = *r++;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
*q = 0;
|
|
|
|
}
|
2005-07-28 04:48:27 +00:00
|
|
|
|
2005-11-10 04:19:43 +00:00
|
|
|
if (process->name) {
|
2005-07-20 00:30:40 +00:00
|
|
|
free(process->name);
|
2005-11-10 04:19:43 +00:00
|
|
|
}
|
2009-06-13 22:02:50 +00:00
|
|
|
process->name = strndup(procname, text_buffer_size);
|
2005-07-20 00:30:40 +00:00
|
|
|
process->rss *= getpagesize();
|
2005-07-28 04:48:27 +00:00
|
|
|
|
2008-01-06 01:35:14 +00:00
|
|
|
process->total_cpu_time = process->user_time + process->kernel_time;
|
2008-02-20 20:30:45 +00:00
|
|
|
if (process->previous_user_time == ULONG_MAX) {
|
2005-07-20 00:30:40 +00:00
|
|
|
process->previous_user_time = process->user_time;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
if (process->previous_kernel_time == ULONG_MAX) {
|
2005-07-20 00:30:40 +00:00
|
|
|
process->previous_kernel_time = process->kernel_time;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2009-09-15 20:59:37 +00:00
|
|
|
/* strangely, the values aren't monotonous */
|
|
|
|
if (process->previous_user_time > process->user_time)
|
|
|
|
process->previous_user_time = process->user_time;
|
|
|
|
|
|
|
|
if (process->previous_kernel_time > process->kernel_time)
|
|
|
|
process->previous_kernel_time = process->kernel_time;
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
/* store the difference of the user_time */
|
|
|
|
user_time = process->user_time - process->previous_user_time;
|
|
|
|
kernel_time = process->kernel_time - process->previous_kernel_time;
|
|
|
|
|
|
|
|
/* backup the process->user_time for next time around */
|
|
|
|
process->previous_user_time = process->user_time;
|
|
|
|
process->previous_kernel_time = process->kernel_time;
|
|
|
|
|
|
|
|
/* store only the difference of the user_time here... */
|
|
|
|
process->user_time = user_time;
|
|
|
|
process->kernel_time = kernel_time;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-12 17:08:44 +00:00
|
|
|
#ifdef IOSTATS
|
|
|
|
static int process_parse_io(struct process *process)
|
|
|
|
{
|
|
|
|
static const char *read_bytes_str="read_bytes:";
|
|
|
|
static const char *write_bytes_str="write_bytes:";
|
|
|
|
|
|
|
|
char line[BUFFER_LEN] = { 0 }, filename[BUFFER_LEN];
|
|
|
|
int ps;
|
|
|
|
int rc;
|
|
|
|
char *pos, *endpos;
|
|
|
|
unsigned long long read_bytes, write_bytes;
|
|
|
|
|
|
|
|
snprintf(filename, sizeof(filename), PROCFS_TEMPLATE_IO, process->pid);
|
|
|
|
|
|
|
|
ps = open(filename, O_RDONLY);
|
|
|
|
if (ps < 0) {
|
|
|
|
/* The process must have finished in the last few jiffies!
|
|
|
|
* Or, the kernel doesn't support I/O accounting.
|
|
|
|
*/
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = read(ps, line, sizeof(line));
|
|
|
|
close(ps);
|
|
|
|
if (rc < 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = strstr(line, read_bytes_str);
|
|
|
|
if (pos == NULL) {
|
|
|
|
/* these should not happen (unless the format of the file changes) */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
pos += strlen(read_bytes_str);
|
|
|
|
process->read_bytes = strtoull(pos, &endpos, 10);
|
|
|
|
if (endpos == pos) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = strstr(line, write_bytes_str);
|
|
|
|
if (pos == NULL) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
pos += strlen(write_bytes_str);
|
|
|
|
process->write_bytes = strtoull(pos, &endpos, 10);
|
|
|
|
if (endpos == pos) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process->previous_read_bytes == ULLONG_MAX) {
|
|
|
|
process->previous_read_bytes = process->read_bytes;
|
|
|
|
}
|
|
|
|
if (process->previous_write_bytes == ULLONG_MAX) {
|
|
|
|
process->previous_write_bytes = process->write_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* store the difference of the byte counts */
|
|
|
|
read_bytes = process->read_bytes - process->previous_read_bytes;
|
|
|
|
write_bytes = process->write_bytes - process->previous_write_bytes;
|
|
|
|
|
|
|
|
/* backup the counts for next time around */
|
|
|
|
process->previous_read_bytes = process->read_bytes;
|
|
|
|
process->previous_write_bytes = process->write_bytes;
|
|
|
|
|
|
|
|
/* store only the difference here... */
|
|
|
|
process->read_bytes = read_bytes;
|
|
|
|
process->write_bytes = write_bytes;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2009-08-05 20:35:20 +00:00
|
|
|
#endif /* IOSTATS */
|
2009-06-12 17:08:44 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/******************************************
|
|
|
|
* Get process structure for process pid *
|
|
|
|
******************************************/
|
|
|
|
|
|
|
|
/* This function seems to hog all of the CPU time.
|
|
|
|
* I can't figure out why - it doesn't do much. */
|
2009-06-12 17:08:44 +00:00
|
|
|
static int calculate_stats(struct process *process)
|
2008-02-20 20:30:45 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* compute each process cpu usage by reading /proc/<proc#>/stat */
|
|
|
|
rc = process_parse_stat(process);
|
2009-08-05 20:35:20 +00:00
|
|
|
if (rc) return 1;
|
2008-02-20 20:30:45 +00:00
|
|
|
/* rc = process_parse_statm(process); if (rc) return 1; */
|
|
|
|
|
2009-06-12 17:08:44 +00:00
|
|
|
#ifdef IOSTATS
|
|
|
|
rc = process_parse_io(process);
|
2009-08-05 20:35:20 +00:00
|
|
|
if (rc) return 1;
|
|
|
|
#endif /* IOSTATS */
|
2009-06-12 17:08:44 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/*
|
|
|
|
* Check name against the exclusion list
|
|
|
|
*/
|
|
|
|
/* if (process->counted && exclusion_expression &&
|
|
|
|
* !regexec(exclusion_expression, process->name, 0, 0, 0))
|
|
|
|
* process->counted = 0; */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************
|
|
|
|
* Update process table *
|
|
|
|
******************************************/
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-03-29 02:01:03 +00:00
|
|
|
static int update_process_table(void)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (!(dir = opendir("/proc"))) {
|
2005-07-20 00:30:40 +00:00
|
|
|
return 1;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
++g_time;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/* Get list of processes from /proc directory */
|
2005-07-20 00:30:40 +00:00
|
|
|
while ((entry = readdir(dir))) {
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
if (!entry) {
|
2008-02-20 20:30:45 +00:00
|
|
|
/* Problem reading list of processes */
|
2005-07-20 00:30:40 +00:00
|
|
|
closedir(dir);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sscanf(entry->d_name, "%d", &pid) > 0) {
|
|
|
|
struct process *p;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
p = find_process(pid);
|
2008-02-20 20:30:45 +00:00
|
|
|
if (!p) {
|
2005-07-20 00:30:40 +00:00
|
|
|
p = new_process(pid);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
/* compute each process cpu usage */
|
2009-06-12 17:08:44 +00:00
|
|
|
calculate_stats(p);
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/******************************************
|
|
|
|
* Destroy and remove a process *
|
|
|
|
******************************************/
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
static void delete_process(struct process *p)
|
|
|
|
{
|
|
|
|
#if defined(PARANOID)
|
|
|
|
assert(p->id == 0x0badfeed);
|
|
|
|
|
|
|
|
/*
|
2005-07-28 04:48:27 +00:00
|
|
|
* Ensure that deleted processes aren't reused.
|
|
|
|
*/
|
2005-07-20 00:30:40 +00:00
|
|
|
p->id = 0x007babe;
|
2008-02-20 20:30:45 +00:00
|
|
|
#endif /* defined(PARANOID) */
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
/*
|
2005-07-28 04:48:27 +00:00
|
|
|
* Maintain doubly linked list.
|
|
|
|
*/
|
2005-07-20 00:30:40 +00:00
|
|
|
if (p->next)
|
|
|
|
p->next->previous = p->previous;
|
|
|
|
if (p->previous)
|
|
|
|
p->previous->next = p->next;
|
|
|
|
else
|
|
|
|
first_process = p->next;
|
|
|
|
|
2005-11-10 04:19:43 +00:00
|
|
|
if (p->name) {
|
2005-07-20 00:30:40 +00:00
|
|
|
free(p->name);
|
2005-11-10 04:19:43 +00:00
|
|
|
}
|
2009-09-20 14:09:18 +00:00
|
|
|
/* remove the process from the hash table */
|
|
|
|
unhash_process(p);
|
2005-07-20 00:30:40 +00:00
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/******************************************
|
|
|
|
* Strip dead process entries *
|
|
|
|
******************************************/
|
|
|
|
|
2008-03-29 02:01:03 +00:00
|
|
|
static void process_cleanup(void)
|
2008-02-20 20:30:45 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
struct process *p = first_process;
|
|
|
|
|
|
|
|
while (p) {
|
|
|
|
struct process *current = p;
|
|
|
|
|
|
|
|
#if defined(PARANOID)
|
|
|
|
assert(p->id == 0x0badfeed);
|
|
|
|
#endif /* defined(PARANOID) */
|
|
|
|
|
|
|
|
p = p->next;
|
|
|
|
/* Delete processes that have died */
|
|
|
|
if (current->time_stamp != g_time) {
|
|
|
|
delete_process(current);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************
|
|
|
|
* Calculate cpu total *
|
|
|
|
******************************************/
|
2005-11-27 06:56:35 +00:00
|
|
|
#define TMPL_SHORTPROC "%*s %llu %llu %llu %llu"
|
|
|
|
#define TMPL_LONGPROC "%*s %llu %llu %llu %llu %llu %llu %llu %llu"
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-03-29 02:01:03 +00:00
|
|
|
static unsigned long long calc_cpu_total(void)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
2005-11-27 06:56:35 +00:00
|
|
|
unsigned long long total = 0;
|
|
|
|
unsigned long long t = 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
int rc;
|
|
|
|
int ps;
|
2007-06-03 09:18:54 +00:00
|
|
|
char line[BUFFER_LEN] = { 0 };
|
2005-11-27 06:56:35 +00:00
|
|
|
unsigned long long cpu = 0;
|
2008-03-29 09:58:09 +00:00
|
|
|
unsigned long long niceval = 0;
|
|
|
|
unsigned long long systemval = 0;
|
2005-11-27 06:56:35 +00:00
|
|
|
unsigned long long idle = 0;
|
|
|
|
unsigned long long iowait = 0;
|
|
|
|
unsigned long long irq = 0;
|
|
|
|
unsigned long long softirq = 0;
|
|
|
|
unsigned long long steal = 0;
|
2008-03-29 03:45:36 +00:00
|
|
|
const char *template =
|
2008-02-20 20:30:45 +00:00
|
|
|
KFLAG_ISSET(KFLAG_IS_LONGSTAT) ? TMPL_LONGPROC : TMPL_SHORTPROC;
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
ps = open("/proc/stat", O_RDONLY);
|
|
|
|
rc = read(ps, line, sizeof(line));
|
|
|
|
close(ps);
|
2008-02-20 20:30:45 +00:00
|
|
|
if (rc < 0) {
|
2005-07-20 00:30:40 +00:00
|
|
|
return 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-11-27 06:56:35 +00:00
|
|
|
|
2008-03-29 09:58:09 +00:00
|
|
|
sscanf(line, template, &cpu, &niceval, &systemval, &idle, &iowait, &irq,
|
2009-08-05 20:35:20 +00:00
|
|
|
&softirq, &steal);
|
2008-03-29 09:58:09 +00:00
|
|
|
total = cpu + niceval + systemval + idle + iowait + irq + softirq + steal;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
t = total - previous_total;
|
|
|
|
previous_total = total;
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/******************************************
|
|
|
|
* Calculate each processes cpu *
|
|
|
|
******************************************/
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2005-11-27 06:56:35 +00:00
|
|
|
inline static void calc_cpu_each(unsigned long long total)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
|
|
|
struct process *p = first_process;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
while (p) {
|
2008-02-20 20:30:45 +00:00
|
|
|
p->amount = 100.0 * (cpu_separate ? info.cpu_count : 1) *
|
|
|
|
(p->user_time + p->kernel_time) / (float) total;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-12 17:08:44 +00:00
|
|
|
#ifdef IOSTATS
|
|
|
|
static void calc_io_each(void)
|
|
|
|
{
|
|
|
|
struct process *p;
|
|
|
|
unsigned long long sum = 0;
|
2009-08-05 20:35:20 +00:00
|
|
|
|
2009-06-12 17:08:44 +00:00
|
|
|
for (p = first_process; p; p = p->next)
|
|
|
|
sum += p->read_bytes + p->write_bytes;
|
|
|
|
|
|
|
|
if(sum == 0)
|
|
|
|
sum = 1; /* to avoid having NANs if no I/O occured */
|
|
|
|
for (p = first_process; p; p = p->next)
|
|
|
|
p->io_perc = 100.0 * (p->read_bytes + p->write_bytes) / (float) sum;
|
|
|
|
}
|
2009-08-05 20:35:20 +00:00
|
|
|
#endif /* IOSTATS */
|
2009-06-12 17:08:44 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/******************************************
|
|
|
|
* Find the top processes *
|
|
|
|
******************************************/
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/* free a sp_process structure */
|
2009-02-08 15:13:45 +00:00
|
|
|
static void free_sp(struct sorted_process *sp)
|
2008-02-20 20:30:45 +00:00
|
|
|
{
|
2005-11-12 02:57:48 +00:00
|
|
|
free(sp);
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/* create a new sp_process structure */
|
2009-02-08 15:13:45 +00:00
|
|
|
static struct sorted_process *malloc_sp(struct process *proc)
|
2008-02-20 20:30:45 +00:00
|
|
|
{
|
|
|
|
struct sorted_process *sp;
|
2005-11-12 02:57:48 +00:00
|
|
|
sp = malloc(sizeof(struct sorted_process));
|
2009-09-14 00:07:45 +00:00
|
|
|
memset(sp, 0, sizeof(struct sorted_process));
|
2005-11-12 02:57:48 +00:00
|
|
|
sp->proc = proc;
|
2008-02-20 20:30:45 +00:00
|
|
|
return sp;
|
2005-11-10 01:20:19 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/* cpu comparison function for insert_sp_element */
|
2009-02-08 15:13:45 +00:00
|
|
|
static int compare_cpu(struct process *a, struct process *b)
|
2008-02-20 20:30:45 +00:00
|
|
|
{
|
|
|
|
if (a->amount < b->amount) {
|
|
|
|
return 1;
|
|
|
|
} else if (a->amount > b->amount) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* mem comparison function for insert_sp_element */
|
2009-02-08 15:13:45 +00:00
|
|
|
static int compare_mem(struct process *a, struct process *b)
|
2008-02-20 20:30:45 +00:00
|
|
|
{
|
2009-11-03 22:11:14 +00:00
|
|
|
if (a->rss < b->rss) {
|
2008-02-20 20:30:45 +00:00
|
|
|
return 1;
|
2009-11-03 22:11:14 +00:00
|
|
|
} else if (a->rss > b->rss) {
|
2008-02-20 20:30:45 +00:00
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2005-08-25 01:13:01 +00:00
|
|
|
}
|
|
|
|
|
2009-02-07 14:01:50 +00:00
|
|
|
/* CPU time comparision function for insert_sp_element */
|
2009-02-08 15:13:45 +00:00
|
|
|
static int compare_time(struct process *a, struct process *b)
|
2009-02-07 14:01:50 +00:00
|
|
|
{
|
|
|
|
return b->total_cpu_time - a->total_cpu_time;
|
|
|
|
}
|
|
|
|
|
2009-06-12 17:08:44 +00:00
|
|
|
#ifdef IOSTATS
|
|
|
|
/* I/O comparision function for insert_sp_element */
|
|
|
|
static int compare_io(struct process *a, struct process *b)
|
|
|
|
{
|
|
|
|
if (a->io_perc < b->io_perc) {
|
|
|
|
return 1;
|
|
|
|
} else if (a->io_perc > b->io_perc) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2009-08-05 20:35:20 +00:00
|
|
|
#endif /* IOSTATS */
|
2009-06-12 17:08:44 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/* insert this process into the list in a sorted fashion,
|
|
|
|
* or destroy it if it doesn't fit on the list */
|
2009-02-08 15:13:45 +00:00
|
|
|
static int insert_sp_element(struct sorted_process *sp_cur,
|
2008-02-20 20:30:45 +00:00
|
|
|
struct sorted_process **p_sp_head, struct sorted_process **p_sp_tail,
|
|
|
|
int max_elements, int compare_funct(struct process *, struct process *))
|
|
|
|
{
|
|
|
|
|
|
|
|
struct sorted_process *sp_readthru = NULL, *sp_destroy = NULL;
|
2005-11-10 01:20:19 +00:00
|
|
|
int did_insert = 0, x = 0;
|
|
|
|
|
|
|
|
if (*p_sp_head == NULL) {
|
|
|
|
*p_sp_head = sp_cur;
|
|
|
|
*p_sp_tail = sp_cur;
|
2008-02-20 20:30:45 +00:00
|
|
|
return 1;
|
2005-11-10 01:48:36 +00:00
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
for (sp_readthru = *p_sp_head, x = 0;
|
|
|
|
sp_readthru != NULL && x < max_elements;
|
|
|
|
sp_readthru = sp_readthru->less, x++) {
|
|
|
|
if (compare_funct(sp_readthru->proc, sp_cur->proc) > 0 && !did_insert) {
|
|
|
|
/* sp_cur is bigger than sp_readthru
|
|
|
|
* so insert it before sp_readthru */
|
|
|
|
sp_cur->less = sp_readthru;
|
|
|
|
if (sp_readthru == *p_sp_head) {
|
|
|
|
/* insert as the new head of the list */
|
|
|
|
*p_sp_head = sp_cur;
|
2005-11-10 01:20:19 +00:00
|
|
|
} else {
|
2008-02-20 20:30:45 +00:00
|
|
|
/* insert inside the list */
|
|
|
|
sp_readthru->greater->less = sp_cur;
|
|
|
|
sp_cur->greater = sp_readthru->greater;
|
2005-11-10 01:20:19 +00:00
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
sp_readthru->greater = sp_cur;
|
|
|
|
/* element was inserted, so increase the counter */
|
|
|
|
did_insert = ++x;
|
2005-11-10 01:20:19 +00:00
|
|
|
}
|
2005-11-10 04:19:43 +00:00
|
|
|
}
|
2005-11-10 01:20:19 +00:00
|
|
|
if (x < max_elements && sp_readthru == NULL && !did_insert) {
|
2008-02-20 20:30:45 +00:00
|
|
|
/* sp_cur is the smallest element and list isn't full,
|
|
|
|
* so insert at the end */
|
|
|
|
(*p_sp_tail)->less = sp_cur;
|
|
|
|
sp_cur->greater = *p_sp_tail;
|
2005-11-10 01:20:19 +00:00
|
|
|
*p_sp_tail = sp_cur;
|
2008-02-20 20:30:45 +00:00
|
|
|
did_insert = x;
|
2005-11-12 02:57:48 +00:00
|
|
|
} else if (x >= max_elements) {
|
2008-02-20 20:30:45 +00:00
|
|
|
/* We inserted an element and now the list is too big by one.
|
|
|
|
* Destroy the smallest element */
|
2005-11-12 02:57:48 +00:00
|
|
|
sp_destroy = *p_sp_tail;
|
|
|
|
*p_sp_tail = sp_destroy->greater;
|
|
|
|
(*p_sp_tail)->less = NULL;
|
|
|
|
free_sp(sp_destroy);
|
2005-08-25 01:13:01 +00:00
|
|
|
}
|
2005-11-10 01:20:19 +00:00
|
|
|
if (!did_insert) {
|
|
|
|
/* sp_cur wasn't added to the sorted list, so destroy it */
|
2005-11-12 02:57:48 +00:00
|
|
|
free_sp(sp_cur);
|
2005-11-10 01:20:19 +00:00
|
|
|
}
|
|
|
|
return did_insert;
|
2005-11-10 01:48:36 +00:00
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
/* copy the procs in the sorted list to the array, and destroy the list */
|
2009-02-08 15:13:45 +00:00
|
|
|
static void sp_acopy(struct sorted_process *sp_head, struct process **ar, int max_size)
|
2005-11-10 04:19:43 +00:00
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
struct sorted_process *sp_cur, *sp_tmp;
|
2005-11-10 01:20:19 +00:00
|
|
|
int x;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2005-11-10 01:20:19 +00:00
|
|
|
sp_cur = sp_head;
|
2008-02-20 20:30:45 +00:00
|
|
|
for (x = 0; x < max_size && sp_cur != NULL; x++) {
|
|
|
|
ar[x] = sp_cur->proc;
|
2005-11-10 01:20:19 +00:00
|
|
|
sp_tmp = sp_cur;
|
2008-02-20 20:30:45 +00:00
|
|
|
sp_cur = sp_cur->less;
|
|
|
|
free_sp(sp_tmp);
|
2005-08-25 01:13:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/* ****************************************************************** *
|
|
|
|
* Get a sorted list of the top cpu hogs and top mem hogs. *
|
|
|
|
* Results are stored in the cpu,mem arrays in decreasing order[0-9]. *
|
|
|
|
* ****************************************************************** */
|
2005-11-10 01:20:19 +00:00
|
|
|
|
2009-02-07 14:01:50 +00:00
|
|
|
void process_find_top(struct process **cpu, struct process **mem,
|
2009-06-12 17:08:44 +00:00
|
|
|
struct process **ptime
|
|
|
|
#ifdef IOSTATS
|
|
|
|
, struct process **io
|
2009-08-05 20:35:20 +00:00
|
|
|
#endif /* IOSTATS */
|
2009-06-12 17:08:44 +00:00
|
|
|
)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
2005-11-10 04:19:43 +00:00
|
|
|
struct sorted_process *spc_head = NULL, *spc_tail = NULL, *spc_cur = NULL;
|
|
|
|
struct sorted_process *spm_head = NULL, *spm_tail = NULL, *spm_cur = NULL;
|
2009-02-07 14:01:50 +00:00
|
|
|
struct sorted_process *spt_head = NULL, *spt_tail = NULL, *spt_cur = NULL;
|
2009-06-12 17:08:44 +00:00
|
|
|
#ifdef IOSTATS
|
|
|
|
struct sorted_process *spi_head = NULL, *spi_tail = NULL, *spi_cur = NULL;
|
2009-08-05 20:35:20 +00:00
|
|
|
#endif /* IOSTATS */
|
2005-11-10 04:19:43 +00:00
|
|
|
struct process *cur_proc = NULL;
|
2005-11-27 06:56:35 +00:00
|
|
|
unsigned long long total = 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2009-06-12 17:08:44 +00:00
|
|
|
if (!top_cpu && !top_mem && !top_time
|
|
|
|
#ifdef IOSTATS
|
|
|
|
&& !top_io
|
2009-08-05 20:35:20 +00:00
|
|
|
#endif /* IOSTATS */
|
2009-09-03 21:13:14 +00:00
|
|
|
&& !top_running
|
2009-06-12 17:08:44 +00:00
|
|
|
) {
|
2008-02-20 20:30:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2005-11-10 01:20:19 +00:00
|
|
|
total = calc_cpu_total(); /* calculate the total of the processor */
|
2008-02-20 20:30:45 +00:00
|
|
|
update_process_table(); /* update the table with process list */
|
|
|
|
calc_cpu_each(total); /* and then the percentage for each task */
|
|
|
|
process_cleanup(); /* cleanup list from exited processes */
|
2009-06-12 17:08:44 +00:00
|
|
|
#ifdef IOSTATS
|
|
|
|
calc_io_each(); /* percentage of I/O for each task */
|
2009-08-05 20:35:20 +00:00
|
|
|
#endif /* IOSTATS */
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2005-11-10 01:20:19 +00:00
|
|
|
cur_proc = first_process;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
while (cur_proc != NULL) {
|
2005-11-10 04:19:43 +00:00
|
|
|
if (top_cpu) {
|
2005-11-10 01:20:19 +00:00
|
|
|
spc_cur = malloc_sp(cur_proc);
|
2008-02-20 20:30:45 +00:00
|
|
|
insert_sp_element(spc_cur, &spc_head, &spc_tail, MAX_SP,
|
2009-08-05 20:35:20 +00:00
|
|
|
&compare_cpu);
|
2005-11-10 04:19:43 +00:00
|
|
|
}
|
2005-11-10 01:20:19 +00:00
|
|
|
if (top_mem) {
|
|
|
|
spm_cur = malloc_sp(cur_proc);
|
2008-02-20 20:30:45 +00:00
|
|
|
insert_sp_element(spm_cur, &spm_head, &spm_tail, MAX_SP,
|
2009-08-05 20:35:20 +00:00
|
|
|
&compare_mem);
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
2009-02-07 14:01:50 +00:00
|
|
|
if (top_time) {
|
|
|
|
spt_cur = malloc_sp(cur_proc);
|
|
|
|
insert_sp_element(spt_cur, &spt_head, &spt_tail, MAX_SP,
|
2009-08-05 20:35:20 +00:00
|
|
|
&compare_time);
|
2009-02-07 14:01:50 +00:00
|
|
|
}
|
2009-06-12 17:08:44 +00:00
|
|
|
#ifdef IOSTATS
|
|
|
|
if (top_io) {
|
|
|
|
spi_cur = malloc_sp(cur_proc);
|
|
|
|
insert_sp_element(spi_cur, &spi_head, &spi_tail, MAX_SP,
|
2009-08-05 20:35:20 +00:00
|
|
|
&compare_io);
|
2009-06-12 17:08:44 +00:00
|
|
|
}
|
2009-08-05 20:35:20 +00:00
|
|
|
#endif /* IOSTATS */
|
2005-11-10 01:20:19 +00:00
|
|
|
cur_proc = cur_proc->next;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
2009-02-08 15:05:42 +00:00
|
|
|
|
2009-08-05 20:35:20 +00:00
|
|
|
if (top_cpu) sp_acopy(spc_head, cpu, MAX_SP);
|
|
|
|
if (top_mem) sp_acopy(spm_head, mem, MAX_SP);
|
|
|
|
if (top_time) sp_acopy(spt_head, ptime, MAX_SP);
|
2009-06-12 17:08:44 +00:00
|
|
|
#ifdef IOSTATS
|
2009-08-05 20:35:20 +00:00
|
|
|
if (top_io) sp_acopy(spi_head, io, MAX_SP);
|
|
|
|
#endif /* IOSTATS */
|
2005-11-10 04:19:43 +00:00
|
|
|
}
|
2009-08-05 22:46:51 +00:00
|
|
|
|
2009-10-06 20:33:28 +00:00
|
|
|
struct top_data {
|
|
|
|
int num;
|
|
|
|
int type;
|
|
|
|
int was_parsed;
|
|
|
|
char *s;
|
|
|
|
};
|
|
|
|
|
2009-08-05 22:46:51 +00:00
|
|
|
int parse_top_args(const char *s, const char *arg, struct text_object *obj)
|
|
|
|
{
|
2009-10-06 20:33:28 +00:00
|
|
|
struct top_data *td;
|
2009-08-05 22:46:51 +00:00
|
|
|
char buf[64];
|
|
|
|
int n;
|
|
|
|
|
2009-10-06 20:33:28 +00:00
|
|
|
if (!arg) {
|
|
|
|
NORM_ERR("top needs arguments");
|
|
|
|
return 0;
|
2009-08-05 22:46:51 +00:00
|
|
|
}
|
|
|
|
|
2009-10-06 20:33:28 +00:00
|
|
|
if (obj->data.opaque) {
|
|
|
|
return 1;
|
2009-08-05 22:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s[3] == 0) {
|
|
|
|
obj->type = OBJ_top;
|
|
|
|
top_cpu = 1;
|
|
|
|
} else if (strcmp(&s[3], "_mem") == EQUAL) {
|
|
|
|
obj->type = OBJ_top_mem;
|
|
|
|
top_mem = 1;
|
|
|
|
} else if (strcmp(&s[3], "_time") == EQUAL) {
|
|
|
|
obj->type = OBJ_top_time;
|
|
|
|
top_time = 1;
|
|
|
|
#ifdef IOSTATS
|
|
|
|
} else if (strcmp(&s[3], "_io") == EQUAL) {
|
|
|
|
obj->type = OBJ_top_io;
|
|
|
|
top_io = 1;
|
|
|
|
#endif /* IOSTATS */
|
|
|
|
} else {
|
|
|
|
#ifdef IOSTATS
|
|
|
|
NORM_ERR("Must be top, top_mem, top_time or top_io");
|
|
|
|
#else /* IOSTATS */
|
|
|
|
NORM_ERR("Must be top, top_mem or top_time");
|
|
|
|
#endif /* IOSTATS */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-06 20:33:28 +00:00
|
|
|
obj->data.opaque = td = malloc(sizeof(struct top_data));
|
|
|
|
memset(td, 0, sizeof(struct top_data));
|
|
|
|
td->s = strndup(arg, text_buffer_size);
|
2009-08-05 22:46:51 +00:00
|
|
|
|
|
|
|
if (sscanf(arg, "%63s %i", buf, &n) == 2) {
|
|
|
|
if (strcmp(buf, "name") == EQUAL) {
|
2009-10-06 20:33:28 +00:00
|
|
|
td->type = TOP_NAME;
|
2009-08-05 22:46:51 +00:00
|
|
|
} else if (strcmp(buf, "cpu") == EQUAL) {
|
2009-10-06 20:33:28 +00:00
|
|
|
td->type = TOP_CPU;
|
2009-08-05 22:46:51 +00:00
|
|
|
} else if (strcmp(buf, "pid") == EQUAL) {
|
2009-10-06 20:33:28 +00:00
|
|
|
td->type = TOP_PID;
|
2009-08-05 22:46:51 +00:00
|
|
|
} else if (strcmp(buf, "mem") == EQUAL) {
|
2009-10-06 20:33:28 +00:00
|
|
|
td->type = TOP_MEM;
|
2009-08-05 22:46:51 +00:00
|
|
|
} else if (strcmp(buf, "time") == EQUAL) {
|
2009-10-06 20:33:28 +00:00
|
|
|
td->type = TOP_TIME;
|
2009-08-05 22:46:51 +00:00
|
|
|
} else if (strcmp(buf, "mem_res") == EQUAL) {
|
2009-10-06 20:33:28 +00:00
|
|
|
td->type = TOP_MEM_RES;
|
2009-08-05 22:46:51 +00:00
|
|
|
} else if (strcmp(buf, "mem_vsize") == EQUAL) {
|
2009-10-06 20:33:28 +00:00
|
|
|
td->type = TOP_MEM_VSIZE;
|
2009-08-05 22:46:51 +00:00
|
|
|
#ifdef IOSTATS
|
|
|
|
} else if (strcmp(buf, "io_read") == EQUAL) {
|
2009-10-06 20:33:28 +00:00
|
|
|
td->type = TOP_READ_BYTES;
|
2009-08-05 22:46:51 +00:00
|
|
|
} else if (strcmp(buf, "io_write") == EQUAL) {
|
2009-10-06 20:33:28 +00:00
|
|
|
td->type = TOP_WRITE_BYTES;
|
2009-08-05 22:46:51 +00:00
|
|
|
} else if (strcmp(buf, "io_perc") == EQUAL) {
|
2009-10-06 20:33:28 +00:00
|
|
|
td->type = TOP_IO_PERC;
|
2009-08-05 22:46:51 +00:00
|
|
|
#endif /* IOSTATS */
|
|
|
|
} else {
|
|
|
|
NORM_ERR("invalid type arg for top");
|
|
|
|
#ifdef IOSTATS
|
|
|
|
NORM_ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize, "
|
|
|
|
"io_read, io_write, io_perc");
|
|
|
|
#else /* IOSTATS */
|
|
|
|
NORM_ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize");
|
|
|
|
#endif /* IOSTATS */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (n < 1 || n > 10) {
|
|
|
|
NORM_ERR("invalid num arg for top. Must be between 1 and 10.");
|
|
|
|
return 0;
|
|
|
|
} else {
|
2009-10-06 20:33:28 +00:00
|
|
|
td->num = n - 1;
|
2009-08-05 22:46:51 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
NORM_ERR("invalid argument count for top");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-10-06 20:33:28 +00:00
|
|
|
static char *format_time(unsigned long timeval, const int width)
|
|
|
|
{
|
|
|
|
char buf[10];
|
|
|
|
unsigned long nt; // narrow time, for speed on 32-bit
|
|
|
|
unsigned cc; // centiseconds
|
|
|
|
unsigned nn; // multi-purpose whatever
|
|
|
|
|
|
|
|
nt = timeval;
|
|
|
|
cc = nt % 100; // centiseconds past second
|
|
|
|
nt /= 100; // total seconds
|
|
|
|
nn = nt % 60; // seconds past the minute
|
|
|
|
nt /= 60; // total minutes
|
|
|
|
if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
|
|
|
|
nt, nn, cc)) {
|
|
|
|
return strndup(buf, text_buffer_size);
|
|
|
|
}
|
|
|
|
if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
|
|
|
|
return strndup(buf, text_buffer_size);
|
|
|
|
}
|
|
|
|
nn = nt % 60; // minutes past the hour
|
|
|
|
nt /= 60; // total hours
|
|
|
|
if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
|
|
|
|
return strndup(buf, text_buffer_size);
|
|
|
|
}
|
|
|
|
nn = nt; // now also hours
|
|
|
|
if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
|
|
|
|
return strndup(buf, text_buffer_size);
|
|
|
|
}
|
|
|
|
nn /= 24; // now days
|
|
|
|
if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
|
|
|
|
return strndup(buf, text_buffer_size);
|
|
|
|
}
|
|
|
|
nn /= 7; // now weeks
|
|
|
|
if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
|
|
|
|
return strndup(buf, text_buffer_size);
|
|
|
|
}
|
|
|
|
// well shoot, this outta' fit...
|
|
|
|
return strndup("<inf>", text_buffer_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_top(struct text_object *obj, char *p, int top_name_width)
|
|
|
|
{
|
|
|
|
struct information *cur = &info;
|
|
|
|
struct top_data *td = obj->data.opaque;
|
|
|
|
struct process **needed = 0;
|
|
|
|
|
|
|
|
if (!td)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (obj->type) {
|
|
|
|
case OBJ_top:
|
|
|
|
needed = cur->cpu;
|
|
|
|
break;
|
|
|
|
case OBJ_top_mem:
|
|
|
|
needed = cur->memu;
|
|
|
|
break;
|
|
|
|
case OBJ_top_time:
|
|
|
|
needed = cur->time;
|
|
|
|
break;
|
2009-11-07 12:21:47 +00:00
|
|
|
#ifdef IOSTATS
|
2009-10-06 20:33:28 +00:00
|
|
|
case OBJ_top_io:
|
|
|
|
needed = cur->io;
|
|
|
|
break;
|
2009-11-07 12:21:47 +00:00
|
|
|
#endif /* IOSTATS */
|
2009-10-06 20:33:28 +00:00
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2009-08-05 22:46:51 +00:00
|
|
|
|
2009-10-06 20:33:28 +00:00
|
|
|
if (needed[td->num]) {
|
|
|
|
char *timeval;
|
|
|
|
|
|
|
|
switch (td->type) {
|
|
|
|
case TOP_NAME:
|
|
|
|
snprintf(p, top_name_width + 1, "%-*s", top_name_width,
|
|
|
|
needed[td->num]->name);
|
|
|
|
break;
|
|
|
|
case TOP_CPU:
|
|
|
|
snprintf(p, 7, "%6.2f",
|
|
|
|
needed[td->num]->amount);
|
|
|
|
break;
|
|
|
|
case TOP_PID:
|
|
|
|
snprintf(p, 6, "%5i",
|
|
|
|
needed[td->num]->pid);
|
|
|
|
break;
|
|
|
|
case TOP_MEM:
|
2009-11-03 22:11:14 +00:00
|
|
|
/* Calculate a percentage of residential mem from total mem available.
|
|
|
|
* Since rss is bytes and memmax kilobytes, dividing by 10 suffices here. */
|
2009-10-06 20:33:28 +00:00
|
|
|
snprintf(p, 7, "%6.2f",
|
2009-11-03 22:11:14 +00:00
|
|
|
(float) ((float)needed[td->num]->rss / cur->memmax) / 10);
|
2009-10-06 20:33:28 +00:00
|
|
|
break;
|
|
|
|
case TOP_TIME:
|
|
|
|
timeval = format_time(
|
|
|
|
needed[td->num]->total_cpu_time, 9);
|
|
|
|
snprintf(p, 10, "%9s", timeval);
|
|
|
|
free(timeval);
|
|
|
|
break;
|
|
|
|
case TOP_MEM_RES:
|
|
|
|
human_readable(needed[td->num]->rss,
|
|
|
|
p, 255);
|
|
|
|
break;
|
|
|
|
case TOP_MEM_VSIZE:
|
|
|
|
human_readable(needed[td->num]->vsize,
|
|
|
|
p, 255);
|
|
|
|
break;
|
|
|
|
#ifdef IOSTATS
|
|
|
|
case TOP_READ_BYTES:
|
|
|
|
human_readable(needed[td->num]->read_bytes / update_interval,
|
|
|
|
p, 255);
|
|
|
|
break;
|
|
|
|
case TOP_WRITE_BYTES:
|
|
|
|
human_readable(needed[td->num]->write_bytes / update_interval,
|
|
|
|
p, 255);
|
|
|
|
break;
|
|
|
|
case TOP_IO_PERC:
|
|
|
|
snprintf(p, 7, "%6.2f",
|
|
|
|
needed[td->num]->io_perc);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_top(struct text_object *obj, int internal)
|
|
|
|
{
|
|
|
|
struct top_data *td = obj->data.opaque;
|
|
|
|
|
|
|
|
if (info.first_process && !internal) {
|
|
|
|
free_all_processes();
|
|
|
|
info.first_process = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!td)
|
|
|
|
return;
|
|
|
|
if (td->s)
|
|
|
|
free(td->s);
|
|
|
|
free(obj->data.opaque);
|
|
|
|
obj->data.opaque = NULL;
|
|
|
|
}
|