aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/util/data.c
diff options
context:
space:
mode:
authorJiri Olsa <jolsa@kernel.org>2017-01-23 16:25:41 -0500
committerArnaldo Carvalho de Melo <acme@redhat.com>2017-10-30 12:37:37 -0400
commiteae8ad8042d82775da1ddf3faa915b32854d9cf4 (patch)
treec00e93fe4edcd756a8dec1cf4e86a51473c636fc /tools/perf/util/data.c
parent8ceb41d7e305f186543c58178d2e1fe34f708948 (diff)
perf tools: Add struct perf_data_file
Add struct perf_data_file to represent a single file within a perf_data struct. Signed-off-by: Jiri Olsa <jolsa@kernel.org> Cc: Andi Kleen <andi@firstfloor.org> Cc: Changbin Du <changbin.du@intel.com> Cc: David Ahern <dsahern@gmail.com> Cc: Jin Yao <yao.jin@linux.intel.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Wang Nan <wangnan0@huawei.com> Link: http://lkml.kernel.org/n/tip-c3f9p4xzykr845ktqcek6p4t@git.kernel.org [ Fixup recent changes in 'perf script --per-event-dump' ] Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf/util/data.c')
-rw-r--r--tools/perf/util/data.c48
1 files changed, 24 insertions, 24 deletions
diff --git a/tools/perf/util/data.c b/tools/perf/util/data.c
index a6eea3df4c10..07ef56a4123c 100644
--- a/tools/perf/util/data.c
+++ b/tools/perf/util/data.c
@@ -28,16 +28,16 @@ static bool check_pipe(struct perf_data *data)
28 int fd = perf_data__is_read(data) ? 28 int fd = perf_data__is_read(data) ?
29 STDIN_FILENO : STDOUT_FILENO; 29 STDIN_FILENO : STDOUT_FILENO;
30 30
31 if (!data->path) { 31 if (!data->file.path) {
32 if (!fstat(fd, &st) && S_ISFIFO(st.st_mode)) 32 if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
33 is_pipe = true; 33 is_pipe = true;
34 } else { 34 } else {
35 if (!strcmp(data->path, "-")) 35 if (!strcmp(data->file.path, "-"))
36 is_pipe = true; 36 is_pipe = true;
37 } 37 }
38 38
39 if (is_pipe) 39 if (is_pipe)
40 data->fd = fd; 40 data->file.fd = fd;
41 41
42 return data->is_pipe = is_pipe; 42 return data->is_pipe = is_pipe;
43} 43}
@@ -46,13 +46,13 @@ static int check_backup(struct perf_data *data)
46{ 46{
47 struct stat st; 47 struct stat st;
48 48
49 if (!stat(data->path, &st) && st.st_size) { 49 if (!stat(data->file.path, &st) && st.st_size) {
50 /* TODO check errors properly */ 50 /* TODO check errors properly */
51 char oldname[PATH_MAX]; 51 char oldname[PATH_MAX];
52 snprintf(oldname, sizeof(oldname), "%s.old", 52 snprintf(oldname, sizeof(oldname), "%s.old",
53 data->path); 53 data->file.path);
54 unlink(oldname); 54 unlink(oldname);
55 rename(data->path, oldname); 55 rename(data->file.path, oldname);
56 } 56 }
57 57
58 return 0; 58 return 0;
@@ -64,13 +64,13 @@ static int open_file_read(struct perf_data *data)
64 int fd; 64 int fd;
65 char sbuf[STRERR_BUFSIZE]; 65 char sbuf[STRERR_BUFSIZE];
66 66
67 fd = open(data->path, O_RDONLY); 67 fd = open(data->file.path, O_RDONLY);
68 if (fd < 0) { 68 if (fd < 0) {
69 int err = errno; 69 int err = errno;
70 70
71 pr_err("failed to open %s: %s", data->path, 71 pr_err("failed to open %s: %s", data->file.path,
72 str_error_r(err, sbuf, sizeof(sbuf))); 72 str_error_r(err, sbuf, sizeof(sbuf)));
73 if (err == ENOENT && !strcmp(data->path, "perf.data")) 73 if (err == ENOENT && !strcmp(data->file.path, "perf.data"))
74 pr_err(" (try 'perf record' first)"); 74 pr_err(" (try 'perf record' first)");
75 pr_err("\n"); 75 pr_err("\n");
76 return -err; 76 return -err;
@@ -81,13 +81,13 @@ static int open_file_read(struct perf_data *data)
81 81
82 if (!data->force && st.st_uid && (st.st_uid != geteuid())) { 82 if (!data->force && st.st_uid && (st.st_uid != geteuid())) {
83 pr_err("File %s not owned by current user or root (use -f to override)\n", 83 pr_err("File %s not owned by current user or root (use -f to override)\n",
84 data->path); 84 data->file.path);
85 goto out_close; 85 goto out_close;
86 } 86 }
87 87
88 if (!st.st_size) { 88 if (!st.st_size) {
89 pr_info("zero-sized data (%s), nothing to do!\n", 89 pr_info("zero-sized data (%s), nothing to do!\n",
90 data->path); 90 data->file.path);
91 goto out_close; 91 goto out_close;
92 } 92 }
93 93
@@ -107,11 +107,11 @@ static int open_file_write(struct perf_data *data)
107 if (check_backup(data)) 107 if (check_backup(data))
108 return -1; 108 return -1;
109 109
110 fd = open(data->path, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC, 110 fd = open(data->file.path, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC,
111 S_IRUSR|S_IWUSR); 111 S_IRUSR|S_IWUSR);
112 112
113 if (fd < 0) 113 if (fd < 0)
114 pr_err("failed to open %s : %s\n", data->path, 114 pr_err("failed to open %s : %s\n", data->file.path,
115 str_error_r(errno, sbuf, sizeof(sbuf))); 115 str_error_r(errno, sbuf, sizeof(sbuf)));
116 116
117 return fd; 117 return fd;
@@ -124,7 +124,7 @@ static int open_file(struct perf_data *data)
124 fd = perf_data__is_read(data) ? 124 fd = perf_data__is_read(data) ?
125 open_file_read(data) : open_file_write(data); 125 open_file_read(data) : open_file_write(data);
126 126
127 data->fd = fd; 127 data->file.fd = fd;
128 return fd < 0 ? -1 : 0; 128 return fd < 0 ? -1 : 0;
129} 129}
130 130
@@ -133,21 +133,21 @@ int perf_data__open(struct perf_data *data)
133 if (check_pipe(data)) 133 if (check_pipe(data))
134 return 0; 134 return 0;
135 135
136 if (!data->path) 136 if (!data->file.path)
137 data->path = "perf.data"; 137 data->file.path = "perf.data";
138 138
139 return open_file(data); 139 return open_file(data);
140} 140}
141 141
142void perf_data__close(struct perf_data *data) 142void perf_data__close(struct perf_data *data)
143{ 143{
144 close(data->fd); 144 close(data->file.fd);
145} 145}
146 146
147ssize_t perf_data__write(struct perf_data *data, 147ssize_t perf_data__write(struct perf_data *data,
148 void *buf, size_t size) 148 void *buf, size_t size)
149{ 149{
150 return writen(data->fd, buf, size); 150 return writen(data->file.fd, buf, size);
151} 151}
152 152
153int perf_data__switch(struct perf_data *data, 153int perf_data__switch(struct perf_data *data,
@@ -162,30 +162,30 @@ int perf_data__switch(struct perf_data *data,
162 if (perf_data__is_read(data)) 162 if (perf_data__is_read(data))
163 return -EINVAL; 163 return -EINVAL;
164 164
165 if (asprintf(&new_filepath, "%s.%s", data->path, postfix) < 0) 165 if (asprintf(&new_filepath, "%s.%s", data->file.path, postfix) < 0)
166 return -ENOMEM; 166 return -ENOMEM;
167 167
168 /* 168 /*
169 * Only fire a warning, don't return error, continue fill 169 * Only fire a warning, don't return error, continue fill
170 * original file. 170 * original file.
171 */ 171 */
172 if (rename(data->path, new_filepath)) 172 if (rename(data->file.path, new_filepath))
173 pr_warning("Failed to rename %s to %s\n", data->path, new_filepath); 173 pr_warning("Failed to rename %s to %s\n", data->file.path, new_filepath);
174 174
175 if (!at_exit) { 175 if (!at_exit) {
176 close(data->fd); 176 close(data->file.fd);
177 ret = perf_data__open(data); 177 ret = perf_data__open(data);
178 if (ret < 0) 178 if (ret < 0)
179 goto out; 179 goto out;
180 180
181 if (lseek(data->fd, pos, SEEK_SET) == (off_t)-1) { 181 if (lseek(data->file.fd, pos, SEEK_SET) == (off_t)-1) {
182 ret = -errno; 182 ret = -errno;
183 pr_debug("Failed to lseek to %zu: %s", 183 pr_debug("Failed to lseek to %zu: %s",
184 pos, strerror(errno)); 184 pos, strerror(errno));
185 goto out; 185 goto out;
186 } 186 }
187 } 187 }
188 ret = data->fd; 188 ret = data->file.fd;
189out: 189out:
190 free(new_filepath); 190 free(new_filepath);
191 return ret; 191 return ret;