aboutsummaryrefslogtreecommitdiff
path: root/tty.c
diff options
context:
space:
mode:
authornicm <nicm>2017-04-19 06:52:27 +0000
committernicm <nicm>2017-04-19 06:52:27 +0000
commitfa6deb58664739a8073f188a4e3a88a122270537 (patch)
tree8051621e0d3aff1bcf8f19ea3867eee68d450fd1 /tty.c
parentf731ae4a2dc6aca565b20dffd6ddf661e585cbb5 (diff)
downloadrtmux-fa6deb58664739a8073f188a4e3a88a122270537.tar.gz
rtmux-fa6deb58664739a8073f188a4e3a88a122270537.tar.bz2
rtmux-fa6deb58664739a8073f188a4e3a88a122270537.zip
When the data we have buffered to write to a terminal grows beyond a
reasonable amount (currently width * height * 8 bytes), discard all output to the terminal and start trying to redraw periodically instead. Continue with this until the amount of data we are trying to write falls to a low level again. This helps to prevent tmux sitting on a huge buffer of data when there are processes with fast output running inside tmux but the outside terminal is slow. A new client_discarded format holds the amount of data that has been discarded due to this mechanism. The three variables (when to start this, when to stop, and how often to redraw) are basically "works for me" at the moment, this is going in to see how it goes and if it causes problems for anyone else.
Diffstat (limited to 'tty.c')
-rw-r--r--tty.c66
1 files changed, 64 insertions, 2 deletions
diff --git a/tty.c b/tty.c
index 73aeb91f..362cd36c 100644
--- a/tty.c
+++ b/tty.c
@@ -79,6 +79,10 @@ static void tty_default_attributes(struct tty *, const struct window_pane *,
#define tty_pane_full_width(tty, ctx) \
((ctx)->xoff == 0 && screen_size_x((ctx)->wp->screen) >= (tty)->sx)
+#define TTY_BLOCK_INTERVAL (100000 /* 100 milliseconds */)
+#define TTY_BLOCK_START(tty) (1 + ((tty)->sx * (tty)->sy) * 8)
+#define TTY_BLOCK_STOP(tty) (1 + ((tty)->sx * (tty)->sy) / 8)
+
void
tty_create_log(void)
{
@@ -172,6 +176,51 @@ tty_read_callback(__unused int fd, __unused short events, void *data)
}
static void
+tty_timer_callback(__unused int fd, __unused short events, void *data)
+{
+ struct tty *tty = data;
+ struct client *c = tty->client;
+ struct timeval tv = { .tv_usec = TTY_BLOCK_INTERVAL };
+
+ log_debug("%s: %zu discarded", c->name, tty->discarded);
+
+ c->flags |= CLIENT_REDRAW;
+ c->discarded += tty->discarded;
+
+ if (tty->discarded < TTY_BLOCK_STOP(tty)) {
+ tty->flags &= ~TTY_BLOCK;
+ tty_invalidate(tty);
+ return;
+ }
+ tty->discarded = 0;
+ evtimer_add(&tty->timer, &tv);
+}
+
+static int
+tty_block_maybe(struct tty *tty)
+{
+ struct client *c = tty->client;
+ size_t size = EVBUFFER_LENGTH(tty->out);
+ struct timeval tv = { .tv_usec = TTY_BLOCK_INTERVAL };
+
+ if (size < TTY_BLOCK_START(tty))
+ return (0);
+
+ if (tty->flags & TTY_BLOCK)
+ return (1);
+ tty->flags |= TTY_BLOCK;
+
+ log_debug("%s: can't keep up, %zu discarded", c->name, size);
+
+ evbuffer_drain(tty->out, size);
+ c->discarded += size;
+
+ tty->discarded = 0;
+ evtimer_add(&tty->timer, &tv);
+ return (1);
+}
+
+static void
tty_write_callback(__unused int fd, __unused short events, void *data)
{
struct tty *tty = data;
@@ -184,6 +233,9 @@ tty_write_callback(__unused int fd, __unused short events, void *data)
return;
log_debug("%s: wrote %d bytes (of %zu)", c->name, nwrite, size);
+ if (tty_block_maybe(tty))
+ return;
+
if (EVBUFFER_LENGTH(tty->out) != 0)
event_add(&tty->event_out, NULL);
}
@@ -198,7 +250,7 @@ tty_open(struct tty *tty, char **cause)
}
tty->flags |= TTY_OPENED;
- tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
+ tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_BLOCK|TTY_TIMER);
event_set(&tty->event_in, tty->fd, EV_PERSIST|EV_READ,
tty_read_callback, tty);
@@ -207,6 +259,8 @@ tty_open(struct tty *tty, char **cause)
event_set(&tty->event_out, tty->fd, EV_WRITE, tty_write_callback, tty);
tty->out = evbuffer_new();
+ evtimer_set(&tty->timer, tty_timer_callback, tty);
+
tty_start_tty(tty);
tty_keys_build(tty);
@@ -273,6 +327,9 @@ tty_stop_tty(struct tty *tty)
return;
tty->flags &= ~TTY_STARTED;
+ event_del(&tty->timer);
+ tty->flags &= ~TTY_BLOCK;
+
event_del(&tty->event_in);
event_del(&tty->event_out);
@@ -425,9 +482,14 @@ tty_add(struct tty *tty, const char *buf, size_t len)
{
struct client *c = tty->client;
+ if (tty->flags & TTY_BLOCK) {
+ tty->discarded += len;
+ return;
+ }
+
evbuffer_add(tty->out, buf, len);
log_debug("%s: %.*s", c->name, (int)len, (const char *)buf);
- tty->written += len;
+ c->written += len;
if (tty_log_fd != -1)
write(tty_log_fd, buf, len);