2010-01-04 17:06:14 +00:00
|
|
|
/* -*- mode: c++; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
|
|
|
|
* vim: ts=4 sw=4 noet ai cindent syntax=cpp
|
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>
|
2010-01-01 23:46:17 +00:00
|
|
|
* Copyright (c) 2005-2010 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
|
|
|
|
2009-12-06 16:17:27 +00:00
|
|
|
#include "prioqueue.h"
|
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
|
|
|
|
2009-12-06 20:34:36 +00:00
|
|
|
/* hash table size - always a power of 2 */
|
|
|
|
#define HTABSIZE 256
|
|
|
|
|
2010-05-30 11:55:50 +00:00
|
|
|
struct process *first_process = 0;
|
|
|
|
|
|
|
|
unsigned long g_time = 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
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;
|
|
|
|
};
|
2009-12-06 20:34:36 +00:00
|
|
|
static struct proc_hash_entry proc_hash_table[HTABSIZE];
|
2009-09-20 14:09:18 +00:00
|
|
|
|
|
|
|
static void hash_process(struct process *p)
|
|
|
|
{
|
|
|
|
struct proc_hash_entry *phe;
|
|
|
|
static char first_run = 1;
|
2009-12-06 16:27:51 +00:00
|
|
|
int bucket;
|
2009-09-20 14:09:18 +00:00
|
|
|
|
|
|
|
/* better make sure all next pointers are zero upon first access */
|
|
|
|
if (first_run) {
|
2009-12-06 20:34:36 +00:00
|
|
|
memset(proc_hash_table, 0, sizeof(struct proc_hash_entry) * HTABSIZE);
|
2009-09-20 14:09:18 +00:00
|
|
|
first_run = 0;
|
|
|
|
}
|
|
|
|
|
2009-12-06 16:27:51 +00:00
|
|
|
/* get the bucket index */
|
2009-12-06 20:34:36 +00:00
|
|
|
bucket = p->pid & (HTABSIZE - 1);
|
2009-09-20 14:09:18 +00:00
|
|
|
|
2009-12-06 16:27:51 +00:00
|
|
|
/* insert a new element on bucket's top */
|
2010-01-04 17:06:14 +00:00
|
|
|
phe = (struct proc_hash_entry *)malloc(sizeof(struct proc_hash_entry));
|
2009-12-06 16:27:51 +00:00
|
|
|
phe->proc = p;
|
|
|
|
phe->next = proc_hash_table[bucket].next;
|
|
|
|
proc_hash_table[bucket].next = phe;
|
2009-09-20 14:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void unhash_process(struct process *p)
|
|
|
|
{
|
|
|
|
struct proc_hash_entry *phe, *tmp;
|
|
|
|
|
|
|
|
/* get the bucket head */
|
2009-12-06 20:34:36 +00:00
|
|
|
phe = &proc_hash_table[p->pid & (HTABSIZE - 1)];
|
2009-09-20 14:09:18 +00:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
2009-12-06 20:34:36 +00:00
|
|
|
for (i = 0; i < HTABSIZE; i++) {
|
2009-09-20 14:09:18 +00:00
|
|
|
__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;
|
2010-02-24 10:52:59 +00:00
|
|
|
free_and_zero(pr->name);
|
2005-11-10 04:19:43 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-05-30 11:55:50 +00:00
|
|
|
struct process *find_process(pid_t pid)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
2009-09-20 14:09:18 +00:00
|
|
|
struct proc_hash_entry *phe;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2009-12-06 20:34:36 +00:00
|
|
|
phe = &proc_hash_table[pid & (HTABSIZE - 1)];
|
2009-09-20 14:09:18 +00:00
|
|
|
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 */
|
2010-05-30 11:55:50 +00:00
|
|
|
struct process *new_process(int p)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
|
|
|
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;
|
2010-01-07 18:14:53 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2009-06-12 17:08:44 +00:00
|
|
|
process->previous_read_bytes = ULLONG_MAX;
|
|
|
|
process->previous_write_bytes = ULLONG_MAX;
|
2010-01-07 18:14:53 +00:00
|
|
|
#endif /* BUILD_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
|
|
|
/******************************************
|
|
|
|
* 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;
|
|
|
|
|
2010-02-24 10:52:59 +00:00
|
|
|
free_and_zero(p->name);
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************
|
|
|
|
* Find the top processes *
|
|
|
|
******************************************/
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2009-12-06 16:17:27 +00:00
|
|
|
/* cpu comparison function for prio queue */
|
|
|
|
static int compare_cpu(void *va, void *vb)
|
2008-02-20 20:30:45 +00:00
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct process *a = (struct process *)va, *b = (struct process *)vb;
|
2005-11-12 02:57:48 +00:00
|
|
|
|
2010-05-30 15:49:26 +00:00
|
|
|
return b->amount - a->amount;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
|
2009-12-06 16:17:27 +00:00
|
|
|
/* mem comparison function for prio queue */
|
|
|
|
static int compare_mem(void *va, void *vb)
|
2008-02-20 20:30:45 +00:00
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct process *a = (struct process *)va, *b = (struct process *)vb;
|
2009-12-06 16:17:27 +00:00
|
|
|
|
2010-05-30 15:49:26 +00:00
|
|
|
return b->rss - a->rss;
|
2005-08-25 01:13:01 +00:00
|
|
|
}
|
|
|
|
|
2009-12-06 16:17:27 +00:00
|
|
|
/* CPU time comparision function for prio queue */
|
|
|
|
static int compare_time(void *va, void *vb)
|
2009-02-07 14:01:50 +00:00
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct process *a = (struct process *)va, *b = (struct process *)vb;
|
2009-12-06 16:17:27 +00:00
|
|
|
|
2009-02-07 14:01:50 +00:00
|
|
|
return b->total_cpu_time - a->total_cpu_time;
|
|
|
|
}
|
|
|
|
|
2010-01-07 18:14:53 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2009-12-06 16:17:27 +00:00
|
|
|
/* I/O comparision function for prio queue */
|
|
|
|
static int compare_io(void *va, void *vb)
|
2009-06-12 17:08:44 +00:00
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct process *a = (struct process *)va, *b = (struct process *)vb;
|
2009-12-06 16:17:27 +00:00
|
|
|
|
2010-05-30 15:49:26 +00:00
|
|
|
return b->io_perc - a->io_perc;
|
2009-06-12 17:08:44 +00:00
|
|
|
}
|
2010-01-07 18:14:53 +00:00
|
|
|
#endif /* BUILD_IOSTATS */
|
2009-06-12 17:08:44 +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
|
|
|
|
2010-05-30 11:55:50 +00:00
|
|
|
static void process_find_top(struct process **cpu, struct process **mem,
|
2009-06-12 17:08:44 +00:00
|
|
|
struct process **ptime
|
2010-01-07 18:14:53 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2009-06-12 17:08:44 +00:00
|
|
|
, struct process **io
|
2010-01-07 18:14:53 +00:00
|
|
|
#endif /* BUILD_IOSTATS */
|
2009-06-12 17:08:44 +00:00
|
|
|
)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
2010-05-30 11:55:50 +00:00
|
|
|
prio_queue_t cpu_queue, mem_queue, time_queue;
|
2010-01-07 18:14:53 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2010-05-30 11:55:50 +00:00
|
|
|
prio_queue_t io_queue;
|
2009-12-06 16:17:27 +00:00
|
|
|
#endif
|
2005-11-10 04:19:43 +00:00
|
|
|
struct process *cur_proc = NULL;
|
2009-12-06 16:17:27 +00:00
|
|
|
int i;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2009-06-12 17:08:44 +00:00
|
|
|
if (!top_cpu && !top_mem && !top_time
|
2010-01-07 18:14:53 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2009-06-12 17:08:44 +00:00
|
|
|
&& !top_io
|
2010-01-07 18:14:53 +00:00
|
|
|
#endif /* BUILD_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
|
|
|
|
2009-12-06 16:17:27 +00:00
|
|
|
cpu_queue = init_prio_queue();
|
|
|
|
pq_set_compare(cpu_queue, &compare_cpu);
|
|
|
|
pq_set_max_size(cpu_queue, MAX_SP);
|
|
|
|
|
|
|
|
mem_queue = init_prio_queue();
|
|
|
|
pq_set_compare(mem_queue, &compare_mem);
|
|
|
|
pq_set_max_size(mem_queue, MAX_SP);
|
|
|
|
|
|
|
|
time_queue = init_prio_queue();
|
|
|
|
pq_set_compare(time_queue, &compare_time);
|
|
|
|
pq_set_max_size(time_queue, MAX_SP);
|
|
|
|
|
2010-01-07 18:14:53 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2009-12-06 16:17:27 +00:00
|
|
|
io_queue = init_prio_queue();
|
|
|
|
pq_set_compare(io_queue, &compare_io);
|
|
|
|
pq_set_max_size(io_queue, MAX_SP);
|
|
|
|
#endif
|
|
|
|
|
2010-05-30 11:55:50 +00:00
|
|
|
/* OS-specific function updating process list */
|
|
|
|
get_top_info();
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
process_cleanup(); /* cleanup list from exited processes */
|
|
|
|
|
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) {
|
2009-12-06 16:17:27 +00:00
|
|
|
insert_prio_elem(cpu_queue, cur_proc);
|
2005-11-10 04:19:43 +00:00
|
|
|
}
|
2005-11-10 01:20:19 +00:00
|
|
|
if (top_mem) {
|
2009-12-06 16:17:27 +00:00
|
|
|
insert_prio_elem(mem_queue, cur_proc);
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
2009-02-07 14:01:50 +00:00
|
|
|
if (top_time) {
|
2009-12-06 16:17:27 +00:00
|
|
|
insert_prio_elem(time_queue, cur_proc);
|
2009-02-07 14:01:50 +00:00
|
|
|
}
|
2010-01-07 18:14:53 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2009-06-12 17:08:44 +00:00
|
|
|
if (top_io) {
|
2009-12-06 16:17:27 +00:00
|
|
|
insert_prio_elem(io_queue, cur_proc);
|
2009-06-12 17:08:44 +00:00
|
|
|
}
|
2010-01-07 18:14:53 +00:00
|
|
|
#endif /* BUILD_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-12-06 16:17:27 +00:00
|
|
|
for (i = 0; i < MAX_SP; i++) {
|
|
|
|
if (top_cpu)
|
2010-01-04 17:06:14 +00:00
|
|
|
cpu[i] = (process*)pop_prio_elem(cpu_queue);
|
2009-12-06 16:17:27 +00:00
|
|
|
if (top_mem)
|
2010-01-04 17:06:14 +00:00
|
|
|
mem[i] = (process*)pop_prio_elem(mem_queue);
|
2009-12-06 16:17:27 +00:00
|
|
|
if (top_time)
|
2010-01-04 17:06:14 +00:00
|
|
|
ptime[i] = (process*)pop_prio_elem(time_queue);
|
2010-01-07 18:14:53 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2009-12-06 16:17:27 +00:00
|
|
|
if (top_io)
|
2010-01-04 17:06:14 +00:00
|
|
|
io[i] = (process*)pop_prio_elem(io_queue);
|
2010-01-07 18:14:53 +00:00
|
|
|
#endif /* BUILD_IOSTATS */
|
2009-12-06 16:17:27 +00:00
|
|
|
}
|
|
|
|
free_prio_queue(cpu_queue);
|
|
|
|
free_prio_queue(mem_queue);
|
|
|
|
free_prio_queue(time_queue);
|
2010-01-07 18:14:53 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2009-12-06 16:17:27 +00:00
|
|
|
free_prio_queue(io_queue);
|
2010-01-07 18:14:53 +00:00
|
|
|
#endif /* BUILD_IOSTATS */
|
2005-11-10 04:19:43 +00:00
|
|
|
}
|
2009-08-05 22:46:51 +00:00
|
|
|
|
2010-05-30 11:55:50 +00:00
|
|
|
int update_top(void)
|
|
|
|
{
|
|
|
|
process_find_top(info.cpu, info.memu, info.time
|
|
|
|
#ifdef BUILD_IOSTATS
|
|
|
|
, info.io
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
info.first_process = get_first_process();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-12 14:47:00 +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);
|
|
|
|
}
|
|
|
|
|
2009-10-06 20:33:28 +00:00
|
|
|
struct top_data {
|
2009-12-12 14:47:00 +00:00
|
|
|
struct process **list;
|
2009-10-06 20:33:28 +00:00
|
|
|
int num;
|
|
|
|
int was_parsed;
|
|
|
|
char *s;
|
|
|
|
};
|
|
|
|
|
2009-12-12 14:47:00 +00:00
|
|
|
static unsigned int top_name_width = 15;
|
|
|
|
|
|
|
|
/* return zero on success, non-zero otherwise */
|
|
|
|
int set_top_name_width(const char *s)
|
|
|
|
{
|
|
|
|
if (!s)
|
|
|
|
return 0;
|
|
|
|
return !(sscanf(s, "%u", &top_name_width) == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_top_name(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct top_data *td = (struct top_data *)obj->data.opaque;
|
2009-12-12 14:47:00 +00:00
|
|
|
int width;
|
|
|
|
|
|
|
|
if (!td || !td->list || !td->list[td->num])
|
|
|
|
return;
|
|
|
|
|
|
|
|
width = MIN(p_max_size, (int)top_name_width + 1);
|
|
|
|
snprintf(p, width + 1, "%-*s", width, td->list[td->num]->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_top_mem(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct top_data *td = (struct top_data *)obj->data.opaque;
|
2009-12-12 14:47:00 +00:00
|
|
|
int width;
|
|
|
|
|
|
|
|
if (!td || !td->list || !td->list[td->num])
|
|
|
|
return;
|
|
|
|
|
|
|
|
width = MIN(p_max_size, 7);
|
|
|
|
snprintf(p, width, "%6.2f", (float) ((float)td->list[td->num]->rss / info.memmax) / 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_top_time(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct top_data *td = (struct top_data *)obj->data.opaque;
|
2009-12-12 14:47:00 +00:00
|
|
|
int width;
|
|
|
|
char *timeval;
|
|
|
|
|
|
|
|
if (!td || !td->list || !td->list[td->num])
|
|
|
|
return;
|
|
|
|
|
|
|
|
width = MIN(p_max_size, 10);
|
|
|
|
timeval = format_time(td->list[td->num]->total_cpu_time, 9);
|
|
|
|
snprintf(p, width, "%9s", timeval);
|
|
|
|
free(timeval);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PRINT_TOP_GENERATOR(name, width, fmt, field) \
|
|
|
|
static void print_top_##name(struct text_object *obj, char *p, int p_max_size) \
|
|
|
|
{ \
|
2010-01-04 17:06:14 +00:00
|
|
|
struct top_data *td = (struct top_data *)obj->data.opaque; \
|
2009-12-12 14:47:00 +00:00
|
|
|
if (!td || !td->list || !td->list[td->num]) \
|
|
|
|
return; \
|
|
|
|
snprintf(p, MIN(p_max_size, width), fmt, td->list[td->num]->field); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PRINT_TOP_HR_GENERATOR(name, field, denom) \
|
|
|
|
static void print_top_##name(struct text_object *obj, char *p, int p_max_size) \
|
|
|
|
{ \
|
2010-01-04 17:06:14 +00:00
|
|
|
struct top_data *td = (struct top_data *)obj->data.opaque; \
|
2009-12-12 14:47:00 +00:00
|
|
|
if (!td || !td->list || !td->list[td->num]) \
|
|
|
|
return; \
|
|
|
|
human_readable(td->list[td->num]->field / denom, p, p_max_size); \
|
|
|
|
}
|
|
|
|
|
|
|
|
PRINT_TOP_GENERATOR(cpu, 7, "%6.2f", amount)
|
|
|
|
PRINT_TOP_GENERATOR(pid, 6, "%5i", pid)
|
|
|
|
PRINT_TOP_HR_GENERATOR(mem_res, rss, 1)
|
|
|
|
PRINT_TOP_HR_GENERATOR(mem_vsize, vsize, 1)
|
2010-01-07 18:14:53 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2009-12-12 14:47:00 +00:00
|
|
|
PRINT_TOP_HR_GENERATOR(read_bytes, read_bytes, update_interval)
|
|
|
|
PRINT_TOP_HR_GENERATOR(write_bytes, write_bytes, update_interval)
|
|
|
|
PRINT_TOP_GENERATOR(io_perc, 7, "%6.2f", io_perc)
|
2010-01-07 18:14:53 +00:00
|
|
|
#endif /* BUILD_IOSTATS */
|
2009-12-12 14:47:00 +00:00
|
|
|
|
|
|
|
static void free_top(struct text_object *obj)
|
|
|
|
{
|
2010-01-04 17:06:14 +00:00
|
|
|
struct top_data *td = (struct top_data *)obj->data.opaque;
|
2009-12-12 14:47:00 +00:00
|
|
|
|
|
|
|
if (!td)
|
|
|
|
return;
|
2010-02-24 10:52:59 +00:00
|
|
|
free_and_zero(td->s);
|
|
|
|
free_and_zero(obj->data.opaque);
|
2009-12-12 14:47:00 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-01-04 17:06:14 +00:00
|
|
|
obj->data.opaque = td = (struct top_data *)malloc(sizeof(struct top_data));
|
2009-12-12 14:47:00 +00:00
|
|
|
memset(td, 0, sizeof(struct top_data));
|
|
|
|
|
2009-08-05 22:46:51 +00:00
|
|
|
if (s[3] == 0) {
|
2009-12-12 14:47:00 +00:00
|
|
|
td->list = info.cpu;
|
2009-08-05 22:46:51 +00:00
|
|
|
top_cpu = 1;
|
|
|
|
} else if (strcmp(&s[3], "_mem") == EQUAL) {
|
2009-12-12 14:47:00 +00:00
|
|
|
td->list = info.memu;
|
2009-08-05 22:46:51 +00:00
|
|
|
top_mem = 1;
|
|
|
|
} else if (strcmp(&s[3], "_time") == EQUAL) {
|
2009-12-12 14:47:00 +00:00
|
|
|
td->list = info.time;
|
2009-08-05 22:46:51 +00:00
|
|
|
top_time = 1;
|
2010-01-07 18:14:53 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2009-08-05 22:46:51 +00:00
|
|
|
} else if (strcmp(&s[3], "_io") == EQUAL) {
|
2009-12-12 14:47:00 +00:00
|
|
|
td->list = info.io;
|
2009-08-05 22:46:51 +00:00
|
|
|
top_io = 1;
|
2010-01-07 18:14:53 +00:00
|
|
|
#endif /* BUILD_IOSTATS */
|
2009-08-05 22:46:51 +00:00
|
|
|
} else {
|
2010-01-07 18:14:53 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2009-08-05 22:46:51 +00:00
|
|
|
NORM_ERR("Must be top, top_mem, top_time or top_io");
|
2010-01-07 18:14:53 +00:00
|
|
|
#else /* BUILD_IOSTATS */
|
2009-08-05 22:46:51 +00:00
|
|
|
NORM_ERR("Must be top, top_mem or top_time");
|
2010-01-07 18:14:53 +00:00
|
|
|
#endif /* BUILD_IOSTATS */
|
2010-02-24 10:52:59 +00:00
|
|
|
free_and_zero(obj->data.opaque);
|
2009-08-05 22:46:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-06 20:33:28 +00:00
|
|
|
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-12-12 14:47:00 +00:00
|
|
|
obj->callbacks.print = &print_top_name;
|
2009-08-05 22:46:51 +00:00
|
|
|
} else if (strcmp(buf, "cpu") == EQUAL) {
|
2009-12-12 14:47:00 +00:00
|
|
|
obj->callbacks.print = &print_top_cpu;
|
2009-08-05 22:46:51 +00:00
|
|
|
} else if (strcmp(buf, "pid") == EQUAL) {
|
2009-12-12 14:47:00 +00:00
|
|
|
obj->callbacks.print = &print_top_pid;
|
2009-08-05 22:46:51 +00:00
|
|
|
} else if (strcmp(buf, "mem") == EQUAL) {
|
2009-12-12 14:47:00 +00:00
|
|
|
obj->callbacks.print = &print_top_mem;
|
2009-12-12 18:50:36 +00:00
|
|
|
add_update_callback(&update_meminfo);
|
2009-08-05 22:46:51 +00:00
|
|
|
} else if (strcmp(buf, "time") == EQUAL) {
|
2009-12-12 14:47:00 +00:00
|
|
|
obj->callbacks.print = &print_top_time;
|
2009-08-05 22:46:51 +00:00
|
|
|
} else if (strcmp(buf, "mem_res") == EQUAL) {
|
2009-12-12 14:47:00 +00:00
|
|
|
obj->callbacks.print = &print_top_mem_res;
|
2009-08-05 22:46:51 +00:00
|
|
|
} else if (strcmp(buf, "mem_vsize") == EQUAL) {
|
2009-12-12 14:47:00 +00:00
|
|
|
obj->callbacks.print = &print_top_mem_vsize;
|
2010-01-07 18:14:53 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2009-08-05 22:46:51 +00:00
|
|
|
} else if (strcmp(buf, "io_read") == EQUAL) {
|
2009-12-12 14:47:00 +00:00
|
|
|
obj->callbacks.print = &print_top_read_bytes;
|
2009-08-05 22:46:51 +00:00
|
|
|
} else if (strcmp(buf, "io_write") == EQUAL) {
|
2009-12-12 14:47:00 +00:00
|
|
|
obj->callbacks.print = &print_top_write_bytes;
|
2009-08-05 22:46:51 +00:00
|
|
|
} else if (strcmp(buf, "io_perc") == EQUAL) {
|
2009-12-12 14:47:00 +00:00
|
|
|
obj->callbacks.print = &print_top_io_perc;
|
2010-01-07 18:14:53 +00:00
|
|
|
#endif /* BUILD_IOSTATS */
|
2009-08-05 22:46:51 +00:00
|
|
|
} else {
|
|
|
|
NORM_ERR("invalid type arg for top");
|
2010-01-07 18:14:53 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2009-08-05 22:46:51 +00:00
|
|
|
NORM_ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize, "
|
|
|
|
"io_read, io_write, io_perc");
|
2010-01-07 18:14:53 +00:00
|
|
|
#else /* BUILD_IOSTATS */
|
2009-08-05 22:46:51 +00:00
|
|
|
NORM_ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize");
|
2010-01-07 18:14:53 +00:00
|
|
|
#endif /* BUILD_IOSTATS */
|
2010-05-30 15:13:40 +00:00
|
|
|
free_and_zero(td->s);
|
|
|
|
free_and_zero(obj->data.opaque);
|
2009-08-05 22:46:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (n < 1 || n > 10) {
|
|
|
|
NORM_ERR("invalid num arg for top. Must be between 1 and 10.");
|
2010-05-30 15:13:40 +00:00
|
|
|
free_and_zero(td->s);
|
|
|
|
free_and_zero(obj->data.opaque);
|
2009-08-05 22:46:51 +00:00
|
|
|
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");
|
2010-05-30 15:13:40 +00:00
|
|
|
free_and_zero(td->s);
|
|
|
|
free_and_zero(obj->data.opaque);
|
2009-08-05 22:46:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2009-12-12 14:47:00 +00:00
|
|
|
obj->callbacks.free = &free_top;
|
2009-08-05 22:46:51 +00:00
|
|
|
return 1;
|
|
|
|
}
|