4 * Linux Trace Toolkit library - Write trace to the virtual file system
6 * This is a simple daemonized library that reads a few LTTng debugfs channels
7 * and save them in a trace.
9 * CPU hot-plugging is supported using inotify.
11 * Copyright 2005-2010 -
12 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
14 * Michael Sills-Lavoie <michael.sills-lavoie@polymtl.ca>
15 * Oumarou Dicko <oumarou.dicko@polymtl.ca>
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.
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.
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
38 #define _XOPEN_SOURCE 600
48 #include "liblttdvfs.h"
50 struct liblttdvfs_channel_data
{
54 struct liblttdvfs_data
{
55 char path_trace
[PATH_MAX
];
62 static __thread
int thread_pipe
[2];
64 #define printf_verbose(fmt, args...) \
66 if (callbacks_data->verbose_mode) \
67 printf(fmt, ##args); \
70 int liblttdvfs_on_open_channel(struct liblttd_callbacks
*data
, struct fd_pair
*pair
, char *relative_channel_path
)
75 struct liblttdvfs_channel_data
*channel_data
;
78 pair
->user_data
= malloc(sizeof(struct liblttdvfs_channel_data
));
79 channel_data
= pair
->user_data
;
81 struct liblttdvfs_data
* callbacks_data
= data
->user_data
;
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
);
86 ret
= stat(callbacks_data
->path_trace
, &stat_buf
);
88 if (callbacks_data
->append_mode
) {
89 printf_verbose("Appending to file %s as requested\n",
90 callbacks_data
->path_trace
);
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
);
98 offset
= lseek(channel_data
->trace
, 0, SEEK_END
);
100 perror(callbacks_data
->path_trace
);
102 close(channel_data
->trace
);
106 printf("File %s exists, cannot open. Try append mode.\n", callbacks_data
->path_trace
);
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
);
121 perror("Channel output file open");
131 int liblttdvfs_on_close_channel(struct liblttd_callbacks
*data
, struct fd_pair
*pair
)
134 ret
= close(((struct liblttdvfs_channel_data
*)(pair
->user_data
))->trace
);
135 free(pair
->user_data
);
139 int liblttdvfs_on_new_channels_folder(struct liblttd_callbacks
*data
, char *relative_folder_path
)
143 struct liblttdvfs_data
* callbacks_data
= data
->user_data
;
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
);
148 ret
= mkdir(callbacks_data
->path_trace
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
150 if (errno
!= EEXIST
) {
151 perror(callbacks_data
->path_trace
);
161 int liblttdvfs_on_read_subbuffer(struct liblttd_callbacks
*data
, struct fd_pair
*pair
, unsigned int len
)
165 off_t orig_offset
= pair
->offset
;
166 int outfd
= ((struct liblttdvfs_channel_data
*)(pair
->user_data
))->trace
;
168 struct liblttdvfs_data
* callbacks_data
= data
->user_data
;
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
);
177 perror("Error in relay splice");
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
);
184 perror("Error in file splice");
188 /* This won't block, but will start writeout asynchronously */
189 sync_file_range(outfd
, pair
->offset
, ret
,
190 SYNC_FILE_RANGE_WRITE
);
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.
200 if (orig_offset
>= pair
->max_sb_size
) {
201 sync_file_range(outfd
, orig_offset
- pair
->max_sb_size
,
203 SYNC_FILE_RANGE_WAIT_BEFORE
204 | SYNC_FILE_RANGE_WRITE
205 | SYNC_FILE_RANGE_WAIT_AFTER
);
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
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.
218 posix_fadvise(outfd
, orig_offset
- pair
->max_sb_size
,
219 pair
->max_sb_size
, POSIX_FADV_DONTNEED
);
225 int liblttdvfs_on_new_thread(struct liblttd_callbacks
*data
, unsigned long thread_num
)
228 ret
= pipe(thread_pipe
);
230 perror("Error creating pipe");
236 int liblttdvfs_on_close_thread(struct liblttd_callbacks
*data
, unsigned long thread_num
)
238 close(thread_pipe
[0]); /* close read end */
239 close(thread_pipe
[1]); /* close write end */
243 int liblttdvfs_on_trace_end(struct liblttd_instance
*instance
)
245 struct liblttd_callbacks
*callbacks
= instance
->callbacks
;
246 struct liblttdvfs_data
*data
= callbacks
->user_data
;
252 struct liblttd_callbacks
* liblttdvfs_new_callbacks(char* trace_name
,
253 int append_mode
, int verbose_mode
)
255 struct liblttdvfs_data
*data
;
256 struct liblttd_callbacks
*callbacks
;
261 data
= malloc(sizeof(struct liblttdvfs_data
));
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
;
271 callbacks
= malloc(sizeof(struct liblttd_callbacks
));
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
;