2009-11-24 14:05:23

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: [PATCH 1/4] perf top: Always show the DSO column, even if its all the same

From: Arnaldo Carvalho de Melo <[email protected]>

Ingo found it confusing, and I agree with that, for 'perf report' its OK
because it is static, but for a tool refreshing it the eventual switch
from column to summary at the top may seem confusing.

Suggested-by: Ingo Molnar <[email protected]>
Cc: Frédéric Weisbecker <[email protected]>
Cc: Mike Galbraith <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Paul Mackerras <[email protected]>
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/builtin-top.c | 38 +++++++++++---------------------------
1 files changed, 11 insertions(+), 27 deletions(-)

diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index 6a5de90..b9a321f 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -451,9 +451,8 @@ static void print_sym_table(void)
struct sym_entry *syme, *n;
struct rb_root tmp = RB_ROOT;
struct rb_node *nd;
- int sym_width = 0, dso_width = 0;
+ int sym_width = 0, dso_width = 0, max_dso_width;
const int win_width = winsize.ws_col - 1;
- struct dso *unique_dso = NULL, *first_dso = NULL;

samples = userspace_samples = 0;

@@ -539,11 +538,6 @@ static void print_sym_table(void)
(int)syme->snap_count < count_filter)
continue;

- if (first_dso == NULL)
- unique_dso = first_dso = syme->map->dso;
- else if (syme->map->dso != first_dso)
- unique_dso = NULL;
-
if (syme->map->dso->long_name_len > dso_width)
dso_width = syme->map->dso->long_name_len;

@@ -553,14 +547,10 @@ static void print_sym_table(void)

printed = 0;

- if (unique_dso)
- printf("DSO: %s\n", unique_dso->long_name);
- else {
- int max_dso_width = winsize.ws_col - sym_width - 29;
- if (dso_width > max_dso_width)
- dso_width = max_dso_width;
- putchar('\n');
- }
+ max_dso_width = winsize.ws_col - sym_width - 29;
+ if (dso_width > max_dso_width)
+ dso_width = max_dso_width;
+ putchar('\n');
if (nr_counters == 1)
printf(" samples pcnt");
else
@@ -568,17 +558,13 @@ static void print_sym_table(void)

if (verbose)
printf(" RIP ");
- printf(" %-*.*s", sym_width, sym_width, "function");
- if (!unique_dso)
- printf(" DSO");
- putchar('\n');
+ printf(" %-*.*s DSO\n", sym_width, sym_width, "function");
printf(" %s _______ _____",
nr_counters == 1 ? " " : "______");
if (verbose)
printf(" ________________");
printf(" %-*.*s", sym_width, sym_width, graph_line);
- if (!unique_dso)
- printf(" %-*.*s", dso_width, dso_width, graph_line);
+ printf(" %-*.*s", dso_width, dso_width, graph_line);
puts("\n");

for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
@@ -603,12 +589,10 @@ static void print_sym_table(void)
if (verbose)
printf(" %016llx", sym->start);
printf(" %-*.*s", sym_width, sym_width, sym->name);
- if (!unique_dso)
- printf(" %-*.*s", dso_width, dso_width,
- dso_width >= syme->map->dso->long_name_len ?
- syme->map->dso->long_name :
- syme->map->dso->short_name);
- printf("\n");
+ printf(" %-*.*s\n", dso_width, dso_width,
+ dso_width >= syme->map->dso->long_name_len ?
+ syme->map->dso->long_name :
+ syme->map->dso->short_name);
}
}

--
1.6.2.5


2009-11-24 14:05:53

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: [PATCH 2/4] perf symbols: Symplify symbol machinery setup

From: Arnaldo Carvalho de Melo <[email protected]>

And also express its configuration toggles via a struct.

Now all one has to do is to call symbol__init(NULL) if the defaults are
OK, or pass a struct symbol_conf pointer with the desired configuration.

If a tool uses kernel_maps__find_symbol to look on the kernel and
modules mappings for a symbol but didn't call symbol__init first, that
will generate a one time warning too, alerting the subcommand developer
that symbol__init() must be called.

Cc: Frédéric Weisbecker <[email protected]>
Cc: Mike Galbraith <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Paul Mackerras <[email protected]>
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/builtin-annotate.c | 20 ++++++++++----------
tools/perf/builtin-kmem.c | 2 +-
tools/perf/builtin-report.c | 15 ++++++++-------
tools/perf/builtin-sched.c | 2 +-
tools/perf/builtin-top.c | 24 +++++++++++++-----------
tools/perf/builtin-trace.c | 2 +-
tools/perf/util/data_map.c | 8 --------
tools/perf/util/data_map.h | 2 --
tools/perf/util/header.c | 6 ------
tools/perf/util/include/asm/bug.h | 22 ++++++++++++++++++++++
tools/perf/util/symbol.c | 31 +++++++++++++++++++++----------
tools/perf/util/symbol.h | 11 ++++++++---
12 files changed, 85 insertions(+), 60 deletions(-)
create mode 100644 tools/perf/util/include/asm/bug.h

diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c
index 6b13a1e..45069a4 100644
--- a/tools/perf/builtin-annotate.c
+++ b/tools/perf/builtin-annotate.c
@@ -33,11 +33,9 @@ static int input;
static int full_paths;

static int print_line;
-static bool use_modules;

static unsigned long page_size;
static unsigned long mmap_window = 32;
-const char *vmlinux_name;

struct sym_hist {
u64 sum;
@@ -55,6 +53,11 @@ struct sym_priv {
struct sym_ext *ext;
};

+static struct symbol_conf symbol_conf = {
+ .priv_size = sizeof(struct sym_priv),
+ .try_vmlinux_path = true,
+};
+
static const char *sym_hist_filter;

static int symbol_filter(struct map *map __used, struct symbol *sym)
@@ -638,11 +641,6 @@ static int __cmd_annotate(void)
exit(0);
}

- if (kernel_maps__init(vmlinux_name, true, use_modules) < 0) {
- pr_err("failed to create kernel maps for symbol resolution\b");
- return -1;
- }
-
remap:
buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
MAP_SHARED, input, offset);
@@ -743,8 +741,9 @@ static const struct option options[] = {
"be more verbose (show symbol address, etc)"),
OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
"dump raw trace in ASCII"),
- OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
- OPT_BOOLEAN('m', "modules", &use_modules,
+ OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
+ "file", "vmlinux pathname"),
+ OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
"load module symbols - WARNING: use only with -k and LIVE kernel"),
OPT_BOOLEAN('l', "print-line", &print_line,
"print matching source lines (may be slow)"),
@@ -770,7 +769,8 @@ static void setup_sorting(void)

int cmd_annotate(int argc, const char **argv, const char *prefix __used)
{
- symbol__init(sizeof(struct sym_priv));
+ if (symbol__init(&symbol_conf) < 0)
+ return -1;

page_size = getpagesize();

diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c
index 256d18f..38b01a9 100644
--- a/tools/perf/builtin-kmem.c
+++ b/tools/perf/builtin-kmem.c
@@ -291,7 +291,7 @@ static int read_events(void)
register_idle_thread();
register_perf_file_handler(&file_handler);

- return mmap_dispatch_perf_file(&header, input_name, NULL, false, 0, 0,
+ return mmap_dispatch_perf_file(&header, input_name, 0, 0,
&cwdlen, &cwd);
}

diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c
index fe474b7..b16f86d 100644
--- a/tools/perf/builtin-report.c
+++ b/tools/perf/builtin-report.c
@@ -38,7 +38,6 @@ static char *dso_list_str, *comm_list_str, *sym_list_str,
static struct strlist *dso_list, *comm_list, *sym_list;

static int force;
-static bool use_modules;

static int full_paths;
static int show_nr_samples;
@@ -52,7 +51,6 @@ static char *pretty_printing_style = default_pretty_printing_style;
static int exclude_other = 1;

static char callchain_default_opt[] = "fractal,0.5";
-const char *vmlinux_name;

static char *cwd;
static int cwdlen;
@@ -61,6 +59,8 @@ static struct perf_header *header;

static u64 sample_type;

+struct symbol_conf symbol_conf;
+

static size_t
callchain__fprintf_left_margin(FILE *fp, int left_margin)
@@ -926,8 +926,7 @@ static int __cmd_report(void)

register_perf_file_handler(&file_handler);

- ret = mmap_dispatch_perf_file(&header, input_name, vmlinux_name,
- !vmlinux_name, force,
+ ret = mmap_dispatch_perf_file(&header, input_name, force,
full_paths, &cwdlen, &cwd);
if (ret)
return ret;
@@ -1024,9 +1023,10 @@ static const struct option options[] = {
"be more verbose (show symbol address, etc)"),
OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
"dump raw trace in ASCII"),
- OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
+ OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
+ "file", "vmlinux pathname"),
OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
- OPT_BOOLEAN('m', "modules", &use_modules,
+ OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
"load module symbols - WARNING: use only with -k and LIVE kernel"),
OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
"Show a column with the number of samples"),
@@ -1096,7 +1096,8 @@ static void setup_list(struct strlist **list, const char *list_str,

int cmd_report(int argc, const char **argv, const char *prefix __used)
{
- symbol__init(0);
+ if (symbol__init(&symbol_conf) < 0)
+ return -1;

argc = parse_options(argc, argv, options, report_usage, 0);

diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index 260f57a..dbf089b 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -1718,7 +1718,7 @@ static int read_events(void)
register_idle_thread();
register_perf_file_handler(&file_handler);

- return mmap_dispatch_perf_file(&header, input_name, NULL, false, 0, 0,
+ return mmap_dispatch_perf_file(&header, input_name, 0, 0,
&cwdlen, &cwd);
}

diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index b9a321f..a212475 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -79,7 +79,7 @@ static int dump_symtab = 0;
static bool hide_kernel_symbols = false;
static bool hide_user_symbols = false;
static struct winsize winsize;
-const char *vmlinux_name;
+struct symbol_conf symbol_conf;

/*
* Source
@@ -128,7 +128,7 @@ struct sym_entry {

static inline struct symbol *sym_entry__symbol(struct sym_entry *self)
{
- return ((void *)self) + symbol__priv_size;
+ return ((void *)self) + symbol_conf.priv_size;
}

static void get_term_dimensions(struct winsize *ws)
@@ -695,7 +695,7 @@ static void print_mapped_keys(void)

fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter);

- if (vmlinux_name) {
+ if (symbol_conf.vmlinux_name) {
fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
fprintf(stdout, "\t[S] stop annotation.\n");
@@ -732,7 +732,7 @@ static int key_mapped(int c)
case 'F':
case 's':
case 'S':
- return vmlinux_name ? 1 : 0;
+ return symbol_conf.vmlinux_name ? 1 : 0;
default:
break;
}
@@ -1261,7 +1261,8 @@ static const struct option options[] = {
"system-wide collection from all CPUs"),
OPT_INTEGER('C', "CPU", &profile_cpu,
"CPU to profile on"),
- OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
+ OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
+ "file", "vmlinux pathname"),
OPT_BOOLEAN('K', "hide_kernel_symbols", &hide_kernel_symbols,
"hide kernel symbols"),
OPT_INTEGER('m', "mmap-pages", &mmap_pages,
@@ -1295,7 +1296,7 @@ static const struct option options[] = {

int cmd_top(int argc, const char **argv, const char *prefix __used)
{
- int counter, err;
+ int counter;

page_size = sysconf(_SC_PAGE_SIZE);

@@ -1313,15 +1314,16 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
if (!nr_counters)
nr_counters = 1;

- symbol__init(sizeof(struct sym_entry) +
- (nr_counters + 1) * sizeof(unsigned long));
+ symbol_conf.priv_size = (sizeof(struct sym_entry) +
+ (nr_counters + 1) * sizeof(unsigned long));
+ if (symbol_conf.vmlinux_name == NULL)
+ symbol_conf.try_vmlinux_path = true;
+ if (symbol__init(&symbol_conf) < 0)
+ return -1;

if (delay_secs < 1)
delay_secs = 1;

- err = kernel_maps__init(vmlinux_name, !vmlinux_name, true);
- if (err < 0)
- return err;
parse_source(sym_filter_entry);

/*
diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
index b71198e..75972fd 100644
--- a/tools/perf/builtin-trace.c
+++ b/tools/perf/builtin-trace.c
@@ -131,7 +131,7 @@ static int __cmd_trace(void)
register_idle_thread();
register_perf_file_handler(&file_handler);

- return mmap_dispatch_perf_file(&header, input_name, NULL, false,
+ return mmap_dispatch_perf_file(&header, input_name,
0, 0, &cwdlen, &cwd);
}

diff --git a/tools/perf/util/data_map.c b/tools/perf/util/data_map.c
index f318d19..b238462 100644
--- a/tools/perf/util/data_map.c
+++ b/tools/perf/util/data_map.c
@@ -101,8 +101,6 @@ out:

int mmap_dispatch_perf_file(struct perf_header **pheader,
const char *input_name,
- const char *vmlinux_name,
- bool try_vmlinux_path,
int force,
int full_paths,
int *cwdlen,
@@ -172,12 +170,6 @@ int mmap_dispatch_perf_file(struct perf_header **pheader,
curr_handler->sample_type_check(sample_type) < 0)
goto out_delete;

- err = -ENOMEM;
- if (kernel_maps__init(vmlinux_name, try_vmlinux_path, true) < 0) {
- pr_err("failed to setup the kernel maps to resolve symbols\n");
- goto out_delete;
- }
-
if (!full_paths) {
if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
pr_err("failed to get the current directory\n");
diff --git a/tools/perf/util/data_map.h b/tools/perf/util/data_map.h
index 3f0d21b..ae036ec 100644
--- a/tools/perf/util/data_map.h
+++ b/tools/perf/util/data_map.h
@@ -23,8 +23,6 @@ struct perf_file_handler {
void register_perf_file_handler(struct perf_file_handler *handler);
int mmap_dispatch_perf_file(struct perf_header **pheader,
const char *input_name,
- const char *vmlinux_name,
- bool try_vmlinux_path,
int force,
int full_paths,
int *cwdlen,
diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index 1332f8e..271a160 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -253,12 +253,6 @@ static int perf_header__adds_write(struct perf_header *self, int fd)

buildid_sec = &feat_sec[idx++];

- /*
- * Read the kernel buildid nad the list of loaded modules with
- * its build_ids:
- */
- kernel_maps__init(NULL, false, true);
-
/* Write build-ids */
buildid_sec->offset = lseek(fd, 0, SEEK_CUR);
err = dsos__write_buildid_table(fd);
diff --git a/tools/perf/util/include/asm/bug.h b/tools/perf/util/include/asm/bug.h
new file mode 100644
index 0000000..7fcc681
--- /dev/null
+++ b/tools/perf/util/include/asm/bug.h
@@ -0,0 +1,22 @@
+#ifndef _PERF_ASM_GENERIC_BUG_H
+#define _PERF_ASM_GENERIC_BUG_H
+
+#define __WARN_printf(arg...) do { fprintf(stderr, arg); } while (0)
+
+#define WARN(condition, format...) ({ \
+ int __ret_warn_on = !!(condition); \
+ if (unlikely(__ret_warn_on)) \
+ __WARN_printf(format); \
+ unlikely(__ret_warn_on); \
+})
+
+#define WARN_ONCE(condition, format...) ({ \
+ static int __warned; \
+ int __ret_warn_once = !!(condition); \
+ \
+ if (unlikely(__ret_warn_once)) \
+ if (WARN(!__warned, format)) \
+ __warned = 1; \
+ unlikely(__ret_warn_once); \
+})
+#endif
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index 44d81d5..c4ca974 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -6,6 +6,7 @@

#include "debug.h"

+#include <asm/bug.h>
#include <libelf.h>
#include <gelf.h>
#include <elf.h>
@@ -37,6 +38,11 @@ unsigned int symbol__priv_size;
static int vmlinux_path__nr_entries;
static char **vmlinux_path;

+static struct symbol_conf symbol_conf__defaults = {
+ .use_modules = true,
+ .try_vmlinux_path = true,
+};
+
static struct rb_root kernel_maps;

static void dso__fixup_sym_end(struct dso *self)
@@ -1166,7 +1172,9 @@ struct symbol *kernel_maps__find_symbol(u64 ip, struct map **mapp,
if (map) {
ip = map->map_ip(map, ip);
return map__find_symbol(map, ip, filter);
- }
+ } else
+ WARN_ONCE(RB_EMPTY_ROOT(&kernel_maps),
+ "Empty kernel_maps, was symbol__init() called?\n");

return NULL;
}
@@ -1485,9 +1493,9 @@ size_t dsos__fprintf_buildid(FILE *fp)
return ret;
}

-static int kernel_maps__create_kernel_map(const char *vmlinux_name)
+static int kernel_maps__create_kernel_map(const struct symbol_conf *conf)
{
- struct dso *kernel = dso__new(vmlinux_name ?: "[kernel.kallsyms]");
+ struct dso *kernel = dso__new(conf->vmlinux_name ?: "[kernel.kallsyms]");

if (kernel == NULL)
return -1;
@@ -1577,18 +1585,21 @@ out_fail:
return -1;
}

-int kernel_maps__init(const char *vmlinux_name, bool try_vmlinux_path,
- bool use_modules)
+static int kernel_maps__init(const struct symbol_conf *conf)
{
- if (try_vmlinux_path && vmlinux_path__init() < 0)
+ const struct symbol_conf *pconf = conf ?: &symbol_conf__defaults;
+
+ symbol__priv_size = pconf->priv_size;
+
+ if (pconf->try_vmlinux_path && vmlinux_path__init() < 0)
return -1;

- if (kernel_maps__create_kernel_map(vmlinux_name) < 0) {
+ if (kernel_maps__create_kernel_map(pconf) < 0) {
vmlinux_path__exit();
return -1;
}

- if (use_modules && kernel_maps__create_module_maps() < 0)
+ if (pconf->use_modules && kernel_maps__create_module_maps() < 0)
pr_debug("Failed to load list of modules in use, "
"continuing...\n");
/*
@@ -1598,8 +1609,8 @@ int kernel_maps__init(const char *vmlinux_name, bool try_vmlinux_path,
return 0;
}

-void symbol__init(unsigned int priv_size)
+int symbol__init(struct symbol_conf *conf)
{
elf_version(EV_CURRENT);
- symbol__priv_size = priv_size;
+ return kernel_maps__init(conf);
}
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 8c4d026..5538691 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -49,6 +49,13 @@ struct symbol {
char name[0];
};

+struct symbol_conf {
+ unsigned short priv_size;
+ bool try_vmlinux_path,
+ use_modules;
+ const char *vmlinux_name;
+};
+
extern unsigned int symbol__priv_size;

static inline void *symbol__priv(struct symbol *self)
@@ -93,11 +100,9 @@ int sysfs__read_build_id(const char *filename, void *bf, size_t size);
bool dsos__read_build_ids(void);
int build_id__sprintf(u8 *self, int len, char *bf);

-int kernel_maps__init(const char *vmlinux_name, bool try_vmlinux_path,
- bool use_modules);
size_t kernel_maps__fprintf(FILE *fp);

-void symbol__init(unsigned int priv_size);
+int symbol__init(struct symbol_conf *conf);

extern struct list_head dsos;
extern struct map *kernel_map;
--
1.6.2.5

2009-11-24 14:05:25

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: [PATCH 3/4] perf tools: Introduce zalloc for the common calloc(1, N) case

From: Arnaldo Carvalho de Melo <[email protected]>

This way we type less characters and it looks more like the kzalloc
kernel counterpart.

Cc: Frédéric Weisbecker <[email protected]>
Cc: Mike Galbraith <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Paul Mackerras <[email protected]>
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/bench/mem-memcpy.c | 4 ++--
tools/perf/builtin-help.c | 4 ++--
tools/perf/builtin-probe.c | 4 ++--
tools/perf/builtin-sched.c | 14 +++++---------
tools/perf/builtin-top.c | 2 +-
tools/perf/util/header.c | 2 +-
tools/perf/util/parse-events.c | 2 +-
tools/perf/util/symbol.c | 11 +++++------
tools/perf/util/thread.c | 2 +-
tools/perf/util/util.h | 5 +++++
10 files changed, 25 insertions(+), 25 deletions(-)

diff --git a/tools/perf/bench/mem-memcpy.c b/tools/perf/bench/mem-memcpy.c
index 5165fd1..8977317 100644
--- a/tools/perf/bench/mem-memcpy.c
+++ b/tools/perf/bench/mem-memcpy.c
@@ -127,11 +127,11 @@ int bench_mem_memcpy(int argc, const char **argv,
return 1;
}

- dst = calloc(length, sizeof(char));
+ dst = zalloc(length);
if (!dst)
die("memory allocation failed - maybe length is too large?\n");

- src = calloc(length, sizeof(char));
+ src = zalloc(length);
if (!src)
die("memory allocation failed - maybe length is too large?\n");

diff --git a/tools/perf/builtin-help.c b/tools/perf/builtin-help.c
index 768f9c8..9f810b1 100644
--- a/tools/perf/builtin-help.c
+++ b/tools/perf/builtin-help.c
@@ -179,7 +179,7 @@ static void add_man_viewer(const char *name)

while (*p)
p = &((*p)->next);
- *p = calloc(1, (sizeof(**p) + len + 1));
+ *p = zalloc(sizeof(**p) + len + 1);
strncpy((*p)->name, name, len);
}

@@ -194,7 +194,7 @@ static void do_add_man_viewer_info(const char *name,
size_t len,
const char *value)
{
- struct man_viewer_info_list *new = calloc(1, sizeof(*new) + len + 1);
+ struct man_viewer_info_list *new = zalloc(sizeof(*new) + len + 1);

strncpy(new->name, name, len);
new->info = strdup(value);
diff --git a/tools/perf/builtin-probe.c b/tools/perf/builtin-probe.c
index d78a3d9..a2f6daf 100644
--- a/tools/perf/builtin-probe.c
+++ b/tools/perf/builtin-probe.c
@@ -309,9 +309,9 @@ static int synthesize_probe_event(struct probe_point *pp)
{
char *buf;
int i, len, ret;
- pp->probes[0] = buf = (char *)calloc(MAX_CMDLEN, sizeof(char));
+ pp->probes[0] = buf = zalloc(MAX_CMDLEN);
if (!buf)
- die("Failed to allocate memory by calloc.");
+ die("Failed to allocate memory by zalloc.");
ret = snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
if (ret <= 0 || ret >= MAX_CMDLEN)
goto error;
diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index dbf089b..19eb708 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -225,7 +225,7 @@ static void calibrate_sleep_measurement_overhead(void)
static struct sched_atom *
get_new_event(struct task_desc *task, u64 timestamp)
{
- struct sched_atom *event = calloc(1, sizeof(*event));
+ struct sched_atom *event = zalloc(sizeof(*event));
unsigned long idx = task->nr_events;
size_t size;

@@ -293,7 +293,7 @@ add_sched_event_wakeup(struct task_desc *task, u64 timestamp,
return;
}

- wakee_event->wait_sem = calloc(1, sizeof(*wakee_event->wait_sem));
+ wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
sem_init(wakee_event->wait_sem, 0, 0);
wakee_event->specific_wait = 1;
event->wait_sem = wakee_event->wait_sem;
@@ -323,7 +323,7 @@ static struct task_desc *register_pid(unsigned long pid, const char *comm)
if (task)
return task;

- task = calloc(1, sizeof(*task));
+ task = zalloc(sizeof(*task));
task->pid = pid;
task->nr = nr_tasks;
strcpy(task->comm, comm);
@@ -962,9 +962,7 @@ __thread_latency_insert(struct rb_root *root, struct work_atoms *data,

static void thread_atoms_insert(struct thread *thread)
{
- struct work_atoms *atoms;
-
- atoms = calloc(sizeof(*atoms), 1);
+ struct work_atoms *atoms = zalloc(sizeof(*atoms));
if (!atoms)
die("No memory");

@@ -996,9 +994,7 @@ add_sched_out_event(struct work_atoms *atoms,
char run_state,
u64 timestamp)
{
- struct work_atom *atom;
-
- atom = calloc(sizeof(*atom), 1);
+ struct work_atom *atom = zalloc(sizeof(*atom));
if (!atom)
die("Non memory");

diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index a212475..4c8653a 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -181,7 +181,7 @@ static void parse_source(struct sym_entry *syme)
return;

if (syme->src == NULL) {
- syme->src = calloc(1, sizeof(*source));
+ syme->src = zalloc(sizeof(*source));
if (syme->src == NULL)
return;
pthread_mutex_init(&syme->src->lock, NULL);
diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index 271a160..4b58656 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -63,7 +63,7 @@ int perf_header_attr__add_id(struct perf_header_attr *self, u64 id)
*/
struct perf_header *perf_header__new(void)
{
- struct perf_header *self = calloc(sizeof(*self), 1);
+ struct perf_header *self = zalloc(sizeof(*self));

if (self != NULL) {
self->size = 1;
diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index 0700274..9e5dbd6 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -197,7 +197,7 @@ struct tracepoint_path *tracepoint_id_to_path(u64 config)
if (id == config) {
closedir(evt_dir);
closedir(sys_dir);
- path = calloc(1, sizeof(path));
+ path = zalloc(sizeof(path));
path->system = malloc(MAX_EVENT_LENGTH);
if (!path->system) {
free(path);
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index c4ca974..8db85b4 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -94,15 +94,14 @@ static void kernel_maps__fixup_end(void)
static struct symbol *symbol__new(u64 start, u64 len, const char *name)
{
size_t namelen = strlen(name) + 1;
- struct symbol *self = calloc(1, (symbol__priv_size +
- sizeof(*self) + namelen));
- if (!self)
+ struct symbol *self = zalloc(symbol__priv_size +
+ sizeof(*self) + namelen);
+ if (self == NULL)
return NULL;

- if (symbol__priv_size) {
- memset(self, 0, symbol__priv_size);
+ if (symbol__priv_size)
self = ((void *)self) + symbol__priv_size;
- }
+
self->start = start;
self->end = len ? start + len - 1 : start;

diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index 0f6d78c..1796625 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -11,7 +11,7 @@ static struct thread *last_match;

static struct thread *thread__new(pid_t pid)
{
- struct thread *self = calloc(1, sizeof(*self));
+ struct thread *self = zalloc(sizeof(*self));

if (self != NULL) {
self->pid = pid;
diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
index e1c623e..30c5517 100644
--- a/tools/perf/util/util.h
+++ b/tools/perf/util/util.h
@@ -302,6 +302,11 @@ extern int xdup(int fd);
extern FILE *xfdopen(int fd, const char *mode);
extern int xmkstemp(char *template);

+static inline void *zalloc(size_t size)
+{
+ return calloc(1, size);
+}
+
static inline size_t xsize_t(off_t len)
{
return (size_t)len;
--
1.6.2.5

2009-11-24 14:05:31

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: [PATCH 4/4] perf tools: Remove unused wrapper routines

From: Arnaldo Carvalho de Melo <[email protected]>

And also make xrealloc and xmalloc weak symbols so that we don't have
this problem:

/usr/lib/gcc/x86_64-redhat-linux/4.4.1/../../../../lib64/libiberty.a(xmalloc.o):
In function `xrealloc':
(.text+0xc0): multiple definition of `xrealloc'
libperf.a(wrapper.o):/home/acme_unencrypted/git/linux-2.6-tip/tools/perf/util/wrapper.c:67:
first defined here
collect2: ld returned 1 exit status

Cc: Frédéric Weisbecker <[email protected]>
Cc: Mike Galbraith <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Paul Mackerras <[email protected]>
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/util/util.h | 11 +------
tools/perf/util/wrapper.c | 61 +-------------------------------------------
2 files changed, 4 insertions(+), 68 deletions(-)

diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
index 30c5517..c673d88 100644
--- a/tools/perf/util/util.h
+++ b/tools/perf/util/util.h
@@ -290,17 +290,10 @@ static inline char *gitstrchrnul(const char *s, int c)
* Wrappers:
*/
extern char *xstrdup(const char *str);
-extern void *xmalloc(size_t size);
+extern void *xmalloc(size_t size) __attribute__((weak));
extern void *xmemdupz(const void *data, size_t len);
extern char *xstrndup(const char *str, size_t len);
-extern void *xrealloc(void *ptr, size_t size);
-extern void *xcalloc(size_t nmemb, size_t size);
-extern void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
-extern ssize_t xread(int fd, void *buf, size_t len);
-extern ssize_t xwrite(int fd, const void *buf, size_t len);
-extern int xdup(int fd);
-extern FILE *xfdopen(int fd, const char *mode);
-extern int xmkstemp(char *template);
+extern void *xrealloc(void *ptr, size_t size) __attribute__((weak));

static inline void *zalloc(size_t size)
{
diff --git a/tools/perf/util/wrapper.c b/tools/perf/util/wrapper.c
index 4574ac2..bf44ca8 100644
--- a/tools/perf/util/wrapper.c
+++ b/tools/perf/util/wrapper.c
@@ -79,43 +79,12 @@ void *xrealloc(void *ptr, size_t size)
return ret;
}

-void *xcalloc(size_t nmemb, size_t size)
-{
- void *ret = calloc(nmemb, size);
- if (!ret && (!nmemb || !size))
- ret = calloc(1, 1);
- if (!ret) {
- release_pack_memory(nmemb * size, -1);
- ret = calloc(nmemb, size);
- if (!ret && (!nmemb || !size))
- ret = calloc(1, 1);
- if (!ret)
- die("Out of memory, calloc failed");
- }
- return ret;
-}
-
-void *xmmap(void *start, size_t length,
- int prot, int flags, int fd, off_t offset)
-{
- void *ret = mmap(start, length, prot, flags, fd, offset);
- if (ret == MAP_FAILED) {
- if (!length)
- return NULL;
- release_pack_memory(length, fd);
- ret = mmap(start, length, prot, flags, fd, offset);
- if (ret == MAP_FAILED)
- die("Out of memory? mmap failed: %s", strerror(errno));
- }
- return ret;
-}
-
/*
* xread() is the same a read(), but it automatically restarts read()
* operations with a recoverable error (EAGAIN and EINTR). xread()
* DOES NOT GUARANTEE that "len" bytes is read even if the data is available.
*/
-ssize_t xread(int fd, void *buf, size_t len)
+static ssize_t xread(int fd, void *buf, size_t len)
{
ssize_t nr;
while (1) {
@@ -131,7 +100,7 @@ ssize_t xread(int fd, void *buf, size_t len)
* operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT
* GUARANTEE that "len" bytes is written even if the operation is successful.
*/
-ssize_t xwrite(int fd, const void *buf, size_t len)
+static ssize_t xwrite(int fd, const void *buf, size_t len)
{
ssize_t nr;
while (1) {
@@ -179,29 +148,3 @@ ssize_t write_in_full(int fd, const void *buf, size_t count)

return total;
}
-
-int xdup(int fd)
-{
- int ret = dup(fd);
- if (ret < 0)
- die("dup failed: %s", strerror(errno));
- return ret;
-}
-
-FILE *xfdopen(int fd, const char *mode)
-{
- FILE *stream = fdopen(fd, mode);
- if (stream == NULL)
- die("Out of memory? fdopen failed: %s", strerror(errno));
- return stream;
-}
-
-int xmkstemp(char *template)
-{
- int fd;
-
- fd = mkstemp(template);
- if (fd < 0)
- die("Unable to create temporary file: %s", strerror(errno));
- return fd;
-}
--
1.6.2.5