ps: report TIME column from TotalTime

This commit is contained in:
K. Lange 2022-04-17 21:06:10 -04:00
parent 083587733a
commit 55c333d183

View File

@ -31,9 +31,10 @@ static int show_threads = 0;
static int show_username = 0;
static int show_mem = 0;
static int show_cpu = 0;
static int show_time = 0;
static int collect_commandline = 0;
static int widths[] = {3,3,4,3,3,4,4};
static int widths[] = {3,3,4,3,3,4,4,4};
struct process {
int uid;
@ -43,6 +44,7 @@ struct process {
int vsz;
int shm;
int cpu;
unsigned long time;
char * process;
char * command_line;
};
@ -71,6 +73,7 @@ struct process * process_entry(struct dirent *dent) {
char line[LINE_LEN];
int pid = 0, uid = 0, tgid = 0, mem = 0, shm = 0, vsz = 0, cpu = 0;
unsigned long ttime = 0;
char name[100];
sprintf(tmp, "/proc/%s/status", dent->d_name);
@ -106,6 +109,8 @@ struct process * process_entry(struct dirent *dent) {
mem = atoi(tab);
} else if (strstr(line, "CpuPermille:") == line) {
cpu = atoi(tab);
} else if (strstr(line, "TotalTime:") == line) {
ttime = strtoul(tab,NULL,0);
}
}
@ -122,6 +127,7 @@ struct process * process_entry(struct dirent *dent) {
struct process * parent = process_from_pid(tgid);
if (parent) {
parent->cpu += cpu;
parent->time += ttime;
}
return NULL;
}
@ -135,6 +141,7 @@ struct process * process_entry(struct dirent *dent) {
out->shm = shm;
out->vsz = vsz;
out->cpu = cpu;
out->time = ttime;
out->process = strdup(name);
out->command_line = NULL;
@ -149,6 +156,10 @@ struct process * process_entry(struct dirent *dent) {
if ((len = sprintf(garbage, "%d", out->shm)) > widths[4]) widths[4] = len;
if ((len = sprintf(garbage, "%d.%01d", out->mem / 10, out->mem % 10)) > widths[5]) widths[5] = len;
if ((len = sprintf(garbage, "%d.%01d", out->cpu / 10, out->cpu % 10)) > widths[6]) widths[6] = len;
if ((len = sprintf(garbage, "%02lu:%02lu:%02lu",
(out->time / (1000000UL * 60 * 60)),
(out->time / (1000000UL * 60)) % 60,
(out->time / (1000000UL)) % 60)) > widths[7]) widths[7] = len;
struct passwd * p = getpwuid(out->uid);
if (p) {
@ -197,6 +208,9 @@ void print_header(void) {
printf("%*s ", widths[3], "VSZ");
printf("%*s ", widths[4], "SHM");
}
if (show_time) {
printf("%*s ", widths[7], "TIME");
}
printf("CMD\n");
}
@ -226,6 +240,15 @@ void print_entry(struct process * out) {
printf("%*d ", widths[3], out->vsz);
printf("%*d ", widths[4], out->shm);
}
if (show_time) {
char tmp[30];
sprintf(tmp, "%02lu:%02lu:%02lu",
(out->time / (1000000UL * 60 * 60)),
(out->time / (1000000UL * 60)) % 60,
(out->time / (1000000UL)) % 60);
printf("%*s ", widths[7], tmp);
}
if (out->command_line) {
printf("%s\n", out->command_line);
} else {
@ -276,6 +299,7 @@ int main (int argc, char * argv[]) {
show_username = 1;
show_mem = 1;
show_cpu = 1;
show_time = 1;
// fallthrough
case 'a':
collect_commandline = 1;