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>
|
2012-05-03 23:34:44 +00:00
|
|
|
* Copyright (c) 2005-2012 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"
|
2015-07-20 23:10:42 +00:00
|
|
|
#include <string>
|
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);
|
2015-08-26 08:57:00 +00:00
|
|
|
free_and_zero(pr->basename);
|
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) {
|
2015-08-26 17:40:04 +00:00
|
|
|
/* Try matching against the full command line first. If that fails,
|
|
|
|
* fall back to the basename.
|
|
|
|
*/
|
|
|
|
if ((p->name && !strcmp(p->name, name)) || (p->basename && !strcmp(p->basename, name)))
|
2009-06-19 23:40:40 +00:00
|
|
|
return p;
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-28 17:04:31 +00:00
|
|
|
static 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
|
|
|
}
|
2012-06-28 17:04:31 +00:00
|
|
|
return NULL;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2012-06-28 17:04:31 +00:00
|
|
|
static struct process *new_process(pid_t pid)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
2012-06-28 17:04:31 +00:00
|
|
|
struct process *p = (struct process *) malloc(sizeof(struct process));
|
2005-11-09 02:15:13 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/* Do stitching necessary for doubly linked list */
|
2012-06-28 17:04:31 +00:00
|
|
|
p->previous = NULL;
|
|
|
|
p->next = first_process;
|
|
|
|
if (p->next) {
|
|
|
|
p->next->previous = p;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2012-06-28 17:04:31 +00:00
|
|
|
first_process = p;
|
|
|
|
|
|
|
|
p->pid = pid;
|
|
|
|
p->name = 0;
|
2015-08-26 08:57:00 +00:00
|
|
|
p->basename = 0;
|
2012-06-28 17:04:31 +00:00
|
|
|
p->amount = 0;
|
|
|
|
p->user_time = 0;
|
|
|
|
p->total = 0;
|
|
|
|
p->kernel_time = 0;
|
|
|
|
p->previous_user_time = ULONG_MAX;
|
|
|
|
p->previous_kernel_time = ULONG_MAX;
|
|
|
|
p->total_cpu_time = 0;
|
|
|
|
p->vsize = 0;
|
|
|
|
p->rss = 0;
|
2010-01-07 18:14:53 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2012-06-28 17:04:31 +00:00
|
|
|
p->read_bytes = 0;
|
|
|
|
p->previous_read_bytes = ULLONG_MAX;
|
|
|
|
p->write_bytes = 0;
|
|
|
|
p->previous_write_bytes = ULLONG_MAX;
|
|
|
|
p->io_perc = 0;
|
2010-01-07 18:14:53 +00:00
|
|
|
#endif /* BUILD_IOSTATS */
|
2012-06-28 17:04:31 +00:00
|
|
|
p->time_stamp = 0;
|
|
|
|
p->counted = 1;
|
|
|
|
p->changed = 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2012-06-28 17:04:31 +00:00
|
|
|
/* process_find_name(p); */
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2009-09-20 14:09:18 +00:00
|
|
|
/* add the process to the hash table */
|
2012-06-28 17:04:31 +00:00
|
|
|
hash_process(p);
|
2009-09-20 14:09:18 +00:00
|
|
|
|
2012-06-28 17:04:31 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get / create a new process object and insert it into the process list */
|
|
|
|
struct process *get_process(pid_t pid)
|
|
|
|
{
|
|
|
|
struct process *p = find_process(pid);
|
|
|
|
return p ? p : new_process(pid);
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2015-08-26 08:57:00 +00:00
|
|
|
free_and_zero(p->basename);
|
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
|
|
|
|
2012-04-22 18:58:56 +00:00
|
|
|
if (b->amount > a->amount) {
|
|
|
|
return 1;
|
|
|
|
} else if (a->amount > b->amount) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
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
|
|
|
|
2012-04-22 18:58:56 +00:00
|
|
|
if (b->rss > a->rss) {
|
|
|
|
return 1;
|
|
|
|
} else if (a->rss > b->rss) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
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
|
|
|
|
2012-04-22 18:58:56 +00:00
|
|
|
if (b->total_cpu_time > a->total_cpu_time) {
|
|
|
|
return 1;
|
|
|
|
} else if (b->total_cpu_time < a->total_cpu_time) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2009-02-07 14:01:50 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2012-04-22 18:58:56 +00:00
|
|
|
if (b->io_perc > a->io_perc) {
|
|
|
|
return 1;
|
|
|
|
} else if (a->io_perc > b->io_perc) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
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-06-03 20:07:57 +00:00
|
|
|
/* g_time is the time_stamp entry for process. It is updated when the
|
|
|
|
* process information is updated to indicate that the process is still
|
|
|
|
* alive (and must not be removed from the process list in
|
|
|
|
* process_cleanup()) */
|
|
|
|
++g_time;
|
|
|
|
|
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)
|
|
|
|
{
|
2010-12-31 12:39:54 +00:00
|
|
|
// XXX: this was a separate callback. and it should be again, as soon as it's possible
|
|
|
|
update_meminfo();
|
|
|
|
|
2010-05-30 11:55:50 +00:00
|
|
|
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)) {
|
2010-08-29 21:50:32 +00:00
|
|
|
return strndup(buf, text_buffer_size.get(*state));
|
2009-12-12 14:47:00 +00:00
|
|
|
}
|
|
|
|
if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
|
2010-08-29 21:50:32 +00:00
|
|
|
return strndup(buf, text_buffer_size.get(*state));
|
2009-12-12 14:47:00 +00:00
|
|
|
}
|
|
|
|
nn = nt % 60; // minutes past the hour
|
|
|
|
nt /= 60; // total hours
|
|
|
|
if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
|
2010-08-29 21:50:32 +00:00
|
|
|
return strndup(buf, text_buffer_size.get(*state));
|
2009-12-12 14:47:00 +00:00
|
|
|
}
|
|
|
|
nn = nt; // now also hours
|
|
|
|
if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
|
2010-08-29 21:50:32 +00:00
|
|
|
return strndup(buf, text_buffer_size.get(*state));
|
2009-12-12 14:47:00 +00:00
|
|
|
}
|
|
|
|
nn /= 24; // now days
|
|
|
|
if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
|
2010-08-29 21:50:32 +00:00
|
|
|
return strndup(buf, text_buffer_size.get(*state));
|
2009-12-12 14:47:00 +00:00
|
|
|
}
|
|
|
|
nn /= 7; // now weeks
|
|
|
|
if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
|
2010-08-29 21:50:32 +00:00
|
|
|
return strndup(buf, text_buffer_size.get(*state));
|
2009-12-12 14:47:00 +00:00
|
|
|
}
|
|
|
|
// well shoot, this outta' fit...
|
2010-08-29 21:50:32 +00:00
|
|
|
return strndup("<inf>", text_buffer_size.get(*state));
|
2009-12-12 14:47:00 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2010-08-25 17:09:15 +00:00
|
|
|
static conky::range_config_setting<unsigned int> top_name_width("top_name_width", 0,
|
|
|
|
std::numeric_limits<unsigned int>::max(), 15, true);
|
2015-07-20 23:10:42 +00:00
|
|
|
static conky::simple_config_setting<bool> top_name_verbose("top_name_verbose", false, false);
|
2009-12-12 14:47:00 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2015-07-20 23:10:42 +00:00
|
|
|
std::string top_name = td->list[td->num]->name;
|
|
|
|
if (!top_name_verbose.get(*state)) {
|
|
|
|
top_name = top_name.substr(0, top_name.find_first_of(' '));
|
|
|
|
}
|
|
|
|
|
2010-08-25 17:09:15 +00:00
|
|
|
width = MIN(p_max_size, (int)top_name_width.get(*state) + 1);
|
2015-07-20 23:10:42 +00:00
|
|
|
snprintf(p, width + 1, "%-*s", width, top_name.c_str());
|
2009-12-12 14:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-10-10 19:54:18 +00:00
|
|
|
static void print_top_user(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
struct top_data *td = (struct top_data *)obj->data.opaque;
|
|
|
|
|
|
|
|
if (!td || !td->list || !td->list[td->num])
|
|
|
|
return;
|
|
|
|
|
|
|
|
snprintf(p, p_max_size, "%.8s", getpwuid(td->list[td->num]->uid)->pw_name);
|
|
|
|
}
|
|
|
|
|
2009-12-12 14:47:00 +00:00
|
|
|
#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)
|
2011-10-10 19:54:18 +00:00
|
|
|
PRINT_TOP_GENERATOR(uid, 6, "%5i", uid)
|
2009-12-12 14:47:00 +00:00
|
|
|
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
|
2010-09-11 12:25:19 +00:00
|
|
|
PRINT_TOP_HR_GENERATOR(read_bytes, read_bytes, active_update_interval())
|
|
|
|
PRINT_TOP_HR_GENERATOR(write_bytes, write_bytes, active_update_interval())
|
2009-12-12 14:47:00 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-08-29 21:50:32 +00:00
|
|
|
td->s = strndup(arg, text_buffer_size.get(*state));
|
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-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;
|
2011-10-10 19:54:18 +00:00
|
|
|
} else if (strcmp(buf, "uid") == EQUAL) {
|
|
|
|
obj->callbacks.print = &print_top_uid;
|
|
|
|
} else if (strcmp(buf, "user") == EQUAL) {
|
|
|
|
obj->callbacks.print = &print_top_user;
|
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;
|
|
|
|
}
|