liblttd: don't fill the page cache
[ltt-control.git] / liblttd / liblttdvfs.c
CommitLineData
e4ef128f
MD
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
14af8709 38#define _XOPEN_SOURCE 600
e4ef128f
MD
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
50struct liblttdvfs_channel_data {
51 int trace;
52};
53
54struct 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
62static __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
70int 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;
14af8709 76 off_t offset = 0;
e4ef128f
MD
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 }
14af8709
MD
98 offset = lseek(channel_data->trace, 0, SEEK_END);
99 if (offset < 0) {
e4ef128f
MD
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 }
14af8709
MD
119 offset = 0;
120 } else {
121 perror("Channel output file open");
122 open_ret = -1;
123 goto end;
e4ef128f
MD
124 }
125 }
e4ef128f
MD
126end:
127 return open_ret;
128
129}
130
131int 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
139int 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
157end:
158 return open_ret;
159}
160
161int liblttdvfs_on_read_subbuffer(struct liblttd_callbacks *data, struct fd_pair *pair, unsigned int len)
162{
163 long ret;
164 off_t offset = 0;
14af8709
MD
165 off_t orig_offset = pair->offset;
166 int outfd = ((struct liblttdvfs_channel_data *)(pair->user_data))->trace;
e4ef128f
MD
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");
14af8709 178 goto write_end;
e4ef128f 179 }
14af8709 180 ret = splice(thread_pipe[0], NULL, outfd,
e4ef128f
MD
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");
14af8709 185 goto write_end;
e4ef128f
MD
186 }
187 len -= ret;
14af8709
MD
188 /*
189 * Give hints to the kernel about how we access the file:
190 * POSIX_FADV_DONTNEED : we won't re-access data in a near
191 * future after we write it.
192 * We need to call fadvise again after the file grows because
193 * the kernel does not seem to apply fadvise to non-existing
194 * parts of the file.
195 */
196 ret = posix_fadvise(outfd, 0, 0, POSIX_FADV_DONTNEED);
197 if (ret != 0) {
198 perror("fadvise");
199 /* Just a hint, not critical. Continue. */
200 ret = 0;
201 }
e4ef128f 202
14af8709
MD
203 /* This won't block, but will start writeout asynchronously */
204 sync_file_range(outfd, pair->offset, ret,
205 SYNC_FILE_RANGE_WRITE);
206 pair->offset += ret;
207 }
208write_end:
209 /*
210 * This does a blocking write-and-wait on any page that belongs to the
211 * subbuffer prior to the one we just wrote.
212 */
213 if (orig_offset >= pair->max_sb_size)
214 sync_file_range(outfd, orig_offset - pair->max_sb_size,
215 pair->max_sb_size,
216 SYNC_FILE_RANGE_WAIT_BEFORE
217 | SYNC_FILE_RANGE_WRITE
218 | SYNC_FILE_RANGE_WAIT_AFTER);
219
e4ef128f
MD
220 return ret;
221}
222
223int liblttdvfs_on_new_thread(struct liblttd_callbacks *data, unsigned long thread_num)
224{
225 int ret;
226 ret = pipe(thread_pipe);
227 if (ret < 0) {
228 perror("Error creating pipe");
229 return ret;
230 }
231 return 0;
232}
233
234int liblttdvfs_on_close_thread(struct liblttd_callbacks *data, unsigned long thread_num)
235{
236 close(thread_pipe[0]); /* close read end */
237 close(thread_pipe[1]); /* close write end */
238 return 0;
239}
240
241int liblttdvfs_on_trace_end(struct liblttd_instance *instance)
242{
243 struct liblttd_callbacks *callbacks = instance->callbacks;
244 struct liblttdvfs_data *data = callbacks->user_data;
245
246 free(data);
247 free(callbacks);
248}
249
250struct liblttd_callbacks* liblttdvfs_new_callbacks(char* trace_name,
251 int append_mode, int verbose_mode)
252{
253 struct liblttdvfs_data *data;
254 struct liblttd_callbacks *callbacks;
255
256 if (!trace_name)
257 goto error;
258
259 data = malloc(sizeof(struct liblttdvfs_data));
260 if (!data)
261 goto error;
262
263 strncpy(data->path_trace, trace_name, PATH_MAX-1);
264 data->path_trace_len = strlen(data->path_trace);
265 data->end_path_trace = data->path_trace + data->path_trace_len;
266 data->append_mode = append_mode;
267 data->verbose_mode = verbose_mode;
268
269 callbacks = malloc(sizeof(struct liblttd_callbacks));
270 if (!callbacks)
271 goto alloc_cb_error;
272
273 callbacks->on_open_channel = liblttdvfs_on_open_channel;
274 callbacks->on_close_channel = liblttdvfs_on_close_channel;
275 callbacks->on_new_channels_folder = liblttdvfs_on_new_channels_folder;
276 callbacks->on_read_subbuffer = liblttdvfs_on_read_subbuffer;
277 callbacks->on_trace_end = liblttdvfs_on_trace_end;
278 callbacks->on_new_thread = liblttdvfs_on_new_thread;
279 callbacks->on_close_thread = liblttdvfs_on_close_thread;
280 callbacks->user_data = data;
281
282 return callbacks;
283
284 /* Error handling */
285alloc_cb_error:
286 free(data);
287error:
288 return NULL;
289}
This page took 0.032531 seconds and 4 git commands to generate.