Skip to content

Commit

Permalink
Fixed short process names in top
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alcazoid committed Apr 9, 2013
1 parent 459dd41 commit 749083a
Showing 1 changed file with 56 additions and 1 deletion.
57 changes: 56 additions & 1 deletion src/linux.cc
Expand Up @@ -2584,8 +2584,10 @@ static void calc_io_each(void)
static void process_parse_stat(struct process *process)
{
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];
int ps;
int ps, cmdline_ps;
unsigned long user_time = 0;
unsigned long kernel_time = 0;
int rc;
Expand All @@ -2596,6 +2598,7 @@ static void process_parse_stat(struct process *process)
struct stat process_stat;

snprintf(filename, sizeof(filename), PROCFS_TEMPLATE, process->pid);
snprintf(cmdline_filename, sizeof(cmdline_filename), PROCFS_CMDLINE_TEMPLATE, process->pid);

ps = open(filename, O_RDONLY);
if (ps < 0) {
Expand All @@ -2616,6 +2619,54 @@ static void process_parse_stat(struct process *process)
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 */
lparen = strchr(line, '(');
rparen = strrchr(line, ')');
Expand All @@ -2625,6 +2676,10 @@ static void process_parse_stat(struct process *process)
rc = MIN((unsigned)(rparen - lparen - 1), sizeof(procname) - 1);
strncpy(procname, lparen + 1, rc);
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 "
"%lu %*s %*s %*s %d %*s %*s %*s %llu %llu", state, &process->user_time,
&process->kernel_time, &nice_val, &process->vsize, &process->rss);
Expand Down

0 comments on commit 749083a

Please sign in to comment.