• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1libtracefs(3)
2=============
3
4NAME
5----
6tracefs_find_cid_pid, tracefs_instance_find_cid_pid, tracefs_time_conversion -
7helper functions to handle tracing guests
8
9SYNOPSIS
10--------
11[verse]
12--
13*#include <tracefs.h>*
14
15char pass:[*]*tracefs_find_cid_pid*(int _cid_);
16char pass:[*]*tracefs_instance_find_cid_pid*(struct tracefs_instance pass:[*]_instance_, int _cid_);
17int *tracefs_time_conversion*(int _cpu_, int pass:[*]_shift_, int pass:[*]_multi_, long long pass:[*]offset);
18--
19
20DESCRIPTION
21-----------
22The *tracefs_find_cid_pid*() will use tracing to follow the wakeups of connecting to
23the given _cid_ in order to find the pid of the guest thread that belongs to the vsocket cid.
24It will then read the proc file system to find the thread leader, and it will return
25the pid of the thread leader.
26
27The *tracefs_instance_find_cid_pid*() is the same as *tracefs_find_cid_pid*() but defines
28the instance to use to perform the tracing in. If NULL it will use the top level
29buffer to perform the tracing.
30
31The *tracefs_time_conversion*() will return the values used by the kernel to convert
32the raw time stamp counter into nanoseconds for the given _cpu_. Pointers for _shift_, _multi_
33and _offset_ can be NULL to be ignored, otherwise they are set with the shift, multiplier
34and offset repectively.
35
36RETURN VALUE
37------------
38Both *tracefs_find_cid_pid*() and *tracefs_instance_find_cid_pid*() will return the
39pid of the guest main thread that belongs to the _cid_, or -1 on error (or not found).
40
41EXAMPLE
42-------
43[source,c]
44--
45#include <stdlib.h>
46#include <unistd.h>
47#include <tracefs.h>
48
49#define MAX_CID		256
50
51static void find_cid(struct tracefs_instance *instance, int cid)
52{
53	int pid;
54
55	pid = tracefs_instance_find_cid_pid(instance, cid);
56	if (pid >= 0)
57		printf("%d\t%d\n", cid, pid);
58}
59
60static int find_cids(void)
61{
62	struct tracefs_instance *instance;
63	char *name;
64	int cid;
65	int ret;
66
67	ret = asprintf(&name, "vsock_find-%d\n", getpid());
68	if (ret < 0)
69		return ret;
70
71	instance = tracefs_instance_create(name);
72	free(name);
73	if (!instance)
74		return -1;
75
76	for (cid = 0; cid < MAX_CID; cid++)
77		find_cid(instance, cid);
78
79	tracefs_event_disable(instance, NULL, NULL);
80	tracefs_instance_destroy(instance);
81	tracefs_instance_free(instance);
82	return 0;
83}
84
85struct time_info {
86	int		shift;
87	int		multi;
88};
89
90static void show_time_conversion(void)
91{
92	struct time_info *tinfo;
93	int cpus;
94	int cpu;
95	int ret;
96
97	cpus = sysconf(_SC_NPROCESSORS_CONF);
98	tinfo = calloc(cpus, sizeof(*tinfo));
99	if (!tinfo)
100		exit(-1);
101
102	for (cpu = 0; cpu < cpus; cpu++) {
103		ret  = tracefs_time_conversion(cpu,
104						&tinfo[cpu].shift,
105						&tinfo[cpu].multi,
106						NULL);
107		if (ret)
108			break;
109	}
110	if (cpu != cpus) {
111		if (!cpu) {
112			perror("tracefs_time_conversion");
113			exit(-1);
114		}
115		printf("Only read %d of %d CPUs", cpu, cpus);
116		cpus = cpu + 1;
117	}
118
119	/* Check if all the shift and mult values are the same */
120	for (cpu = 1; cpu < cpus; cpu++) {
121		if (tinfo[cpu - 1].shift != tinfo[cpu].shift)
122			break;
123		if (tinfo[cpu - 1].multi != tinfo[cpu].multi)
124			break;
125	}
126
127	if (cpu == cpus) {
128		printf("All cpus have:\n");
129		printf(" shift:  %d\n", tinfo[0].shift);
130		printf(" multi:  %d\n", tinfo[0].multi);
131		printf("\n");
132		return;
133	}
134
135	for (cpu = 0; cpu < cpus; cpu++) {
136		printf("CPU: %d\n", cpu);
137		printf(" shift:  %d\n", tinfo[cpu].shift);
138		printf(" multi:  %d\n", tinfo[cpu].multi);
139		printf("\n");
140	}
141}
142
143int main(int argc, char *argv[])
144{
145	show_time_conversion();
146	find_cids();
147	exit(0);
148}
149--
150FILES
151-----
152[verse]
153--
154*tracefs.h*
155	Header file to include in order to have access to the library APIs.
156*-ltracefs*
157	Linker switch to add when building a program that uses the library.
158--
159
160SEE ALSO
161--------
162*libtracefs*(3),
163*libtraceevent*(3),
164*trace-cmd*(1)
165
166AUTHOR
167------
168[verse]
169--
170*Steven Rostedt* <rostedt@goodmis.org>
171*Tzvetomir Stoyanov* <tz.stoyanov@gmail.com>
172--
173REPORTING BUGS
174--------------
175Report bugs to  <linux-trace-devel@vger.kernel.org>
176
177LICENSE
178-------
179libtracefs is Free Software licensed under the GNU LGPL 2.1
180
181RESOURCES
182---------
183https://git.kernel.org/pub/scm/libs/libtrace/libtracefs.git/
184
185COPYING
186-------
187Copyright \(C) 2020 VMware, Inc. Free use of this software is granted under
188the terms of the GNU Public License (GPL).
189