tools/lib/perf/mmap.c
Source file repositories/reference/linux-study-clean/tools/lib/perf/mmap.c
File Facts
- System
- Linux kernel
- Corpus path
tools/lib/perf/mmap.c- Extension
.c- Size
- 12284 bytes
- Lines
- 540
- 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.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
sys/mman.hinttypes.hasm/bug.herrno.hstring.hlinux/ring_buffer.hlinux/perf_event.hperf/mmap.hperf/event.hperf/evsel.hinternal/mmap.hinternal/lib.hlinux/kernel.hlinux/math64.hlinux/stringify.hinternal.h
Detected Declarations
function perf_mmap__initfunction perf_mmap__mmap_lenfunction perf_mmap__mmapfunction perf_mmap__munmapfunction perf_mmap__getfunction perf_mmap__putfunction perf_mmap__write_tailfunction perf_mmap__read_headfunction perf_mmap__emptyfunction perf_mmap__consumefunction overwrite_rb_find_rangefunction __perf_mmap__read_initfunction perf_mmap__read_initfunction perf_mmap__readfunction perf_mmap__read_initfunction read_perf_counterfunction read_timestampfunction read_pmccntrfunction read_perf_counterfunction read_timestampfunction csr_read_numfunction read_perf_counterfunction read_timestampfunction read_perf_counterfunction read_timestampfunction perf_mmap__read_self
Annotated Snippet
if (evt_head - *start >= (unsigned int)size) {
pr_debug("Finished reading overwrite ring buffer: rewind\n");
if (evt_head - *start > (unsigned int)size)
evt_head -= pheader->size;
*end = evt_head;
return 0;
}
pheader = (struct perf_event_header *)(buf + (evt_head & mask));
if (pheader->size == 0) {
pr_debug("Finished reading overwrite ring buffer: get start\n");
*end = evt_head;
return 0;
}
evt_head += pheader->size;
pr_debug3("move evt_head: %"PRIx64"\n", evt_head);
}
WARN_ONCE(1, "Shouldn't get here\n");
return -1;
}
/*
* Report the start and end of the available data in ringbuffer
*/
static int __perf_mmap__read_init(struct perf_mmap *md)
{
u64 head = perf_mmap__read_head(md);
u64 old = md->prev;
unsigned char *data = md->base + page_size;
unsigned long size;
md->start = md->overwrite ? head : old;
md->end = md->overwrite ? old : head;
if ((md->end - md->start) < md->flush)
return -EAGAIN;
size = md->end - md->start;
if (size > (unsigned long)(md->mask) + 1) {
if (!md->overwrite) {
WARN_ONCE(1, "failed to keep up with mmap data. (warn only once)\n");
md->prev = head;
perf_mmap__consume(md);
return -EAGAIN;
}
/*
* Backward ring buffer is full. We still have a chance to read
* most of data from it.
*/
if (overwrite_rb_find_range(data, md->mask, &md->start, &md->end))
return -EINVAL;
}
return 0;
}
int perf_mmap__read_init(struct perf_mmap *map)
{
/*
* Check if event was unmapped due to a POLLHUP/POLLERR.
*/
if (!refcount_read(&map->refcnt))
return -ENOENT;
return __perf_mmap__read_init(map);
}
/*
* Mandatory for overwrite mode
* The direction of overwrite mode is backward.
* The last perf_mmap__read() will set tail to map->core.prev.
* Need to correct the map->core.prev to head which is the end of next read.
*/
void perf_mmap__read_done(struct perf_mmap *map)
{
/*
* Check if event was unmapped due to a POLLHUP/POLLERR.
*/
if (!refcount_read(&map->refcnt))
return;
map->prev = perf_mmap__read_head(map);
}
/* When check_messup is true, 'end' must points to a good entry */
static union perf_event *perf_mmap__read(struct perf_mmap *map,
Annotation
- Immediate include surface: `sys/mman.h`, `inttypes.h`, `asm/bug.h`, `errno.h`, `string.h`, `linux/ring_buffer.h`, `linux/perf_event.h`, `perf/mmap.h`.
- Detected declarations: `function perf_mmap__init`, `function perf_mmap__mmap_len`, `function perf_mmap__mmap`, `function perf_mmap__munmap`, `function perf_mmap__get`, `function perf_mmap__put`, `function perf_mmap__write_tail`, `function perf_mmap__read_head`, `function perf_mmap__empty`, `function perf_mmap__consume`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
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.