summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNicholas Marriott <nicholas.marriott@gmail.com>2009-02-09 18:08:01 +0000
committerNicholas Marriott <nicholas.marriott@gmail.com>2009-02-09 18:08:01 +0000
commitc9cfc9a9f356386e07e6273f91dd3d46ca076c12 (patch)
treefd4f75d10b6b32715b6626e0af2c175d3b603401
parent9d90d9ad705b9059e9bfa61b86cca7744cbca53d (diff)
Don't try to change the window name unless the pid of the process chosen has
changed. Reduces CPU use. osdep-* stuff is a bit horrible now but there we go :-/.
-rw-r--r--names.c11
-rw-r--r--osdep-darwin.c10
-rw-r--r--osdep-freebsd.c40
-rw-r--r--osdep-linux.c15
-rw-r--r--osdep-openbsd.c53
-rw-r--r--tmux.h5
-rw-r--r--window.c3
7 files changed, 83 insertions, 54 deletions
diff --git a/names.c b/names.c
index 4e6eba63..1d83e8d2 100644
--- a/names.c
+++ b/names.c
@@ -1,4 +1,4 @@
-/* $Id: names.c,v 1.2 2009-01-26 22:57:19 nicm Exp $ */
+/* $Id: names.c,v 1.3 2009-02-09 18:08:01 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -53,14 +53,19 @@ set_window_names(void)
if (w->active->screen != &w->active->base)
name = NULL;
- else
- name = get_argv0(w->active->fd, w->active->tty);
+ else {
+ if (osdep_get_name(w->active->fd,
+ w->active->tty, &w->name_pid, &name) == 1)
+ continue;
+ }
+
if (name == NULL)
wname = default_window_name(w);
else {
wname = parse_window_name(name);
xfree(name);
}
+
if (strcmp(wname, w->name) == 0)
xfree(wname);
else {
diff --git a/osdep-darwin.c b/osdep-darwin.c
index 9ca0b3dd..441e5f57 100644
--- a/osdep-darwin.c
+++ b/osdep-darwin.c
@@ -1,4 +1,4 @@
-/* $Id: osdep-darwin.c,v 1.4 2009-02-07 19:41:35 nicm Exp $ */
+/* $Id: osdep-darwin.c,v 1.5 2009-02-09 18:08:01 nicm Exp $ */
/*
* Copyright (c) 2009 Joshua Elsasser <josh@elsasser.org>
@@ -25,7 +25,9 @@
#include <string.h>
#include <unistd.h>
-char *get_argv0(int, char *);
+int osdep_get_name(int, char *, pid_t *, char **);
+
+#define unused __attribute__ ((unused))
/*
* XXX This actually returns the executable path, not the process's argv[0].
@@ -33,8 +35,8 @@ char *get_argv0(int, char *);
* Apple's 'ps' source and start digging.
*/
-char *
-get_argv0(int fd, __attribute__ ((unused)) char *tty)
+int
+osdep_get_name(int fd, unused char *tty, unused pid_t *last_pid, char **name)
{
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, 0 };
size_t size;
diff --git a/osdep-freebsd.c b/osdep-freebsd.c
index 113bac3c..7b7475ee 100644
--- a/osdep-freebsd.c
+++ b/osdep-freebsd.c
@@ -1,4 +1,4 @@
-/* $Id: osdep-freebsd.c,v 1.11 2009-02-08 13:03:43 nicm Exp $ */
+/* $Id: osdep-freebsd.c,v 1.12 2009-02-09 18:08:01 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -31,8 +31,8 @@
#include <string.h>
#include <unistd.h>
-char *get_argv0(int, char *);
-char *get_proc_argv0(pid_t);
+int osdep_get_name(int, char *, pid_t *, char **);
+char *osdep_get_argv0(pid_t);
#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
@@ -41,16 +41,17 @@ char *get_proc_argv0(pid_t);
#define is_stopped(p) \
((p)->ki_stat == SSTOP || (p)->ki_stat == SZOMB)
-char *
-get_argv0(int fd, char *tty)
+int
+osdep_get_name(int fd, char *tty, pid_t *last_pid, char **name)
{
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PGRP, 0 };
struct stat sb;
size_t len;
struct kinfo_proc *buf, *newbuf, *p, *bestp;
- char *procname;
u_int i;
+ *name = NULL;
+
buf = NULL;
if (stat(tty, &sb) == -1)
@@ -114,22 +115,29 @@ retry:
if (p->ki_pid > bestp->ki_pid)
bestp = p;
}
- if (bestp != NULL) {
- procname = get_proc_argv0(bestp->ki_pid);
- if (procname == NULL || *procname == '\0') {
- free(procname);
- procname = strdup(bestp->ki_comm);
- }
- } else
- procname = NULL;
+ if (bestp == NULL) {
+ free(buf);
+ return (-1);
+ }
+ if (bestp->ki_pid == *last_pid) {
+ free(buf);
+ return (1);
+ }
+ *last_pid = bestp->ki_pid;
+
+ *name = osdep_get_argv0(bestp->ki_pid);
+ if (*name == NULL || **name == '\0') {
+ free(*name);
+ *name = strdup(bestp->ki_comm);
+ }
free(buf);
- return (procname);
+ return (0);
}
char *
-get_proc_argv0(pid_t pid)
+osdep_get_argv0(pid_t pid)
{
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ARGS, 0 };
size_t size, size2;
diff --git a/osdep-linux.c b/osdep-linux.c
index 71085aa5..079b562d 100644
--- a/osdep-linux.c
+++ b/osdep-linux.c
@@ -1,4 +1,4 @@
-/* $Id: osdep-linux.c,v 1.3 2009-02-02 15:46:36 nicm Exp $ */
+/* $Id: osdep-linux.c,v 1.4 2009-02-09 18:08:01 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -26,8 +26,8 @@
#include "tmux.h"
-char *
-get_argv0(int fd, unused char *tty)
+int
+osdep_get_name(int fd, unused char *tty, unused pid_t *last_pid, char **name)
{
FILE *f;
char *path, *buf;
@@ -35,13 +35,15 @@ get_argv0(int fd, unused char *tty)
int ch;
pid_t pgrp;
+ *name = NULL;
+
if ((pgrp = tcgetpgrp(fd)) == -1)
- return (NULL);
+ return (-1);
xasprintf(&path, "/proc/%lld/cmdline", (long long) pgrp);
if ((f = fopen(path, "r")) == NULL) {
xfree(path);
- return (NULL);
+ return (-1);
}
xfree(path);
@@ -55,9 +57,10 @@ get_argv0(int fd, unused char *tty)
}
if (buf != NULL)
buf[len] = '\0';
+ *name = buf;
fclose(f);
- return (buf);
+ return (0);
}
#endif
diff --git a/osdep-openbsd.c b/osdep-openbsd.c
index e7dcdd08..2321f5e8 100644
--- a/osdep-openbsd.c
+++ b/osdep-openbsd.c
@@ -1,4 +1,4 @@
-/* $Id: osdep-openbsd.c,v 1.11 2009-02-08 12:31:02 nicm Exp $ */
+/* $Id: osdep-openbsd.c,v 1.12 2009-02-09 18:08:01 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -35,35 +35,36 @@
#define is_stopped(p) \
((p)->p_stat == SSTOP || (p)->p_stat == SZOMB || (p)->p_stat == SDEAD)
-char *get_argv0(int, char *);
-char *get_proc_argv0(pid_t);
+int osdep_get_name(int, char *, pid_t *, char **);
+char *osdep_get_argv0(pid_t);
-char *
-get_argv0(int fd, char *tty)
+int
+osdep_get_name(int fd, char *tty, pid_t *last_pid, char **name)
{
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PGRP, 0 };
struct stat sb;
size_t len;
struct kinfo_proc *buf, *newbuf;
struct proc *p, *bestp;
- char *procname;
u_int i;
+ *name = NULL;
+
buf = NULL;
if (stat(tty, &sb) == -1)
- return (NULL);
+ return (-1);
if ((mib[3] = tcgetpgrp(fd)) == -1)
- return (NULL);
+ return (-1);
retry:
if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) == -1)
- return (NULL);
+ return (-1);
len = (len * 5) / 4;
if ((newbuf = realloc(buf, len)) == NULL) {
free(buf);
- return (NULL);
+ return (-1);
}
buf = newbuf;
@@ -71,7 +72,7 @@ retry:
if (errno == ENOMEM)
goto retry;
free(buf);
- return (NULL);
+ return (-1);
}
bestp = NULL;
@@ -123,22 +124,30 @@ retry:
if (p->p_pid > bestp->p_pid)
bestp = p;
- }
- if (bestp != NULL) {
- procname = get_proc_argv0(bestp->p_pid);
- if (procname == NULL || *procname == '\0') {
- free(procname);
- procname = strdup(bestp->p_comm);
- }
- } else
- procname = NULL;
+ }
+ if (bestp == NULL) {
+ free(buf);
+ return (-1);
+ }
+
+ if (bestp->p_pid == *last_pid) {
+ free(buf);
+ return (1);
+ }
+ *last_pid = bestp->p_pid;
+
+ *name = osdep_get_argv0(bestp->p_pid);
+ if (*name == NULL || **name == '\0') {
+ free(*name);
+ *name = strdup(bestp->p_comm);
+ }
free(buf);
- return (procname);
+ return (0);
}
char *
-get_proc_argv0(pid_t pid)
+osdep_get_argv0(pid_t pid)
{
int mib[4] = { CTL_KERN, KERN_PROC_ARGS, 0, KERN_PROC_ARGV };
size_t size;
diff --git a/tmux.h b/tmux.h
index 1c6eb8a5..86ac1da6 100644
--- a/tmux.h
+++ b/tmux.h
@@ -1,4 +1,4 @@
-/* $Id: tmux.h,v 1.261 2009-02-09 16:11:26 nicm Exp $ */
+/* $Id: tmux.h,v 1.262 2009-02-09 18:08:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -612,6 +612,7 @@ TAILQ_HEAD(window_panes, window_pane);
struct window {
char *name;
struct timeval name_timer;
+ pid_t name_pid;
struct window_pane *active;
struct window_panes panes;
@@ -1519,7 +1520,7 @@ int utf8_width(u_int);
char *section_string(char *, size_t, size_t, size_t);
/* osdep-*.c */
-char *get_argv0(int, char *);
+int osdep_get_name(int, char *, pid_t *, char **);
/* buffer.c */
struct buffer *buffer_create(size_t);
diff --git a/window.c b/window.c
index c8780e31..e77b5f46 100644
--- a/window.c
+++ b/window.c
@@ -1,4 +1,4 @@
-/* $Id: window.c,v 1.65 2009-02-08 16:11:26 nicm Exp $ */
+/* $Id: window.c,v 1.66 2009-02-09 18:08:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -235,6 +235,7 @@ window_create(const char *name, const char *cmd, const char *cwd,
}
w->active = TAILQ_FIRST(&w->panes);
+ w->name_pid = -1;
if (name != NULL) {
w->name = xstrdup(name);
options_set_number(&w->options, "automatic-rename", 0);