Commit 4dc0a04b authored by Arnaldo Carvalho de Melo's avatar Arnaldo Carvalho de Melo Committed by Ingo Molnar

perf tools: perf_header__read() shouldn't die()

And also don't call the constructor in it, this way it adheres
to the model the other methods follow.
Signed-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
Cc: Frédéric Weisbecker <fweisbec@gmail.com>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Paul Mackerras <paulus@samba.org>
LKML-Reference: <1258649757-17554-1-git-send-email-acme@infradead.org>
Signed-off-by: default avatarIngo Molnar <mingo@elte.hu>
parent 2446042c
...@@ -400,7 +400,7 @@ static int __cmd_record(int argc, const char **argv) ...@@ -400,7 +400,7 @@ static int __cmd_record(int argc, const char **argv)
struct stat st; struct stat st;
pid_t pid = 0; pid_t pid = 0;
int flags; int flags;
int ret; int err;
unsigned long waking = 0; unsigned long waking = 0;
page_size = sysconf(_SC_PAGE_SIZE); page_size = sysconf(_SC_PAGE_SIZE);
...@@ -434,16 +434,18 @@ static int __cmd_record(int argc, const char **argv) ...@@ -434,16 +434,18 @@ static int __cmd_record(int argc, const char **argv)
exit(-1); exit(-1);
} }
if (!file_new) header = perf_header__new();
header = perf_header__read(output);
else
header = perf_header__new();
if (header == NULL) { if (header == NULL) {
pr_err("Not enough memory for reading perf file header\n"); pr_err("Not enough memory for reading perf file header\n");
return -1; return -1;
} }
if (!file_new) {
err = perf_header__read(header, output);
if (err < 0)
return err;
}
if (raw_samples) { if (raw_samples) {
perf_header__set_feat(header, HEADER_TRACE_INFO); perf_header__set_feat(header, HEADER_TRACE_INFO);
} else { } else {
...@@ -527,7 +529,7 @@ static int __cmd_record(int argc, const char **argv) ...@@ -527,7 +529,7 @@ static int __cmd_record(int argc, const char **argv)
if (hits == samples) { if (hits == samples) {
if (done) if (done)
break; break;
ret = poll(event_array, nr_poll, -1); err = poll(event_array, nr_poll, -1);
waking++; waking++;
} }
......
...@@ -1093,7 +1093,7 @@ static void process_samples(void) ...@@ -1093,7 +1093,7 @@ static void process_samples(void)
static int __cmd_timechart(void) static int __cmd_timechart(void)
{ {
int ret, rc = EXIT_FAILURE; int err, rc = EXIT_FAILURE;
unsigned long offset = 0; unsigned long offset = 0;
unsigned long head, shift; unsigned long head, shift;
struct stat statbuf; struct stat statbuf;
...@@ -1111,8 +1111,8 @@ static int __cmd_timechart(void) ...@@ -1111,8 +1111,8 @@ static int __cmd_timechart(void)
exit(-1); exit(-1);
} }
ret = fstat(input, &statbuf); err = fstat(input, &statbuf);
if (ret < 0) { if (err < 0) {
perror("failed to stat file"); perror("failed to stat file");
exit(-1); exit(-1);
} }
...@@ -1122,7 +1122,16 @@ static int __cmd_timechart(void) ...@@ -1122,7 +1122,16 @@ static int __cmd_timechart(void)
exit(0); exit(0);
} }
header = perf_header__read(input); header = perf_header__new();
if (header == NULL)
return -ENOMEM;
err = perf_header__read(header, input);
if (err < 0) {
perf_header__delete(header);
return err;
}
head = header->data_offset; head = header->data_offset;
sample_type = perf_header__sample_type(header); sample_type = perf_header__sample_type(header);
......
...@@ -106,7 +106,7 @@ int mmap_dispatch_perf_file(struct perf_header **pheader, ...@@ -106,7 +106,7 @@ int mmap_dispatch_perf_file(struct perf_header **pheader,
int *cwdlen, int *cwdlen,
char **cwd) char **cwd)
{ {
int ret, rc = EXIT_FAILURE; int err, rc = EXIT_FAILURE;
struct perf_header *header; struct perf_header *header;
unsigned long head, shift; unsigned long head, shift;
unsigned long offset = 0; unsigned long offset = 0;
...@@ -132,8 +132,8 @@ int mmap_dispatch_perf_file(struct perf_header **pheader, ...@@ -132,8 +132,8 @@ int mmap_dispatch_perf_file(struct perf_header **pheader,
exit(-1); exit(-1);
} }
ret = fstat(input, &input_stat); err = fstat(input, &input_stat);
if (ret < 0) { if (err < 0) {
perror("failed to stat file"); perror("failed to stat file");
exit(-1); exit(-1);
} }
...@@ -149,8 +149,16 @@ int mmap_dispatch_perf_file(struct perf_header **pheader, ...@@ -149,8 +149,16 @@ int mmap_dispatch_perf_file(struct perf_header **pheader,
exit(0); exit(0);
} }
*pheader = perf_header__read(input); header = perf_header__new();
header = *pheader; if (header == NULL)
return -ENOMEM;
err = perf_header__read(header, input);
if (err < 0) {
perf_header__delete(header);
return err;
}
*pheader = header;
head = header->data_offset; head = header->data_offset;
sample_type = perf_header__sample_type(header); sample_type = perf_header__sample_type(header);
......
...@@ -78,16 +78,24 @@ struct perf_header *perf_header__new(void) ...@@ -78,16 +78,24 @@ struct perf_header *perf_header__new(void)
return self; return self;
} }
void perf_header__delete(struct perf_header *self)
{
int i;
for (i = 0; i < self->attrs; ++i)
perf_header_attr__delete(self->attr[i]);
free(self->attr);
free(self);
}
int perf_header__add_attr(struct perf_header *self, int perf_header__add_attr(struct perf_header *self,
struct perf_header_attr *attr) struct perf_header_attr *attr)
{ {
int pos = self->attrs;
if (self->frozen) if (self->frozen)
return -1; return -1;
self->attrs++; if (self->attrs == self->size) {
if (self->attrs > self->size) {
int nsize = self->size * 2; int nsize = self->size * 2;
struct perf_header_attr **nattr; struct perf_header_attr **nattr;
...@@ -98,7 +106,8 @@ int perf_header__add_attr(struct perf_header *self, ...@@ -98,7 +106,8 @@ int perf_header__add_attr(struct perf_header *self,
self->size = nsize; self->size = nsize;
self->attr = nattr; self->attr = nattr;
} }
self->attr[pos] = attr;
self->attr[self->attrs++] = attr;
return 0; return 0;
} }
...@@ -441,19 +450,17 @@ static int perf_file_section__process(struct perf_file_section *self, ...@@ -441,19 +450,17 @@ static int perf_file_section__process(struct perf_file_section *self,
return 0; return 0;
} }
struct perf_header *perf_header__read(int fd) int perf_header__read(struct perf_header *self, int fd)
{ {
struct perf_header *self = perf_header__new();
struct perf_file_header f_header; struct perf_file_header f_header;
struct perf_file_attr f_attr; struct perf_file_attr f_attr;
u64 f_id; u64 f_id;
int nr_attrs, nr_ids, i, j; int nr_attrs, nr_ids, i, j;
if (self == NULL) if (perf_file_header__read(&f_header, self, fd) < 0) {
die("nomem"); pr_debug("incompatible file format\n");
return -EINVAL;
if (perf_file_header__read(&f_header, self, fd) < 0) }
die("incompatible file format");
nr_attrs = f_header.attrs.size / sizeof(f_attr); nr_attrs = f_header.attrs.size / sizeof(f_attr);
lseek(fd, f_header.attrs.offset, SEEK_SET); lseek(fd, f_header.attrs.offset, SEEK_SET);
...@@ -467,7 +474,7 @@ struct perf_header *perf_header__read(int fd) ...@@ -467,7 +474,7 @@ struct perf_header *perf_header__read(int fd)
attr = perf_header_attr__new(&f_attr.attr); attr = perf_header_attr__new(&f_attr.attr);
if (attr == NULL) if (attr == NULL)
die("nomem"); return -ENOMEM;
nr_ids = f_attr.ids.size / sizeof(u64); nr_ids = f_attr.ids.size / sizeof(u64);
lseek(fd, f_attr.ids.offset, SEEK_SET); lseek(fd, f_attr.ids.offset, SEEK_SET);
...@@ -475,11 +482,15 @@ struct perf_header *perf_header__read(int fd) ...@@ -475,11 +482,15 @@ struct perf_header *perf_header__read(int fd)
for (j = 0; j < nr_ids; j++) { for (j = 0; j < nr_ids; j++) {
do_read(fd, &f_id, sizeof(f_id)); do_read(fd, &f_id, sizeof(f_id));
if (perf_header_attr__add_id(attr, f_id) < 0) if (perf_header_attr__add_id(attr, f_id) < 0) {
die("nomem"); perf_header_attr__delete(attr);
return -ENOMEM;
}
}
if (perf_header__add_attr(self, attr) < 0) {
perf_header_attr__delete(attr);
return -ENOMEM;
} }
if (perf_header__add_attr(self, attr) < 0)
die("nomem");
lseek(fd, tmp, SEEK_SET); lseek(fd, tmp, SEEK_SET);
} }
...@@ -487,8 +498,8 @@ struct perf_header *perf_header__read(int fd) ...@@ -487,8 +498,8 @@ struct perf_header *perf_header__read(int fd)
if (f_header.event_types.size) { if (f_header.event_types.size) {
lseek(fd, f_header.event_types.offset, SEEK_SET); lseek(fd, f_header.event_types.offset, SEEK_SET);
events = malloc(f_header.event_types.size); events = malloc(f_header.event_types.size);
if (!events) if (events == NULL)
die("nomem"); return -ENOMEM;
do_read(fd, events, f_header.event_types.size); do_read(fd, events, f_header.event_types.size);
event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type); event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
} }
...@@ -498,8 +509,7 @@ struct perf_header *perf_header__read(int fd) ...@@ -498,8 +509,7 @@ struct perf_header *perf_header__read(int fd)
lseek(fd, self->data_offset, SEEK_SET); lseek(fd, self->data_offset, SEEK_SET);
self->frozen = 1; self->frozen = 1;
return 0;
return self;
} }
u64 perf_header__sample_type(struct perf_header *header) u64 perf_header__sample_type(struct perf_header *header)
......
...@@ -55,7 +55,10 @@ struct perf_header { ...@@ -55,7 +55,10 @@ struct perf_header {
DECLARE_BITMAP(adds_features, HEADER_FEAT_BITS); DECLARE_BITMAP(adds_features, HEADER_FEAT_BITS);
}; };
struct perf_header *perf_header__read(int fd); struct perf_header *perf_header__new(void);
void perf_header__delete(struct perf_header *self);
int perf_header__read(struct perf_header *self, int fd);
void perf_header__write(struct perf_header *self, int fd, bool at_exit); void perf_header__write(struct perf_header *self, int fd, bool at_exit);
int perf_header__add_attr(struct perf_header *self, int perf_header__add_attr(struct perf_header *self,
...@@ -75,8 +78,6 @@ perf_header__find_attr(u64 id, struct perf_header *header); ...@@ -75,8 +78,6 @@ perf_header__find_attr(u64 id, struct perf_header *header);
void perf_header__set_feat(struct perf_header *self, int feat); void perf_header__set_feat(struct perf_header *self, int feat);
bool perf_header__has_feat(const struct perf_header *self, int feat); bool perf_header__has_feat(const struct perf_header *self, int feat);
struct perf_header *perf_header__new(void);
int perf_header__process_sections(struct perf_header *self, int fd, int perf_header__process_sections(struct perf_header *self, int fd,
int (*process)(struct perf_file_section *self, int (*process)(struct perf_file_section *self,
int feat, int fd)); int feat, int fd));
......
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