Skip to content

Commit 326ba49

Browse files
JasonXingkawasaki
authored andcommitted
relayfs: support a counter tracking if per-cpu buffers is full
When using relay mechanism, we often encounter the case where new data are lost or old unconsumed data are overwritten because of slow reader. Add 'full' field in per-cpu buffer structure to detect if the above case is happening. Relay has two modes: 1) non-overwrite mode, 2) overwrite mode. So buffer being full here respectively means: 1) relayfs doesn't intend to accept new data and then simply drop them, or 2) relayfs is going to start over again and overwrite old unread data with new data. Note: this counter doesn't need any explicit lock to protect from being modified by different threads for the better performance consideration. Writers calling __relay_write/relay_write should consider how to use the lock and ensure it performs under the lock protection, thus it's not necessary to add a new small lock here. Reviewed-by: Yushan Zhou <[email protected]> Reviewed-by: Jens Axboe <[email protected]> Reviewed-by: Masami Hiramatsu (Google) <[email protected]> Signed-off-by: Jason Xing <[email protected]>
1 parent be40bba commit 326ba49

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

include/linux/relay.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@
2828
*/
2929
#define RELAYFS_CHANNEL_VERSION 7
3030

31+
/*
32+
* Relay buffer statistics
33+
*/
34+
struct rchan_buf_stats
35+
{
36+
unsigned int full_count; /* counter for buffer full */
37+
};
38+
3139
/*
3240
* Per-cpu relay channel buffer
3341
*/
@@ -43,6 +51,7 @@ struct rchan_buf
4351
struct irq_work wakeup_work; /* reader wakeup */
4452
struct dentry *dentry; /* channel file dentry */
4553
struct kref kref; /* channel buffer refcount */
54+
struct rchan_buf_stats stats; /* buffer stats */
4655
struct page **page_array; /* array of current buffer pages */
4756
unsigned int page_count; /* number of current buffer pages */
4857
unsigned int finalized; /* buffer has been finalized */

kernel/relay.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,13 @@ EXPORT_SYMBOL_GPL(relay_buf_full);
252252
static int relay_subbuf_start(struct rchan_buf *buf, void *subbuf,
253253
void *prev_subbuf)
254254
{
255+
int full = relay_buf_full(buf);
256+
257+
if (full)
258+
buf->stats.full_count++;
259+
255260
if (!buf->chan->cb->subbuf_start)
256-
return !relay_buf_full(buf);
261+
return !full;
257262

258263
return buf->chan->cb->subbuf_start(buf, subbuf,
259264
prev_subbuf);
@@ -298,6 +303,7 @@ static void __relay_reset(struct rchan_buf *buf, unsigned int init)
298303
buf->finalized = 0;
299304
buf->data = buf->start;
300305
buf->offset = 0;
306+
buf->stats.full_count = 0;
301307

302308
for (i = 0; i < buf->chan->n_subbufs; i++)
303309
buf->padding[i] = 0;

0 commit comments

Comments
 (0)