Commit 237c96b8 authored by Adrian Hunter's avatar Adrian Hunter Committed by Arnaldo Carvalho de Melo

perf header: Add ability to keep feature sections

Many feature sections should not be re-written during perf inject. In
preparation to support that, add callbacks that a tool can use to copy
a feature section from elsewhere. perf inject will use this facility to
copy features sections from the input file.
Signed-off-by: default avatarAdrian Hunter <adrian.hunter@intel.com>
Acked-by: default avatarJiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/r/20220520132404.25853-2-adrian.hunter@intel.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 1634b5a1
...@@ -3462,9 +3462,22 @@ int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full) ...@@ -3462,9 +3462,22 @@ int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
return 0; return 0;
} }
struct header_fw {
struct feat_writer fw;
struct feat_fd *ff;
};
static int feat_writer_cb(struct feat_writer *fw, void *buf, size_t sz)
{
struct header_fw *h = container_of(fw, struct header_fw, fw);
return do_write(h->ff, buf, sz);
}
static int do_write_feat(struct feat_fd *ff, int type, static int do_write_feat(struct feat_fd *ff, int type,
struct perf_file_section **p, struct perf_file_section **p,
struct evlist *evlist) struct evlist *evlist,
struct feat_copier *fc)
{ {
int err; int err;
int ret = 0; int ret = 0;
...@@ -3478,7 +3491,23 @@ static int do_write_feat(struct feat_fd *ff, int type, ...@@ -3478,7 +3491,23 @@ static int do_write_feat(struct feat_fd *ff, int type,
(*p)->offset = lseek(ff->fd, 0, SEEK_CUR); (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
err = feat_ops[type].write(ff, evlist); /*
* Hook to let perf inject copy features sections from the input
* file.
*/
if (fc && fc->copy) {
struct header_fw h = {
.fw.write = feat_writer_cb,
.ff = ff,
};
/* ->copy() returns 0 if the feature was not copied */
err = fc->copy(fc, type, &h.fw);
} else {
err = 0;
}
if (!err)
err = feat_ops[type].write(ff, evlist);
if (err < 0) { if (err < 0) {
pr_debug("failed to write feature %s\n", feat_ops[type].name); pr_debug("failed to write feature %s\n", feat_ops[type].name);
...@@ -3494,7 +3523,8 @@ static int do_write_feat(struct feat_fd *ff, int type, ...@@ -3494,7 +3523,8 @@ static int do_write_feat(struct feat_fd *ff, int type,
} }
static int perf_header__adds_write(struct perf_header *header, static int perf_header__adds_write(struct perf_header *header,
struct evlist *evlist, int fd) struct evlist *evlist, int fd,
struct feat_copier *fc)
{ {
int nr_sections; int nr_sections;
struct feat_fd ff; struct feat_fd ff;
...@@ -3523,7 +3553,7 @@ static int perf_header__adds_write(struct perf_header *header, ...@@ -3523,7 +3553,7 @@ static int perf_header__adds_write(struct perf_header *header,
lseek(fd, sec_start + sec_size, SEEK_SET); lseek(fd, sec_start + sec_size, SEEK_SET);
for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) { for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
if (do_write_feat(&ff, feat, &p, evlist)) if (do_write_feat(&ff, feat, &p, evlist, fc))
perf_header__clear_feat(header, feat); perf_header__clear_feat(header, feat);
} }
...@@ -3561,9 +3591,10 @@ int perf_header__write_pipe(int fd) ...@@ -3561,9 +3591,10 @@ int perf_header__write_pipe(int fd)
return 0; return 0;
} }
int perf_session__write_header(struct perf_session *session, static int perf_session__do_write_header(struct perf_session *session,
struct evlist *evlist, struct evlist *evlist,
int fd, bool at_exit) int fd, bool at_exit,
struct feat_copier *fc)
{ {
struct perf_file_header f_header; struct perf_file_header f_header;
struct perf_file_attr f_attr; struct perf_file_attr f_attr;
...@@ -3615,7 +3646,7 @@ int perf_session__write_header(struct perf_session *session, ...@@ -3615,7 +3646,7 @@ int perf_session__write_header(struct perf_session *session,
header->feat_offset = header->data_offset + header->data_size; header->feat_offset = header->data_offset + header->data_size;
if (at_exit) { if (at_exit) {
err = perf_header__adds_write(header, evlist, fd); err = perf_header__adds_write(header, evlist, fd, fc);
if (err < 0) if (err < 0)
return err; return err;
} }
...@@ -3648,6 +3679,13 @@ int perf_session__write_header(struct perf_session *session, ...@@ -3648,6 +3679,13 @@ int perf_session__write_header(struct perf_session *session,
return 0; return 0;
} }
int perf_session__write_header(struct perf_session *session,
struct evlist *evlist,
int fd, bool at_exit)
{
return perf_session__do_write_header(session, evlist, fd, at_exit, NULL);
}
static int perf_header__getbuffer64(struct perf_header *header, static int perf_header__getbuffer64(struct perf_header *header,
int fd, void *buf, size_t size) int fd, void *buf, size_t size)
{ {
......
...@@ -121,6 +121,16 @@ int perf_session__write_header(struct perf_session *session, ...@@ -121,6 +121,16 @@ int perf_session__write_header(struct perf_session *session,
int fd, bool at_exit); int fd, bool at_exit);
int perf_header__write_pipe(int fd); int perf_header__write_pipe(int fd);
/* feat_writer writes a feature section to output */
struct feat_writer {
int (*write)(struct feat_writer *fw, void *buf, size_t sz);
};
/* feat_copier copies a feature section using feat_writer to output */
struct feat_copier {
int (*copy)(struct feat_copier *fc, int feat, struct feat_writer *fw);
};
void perf_header__set_feat(struct perf_header *header, int feat); void perf_header__set_feat(struct perf_header *header, int feat);
void perf_header__clear_feat(struct perf_header *header, int feat); void perf_header__clear_feat(struct perf_header *header, int feat);
bool perf_header__has_feat(const struct perf_header *header, int feat); bool perf_header__has_feat(const struct perf_header *header, int feat);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment