xref: /aosp_15_r20/external/libtraceevent/Documentation/libtraceevent-kbuffer-timestamp.txt (revision 436bf2bcd5202612ffffe471bbcc1f277cc8d28e)
1libtraceevent(3)
2================
3
4NAME
5----
6kbuffer_timestamp, kbuffer_subbuf_timestamp -
7Functions that read various data of a kbuffer descriptor
8
9SYNOPSIS
10--------
11[verse]
12--
13*#include <kbuffer.h>*
14
15unsigned long long *kbuffer_timestamp*(struct kbuffer pass:[*]_kbuf_);
16unsigned long long *kbuffer_subbuf_timestamp*(struct kbuffer pass:[*]_kbuf_, void pass:[*]_subbuf_);
17--
18
19DESCRIPTION
20-----------
21The function *kbuffer_timestamp()* returns the timestamp of the current event of _kbuf_.
22
23The function *kbuffer_subbuf_timestamp()* returns the timestamp for the sub-buffer
24that was loaded in _kbuf_. This usually is (but not guaranteed to be) the timestamp
25of the first event on the sub-buffer.
26
27The function *kbuffer_start_of_data()* returns the offset of where the delta
28
29RETURN VALUE
30------------
31*kbuffer_read_event()* returns the event that the _kbuf_ descriptor is currently at,
32or NULL if the last event was passed (by *kbuffer_next_event()*).
33
34*kbuffer_next_event()* returns the next event after the current event or NULL if there
35are no more events.
36
37*kbuffer_read_at_offset()* returns the event at a given _offset_ from the start of
38the sub-buffer stored in _kbuf_, or NULL if there exists no event. Note, _offset_
39only needs to be an offset that lands on the record, or is at the start of it. It does
40not need to be exactly at the beginning of the record.
41
42*kbuffer_missed_events()* returns 0 if there were no missed events before loaded sub-buffer.
43Returns -1 if there were an unknown number of missed events, or if the number of missed events
44is known, that number will be returned.
45
46*kbuffer_event_size()* returns the size of the data payload of the current event of _kbuf_.
47
48*kbuffer_curr_size()* returns the size of the entire record of the current event of _kbuf_.
49This includes the size of the meta data for that record.
50
51*kbuf_curr_offset()* returns the offset of the current record from the beginning of the _kbuf_
52sub-buffer.
53
54*kbuf_curr_index()* returns the index of the current record from the beginning of the _kbuf_
55data section.
56
57EXAMPLE
58-------
59[source,c]
60--
61#include <stdio.h>
62#include <stdlib.h>
63#include <fcntl.h>
64#include <unistd.h>
65#include <sys/stat.h>
66
67#include <kbuffer.h>
68
69int main (int argc, char **argv)
70{
71	unsigned long long ts;
72	struct kbuffer *kbuf;
73	struct stat st;
74	char *buf;
75	void *event;
76	int save_offset = -1;
77	int record_size;
78	int offset;
79	int index;
80	int size;
81	int ret;
82	int fd;
83	int i = 0;
84
85	if (argc < 2) {
86		printf("usage: %s raw-subbuffer-page\n", argv[0]);
87		printf(" Try: dd count=1 bs=4096 if=/sys/kernel/tracing/per_cpu/cpu0/trace_pipe_raw of=/tmp/file\n");
88		exit(0);
89	}
90
91	if (stat(argv[1], &st) < 0) {
92		perror("stat");
93		exit(-1);
94	}
95
96	buf = malloc(st.st_size);
97	if (!buf) {
98		perror("Allocating buffer");
99		exit(-1);
100	}
101
102	fd = open(argv[1], O_RDONLY);
103	if (fd < 0) {
104		perror(argv[1]);
105		exit(-1);
106	}
107
108	ret = read(fd, buf, st.st_size);
109	if (ret < 0) {
110		perror("Reading buffer");
111		exit(-1);
112	}
113	close(fd);
114
115	kbuf = kbuffer_alloc(KBUFFER_ENDIAN_SAME_AS_HOST,
116			     KBUFFER_LSIZE_SAME_AS_HOST);
117	if (!kbuf) {
118		perror("Creating kbuffer");
119		exit(-1);
120	}
121	ret = kbuffer_load_subbuffer(kbuf, buf);
122	if (ret < 0) {
123		perror("Loading sub bufer");
124		exit(-1);
125	}
126
127	if (kbuffer_subbuffer_size(kbuf) > st.st_size) {
128		fprintf(stderr, "kbuffer is bigger than raw size %d > %ld\n",
129			kbuffer_subbuffer_size(kbuf), st.st_size);
130		exit(-1);
131	}
132
133	ret = kbuffer_missed_events(kbuf);
134	if (ret) {
135		if (ret > 0)
136			printf("Missed %d events before this buffer\n", ret);
137		else
138			printf("Missed unknown number of events before this buffer\n");
139	}
140	do {
141		event = kbuffer_read_event(kbuf, &ts);
142		if (event) {
143			record_size = kbuffer_curr_size(kbuf);
144			offset = kbuffer_curr_offset(kbuf);
145			index = kbuffer_curr_index(kbuf);
146			size = kbuffer_event_size(kbuf);
147
148			if (i == 20)
149				save_offset = offset;
150			printf(" event %3d ts:%lld\trecord_size:%d size:%d\tindex:%d offset:%d\n",
151			       i++, ts, record_size, size, index, offset);
152			event = kbuffer_next_event(kbuf, NULL);
153		}
154	} while (event);
155
156	if (!event)
157		printf("Finished sub buffer\n");
158
159	if (save_offset > 0) {
160		event = kbuffer_read_at_offset(kbuf, save_offset, &ts);
161		if (!event) {
162			fprintf(stderr, "Funny, can't find event 20 at offset %d\n", save_offset);
163			exit(-1);
164		}
165		record_size = kbuffer_curr_size(kbuf);
166		offset = kbuffer_curr_offset(kbuf);
167		index = kbuffer_curr_index(kbuf);
168		size = kbuffer_event_size(kbuf);
169
170		printf("\n saved event 20 ts:%lld\trecord_size:%d size:%d\tindex:%d offset:%d\n\n",
171		       ts, record_size, size, index, offset);
172	}
173	kbuffer_free(kbuf);
174
175	return 0;
176}
177--
178FILES
179-----
180[verse]
181--
182*event-parse.h*
183	Header file to include in order to have access to the library APIs.
184*-ltraceevent*
185	Linker switch to add when building a program that uses the library.
186--
187
188SEE ALSO
189--------
190*libtraceevent*(3), *trace-cmd*(1)
191
192AUTHOR
193------
194[verse]
195--
196*Steven Rostedt* <[email protected]>, author of *libtraceevent*.
197--
198REPORTING BUGS
199--------------
200Report bugs to  <[email protected]>
201
202LICENSE
203-------
204libtraceevent is Free Software licensed under the GNU LGPL 2.1
205
206RESOURCES
207---------
208https://git.kernel.org/pub/scm/libs/libtrace/libtraceevent.git/
209