tools/testing/selftests/pipe/pipe_bench.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/pipe/pipe_bench.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/pipe/pipe_bench.c- Extension
.c- Size
- 14780 bytes
- Lines
- 617
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
errno.hfcntl.hgetopt.hpoll.hpthread.hsignal.hstdatomic.hstdint.hstdio.hstdlib.hstring.hsys/wait.htime.hunistd.h
Detected Declarations
struct wstatsstruct rstatsstruct hist_totalsfunction now_nsfunction log2_bucketfunction aggregate_wstatsfunction firstfunction compute_p50_p99function print_summaryfunction summarizefunction stress_ng_childfunction stress_ng_wait_handshakefunction spawn_stress_ngfunction kill_stress_ngfunction aligned_allocfunction free_thread_bufsfunction start_readersfunction start_writersfunction open_bench_pipefunction writefunction run_onefunction usagefunction parse_argsfunction validate_argsfunction run_sweepfunction main
Annotated Snippet
struct wstats {
uint64_t writes;
uint64_t bytes;
uint64_t lat_sum_ns;
uint64_t lat_max_ns;
uint64_t lat_hist[HIST_BUCKETS];
char *buf;
};
struct rstats {
char *buf;
};
struct hist_totals {
uint64_t writes;
uint64_t bytes;
uint64_t lat_sum;
uint64_t lat_max;
};
static inline uint64_t now_ns(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
}
static inline int log2_bucket(uint64_t v)
{
int b = 0;
if (!v)
return 0;
while (v >>= 1)
b++;
return b < HIST_BUCKETS ? b : HIST_BUCKETS - 1;
}
static void *writer(void *arg)
{
struct wstats *s = arg;
while (!atomic_load_explicit(&g_stop, memory_order_relaxed)) {
uint64_t t0 = now_ns();
ssize_t n = write(g_pipe[1], s->buf, g_msgsize);
uint64_t dt = now_ns() - t0;
if (n > 0) {
s->writes++;
s->bytes += (uint64_t)n;
s->lat_sum_ns += dt;
if (dt > s->lat_max_ns)
s->lat_max_ns = dt;
s->lat_hist[log2_bucket(dt)]++;
} else if (n < 0 && (errno == EPIPE || errno == EBADF)) {
break;
}
}
return NULL;
}
static void *reader(void *arg)
{
struct rstats *s = arg;
/*
* Drain until EOF (write end closed by main). g_stop is not checked
* here on purpose: writers may be blocked in write() with the pipe
* full when g_stop is set, so the reader must keep draining until
* main closes the write end.
*/
for (;;) {
ssize_t n = read(g_pipe[0], s->buf, g_msgsize);
if (n <= 0)
break;
}
return NULL;
}
/* Sum per-writer stats and per-bucket counts into the caller's aggregates. */
static void aggregate_wstats(struct wstats *all, int nw,
uint64_t agg[HIST_BUCKETS],
struct hist_totals *t)
{
memset(t, 0, sizeof(*t));
for (int i = 0; i < nw; i++) {
t->writes += all[i].writes;
t->bytes += all[i].bytes;
Annotation
- Immediate include surface: `errno.h`, `fcntl.h`, `getopt.h`, `poll.h`, `pthread.h`, `signal.h`, `stdatomic.h`, `stdint.h`.
- Detected declarations: `struct wstats`, `struct rstats`, `struct hist_totals`, `function now_ns`, `function log2_bucket`, `function aggregate_wstats`, `function first`, `function compute_p50_p99`, `function print_summary`, `function summarize`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.