Skip to content

Commit

Permalink
perf mmap: Adopt push method from builtin-record.c
Browse files Browse the repository at this point in the history
The previous prep patch was just to show exactly what changed in that
function, now its time to move that method and things only it uses to
the right place, mmap.[ch]

Cc: Adrian Hunter <[email protected]>
Cc: David Ahern <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Wang Nan <[email protected]>
Link: http://lkml.kernel.org/n/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
  • Loading branch information
acmel committed Oct 23, 2017
1 parent d37f158 commit 73c17d8
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 100 deletions.
100 changes: 0 additions & 100 deletions tools/perf/builtin-record.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,53 +129,6 @@ static int process_synthesized_event(struct perf_tool *tool,
return record__write(rec, event, event->header.size);
}

static int
backward_rb_find_range(void *buf, int mask, u64 head, u64 *start, u64 *end)
{
struct perf_event_header *pheader;
u64 evt_head = head;
int size = mask + 1;

pr_debug2("backward_rb_find_range: buf=%p, head=%"PRIx64"\n", buf, head);
pheader = (struct perf_event_header *)(buf + (head & mask));
*start = head;
while (true) {
if (evt_head - head >= (unsigned int)size) {
pr_debug("Finished reading backward ring buffer: rewind\n");
if (evt_head - head > (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 backward 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;
}

static int
rb_find_range(void *data, int mask, u64 head, u64 old,
u64 *start, u64 *end, bool backward)
{
if (!backward) {
*start = old;
*end = head;
return 0;
}

return backward_rb_find_range(data, mask, head, start, end);
}

static int record__pushfn(void *to, void *bf, size_t size)
{
struct record *rec = to;
Expand All @@ -184,59 +137,6 @@ static int record__pushfn(void *to, void *bf, size_t size)
return record__write(rec, bf, size);
}

static int perf_mmap__push(struct perf_mmap *md, bool overwrite, bool backward,
void *to, int push(void *to, void *buf, size_t size))
{
u64 head = perf_mmap__read_head(md);
u64 old = md->prev;
u64 end = head, start = old;
unsigned char *data = md->base + page_size;
unsigned long size;
void *buf;
int rc = 0;

if (rb_find_range(data, md->mask, head,
old, &start, &end, backward))
return -1;

if (start == end)
return 0;

size = end - start;
if (size > (unsigned long)(md->mask) + 1) {
WARN_ONCE(1, "failed to keep up with mmap data. (warn only once)\n");

md->prev = head;
perf_mmap__consume(md, overwrite || backward);
return 0;
}

if ((start & md->mask) + size != (end & md->mask)) {
buf = &data[start & md->mask];
size = md->mask + 1 - (start & md->mask);
start += size;

if (push(to, buf, size) < 0) {
rc = -1;
goto out;
}
}

buf = &data[start & md->mask];
size = end - start;
start += size;

if (push(to, buf, size) < 0) {
rc = -1;
goto out;
}

md->prev = head;
perf_mmap__consume(md, overwrite || backward);
out:
return rc;
}

static volatile int done;
static volatile int signr = -1;
static volatile int child_finished;
Expand Down
100 changes: 100 additions & 0 deletions tools/perf/util/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
*/

#include <sys/mman.h>
#include <inttypes.h>
#include <asm/bug.h>
#include "debug.h"
#include "event.h"
#include "mmap.h"
#include "util.h" /* page_size */
Expand Down Expand Up @@ -250,3 +253,100 @@ int perf_mmap__mmap(struct perf_mmap *map, struct mmap_params *mp, int fd)

return 0;
}

static int backward_rb_find_range(void *buf, int mask, u64 head, u64 *start, u64 *end)
{
struct perf_event_header *pheader;
u64 evt_head = head;
int size = mask + 1;

pr_debug2("backward_rb_find_range: buf=%p, head=%"PRIx64"\n", buf, head);
pheader = (struct perf_event_header *)(buf + (head & mask));
*start = head;
while (true) {
if (evt_head - head >= (unsigned int)size) {
pr_debug("Finished reading backward ring buffer: rewind\n");
if (evt_head - head > (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 backward 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;
}

static int rb_find_range(void *data, int mask, u64 head, u64 old,
u64 *start, u64 *end, bool backward)
{
if (!backward) {
*start = old;
*end = head;
return 0;
}

return backward_rb_find_range(data, mask, head, start, end);
}

int perf_mmap__push(struct perf_mmap *md, bool overwrite, bool backward,
void *to, int push(void *to, void *buf, size_t size))
{
u64 head = perf_mmap__read_head(md);
u64 old = md->prev;
u64 end = head, start = old;
unsigned char *data = md->base + page_size;
unsigned long size;
void *buf;
int rc = 0;

if (rb_find_range(data, md->mask, head, old, &start, &end, backward))
return -1;

if (start == end)
return 0;

size = end - start;
if (size > (unsigned long)(md->mask) + 1) {
WARN_ONCE(1, "failed to keep up with mmap data. (warn only once)\n");

md->prev = head;
perf_mmap__consume(md, overwrite || backward);
return 0;
}

if ((start & md->mask) + size != (end & md->mask)) {
buf = &data[start & md->mask];
size = md->mask + 1 - (start & md->mask);
start += size;

if (push(to, buf, size) < 0) {
rc = -1;
goto out;
}
}

buf = &data[start & md->mask];
size = end - start;
start += size;

if (push(to, buf, size) < 0) {
rc = -1;
goto out;
}

md->prev = head;
perf_mmap__consume(md, overwrite || backward);
out:
return rc;
}
3 changes: 3 additions & 0 deletions tools/perf/util/mmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ static inline void perf_mmap__write_tail(struct perf_mmap *md, u64 tail)
union perf_event *perf_mmap__read_forward(struct perf_mmap *map, bool check_messup);
union perf_event *perf_mmap__read_backward(struct perf_mmap *map);

int perf_mmap__push(struct perf_mmap *md, bool overwrite, bool backward,
void *to, int push(void *to, void *buf, size_t size));

size_t perf_mmap__mmap_len(struct perf_mmap *map);

#endif /*__PERF_MMAP_H */

0 comments on commit 73c17d8

Please sign in to comment.