2018-05-12 16:03:00 +00:00
|
|
|
/*
|
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>
|
2019-01-05 15:52:43 +00:00
|
|
|
* Copyright (c) 2005-2019 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"
|
2018-05-12 16:03:00 +00:00
|
|
|
#include "prioqueue.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
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
struct process *first_process = nullptr;
|
2010-05-30 11:55:50 +00:00
|
|
|
|
|
|
|
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 {
|
2018-05-12 16:03:00 +00:00
|
|
|
struct proc_hash_entry *next;
|
|
|
|
struct process *proc;
|
2009-09-20 14:09:18 +00:00
|
|
|
};
|
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
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
static void hash_process(struct process *p) {
|
|
|
|
struct proc_hash_entry *phe;
|
|
|
|
static char first_run = 1;
|
|
|
|
int bucket;
|
|
|
|
|
|
|
|
/* better make sure all next pointers are zero upon first access */
|
2018-05-12 23:26:31 +00:00
|
|
|
if (first_run != 0) {
|
2018-05-12 16:03:00 +00:00
|
|
|
memset(proc_hash_table, 0, sizeof(struct proc_hash_entry) * HTABSIZE);
|
|
|
|
first_run = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the bucket index */
|
|
|
|
bucket = p->pid & (HTABSIZE - 1);
|
|
|
|
|
|
|
|
/* insert a new element on bucket's top */
|
2018-05-12 23:26:31 +00:00
|
|
|
phe = static_cast<struct proc_hash_entry *>(
|
|
|
|
malloc(sizeof(struct proc_hash_entry)));
|
2018-05-12 16:03:00 +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
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
static void unhash_process(struct process *p) {
|
|
|
|
struct proc_hash_entry *phe, *tmp;
|
|
|
|
|
|
|
|
/* get the bucket head */
|
|
|
|
phe = &proc_hash_table[p->pid & (HTABSIZE - 1)];
|
|
|
|
/* find the entry pointing to p and drop it */
|
2018-05-12 23:26:31 +00:00
|
|
|
while (phe->next != nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
if (phe->next->proc == p) {
|
|
|
|
tmp = phe->next;
|
|
|
|
phe->next = phe->next->next;
|
|
|
|
free(tmp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
phe = phe->next;
|
|
|
|
}
|
2009-09-20 14:09:18 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
static void __unhash_all_processes(struct proc_hash_entry *phe) {
|
2018-12-21 02:20:43 +00:00
|
|
|
if (phe->next != nullptr) { __unhash_all_processes(phe->next); }
|
2018-05-12 16:03:00 +00:00
|
|
|
free(phe->next);
|
2009-09-20 14:09:18 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
static void unhash_all_processes() {
|
2018-05-12 16:03:00 +00:00
|
|
|
int i;
|
2009-09-20 14:09:18 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
for (i = 0; i < HTABSIZE; i++) {
|
|
|
|
__unhash_all_processes(&proc_hash_table[i]);
|
2018-05-12 23:26:31 +00:00
|
|
|
proc_hash_table[i].next = nullptr;
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
2009-09-20 14:09:18 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
struct process *get_first_process() {
|
2018-05-12 16:03:00 +00:00
|
|
|
return first_process;
|
2005-11-10 04:19:43 +00:00
|
|
|
}
|
2005-11-10 01:20:19 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
void free_all_processes() {
|
|
|
|
struct process *next = nullptr, *pr = first_process;
|
2018-05-12 16:03:00 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
while (pr != nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
next = pr->next;
|
|
|
|
free_and_zero(pr->name);
|
|
|
|
free_and_zero(pr->basename);
|
|
|
|
free(pr);
|
|
|
|
pr = next;
|
|
|
|
}
|
2018-05-12 23:26:31 +00:00
|
|
|
first_process = nullptr;
|
2018-05-12 16:03:00 +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
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
struct process *get_process_by_name(const char *name) {
|
|
|
|
struct process *p = first_process;
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
while (p != nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
/* Try matching against the full command line first. If that fails,
|
|
|
|
* fall back to the basename.
|
|
|
|
*/
|
2018-05-12 23:26:31 +00:00
|
|
|
if (((p->name != nullptr) && (strcmp(p->name, name) == 0)) ||
|
|
|
|
((p->basename != nullptr) && (strcmp(p->basename, name) == 0))) {
|
2018-05-12 16:03:00 +00:00
|
|
|
return p;
|
2018-05-12 23:26:31 +00:00
|
|
|
}
|
2018-05-12 16:03:00 +00:00
|
|
|
p = p->next;
|
|
|
|
}
|
2018-05-12 23:26:31 +00:00
|
|
|
return nullptr;
|
2009-06-19 23:40:40 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
static struct process *find_process(pid_t pid) {
|
|
|
|
struct proc_hash_entry *phe;
|
|
|
|
|
|
|
|
phe = &proc_hash_table[pid & (HTABSIZE - 1)];
|
2018-05-12 23:26:31 +00:00
|
|
|
while (phe->next != nullptr) {
|
2018-12-21 02:20:43 +00:00
|
|
|
if (phe->next->proc->pid == pid) { return phe->next->proc; }
|
2018-05-12 16:03:00 +00:00
|
|
|
phe = phe->next;
|
|
|
|
}
|
2018-05-12 23:26:31 +00:00
|
|
|
return nullptr;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
static struct process *new_process(pid_t pid) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *p = static_cast<struct process *>(malloc(sizeof(struct process)));
|
2018-05-12 16:03:00 +00:00
|
|
|
|
|
|
|
/* Do stitching necessary for doubly linked list */
|
2018-05-12 23:26:31 +00:00
|
|
|
p->previous = nullptr;
|
2018-05-12 16:03:00 +00:00
|
|
|
p->next = first_process;
|
2018-12-21 02:20:43 +00:00
|
|
|
if (p->next != nullptr) { p->next->previous = p; }
|
2018-05-12 16:03:00 +00:00
|
|
|
first_process = p;
|
|
|
|
|
|
|
|
p->pid = pid;
|
2018-05-12 23:26:31 +00:00
|
|
|
p->name = nullptr;
|
|
|
|
p->basename = nullptr;
|
2018-05-12 16:03:00 +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
|
2018-05-12 16:03:00 +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 */
|
2018-05-12 16:03:00 +00:00
|
|
|
p->time_stamp = 0;
|
|
|
|
p->counted = 1;
|
|
|
|
p->changed = 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
/* process_find_name(p); */
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
/* add the process to the hash table */
|
|
|
|
hash_process(p);
|
2009-09-20 14:09:18 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
return p;
|
2012-06-28 17:04:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get / create a new process object and insert it into the process list */
|
2018-05-12 16:03:00 +00:00
|
|
|
struct process *get_process(pid_t pid) {
|
|
|
|
struct process *p = find_process(pid);
|
2018-05-12 23:26:31 +00:00
|
|
|
return p != nullptr ? 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
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
static void delete_process(struct process *p) {
|
2005-07-20 00:30:40 +00:00
|
|
|
#if defined(PARANOID)
|
2018-05-12 16:03:00 +00:00
|
|
|
assert(p->id == 0x0badfeed);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
/*
|
|
|
|
* Ensure that deleted processes aren't reused.
|
|
|
|
*/
|
|
|
|
p->id = 0x007babe;
|
2008-02-20 20:30:45 +00:00
|
|
|
#endif /* defined(PARANOID) */
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
/*
|
|
|
|
* Maintain doubly linked list.
|
|
|
|
*/
|
2018-12-21 02:20:43 +00:00
|
|
|
if (p->next != nullptr) { p->next->previous = p->previous; }
|
2018-05-12 23:26:31 +00:00
|
|
|
if (p->previous != nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
p->previous->next = p->next;
|
2018-05-12 23:26:31 +00:00
|
|
|
} else {
|
2018-05-12 16:03:00 +00:00
|
|
|
first_process = p->next;
|
2018-05-12 23:26:31 +00:00
|
|
|
}
|
2018-05-12 16:03:00 +00:00
|
|
|
|
|
|
|
free_and_zero(p->name);
|
|
|
|
free_and_zero(p->basename);
|
|
|
|
/* remove the process from the hash table */
|
|
|
|
unhash_process(p);
|
|
|
|
free(p);
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/******************************************
|
|
|
|
* Strip dead process entries *
|
|
|
|
******************************************/
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
static void process_cleanup() {
|
2018-05-12 16:03:00 +00:00
|
|
|
struct process *p = first_process;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
while (p != nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
struct process *current = p;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
#if defined(PARANOID)
|
2018-05-12 16:03:00 +00:00
|
|
|
assert(p->id == 0x0badfeed);
|
2008-02-20 20:30:45 +00:00
|
|
|
#endif /* defined(PARANOID) */
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
p = p->next;
|
|
|
|
/* Delete processes that have died */
|
2018-12-22 17:45:25 +00:00
|
|
|
if (current->time_stamp != g_time) {
|
|
|
|
delete_process(current);
|
|
|
|
if (current == first_process) { first_process = nullptr; }
|
|
|
|
current = nullptr;
|
|
|
|
}
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************
|
|
|
|
* 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 */
|
2018-05-12 16:03:00 +00:00
|
|
|
static int compare_cpu(void *va, void *vb) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *a = static_cast<struct process *>(va),
|
|
|
|
*b = static_cast<struct process *>(vb);
|
2018-05-12 16:03:00 +00:00
|
|
|
|
2018-12-21 02:20:43 +00:00
|
|
|
if (b->amount > a->amount) { return 1; }
|
|
|
|
if (a->amount > b->amount) { return -1; }
|
|
|
|
return 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
|
2009-12-06 16:17:27 +00:00
|
|
|
/* mem comparison function for prio queue */
|
2018-05-12 16:03:00 +00:00
|
|
|
static int compare_mem(void *va, void *vb) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *a = static_cast<struct process *>(va),
|
|
|
|
*b = static_cast<struct process *>(vb);
|
2018-05-12 16:03:00 +00:00
|
|
|
|
2018-12-21 02:20:43 +00:00
|
|
|
if (b->rss > a->rss) { return 1; }
|
|
|
|
if (a->rss > b->rss) { return -1; }
|
|
|
|
return 0;
|
2005-08-25 01:13:01 +00:00
|
|
|
}
|
|
|
|
|
2020-12-31 08:09:23 +00:00
|
|
|
/* CPU time comparison function for prio queue */
|
2018-05-12 16:03:00 +00:00
|
|
|
static int compare_time(void *va, void *vb) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *a = static_cast<struct process *>(va),
|
|
|
|
*b = static_cast<struct process *>(vb);
|
2018-05-12 16:03:00 +00:00
|
|
|
|
2018-12-21 02:20:43 +00:00
|
|
|
if (b->total_cpu_time > a->total_cpu_time) { return 1; }
|
|
|
|
if (b->total_cpu_time < a->total_cpu_time) { return -1; }
|
|
|
|
return 0;
|
2009-02-07 14:01:50 +00:00
|
|
|
}
|
|
|
|
|
2010-01-07 18:14:53 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2020-12-31 08:09:23 +00:00
|
|
|
/* I/O comparison function for prio queue */
|
2018-05-12 16:03:00 +00:00
|
|
|
static int compare_io(void *va, void *vb) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *a = static_cast<struct process *>(va),
|
|
|
|
*b = static_cast<struct process *>(vb);
|
2018-05-12 16:03:00 +00:00
|
|
|
|
2018-12-21 02:20:43 +00:00
|
|
|
if (b->io_perc > a->io_perc) { return 1; }
|
|
|
|
if (a->io_perc > b->io_perc) { return -1; }
|
|
|
|
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
|
|
|
/* ****************************************************************** *
|
2018-12-21 02:20:43 +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]. *
|
2008-02-20 20:30:45 +00:00
|
|
|
* ****************************************************************** */
|
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,
|
2018-05-12 16:03:00 +00:00
|
|
|
struct process **ptime
|
2010-01-07 18:14:53 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2018-05-12 16:03:00 +00:00
|
|
|
,
|
|
|
|
struct process **io
|
2010-01-07 18:14:53 +00:00
|
|
|
#endif /* BUILD_IOSTATS */
|
2018-05-12 16:03:00 +00:00
|
|
|
) {
|
|
|
|
prio_queue_t cpu_queue, mem_queue, time_queue;
|
2010-01-07 18:14:53 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2018-05-12 16:03:00 +00:00
|
|
|
prio_queue_t io_queue;
|
2009-12-06 16:17:27 +00:00
|
|
|
#endif
|
2018-05-12 23:26:31 +00:00
|
|
|
struct process *cur_proc = nullptr;
|
2018-05-12 16:03:00 +00:00
|
|
|
int i;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
if ((top_cpu == 0) && (top_mem == 0) && (top_time == 0)
|
2010-01-07 18:14:53 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2018-05-12 23:26:31 +00:00
|
|
|
&& (top_io == 0)
|
2010-01-07 18:14:53 +00:00
|
|
|
#endif /* BUILD_IOSTATS */
|
2018-05-12 23:26:31 +00:00
|
|
|
&& (top_running == 0)) {
|
2018-05-12 16:03:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
cpu_queue = init_prio_queue();
|
|
|
|
pq_set_compare(cpu_queue, &compare_cpu);
|
|
|
|
pq_set_max_size(cpu_queue, MAX_SP);
|
2009-12-06 16:17:27 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
mem_queue = init_prio_queue();
|
|
|
|
pq_set_compare(mem_queue, &compare_mem);
|
|
|
|
pq_set_max_size(mem_queue, MAX_SP);
|
2009-12-06 16:17:27 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
time_queue = init_prio_queue();
|
|
|
|
pq_set_compare(time_queue, &compare_time);
|
|
|
|
pq_set_max_size(time_queue, MAX_SP);
|
2009-12-06 16:17:27 +00:00
|
|
|
|
2010-01-07 18:14:53 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2018-05-12 16:03:00 +00:00
|
|
|
io_queue = init_prio_queue();
|
|
|
|
pq_set_compare(io_queue, &compare_io);
|
|
|
|
pq_set_max_size(io_queue, MAX_SP);
|
2009-12-06 16:17:27 +00:00
|
|
|
#endif
|
|
|
|
|
2018-05-12 16:03:00 +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;
|
|
|
|
|
|
|
|
/* OS-specific function updating process list */
|
|
|
|
get_top_info();
|
|
|
|
|
|
|
|
process_cleanup(); /* cleanup list from exited processes */
|
|
|
|
|
|
|
|
cur_proc = first_process;
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
while (cur_proc != nullptr) {
|
2018-12-21 02:20:43 +00:00
|
|
|
if (top_cpu != 0) { insert_prio_elem(cpu_queue, cur_proc); }
|
|
|
|
if (top_mem != 0) { insert_prio_elem(mem_queue, cur_proc); }
|
|
|
|
if (top_time != 0) { insert_prio_elem(time_queue, cur_proc); }
|
2010-01-07 18:14:53 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2018-12-21 02:20:43 +00:00
|
|
|
if (top_io != 0) { insert_prio_elem(io_queue, cur_proc); }
|
2010-01-07 18:14:53 +00:00
|
|
|
#endif /* BUILD_IOSTATS */
|
2018-05-12 16:03:00 +00:00
|
|
|
cur_proc = cur_proc->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_SP; i++) {
|
2018-05-12 23:26:31 +00:00
|
|
|
if (top_cpu != 0) {
|
|
|
|
cpu[i] = static_cast<process *>(pop_prio_elem(cpu_queue));
|
|
|
|
}
|
|
|
|
if (top_mem != 0) {
|
|
|
|
mem[i] = static_cast<process *>(pop_prio_elem(mem_queue));
|
|
|
|
}
|
|
|
|
if (top_time != 0) {
|
|
|
|
ptime[i] = static_cast<process *>(pop_prio_elem(time_queue));
|
|
|
|
}
|
2010-01-07 18:14:53 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2018-05-12 23:26:31 +00:00
|
|
|
if (top_io != 0) {
|
|
|
|
io[i] = static_cast<process *>(pop_prio_elem(io_queue));
|
|
|
|
}
|
2010-01-07 18:14:53 +00:00
|
|
|
#endif /* BUILD_IOSTATS */
|
2018-05-12 16:03:00 +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
|
2018-05-12 16:03:00 +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
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
int update_top() {
|
2018-05-12 16:03:00 +00:00
|
|
|
process_find_top(info.cpu, info.memu, info.time
|
2010-05-30 11:55:50 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2018-05-12 16:03:00 +00:00
|
|
|
,
|
|
|
|
info.io
|
2010-05-30 11:55:50 +00:00
|
|
|
#endif
|
2018-05-12 16:03:00 +00:00
|
|
|
);
|
|
|
|
info.first_process = get_first_process();
|
|
|
|
return 0;
|
2010-05-30 11:55:50 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03: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.get(*state));
|
|
|
|
}
|
|
|
|
if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
|
|
|
|
return strndup(buf, text_buffer_size.get(*state));
|
|
|
|
}
|
|
|
|
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.get(*state));
|
|
|
|
}
|
|
|
|
nn = nt; // now also hours
|
|
|
|
if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
|
|
|
|
return strndup(buf, text_buffer_size.get(*state));
|
|
|
|
}
|
|
|
|
nn /= 24; // now days
|
|
|
|
if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
|
|
|
|
return strndup(buf, text_buffer_size.get(*state));
|
|
|
|
}
|
|
|
|
nn /= 7; // now weeks
|
|
|
|
if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
|
|
|
|
return strndup(buf, text_buffer_size.get(*state));
|
|
|
|
}
|
|
|
|
// well shoot, this outta' fit...
|
|
|
|
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 {
|
2018-05-12 16:03:00 +00:00
|
|
|
struct process **list;
|
|
|
|
int num;
|
|
|
|
int was_parsed;
|
|
|
|
char *s;
|
2009-10-06 20:33:28 +00:00
|
|
|
};
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
static conky::range_config_setting<unsigned int> top_name_width(
|
|
|
|
"top_name_width", 0, std::numeric_limits<unsigned int>::max(), 15, true);
|
|
|
|
static conky::simple_config_setting<bool> top_name_verbose("top_name_verbose",
|
|
|
|
false, true);
|
|
|
|
|
2018-12-21 02:20:43 +00:00
|
|
|
static void print_top_name(struct text_object *obj, char *p,
|
|
|
|
unsigned int p_max_size) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *td = static_cast<struct top_data *>(obj->data.opaque);
|
2018-05-12 16:03:00 +00:00
|
|
|
int width;
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
if ((td == nullptr) || (td->list == nullptr) ||
|
|
|
|
(td->list[td->num] == nullptr)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-12 16:03:00 +00:00
|
|
|
|
2019-02-23 19:43:44 +00:00
|
|
|
width = std::min(p_max_size,
|
|
|
|
static_cast<unsigned int>(top_name_width.get(*state)) + 1);
|
2018-05-12 16:03:00 +00:00
|
|
|
if (top_name_verbose.get(*state)) {
|
|
|
|
/* print the full command line */
|
|
|
|
snprintf(p, width + 1, "%-*s", width, td->list[td->num]->name);
|
|
|
|
} else {
|
|
|
|
/* print only the basename (i.e. executable name) */
|
|
|
|
snprintf(p, width + 1, "%-*s", width, td->list[td->num]->basename);
|
|
|
|
}
|
2009-12-12 14:47:00 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 02:20:43 +00:00
|
|
|
static void print_top_mem(struct text_object *obj, char *p,
|
|
|
|
unsigned int p_max_size) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *td = static_cast<struct top_data *>(obj->data.opaque);
|
2018-05-12 16:03:00 +00:00
|
|
|
int width;
|
2009-12-12 14:47:00 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
if ((td == nullptr) || (td->list == nullptr) ||
|
|
|
|
(td->list[td->num] == nullptr)) {
|
|
|
|
return;
|
|
|
|
}
|
2009-12-12 14:47:00 +00:00
|
|
|
|
2019-02-23 19:43:44 +00:00
|
|
|
width = std::min(p_max_size, static_cast<unsigned int>(7));
|
2018-05-12 16:03:00 +00:00
|
|
|
snprintf(p, width, "%6.2f",
|
2018-05-12 23:26:31 +00:00
|
|
|
(static_cast<float>(td->list[td->num]->rss) / info.memmax) / 10);
|
2009-12-12 14:47:00 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 02:20:43 +00:00
|
|
|
static void print_top_time(struct text_object *obj, char *p,
|
|
|
|
unsigned int p_max_size) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *td = static_cast<struct top_data *>(obj->data.opaque);
|
2018-05-12 16:03:00 +00:00
|
|
|
int width;
|
|
|
|
char *timeval;
|
2009-12-12 14:47:00 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
if ((td == nullptr) || (td->list == nullptr) ||
|
|
|
|
(td->list[td->num] == nullptr)) {
|
|
|
|
return;
|
|
|
|
}
|
2009-12-12 14:47:00 +00:00
|
|
|
|
2019-02-23 19:43:44 +00:00
|
|
|
width = std::min(p_max_size, static_cast<unsigned int>(10));
|
2018-05-12 16:03:00 +00:00
|
|
|
timeval = format_time(td->list[td->num]->total_cpu_time, 9);
|
|
|
|
snprintf(p, width, "%9s", timeval);
|
|
|
|
free(timeval);
|
2009-12-12 14:47:00 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 02:20:43 +00:00
|
|
|
static void print_top_user(struct text_object *obj, char *p,
|
|
|
|
unsigned int p_max_size) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *td = static_cast<struct top_data *>(obj->data.opaque);
|
2018-08-06 08:42:08 +00:00
|
|
|
struct passwd *pw;
|
2011-10-10 19:54:18 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
if ((td == nullptr) || (td->list == nullptr) ||
|
|
|
|
(td->list[td->num] == nullptr)) {
|
|
|
|
return;
|
|
|
|
}
|
2011-10-10 19:54:18 +00:00
|
|
|
|
2018-08-06 08:42:08 +00:00
|
|
|
pw = getpwuid(td->list[td->num]->uid);
|
|
|
|
if (pw != nullptr) {
|
|
|
|
snprintf(p, p_max_size, "%.8s", pw->pw_name);
|
|
|
|
} else {
|
2020-02-26 10:32:34 +00:00
|
|
|
snprintf(p, p_max_size, "%d", td->list[td->num]->uid);
|
2018-08-06 08:42:08 +00:00
|
|
|
}
|
2011-10-10 19:54:18 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 02:20:43 +00:00
|
|
|
#define PRINT_TOP_GENERATOR(name, width, fmt, field) \
|
|
|
|
static void print_top_##name(struct text_object *obj, char *p, \
|
|
|
|
unsigned 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, std::min(p_max_size, width), fmt, td->list[td->num]->field); \
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
#define PRINT_TOP_HR_GENERATOR(name, field, denom) \
|
|
|
|
static void print_top_##name(struct text_object *obj, char *p, \
|
2018-08-08 13:33:13 +00:00
|
|
|
unsigned int p_max_size) { \
|
2018-05-12 23:26:31 +00:00
|
|
|
struct top_data *td = (struct top_data *)obj->data.opaque; \
|
|
|
|
if (!td || !td->list || !td->list[td->num]) return; \
|
|
|
|
human_readable(td->list[td->num]->field / (denom), p, p_max_size); \
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
2009-12-12 14:47:00 +00:00
|
|
|
|
2018-12-21 02:20:43 +00:00
|
|
|
PRINT_TOP_GENERATOR(cpu, (unsigned int)7, "%6.2f", amount)
|
2019-10-16 23:44:39 +00:00
|
|
|
PRINT_TOP_GENERATOR(pid, (unsigned int)8, "%7i", pid)
|
2018-12-21 02:20:43 +00:00
|
|
|
PRINT_TOP_GENERATOR(uid, (unsigned int)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())
|
2018-12-21 02:20:43 +00:00
|
|
|
PRINT_TOP_GENERATOR(io_perc, (unsigned int)7, "%6.2f", io_perc)
|
2010-01-07 18:14:53 +00:00
|
|
|
#endif /* BUILD_IOSTATS */
|
2009-12-12 14:47:00 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
static void free_top(struct text_object *obj) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *td = static_cast<struct top_data *>(obj->data.opaque);
|
2009-12-12 14:47:00 +00:00
|
|
|
|
2018-12-21 02:20:43 +00:00
|
|
|
if (td == nullptr) { return; }
|
2018-05-12 16:03:00 +00:00
|
|
|
free_and_zero(td->s);
|
|
|
|
free_and_zero(obj->data.opaque);
|
2009-12-12 14:47:00 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
int parse_top_args(const char *s, const char *arg, struct text_object *obj) {
|
|
|
|
struct top_data *td;
|
|
|
|
char buf[64];
|
|
|
|
int n;
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
if (arg == nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
NORM_ERR("top needs arguments");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
obj->data.opaque = td =
|
|
|
|
static_cast<struct top_data *>(malloc(sizeof(struct top_data)));
|
2018-05-12 16:03:00 +00:00
|
|
|
memset(td, 0, sizeof(struct top_data));
|
|
|
|
|
|
|
|
if (s[3] == 0) {
|
|
|
|
td->list = info.cpu;
|
|
|
|
top_cpu = 1;
|
|
|
|
} else if (strcmp(&s[3], "_mem") == EQUAL) {
|
|
|
|
td->list = info.memu;
|
|
|
|
top_mem = 1;
|
|
|
|
} else if (strcmp(&s[3], "_time") == EQUAL) {
|
|
|
|
td->list = info.time;
|
|
|
|
top_time = 1;
|
2010-01-07 18:14:53 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2018-05-12 16:03:00 +00:00
|
|
|
} else if (strcmp(&s[3], "_io") == EQUAL) {
|
|
|
|
td->list = info.io;
|
|
|
|
top_io = 1;
|
2010-01-07 18:14:53 +00:00
|
|
|
#endif /* BUILD_IOSTATS */
|
2018-05-12 16:03:00 +00:00
|
|
|
} else {
|
2010-01-07 18:14:53 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2018-05-12 16:03:00 +00:00
|
|
|
NORM_ERR("Must be top, top_mem, top_time or top_io");
|
2021-01-24 00:19:04 +00:00
|
|
|
#else /* BUILD_IOSTATS */
|
2018-05-12 16:03:00 +00:00
|
|
|
NORM_ERR("Must be top, top_mem or top_time");
|
2010-01-07 18:14:53 +00:00
|
|
|
#endif /* BUILD_IOSTATS */
|
2018-05-12 16:03:00 +00:00
|
|
|
free_and_zero(obj->data.opaque);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
td->s = strndup(arg, text_buffer_size.get(*state));
|
|
|
|
|
|
|
|
if (sscanf(arg, "%63s %i", buf, &n) == 2) {
|
|
|
|
if (strcmp(buf, "name") == EQUAL) {
|
|
|
|
obj->callbacks.print = &print_top_name;
|
|
|
|
} else if (strcmp(buf, "cpu") == EQUAL) {
|
|
|
|
obj->callbacks.print = &print_top_cpu;
|
|
|
|
} else if (strcmp(buf, "pid") == EQUAL) {
|
|
|
|
obj->callbacks.print = &print_top_pid;
|
|
|
|
} else if (strcmp(buf, "mem") == EQUAL) {
|
|
|
|
obj->callbacks.print = &print_top_mem;
|
|
|
|
} else if (strcmp(buf, "time") == EQUAL) {
|
|
|
|
obj->callbacks.print = &print_top_time;
|
|
|
|
} else if (strcmp(buf, "mem_res") == EQUAL) {
|
|
|
|
obj->callbacks.print = &print_top_mem_res;
|
|
|
|
} else if (strcmp(buf, "mem_vsize") == EQUAL) {
|
|
|
|
obj->callbacks.print = &print_top_mem_vsize;
|
|
|
|
} 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
|
2018-05-12 16:03:00 +00:00
|
|
|
} else if (strcmp(buf, "io_read") == EQUAL) {
|
|
|
|
obj->callbacks.print = &print_top_read_bytes;
|
|
|
|
} else if (strcmp(buf, "io_write") == EQUAL) {
|
|
|
|
obj->callbacks.print = &print_top_write_bytes;
|
|
|
|
} else if (strcmp(buf, "io_perc") == EQUAL) {
|
|
|
|
obj->callbacks.print = &print_top_io_perc;
|
2010-01-07 18:14:53 +00:00
|
|
|
#endif /* BUILD_IOSTATS */
|
2018-05-12 16:03:00 +00:00
|
|
|
} else {
|
|
|
|
NORM_ERR("invalid type arg for top");
|
2010-01-07 18:14:53 +00:00
|
|
|
#ifdef BUILD_IOSTATS
|
2018-05-12 16:03:00 +00:00
|
|
|
NORM_ERR(
|
|
|
|
"must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize, "
|
|
|
|
"io_read, io_write, io_perc");
|
2021-01-24 00:19:04 +00:00
|
|
|
#else /* BUILD_IOSTATS */
|
2018-05-12 16:03:00 +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 */
|
2018-05-12 16:03:00 +00:00
|
|
|
free_and_zero(td->s);
|
|
|
|
free_and_zero(obj->data.opaque);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (n < 1 || n > MAX_SP) {
|
|
|
|
NORM_ERR("invalid num arg for top. Must be between 1 and %d.", MAX_SP);
|
|
|
|
free_and_zero(td->s);
|
|
|
|
free_and_zero(obj->data.opaque);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-12-21 02:20:43 +00:00
|
|
|
td->num = n - 1;
|
2018-05-12 23:26:31 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
} else {
|
|
|
|
NORM_ERR("invalid argument count for top");
|
|
|
|
free_and_zero(td->s);
|
|
|
|
free_and_zero(obj->data.opaque);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
obj->callbacks.free = &free_top;
|
|
|
|
return 1;
|
2009-08-05 22:46:51 +00:00
|
|
|
}
|