1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-17 18:45:10 +00:00

Fixed short process names in top

Conky takes process names from /proc/<pid>/stat and it's limited to 16
chars in kernel. That obviously makes top_name_width option to work only
for decreasing name length. To obtain longer names we parse
/proc/<pid>/cmdline.

Not every process has cmdline, so we take both process names and choose
one we want to use. Like this:

- Get process name from /proc/<pid>/stat

- Get process cmdline from /proc/<pid>/cmdline

- Transform cmdline to a bit simpler form(i.e. "/usr/bin/python
 /usr/bin/terminator" to "python /usr/bin/terminator"), keeping
the arguments(might be changed easily)

- Choose the one that is longer and use it
This commit is contained in:
alcazoid 2013-04-09 16:41:27 +02:00
parent 459dd4109c
commit 749083a450

View File

@ -2584,8 +2584,10 @@ static void calc_io_each(void)
static void process_parse_stat(struct process *process) static void process_parse_stat(struct process *process)
{ {
char line[BUFFER_LEN] = { 0 }, filename[BUFFER_LEN], procname[BUFFER_LEN]; char line[BUFFER_LEN] = { 0 }, filename[BUFFER_LEN], procname[BUFFER_LEN];
char cmdline[BUFFER_LEN] = { 0 }, cmdline_filename[BUFFER_LEN], cmdline_procname[BUFFER_LEN];
char tmpstr[BUFFER_LEN] = { 0 };
char state[4]; char state[4];
int ps; int ps, cmdline_ps;
unsigned long user_time = 0; unsigned long user_time = 0;
unsigned long kernel_time = 0; unsigned long kernel_time = 0;
int rc; int rc;
@ -2596,6 +2598,7 @@ static void process_parse_stat(struct process *process)
struct stat process_stat; struct stat process_stat;
snprintf(filename, sizeof(filename), PROCFS_TEMPLATE, process->pid); snprintf(filename, sizeof(filename), PROCFS_TEMPLATE, process->pid);
snprintf(cmdline_filename, sizeof(cmdline_filename), PROCFS_CMDLINE_TEMPLATE, process->pid);
ps = open(filename, O_RDONLY); ps = open(filename, O_RDONLY);
if (ps < 0) { if (ps < 0) {
@ -2616,6 +2619,54 @@ static void process_parse_stat(struct process *process)
return; return;
} }
/* Read /proc/<pid>/cmdline */
cmdline_ps = open(cmdline_filename, O_RDONLY);
if (cmdline_ps < 0) {
/* The process must have finished in the last few jiffies! */
return;
}
endl = read(cmdline_ps, cmdline, BUFFER_LEN - 1);
close(cmdline_ps);
if (endl < 0) {
return;
}
/* Some processes have null-separated arguments, let's fix it */
for(int i = 0; i < endl; i++)
if (cmdline[i] == 0)
cmdline[i] = ' ';
cmdline[endl] = 0;
/* We want to transform for example "/usr/bin/python program.py" to "python program.py"
* 1. search for first space
* 2. search for last / before first space
* 3. copy string from it's position */
char * space_ptr = strchr(cmdline, ' ');
if (space_ptr == NULL)
{
strcpy(tmpstr, cmdline);
}
else
{
long int space_pos = space_ptr - cmdline;
strncpy(tmpstr, cmdline, space_pos);
tmpstr[space_pos] = 0;
}
char * slash_ptr = strrchr(tmpstr, '/');
if (slash_ptr == NULL )
{
strncpy(cmdline_procname, cmdline, BUFFER_LEN);
}
else
{
long int slash_pos = slash_ptr - tmpstr;
strncpy(cmdline_procname, cmdline + slash_pos + 1, BUFFER_LEN - slash_pos);
cmdline_procname[BUFFER_LEN - slash_pos] = 0;
}
/* Extract cpu times from data in /proc filesystem */ /* Extract cpu times from data in /proc filesystem */
lparen = strchr(line, '('); lparen = strchr(line, '(');
rparen = strrchr(line, ')'); rparen = strrchr(line, ')');
@ -2625,6 +2676,10 @@ static void process_parse_stat(struct process *process)
rc = MIN((unsigned)(rparen - lparen - 1), sizeof(procname) - 1); rc = MIN((unsigned)(rparen - lparen - 1), sizeof(procname) - 1);
strncpy(procname, lparen + 1, rc); strncpy(procname, lparen + 1, rc);
procname[rc] = '\0'; procname[rc] = '\0';
if (strlen(procname) < strlen(cmdline_procname))
strncpy(procname, cmdline_procname, strlen(cmdline_procname)+1);
rc = sscanf(rparen + 1, "%3s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %lu " rc = sscanf(rparen + 1, "%3s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %lu "
"%lu %*s %*s %*s %d %*s %*s %*s %llu %llu", state, &process->user_time, "%lu %*s %*s %*s %d %*s %*s %*s %llu %llu", state, &process->user_time,
&process->kernel_time, &nice_val, &process->vsize, &process->rss); &process->kernel_time, &nice_val, &process->vsize, &process->rss);