Skip to content

Commit 4c04654

Browse files
captain5050acmel
authored andcommitted
perf machine: Factor creating a "live" machine out of dwarf-unwind
Factor out for use in places other than the dwarf unwinding tests for libunwind. Signed-off-by: Ian Rogers <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Andi Kleen <[email protected]> Cc: Anne Macedo <[email protected]> Cc: Dmitriy Vyukov <[email protected]> Cc: Dr. David Alan Gilbert <[email protected]> Cc: Howard Chu <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: James Clark <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Kan Liang <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Michael Petlan <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Yicong Yang <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 0e71bcd commit 4c04654

File tree

3 files changed

+51
-38
lines changed

3 files changed

+51
-38
lines changed

tools/perf/tests/dwarf-unwind.c

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "symbol.h"
1616
#include "thread.h"
1717
#include "callchain.h"
18-
#include "util/synthetic-events.h"
1918

2019
/* For bsearch. We try to unwind functions in shared object. */
2120
#include <stdlib.h>
@@ -37,24 +36,6 @@
3736
#define NO_TAIL_CALL_BARRIER __asm__ __volatile__("" : : : "memory");
3837
#endif
3938

40-
static int mmap_handler(const struct perf_tool *tool __maybe_unused,
41-
union perf_event *event,
42-
struct perf_sample *sample,
43-
struct machine *machine)
44-
{
45-
return machine__process_mmap2_event(machine, event, sample);
46-
}
47-
48-
static int init_live_machine(struct machine *machine)
49-
{
50-
union perf_event event;
51-
pid_t pid = getpid();
52-
53-
memset(&event, 0, sizeof(event));
54-
return perf_event__synthesize_mmap_events(NULL, &event, pid, pid,
55-
mmap_handler, machine, true);
56-
}
57-
5839
/*
5940
* We need to keep these functions global, despite the
6041
* fact that they are used only locally in this object,
@@ -202,8 +183,12 @@ noinline int test__dwarf_unwind(struct test_suite *test __maybe_unused,
202183
struct machine *machine;
203184
struct thread *thread;
204185
int err = -1;
186+
pid_t pid = getpid();
205187

206-
machine = machine__new_host();
188+
callchain_param.record_mode = CALLCHAIN_DWARF;
189+
dwarf_callchain_users = true;
190+
191+
machine = machine__new_live(/*kernel_maps=*/true, pid);
207192
if (!machine) {
208193
pr_err("Could not get machine\n");
209194
return -1;
@@ -214,18 +199,10 @@ noinline int test__dwarf_unwind(struct test_suite *test __maybe_unused,
214199
return -1;
215200
}
216201

217-
callchain_param.record_mode = CALLCHAIN_DWARF;
218-
dwarf_callchain_users = true;
219-
220-
if (init_live_machine(machine)) {
221-
pr_err("Could not init machine\n");
222-
goto out;
223-
}
224-
225202
if (verbose > 1)
226203
machine__fprintf(machine, stderr);
227204

228-
thread = machine__find_thread(machine, getpid(), getpid());
205+
thread = machine__find_thread(machine, pid, pid);
229206
if (!thread) {
230207
pr_err("Could not get thread\n");
231208
goto out;

tools/perf/util/machine.c

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "path.h"
2121
#include "srcline.h"
2222
#include "symbol.h"
23+
#include "synthetic-events.h"
2324
#include "sort.h"
2425
#include "strlist.h"
2526
#include "target.h"
@@ -128,23 +129,57 @@ int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
128129
return 0;
129130
}
130131

131-
struct machine *machine__new_host(void)
132+
static struct machine *__machine__new_host(bool kernel_maps)
132133
{
133134
struct machine *machine = malloc(sizeof(*machine));
134135

135-
if (machine != NULL) {
136-
machine__init(machine, "", HOST_KERNEL_ID);
136+
if (!machine)
137+
return NULL;
137138

138-
if (machine__create_kernel_maps(machine) < 0)
139-
goto out_delete;
139+
machine__init(machine, "", HOST_KERNEL_ID);
140140

141-
machine->env = &perf_env;
141+
if (kernel_maps && machine__create_kernel_maps(machine) < 0) {
142+
free(machine);
143+
return NULL;
142144
}
145+
machine->env = &perf_env;
146+
return machine;
147+
}
148+
149+
struct machine *machine__new_host(void)
150+
{
151+
return __machine__new_host(/*kernel_maps=*/true);
152+
}
153+
154+
static int mmap_handler(const struct perf_tool *tool __maybe_unused,
155+
union perf_event *event,
156+
struct perf_sample *sample,
157+
struct machine *machine)
158+
{
159+
return machine__process_mmap2_event(machine, event, sample);
160+
}
143161

162+
static int machine__init_live(struct machine *machine, pid_t pid)
163+
{
164+
union perf_event event;
165+
166+
memset(&event, 0, sizeof(event));
167+
return perf_event__synthesize_mmap_events(NULL, &event, pid, pid,
168+
mmap_handler, machine, true);
169+
}
170+
171+
struct machine *machine__new_live(bool kernel_maps, pid_t pid)
172+
{
173+
struct machine *machine = __machine__new_host(kernel_maps);
174+
175+
if (!machine)
176+
return NULL;
177+
178+
if (machine__init_live(machine, pid)) {
179+
machine__delete(machine);
180+
return NULL;
181+
}
144182
return machine;
145-
out_delete:
146-
free(machine);
147-
return NULL;
148183
}
149184

150185
struct machine *machine__new_kallsyms(void)

tools/perf/util/machine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ void machines__set_comm_exec(struct machines *machines, bool comm_exec);
171171

172172
struct machine *machine__new_host(void);
173173
struct machine *machine__new_kallsyms(void);
174+
struct machine *machine__new_live(bool kernel_maps, pid_t pid);
174175
int machine__init(struct machine *machine, const char *root_dir, pid_t pid);
175176
void machine__exit(struct machine *machine);
176177
void machine__delete_threads(struct machine *machine);

0 commit comments

Comments
 (0)