Skip to content

Commit

Permalink
perf machine: Move more methods to machine.[ch]
Browse files Browse the repository at this point in the history
This time out of map.[ch] mostly, just code move plus a buch of 'self'
removal, using machine or machines instead.

Cc: David Ahern <dsahern@gmail.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Namhyung Kim <namhyung@gmail.com>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/n/tip-j1vtux3vnu6wzmrjutpxnjcz@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
  • Loading branch information
Arnaldo Carvalho de Melo committed Nov 9, 2012
1 parent bfaef4b commit 69d2591
Show file tree
Hide file tree
Showing 10 changed files with 318 additions and 297 deletions.
1 change: 1 addition & 0 deletions tools/perf/tests/builtin-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "util/debug.h"
#include "util/debugfs.h"
#include "util/evlist.h"
#include "util/machine.h"
#include "util/parse-options.h"
#include "util/parse-events.h"
#include "util/symbol.h"
Expand Down
1 change: 1 addition & 0 deletions tools/perf/tests/dso-data.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <fcntl.h>
#include <string.h>

#include "machine.h"
#include "symbol.h"

#define TEST_ASSERT_VAL(text, cond) \
Expand Down
1 change: 1 addition & 0 deletions tools/perf/util/dso.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "symbol.h"
#include "dso.h"
#include "machine.h"
#include "util.h"
#include "debug.h"

Expand Down
183 changes: 183 additions & 0 deletions tools/perf/util/machine.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,192 @@
#include "event.h"
#include "machine.h"
#include "map.h"
#include "strlist.h"
#include "thread.h"
#include <stdbool.h>

int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
{
map_groups__init(&machine->kmaps);
RB_CLEAR_NODE(&machine->rb_node);
INIT_LIST_HEAD(&machine->user_dsos);
INIT_LIST_HEAD(&machine->kernel_dsos);

machine->threads = RB_ROOT;
INIT_LIST_HEAD(&machine->dead_threads);
machine->last_match = NULL;

machine->kmaps.machine = machine;
machine->pid = pid;

machine->root_dir = strdup(root_dir);
if (machine->root_dir == NULL)
return -ENOMEM;

if (pid != HOST_KERNEL_ID) {
struct thread *thread = machine__findnew_thread(machine, pid);
char comm[64];

if (thread == NULL)
return -ENOMEM;

snprintf(comm, sizeof(comm), "[guest/%d]", pid);
thread__set_comm(thread, comm);
}

return 0;
}

static void dsos__delete(struct list_head *dsos)
{
struct dso *pos, *n;

list_for_each_entry_safe(pos, n, dsos, node) {
list_del(&pos->node);
dso__delete(pos);
}
}

void machine__exit(struct machine *machine)
{
map_groups__exit(&machine->kmaps);
dsos__delete(&machine->user_dsos);
dsos__delete(&machine->kernel_dsos);
free(machine->root_dir);
machine->root_dir = NULL;
}

void machine__delete(struct machine *machine)
{
machine__exit(machine);
free(machine);
}

struct machine *machines__add(struct rb_root *machines, pid_t pid,
const char *root_dir)
{
struct rb_node **p = &machines->rb_node;
struct rb_node *parent = NULL;
struct machine *pos, *machine = malloc(sizeof(*machine));

if (machine == NULL)
return NULL;

if (machine__init(machine, root_dir, pid) != 0) {
free(machine);
return NULL;
}

while (*p != NULL) {
parent = *p;
pos = rb_entry(parent, struct machine, rb_node);
if (pid < pos->pid)
p = &(*p)->rb_left;
else
p = &(*p)->rb_right;
}

rb_link_node(&machine->rb_node, parent, p);
rb_insert_color(&machine->rb_node, machines);

return machine;
}

struct machine *machines__find(struct rb_root *machines, pid_t pid)
{
struct rb_node **p = &machines->rb_node;
struct rb_node *parent = NULL;
struct machine *machine;
struct machine *default_machine = NULL;

while (*p != NULL) {
parent = *p;
machine = rb_entry(parent, struct machine, rb_node);
if (pid < machine->pid)
p = &(*p)->rb_left;
else if (pid > machine->pid)
p = &(*p)->rb_right;
else
return machine;
if (!machine->pid)
default_machine = machine;
}

return default_machine;
}

struct machine *machines__findnew(struct rb_root *machines, pid_t pid)
{
char path[PATH_MAX];
const char *root_dir = "";
struct machine *machine = machines__find(machines, pid);

if (machine && (machine->pid == pid))
goto out;

if ((pid != HOST_KERNEL_ID) &&
(pid != DEFAULT_GUEST_KERNEL_ID) &&
(symbol_conf.guestmount)) {
sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
if (access(path, R_OK)) {
static struct strlist *seen;

if (!seen)
seen = strlist__new(true, NULL);

if (!strlist__has_entry(seen, path)) {
pr_err("Can't access file %s\n", path);
strlist__add(seen, path);
}
machine = NULL;
goto out;
}
root_dir = path;
}

machine = machines__add(machines, pid, root_dir);
out:
return machine;
}

void machines__process(struct rb_root *machines,
machine__process_t process, void *data)
{
struct rb_node *nd;

for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
struct machine *pos = rb_entry(nd, struct machine, rb_node);
process(pos, data);
}
}

char *machine__mmap_name(struct machine *machine, char *bf, size_t size)
{
if (machine__is_host(machine))
snprintf(bf, size, "[%s]", "kernel.kallsyms");
else if (machine__is_default_guest(machine))
snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
else {
snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms",
machine->pid);
}

return bf;
}

void machines__set_id_hdr_size(struct rb_root *machines, u16 id_hdr_size)
{
struct rb_node *node;
struct machine *machine;

for (node = rb_first(machines); node; node = rb_next(node)) {
machine = rb_entry(node, struct machine, rb_node);
machine->id_hdr_size = id_hdr_size;
}

return;
}

static struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid,
bool create)
{
Expand Down
131 changes: 130 additions & 1 deletion tools/perf/util/machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,40 @@
#define __PERF_MACHINE_H

#include <sys/types.h>
#include <linux/rbtree.h>
#include "map.h"

struct branch_stack;
struct perf_evsel;
struct perf_sample;
struct symbol;
struct thread;
struct machine;
union perf_event;

/* Native host kernel uses -1 as pid index in machine */
#define HOST_KERNEL_ID (-1)
#define DEFAULT_GUEST_KERNEL_ID (0)

struct machine {
struct rb_node rb_node;
pid_t pid;
u16 id_hdr_size;
char *root_dir;
struct rb_root threads;
struct list_head dead_threads;
struct thread *last_match;
struct list_head user_dsos;
struct list_head kernel_dsos;
struct map_groups kmaps;
struct map *vmlinux_maps[MAP__NR_TYPES];
};

static inline
struct map *machine__kernel_map(struct machine *machine, enum map_type type)
{
return machine->vmlinux_maps[type];
}

struct thread *machine__find_thread(struct machine *machine, pid_t pid);

int machine__process_comm_event(struct machine *machine, union perf_event *event);
Expand All @@ -16,4 +45,104 @@ int machine__process_lost_event(struct machine *machine, union perf_event *event
int machine__process_mmap_event(struct machine *machine, union perf_event *event);
int machine__process_event(struct machine *machine, union perf_event *event);

typedef void (*machine__process_t)(struct machine *machine, void *data);

void machines__process(struct rb_root *machines,
machine__process_t process, void *data);

struct machine *machines__add(struct rb_root *machines, pid_t pid,
const char *root_dir);
struct machine *machines__find_host(struct rb_root *machines);
struct machine *machines__find(struct rb_root *machines, pid_t pid);
struct machine *machines__findnew(struct rb_root *machines, pid_t pid);

void machines__set_id_hdr_size(struct rb_root *machines, u16 id_hdr_size);
char *machine__mmap_name(struct machine *machine, char *bf, size_t size);

int machine__init(struct machine *machine, const char *root_dir, pid_t pid);
void machine__exit(struct machine *machine);
void machine__delete(struct machine *machine);


struct branch_info *machine__resolve_bstack(struct machine *machine,
struct thread *thread,
struct branch_stack *bs);
int machine__resolve_callchain(struct machine *machine,
struct perf_evsel *evsel,
struct thread *thread,
struct perf_sample *sample,
struct symbol **parent);

/*
* Default guest kernel is defined by parameter --guestkallsyms
* and --guestmodules
*/
static inline bool machine__is_default_guest(struct machine *machine)
{
return machine ? machine->pid == DEFAULT_GUEST_KERNEL_ID : false;
}

static inline bool machine__is_host(struct machine *machine)
{
return machine ? machine->pid == HOST_KERNEL_ID : false;
}

struct thread *machine__findnew_thread(struct machine *machine, pid_t pid);
void machine__remove_thread(struct machine *machine, struct thread *th);

size_t machine__fprintf(struct machine *machine, FILE *fp);

static inline
struct symbol *machine__find_kernel_symbol(struct machine *machine,
enum map_type type, u64 addr,
struct map **mapp,
symbol_filter_t filter)
{
return map_groups__find_symbol(&machine->kmaps, type, addr,
mapp, filter);
}

static inline
struct symbol *machine__find_kernel_function(struct machine *machine, u64 addr,
struct map **mapp,
symbol_filter_t filter)
{
return machine__find_kernel_symbol(machine, MAP__FUNCTION, addr,
mapp, filter);
}

static inline
struct symbol *machine__find_kernel_function_by_name(struct machine *machine,
const char *name,
struct map **mapp,
symbol_filter_t filter)
{
return map_groups__find_function_by_name(&machine->kmaps, name, mapp,
filter);
}

struct map *machine__new_module(struct machine *machine, u64 start,
const char *filename);

int machine__load_kallsyms(struct machine *machine, const char *filename,
enum map_type type, symbol_filter_t filter);
int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
symbol_filter_t filter);

size_t machine__fprintf_dsos_buildid(struct machine *machine,
FILE *fp, bool with_hits);
size_t machines__fprintf_dsos(struct rb_root *machines, FILE *fp);
size_t machines__fprintf_dsos_buildid(struct rb_root *machines,
FILE *fp, bool with_hits);

void machine__destroy_kernel_maps(struct machine *machine);
int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel);
int machine__create_kernel_maps(struct machine *machine);

int machines__create_kernel_maps(struct rb_root *machines, pid_t pid);
int machines__create_guest_kernel_maps(struct rb_root *machines);
void machines__destroy_guest_kernel_maps(struct rb_root *machines);

size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp);

#endif /* __PERF_MACHINE_H */
Loading

0 comments on commit 69d2591

Please sign in to comment.