#include "../perf.h"
#include "trace-event.h"
+#include <linux/bitmap.h>
+
/*
* Create new perf.data header attribute:
*/
*/
struct perf_header *perf_header__new(void)
{
- struct perf_header *self = malloc(sizeof(*self));
+ struct perf_header *self = calloc(sizeof(*self), 1);
if (!self)
die("nomem");
- self->frozen = 0;
-
- self->attrs = 0;
self->size = 1;
self->attr = malloc(sizeof(void *));
if (!self->attr)
die("nomem");
- self->data_offset = 0;
- self->data_size = 0;
- self->trace_info_offset = 0;
- self->trace_info_size = 0;
-
return self;
}
struct perf_file_section attrs;
struct perf_file_section data;
struct perf_file_section event_types;
- struct perf_file_section trace_info;
+ feat_mask_t adds_features;
};
-static int trace_info;
-
-void perf_header__set_trace_info(void)
+void perf_header__feat_trace_info(struct perf_header *header)
{
- trace_info = 1;
+ set_bit(HEADER_TRACE_INFO, perf_header__adds_mask(header));
}
static void do_write(int fd, void *buf, size_t size)
}
}
+static void perf_header__adds_write(struct perf_header *self, int fd)
+{
+ struct perf_file_section trace_sec;
+ u64 cur_offset = lseek(fd, 0, SEEK_CUR);
+ unsigned long *feat_mask = perf_header__adds_mask(self);
+
+ if (test_bit(HEADER_TRACE_INFO, feat_mask)) {
+ /* Write trace info */
+ trace_sec.offset = lseek(fd, sizeof(trace_sec), SEEK_CUR);
+ read_tracing_data(fd, attrs, nr_counters);
+ trace_sec.size = lseek(fd, 0, SEEK_CUR) - trace_sec.offset;
+
+ /* Write trace info headers */
+ lseek(fd, cur_offset, SEEK_SET);
+ do_write(fd, &trace_sec, sizeof(trace_sec));
+
+ /*
+ * Update cur_offset. So that other (future)
+ * features can set their own infos in this place. But if we are
+ * the only feature, at least that seeks to the place the data
+ * should begin.
+ */
+ cur_offset = lseek(fd, trace_sec.offset + trace_sec.size, SEEK_SET);
+ }
+};
+
void perf_header__write(struct perf_header *self, int fd)
{
struct perf_file_header f_header;
if (events)
do_write(fd, events, self->event_size);
- if (trace_info) {
- static int trace_info_written;
-
- /*
- * Write it only once
- */
- if (!trace_info_written) {
- self->trace_info_offset = lseek(fd, 0, SEEK_CUR);
- read_tracing_data(fd, attrs, nr_counters);
- self->trace_info_size = lseek(fd, 0, SEEK_CUR) -
- self->trace_info_offset;
- trace_info_written = 1;
- } else {
- lseek(fd, self->trace_info_offset +
- self->trace_info_size, SEEK_SET);
- }
- }
+ perf_header__adds_write(self, fd);
self->data_offset = lseek(fd, 0, SEEK_CUR);
.offset = self->event_offset,
.size = self->event_size,
},
- .trace_info = {
- .offset = self->trace_info_offset,
- .size = self->trace_info_size,
- },
};
+ memcpy(&f_header.adds_features, &self->adds_features, sizeof(feat_mask_t));
+
lseek(fd, 0, SEEK_SET);
do_write(fd, &f_header, sizeof(f_header));
lseek(fd, self->data_offset + self->data_size, SEEK_SET);
}
}
+static void perf_header__adds_read(struct perf_header *self, int fd)
+{
+ const unsigned long *feat_mask = perf_header__adds_mask(self);
+
+ if (test_bit(HEADER_TRACE_INFO, feat_mask)) {
+ struct perf_file_section trace_sec;
+
+ do_read(fd, &trace_sec, sizeof(trace_sec));
+ lseek(fd, trace_sec.offset, SEEK_SET);
+ trace_report(fd);
+ lseek(fd, trace_sec.offset + trace_sec.size, SEEK_SET);
+ }
+};
+
struct perf_header *perf_header__read(int fd)
{
struct perf_header *self = perf_header__new();
if (f_header.size != sizeof(f_header)) {
/* Support the previous format */
- if (f_header.size == offsetof(typeof(f_header), trace_info))
- f_header.trace_info.size = 0;
- else
+ if (f_header.size == offsetof(typeof(f_header), adds_features)) {
+ unsigned long *mask = (unsigned long *)(void *)
+ &f_header.adds_features;
+ bitmap_zero(mask, HEADER_FEAT_BITS);
+ } else
die("incompatible file format");
}
nr_attrs = f_header.attrs.size / sizeof(f_attr);
event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
}
- self->trace_info_offset = f_header.trace_info.offset;
- self->trace_info_size = f_header.trace_info.size;
+ memcpy(&self->adds_features, &f_header.adds_features, sizeof(feat_mask_t));
- if (self->trace_info_size) {
- lseek(fd, self->trace_info_offset, SEEK_SET);
- trace_report(fd);
- }
+ perf_header__adds_read(self, fd);
self->event_offset = f_header.event_types.offset;
self->event_size = f_header.event_types.size;