Commit 259032bf authored by Sonny Rao's avatar Sonny Rao Committed by Steven Rostedt

perf: Robustify proc and debugfs file recording

While attempting to create a timechart of boot up I found perf didn't
tolerate modules being loaded/unloaded.  This patch fixes this by
reading the file once and then writing the size read at the correct
point in the file.  It also simplifies the code somewhat.

Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
Signed-off-by: default avatarSonny Rao <sonnyrao@chromium.org>
Signed-off-by: default avatarMichael Neuling <mikey@neuling.org>
Link: http://lkml.kernel.org/r/10011.1310614483@neuling.orgSigned-off-by: default avatarSteven Rostedt <rostedt@goodmis.org>
parent 6331c28c
...@@ -183,106 +183,59 @@ int bigendian(void) ...@@ -183,106 +183,59 @@ int bigendian(void)
return *ptr == 0x01020304; return *ptr == 0x01020304;
} }
static unsigned long long copy_file_fd(int fd) /* unfortunately, you can not stat debugfs or proc files for size */
static void record_file(const char *file, size_t hdr_sz)
{ {
unsigned long long size = 0; unsigned long long size = 0;
char buf[BUFSIZ]; char buf[BUFSIZ], *sizep;
int r; off_t hdr_pos = lseek(output_fd, 0, SEEK_CUR);
int r, fd;
do {
r = read(fd, buf, BUFSIZ);
if (r > 0) {
size += r;
write_or_die(buf, r);
}
} while (r > 0);
return size;
}
static unsigned long long copy_file(const char *file)
{
unsigned long long size = 0;
int fd;
fd = open(file, O_RDONLY); fd = open(file, O_RDONLY);
if (fd < 0) if (fd < 0)
die("Can't read '%s'", file); die("Can't read '%s'", file);
size = copy_file_fd(fd);
close(fd);
return size; /* put in zeros for file size, then fill true size later */
} write_or_die(&size, hdr_sz);
static unsigned long get_size_fd(int fd)
{
unsigned long long size = 0;
char buf[BUFSIZ];
int r;
do { do {
r = read(fd, buf, BUFSIZ); r = read(fd, buf, BUFSIZ);
if (r > 0) if (r > 0) {
size += r; size += r;
write_or_die(buf, r);
}
} while (r > 0); } while (r > 0);
lseek(fd, 0, SEEK_SET);
return size;
}
static unsigned long get_size(const char *file)
{
unsigned long long size = 0;
int fd;
fd = open(file, O_RDONLY);
if (fd < 0)
die("Can't read '%s'", file);
size = get_size_fd(fd);
close(fd); close(fd);
return size; /* ugh, handle big-endian hdr_size == 4 */
sizep = (char*)&size;
if (bigendian())
sizep += sizeof(u64) - hdr_sz;
if (pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0)
die("writing to %s", output_file);
} }
static void read_header_files(void) static void read_header_files(void)
{ {
unsigned long long size, check_size;
char *path; char *path;
int fd; struct stat st;
path = get_tracing_file("events/header_page"); path = get_tracing_file("events/header_page");
fd = open(path, O_RDONLY); if (stat(path, &st) < 0)
if (fd < 0)
die("can't read '%s'", path); die("can't read '%s'", path);
/* unfortunately, you can not stat debugfs files for size */
size = get_size_fd(fd);
write_or_die("header_page", 12); write_or_die("header_page", 12);
write_or_die(&size, 8); record_file(path, 8);
check_size = copy_file_fd(fd);
close(fd);
if (size != check_size)
die("wrong size for '%s' size=%lld read=%lld",
path, size, check_size);
put_tracing_file(path); put_tracing_file(path);
path = get_tracing_file("events/header_event"); path = get_tracing_file("events/header_event");
fd = open(path, O_RDONLY); if (stat(path, &st) < 0)
if (fd < 0)
die("can't read '%s'", path); die("can't read '%s'", path);
size = get_size_fd(fd);
write_or_die("header_event", 13); write_or_die("header_event", 13);
write_or_die(&size, 8); record_file(path, 8);
check_size = copy_file_fd(fd);
if (size != check_size)
die("wrong size for '%s'", path);
put_tracing_file(path); put_tracing_file(path);
close(fd);
} }
static bool name_in_tp_list(char *sys, struct tracepoint_path *tps) static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
...@@ -298,7 +251,6 @@ static bool name_in_tp_list(char *sys, struct tracepoint_path *tps) ...@@ -298,7 +251,6 @@ static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
static void copy_event_system(const char *sys, struct tracepoint_path *tps) static void copy_event_system(const char *sys, struct tracepoint_path *tps)
{ {
unsigned long long size, check_size;
struct dirent *dent; struct dirent *dent;
struct stat st; struct stat st;
char *format; char *format;
...@@ -338,14 +290,8 @@ static void copy_event_system(const char *sys, struct tracepoint_path *tps) ...@@ -338,14 +290,8 @@ static void copy_event_system(const char *sys, struct tracepoint_path *tps)
sprintf(format, "%s/%s/format", sys, dent->d_name); sprintf(format, "%s/%s/format", sys, dent->d_name);
ret = stat(format, &st); ret = stat(format, &st);
if (ret >= 0) { if (ret >= 0)
/* unfortunately, you can not stat debugfs files for size */ record_file(format, 8);
size = get_size(format);
write_or_die(&size, 8);
check_size = copy_file(format);
if (size != check_size)
die("error in size of file '%s'", format);
}
free(format); free(format);
} }
...@@ -426,7 +372,7 @@ static void read_event_files(struct tracepoint_path *tps) ...@@ -426,7 +372,7 @@ static void read_event_files(struct tracepoint_path *tps)
static void read_proc_kallsyms(void) static void read_proc_kallsyms(void)
{ {
unsigned int size, check_size; unsigned int size;
const char *path = "/proc/kallsyms"; const char *path = "/proc/kallsyms";
struct stat st; struct stat st;
int ret; int ret;
...@@ -438,17 +384,12 @@ static void read_proc_kallsyms(void) ...@@ -438,17 +384,12 @@ static void read_proc_kallsyms(void)
write_or_die(&size, 4); write_or_die(&size, 4);
return; return;
} }
size = get_size(path); record_file(path, 4);
write_or_die(&size, 4);
check_size = copy_file(path);
if (size != check_size)
die("error in size of file '%s'", path);
} }
static void read_ftrace_printk(void) static void read_ftrace_printk(void)
{ {
unsigned int size, check_size; unsigned int size;
char *path; char *path;
struct stat st; struct stat st;
int ret; int ret;
...@@ -461,11 +402,8 @@ static void read_ftrace_printk(void) ...@@ -461,11 +402,8 @@ static void read_ftrace_printk(void)
write_or_die(&size, 4); write_or_die(&size, 4);
goto out; goto out;
} }
size = get_size(path); record_file(path, 4);
write_or_die(&size, 4);
check_size = copy_file(path);
if (size != check_size)
die("error in size of file '%s'", path);
out: out:
put_tracing_file(path); put_tracing_file(path);
} }
......
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