summaryrefslogtreecommitdiffstats
path: root/src/tracefs-record.c
blob: b078c8615194d28150304fedaf22a41a24b76c3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
// SPDX-License-Identifier: LGPL-2.1
/*
 * Copyright (C) 2022 Google Inc, Steven Rostedt <rostedt@goodmis.org>
 */
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/select.h>

#include <kbuffer.h>

#include "tracefs.h"
#include "tracefs-local.h"

enum {
	TC_STOP			= 1 << 0,   /* Stop reading */
	TC_PERM_NONBLOCK	= 1 << 1,   /* read is always non blocking */
	TC_NONBLOCK		= 1 << 2,   /* read is non blocking */
};

struct tracefs_cpu {
	int		fd;
	int		flags;
	int		nfds;
	int		ctrl_pipe[2];
	int		splice_pipe[2];
	int		pipe_size;
	int		subbuf_size;
	int		buffered;
	int		splice_read_flags;
};

/**
 * tracefs_cpu_alloc_fd - create a tracefs_cpu instance for an existing fd
 * @fd: The file descriptor to attach the tracefs_cpu to
 * @subbuf_size: The expected size to read the subbuffer with
 * @nonblock: If true, the file will be opened in O_NONBLOCK mode
 *
 * Return a descriptor that can read the tracefs trace_pipe_raw file
 * that is associated with the given @fd and must be read in @subbuf_size.
 *
 * Returns NULL on error.
 */
struct tracefs_cpu *
tracefs_cpu_alloc_fd(int fd, int subbuf_size, bool nonblock)
{
	struct tracefs_cpu *tcpu;
	int mode = O_RDONLY;
	int ret;

	tcpu = calloc(1, sizeof(*tcpu));
	if (!tcpu)
		return NULL;

	if (nonblock) {
		mode |= O_NONBLOCK;
		tcpu->flags |= TC_NONBLOCK | TC_PERM_NONBLOCK;
	}

	tcpu->splice_pipe[0] = -1;
	tcpu->splice_pipe[1] = -1;

	tcpu->fd = fd;

	tcpu->subbuf_size = subbuf_size;

	if (tcpu->flags & TC_PERM_NONBLOCK) {
		tcpu->ctrl_pipe[0] = -1;
		tcpu->ctrl_pipe[1] = -1;
	} else {
		/* ctrl_pipe is used to break out of blocked reads */
		ret = pipe(tcpu->ctrl_pipe);
		if (ret < 0)
			goto fail;
		if (tcpu->ctrl_pipe[0] > tcpu->fd)
			tcpu->nfds = tcpu->ctrl_pipe[0] + 1;
		else
			tcpu->nfds = tcpu->fd + 1;
	}

	return tcpu;
 fail:
	free(tcpu);
	return NULL;
}

/**
 * tracefs_cpu_open - open an instance raw trace file
 * @instance: the instance (NULL for toplevel) of the cpu raw file to open
 * @cpu: The CPU that the raw trace file is associated with
 * @nonblock: If true, the file will be opened in O_NONBLOCK mode
 *
 * Return a descriptor that can read the tracefs trace_pipe_raw file
 * for a give @cpu in a given @instance.
 *
 * Returns NULL on error.
 */
struct tracefs_cpu *
tracefs_cpu_open(struct tracefs_instance *instance, int cpu, bool nonblock)
{
	struct tracefs_cpu *tcpu;
	struct tep_handle *tep;
	char path[128];
	char *buf;
	int mode = O_RDONLY;
	int subbuf_size;
	int len;
	int ret;
	int fd;

	if (nonblock)
		mode |= O_NONBLOCK;

	sprintf(path, "per_cpu/cpu%d/trace_pipe_raw", cpu);

	fd = tracefs_instance_file_open(instance, path, mode);
	if (fd < 0)
		return NULL;

	tep = tep_alloc();
	if (!tep)
		goto fail;

	/* Get the size of the page */
	buf = tracefs_instance_file_read(NULL, "events/header_page", &len);
	if (!buf)
		goto fail;

	ret = tep_parse_header_page(tep, buf, len, sizeof(long));
	free(buf);
	if (ret < 0)
		goto fail;

	subbuf_size = tep_get_sub_buffer_size(tep);
	tep_free(tep);
	tep = NULL;

	tcpu = tracefs_cpu_alloc_fd(fd, subbuf_size, nonblock);
	if (!tcpu)
		goto fail;

	return tcpu;
 fail:
	tep_free(tep);
	close(fd);
	return NULL;
}

static void close_fd(int fd)
{
	if (fd < 0)
		return;
	close(fd);
}

/**
 * tracefs_cpu_free_fd - clean up the tracefs_cpu descriptor
 * @tcpu: The descriptor created with tracefs_cpu_alloc_fd()
 *
 * Closes all the internal file descriptors that were opened by
 * tracefs_cpu_alloc_fd(), and frees the descriptor.
 */
void tracefs_cpu_free_fd(struct tracefs_cpu *tcpu)
{
	close_fd(tcpu->ctrl_pipe[0]);
	close_fd(tcpu->ctrl_pipe[1]);
	close_fd(tcpu->splice_pipe[0]);
	close_fd(tcpu->splice_pipe[1]);

	free(tcpu);
}

/**
 * tracefs_cpu_close - clean up and close a raw trace descriptor
 * @tcpu: The descriptor created with tracefs_cpu_open()
 *
 * Closes all the file descriptors associated to the trace_pipe_raw
 * opened by tracefs_cpu_open().
 */
void tracefs_cpu_close(struct tracefs_cpu *tcpu)
{
	if (!tcpu)
		return;

	close(tcpu->fd);
	tracefs_cpu_free_fd(tcpu);
}

/**
 * tracefs_cpu_read_size - Return the size of the sub buffer
 * @tcpu: The descriptor that holds the size of the sub buffer
 *
 * A lot of the functions that read the data from the trace_pipe_raw
 * expect the caller to have allocated enough space to store a full
 * subbuffer. Calling this function is a requirement to do so.
 */
int tracefs_cpu_read_size(struct tracefs_cpu *tcpu)
{
	if (!tcpu)
		return -1;
	return tcpu->subbuf_size;
}

static void set_nonblock(struct tracefs_cpu *tcpu)
{
	long flags;

	if (tcpu->flags & TC_NONBLOCK)
		return;

	flags = fcntl(tcpu->fd, F_GETFL);
	fcntl(tcpu->fd, F_SETFL, flags | O_NONBLOCK);
	tcpu->flags |= TC_NONBLOCK;
}

static void unset_nonblock(struct tracefs_cpu *tcpu)
{
	long flags;

	if (!(tcpu->flags & TC_NONBLOCK))
		return;

	flags = fcntl(tcpu->fd, F_GETFL);
	flags &= ~O_NONBLOCK;
	fcntl(tcpu->fd, F_SETFL, flags);
	tcpu->flags &= ~TC_NONBLOCK;
}

/*
 * If set to blocking mode, block until the watermark has been
 * reached, or the control has said to stop. If the contol is
 * set, then nonblock will be set to true on the way out.
 */
static int wait_on_input(struct tracefs_cpu *tcpu, bool nonblock)
{
	fd_set rfds;
	int ret;

	if (tcpu->flags & TC_PERM_NONBLOCK)
		return 1;

	if (nonblock) {
		set_nonblock(tcpu);
		return 1;
	} else {
		unset_nonblock(tcpu);
	}

	FD_ZERO(&rfds);
	FD_SET(tcpu->fd, &rfds);
	FD_SET(tcpu->ctrl_pipe[0], &rfds);

	ret = select(tcpu->nfds, &rfds, NULL, NULL, NULL);

	/* Let the application decide what to do with signals and such */
	if (ret < 0)
		return ret;

	if (FD_ISSET(tcpu->ctrl_pipe[0], &rfds)) {
		/* Flush the ctrl pipe */
		read(tcpu->ctrl_pipe[0], &ret, 1);

		/* Make nonblock as it is now stopped */
		set_nonblock(tcpu);
		/* Permanently set unblock */
		tcpu->flags |= TC_PERM_NONBLOCK;
	}

	return FD_ISSET(tcpu->fd, &rfds);
}

/**
 * tracefs_cpu_read - read from the raw trace file
 * @tcpu: The descriptor representing the raw trace file
 * @buffer: Where to read into (must be at least the size of the subbuffer)
 * @nonblock: Hint to not block on the read if there's no data.
 *
 * Reads the trace_pipe_raw files associated to @tcpu into @buffer.
 * @buffer must be at least the size of the sub buffer of the ring buffer,
 * which is returned by tracefs_cpu_read_size().
 *
 * If @nonblock is set, and there's no data available, it will return
 * immediately. Otherwise depending on how @tcpu was opened, it will
 * block. If @tcpu was opened with nonblock set, then this @nonblock
 * will make no difference.
 *
 * Returns the amount read or -1 on error.
 */
int tracefs_cpu_read(struct tracefs_cpu *tcpu, void *buffer, bool nonblock)
{
	int ret;

	/*
	 * If nonblock is set, then the wait_on_input() will return
	 * immediately, if there's nothing in the buffer, with
	 * ret == 0.
	 */
	ret = wait_on_input(tcpu, nonblock);
	if (ret <= 0)
		return ret;

	ret = read(tcpu->fd, buffer, tcpu->subbuf_size);

	/* It's OK if there's no data to read */
	if (ret < 0 && errno == EAGAIN) {
		/* Reset errno */
		errno = 0;
		ret = 0;
	}

	return ret;
}

static int init_splice(struct tracefs_cpu *tcpu)
{
	int ret;

	if (tcpu->splice_pipe[0] >= 0)
		return 0;

	ret = pipe(tcpu->splice_pipe);
	if (ret < 0)
		return ret;

	ret = fcntl(tcpu->splice_pipe[0], F_GETPIPE_SZ, &tcpu->pipe_size);
	/*
	 * F_GETPIPE_SZ was introduced in 2.6.35, ftrace was introduced
	 * in 2.6.31. If we are running on an older kernel, just fall
	 * back to using subbuf_size for splice(). It could also return
	 * the size of the pipe and not set pipe_size.
	 */
	if (ret > 0 && !tcpu->pipe_size)
		tcpu->pipe_size = ret;
	else if (ret < 0)
		tcpu->pipe_size = tcpu->subbuf_size;

	tcpu->splice_read_flags = SPLICE_F_MOVE;
	if (tcpu->flags & TC_NONBLOCK)
		tcpu->splice_read_flags |= SPLICE_F_NONBLOCK;

	return 0;
}

/**
 * tracefs_cpu_buffered_read - Read the raw trace data buffering through a pipe
 * @tcpu: The descriptor representing the raw trace file
 * @buffer: Where to read into (must be at least the size of the subbuffer)
 * @nonblock: Hint to not block on the read if there's no data.
 *
 * This is basically the same as tracefs_cpu_read() except that it uses
 * a pipe through splice to buffer reads. This will batch reads keeping
 * the reading from the ring buffer less intrusive to the system, as
 * just reading all the time can cause quite a disturbance.
 *
 * Note, one difference between this and tracefs_cpu_read() is that it
 * will read only in sub buffer pages. If the ring buffer has not filled
 * a page, then it will not return anything, even with @nonblock set.
 * Calls to tracefs_cpu_flush() should be done to read the rest of
 * the file at the end of the trace.
 *
 * Returns the amount read or -1 on error.
 */
int tracefs_cpu_buffered_read(struct tracefs_cpu *tcpu, void *buffer, bool nonblock)
{
	int mode = SPLICE_F_MOVE;
	int ret;

	if (tcpu->buffered < 0)
		tcpu->buffered = 0;

	if (tcpu->buffered)
		goto do_read;

	ret = wait_on_input(tcpu, nonblock);
	if (ret <= 0)
		return ret;

	if (tcpu->flags & TC_NONBLOCK)
		mode |= SPLICE_F_NONBLOCK;

	ret = init_splice(tcpu);
	if (ret < 0)
		return ret;

	ret = splice(tcpu->fd, NULL, tcpu->splice_pipe[1], NULL,
		     tcpu->pipe_size, mode);
	if (ret <= 0)
		return ret;

	tcpu->buffered = ret;

 do_read:
	ret = read(tcpu->splice_pipe[0], buffer, tcpu->subbuf_size);
	if (ret > 0)
		tcpu->buffered -= ret;
	return ret;
}

/**
 * tracefs_cpu_stop - Stop a blocked read of the raw tracing file
 * @tcpu: The descriptor representing the raw trace file
 *
 * This will attempt to unblock a task blocked on @tcpu reading it.
 * On older kernels, it may not do anything for the pipe reads, as
 * older kernels do not wake up tasks waiting on the ring buffer.
 *
 * Returns 0 if the tasks reading the raw tracing file does not
 * need a nudge.
 *
 * Returns 1 if that tasks may need a nudge (send a signal).
 *
 * Returns negative on error.
 */
int tracefs_cpu_stop(struct tracefs_cpu *tcpu)
{
	int ret = 1;

	if (tcpu->flags & TC_PERM_NONBLOCK)
		return 0;

	ret = write(tcpu->ctrl_pipe[1], &ret, 1);
	if (ret < 0)
		return ret;

	/* Calling ioctl() on recent kernels will wake up the waiters */
	ret = ioctl(tcpu->fd, 0);
	if (ret < 0)
		ret = 1;
	else
		ret = 0;

	set_nonblock(tcpu);

	return ret;
}

/**
 * tracefs_cpu_flush - Finish out and read the rest of the raw tracing file
 * @tcpu: The descriptor representing the raw trace file
 * @buffer: Where to read into (must be at least the size of the subbuffer)
 *
 * Reads the trace_pipe_raw file associated by the @tcpu and puts it
 * into @buffer, which must be the size of the sub buffer which is retrieved.
 * by tracefs_cpu_read_size(). This should be called at the end of tracing
 * to get the rest of the data.
 *
 * This will set the file descriptor for reading to non-blocking mode.
 *
 * Returns the number of bytes read, or negative on error.
 */
int tracefs_cpu_flush(struct tracefs_cpu *tcpu, void *buffer)
{
	int ret;

	/* Make sure that reading is now non blocking */
	set_nonblock(tcpu);

	if (tcpu->buffered < 0)
		tcpu->buffered = 0;

	if (tcpu->buffered) {
		ret = read(tcpu->splice_pipe[0], buffer, tcpu->subbuf_size);
		if (ret > 0)
			tcpu->buffered -= ret;
		return ret;
	}

	ret = read(tcpu->fd, buffer, tcpu->subbuf_size);
	if (ret > 0 && tcpu->buffered)
		tcpu->buffered -= ret;

	/* It's OK if there's no data to read */
	if (ret < 0 && errno == EAGAIN) {
		/* Reset errno */
		errno = 0;
		ret = 0;
	}

	return ret;
}

/**
 * tracefs_cpu_flush_write - Finish out and read the rest of the raw tracing file
 * @tcpu: The descriptor representing the raw trace file
 * @wfd: The write file descriptor to write the data to
 *
 * Reads the trace_pipe_raw file associated by the @tcpu and writes it to
 * @wfd. This should be called at the end of tracing to get the rest of the data.
 *
 * Returns the number of bytes written, or negative on error.
 */
int tracefs_cpu_flush_write(struct tracefs_cpu *tcpu, int wfd)
{
	char buffer[tcpu->subbuf_size];
	int ret;

	ret = tracefs_cpu_flush(tcpu, buffer);
	if (ret > 0)
		ret = write(wfd, buffer, ret);

	/* It's OK if there's no data to read */
	if (ret < 0 && errno == EAGAIN)
		ret = 0;

	return ret;
}

/**
 * tracefs_cpu_write - Write the raw trace file into a file descriptor
 * @tcpu: The descriptor representing the raw trace file
 * @wfd: The write file descriptor to write the data to
 * @nonblock: Hint to not block on the read if there's no data.
 *
 * This will pipe the data from the trace_pipe_raw file associated with @tcpu
 * into the @wfd file descriptor. If @nonblock is set, then it will not
 * block on if there's nothing to write. Note, it will only write sub buffer
 * size data to @wfd. Calls to tracefs_cpu_flush_write() are needed to
 * write out the rest.
 *
 * Returns the number of bytes read or negative on error.
 */
int tracefs_cpu_write(struct tracefs_cpu *tcpu, int wfd, bool nonblock)
{
	char buffer[tcpu->subbuf_size];
	int mode = SPLICE_F_MOVE;
	int tot_write = 0;
	int tot;
	int ret;

	ret = wait_on_input(tcpu, nonblock);
	if (ret <= 0)
		return ret;

	if (tcpu->flags & TC_NONBLOCK)
		mode |= SPLICE_F_NONBLOCK;

	ret = init_splice(tcpu);
	if (ret < 0)
		return ret;

	tot = splice(tcpu->fd, NULL, tcpu->splice_pipe[1], NULL,
		     tcpu->pipe_size, mode);
	if (tot < 0)
		return tot;

	if (tot == 0)
		return 0;

	ret = splice(tcpu->splice_pipe[0], NULL, wfd, NULL,
		     tot, SPLICE_F_MOVE | SPLICE_F_NONBLOCK);

	if (ret >= 0)
		return ret;

	/* Some file systems do not allow splicing, try writing instead */
	do {
		int r = tcpu->subbuf_size;

		if (r > tot)
			r = tot;

		ret = read(tcpu->splice_pipe[0], buffer, r);
		if (ret > 0) {
			tot -= ret;
			ret = write(wfd, buffer, ret);
		}
		if (ret > 0)
			tot_write += ret;
	} while (ret > 0);

	if (ret < 0)
		return ret;

	return tot_write;
}

/**
 * tracefs_cpu_pipe - Write the raw trace file into a pipe descriptor
 * @tcpu: The descriptor representing the raw trace file
 * @wfd: The write file descriptor to write the data to (must be a pipe)
 * @nonblock: Hint to not block on the read if there's no data.
 *
 * This will splice directly the file descriptor of the trace_pipe_raw
 * file to the given @wfd, which must be a pipe. This can also be used
 * if @tcpu was created with tracefs_cpu_create_fd() where the passed
 * in @fd there was a pipe, then @wfd does not need to be a pipe.
 *
 * Returns the number of bytes read or negative on error.
 */
int tracefs_cpu_pipe(struct tracefs_cpu *tcpu, int wfd, bool nonblock)
{
	int mode = SPLICE_F_MOVE;
	int ret;

	ret = wait_on_input(tcpu, nonblock);
	if (ret <= 0)
		return ret;

	if (tcpu->flags & TC_NONBLOCK)
		mode |= SPLICE_F_NONBLOCK;

	ret = splice(tcpu->fd, NULL, wfd, NULL,
		     tcpu->pipe_size, mode);
	return ret;
}