Skip to content

Instantly share code, notes, and snippets.

@kerneltoast
Created January 22, 2021 01:12
Show Gist options
  • Save kerneltoast/3171a233f88d574e9712e49e926a59cf to your computer and use it in GitHub Desktop.
Save kerneltoast/3171a233f88d574e9712e49e926a59cf to your computer and use it in GitHub Desktop.
From ecda3a46517357d190c8978b502dac1a1cb45ab0 Mon Sep 17 00:00:00 2001
From: Sultan Alsawaf <sultan@openresty.com>
Date: Thu, 21 Jan 2021 13:52:46 -0800
Subject: [PATCH] staprun print flush
---
runtime/transport/control.c | 12 ++++++++++++
runtime/transport/relay_v2.c | 24 +++++++++++++++++++++---
runtime/transport/transport_msgs.h | 3 +++
staprun/common.c | 3 ++-
staprun/relay.c | 3 +++
5 files changed, 41 insertions(+), 4 deletions(-)
diff --git a/runtime/transport/control.c b/runtime/transport/control.c
index d123bef2f..7f420a7f5 100644
--- a/runtime/transport/control.c
+++ b/runtime/transport/control.c
@@ -52,6 +52,18 @@ static ssize_t _stp_ctl_write_cmd(struct file *file, const char __user *buf, siz
if (get_user(type, (u32 __user *)buf))
return -EFAULT;
+ /* Swap the subbuf without potentially sleeping on _cmd_mutex */
+ if (type == STP_RELAY_SWITCH_SUBBUF) {
+ unsigned long flags;
+
+ /* This will only fail after the print driver is destroyed */
+ if (_stp_print_trylock_irqsave(&flags)) {
+ _stp_switch_current_subbuf();
+ _stp_print_unlock_irqrestore(&flags);
+ }
+ return count;
+ }
+
count -= sizeof(u32);
buf += sizeof(u32);
diff --git a/runtime/transport/relay_v2.c b/runtime/transport/relay_v2.c
index 27729f4c8..4ee260888 100644
--- a/runtime/transport/relay_v2.c
+++ b/runtime/transport/relay_v2.c
@@ -323,10 +323,12 @@ _stp_data_write_reserve(size_t size_request, void **entry)
buf = _stp_get_rchan_subbuf(_stp_relay_data.rchan->buf,
smp_processor_id());
- if (unlikely(buf->offset + size_request > buf->chan->subbuf_size)) {
+ if (buf->offset >= buf->chan->subbuf_size) {
size_request = __stp_relay_switch_subbuf(buf, size_request);
if (!size_request)
return 0;
+ } else if (buf->offset + size_request > buf->chan->subbuf_size) {
+ size_request = buf->chan->subbuf_size - buf->offset;
}
*entry = (char*)buf->data + buf->offset;
buf->offset += size_request;
@@ -344,8 +346,24 @@ static int _stp_data_write_commit(void *entry)
{
struct rchan_buf *buf;
+ /*
+ * For the case of a single partially filled subbuf, we must flush it in
+ * order for relay to wake the reader.
+ */
buf = _stp_get_rchan_subbuf(_stp_relay_data.rchan->buf,
- smp_processor_id());
- __stp_relay_switch_subbuf(buf, 0);
+ raw_smp_processor_id());
+ if (buf->subbufs_produced == buf->subbufs_consumed)
+ __stp_relay_switch_subbuf(buf, 0);
return 0;
}
+
+static void _stp_switch_current_subbuf(void)
+{
+ struct rchan_buf *buf;
+
+ /* Switch the current subbuf if it has data so staprun can read it */
+ buf = _stp_get_rchan_subbuf(_stp_relay_data.rchan->buf,
+ raw_smp_processor_id());
+ if (buf->offset)
+ __stp_relay_switch_subbuf(buf, 0);
+}
diff --git a/runtime/transport/transport_msgs.h b/runtime/transport/transport_msgs.h
index 9e0081c80..85f8db56f 100644
--- a/runtime/transport/transport_msgs.h
+++ b/runtime/transport/transport_msgs.h
@@ -87,6 +87,9 @@ enum
/** Send by staprun to notify module of remote identity, if any.
Only send once at startup. */
STP_REMOTE_ID,
+ /** Send by staprun to make module switch the current CPU's print
+ subbuf out for reading if there's data in it. */
+ STP_RELAY_SWITCH_SUBBUF,
/** Max number of message types, sanity check only. */
STP_MAX_CMD,
/** Sent by stapio after having recevied STP_TRANSPORT. Notifies
diff --git a/staprun/common.c b/staprun/common.c
index 6a603cd2b..b8861106b 100644
--- a/staprun/common.c
+++ b/staprun/common.c
@@ -668,7 +668,8 @@ int send_request(int type, void *data, int len)
return -1;
}
memcpy(buf, &type, sizeof (type));
- memcpy(&buf[sizeof (type)], data, len);
+ if (len)
+ memcpy(&buf[sizeof (type)], data, len);
errno = 0;
assert (control_channel >= 0);
diff --git a/staprun/relay.c b/staprun/relay.c
index d0202e52f..8d933e0df 100644
--- a/staprun/relay.c
+++ b/staprun/relay.c
@@ -188,6 +188,9 @@ static void *reader_thread(void *data)
}
}
+ /* Switch out the current subbuf if it has data */
+ send_request(STP_RELAY_SWITCH_SUBBUF, NULL, 0);
+
while ((rc = read(relay_fd[cpu], buf, sizeof(buf))) > 0) {
int wbytes = rc;
char *wbuf = buf;
--
2.30.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment