Add project status to readme
[ltt-control.git] / liblttd / liblttdvfs.c
1 /*
2 * liblttdvfs
3 *
4 * Linux Trace Toolkit library - Write trace to the virtual file system
5 *
6 * This is a simple daemonized library that reads a few LTTng debugfs channels
7 * and save them in a trace.
8 *
9 * CPU hot-plugging is supported using inotify.
10 *
11 * Copyright 2005-2010 -
12 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
13 * Copyright 2010 -
14 * Michael Sills-Lavoie <michael.sills-lavoie@polymtl.ca>
15 * Oumarou Dicko <oumarou.dicko@polymtl.ca>
16 *
17 * This library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License as published by the Free Software Foundation; either
20 * version 2.1 of the License, or (at your option) any later version.
21 *
22 * This library is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Lesser General Public License for more details.
26 *
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with this library; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 */
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #define _REENTRANT
37 #define _GNU_SOURCE
38 #define _XOPEN_SOURCE 600
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <dirent.h>
45 #include <pthread.h>
46 #include <sys/stat.h>
47
48 #include "liblttdvfs.h"
49
50 struct liblttdvfs_channel_data {
51 int trace;
52 };
53
54 struct liblttdvfs_data {
55 char path_trace[PATH_MAX];
56 char *end_path_trace;
57 int path_trace_len;
58 int append_mode;
59 int verbose_mode;
60 };
61
62 static __thread int thread_pipe[2];
63
64 #define printf_verbose(fmt, args...) \
65 do { \
66 if (callbacks_data->verbose_mode) \
67 printf(fmt, ##args); \
68 } while (0)
69
70 int liblttdvfs_on_open_channel(struct liblttd_callbacks *data, struct fd_pair *pair, char *relative_channel_path)
71 {
72 int open_ret = 0;
73 int ret;
74 struct stat stat_buf;
75 struct liblttdvfs_channel_data *channel_data;
76 off_t offset = 0;
77
78 pair->user_data = malloc(sizeof(struct liblttdvfs_channel_data));
79 channel_data = pair->user_data;
80
81 struct liblttdvfs_data* callbacks_data = data->user_data;
82
83 strncpy(callbacks_data->end_path_trace, relative_channel_path, PATH_MAX - callbacks_data->path_trace_len);
84 printf_verbose("Creating trace file %s\n", callbacks_data->path_trace);
85
86 ret = stat(callbacks_data->path_trace, &stat_buf);
87 if (ret == 0) {
88 if (callbacks_data->append_mode) {
89 printf_verbose("Appending to file %s as requested\n",
90 callbacks_data->path_trace);
91
92 channel_data->trace = open(callbacks_data->path_trace, O_WRONLY, S_IRWXU|S_IRWXG|S_IRWXO);
93 if (channel_data->trace == -1) {
94 perror(callbacks_data->path_trace);
95 open_ret = -1;
96 goto end;
97 }
98 offset = lseek(channel_data->trace, 0, SEEK_END);
99 if (offset < 0) {
100 perror(callbacks_data->path_trace);
101 open_ret = -1;
102 close(channel_data->trace);
103 goto end;
104 }
105 } else {
106 printf("File %s exists, cannot open. Try append mode.\n", callbacks_data->path_trace);
107 open_ret = -1;
108 goto end;
109 }
110 } else {
111 if (errno == ENOENT) {
112 channel_data->trace =
113 open(callbacks_data->path_trace, O_WRONLY|O_CREAT|O_EXCL, S_IRWXU|S_IRWXG|S_IRWXO);
114 if (channel_data->trace == -1) {
115 perror(callbacks_data->path_trace);
116 open_ret = -1;
117 goto end;
118 }
119 offset = 0;
120 } else {
121 perror("Channel output file open");
122 open_ret = -1;
123 goto end;
124 }
125 }
126 end:
127 return open_ret;
128
129 }
130
131 int liblttdvfs_on_close_channel(struct liblttd_callbacks *data, struct fd_pair *pair)
132 {
133 int ret;
134 ret = close(((struct liblttdvfs_channel_data *)(pair->user_data))->trace);
135 free(pair->user_data);
136 return ret;
137 }
138
139 int liblttdvfs_on_new_channels_folder(struct liblttd_callbacks *data, char *relative_folder_path)
140 {
141 int ret;
142 int open_ret = 0;
143 struct liblttdvfs_data* callbacks_data = data->user_data;
144
145 strncpy(callbacks_data->end_path_trace, relative_folder_path, PATH_MAX - callbacks_data->path_trace_len);
146 printf_verbose("Creating trace subdirectory %s\n", callbacks_data->path_trace);
147
148 ret = mkdir(callbacks_data->path_trace, S_IRWXU|S_IRWXG|S_IRWXO);
149 if (ret == -1) {
150 if (errno != EEXIST) {
151 perror(callbacks_data->path_trace);
152 open_ret = -1;
153 goto end;
154 }
155 }
156
157 end:
158 return open_ret;
159 }
160
161 int liblttdvfs_on_read_subbuffer(struct liblttd_callbacks *data, struct fd_pair *pair, unsigned int len)
162 {
163 long ret;
164 off_t offset = 0;
165 off_t orig_offset = pair->offset;
166 int outfd = ((struct liblttdvfs_channel_data *)(pair->user_data))->trace;
167
168 struct liblttdvfs_data* callbacks_data = data->user_data;
169
170 while (len > 0) {
171 printf_verbose("splice chan to pipe offset %lu\n",
172 (unsigned long)offset);
173 ret = splice(pair->channel, &offset, thread_pipe[1], NULL,
174 len, SPLICE_F_MOVE | SPLICE_F_MORE);
175 printf_verbose("splice chan to pipe ret %ld\n", ret);
176 if (ret < 0) {
177 perror("Error in relay splice");
178 goto write_end;
179 }
180 ret = splice(thread_pipe[0], NULL, outfd,
181 NULL, ret, SPLICE_F_MOVE | SPLICE_F_MORE);
182 printf_verbose("splice pipe to file %ld\n", ret);
183 if (ret < 0) {
184 perror("Error in file splice");
185 goto write_end;
186 }
187 len -= ret;
188 /* This won't block, but will start writeout asynchronously */
189 sync_file_range(outfd, pair->offset, ret,
190 SYNC_FILE_RANGE_WRITE);
191 pair->offset += ret;
192 }
193 write_end:
194 /*
195 * This does a blocking write-and-wait on any page that belongs to the
196 * subbuffer prior to the one we just wrote.
197 * Don't care about error values, as these are just hints and ways to
198 * limit the amount of page cache used.
199 */
200 if (orig_offset >= pair->max_sb_size) {
201 sync_file_range(outfd, orig_offset - pair->max_sb_size,
202 pair->max_sb_size,
203 SYNC_FILE_RANGE_WAIT_BEFORE
204 | SYNC_FILE_RANGE_WRITE
205 | SYNC_FILE_RANGE_WAIT_AFTER);
206 /*
207 * Give hints to the kernel about how we access the file:
208 * POSIX_FADV_DONTNEED : we won't re-access data in a near
209 * future after we write it.
210 * We need to call fadvise again after the file grows because
211 * the kernel does not seem to apply fadvise to non-existing
212 * parts of the file.
213 * Call fadvise _after_ having waited for the page writeback to
214 * complete because the dirty page writeback semantic is not
215 * well defined. So it can be expected to lead to lower
216 * throughput in streaming.
217 */
218 posix_fadvise(outfd, orig_offset - pair->max_sb_size,
219 pair->max_sb_size, POSIX_FADV_DONTNEED);
220 }
221
222 return ret;
223 }
224
225 int liblttdvfs_on_new_thread(struct liblttd_callbacks *data, unsigned long thread_num)
226 {
227 int ret;
228 ret = pipe(thread_pipe);
229 if (ret < 0) {
230 perror("Error creating pipe");
231 return ret;
232 }
233 return 0;
234 }
235
236 int liblttdvfs_on_close_thread(struct liblttd_callbacks *data, unsigned long thread_num)
237 {
238 close(thread_pipe[0]); /* close read end */
239 close(thread_pipe[1]); /* close write end */
240 return 0;
241 }
242
243 int liblttdvfs_on_trace_end(struct liblttd_instance *instance)
244 {
245 struct liblttd_callbacks *callbacks = instance->callbacks;
246 struct liblttdvfs_data *data = callbacks->user_data;
247
248 free(data);
249 free(callbacks);
250 }
251
252 struct liblttd_callbacks* liblttdvfs_new_callbacks(char* trace_name,
253 int append_mode, int verbose_mode)
254 {
255 struct liblttdvfs_data *data;
256 struct liblttd_callbacks *callbacks;
257
258 if (!trace_name)
259 goto error;
260
261 data = malloc(sizeof(struct liblttdvfs_data));
262 if (!data)
263 goto error;
264
265 strncpy(data->path_trace, trace_name, PATH_MAX-1);
266 data->path_trace_len = strlen(data->path_trace);
267 data->end_path_trace = data->path_trace + data->path_trace_len;
268 data->append_mode = append_mode;
269 data->verbose_mode = verbose_mode;
270
271 callbacks = malloc(sizeof(struct liblttd_callbacks));
272 if (!callbacks)
273 goto alloc_cb_error;
274
275 callbacks->on_open_channel = liblttdvfs_on_open_channel;
276 callbacks->on_close_channel = liblttdvfs_on_close_channel;
277 callbacks->on_new_channels_folder = liblttdvfs_on_new_channels_folder;
278 callbacks->on_read_subbuffer = liblttdvfs_on_read_subbuffer;
279 callbacks->on_trace_end = liblttdvfs_on_trace_end;
280 callbacks->on_new_thread = liblttdvfs_on_new_thread;
281 callbacks->on_close_thread = liblttdvfs_on_close_thread;
282 callbacks->user_data = data;
283
284 return callbacks;
285
286 /* Error handling */
287 alloc_cb_error:
288 free(data);
289 error:
290 return NULL;
291 }
This page took 0.034187 seconds and 4 git commands to generate.