aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tools')
-rw-r--r--tools/perf/CREDITS30
-rw-r--r--tools/perf/Documentation/perf-report.txt14
-rw-r--r--tools/perf/Documentation/perf-stat.txt6
-rw-r--r--tools/perf/Makefile26
-rw-r--r--tools/perf/builtin-annotate.c77
-rw-r--r--tools/perf/builtin-help.c6
-rw-r--r--tools/perf/builtin-list.c2
-rw-r--r--tools/perf/builtin-record.c131
-rw-r--r--tools/perf/builtin-report.c595
-rw-r--r--tools/perf/builtin-stat.c216
-rw-r--r--tools/perf/builtin-top.c81
-rw-r--r--tools/perf/perf.c5
-rw-r--r--tools/perf/perf.h27
-rw-r--r--tools/perf/util/alias.c2
-rw-r--r--tools/perf/util/cache.h1
-rw-r--r--tools/perf/util/callchain.c335
-rw-r--r--tools/perf/util/callchain.h54
-rw-r--r--tools/perf/util/color.c37
-rw-r--r--tools/perf/util/color.h5
-rw-r--r--tools/perf/util/config.c18
-rw-r--r--tools/perf/util/exec_cmd.c5
-rw-r--r--tools/perf/util/header.c242
-rw-r--r--tools/perf/util/header.h37
-rw-r--r--tools/perf/util/help.c41
-rw-r--r--tools/perf/util/help.h6
-rw-r--r--tools/perf/util/include/asm/system.h1
-rw-r--r--tools/perf/util/include/linux/kernel.h21
-rw-r--r--tools/perf/util/include/linux/list.h18
-rw-r--r--tools/perf/util/include/linux/module.h6
-rw-r--r--tools/perf/util/include/linux/poison.h1
-rw-r--r--tools/perf/util/include/linux/prefetch.h6
-rw-r--r--tools/perf/util/include/linux/rbtree.h1
-rw-r--r--tools/perf/util/list.h603
-rw-r--r--tools/perf/util/module.c509
-rw-r--r--tools/perf/util/module.h53
-rw-r--r--tools/perf/util/pager.c5
-rw-r--r--tools/perf/util/parse-events.c378
-rw-r--r--tools/perf/util/parse-options.c5
-rw-r--r--tools/perf/util/parse-options.h27
-rw-r--r--tools/perf/util/quote.c46
-rw-r--r--tools/perf/util/quote.h2
-rw-r--r--tools/perf/util/rbtree.c383
-rw-r--r--tools/perf/util/rbtree.h171
-rw-r--r--tools/perf/util/run-command.c95
-rw-r--r--tools/perf/util/run-command.h5
-rw-r--r--tools/perf/util/strbuf.c15
-rw-r--r--tools/perf/util/strbuf.h10
-rw-r--r--tools/perf/util/string.h2
-rw-r--r--tools/perf/util/strlist.c184
-rw-r--r--tools/perf/util/strlist.h32
-rw-r--r--tools/perf/util/symbol.c183
-rw-r--r--tools/perf/util/symbol.h14
-rw-r--r--tools/perf/util/types.h (renamed from tools/perf/types.h)0
-rw-r--r--tools/perf/util/util.h15
-rw-r--r--tools/perf/util/wrapper.c5
55 files changed, 2954 insertions, 1841 deletions
diff --git a/tools/perf/CREDITS b/tools/perf/CREDITS
new file mode 100644
index 000000000000..c2ddcb3acbd0
--- /dev/null
+++ b/tools/perf/CREDITS
@@ -0,0 +1,30 @@
1Most of the infrastructure that 'perf' uses here has been reused
2from the Git project, as of version:
3
4 66996ec: Sync with 1.6.2.4
5
6Here is an (incomplete!) list of main contributors to those files
7in util/* and elsewhere:
8
9 Alex Riesen
10 Christian Couder
11 Dmitry Potapov
12 Jeff King
13 Johannes Schindelin
14 Johannes Sixt
15 Junio C Hamano
16 Linus Torvalds
17 Matthias Kestenholz
18 Michal Ostrowski
19 Miklos Vajna
20 Petr Baudis
21 Pierre Habouzit
22 René Scharfe
23 Samuel Tardieu
24 Shawn O. Pearce
25 Steffen Prohaska
26 Steve Haslam
27
28Thanks guys!
29
30The full history of the files can be found in the upstream Git commits.
diff --git a/tools/perf/Documentation/perf-report.txt b/tools/perf/Documentation/perf-report.txt
index 52d3fc6846a9..8aa3f8c88707 100644
--- a/tools/perf/Documentation/perf-report.txt
+++ b/tools/perf/Documentation/perf-report.txt
@@ -13,13 +13,25 @@ SYNOPSIS
13DESCRIPTION 13DESCRIPTION
14----------- 14-----------
15This command displays the performance counter profile information recorded 15This command displays the performance counter profile information recorded
16via perf report. 16via perf record.
17 17
18OPTIONS 18OPTIONS
19------- 19-------
20-i:: 20-i::
21--input=:: 21--input=::
22 Input file name. (default: perf.data) 22 Input file name. (default: perf.data)
23-d::
24--dsos=::
25 Only consider symbols in these dsos. CSV that understands
26 file://filename entries.
27-C::
28--comms=::
29 Only consider symbols in these comms. CSV that understands
30 file://filename entries.
31-S::
32--symbols=::
33 Only consider these symbols. CSV that understands
34 file://filename entries.
23 35
24SEE ALSO 36SEE ALSO
25-------- 37--------
diff --git a/tools/perf/Documentation/perf-stat.txt b/tools/perf/Documentation/perf-stat.txt
index c368a72721d7..0d74346d21ab 100644
--- a/tools/perf/Documentation/perf-stat.txt
+++ b/tools/perf/Documentation/perf-stat.txt
@@ -8,8 +8,8 @@ perf-stat - Run a command and gather performance counter statistics
8SYNOPSIS 8SYNOPSIS
9-------- 9--------
10[verse] 10[verse]
11'perf stat' [-e <EVENT> | --event=EVENT] [-l] [-a] <command> 11'perf stat' [-e <EVENT> | --event=EVENT] [-S] [-a] <command>
12'perf stat' [-e <EVENT> | --event=EVENT] [-l] [-a] -- <command> [<options>] 12'perf stat' [-e <EVENT> | --event=EVENT] [-S] [-a] -- <command> [<options>]
13 13
14DESCRIPTION 14DESCRIPTION
15----------- 15-----------
@@ -40,7 +40,7 @@ OPTIONS
40-a:: 40-a::
41 system-wide collection 41 system-wide collection
42 42
43-l:: 43-S::
44 scale counter values 44 scale counter values
45 45
46EXAMPLES 46EXAMPLES
diff --git a/tools/perf/Makefile b/tools/perf/Makefile
index 36d7eef49913..7822b3d6baca 100644
--- a/tools/perf/Makefile
+++ b/tools/perf/Makefile
@@ -164,7 +164,7 @@ endif
164 164
165# CFLAGS and LDFLAGS are for the users to override from the command line. 165# CFLAGS and LDFLAGS are for the users to override from the command line.
166 166
167CFLAGS = $(M64) -ggdb3 -Wall -Wstrict-prototypes -Wmissing-declarations -Wmissing-prototypes -std=gnu99 -Wdeclaration-after-statement -Werror -O6 167CFLAGS = $(M64) -ggdb3 -Wall -Wextra -Wstrict-prototypes -Wmissing-declarations -Wmissing-prototypes -std=gnu99 -Wdeclaration-after-statement -Werror -O6
168LDFLAGS = -lpthread -lrt -lelf -lm 168LDFLAGS = -lpthread -lrt -lelf -lm
169ALL_CFLAGS = $(CFLAGS) 169ALL_CFLAGS = $(CFLAGS)
170ALL_LDFLAGS = $(LDFLAGS) 170ALL_LDFLAGS = $(LDFLAGS)
@@ -223,7 +223,7 @@ SPARSE_FLAGS = -D__BIG_ENDIAN__ -D__powerpc__
223# Those must not be GNU-specific; they are shared with perl/ which may 223# Those must not be GNU-specific; they are shared with perl/ which may
224# be built by a different compiler. (Note that this is an artifact now 224# be built by a different compiler. (Note that this is an artifact now
225# but it still might be nice to keep that distinction.) 225# but it still might be nice to keep that distinction.)
226BASIC_CFLAGS = 226BASIC_CFLAGS = -Iutil/include
227BASIC_LDFLAGS = 227BASIC_LDFLAGS =
228 228
229# Guard against environment variables 229# Guard against environment variables
@@ -289,10 +289,11 @@ export PERL_PATH
289LIB_FILE=libperf.a 289LIB_FILE=libperf.a
290 290
291LIB_H += ../../include/linux/perf_counter.h 291LIB_H += ../../include/linux/perf_counter.h
292LIB_H += ../../include/linux/rbtree.h
293LIB_H += ../../include/linux/list.h
294LIB_H += util/include/linux/list.h
292LIB_H += perf.h 295LIB_H += perf.h
293LIB_H += types.h 296LIB_H += util/types.h
294LIB_H += util/list.h
295LIB_H += util/rbtree.h
296LIB_H += util/levenshtein.h 297LIB_H += util/levenshtein.h
297LIB_H += util/parse-options.h 298LIB_H += util/parse-options.h
298LIB_H += util/parse-events.h 299LIB_H += util/parse-events.h
@@ -301,9 +302,11 @@ LIB_H += util/util.h
301LIB_H += util/help.h 302LIB_H += util/help.h
302LIB_H += util/strbuf.h 303LIB_H += util/strbuf.h
303LIB_H += util/string.h 304LIB_H += util/string.h
305LIB_H += util/strlist.h
304LIB_H += util/run-command.h 306LIB_H += util/run-command.h
305LIB_H += util/sigchain.h 307LIB_H += util/sigchain.h
306LIB_H += util/symbol.h 308LIB_H += util/symbol.h
309LIB_H += util/module.h
307LIB_H += util/color.h 310LIB_H += util/color.h
308 311
309LIB_OBJS += util/abspath.o 312LIB_OBJS += util/abspath.o
@@ -322,12 +325,16 @@ LIB_OBJS += util/run-command.o
322LIB_OBJS += util/quote.o 325LIB_OBJS += util/quote.o
323LIB_OBJS += util/strbuf.o 326LIB_OBJS += util/strbuf.o
324LIB_OBJS += util/string.o 327LIB_OBJS += util/string.o
328LIB_OBJS += util/strlist.o
325LIB_OBJS += util/usage.o 329LIB_OBJS += util/usage.o
326LIB_OBJS += util/wrapper.o 330LIB_OBJS += util/wrapper.o
327LIB_OBJS += util/sigchain.o 331LIB_OBJS += util/sigchain.o
328LIB_OBJS += util/symbol.o 332LIB_OBJS += util/symbol.o
333LIB_OBJS += util/module.o
329LIB_OBJS += util/color.o 334LIB_OBJS += util/color.o
330LIB_OBJS += util/pager.o 335LIB_OBJS += util/pager.o
336LIB_OBJS += util/header.o
337LIB_OBJS += util/callchain.o
331 338
332BUILTIN_OBJS += builtin-annotate.o 339BUILTIN_OBJS += builtin-annotate.o
333BUILTIN_OBJS += builtin-help.o 340BUILTIN_OBJS += builtin-help.o
@@ -377,12 +384,6 @@ ifndef CC_LD_DYNPATH
377 endif 384 endif
378endif 385endif
379 386
380ifdef ZLIB_PATH
381 BASIC_CFLAGS += -I$(ZLIB_PATH)/include
382 EXTLIBS += -L$(ZLIB_PATH)/$(lib) $(CC_LD_DYNPATH)$(ZLIB_PATH)/$(lib)
383endif
384EXTLIBS += -lz
385
386ifdef NEEDS_SOCKET 387ifdef NEEDS_SOCKET
387 EXTLIBS += -lsocket 388 EXTLIBS += -lsocket
388endif 389endif
@@ -693,6 +694,9 @@ builtin-init-db.o: builtin-init-db.c PERF-CFLAGS
693util/config.o: util/config.c PERF-CFLAGS 694util/config.o: util/config.c PERF-CFLAGS
694 $(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $< 695 $(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $<
695 696
697util/rbtree.o: ../../lib/rbtree.c PERF-CFLAGS
698 $(QUIET_CC)$(CC) -o util/rbtree.o -c $(ALL_CFLAGS) -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $<
699
696perf-%$X: %.o $(PERFLIBS) 700perf-%$X: %.o $(PERFLIBS)
697 $(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS) 701 $(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS)
698 702
diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c
index 7e58e3ad1508..5f9eefecc574 100644
--- a/tools/perf/builtin-annotate.c
+++ b/tools/perf/builtin-annotate.c
@@ -10,9 +10,9 @@
10#include "util/util.h" 10#include "util/util.h"
11 11
12#include "util/color.h" 12#include "util/color.h"
13#include "util/list.h" 13#include <linux/list.h>
14#include "util/cache.h" 14#include "util/cache.h"
15#include "util/rbtree.h" 15#include <linux/rbtree.h>
16#include "util/symbol.h" 16#include "util/symbol.h"
17#include "util/string.h" 17#include "util/string.h"
18 18
@@ -25,10 +25,6 @@
25#define SHOW_USER 2 25#define SHOW_USER 2
26#define SHOW_HV 4 26#define SHOW_HV 4
27 27
28#define MIN_GREEN 0.5
29#define MIN_RED 5.0
30
31
32static char const *input_name = "perf.data"; 28static char const *input_name = "perf.data";
33static char *vmlinux = "vmlinux"; 29static char *vmlinux = "vmlinux";
34 30
@@ -43,6 +39,10 @@ static int dump_trace = 0;
43 39
44static int verbose; 40static int verbose;
45 41
42static int modules;
43
44static int full_paths;
45
46static int print_line; 46static int print_line;
47 47
48static unsigned long page_size; 48static unsigned long page_size;
@@ -160,7 +160,7 @@ static void dsos__fprintf(FILE *fp)
160 160
161static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip) 161static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
162{ 162{
163 return dso__find_symbol(kernel_dso, ip); 163 return dso__find_symbol(dso, ip);
164} 164}
165 165
166static int load_kernel(void) 166static int load_kernel(void)
@@ -171,8 +171,8 @@ static int load_kernel(void)
171 if (!kernel_dso) 171 if (!kernel_dso)
172 return -1; 172 return -1;
173 173
174 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose); 174 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose, modules);
175 if (err) { 175 if (err <= 0) {
176 dso__delete(kernel_dso); 176 dso__delete(kernel_dso);
177 kernel_dso = NULL; 177 kernel_dso = NULL;
178 } else 178 } else
@@ -203,7 +203,7 @@ static u64 map__map_ip(struct map *map, u64 ip)
203 return ip - map->start + map->pgoff; 203 return ip - map->start + map->pgoff;
204} 204}
205 205
206static u64 vdso__map_ip(struct map *map, u64 ip) 206static u64 vdso__map_ip(struct map *map __used, u64 ip)
207{ 207{
208 return ip; 208 return ip;
209} 209}
@@ -600,7 +600,7 @@ static LIST_HEAD(hist_entry__sort_list);
600 600
601static int sort_dimension__add(char *tok) 601static int sort_dimension__add(char *tok)
602{ 602{
603 int i; 603 unsigned int i;
604 604
605 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) { 605 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
606 struct sort_dimension *sd = &sort_dimensions[i]; 606 struct sort_dimension *sd = &sort_dimensions[i];
@@ -855,7 +855,7 @@ static unsigned long total = 0,
855 total_unknown = 0; 855 total_unknown = 0;
856 856
857static int 857static int
858process_overflow_event(event_t *event, unsigned long offset, unsigned long head) 858process_sample_event(event_t *event, unsigned long offset, unsigned long head)
859{ 859{
860 char level; 860 char level;
861 int show = 0; 861 int show = 0;
@@ -1013,10 +1013,10 @@ process_period_event(event_t *event, unsigned long offset, unsigned long head)
1013static int 1013static int
1014process_event(event_t *event, unsigned long offset, unsigned long head) 1014process_event(event_t *event, unsigned long offset, unsigned long head)
1015{ 1015{
1016 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
1017 return process_overflow_event(event, offset, head);
1018
1019 switch (event->header.type) { 1016 switch (event->header.type) {
1017 case PERF_EVENT_SAMPLE:
1018 return process_sample_event(event, offset, head);
1019
1020 case PERF_EVENT_MMAP: 1020 case PERF_EVENT_MMAP:
1021 return process_mmap_event(event, offset, head); 1021 return process_mmap_event(event, offset, head);
1022 1022
@@ -1043,24 +1043,6 @@ process_event(event_t *event, unsigned long offset, unsigned long head)
1043 return 0; 1043 return 0;
1044} 1044}
1045 1045
1046static char *get_color(double percent)
1047{
1048 char *color = PERF_COLOR_NORMAL;
1049
1050 /*
1051 * We color high-overhead entries in red, mid-overhead
1052 * entries in green - and keep the low overhead places
1053 * normal:
1054 */
1055 if (percent >= MIN_RED)
1056 color = PERF_COLOR_RED;
1057 else {
1058 if (percent > MIN_GREEN)
1059 color = PERF_COLOR_GREEN;
1060 }
1061 return color;
1062}
1063
1064static int 1046static int
1065parse_line(FILE *file, struct symbol *sym, u64 start, u64 len) 1047parse_line(FILE *file, struct symbol *sym, u64 start, u64 len)
1066{ 1048{
@@ -1069,7 +1051,7 @@ parse_line(FILE *file, struct symbol *sym, u64 start, u64 len)
1069 static const char *prev_color; 1051 static const char *prev_color;
1070 unsigned int offset; 1052 unsigned int offset;
1071 size_t line_len; 1053 size_t line_len;
1072 u64 line_ip; 1054 s64 line_ip;
1073 int ret; 1055 int ret;
1074 char *c; 1056 char *c;
1075 1057
@@ -1122,7 +1104,7 @@ parse_line(FILE *file, struct symbol *sym, u64 start, u64 len)
1122 } else if (sym->hist_sum) 1104 } else if (sym->hist_sum)
1123 percent = 100.0 * hits / sym->hist_sum; 1105 percent = 100.0 * hits / sym->hist_sum;
1124 1106
1125 color = get_color(percent); 1107 color = get_percent_color(percent);
1126 1108
1127 /* 1109 /*
1128 * Also color the filename and line if needed, with 1110 * Also color the filename and line if needed, with
@@ -1258,7 +1240,7 @@ static void print_summary(char *filename)
1258 1240
1259 sym_ext = rb_entry(node, struct sym_ext, node); 1241 sym_ext = rb_entry(node, struct sym_ext, node);
1260 percent = sym_ext->percent; 1242 percent = sym_ext->percent;
1261 color = get_color(percent); 1243 color = get_percent_color(percent);
1262 path = sym_ext->path; 1244 path = sym_ext->path;
1263 1245
1264 color_fprintf(stdout, color, " %7.2f %s", percent, path); 1246 color_fprintf(stdout, color, " %7.2f %s", percent, path);
@@ -1268,19 +1250,25 @@ static void print_summary(char *filename)
1268 1250
1269static void annotate_sym(struct dso *dso, struct symbol *sym) 1251static void annotate_sym(struct dso *dso, struct symbol *sym)
1270{ 1252{
1271 char *filename = dso->name; 1253 char *filename = dso->name, *d_filename;
1272 u64 start, end, len; 1254 u64 start, end, len;
1273 char command[PATH_MAX*2]; 1255 char command[PATH_MAX*2];
1274 FILE *file; 1256 FILE *file;
1275 1257
1276 if (!filename) 1258 if (!filename)
1277 return; 1259 return;
1278 if (dso == kernel_dso) 1260 if (sym->module)
1261 filename = sym->module->path;
1262 else if (dso == kernel_dso)
1279 filename = vmlinux; 1263 filename = vmlinux;
1280 1264
1281 start = sym->obj_start; 1265 start = sym->obj_start;
1282 if (!start) 1266 if (!start)
1283 start = sym->start; 1267 start = sym->start;
1268 if (full_paths)
1269 d_filename = filename;
1270 else
1271 d_filename = basename(filename);
1284 1272
1285 end = start + sym->end - sym->start + 1; 1273 end = start + sym->end - sym->start + 1;
1286 len = sym->end - sym->start; 1274 len = sym->end - sym->start;
@@ -1291,13 +1279,14 @@ static void annotate_sym(struct dso *dso, struct symbol *sym)
1291 } 1279 }
1292 1280
1293 printf("\n\n------------------------------------------------\n"); 1281 printf("\n\n------------------------------------------------\n");
1294 printf(" Percent | Source code & Disassembly of %s\n", filename); 1282 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
1295 printf("------------------------------------------------\n"); 1283 printf("------------------------------------------------\n");
1296 1284
1297 if (verbose >= 2) 1285 if (verbose >= 2)
1298 printf("annotating [%p] %30s : [%p] %30s\n", dso, dso->name, sym, sym->name); 1286 printf("annotating [%p] %30s : [%p] %30s\n", dso, dso->name, sym, sym->name);
1299 1287
1300 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s", (u64)start, (u64)end, filename); 1288 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
1289 (u64)start, (u64)end, filename, filename);
1301 1290
1302 if (verbose >= 3) 1291 if (verbose >= 3)
1303 printf("doing: %s\n", command); 1292 printf("doing: %s\n", command);
@@ -1428,7 +1417,7 @@ more:
1428 1417
1429 head += size; 1418 head += size;
1430 1419
1431 if (offset + head < stat.st_size) 1420 if (offset + head < (unsigned long)stat.st_size)
1432 goto more; 1421 goto more;
1433 1422
1434 rc = EXIT_SUCCESS; 1423 rc = EXIT_SUCCESS;
@@ -1472,8 +1461,12 @@ static const struct option options[] = {
1472 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, 1461 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1473 "dump raw trace in ASCII"), 1462 "dump raw trace in ASCII"),
1474 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"), 1463 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1464 OPT_BOOLEAN('m', "modules", &modules,
1465 "load module symbols - WARNING: use only with -k and LIVE kernel"),
1475 OPT_BOOLEAN('l', "print-line", &print_line, 1466 OPT_BOOLEAN('l', "print-line", &print_line,
1476 "print matching source lines (may be slow)"), 1467 "print matching source lines (may be slow)"),
1468 OPT_BOOLEAN('P', "full-paths", &full_paths,
1469 "Don't shorten the displayed pathnames"),
1477 OPT_END() 1470 OPT_END()
1478}; 1471};
1479 1472
@@ -1492,7 +1485,7 @@ static void setup_sorting(void)
1492 free(str); 1485 free(str);
1493} 1486}
1494 1487
1495int cmd_annotate(int argc, const char **argv, const char *prefix) 1488int cmd_annotate(int argc, const char **argv, const char *prefix __used)
1496{ 1489{
1497 symbol__init(); 1490 symbol__init();
1498 1491
diff --git a/tools/perf/builtin-help.c b/tools/perf/builtin-help.c
index 0f32dc3f3c4c..2599d86a733b 100644
--- a/tools/perf/builtin-help.c
+++ b/tools/perf/builtin-help.c
@@ -3,6 +3,7 @@
3 * 3 *
4 * Builtin help command 4 * Builtin help command
5 */ 5 */
6#include "perf.h"
6#include "util/cache.h" 7#include "util/cache.h"
7#include "builtin.h" 8#include "builtin.h"
8#include "util/exec_cmd.h" 9#include "util/exec_cmd.h"
@@ -277,7 +278,7 @@ static struct cmdnames main_cmds, other_cmds;
277 278
278void list_common_cmds_help(void) 279void list_common_cmds_help(void)
279{ 280{
280 int i, longest = 0; 281 unsigned int i, longest = 0;
281 282
282 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) { 283 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
283 if (longest < strlen(common_cmds[i].name)) 284 if (longest < strlen(common_cmds[i].name))
@@ -415,9 +416,10 @@ static void show_html_page(const char *perf_cmd)
415 open_html(page_path.buf); 416 open_html(page_path.buf);
416} 417}
417 418
418int cmd_help(int argc, const char **argv, const char *prefix) 419int cmd_help(int argc, const char **argv, const char *prefix __used)
419{ 420{
420 const char *alias; 421 const char *alias;
422
421 load_command_list("perf-", &main_cmds, &other_cmds); 423 load_command_list("perf-", &main_cmds, &other_cmds);
422 424
423 perf_config(perf_help_config, NULL); 425 perf_config(perf_help_config, NULL);
diff --git a/tools/perf/builtin-list.c b/tools/perf/builtin-list.c
index fe60e37c96ef..f990fa8a35c9 100644
--- a/tools/perf/builtin-list.c
+++ b/tools/perf/builtin-list.c
@@ -13,7 +13,7 @@
13#include "util/parse-options.h" 13#include "util/parse-options.h"
14#include "util/parse-events.h" 14#include "util/parse-events.h"
15 15
16int cmd_list(int argc, const char **argv, const char *prefix) 16int cmd_list(int argc __used, const char **argv __used, const char *prefix __used)
17{ 17{
18 print_events(); 18 print_events();
19 return 0; 19 return 0;
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index d7ebbd757543..4ef78a5e6f32 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -14,6 +14,8 @@
14#include "util/parse-events.h" 14#include "util/parse-events.h"
15#include "util/string.h" 15#include "util/string.h"
16 16
17#include "util/header.h"
18
17#include <unistd.h> 19#include <unistd.h>
18#include <sched.h> 20#include <sched.h>
19 21
@@ -39,6 +41,8 @@ static int force = 0;
39static int append_file = 0; 41static int append_file = 0;
40static int call_graph = 0; 42static int call_graph = 0;
41static int verbose = 0; 43static int verbose = 0;
44static int inherit_stat = 0;
45static int no_samples = 0;
42 46
43static long samples; 47static long samples;
44static struct timeval last_read; 48static struct timeval last_read;
@@ -52,7 +56,8 @@ static int nr_poll;
52static int nr_cpu; 56static int nr_cpu;
53 57
54static int file_new = 1; 58static int file_new = 1;
55static struct perf_file_header file_header; 59
60struct perf_header *header;
56 61
57struct mmap_event { 62struct mmap_event {
58 struct perf_event_header header; 63 struct perf_event_header header;
@@ -289,7 +294,7 @@ static void pid_synthesize_mmap_samples(pid_t pid)
289 while (1) { 294 while (1) {
290 char bf[BUFSIZ], *pbf = bf; 295 char bf[BUFSIZ], *pbf = bf;
291 struct mmap_event mmap_ev = { 296 struct mmap_event mmap_ev = {
292 .header.type = PERF_EVENT_MMAP, 297 .header = { .type = PERF_EVENT_MMAP },
293 }; 298 };
294 int n; 299 int n;
295 size_t size; 300 size_t size;
@@ -306,12 +311,11 @@ static void pid_synthesize_mmap_samples(pid_t pid)
306 continue; 311 continue;
307 pbf += n + 3; 312 pbf += n + 3;
308 if (*pbf == 'x') { /* vm_exec */ 313 if (*pbf == 'x') { /* vm_exec */
309 char *execname = strrchr(bf, ' '); 314 char *execname = strchr(bf, '/');
310 315
311 if (execname == NULL || execname[1] != '/') 316 if (execname == NULL)
312 continue; 317 continue;
313 318
314 execname += 1;
315 size = strlen(execname); 319 size = strlen(execname);
316 execname[size - 1] = '\0'; /* Remove \n */ 320 execname[size - 1] = '\0'; /* Remove \n */
317 memcpy(mmap_ev.filename, execname, size); 321 memcpy(mmap_ev.filename, execname, size);
@@ -329,7 +333,7 @@ static void pid_synthesize_mmap_samples(pid_t pid)
329 fclose(fp); 333 fclose(fp);
330} 334}
331 335
332static void synthesize_samples(void) 336static void synthesize_all(void)
333{ 337{
334 DIR *proc; 338 DIR *proc;
335 struct dirent dirent, *next; 339 struct dirent dirent, *next;
@@ -353,10 +357,35 @@ static void synthesize_samples(void)
353 357
354static int group_fd; 358static int group_fd;
355 359
360static struct perf_header_attr *get_header_attr(struct perf_counter_attr *a, int nr)
361{
362 struct perf_header_attr *h_attr;
363
364 if (nr < header->attrs) {
365 h_attr = header->attr[nr];
366 } else {
367 h_attr = perf_header_attr__new(a);
368 perf_header__add_attr(header, h_attr);
369 }
370
371 return h_attr;
372}
373
356static void create_counter(int counter, int cpu, pid_t pid) 374static void create_counter(int counter, int cpu, pid_t pid)
357{ 375{
358 struct perf_counter_attr *attr = attrs + counter; 376 struct perf_counter_attr *attr = attrs + counter;
359 int track = 1; 377 struct perf_header_attr *h_attr;
378 int track = !counter; /* only the first counter needs these */
379 struct {
380 u64 count;
381 u64 time_enabled;
382 u64 time_running;
383 u64 id;
384 } read_data;
385
386 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
387 PERF_FORMAT_TOTAL_TIME_RUNNING |
388 PERF_FORMAT_ID;
360 389
361 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID; 390 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
362 391
@@ -366,25 +395,20 @@ static void create_counter(int counter, int cpu, pid_t pid)
366 attr->sample_freq = freq; 395 attr->sample_freq = freq;
367 } 396 }
368 397
398 if (no_samples)
399 attr->sample_freq = 0;
400
401 if (inherit_stat)
402 attr->inherit_stat = 1;
403
369 if (call_graph) 404 if (call_graph)
370 attr->sample_type |= PERF_SAMPLE_CALLCHAIN; 405 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
371 406
372 if (file_new) {
373 file_header.sample_type = attr->sample_type;
374 } else {
375 if (file_header.sample_type != attr->sample_type) {
376 fprintf(stderr, "incompatible append\n");
377 exit(-1);
378 }
379 }
380
381 attr->mmap = track; 407 attr->mmap = track;
382 attr->comm = track; 408 attr->comm = track;
383 attr->inherit = (cpu < 0) && inherit; 409 attr->inherit = (cpu < 0) && inherit;
384 attr->disabled = 1; 410 attr->disabled = 1;
385 411
386 track = 0; /* only the first counter needs these */
387
388try_again: 412try_again:
389 fd[nr_cpu][counter] = sys_perf_counter_open(attr, pid, cpu, group_fd, 0); 413 fd[nr_cpu][counter] = sys_perf_counter_open(attr, pid, cpu, group_fd, 0);
390 414
@@ -415,6 +439,22 @@ try_again:
415 exit(-1); 439 exit(-1);
416 } 440 }
417 441
442 h_attr = get_header_attr(attr, counter);
443
444 if (!file_new) {
445 if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
446 fprintf(stderr, "incompatible append\n");
447 exit(-1);
448 }
449 }
450
451 if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) {
452 perror("Unable to read perf file descriptor\n");
453 exit(-1);
454 }
455
456 perf_header_attr__add_id(h_attr, read_data.id);
457
418 assert(fd[nr_cpu][counter] >= 0); 458 assert(fd[nr_cpu][counter] >= 0);
419 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK); 459 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
420 460
@@ -445,11 +485,6 @@ static void open_counters(int cpu, pid_t pid)
445{ 485{
446 int counter; 486 int counter;
447 487
448 if (pid > 0) {
449 pid_synthesize_comm_event(pid, 0);
450 pid_synthesize_mmap_samples(pid);
451 }
452
453 group_fd = -1; 488 group_fd = -1;
454 for (counter = 0; counter < nr_counters; counter++) 489 for (counter = 0; counter < nr_counters; counter++)
455 create_counter(counter, cpu, pid); 490 create_counter(counter, cpu, pid);
@@ -459,17 +494,16 @@ static void open_counters(int cpu, pid_t pid)
459 494
460static void atexit_header(void) 495static void atexit_header(void)
461{ 496{
462 file_header.data_size += bytes_written; 497 header->data_size += bytes_written;
463 498
464 if (pwrite(output, &file_header, sizeof(file_header), 0) == -1) 499 perf_header__write(header, output);
465 perror("failed to write on file headers");
466} 500}
467 501
468static int __cmd_record(int argc, const char **argv) 502static int __cmd_record(int argc, const char **argv)
469{ 503{
470 int i, counter; 504 int i, counter;
471 struct stat st; 505 struct stat st;
472 pid_t pid; 506 pid_t pid = 0;
473 int flags; 507 int flags;
474 int ret; 508 int ret;
475 509
@@ -500,22 +534,31 @@ static int __cmd_record(int argc, const char **argv)
500 exit(-1); 534 exit(-1);
501 } 535 }
502 536
503 if (!file_new) { 537 if (!file_new)
504 if (read(output, &file_header, sizeof(file_header)) == -1) { 538 header = perf_header__read(output);
505 perror("failed to read file headers"); 539 else
506 exit(-1); 540 header = perf_header__new();
507 }
508
509 lseek(output, file_header.data_size, SEEK_CUR);
510 }
511 541
512 atexit(atexit_header); 542 atexit(atexit_header);
513 543
514 if (!system_wide) { 544 if (!system_wide) {
515 open_counters(-1, target_pid != -1 ? target_pid : getpid()); 545 pid = target_pid;
546 if (pid == -1)
547 pid = getpid();
548
549 open_counters(-1, pid);
516 } else for (i = 0; i < nr_cpus; i++) 550 } else for (i = 0; i < nr_cpus; i++)
517 open_counters(i, target_pid); 551 open_counters(i, target_pid);
518 552
553 if (file_new)
554 perf_header__write(header, output);
555
556 if (!system_wide) {
557 pid_synthesize_comm_event(pid, 0);
558 pid_synthesize_mmap_samples(pid);
559 } else
560 synthesize_all();
561
519 if (target_pid == -1 && argc) { 562 if (target_pid == -1 && argc) {
520 pid = fork(); 563 pid = fork();
521 if (pid < 0) 564 if (pid < 0)
@@ -539,10 +582,7 @@ static int __cmd_record(int argc, const char **argv)
539 } 582 }
540 } 583 }
541 584
542 if (system_wide) 585 for (;;) {
543 synthesize_samples();
544
545 while (!done) {
546 int hits = samples; 586 int hits = samples;
547 587
548 for (i = 0; i < nr_cpu; i++) { 588 for (i = 0; i < nr_cpu; i++) {
@@ -550,8 +590,11 @@ static int __cmd_record(int argc, const char **argv)
550 mmap_read(&mmap_array[i][counter]); 590 mmap_read(&mmap_array[i][counter]);
551 } 591 }
552 592
553 if (hits == samples) 593 if (hits == samples) {
594 if (done)
595 break;
554 ret = poll(event_array, nr_poll, 100); 596 ret = poll(event_array, nr_poll, 100);
597 }
555 } 598 }
556 599
557 /* 600 /*
@@ -600,10 +643,14 @@ static const struct option options[] = {
600 "do call-graph (stack chain/backtrace) recording"), 643 "do call-graph (stack chain/backtrace) recording"),
601 OPT_BOOLEAN('v', "verbose", &verbose, 644 OPT_BOOLEAN('v', "verbose", &verbose,
602 "be more verbose (show counter open errors, etc)"), 645 "be more verbose (show counter open errors, etc)"),
646 OPT_BOOLEAN('s', "stat", &inherit_stat,
647 "per thread counts"),
648 OPT_BOOLEAN('n', "no-samples", &no_samples,
649 "don't sample"),
603 OPT_END() 650 OPT_END()
604}; 651};
605 652
606int cmd_record(int argc, const char **argv, const char *prefix) 653int cmd_record(int argc, const char **argv, const char *prefix __used)
607{ 654{
608 int counter; 655 int counter;
609 656
diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c
index 5eb5566f0c95..4e5cc266311e 100644
--- a/tools/perf/builtin-report.c
+++ b/tools/perf/builtin-report.c
@@ -10,13 +10,16 @@
10#include "util/util.h" 10#include "util/util.h"
11 11
12#include "util/color.h" 12#include "util/color.h"
13#include "util/list.h" 13#include <linux/list.h>
14#include "util/cache.h" 14#include "util/cache.h"
15#include "util/rbtree.h" 15#include <linux/rbtree.h>
16#include "util/symbol.h" 16#include "util/symbol.h"
17#include "util/string.h" 17#include "util/string.h"
18#include "util/callchain.h"
19#include "util/strlist.h"
18 20
19#include "perf.h" 21#include "perf.h"
22#include "util/header.h"
20 23
21#include "util/parse-options.h" 24#include "util/parse-options.h"
22#include "util/parse-events.h" 25#include "util/parse-events.h"
@@ -30,6 +33,8 @@ static char *vmlinux = NULL;
30 33
31static char default_sort_order[] = "comm,dso"; 34static char default_sort_order[] = "comm,dso";
32static char *sort_order = default_sort_order; 35static char *sort_order = default_sort_order;
36static char *dso_list_str, *comm_list_str, *sym_list_str;
37static struct strlist *dso_list, *comm_list, *sym_list;
33 38
34static int input; 39static int input;
35static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV; 40static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
@@ -41,6 +46,8 @@ static int dump_trace = 0;
41static int verbose; 46static int verbose;
42#define eprintf(x...) do { if (verbose) fprintf(stderr, x); } while (0) 47#define eprintf(x...) do { if (verbose) fprintf(stderr, x); } while (0)
43 48
49static int modules;
50
44static int full_paths; 51static int full_paths;
45 52
46static unsigned long page_size; 53static unsigned long page_size;
@@ -52,6 +59,18 @@ static regex_t parent_regex;
52 59
53static int exclude_other = 1; 60static int exclude_other = 1;
54 61
62static char callchain_default_opt[] = "fractal,0.5";
63
64static int callchain;
65
66static
67struct callchain_param callchain_param = {
68 .mode = CHAIN_GRAPH_ABS,
69 .min_percent = 0.5
70};
71
72static u64 sample_type;
73
55struct ip_event { 74struct ip_event {
56 struct perf_event_header header; 75 struct perf_event_header header;
57 u64 ip; 76 u64 ip;
@@ -59,11 +78,6 @@ struct ip_event {
59 unsigned char __more_data[]; 78 unsigned char __more_data[];
60}; 79};
61 80
62struct ip_callchain {
63 u64 nr;
64 u64 ips[0];
65};
66
67struct mmap_event { 81struct mmap_event {
68 struct perf_event_header header; 82 struct perf_event_header header;
69 u32 pid, tid; 83 u32 pid, tid;
@@ -97,6 +111,13 @@ struct lost_event {
97 u64 lost; 111 u64 lost;
98}; 112};
99 113
114struct read_event {
115 struct perf_event_header header;
116 u32 pid,tid;
117 u64 value;
118 u64 format[3];
119};
120
100typedef union event_union { 121typedef union event_union {
101 struct perf_event_header header; 122 struct perf_event_header header;
102 struct ip_event ip; 123 struct ip_event ip;
@@ -105,11 +126,13 @@ typedef union event_union {
105 struct fork_event fork; 126 struct fork_event fork;
106 struct period_event period; 127 struct period_event period;
107 struct lost_event lost; 128 struct lost_event lost;
129 struct read_event read;
108} event_t; 130} event_t;
109 131
110static LIST_HEAD(dsos); 132static LIST_HEAD(dsos);
111static struct dso *kernel_dso; 133static struct dso *kernel_dso;
112static struct dso *vdso; 134static struct dso *vdso;
135static struct dso *hypervisor_dso;
113 136
114static void dsos__add(struct dso *dso) 137static void dsos__add(struct dso *dso)
115{ 138{
@@ -165,7 +188,7 @@ static void dsos__fprintf(FILE *fp)
165 188
166static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip) 189static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
167{ 190{
168 return dso__find_symbol(kernel_dso, ip); 191 return dso__find_symbol(dso, ip);
169} 192}
170 193
171static int load_kernel(void) 194static int load_kernel(void)
@@ -176,8 +199,8 @@ static int load_kernel(void)
176 if (!kernel_dso) 199 if (!kernel_dso)
177 return -1; 200 return -1;
178 201
179 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose); 202 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose, modules);
180 if (err) { 203 if (err <= 0) {
181 dso__delete(kernel_dso); 204 dso__delete(kernel_dso);
182 kernel_dso = NULL; 205 kernel_dso = NULL;
183 } else 206 } else
@@ -191,6 +214,11 @@ static int load_kernel(void)
191 214
192 dsos__add(vdso); 215 dsos__add(vdso);
193 216
217 hypervisor_dso = dso__new("[hypervisor]", 0);
218 if (!hypervisor_dso)
219 return -1;
220 dsos__add(hypervisor_dso);
221
194 return err; 222 return err;
195} 223}
196 224
@@ -222,14 +250,14 @@ static u64 map__map_ip(struct map *map, u64 ip)
222 return ip - map->start + map->pgoff; 250 return ip - map->start + map->pgoff;
223} 251}
224 252
225static u64 vdso__map_ip(struct map *map, u64 ip) 253static u64 vdso__map_ip(struct map *map __used, u64 ip)
226{ 254{
227 return ip; 255 return ip;
228} 256}
229 257
230static inline int is_anon_memory(const char *filename) 258static inline int is_anon_memory(const char *filename)
231{ 259{
232 return strcmp(filename, "//anon") == 0; 260 return strcmp(filename, "//anon") == 0;
233} 261}
234 262
235static struct map *map__new(struct mmap_event *event) 263static struct map *map__new(struct mmap_event *event)
@@ -400,9 +428,27 @@ static void thread__insert_map(struct thread *self, struct map *map)
400 428
401 list_for_each_entry_safe(pos, tmp, &self->maps, node) { 429 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
402 if (map__overlap(pos, map)) { 430 if (map__overlap(pos, map)) {
403 list_del_init(&pos->node); 431 if (verbose >= 2) {
404 /* XXX leaks dsos */ 432 printf("overlapping maps:\n");
405 free(pos); 433 map__fprintf(map, stdout);
434 map__fprintf(pos, stdout);
435 }
436
437 if (map->start <= pos->start && map->end > pos->start)
438 pos->start = map->end;
439
440 if (map->end >= pos->end && map->start < pos->end)
441 pos->end = map->start;
442
443 if (verbose >= 2) {
444 printf("after collision:\n");
445 map__fprintf(pos, stdout);
446 }
447
448 if (pos->start >= pos->end) {
449 list_del_init(&pos->node);
450 free(pos);
451 }
406 } 452 }
407 } 453 }
408 454
@@ -464,17 +510,19 @@ static size_t threads__fprintf(FILE *fp)
464static struct rb_root hist; 510static struct rb_root hist;
465 511
466struct hist_entry { 512struct hist_entry {
467 struct rb_node rb_node; 513 struct rb_node rb_node;
468 514
469 struct thread *thread; 515 struct thread *thread;
470 struct map *map; 516 struct map *map;
471 struct dso *dso; 517 struct dso *dso;
472 struct symbol *sym; 518 struct symbol *sym;
473 struct symbol *parent; 519 struct symbol *parent;
474 u64 ip; 520 u64 ip;
475 char level; 521 char level;
476 522 struct callchain_node callchain;
477 u64 count; 523 struct rb_root sorted_chain;
524
525 u64 count;
478}; 526};
479 527
480/* 528/*
@@ -609,7 +657,11 @@ sort__sym_print(FILE *fp, struct hist_entry *self)
609 657
610 if (self->sym) { 658 if (self->sym) {
611 ret += fprintf(fp, "[%c] %s", 659 ret += fprintf(fp, "[%c] %s",
612 self->dso == kernel_dso ? 'k' : '.', self->sym->name); 660 self->dso == kernel_dso ? 'k' :
661 self->dso == hypervisor_dso ? 'h' : '.', self->sym->name);
662
663 if (self->sym->module)
664 ret += fprintf(fp, "\t[%s]", self->sym->module->name);
613 } else { 665 } else {
614 ret += fprintf(fp, "%#016llx", (u64)self->ip); 666 ret += fprintf(fp, "%#016llx", (u64)self->ip);
615 } 667 }
@@ -674,7 +726,7 @@ static LIST_HEAD(hist_entry__sort_list);
674 726
675static int sort_dimension__add(char *tok) 727static int sort_dimension__add(char *tok)
676{ 728{
677 int i; 729 unsigned int i;
678 730
679 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) { 731 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
680 struct sort_dimension *sd = &sort_dimensions[i]; 732 struct sort_dimension *sd = &sort_dimensions[i];
@@ -744,34 +796,180 @@ hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
744 return cmp; 796 return cmp;
745} 797}
746 798
799static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask)
800{
801 int i;
802 size_t ret = 0;
803
804 ret += fprintf(fp, "%s", " ");
805
806 for (i = 0; i < depth; i++)
807 if (depth_mask & (1 << i))
808 ret += fprintf(fp, "| ");
809 else
810 ret += fprintf(fp, " ");
811
812 ret += fprintf(fp, "\n");
813
814 return ret;
815}
747static size_t 816static size_t
748hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples) 817ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
818 int depth_mask, int count, u64 total_samples,
819 int hits)
749{ 820{
750 struct sort_entry *se; 821 int i;
751 size_t ret; 822 size_t ret = 0;
752 823
753 if (exclude_other && !self->parent) 824 ret += fprintf(fp, "%s", " ");
754 return 0; 825 for (i = 0; i < depth; i++) {
826 if (depth_mask & (1 << i))
827 ret += fprintf(fp, "|");
828 else
829 ret += fprintf(fp, " ");
830 if (!count && i == depth - 1) {
831 double percent;
832
833 percent = hits * 100.0 / total_samples;
834 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
835 } else
836 ret += fprintf(fp, "%s", " ");
837 }
838 if (chain->sym)
839 ret += fprintf(fp, "%s\n", chain->sym->name);
840 else
841 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
842
843 return ret;
844}
845
846static size_t
847callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
848 u64 total_samples, int depth, int depth_mask)
849{
850 struct rb_node *node, *next;
851 struct callchain_node *child;
852 struct callchain_list *chain;
853 int new_depth_mask = depth_mask;
854 u64 new_total;
855 size_t ret = 0;
856 int i;
755 857
756 if (total_samples) { 858 if (callchain_param.mode == CHAIN_GRAPH_REL)
757 double percent = self->count * 100.0 / total_samples; 859 new_total = self->cumul_hit;
758 char *color = PERF_COLOR_NORMAL; 860 else
861 new_total = total_samples;
862
863 node = rb_first(&self->rb_root);
864 while (node) {
865 child = rb_entry(node, struct callchain_node, rb_node);
759 866
760 /* 867 /*
761 * We color high-overhead entries in red, mid-overhead 868 * The depth mask manages the output of pipes that show
762 * entries in green - and keep the low overhead places 869 * the depth. We don't want to keep the pipes of the current
763 * normal: 870 * level for the last child of this depth
764 */ 871 */
765 if (percent >= 5.0) { 872 next = rb_next(node);
766 color = PERF_COLOR_RED; 873 if (!next)
767 } else { 874 new_depth_mask &= ~(1 << (depth - 1));
768 if (percent >= 0.5) 875
769 color = PERF_COLOR_GREEN; 876 /*
877 * But we keep the older depth mask for the line seperator
878 * to keep the level link until we reach the last child
879 */
880 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask);
881 i = 0;
882 list_for_each_entry(chain, &child->val, list) {
883 if (chain->ip >= PERF_CONTEXT_MAX)
884 continue;
885 ret += ipchain__fprintf_graph(fp, chain, depth,
886 new_depth_mask, i++,
887 new_total,
888 child->cumul_hit);
889 }
890 ret += callchain__fprintf_graph(fp, child, new_total,
891 depth + 1,
892 new_depth_mask | (1 << depth));
893 node = next;
894 }
895
896 return ret;
897}
898
899static size_t
900callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
901 u64 total_samples)
902{
903 struct callchain_list *chain;
904 size_t ret = 0;
905
906 if (!self)
907 return 0;
908
909 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
910
911
912 list_for_each_entry(chain, &self->val, list) {
913 if (chain->ip >= PERF_CONTEXT_MAX)
914 continue;
915 if (chain->sym)
916 ret += fprintf(fp, " %s\n", chain->sym->name);
917 else
918 ret += fprintf(fp, " %p\n",
919 (void *)(long)chain->ip);
920 }
921
922 return ret;
923}
924
925static size_t
926hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
927 u64 total_samples)
928{
929 struct rb_node *rb_node;
930 struct callchain_node *chain;
931 size_t ret = 0;
932
933 rb_node = rb_first(&self->sorted_chain);
934 while (rb_node) {
935 double percent;
936
937 chain = rb_entry(rb_node, struct callchain_node, rb_node);
938 percent = chain->hit * 100.0 / total_samples;
939 switch (callchain_param.mode) {
940 case CHAIN_FLAT:
941 ret += percent_color_fprintf(fp, " %6.2f%%\n",
942 percent);
943 ret += callchain__fprintf_flat(fp, chain, total_samples);
944 break;
945 case CHAIN_GRAPH_ABS: /* Falldown */
946 case CHAIN_GRAPH_REL:
947 ret += callchain__fprintf_graph(fp, chain,
948 total_samples, 1, 1);
949 default:
950 break;
770 } 951 }
952 ret += fprintf(fp, "\n");
953 rb_node = rb_next(rb_node);
954 }
955
956 return ret;
957}
958
771 959
772 ret = color_fprintf(fp, color, " %6.2f%%", 960static size_t
961hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
962{
963 struct sort_entry *se;
964 size_t ret;
965
966 if (exclude_other && !self->parent)
967 return 0;
968
969 if (total_samples)
970 ret = percent_color_fprintf(fp, " %6.2f%%",
773 (self->count * 100.0) / total_samples); 971 (self->count * 100.0) / total_samples);
774 } else 972 else
775 ret = fprintf(fp, "%12Ld ", self->count); 973 ret = fprintf(fp, "%12Ld ", self->count);
776 974
777 list_for_each_entry(se, &hist_entry__sort_list, list) { 975 list_for_each_entry(se, &hist_entry__sort_list, list) {
@@ -784,6 +982,9 @@ hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
784 982
785 ret += fprintf(fp, "\n"); 983 ret += fprintf(fp, "\n");
786 984
985 if (callchain)
986 hist_entry_callchain__fprintf(fp, self, total_samples);
987
787 return ret; 988 return ret;
788} 989}
789 990
@@ -797,7 +998,7 @@ resolve_symbol(struct thread *thread, struct map **mapp,
797{ 998{
798 struct dso *dso = dsop ? *dsop : NULL; 999 struct dso *dso = dsop ? *dsop : NULL;
799 struct map *map = mapp ? *mapp : NULL; 1000 struct map *map = mapp ? *mapp : NULL;
800 uint64_t ip = *ipp; 1001 u64 ip = *ipp;
801 1002
802 if (!thread) 1003 if (!thread)
803 return NULL; 1004 return NULL;
@@ -814,7 +1015,6 @@ resolve_symbol(struct thread *thread, struct map **mapp,
814 *mapp = map; 1015 *mapp = map;
815got_map: 1016got_map:
816 ip = map->map_ip(map, ip); 1017 ip = map->map_ip(map, ip);
817 *ipp = ip;
818 1018
819 dso = map->dso; 1019 dso = map->dso;
820 } else { 1020 } else {
@@ -828,6 +1028,8 @@ got_map:
828 dso = kernel_dso; 1028 dso = kernel_dso;
829 } 1029 }
830 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>"); 1030 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
1031 dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
1032 *ipp = ip;
831 1033
832 if (dsop) 1034 if (dsop)
833 *dsop = dso; 1035 *dsop = dso;
@@ -846,6 +1048,58 @@ static int call__match(struct symbol *sym)
846 return 0; 1048 return 0;
847} 1049}
848 1050
1051static struct symbol **
1052resolve_callchain(struct thread *thread, struct map *map __used,
1053 struct ip_callchain *chain, struct hist_entry *entry)
1054{
1055 u64 context = PERF_CONTEXT_MAX;
1056 struct symbol **syms = NULL;
1057 unsigned int i;
1058
1059 if (callchain) {
1060 syms = calloc(chain->nr, sizeof(*syms));
1061 if (!syms) {
1062 fprintf(stderr, "Can't allocate memory for symbols\n");
1063 exit(-1);
1064 }
1065 }
1066
1067 for (i = 0; i < chain->nr; i++) {
1068 u64 ip = chain->ips[i];
1069 struct dso *dso = NULL;
1070 struct symbol *sym;
1071
1072 if (ip >= PERF_CONTEXT_MAX) {
1073 context = ip;
1074 continue;
1075 }
1076
1077 switch (context) {
1078 case PERF_CONTEXT_HV:
1079 dso = hypervisor_dso;
1080 break;
1081 case PERF_CONTEXT_KERNEL:
1082 dso = kernel_dso;
1083 break;
1084 default:
1085 break;
1086 }
1087
1088 sym = resolve_symbol(thread, NULL, &dso, &ip);
1089
1090 if (sym) {
1091 if (sort__has_parent && call__match(sym) &&
1092 !entry->parent)
1093 entry->parent = sym;
1094 if (!callchain)
1095 break;
1096 syms[i] = sym;
1097 }
1098 }
1099
1100 return syms;
1101}
1102
849/* 1103/*
850 * collect histogram counts 1104 * collect histogram counts
851 */ 1105 */
@@ -858,6 +1112,7 @@ hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
858 struct rb_node **p = &hist.rb_node; 1112 struct rb_node **p = &hist.rb_node;
859 struct rb_node *parent = NULL; 1113 struct rb_node *parent = NULL;
860 struct hist_entry *he; 1114 struct hist_entry *he;
1115 struct symbol **syms = NULL;
861 struct hist_entry entry = { 1116 struct hist_entry entry = {
862 .thread = thread, 1117 .thread = thread,
863 .map = map, 1118 .map = map,
@@ -867,39 +1122,12 @@ hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
867 .level = level, 1122 .level = level,
868 .count = count, 1123 .count = count,
869 .parent = NULL, 1124 .parent = NULL,
1125 .sorted_chain = RB_ROOT
870 }; 1126 };
871 int cmp; 1127 int cmp;
872 1128
873 if (sort__has_parent && chain) { 1129 if ((sort__has_parent || callchain) && chain)
874 u64 context = PERF_CONTEXT_MAX; 1130 syms = resolve_callchain(thread, map, chain, &entry);
875 int i;
876
877 for (i = 0; i < chain->nr; i++) {
878 u64 ip = chain->ips[i];
879 struct dso *dso = NULL;
880 struct symbol *sym;
881
882 if (ip >= PERF_CONTEXT_MAX) {
883 context = ip;
884 continue;
885 }
886
887 switch (context) {
888 case PERF_CONTEXT_KERNEL:
889 dso = kernel_dso;
890 break;
891 default:
892 break;
893 }
894
895 sym = resolve_symbol(thread, NULL, &dso, &ip);
896
897 if (sym && call__match(sym)) {
898 entry.parent = sym;
899 break;
900 }
901 }
902 }
903 1131
904 while (*p != NULL) { 1132 while (*p != NULL) {
905 parent = *p; 1133 parent = *p;
@@ -909,6 +1137,10 @@ hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
909 1137
910 if (!cmp) { 1138 if (!cmp) {
911 he->count += count; 1139 he->count += count;
1140 if (callchain) {
1141 append_chain(&he->callchain, chain, syms);
1142 free(syms);
1143 }
912 return 0; 1144 return 0;
913 } 1145 }
914 1146
@@ -922,6 +1154,11 @@ hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
922 if (!he) 1154 if (!he)
923 return -ENOMEM; 1155 return -ENOMEM;
924 *he = entry; 1156 *he = entry;
1157 if (callchain) {
1158 callchain_init(&he->callchain);
1159 append_chain(&he->callchain, chain, syms);
1160 free(syms);
1161 }
925 rb_link_node(&he->rb_node, parent, p); 1162 rb_link_node(&he->rb_node, parent, p);
926 rb_insert_color(&he->rb_node, &hist); 1163 rb_insert_color(&he->rb_node, &hist);
927 1164
@@ -992,12 +1229,16 @@ static void collapse__resort(void)
992 1229
993static struct rb_root output_hists; 1230static struct rb_root output_hists;
994 1231
995static void output__insert_entry(struct hist_entry *he) 1232static void output__insert_entry(struct hist_entry *he, u64 min_callchain_hits)
996{ 1233{
997 struct rb_node **p = &output_hists.rb_node; 1234 struct rb_node **p = &output_hists.rb_node;
998 struct rb_node *parent = NULL; 1235 struct rb_node *parent = NULL;
999 struct hist_entry *iter; 1236 struct hist_entry *iter;
1000 1237
1238 if (callchain)
1239 callchain_param.sort(&he->sorted_chain, &he->callchain,
1240 min_callchain_hits, &callchain_param);
1241
1001 while (*p != NULL) { 1242 while (*p != NULL) {
1002 parent = *p; 1243 parent = *p;
1003 iter = rb_entry(parent, struct hist_entry, rb_node); 1244 iter = rb_entry(parent, struct hist_entry, rb_node);
@@ -1012,11 +1253,14 @@ static void output__insert_entry(struct hist_entry *he)
1012 rb_insert_color(&he->rb_node, &output_hists); 1253 rb_insert_color(&he->rb_node, &output_hists);
1013} 1254}
1014 1255
1015static void output__resort(void) 1256static void output__resort(u64 total_samples)
1016{ 1257{
1017 struct rb_node *next; 1258 struct rb_node *next;
1018 struct hist_entry *n; 1259 struct hist_entry *n;
1019 struct rb_root *tree = &hist; 1260 struct rb_root *tree = &hist;
1261 u64 min_callchain_hits;
1262
1263 min_callchain_hits = total_samples * (callchain_param.min_percent / 100);
1020 1264
1021 if (sort__need_collapse) 1265 if (sort__need_collapse)
1022 tree = &collapse_hists; 1266 tree = &collapse_hists;
@@ -1028,7 +1272,7 @@ static void output__resort(void)
1028 next = rb_next(&n->rb_node); 1272 next = rb_next(&n->rb_node);
1029 1273
1030 rb_erase(&n->rb_node, tree); 1274 rb_erase(&n->rb_node, tree);
1031 output__insert_entry(n); 1275 output__insert_entry(n, min_callchain_hits);
1032 } 1276 }
1033} 1277}
1034 1278
@@ -1054,7 +1298,7 @@ static size_t output__fprintf(FILE *fp, u64 total_samples)
1054 1298
1055 fprintf(fp, "# ........"); 1299 fprintf(fp, "# ........");
1056 list_for_each_entry(se, &hist_entry__sort_list, list) { 1300 list_for_each_entry(se, &hist_entry__sort_list, list) {
1057 int i; 1301 unsigned int i;
1058 1302
1059 if (exclude_other && (se == &sort_parent)) 1303 if (exclude_other && (se == &sort_parent))
1060 continue; 1304 continue;
@@ -1115,7 +1359,7 @@ static int validate_chain(struct ip_callchain *chain, event_t *event)
1115} 1359}
1116 1360
1117static int 1361static int
1118process_overflow_event(event_t *event, unsigned long offset, unsigned long head) 1362process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1119{ 1363{
1120 char level; 1364 char level;
1121 int show = 0; 1365 int show = 0;
@@ -1126,13 +1370,14 @@ process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
1126 struct map *map = NULL; 1370 struct map *map = NULL;
1127 void *more_data = event->ip.__more_data; 1371 void *more_data = event->ip.__more_data;
1128 struct ip_callchain *chain = NULL; 1372 struct ip_callchain *chain = NULL;
1373 int cpumode;
1129 1374
1130 if (event->header.type & PERF_SAMPLE_PERIOD) { 1375 if (sample_type & PERF_SAMPLE_PERIOD) {
1131 period = *(u64 *)more_data; 1376 period = *(u64 *)more_data;
1132 more_data += sizeof(u64); 1377 more_data += sizeof(u64);
1133 } 1378 }
1134 1379
1135 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p period: %Ld\n", 1380 dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d: %p period: %Ld\n",
1136 (void *)(offset + head), 1381 (void *)(offset + head),
1137 (void *)(long)(event->header.size), 1382 (void *)(long)(event->header.size),
1138 event->header.misc, 1383 event->header.misc,
@@ -1140,8 +1385,8 @@ process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
1140 (void *)(long)ip, 1385 (void *)(long)ip,
1141 (long long)period); 1386 (long long)period);
1142 1387
1143 if (event->header.type & PERF_SAMPLE_CALLCHAIN) { 1388 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
1144 int i; 1389 unsigned int i;
1145 1390
1146 chain = (void *)more_data; 1391 chain = (void *)more_data;
1147 1392
@@ -1166,7 +1411,12 @@ process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
1166 return -1; 1411 return -1;
1167 } 1412 }
1168 1413
1169 if (event->header.misc & PERF_EVENT_MISC_KERNEL) { 1414 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
1415 return 0;
1416
1417 cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
1418
1419 if (cpumode == PERF_EVENT_MISC_KERNEL) {
1170 show = SHOW_KERNEL; 1420 show = SHOW_KERNEL;
1171 level = 'k'; 1421 level = 'k';
1172 1422
@@ -1174,7 +1424,7 @@ process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
1174 1424
1175 dprintf(" ...... dso: %s\n", dso->name); 1425 dprintf(" ...... dso: %s\n", dso->name);
1176 1426
1177 } else if (event->header.misc & PERF_EVENT_MISC_USER) { 1427 } else if (cpumode == PERF_EVENT_MISC_USER) {
1178 1428
1179 show = SHOW_USER; 1429 show = SHOW_USER;
1180 level = '.'; 1430 level = '.';
@@ -1182,12 +1432,21 @@ process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
1182 } else { 1432 } else {
1183 show = SHOW_HV; 1433 show = SHOW_HV;
1184 level = 'H'; 1434 level = 'H';
1435
1436 dso = hypervisor_dso;
1437
1185 dprintf(" ...... dso: [hypervisor]\n"); 1438 dprintf(" ...... dso: [hypervisor]\n");
1186 } 1439 }
1187 1440
1188 if (show & show_mask) { 1441 if (show & show_mask) {
1189 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip); 1442 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
1190 1443
1444 if (dso_list && dso && dso->name && !strlist__has_entry(dso_list, dso->name))
1445 return 0;
1446
1447 if (sym_list && sym && !strlist__has_entry(sym_list, sym->name))
1448 return 0;
1449
1191 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) { 1450 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
1192 eprintf("problem incrementing symbol count, skipping event\n"); 1451 eprintf("problem incrementing symbol count, skipping event\n");
1193 return -1; 1452 return -1;
@@ -1328,14 +1587,27 @@ static void trace_event(event_t *event)
1328} 1587}
1329 1588
1330static int 1589static int
1590process_read_event(event_t *event, unsigned long offset, unsigned long head)
1591{
1592 dprintf("%p [%p]: PERF_EVENT_READ: %d %d %Lu\n",
1593 (void *)(offset + head),
1594 (void *)(long)(event->header.size),
1595 event->read.pid,
1596 event->read.tid,
1597 event->read.value);
1598
1599 return 0;
1600}
1601
1602static int
1331process_event(event_t *event, unsigned long offset, unsigned long head) 1603process_event(event_t *event, unsigned long offset, unsigned long head)
1332{ 1604{
1333 trace_event(event); 1605 trace_event(event);
1334 1606
1335 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
1336 return process_overflow_event(event, offset, head);
1337
1338 switch (event->header.type) { 1607 switch (event->header.type) {
1608 case PERF_EVENT_SAMPLE:
1609 return process_sample_event(event, offset, head);
1610
1339 case PERF_EVENT_MMAP: 1611 case PERF_EVENT_MMAP:
1340 return process_mmap_event(event, offset, head); 1612 return process_mmap_event(event, offset, head);
1341 1613
@@ -1351,6 +1623,9 @@ process_event(event_t *event, unsigned long offset, unsigned long head)
1351 case PERF_EVENT_LOST: 1623 case PERF_EVENT_LOST:
1352 return process_lost_event(event, offset, head); 1624 return process_lost_event(event, offset, head);
1353 1625
1626 case PERF_EVENT_READ:
1627 return process_read_event(event, offset, head);
1628
1354 /* 1629 /*
1355 * We dont process them right now but they are fine: 1630 * We dont process them right now but they are fine:
1356 */ 1631 */
@@ -1366,13 +1641,30 @@ process_event(event_t *event, unsigned long offset, unsigned long head)
1366 return 0; 1641 return 0;
1367} 1642}
1368 1643
1369static struct perf_file_header file_header; 1644static struct perf_header *header;
1645
1646static u64 perf_header__sample_type(void)
1647{
1648 u64 sample_type = 0;
1649 int i;
1650
1651 for (i = 0; i < header->attrs; i++) {
1652 struct perf_header_attr *attr = header->attr[i];
1653
1654 if (!sample_type)
1655 sample_type = attr->attr.sample_type;
1656 else if (sample_type != attr->attr.sample_type)
1657 die("non matching sample_type");
1658 }
1659
1660 return sample_type;
1661}
1370 1662
1371static int __cmd_report(void) 1663static int __cmd_report(void)
1372{ 1664{
1373 int ret, rc = EXIT_FAILURE; 1665 int ret, rc = EXIT_FAILURE;
1374 unsigned long offset = 0; 1666 unsigned long offset = 0;
1375 unsigned long head = sizeof(file_header); 1667 unsigned long head, shift;
1376 struct stat stat; 1668 struct stat stat;
1377 event_t *event; 1669 event_t *event;
1378 uint32_t size; 1670 uint32_t size;
@@ -1400,15 +1692,24 @@ static int __cmd_report(void)
1400 exit(0); 1692 exit(0);
1401 } 1693 }
1402 1694
1403 if (read(input, &file_header, sizeof(file_header)) == -1) { 1695 header = perf_header__read(input);
1404 perror("failed to read file headers"); 1696 head = header->data_offset;
1405 exit(-1);
1406 }
1407 1697
1408 if (sort__has_parent && 1698 sample_type = perf_header__sample_type();
1409 !(file_header.sample_type & PERF_SAMPLE_CALLCHAIN)) { 1699
1410 fprintf(stderr, "selected --sort parent, but no callchain data\n"); 1700 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1411 exit(-1); 1701 if (sort__has_parent) {
1702 fprintf(stderr, "selected --sort parent, but no"
1703 " callchain data. Did you call"
1704 " perf record without -g?\n");
1705 exit(-1);
1706 }
1707 if (callchain) {
1708 fprintf(stderr, "selected -c but no callchain data."
1709 " Did you call perf record without"
1710 " -g?\n");
1711 exit(-1);
1712 }
1412 } 1713 }
1413 1714
1414 if (load_kernel() < 0) { 1715 if (load_kernel() < 0) {
@@ -1426,6 +1727,11 @@ static int __cmd_report(void)
1426 cwd = NULL; 1727 cwd = NULL;
1427 cwdlen = 0; 1728 cwdlen = 0;
1428 } 1729 }
1730
1731 shift = page_size * (head / page_size);
1732 offset += shift;
1733 head -= shift;
1734
1429remap: 1735remap:
1430 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ, 1736 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1431 MAP_SHARED, input, offset); 1737 MAP_SHARED, input, offset);
@@ -1442,9 +1748,10 @@ more:
1442 size = 8; 1748 size = 8;
1443 1749
1444 if (head + event->header.size >= page_size * mmap_window) { 1750 if (head + event->header.size >= page_size * mmap_window) {
1445 unsigned long shift = page_size * (head / page_size);
1446 int ret; 1751 int ret;
1447 1752
1753 shift = page_size * (head / page_size);
1754
1448 ret = munmap(buf, page_size * mmap_window); 1755 ret = munmap(buf, page_size * mmap_window);
1449 assert(ret == 0); 1756 assert(ret == 0);
1450 1757
@@ -1482,10 +1789,10 @@ more:
1482 1789
1483 head += size; 1790 head += size;
1484 1791
1485 if (offset + head >= sizeof(file_header) + file_header.data_size) 1792 if (offset + head >= header->data_offset + header->data_size)
1486 goto done; 1793 goto done;
1487 1794
1488 if (offset + head < stat.st_size) 1795 if (offset + head < (unsigned long)stat.st_size)
1489 goto more; 1796 goto more;
1490 1797
1491done: 1798done:
@@ -1509,12 +1816,58 @@ done:
1509 dsos__fprintf(stdout); 1816 dsos__fprintf(stdout);
1510 1817
1511 collapse__resort(); 1818 collapse__resort();
1512 output__resort(); 1819 output__resort(total);
1513 output__fprintf(stdout, total); 1820 output__fprintf(stdout, total);
1514 1821
1515 return rc; 1822 return rc;
1516} 1823}
1517 1824
1825static int
1826parse_callchain_opt(const struct option *opt __used, const char *arg,
1827 int unset __used)
1828{
1829 char *tok;
1830 char *endptr;
1831
1832 callchain = 1;
1833
1834 if (!arg)
1835 return 0;
1836
1837 tok = strtok((char *)arg, ",");
1838 if (!tok)
1839 return -1;
1840
1841 /* get the output mode */
1842 if (!strncmp(tok, "graph", strlen(arg)))
1843 callchain_param.mode = CHAIN_GRAPH_ABS;
1844
1845 else if (!strncmp(tok, "flat", strlen(arg)))
1846 callchain_param.mode = CHAIN_FLAT;
1847
1848 else if (!strncmp(tok, "fractal", strlen(arg)))
1849 callchain_param.mode = CHAIN_GRAPH_REL;
1850
1851 else
1852 return -1;
1853
1854 /* get the min percentage */
1855 tok = strtok(NULL, ",");
1856 if (!tok)
1857 goto setup;
1858
1859 callchain_param.min_percent = strtod(tok, &endptr);
1860 if (tok == endptr)
1861 return -1;
1862
1863setup:
1864 if (register_callchain_param(&callchain_param) < 0) {
1865 fprintf(stderr, "Can't register callchain params\n");
1866 return -1;
1867 }
1868 return 0;
1869}
1870
1518static const char * const report_usage[] = { 1871static const char * const report_usage[] = {
1519 "perf report [<options>] <command>", 1872 "perf report [<options>] <command>",
1520 NULL 1873 NULL
@@ -1528,6 +1881,8 @@ static const struct option options[] = {
1528 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, 1881 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1529 "dump raw trace in ASCII"), 1882 "dump raw trace in ASCII"),
1530 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"), 1883 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1884 OPT_BOOLEAN('m', "modules", &modules,
1885 "load module symbols - WARNING: use only with -k and LIVE kernel"),
1531 OPT_STRING('s', "sort", &sort_order, "key[,key2...]", 1886 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1532 "sort by key(s): pid, comm, dso, symbol, parent"), 1887 "sort by key(s): pid, comm, dso, symbol, parent"),
1533 OPT_BOOLEAN('P', "full-paths", &full_paths, 1888 OPT_BOOLEAN('P', "full-paths", &full_paths,
@@ -1536,6 +1891,15 @@ static const struct option options[] = {
1536 "regex filter to identify parent, see: '--sort parent'"), 1891 "regex filter to identify parent, see: '--sort parent'"),
1537 OPT_BOOLEAN('x', "exclude-other", &exclude_other, 1892 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1538 "Only display entries with parent-match"), 1893 "Only display entries with parent-match"),
1894 OPT_CALLBACK_DEFAULT('c', "callchain", NULL, "output_type,min_percent",
1895 "Display callchains using output_type and min percent threshold. "
1896 "Default: flat,0", &parse_callchain_opt, callchain_default_opt),
1897 OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
1898 "only consider symbols in these dsos"),
1899 OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
1900 "only consider symbols in these comms"),
1901 OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
1902 "only consider these symbols"),
1539 OPT_END() 1903 OPT_END()
1540}; 1904};
1541 1905
@@ -1554,7 +1918,20 @@ static void setup_sorting(void)
1554 free(str); 1918 free(str);
1555} 1919}
1556 1920
1557int cmd_report(int argc, const char **argv, const char *prefix) 1921static void setup_list(struct strlist **list, const char *list_str,
1922 const char *list_name)
1923{
1924 if (list_str) {
1925 *list = strlist__new(true, list_str);
1926 if (!*list) {
1927 fprintf(stderr, "problems parsing %s list\n",
1928 list_name);
1929 exit(129);
1930 }
1931 }
1932}
1933
1934int cmd_report(int argc, const char **argv, const char *prefix __used)
1558{ 1935{
1559 symbol__init(); 1936 symbol__init();
1560 1937
@@ -1575,6 +1952,10 @@ int cmd_report(int argc, const char **argv, const char *prefix)
1575 if (argc) 1952 if (argc)
1576 usage_with_options(report_usage, options); 1953 usage_with_options(report_usage, options);
1577 1954
1955 setup_list(&dso_list, dso_list_str, "dso");
1956 setup_list(&comm_list, comm_list_str, "comm");
1957 setup_list(&sym_list, sym_list_str, "symbol");
1958
1578 setup_pager(); 1959 setup_pager();
1579 1960
1580 return __cmd_report(); 1961 return __cmd_report();
diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index 6d3eeac1ea25..27921a8ce1a9 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -32,6 +32,7 @@
32 * Wu Fengguang <fengguang.wu@intel.com> 32 * Wu Fengguang <fengguang.wu@intel.com>
33 * Mike Galbraith <efault@gmx.de> 33 * Mike Galbraith <efault@gmx.de>
34 * Paul Mackerras <paulus@samba.org> 34 * Paul Mackerras <paulus@samba.org>
35 * Jaswinder Singh Rajput <jaswinder@kernel.org>
35 * 36 *
36 * Released under the GPL v2. (and only v2, not any later version) 37 * Released under the GPL v2. (and only v2, not any later version)
37 */ 38 */
@@ -45,7 +46,7 @@
45#include <sys/prctl.h> 46#include <sys/prctl.h>
46#include <math.h> 47#include <math.h>
47 48
48static struct perf_counter_attr default_attrs[MAX_COUNTERS] = { 49static struct perf_counter_attr default_attrs[] = {
49 50
50 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK }, 51 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
51 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES}, 52 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES},
@@ -59,42 +60,28 @@ static struct perf_counter_attr default_attrs[MAX_COUNTERS] = {
59 60
60}; 61};
61 62
63#define MAX_RUN 100
64
62static int system_wide = 0; 65static int system_wide = 0;
63static int inherit = 1;
64static int verbose = 0; 66static int verbose = 0;
67static unsigned int nr_cpus = 0;
68static int run_idx = 0;
65 69
66static int fd[MAX_NR_CPUS][MAX_COUNTERS]; 70static int run_count = 1;
67 71static int inherit = 1;
68static int target_pid = -1;
69static int nr_cpus = 0;
70static unsigned int page_size;
71
72static int scale = 1; 72static int scale = 1;
73static int target_pid = -1;
74static int null_run = 0;
73 75
74static const unsigned int default_count[] = { 76static int fd[MAX_NR_CPUS][MAX_COUNTERS];
75 1000000,
76 1000000,
77 10000,
78 10000,
79 1000000,
80 10000,
81};
82
83#define MAX_RUN 100
84
85static int run_count = 1;
86static int run_idx = 0;
87
88static u64 event_res[MAX_RUN][MAX_COUNTERS][3];
89static u64 event_scaled[MAX_RUN][MAX_COUNTERS];
90
91//static u64 event_hist[MAX_RUN][MAX_COUNTERS][3];
92
93 77
94static u64 runtime_nsecs[MAX_RUN]; 78static u64 runtime_nsecs[MAX_RUN];
95static u64 walltime_nsecs[MAX_RUN]; 79static u64 walltime_nsecs[MAX_RUN];
96static u64 runtime_cycles[MAX_RUN]; 80static u64 runtime_cycles[MAX_RUN];
97 81
82static u64 event_res[MAX_RUN][MAX_COUNTERS][3];
83static u64 event_scaled[MAX_RUN][MAX_COUNTERS];
84
98static u64 event_res_avg[MAX_COUNTERS][3]; 85static u64 event_res_avg[MAX_COUNTERS][3];
99static u64 event_res_noise[MAX_COUNTERS][3]; 86static u64 event_res_noise[MAX_COUNTERS][3];
100 87
@@ -109,7 +96,14 @@ static u64 walltime_nsecs_noise;
109static u64 runtime_cycles_avg; 96static u64 runtime_cycles_avg;
110static u64 runtime_cycles_noise; 97static u64 runtime_cycles_noise;
111 98
112static void create_perf_stat_counter(int counter) 99#define MATCH_EVENT(t, c, counter) \
100 (attrs[counter].type == PERF_TYPE_##t && \
101 attrs[counter].config == PERF_COUNT_##c)
102
103#define ERR_PERF_OPEN \
104"Error: counter %d, sys_perf_counter_open() syscall returned with %d (%s)\n"
105
106static void create_perf_stat_counter(int counter, int pid)
113{ 107{
114 struct perf_counter_attr *attr = attrs + counter; 108 struct perf_counter_attr *attr = attrs + counter;
115 109
@@ -118,21 +112,23 @@ static void create_perf_stat_counter(int counter)
118 PERF_FORMAT_TOTAL_TIME_RUNNING; 112 PERF_FORMAT_TOTAL_TIME_RUNNING;
119 113
120 if (system_wide) { 114 if (system_wide) {
121 int cpu; 115 unsigned int cpu;
122 for (cpu = 0; cpu < nr_cpus; cpu ++) { 116
117 for (cpu = 0; cpu < nr_cpus; cpu++) {
123 fd[cpu][counter] = sys_perf_counter_open(attr, -1, cpu, -1, 0); 118 fd[cpu][counter] = sys_perf_counter_open(attr, -1, cpu, -1, 0);
124 if (fd[cpu][counter] < 0 && verbose) { 119 if (fd[cpu][counter] < 0 && verbose)
125 printf("Error: counter %d, sys_perf_counter_open() syscall returned with %d (%s)\n", counter, fd[cpu][counter], strerror(errno)); 120 fprintf(stderr, ERR_PERF_OPEN, counter,
126 } 121 fd[cpu][counter], strerror(errno));
127 } 122 }
128 } else { 123 } else {
129 attr->inherit = inherit; 124 attr->inherit = inherit;
130 attr->disabled = 1; 125 attr->disabled = 1;
131 126 attr->enable_on_exec = 1;
132 fd[0][counter] = sys_perf_counter_open(attr, 0, -1, -1, 0); 127
133 if (fd[0][counter] < 0 && verbose) { 128 fd[0][counter] = sys_perf_counter_open(attr, pid, -1, -1, 0);
134 printf("Error: counter %d, sys_perf_counter_open() syscall returned with %d (%s)\n", counter, fd[0][counter], strerror(errno)); 129 if (fd[0][counter] < 0 && verbose)
135 } 130 fprintf(stderr, ERR_PERF_OPEN, counter,
131 fd[0][counter], strerror(errno));
136 } 132 }
137} 133}
138 134
@@ -141,13 +137,8 @@ static void create_perf_stat_counter(int counter)
141 */ 137 */
142static inline int nsec_counter(int counter) 138static inline int nsec_counter(int counter)
143{ 139{
144 if (attrs[counter].type != PERF_TYPE_SOFTWARE) 140 if (MATCH_EVENT(SOFTWARE, SW_CPU_CLOCK, counter) ||
145 return 0; 141 MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter))
146
147 if (attrs[counter].config == PERF_COUNT_SW_CPU_CLOCK)
148 return 1;
149
150 if (attrs[counter].config == PERF_COUNT_SW_TASK_CLOCK)
151 return 1; 142 return 1;
152 143
153 return 0; 144 return 0;
@@ -159,8 +150,8 @@ static inline int nsec_counter(int counter)
159static void read_counter(int counter) 150static void read_counter(int counter)
160{ 151{
161 u64 *count, single_count[3]; 152 u64 *count, single_count[3];
162 ssize_t res; 153 unsigned int cpu;
163 int cpu, nv; 154 size_t res, nv;
164 int scaled; 155 int scaled;
165 156
166 count = event_res[run_idx][counter]; 157 count = event_res[run_idx][counter];
@@ -168,12 +159,13 @@ static void read_counter(int counter)
168 count[0] = count[1] = count[2] = 0; 159 count[0] = count[1] = count[2] = 0;
169 160
170 nv = scale ? 3 : 1; 161 nv = scale ? 3 : 1;
171 for (cpu = 0; cpu < nr_cpus; cpu ++) { 162 for (cpu = 0; cpu < nr_cpus; cpu++) {
172 if (fd[cpu][counter] < 0) 163 if (fd[cpu][counter] < 0)
173 continue; 164 continue;
174 165
175 res = read(fd[cpu][counter], single_count, nv * sizeof(u64)); 166 res = read(fd[cpu][counter], single_count, nv * sizeof(u64));
176 assert(res == nv * sizeof(u64)); 167 assert(res == nv * sizeof(u64));
168
177 close(fd[cpu][counter]); 169 close(fd[cpu][counter]);
178 fd[cpu][counter] = -1; 170 fd[cpu][counter] = -1;
179 171
@@ -201,46 +193,81 @@ static void read_counter(int counter)
201 /* 193 /*
202 * Save the full runtime - to allow normalization during printout: 194 * Save the full runtime - to allow normalization during printout:
203 */ 195 */
204 if (attrs[counter].type == PERF_TYPE_SOFTWARE && 196 if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter))
205 attrs[counter].config == PERF_COUNT_SW_TASK_CLOCK)
206 runtime_nsecs[run_idx] = count[0]; 197 runtime_nsecs[run_idx] = count[0];
207 if (attrs[counter].type == PERF_TYPE_HARDWARE && 198 if (MATCH_EVENT(HARDWARE, HW_CPU_CYCLES, counter))
208 attrs[counter].config == PERF_COUNT_HW_CPU_CYCLES)
209 runtime_cycles[run_idx] = count[0]; 199 runtime_cycles[run_idx] = count[0];
210} 200}
211 201
212static int run_perf_stat(int argc, const char **argv) 202static int run_perf_stat(int argc __used, const char **argv)
213{ 203{
214 unsigned long long t0, t1; 204 unsigned long long t0, t1;
215 int status = 0; 205 int status = 0;
216 int counter; 206 int counter;
217 int pid; 207 int pid;
208 int child_ready_pipe[2], go_pipe[2];
209 char buf;
218 210
219 if (!system_wide) 211 if (!system_wide)
220 nr_cpus = 1; 212 nr_cpus = 1;
221 213
222 for (counter = 0; counter < nr_counters; counter++) 214 if (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0) {
223 create_perf_stat_counter(counter); 215 perror("failed to create pipes");
224 216 exit(1);
225 /* 217 }
226 * Enable counters and exec the command:
227 */
228 t0 = rdclock();
229 prctl(PR_TASK_PERF_COUNTERS_ENABLE);
230 218
231 if ((pid = fork()) < 0) 219 if ((pid = fork()) < 0)
232 perror("failed to fork"); 220 perror("failed to fork");
233 221
234 if (!pid) { 222 if (!pid) {
235 if (execvp(argv[0], (char **)argv)) { 223 close(child_ready_pipe[0]);
236 perror(argv[0]); 224 close(go_pipe[1]);
237 exit(-1); 225 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
238 } 226
227 /*
228 * Do a dummy execvp to get the PLT entry resolved,
229 * so we avoid the resolver overhead on the real
230 * execvp call.
231 */
232 execvp("", (char **)argv);
233
234 /*
235 * Tell the parent we're ready to go
236 */
237 close(child_ready_pipe[1]);
238
239 /*
240 * Wait until the parent tells us to go.
241 */
242 if (read(go_pipe[0], &buf, 1) == -1)
243 perror("unable to read pipe");
244
245 execvp(argv[0], (char **)argv);
246
247 perror(argv[0]);
248 exit(-1);
239 } 249 }
240 250
251 /*
252 * Wait for the child to be ready to exec.
253 */
254 close(child_ready_pipe[1]);
255 close(go_pipe[0]);
256 if (read(child_ready_pipe[0], &buf, 1) == -1)
257 perror("unable to read pipe");
258 close(child_ready_pipe[0]);
259
260 for (counter = 0; counter < nr_counters; counter++)
261 create_perf_stat_counter(counter, pid);
262
263 /*
264 * Enable counters and exec the command:
265 */
266 t0 = rdclock();
267
268 close(go_pipe[1]);
241 wait(&status); 269 wait(&status);
242 270
243 prctl(PR_TASK_PERF_COUNTERS_DISABLE);
244 t1 = rdclock(); 271 t1 = rdclock();
245 272
246 walltime_nsecs[run_idx] = t1 - t0; 273 walltime_nsecs[run_idx] = t1 - t0;
@@ -262,11 +289,9 @@ static void nsec_printout(int counter, u64 *count, u64 *noise)
262{ 289{
263 double msecs = (double)count[0] / 1000000; 290 double msecs = (double)count[0] / 1000000;
264 291
265 fprintf(stderr, " %14.6f %-20s", msecs, event_name(counter)); 292 fprintf(stderr, " %14.6f %-24s", msecs, event_name(counter));
266
267 if (attrs[counter].type == PERF_TYPE_SOFTWARE &&
268 attrs[counter].config == PERF_COUNT_SW_TASK_CLOCK) {
269 293
294 if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter)) {
270 if (walltime_nsecs_avg) 295 if (walltime_nsecs_avg)
271 fprintf(stderr, " # %10.3f CPUs ", 296 fprintf(stderr, " # %10.3f CPUs ",
272 (double)count[0] / (double)walltime_nsecs_avg); 297 (double)count[0] / (double)walltime_nsecs_avg);
@@ -276,12 +301,10 @@ static void nsec_printout(int counter, u64 *count, u64 *noise)
276 301
277static void abs_printout(int counter, u64 *count, u64 *noise) 302static void abs_printout(int counter, u64 *count, u64 *noise)
278{ 303{
279 fprintf(stderr, " %14Ld %-20s", count[0], event_name(counter)); 304 fprintf(stderr, " %14Ld %-24s", count[0], event_name(counter));
280 305
281 if (runtime_cycles_avg && 306 if (runtime_cycles_avg &&
282 attrs[counter].type == PERF_TYPE_HARDWARE && 307 MATCH_EVENT(HARDWARE, HW_INSTRUCTIONS, counter)) {
283 attrs[counter].config == PERF_COUNT_HW_INSTRUCTIONS) {
284
285 fprintf(stderr, " # %10.3f IPC ", 308 fprintf(stderr, " # %10.3f IPC ",
286 (double)count[0] / (double)runtime_cycles_avg); 309 (double)count[0] / (double)runtime_cycles_avg);
287 } else { 310 } else {
@@ -306,7 +329,7 @@ static void print_counter(int counter)
306 scaled = event_scaled_avg[counter]; 329 scaled = event_scaled_avg[counter];
307 330
308 if (scaled == -1) { 331 if (scaled == -1) {
309 fprintf(stderr, " %14s %-20s\n", 332 fprintf(stderr, " %14s %-24s\n",
310 "<not counted>", event_name(counter)); 333 "<not counted>", event_name(counter));
311 return; 334 return;
312 } 335 }
@@ -364,8 +387,11 @@ static void calc_avg(void)
364 event_res_avg[j]+1, event_res[i][j]+1); 387 event_res_avg[j]+1, event_res[i][j]+1);
365 update_avg("counter/2", j, 388 update_avg("counter/2", j,
366 event_res_avg[j]+2, event_res[i][j]+2); 389 event_res_avg[j]+2, event_res[i][j]+2);
367 update_avg("scaled", j, 390 if (event_scaled[i][j] != (u64)-1)
368 event_scaled_avg + j, event_scaled[i]+j); 391 update_avg("scaled", j,
392 event_scaled_avg + j, event_scaled[i]+j);
393 else
394 event_scaled_avg[j] = -1;
369 } 395 }
370 } 396 }
371 runtime_nsecs_avg /= run_count; 397 runtime_nsecs_avg /= run_count;
@@ -429,11 +455,14 @@ static void print_stat(int argc, const char **argv)
429 for (counter = 0; counter < nr_counters; counter++) 455 for (counter = 0; counter < nr_counters; counter++)
430 print_counter(counter); 456 print_counter(counter);
431 457
432
433 fprintf(stderr, "\n"); 458 fprintf(stderr, "\n");
434 fprintf(stderr, " %14.9f seconds time elapsed.\n", 459 fprintf(stderr, " %14.9f seconds time elapsed",
435 (double)walltime_nsecs_avg/1e9); 460 (double)walltime_nsecs_avg/1e9);
436 fprintf(stderr, "\n"); 461 if (run_count > 1) {
462 fprintf(stderr, " ( +- %7.3f%% )",
463 100.0*(double)walltime_nsecs_noise/(double)walltime_nsecs_avg);
464 }
465 fprintf(stderr, "\n\n");
437} 466}
438 467
439static volatile int signr = -1; 468static volatile int signr = -1;
@@ -466,36 +495,37 @@ static const struct option options[] = {
466 OPT_INTEGER('p', "pid", &target_pid, 495 OPT_INTEGER('p', "pid", &target_pid,
467 "stat events on existing pid"), 496 "stat events on existing pid"),
468 OPT_BOOLEAN('a', "all-cpus", &system_wide, 497 OPT_BOOLEAN('a', "all-cpus", &system_wide,
469 "system-wide collection from all CPUs"), 498 "system-wide collection from all CPUs"),
470 OPT_BOOLEAN('S', "scale", &scale, 499 OPT_BOOLEAN('S', "scale", &scale,
471 "scale/normalize counters"), 500 "scale/normalize counters"),
472 OPT_BOOLEAN('v', "verbose", &verbose, 501 OPT_BOOLEAN('v', "verbose", &verbose,
473 "be more verbose (show counter open errors, etc)"), 502 "be more verbose (show counter open errors, etc)"),
474 OPT_INTEGER('r', "repeat", &run_count, 503 OPT_INTEGER('r', "repeat", &run_count,
475 "repeat command and print average + stddev (max: 100)"), 504 "repeat command and print average + stddev (max: 100)"),
505 OPT_BOOLEAN('n', "null", &null_run,
506 "null run - dont start any counters"),
476 OPT_END() 507 OPT_END()
477}; 508};
478 509
479int cmd_stat(int argc, const char **argv, const char *prefix) 510int cmd_stat(int argc, const char **argv, const char *prefix __used)
480{ 511{
481 int status; 512 int status;
482 513
483 page_size = sysconf(_SC_PAGE_SIZE);
484
485 memcpy(attrs, default_attrs, sizeof(attrs));
486
487 argc = parse_options(argc, argv, options, stat_usage, 0); 514 argc = parse_options(argc, argv, options, stat_usage, 0);
488 if (!argc) 515 if (!argc)
489 usage_with_options(stat_usage, options); 516 usage_with_options(stat_usage, options);
490 if (run_count <= 0 || run_count > MAX_RUN) 517 if (run_count <= 0 || run_count > MAX_RUN)
491 usage_with_options(stat_usage, options); 518 usage_with_options(stat_usage, options);
492 519
493 if (!nr_counters) 520 /* Set attrs and nr_counters if no event is selected and !null_run */
494 nr_counters = 8; 521 if (!null_run && !nr_counters) {
522 memcpy(attrs, default_attrs, sizeof(default_attrs));
523 nr_counters = ARRAY_SIZE(default_attrs);
524 }
495 525
496 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); 526 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
497 assert(nr_cpus <= MAX_NR_CPUS); 527 assert(nr_cpus <= MAX_NR_CPUS);
498 assert(nr_cpus >= 0); 528 assert((int)nr_cpus >= 0);
499 529
500 /* 530 /*
501 * We dont want to block the signals - that would cause 531 * We dont want to block the signals - that would cause
@@ -511,7 +541,7 @@ int cmd_stat(int argc, const char **argv, const char *prefix)
511 status = 0; 541 status = 0;
512 for (run_idx = 0; run_idx < run_count; run_idx++) { 542 for (run_idx = 0; run_idx < run_count; run_idx++) {
513 if (run_count != 1 && verbose) 543 if (run_count != 1 && verbose)
514 fprintf(stderr, "[ perf stat: executing run #%d ... ]\n", run_idx+1); 544 fprintf(stderr, "[ perf stat: executing run #%d ... ]\n", run_idx + 1);
515 status = run_perf_stat(argc, argv); 545 status = run_perf_stat(argc, argv);
516 } 546 }
517 547
diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index 5352b5e352ed..95d5c0ae375a 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -23,7 +23,7 @@
23#include "util/symbol.h" 23#include "util/symbol.h"
24#include "util/color.h" 24#include "util/color.h"
25#include "util/util.h" 25#include "util/util.h"
26#include "util/rbtree.h" 26#include <linux/rbtree.h>
27#include "util/parse-options.h" 27#include "util/parse-options.h"
28#include "util/parse-events.h" 28#include "util/parse-events.h"
29 29
@@ -66,6 +66,7 @@ static unsigned int page_size;
66static unsigned int mmap_pages = 16; 66static unsigned int mmap_pages = 16;
67static int freq = 0; 67static int freq = 0;
68static int verbose = 0; 68static int verbose = 0;
69static char *vmlinux = NULL;
69 70
70static char *sym_filter; 71static char *sym_filter;
71static unsigned long filter_start; 72static unsigned long filter_start;
@@ -238,7 +239,6 @@ static void print_sym_table(void)
238 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) { 239 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
239 struct sym_entry *syme = rb_entry(nd, struct sym_entry, rb_node); 240 struct sym_entry *syme = rb_entry(nd, struct sym_entry, rb_node);
240 struct symbol *sym = (struct symbol *)(syme + 1); 241 struct symbol *sym = (struct symbol *)(syme + 1);
241 char *color = PERF_COLOR_NORMAL;
242 double pcnt; 242 double pcnt;
243 243
244 if (++printed > print_entries || syme->snap_count < count_filter) 244 if (++printed > print_entries || syme->snap_count < count_filter)
@@ -247,29 +247,20 @@ static void print_sym_table(void)
247 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) / 247 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
248 sum_ksamples)); 248 sum_ksamples));
249 249
250 /*
251 * We color high-overhead entries in red, mid-overhead
252 * entries in green - and keep the low overhead places
253 * normal:
254 */
255 if (pcnt >= 5.0) {
256 color = PERF_COLOR_RED;
257 } else {
258 if (pcnt >= 0.5)
259 color = PERF_COLOR_GREEN;
260 }
261
262 if (nr_counters == 1) 250 if (nr_counters == 1)
263 printf("%20.2f - ", syme->weight); 251 printf("%20.2f - ", syme->weight);
264 else 252 else
265 printf("%9.1f %10ld - ", syme->weight, syme->snap_count); 253 printf("%9.1f %10ld - ", syme->weight, syme->snap_count);
266 254
267 color_fprintf(stdout, color, "%4.1f%%", pcnt); 255 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
268 printf(" - %016llx : %s\n", sym->start, sym->name); 256 printf(" - %016llx : %s", sym->start, sym->name);
257 if (sym->module)
258 printf("\t[%s]", sym->module->name);
259 printf("\n");
269 } 260 }
270} 261}
271 262
272static void *display_thread(void *arg) 263static void *display_thread(void *arg __used)
273{ 264{
274 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN }; 265 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
275 int delay_msecs = delay_secs * 1000; 266 int delay_msecs = delay_secs * 1000;
@@ -286,11 +277,31 @@ static void *display_thread(void *arg)
286 return NULL; 277 return NULL;
287} 278}
288 279
280/* Tag samples to be skipped. */
281static const char *skip_symbols[] = {
282 "default_idle",
283 "cpu_idle",
284 "enter_idle",
285 "exit_idle",
286 "mwait_idle",
287 "ppc64_runlatch_off",
288 "pseries_dedicated_idle_sleep",
289 NULL
290};
291
289static int symbol_filter(struct dso *self, struct symbol *sym) 292static int symbol_filter(struct dso *self, struct symbol *sym)
290{ 293{
291 static int filter_match; 294 static int filter_match;
292 struct sym_entry *syme; 295 struct sym_entry *syme;
293 const char *name = sym->name; 296 const char *name = sym->name;
297 int i;
298
299 /*
300 * ppc64 uses function descriptors and appends a '.' to the
301 * start of every instruction address. Remove it.
302 */
303 if (name[0] == '.')
304 name++;
294 305
295 if (!strcmp(name, "_text") || 306 if (!strcmp(name, "_text") ||
296 !strcmp(name, "_etext") || 307 !strcmp(name, "_etext") ||
@@ -302,13 +313,12 @@ static int symbol_filter(struct dso *self, struct symbol *sym)
302 return 1; 313 return 1;
303 314
304 syme = dso__sym_priv(self, sym); 315 syme = dso__sym_priv(self, sym);
305 /* Tag samples to be skipped. */ 316 for (i = 0; skip_symbols[i]; i++) {
306 if (!strcmp("default_idle", name) || 317 if (!strcmp(skip_symbols[i], name)) {
307 !strcmp("cpu_idle", name) || 318 syme->skip = 1;
308 !strcmp("enter_idle", name) || 319 break;
309 !strcmp("exit_idle", name) || 320 }
310 !strcmp("mwait_idle", name)) 321 }
311 syme->skip = 1;
312 322
313 if (filter_match == 1) { 323 if (filter_match == 1) {
314 filter_end = sym->start; 324 filter_end = sym->start;
@@ -340,12 +350,13 @@ static int parse_symbols(void)
340{ 350{
341 struct rb_node *node; 351 struct rb_node *node;
342 struct symbol *sym; 352 struct symbol *sym;
353 int modules = vmlinux ? 1 : 0;
343 354
344 kernel_dso = dso__new("[kernel]", sizeof(struct sym_entry)); 355 kernel_dso = dso__new("[kernel]", sizeof(struct sym_entry));
345 if (kernel_dso == NULL) 356 if (kernel_dso == NULL)
346 return -1; 357 return -1;
347 358
348 if (dso__load_kernel(kernel_dso, NULL, symbol_filter, 1) != 0) 359 if (dso__load_kernel(kernel_dso, vmlinux, symbol_filter, verbose, modules) <= 0)
349 goto out_delete_dso; 360 goto out_delete_dso;
350 361
351 node = rb_first(&kernel_dso->syms); 362 node = rb_first(&kernel_dso->syms);
@@ -392,11 +403,11 @@ static void record_ip(u64 ip, int counter)
392 samples--; 403 samples--;
393} 404}
394 405
395static void process_event(u64 ip, int counter) 406static void process_event(u64 ip, int counter, int user)
396{ 407{
397 samples++; 408 samples++;
398 409
399 if (ip < min_ip || ip > max_ip) { 410 if (user) {
400 userspace_samples++; 411 userspace_samples++;
401 return; 412 return;
402 } 413 }
@@ -407,7 +418,7 @@ static void process_event(u64 ip, int counter)
407struct mmap_data { 418struct mmap_data {
408 int counter; 419 int counter;
409 void *base; 420 void *base;
410 unsigned int mask; 421 int mask;
411 unsigned int prev; 422 unsigned int prev;
412}; 423};
413 424
@@ -509,9 +520,10 @@ static void mmap_read_counter(struct mmap_data *md)
509 520
510 old += size; 521 old += size;
511 522
512 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) { 523 if (event->header.type == PERF_EVENT_SAMPLE) {
513 if (event->header.type & PERF_SAMPLE_IP) 524 int user =
514 process_event(event->ip.ip, md->counter); 525 (event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK) == PERF_EVENT_MISC_USER;
526 process_event(event->ip.ip, md->counter, user);
515 } 527 }
516 } 528 }
517 529
@@ -660,6 +672,7 @@ static const struct option options[] = {
660 "system-wide collection from all CPUs"), 672 "system-wide collection from all CPUs"),
661 OPT_INTEGER('C', "CPU", &profile_cpu, 673 OPT_INTEGER('C', "CPU", &profile_cpu,
662 "CPU to profile on"), 674 "CPU to profile on"),
675 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
663 OPT_INTEGER('m', "mmap-pages", &mmap_pages, 676 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
664 "number of mmap data pages"), 677 "number of mmap data pages"),
665 OPT_INTEGER('r', "realtime", &realtime_prio, 678 OPT_INTEGER('r', "realtime", &realtime_prio,
@@ -674,7 +687,7 @@ static const struct option options[] = {
674 "put the counters into a counter group"), 687 "put the counters into a counter group"),
675 OPT_STRING('s', "sym-filter", &sym_filter, "pattern", 688 OPT_STRING('s', "sym-filter", &sym_filter, "pattern",
676 "only display symbols matchig this pattern"), 689 "only display symbols matchig this pattern"),
677 OPT_BOOLEAN('z', "zero", &group, 690 OPT_BOOLEAN('z', "zero", &zero,
678 "zero history across updates"), 691 "zero history across updates"),
679 OPT_INTEGER('F', "freq", &freq, 692 OPT_INTEGER('F', "freq", &freq,
680 "profile at this frequency"), 693 "profile at this frequency"),
@@ -685,10 +698,12 @@ static const struct option options[] = {
685 OPT_END() 698 OPT_END()
686}; 699};
687 700
688int cmd_top(int argc, const char **argv, const char *prefix) 701int cmd_top(int argc, const char **argv, const char *prefix __used)
689{ 702{
690 int counter; 703 int counter;
691 704
705 symbol__init();
706
692 page_size = sysconf(_SC_PAGE_SIZE); 707 page_size = sysconf(_SC_PAGE_SIZE);
693 708
694 argc = parse_options(argc, argv, options, top_usage, 0); 709 argc = parse_options(argc, argv, options, top_usage, 0);
diff --git a/tools/perf/perf.c b/tools/perf/perf.c
index 4eb725933703..c5656784c61d 100644
--- a/tools/perf/perf.c
+++ b/tools/perf/perf.c
@@ -229,9 +229,6 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
229 use_pager = 1; 229 use_pager = 1;
230 commit_pager_choice(); 230 commit_pager_choice();
231 231
232 if (p->option & NEED_WORK_TREE)
233 /* setup_work_tree() */;
234
235 status = p->fn(argc, argv, prefix); 232 status = p->fn(argc, argv, prefix);
236 if (status) 233 if (status)
237 return status & 0xff; 234 return status & 0xff;
@@ -266,7 +263,7 @@ static void handle_internal_command(int argc, const char **argv)
266 { "annotate", cmd_annotate, 0 }, 263 { "annotate", cmd_annotate, 0 },
267 { "version", cmd_version, 0 }, 264 { "version", cmd_version, 0 },
268 }; 265 };
269 int i; 266 unsigned int i;
270 static const char ext[] = STRIP_EXTENSION; 267 static const char ext[] = STRIP_EXTENSION;
271 268
272 if (sizeof(ext) > 1) { 269 if (sizeof(ext) > 1) {
diff --git a/tools/perf/perf.h b/tools/perf/perf.h
index ceb68aa51f7f..63e67cc5487b 100644
--- a/tools/perf/perf.h
+++ b/tools/perf/perf.h
@@ -19,13 +19,29 @@
19#define cpu_relax() asm volatile("" ::: "memory"); 19#define cpu_relax() asm volatile("" ::: "memory");
20#endif 20#endif
21 21
22#ifdef __sh__
23#include "../../arch/sh/include/asm/unistd.h"
24#if defined(__SH4A__) || defined(__SH5__)
25# define rmb() asm volatile("synco" ::: "memory")
26#else
27# define rmb() asm volatile("" ::: "memory")
28#endif
29#define cpu_relax() asm volatile("" ::: "memory")
30#endif
31
32#ifdef __hppa__
33#include "../../arch/parisc/include/asm/unistd.h"
34#define rmb() asm volatile("" ::: "memory")
35#define cpu_relax() asm volatile("" ::: "memory");
36#endif
37
22#include <time.h> 38#include <time.h>
23#include <unistd.h> 39#include <unistd.h>
24#include <sys/types.h> 40#include <sys/types.h>
25#include <sys/syscall.h> 41#include <sys/syscall.h>
26 42
27#include "../../include/linux/perf_counter.h" 43#include "../../include/linux/perf_counter.h"
28#include "types.h" 44#include "util/types.h"
29 45
30/* 46/*
31 * prctl(PR_TASK_PERF_COUNTERS_DISABLE) will (cheaply) disable all 47 * prctl(PR_TASK_PERF_COUNTERS_DISABLE) will (cheaply) disable all
@@ -52,6 +68,8 @@ static inline unsigned long long rdclock(void)
52#define __user 68#define __user
53#define asmlinkage 69#define asmlinkage
54 70
71#define __used __attribute__((__unused__))
72
55#define unlikely(x) __builtin_expect(!!(x), 0) 73#define unlikely(x) __builtin_expect(!!(x), 0)
56#define min(x, y) ({ \ 74#define min(x, y) ({ \
57 typeof(x) _min1 = (x); \ 75 typeof(x) _min1 = (x); \
@@ -72,10 +90,9 @@ sys_perf_counter_open(struct perf_counter_attr *attr,
72#define MAX_COUNTERS 256 90#define MAX_COUNTERS 256
73#define MAX_NR_CPUS 256 91#define MAX_NR_CPUS 256
74 92
75struct perf_file_header { 93struct ip_callchain {
76 u64 version; 94 u64 nr;
77 u64 sample_type; 95 u64 ips[0];
78 u64 data_size;
79}; 96};
80 97
81#endif 98#endif
diff --git a/tools/perf/util/alias.c b/tools/perf/util/alias.c
index 9b3dd2b428df..b8144e80bb1e 100644
--- a/tools/perf/util/alias.c
+++ b/tools/perf/util/alias.c
@@ -3,7 +3,7 @@
3static const char *alias_key; 3static const char *alias_key;
4static char *alias_val; 4static char *alias_val;
5 5
6static int alias_lookup_cb(const char *k, const char *v, void *cb) 6static int alias_lookup_cb(const char *k, const char *v, void *cb __used)
7{ 7{
8 if (!prefixcmp(k, "alias.") && !strcmp(k+6, alias_key)) { 8 if (!prefixcmp(k, "alias.") && !strcmp(k+6, alias_key)) {
9 if (!v) 9 if (!v)
diff --git a/tools/perf/util/cache.h b/tools/perf/util/cache.h
index 393d6146d13b..161d5f413e28 100644
--- a/tools/perf/util/cache.h
+++ b/tools/perf/util/cache.h
@@ -3,6 +3,7 @@
3 3
4#include "util.h" 4#include "util.h"
5#include "strbuf.h" 5#include "strbuf.h"
6#include "../perf.h"
6 7
7#define PERF_DIR_ENVIRONMENT "PERF_DIR" 8#define PERF_DIR_ENVIRONMENT "PERF_DIR"
8#define PERF_WORK_TREE_ENVIRONMENT "PERF_WORK_TREE" 9#define PERF_WORK_TREE_ENVIRONMENT "PERF_WORK_TREE"
diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c
new file mode 100644
index 000000000000..9d3c8141b8c1
--- /dev/null
+++ b/tools/perf/util/callchain.c
@@ -0,0 +1,335 @@
1/*
2 * Copyright (C) 2009, Frederic Weisbecker <fweisbec@gmail.com>
3 *
4 * Handle the callchains from the stream in an ad-hoc radix tree and then
5 * sort them in an rbtree.
6 *
7 * Using a radix for code path provides a fast retrieval and factorizes
8 * memory use. Also that lets us use the paths in a hierarchical graph view.
9 *
10 */
11
12#include <stdlib.h>
13#include <stdio.h>
14#include <stdbool.h>
15#include <errno.h>
16
17#include "callchain.h"
18
19#define chain_for_each_child(child, parent) \
20 list_for_each_entry(child, &parent->children, brothers)
21
22static void
23rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
24 enum chain_mode mode)
25{
26 struct rb_node **p = &root->rb_node;
27 struct rb_node *parent = NULL;
28 struct callchain_node *rnode;
29
30 while (*p) {
31 parent = *p;
32 rnode = rb_entry(parent, struct callchain_node, rb_node);
33
34 switch (mode) {
35 case CHAIN_FLAT:
36 if (rnode->hit < chain->hit)
37 p = &(*p)->rb_left;
38 else
39 p = &(*p)->rb_right;
40 break;
41 case CHAIN_GRAPH_ABS: /* Falldown */
42 case CHAIN_GRAPH_REL:
43 if (rnode->cumul_hit < chain->cumul_hit)
44 p = &(*p)->rb_left;
45 else
46 p = &(*p)->rb_right;
47 break;
48 default:
49 break;
50 }
51 }
52
53 rb_link_node(&chain->rb_node, parent, p);
54 rb_insert_color(&chain->rb_node, root);
55}
56
57static void
58__sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
59 u64 min_hit)
60{
61 struct callchain_node *child;
62
63 chain_for_each_child(child, node)
64 __sort_chain_flat(rb_root, child, min_hit);
65
66 if (node->hit && node->hit >= min_hit)
67 rb_insert_callchain(rb_root, node, CHAIN_FLAT);
68}
69
70/*
71 * Once we get every callchains from the stream, we can now
72 * sort them by hit
73 */
74static void
75sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
76 u64 min_hit, struct callchain_param *param __used)
77{
78 __sort_chain_flat(rb_root, node, min_hit);
79}
80
81static void __sort_chain_graph_abs(struct callchain_node *node,
82 u64 min_hit)
83{
84 struct callchain_node *child;
85
86 node->rb_root = RB_ROOT;
87
88 chain_for_each_child(child, node) {
89 __sort_chain_graph_abs(child, min_hit);
90 if (child->cumul_hit >= min_hit)
91 rb_insert_callchain(&node->rb_root, child,
92 CHAIN_GRAPH_ABS);
93 }
94}
95
96static void
97sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_node *chain_root,
98 u64 min_hit, struct callchain_param *param __used)
99{
100 __sort_chain_graph_abs(chain_root, min_hit);
101 rb_root->rb_node = chain_root->rb_root.rb_node;
102}
103
104static void __sort_chain_graph_rel(struct callchain_node *node,
105 double min_percent)
106{
107 struct callchain_node *child;
108 u64 min_hit;
109
110 node->rb_root = RB_ROOT;
111 min_hit = node->cumul_hit * min_percent / 100.0;
112
113 chain_for_each_child(child, node) {
114 __sort_chain_graph_rel(child, min_percent);
115 if (child->cumul_hit >= min_hit)
116 rb_insert_callchain(&node->rb_root, child,
117 CHAIN_GRAPH_REL);
118 }
119}
120
121static void
122sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_node *chain_root,
123 u64 min_hit __used, struct callchain_param *param)
124{
125 __sort_chain_graph_rel(chain_root, param->min_percent);
126 rb_root->rb_node = chain_root->rb_root.rb_node;
127}
128
129int register_callchain_param(struct callchain_param *param)
130{
131 switch (param->mode) {
132 case CHAIN_GRAPH_ABS:
133 param->sort = sort_chain_graph_abs;
134 break;
135 case CHAIN_GRAPH_REL:
136 param->sort = sort_chain_graph_rel;
137 break;
138 case CHAIN_FLAT:
139 param->sort = sort_chain_flat;
140 break;
141 default:
142 return -1;
143 }
144 return 0;
145}
146
147/*
148 * Create a child for a parent. If inherit_children, then the new child
149 * will become the new parent of it's parent children
150 */
151static struct callchain_node *
152create_child(struct callchain_node *parent, bool inherit_children)
153{
154 struct callchain_node *new;
155
156 new = malloc(sizeof(*new));
157 if (!new) {
158 perror("not enough memory to create child for code path tree");
159 return NULL;
160 }
161 new->parent = parent;
162 INIT_LIST_HEAD(&new->children);
163 INIT_LIST_HEAD(&new->val);
164
165 if (inherit_children) {
166 struct callchain_node *next;
167
168 list_splice(&parent->children, &new->children);
169 INIT_LIST_HEAD(&parent->children);
170
171 chain_for_each_child(next, new)
172 next->parent = new;
173 }
174 list_add_tail(&new->brothers, &parent->children);
175
176 return new;
177}
178
179/*
180 * Fill the node with callchain values
181 */
182static void
183fill_node(struct callchain_node *node, struct ip_callchain *chain,
184 int start, struct symbol **syms)
185{
186 unsigned int i;
187
188 for (i = start; i < chain->nr; i++) {
189 struct callchain_list *call;
190
191 call = malloc(sizeof(*call));
192 if (!call) {
193 perror("not enough memory for the code path tree");
194 return;
195 }
196 call->ip = chain->ips[i];
197 call->sym = syms[i];
198 list_add_tail(&call->list, &node->val);
199 }
200 node->val_nr = chain->nr - start;
201 if (!node->val_nr)
202 printf("Warning: empty node in callchain tree\n");
203}
204
205static void
206add_child(struct callchain_node *parent, struct ip_callchain *chain,
207 int start, struct symbol **syms)
208{
209 struct callchain_node *new;
210
211 new = create_child(parent, false);
212 fill_node(new, chain, start, syms);
213
214 new->cumul_hit = new->hit = 1;
215}
216
217/*
218 * Split the parent in two parts (a new child is created) and
219 * give a part of its callchain to the created child.
220 * Then create another child to host the given callchain of new branch
221 */
222static void
223split_add_child(struct callchain_node *parent, struct ip_callchain *chain,
224 struct callchain_list *to_split, int idx_parents, int idx_local,
225 struct symbol **syms)
226{
227 struct callchain_node *new;
228 struct list_head *old_tail;
229 unsigned int idx_total = idx_parents + idx_local;
230
231 /* split */
232 new = create_child(parent, true);
233
234 /* split the callchain and move a part to the new child */
235 old_tail = parent->val.prev;
236 list_del_range(&to_split->list, old_tail);
237 new->val.next = &to_split->list;
238 new->val.prev = old_tail;
239 to_split->list.prev = &new->val;
240 old_tail->next = &new->val;
241
242 /* split the hits */
243 new->hit = parent->hit;
244 new->cumul_hit = parent->cumul_hit;
245 new->val_nr = parent->val_nr - idx_local;
246 parent->val_nr = idx_local;
247
248 /* create a new child for the new branch if any */
249 if (idx_total < chain->nr) {
250 parent->hit = 0;
251 add_child(parent, chain, idx_total, syms);
252 } else {
253 parent->hit = 1;
254 }
255}
256
257static int
258__append_chain(struct callchain_node *root, struct ip_callchain *chain,
259 unsigned int start, struct symbol **syms);
260
261static void
262__append_chain_children(struct callchain_node *root, struct ip_callchain *chain,
263 struct symbol **syms, unsigned int start)
264{
265 struct callchain_node *rnode;
266
267 /* lookup in childrens */
268 chain_for_each_child(rnode, root) {
269 unsigned int ret = __append_chain(rnode, chain, start, syms);
270
271 if (!ret)
272 goto cumul;
273 }
274 /* nothing in children, add to the current node */
275 add_child(root, chain, start, syms);
276
277cumul:
278 root->cumul_hit++;
279}
280
281static int
282__append_chain(struct callchain_node *root, struct ip_callchain *chain,
283 unsigned int start, struct symbol **syms)
284{
285 struct callchain_list *cnode;
286 unsigned int i = start;
287 bool found = false;
288
289 /*
290 * Lookup in the current node
291 * If we have a symbol, then compare the start to match
292 * anywhere inside a function.
293 */
294 list_for_each_entry(cnode, &root->val, list) {
295 if (i == chain->nr)
296 break;
297 if (cnode->sym && syms[i]) {
298 if (cnode->sym->start != syms[i]->start)
299 break;
300 } else if (cnode->ip != chain->ips[i])
301 break;
302 if (!found)
303 found = true;
304 i++;
305 }
306
307 /* matches not, relay on the parent */
308 if (!found)
309 return -1;
310
311 /* we match only a part of the node. Split it and add the new chain */
312 if (i - start < root->val_nr) {
313 split_add_child(root, chain, cnode, start, i - start, syms);
314 return 0;
315 }
316
317 /* we match 100% of the path, increment the hit */
318 if (i - start == root->val_nr && i == chain->nr) {
319 root->hit++;
320 root->cumul_hit++;
321
322 return 0;
323 }
324
325 /* We match the node and still have a part remaining */
326 __append_chain_children(root, chain, syms, i);
327
328 return 0;
329}
330
331void append_chain(struct callchain_node *root, struct ip_callchain *chain,
332 struct symbol **syms)
333{
334 __append_chain_children(root, chain, syms, 0);
335}
diff --git a/tools/perf/util/callchain.h b/tools/perf/util/callchain.h
new file mode 100644
index 000000000000..7812122bea1d
--- /dev/null
+++ b/tools/perf/util/callchain.h
@@ -0,0 +1,54 @@
1#ifndef __PERF_CALLCHAIN_H
2#define __PERF_CALLCHAIN_H
3
4#include "../perf.h"
5#include <linux/list.h>
6#include <linux/rbtree.h>
7#include "symbol.h"
8
9enum chain_mode {
10 CHAIN_FLAT,
11 CHAIN_GRAPH_ABS,
12 CHAIN_GRAPH_REL
13};
14
15struct callchain_node {
16 struct callchain_node *parent;
17 struct list_head brothers;
18 struct list_head children;
19 struct list_head val;
20 struct rb_node rb_node; /* to sort nodes in an rbtree */
21 struct rb_root rb_root; /* sorted tree of children */
22 unsigned int val_nr;
23 u64 hit;
24 u64 cumul_hit; /* hit + hits of children */
25};
26
27struct callchain_param;
28
29typedef void (*sort_chain_func_t)(struct rb_root *, struct callchain_node *,
30 u64, struct callchain_param *);
31
32struct callchain_param {
33 enum chain_mode mode;
34 double min_percent;
35 sort_chain_func_t sort;
36};
37
38struct callchain_list {
39 u64 ip;
40 struct symbol *sym;
41 struct list_head list;
42};
43
44static inline void callchain_init(struct callchain_node *node)
45{
46 INIT_LIST_HEAD(&node->brothers);
47 INIT_LIST_HEAD(&node->children);
48 INIT_LIST_HEAD(&node->val);
49}
50
51int register_callchain_param(struct callchain_param *param);
52void append_chain(struct callchain_node *root, struct ip_callchain *chain,
53 struct symbol **syms);
54#endif
diff --git a/tools/perf/util/color.c b/tools/perf/util/color.c
index 9a8c20ccc53e..90a044d1fe7d 100644
--- a/tools/perf/util/color.c
+++ b/tools/perf/util/color.c
@@ -11,7 +11,8 @@ static int parse_color(const char *name, int len)
11 }; 11 };
12 char *end; 12 char *end;
13 int i; 13 int i;
14 for (i = 0; i < ARRAY_SIZE(color_names); i++) { 14
15 for (i = 0; i < (int)ARRAY_SIZE(color_names); i++) {
15 const char *str = color_names[i]; 16 const char *str = color_names[i];
16 if (!strncasecmp(name, str, len) && !str[len]) 17 if (!strncasecmp(name, str, len) && !str[len])
17 return i - 1; 18 return i - 1;
@@ -28,7 +29,8 @@ static int parse_attr(const char *name, int len)
28 static const char * const attr_names[] = { 29 static const char * const attr_names[] = {
29 "bold", "dim", "ul", "blink", "reverse" 30 "bold", "dim", "ul", "blink", "reverse"
30 }; 31 };
31 int i; 32 unsigned int i;
33
32 for (i = 0; i < ARRAY_SIZE(attr_names); i++) { 34 for (i = 0; i < ARRAY_SIZE(attr_names); i++) {
33 const char *str = attr_names[i]; 35 const char *str = attr_names[i];
34 if (!strncasecmp(name, str, len) && !str[len]) 36 if (!strncasecmp(name, str, len) && !str[len])
@@ -222,10 +224,12 @@ int color_fwrite_lines(FILE *fp, const char *color,
222{ 224{
223 if (!*color) 225 if (!*color)
224 return fwrite(buf, count, 1, fp) != 1; 226 return fwrite(buf, count, 1, fp) != 1;
227
225 while (count) { 228 while (count) {
226 char *p = memchr(buf, '\n', count); 229 char *p = memchr(buf, '\n', count);
230
227 if (p != buf && (fputs(color, fp) < 0 || 231 if (p != buf && (fputs(color, fp) < 0 ||
228 fwrite(buf, p ? p - buf : count, 1, fp) != 1 || 232 fwrite(buf, p ? (size_t)(p - buf) : count, 1, fp) != 1 ||
229 fputs(PERF_COLOR_RESET, fp) < 0)) 233 fputs(PERF_COLOR_RESET, fp) < 0))
230 return -1; 234 return -1;
231 if (!p) 235 if (!p)
@@ -238,4 +242,31 @@ int color_fwrite_lines(FILE *fp, const char *color,
238 return 0; 242 return 0;
239} 243}
240 244
245char *get_percent_color(double percent)
246{
247 char *color = PERF_COLOR_NORMAL;
241 248
249 /*
250 * We color high-overhead entries in red, mid-overhead
251 * entries in green - and keep the low overhead places
252 * normal:
253 */
254 if (percent >= MIN_RED)
255 color = PERF_COLOR_RED;
256 else {
257 if (percent > MIN_GREEN)
258 color = PERF_COLOR_GREEN;
259 }
260 return color;
261}
262
263int percent_color_fprintf(FILE *fp, const char *fmt, double percent)
264{
265 int r;
266 char *color;
267
268 color = get_percent_color(percent);
269 r = color_fprintf(fp, color, fmt, percent);
270
271 return r;
272}
diff --git a/tools/perf/util/color.h b/tools/perf/util/color.h
index 5abfd379582b..706cec50bd25 100644
--- a/tools/perf/util/color.h
+++ b/tools/perf/util/color.h
@@ -15,6 +15,9 @@
15#define PERF_COLOR_CYAN "\033[36m" 15#define PERF_COLOR_CYAN "\033[36m"
16#define PERF_COLOR_BG_RED "\033[41m" 16#define PERF_COLOR_BG_RED "\033[41m"
17 17
18#define MIN_GREEN 0.5
19#define MIN_RED 5.0
20
18/* 21/*
19 * This variable stores the value of color.ui 22 * This variable stores the value of color.ui
20 */ 23 */
@@ -32,5 +35,7 @@ void color_parse_mem(const char *value, int len, const char *var, char *dst);
32int color_fprintf(FILE *fp, const char *color, const char *fmt, ...); 35int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);
33int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...); 36int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...);
34int color_fwrite_lines(FILE *fp, const char *color, size_t count, const char *buf); 37int color_fwrite_lines(FILE *fp, const char *color, size_t count, const char *buf);
38int percent_color_fprintf(FILE *fp, const char *fmt, double percent);
39char *get_percent_color(double percent);
35 40
36#endif /* COLOR_H */ 41#endif /* COLOR_H */
diff --git a/tools/perf/util/config.c b/tools/perf/util/config.c
index 3dd13faa6a27..780df541006d 100644
--- a/tools/perf/util/config.c
+++ b/tools/perf/util/config.c
@@ -47,10 +47,12 @@ static int get_next_char(void)
47static char *parse_value(void) 47static char *parse_value(void)
48{ 48{
49 static char value[1024]; 49 static char value[1024];
50 int quote = 0, comment = 0, len = 0, space = 0; 50 int quote = 0, comment = 0, space = 0;
51 size_t len = 0;
51 52
52 for (;;) { 53 for (;;) {
53 int c = get_next_char(); 54 int c = get_next_char();
55
54 if (len >= sizeof(value) - 1) 56 if (len >= sizeof(value) - 1)
55 return NULL; 57 return NULL;
56 if (c == '\n') { 58 if (c == '\n') {
@@ -353,13 +355,13 @@ int perf_config_string(const char **dest, const char *var, const char *value)
353 return 0; 355 return 0;
354} 356}
355 357
356static int perf_default_core_config(const char *var, const char *value) 358static int perf_default_core_config(const char *var __used, const char *value __used)
357{ 359{
358 /* Add other config variables here and to Documentation/config.txt. */ 360 /* Add other config variables here and to Documentation/config.txt. */
359 return 0; 361 return 0;
360} 362}
361 363
362int perf_default_config(const char *var, const char *value, void *dummy) 364int perf_default_config(const char *var, const char *value, void *dummy __used)
363{ 365{
364 if (!prefixcmp(var, "core.")) 366 if (!prefixcmp(var, "core."))
365 return perf_default_core_config(var, value); 367 return perf_default_core_config(var, value);
@@ -471,10 +473,10 @@ static int matches(const char* key, const char* value)
471 !regexec(store.value_regex, value, 0, NULL, 0))); 473 !regexec(store.value_regex, value, 0, NULL, 0)));
472} 474}
473 475
474static int store_aux(const char* key, const char* value, void *cb) 476static int store_aux(const char* key, const char* value, void *cb __used)
475{ 477{
478 int section_len;
476 const char *ep; 479 const char *ep;
477 size_t section_len;
478 480
479 switch (store.state) { 481 switch (store.state) {
480 case KEY_SEEN: 482 case KEY_SEEN:
@@ -551,7 +553,7 @@ static int store_write_section(int fd, const char* key)
551 strbuf_addf(&sb, "[%.*s]\n", store.baselen, key); 553 strbuf_addf(&sb, "[%.*s]\n", store.baselen, key);
552 } 554 }
553 555
554 success = write_in_full(fd, sb.buf, sb.len) == sb.len; 556 success = (write_in_full(fd, sb.buf, sb.len) == (ssize_t)sb.len);
555 strbuf_release(&sb); 557 strbuf_release(&sb);
556 558
557 return success; 559 return success;
@@ -599,7 +601,7 @@ static int store_write_pair(int fd, const char* key, const char* value)
599 } 601 }
600 strbuf_addf(&sb, "%s\n", quote); 602 strbuf_addf(&sb, "%s\n", quote);
601 603
602 success = write_in_full(fd, sb.buf, sb.len) == sb.len; 604 success = (write_in_full(fd, sb.buf, sb.len) == (ssize_t)sb.len);
603 strbuf_release(&sb); 605 strbuf_release(&sb);
604 606
605 return success; 607 return success;
@@ -741,7 +743,7 @@ int perf_config_set_multivar(const char* key, const char* value,
741 } else { 743 } else {
742 struct stat st; 744 struct stat st;
743 char* contents; 745 char* contents;
744 size_t contents_sz, copy_begin, copy_end; 746 ssize_t contents_sz, copy_begin, copy_end;
745 int i, new_line = 0; 747 int i, new_line = 0;
746 748
747 if (value_regex == NULL) 749 if (value_regex == NULL)
diff --git a/tools/perf/util/exec_cmd.c b/tools/perf/util/exec_cmd.c
index d39292263153..34a352867382 100644
--- a/tools/perf/util/exec_cmd.c
+++ b/tools/perf/util/exec_cmd.c
@@ -1,6 +1,9 @@
1#include "cache.h" 1#include "cache.h"
2#include "exec_cmd.h" 2#include "exec_cmd.h"
3#include "quote.h" 3#include "quote.h"
4
5#include <string.h>
6
4#define MAX_ARGS 32 7#define MAX_ARGS 32
5 8
6extern char **environ; 9extern char **environ;
@@ -51,7 +54,7 @@ const char *perf_extract_argv0_path(const char *argv0)
51 slash--; 54 slash--;
52 55
53 if (slash >= argv0) { 56 if (slash >= argv0) {
54 argv0_path = strndup(argv0, slash - argv0); 57 argv0_path = xstrndup(argv0, slash - argv0);
55 return slash + 1; 58 return slash + 1;
56 } 59 }
57 60
diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
new file mode 100644
index 000000000000..450384b3bbe5
--- /dev/null
+++ b/tools/perf/util/header.c
@@ -0,0 +1,242 @@
1#include <sys/types.h>
2#include <unistd.h>
3#include <stdio.h>
4#include <stdlib.h>
5
6#include "util.h"
7#include "header.h"
8
9/*
10 *
11 */
12
13struct perf_header_attr *perf_header_attr__new(struct perf_counter_attr *attr)
14{
15 struct perf_header_attr *self = malloc(sizeof(*self));
16
17 if (!self)
18 die("nomem");
19
20 self->attr = *attr;
21 self->ids = 0;
22 self->size = 1;
23 self->id = malloc(sizeof(u64));
24
25 if (!self->id)
26 die("nomem");
27
28 return self;
29}
30
31void perf_header_attr__add_id(struct perf_header_attr *self, u64 id)
32{
33 int pos = self->ids;
34
35 self->ids++;
36 if (self->ids > self->size) {
37 self->size *= 2;
38 self->id = realloc(self->id, self->size * sizeof(u64));
39 if (!self->id)
40 die("nomem");
41 }
42 self->id[pos] = id;
43}
44
45/*
46 *
47 */
48
49struct perf_header *perf_header__new(void)
50{
51 struct perf_header *self = malloc(sizeof(*self));
52
53 if (!self)
54 die("nomem");
55
56 self->frozen = 0;
57
58 self->attrs = 0;
59 self->size = 1;
60 self->attr = malloc(sizeof(void *));
61
62 if (!self->attr)
63 die("nomem");
64
65 self->data_offset = 0;
66 self->data_size = 0;
67
68 return self;
69}
70
71void perf_header__add_attr(struct perf_header *self,
72 struct perf_header_attr *attr)
73{
74 int pos = self->attrs;
75
76 if (self->frozen)
77 die("frozen");
78
79 self->attrs++;
80 if (self->attrs > self->size) {
81 self->size *= 2;
82 self->attr = realloc(self->attr, self->size * sizeof(void *));
83 if (!self->attr)
84 die("nomem");
85 }
86 self->attr[pos] = attr;
87}
88
89static const char *__perf_magic = "PERFFILE";
90
91#define PERF_MAGIC (*(u64 *)__perf_magic)
92
93struct perf_file_section {
94 u64 offset;
95 u64 size;
96};
97
98struct perf_file_attr {
99 struct perf_counter_attr attr;
100 struct perf_file_section ids;
101};
102
103struct perf_file_header {
104 u64 magic;
105 u64 size;
106 u64 attr_size;
107 struct perf_file_section attrs;
108 struct perf_file_section data;
109};
110
111static void do_write(int fd, void *buf, size_t size)
112{
113 while (size) {
114 int ret = write(fd, buf, size);
115
116 if (ret < 0)
117 die("failed to write");
118
119 size -= ret;
120 buf += ret;
121 }
122}
123
124void perf_header__write(struct perf_header *self, int fd)
125{
126 struct perf_file_header f_header;
127 struct perf_file_attr f_attr;
128 struct perf_header_attr *attr;
129 int i;
130
131 lseek(fd, sizeof(f_header), SEEK_SET);
132
133
134 for (i = 0; i < self->attrs; i++) {
135 attr = self->attr[i];
136
137 attr->id_offset = lseek(fd, 0, SEEK_CUR);
138 do_write(fd, attr->id, attr->ids * sizeof(u64));
139 }
140
141
142 self->attr_offset = lseek(fd, 0, SEEK_CUR);
143
144 for (i = 0; i < self->attrs; i++) {
145 attr = self->attr[i];
146
147 f_attr = (struct perf_file_attr){
148 .attr = attr->attr,
149 .ids = {
150 .offset = attr->id_offset,
151 .size = attr->ids * sizeof(u64),
152 }
153 };
154 do_write(fd, &f_attr, sizeof(f_attr));
155 }
156
157
158 self->data_offset = lseek(fd, 0, SEEK_CUR);
159
160 f_header = (struct perf_file_header){
161 .magic = PERF_MAGIC,
162 .size = sizeof(f_header),
163 .attr_size = sizeof(f_attr),
164 .attrs = {
165 .offset = self->attr_offset,
166 .size = self->attrs * sizeof(f_attr),
167 },
168 .data = {
169 .offset = self->data_offset,
170 .size = self->data_size,
171 },
172 };
173
174 lseek(fd, 0, SEEK_SET);
175 do_write(fd, &f_header, sizeof(f_header));
176 lseek(fd, self->data_offset + self->data_size, SEEK_SET);
177
178 self->frozen = 1;
179}
180
181static void do_read(int fd, void *buf, size_t size)
182{
183 while (size) {
184 int ret = read(fd, buf, size);
185
186 if (ret < 0)
187 die("failed to read");
188
189 size -= ret;
190 buf += ret;
191 }
192}
193
194struct perf_header *perf_header__read(int fd)
195{
196 struct perf_header *self = perf_header__new();
197 struct perf_file_header f_header;
198 struct perf_file_attr f_attr;
199 u64 f_id;
200
201 int nr_attrs, nr_ids, i, j;
202
203 lseek(fd, 0, SEEK_SET);
204 do_read(fd, &f_header, sizeof(f_header));
205
206 if (f_header.magic != PERF_MAGIC ||
207 f_header.size != sizeof(f_header) ||
208 f_header.attr_size != sizeof(f_attr))
209 die("incompatible file format");
210
211 nr_attrs = f_header.attrs.size / sizeof(f_attr);
212 lseek(fd, f_header.attrs.offset, SEEK_SET);
213
214 for (i = 0; i < nr_attrs; i++) {
215 struct perf_header_attr *attr;
216 off_t tmp = lseek(fd, 0, SEEK_CUR);
217
218 do_read(fd, &f_attr, sizeof(f_attr));
219
220 attr = perf_header_attr__new(&f_attr.attr);
221
222 nr_ids = f_attr.ids.size / sizeof(u64);
223 lseek(fd, f_attr.ids.offset, SEEK_SET);
224
225 for (j = 0; j < nr_ids; j++) {
226 do_read(fd, &f_id, sizeof(f_id));
227
228 perf_header_attr__add_id(attr, f_id);
229 }
230 perf_header__add_attr(self, attr);
231 lseek(fd, tmp, SEEK_SET);
232 }
233
234 self->data_offset = f_header.data.offset;
235 self->data_size = f_header.data.size;
236
237 lseek(fd, self->data_offset + self->data_size, SEEK_SET);
238
239 self->frozen = 1;
240
241 return self;
242}
diff --git a/tools/perf/util/header.h b/tools/perf/util/header.h
new file mode 100644
index 000000000000..b5ef53ad4c7a
--- /dev/null
+++ b/tools/perf/util/header.h
@@ -0,0 +1,37 @@
1#ifndef _PERF_HEADER_H
2#define _PERF_HEADER_H
3
4#include "../../../include/linux/perf_counter.h"
5#include <sys/types.h>
6#include "types.h"
7
8struct perf_header_attr {
9 struct perf_counter_attr attr;
10 int ids, size;
11 u64 *id;
12 off_t id_offset;
13};
14
15struct perf_header {
16 int frozen;
17 int attrs, size;
18 struct perf_header_attr **attr;
19 off_t attr_offset;
20 u64 data_offset;
21 u64 data_size;
22};
23
24struct perf_header *perf_header__read(int fd);
25void perf_header__write(struct perf_header *self, int fd);
26
27void perf_header__add_attr(struct perf_header *self,
28 struct perf_header_attr *attr);
29
30struct perf_header_attr *
31perf_header_attr__new(struct perf_counter_attr *attr);
32void perf_header_attr__add_id(struct perf_header_attr *self, u64 id);
33
34
35struct perf_header *perf_header__new(void);
36
37#endif /* _PERF_HEADER_H */
diff --git a/tools/perf/util/help.c b/tools/perf/util/help.c
index 6653f7dd1d78..fbb00978b2e2 100644
--- a/tools/perf/util/help.c
+++ b/tools/perf/util/help.c
@@ -26,7 +26,7 @@ static int term_columns(void)
26 return 80; 26 return 80;
27} 27}
28 28
29void add_cmdname(struct cmdnames *cmds, const char *name, int len) 29void add_cmdname(struct cmdnames *cmds, const char *name, size_t len)
30{ 30{
31 struct cmdname *ent = malloc(sizeof(*ent) + len + 1); 31 struct cmdname *ent = malloc(sizeof(*ent) + len + 1);
32 32
@@ -40,7 +40,8 @@ void add_cmdname(struct cmdnames *cmds, const char *name, int len)
40 40
41static void clean_cmdnames(struct cmdnames *cmds) 41static void clean_cmdnames(struct cmdnames *cmds)
42{ 42{
43 int i; 43 unsigned int i;
44
44 for (i = 0; i < cmds->cnt; ++i) 45 for (i = 0; i < cmds->cnt; ++i)
45 free(cmds->names[i]); 46 free(cmds->names[i]);
46 free(cmds->names); 47 free(cmds->names);
@@ -57,7 +58,7 @@ static int cmdname_compare(const void *a_, const void *b_)
57 58
58static void uniq(struct cmdnames *cmds) 59static void uniq(struct cmdnames *cmds)
59{ 60{
60 int i, j; 61 unsigned int i, j;
61 62
62 if (!cmds->cnt) 63 if (!cmds->cnt)
63 return; 64 return;
@@ -71,7 +72,7 @@ static void uniq(struct cmdnames *cmds)
71 72
72void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes) 73void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
73{ 74{
74 int ci, cj, ei; 75 size_t ci, cj, ei;
75 int cmp; 76 int cmp;
76 77
77 ci = cj = ei = 0; 78 ci = cj = ei = 0;
@@ -106,8 +107,9 @@ static void pretty_print_string_list(struct cmdnames *cmds, int longest)
106 printf(" "); 107 printf(" ");
107 108
108 for (j = 0; j < cols; j++) { 109 for (j = 0; j < cols; j++) {
109 int n = j * rows + i; 110 unsigned int n = j * rows + i;
110 int size = space; 111 unsigned int size = space;
112
111 if (n >= cmds->cnt) 113 if (n >= cmds->cnt)
112 break; 114 break;
113 if (j == cols-1 || n + rows >= cmds->cnt) 115 if (j == cols-1 || n + rows >= cmds->cnt)
@@ -126,21 +128,6 @@ static int is_executable(const char *name)
126 !S_ISREG(st.st_mode)) 128 !S_ISREG(st.st_mode))
127 return 0; 129 return 0;
128 130
129#ifdef __MINGW32__
130 /* cannot trust the executable bit, peek into the file instead */
131 char buf[3] = { 0 };
132 int n;
133 int fd = open(name, O_RDONLY);
134 st.st_mode &= ~S_IXUSR;
135 if (fd >= 0) {
136 n = read(fd, buf, 2);
137 if (n == 2)
138 /* DOS executables start with "MZ" */
139 if (!strcmp(buf, "#!") || !strcmp(buf, "MZ"))
140 st.st_mode |= S_IXUSR;
141 close(fd);
142 }
143#endif
144 return st.st_mode & S_IXUSR; 131 return st.st_mode & S_IXUSR;
145} 132}
146 133
@@ -223,7 +210,7 @@ void load_command_list(const char *prefix,
223void list_commands(const char *title, struct cmdnames *main_cmds, 210void list_commands(const char *title, struct cmdnames *main_cmds,
224 struct cmdnames *other_cmds) 211 struct cmdnames *other_cmds)
225{ 212{
226 int i, longest = 0; 213 unsigned int i, longest = 0;
227 214
228 for (i = 0; i < main_cmds->cnt; i++) 215 for (i = 0; i < main_cmds->cnt; i++)
229 if (longest < main_cmds->names[i]->len) 216 if (longest < main_cmds->names[i]->len)
@@ -254,7 +241,8 @@ void list_commands(const char *title, struct cmdnames *main_cmds,
254 241
255int is_in_cmdlist(struct cmdnames *c, const char *s) 242int is_in_cmdlist(struct cmdnames *c, const char *s)
256{ 243{
257 int i; 244 unsigned int i;
245
258 for (i = 0; i < c->cnt; i++) 246 for (i = 0; i < c->cnt; i++)
259 if (!strcmp(s, c->names[i]->name)) 247 if (!strcmp(s, c->names[i]->name))
260 return 1; 248 return 1;
@@ -286,7 +274,8 @@ static int levenshtein_compare(const void *p1, const void *p2)
286 274
287static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old) 275static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
288{ 276{
289 int i; 277 unsigned int i;
278
290 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc); 279 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
291 280
292 for (i = 0; i < old->cnt; i++) 281 for (i = 0; i < old->cnt; i++)
@@ -298,7 +287,7 @@ static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
298 287
299const char *help_unknown_cmd(const char *cmd) 288const char *help_unknown_cmd(const char *cmd)
300{ 289{
301 int i, n = 0, best_similarity = 0; 290 unsigned int i, n = 0, best_similarity = 0;
302 struct cmdnames main_cmds, other_cmds; 291 struct cmdnames main_cmds, other_cmds;
303 292
304 memset(&main_cmds, 0, sizeof(main_cmds)); 293 memset(&main_cmds, 0, sizeof(main_cmds));
@@ -360,7 +349,7 @@ const char *help_unknown_cmd(const char *cmd)
360 exit(1); 349 exit(1);
361} 350}
362 351
363int cmd_version(int argc, const char **argv, const char *prefix) 352int cmd_version(int argc __used, const char **argv __used, const char *prefix __used)
364{ 353{
365 printf("perf version %s\n", perf_version_string); 354 printf("perf version %s\n", perf_version_string);
366 return 0; 355 return 0;
diff --git a/tools/perf/util/help.h b/tools/perf/util/help.h
index 56bc15406ffc..7128783637b4 100644
--- a/tools/perf/util/help.h
+++ b/tools/perf/util/help.h
@@ -2,8 +2,8 @@
2#define HELP_H 2#define HELP_H
3 3
4struct cmdnames { 4struct cmdnames {
5 int alloc; 5 size_t alloc;
6 int cnt; 6 size_t cnt;
7 struct cmdname { 7 struct cmdname {
8 size_t len; /* also used for similarity index in help.c */ 8 size_t len; /* also used for similarity index in help.c */
9 char name[FLEX_ARRAY]; 9 char name[FLEX_ARRAY];
@@ -19,7 +19,7 @@ static inline void mput_char(char c, unsigned int num)
19void load_command_list(const char *prefix, 19void load_command_list(const char *prefix,
20 struct cmdnames *main_cmds, 20 struct cmdnames *main_cmds,
21 struct cmdnames *other_cmds); 21 struct cmdnames *other_cmds);
22void add_cmdname(struct cmdnames *cmds, const char *name, int len); 22void add_cmdname(struct cmdnames *cmds, const char *name, size_t len);
23/* Here we require that excludes is a sorted list. */ 23/* Here we require that excludes is a sorted list. */
24void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes); 24void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes);
25int is_in_cmdlist(struct cmdnames *c, const char *s); 25int is_in_cmdlist(struct cmdnames *c, const char *s);
diff --git a/tools/perf/util/include/asm/system.h b/tools/perf/util/include/asm/system.h
new file mode 100644
index 000000000000..710cecca972d
--- /dev/null
+++ b/tools/perf/util/include/asm/system.h
@@ -0,0 +1 @@
/* Empty */
diff --git a/tools/perf/util/include/linux/kernel.h b/tools/perf/util/include/linux/kernel.h
new file mode 100644
index 000000000000..99c1b3d1edd9
--- /dev/null
+++ b/tools/perf/util/include/linux/kernel.h
@@ -0,0 +1,21 @@
1#ifndef PERF_LINUX_KERNEL_H_
2#define PERF_LINUX_KERNEL_H_
3
4#ifndef offsetof
5#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
6#endif
7
8#ifndef container_of
9/**
10 * container_of - cast a member of a structure out to the containing structure
11 * @ptr: the pointer to the member.
12 * @type: the type of the container struct this is embedded in.
13 * @member: the name of the member within the struct.
14 *
15 */
16#define container_of(ptr, type, member) ({ \
17 const typeof(((type *)0)->member) * __mptr = (ptr); \
18 (type *)((char *)__mptr - offsetof(type, member)); })
19#endif
20
21#endif
diff --git a/tools/perf/util/include/linux/list.h b/tools/perf/util/include/linux/list.h
new file mode 100644
index 000000000000..dbe4b814382a
--- /dev/null
+++ b/tools/perf/util/include/linux/list.h
@@ -0,0 +1,18 @@
1#include "../../../../include/linux/list.h"
2
3#ifndef PERF_LIST_H
4#define PERF_LIST_H
5/**
6 * list_del_range - deletes range of entries from list.
7 * @begin: first element in the range to delete from the list.
8 * @end: last element in the range to delete from the list.
9 * Note: list_empty on the range of entries does not return true after this,
10 * the entries is in an undefined state.
11 */
12static inline void list_del_range(struct list_head *begin,
13 struct list_head *end)
14{
15 begin->prev->next = end->next;
16 end->next->prev = begin->prev;
17}
18#endif
diff --git a/tools/perf/util/include/linux/module.h b/tools/perf/util/include/linux/module.h
new file mode 100644
index 000000000000..b43e2dc21e04
--- /dev/null
+++ b/tools/perf/util/include/linux/module.h
@@ -0,0 +1,6 @@
1#ifndef PERF_LINUX_MODULE_H
2#define PERF_LINUX_MODULE_H
3
4#define EXPORT_SYMBOL(name)
5
6#endif
diff --git a/tools/perf/util/include/linux/poison.h b/tools/perf/util/include/linux/poison.h
new file mode 100644
index 000000000000..fef6dbc9ce13
--- /dev/null
+++ b/tools/perf/util/include/linux/poison.h
@@ -0,0 +1 @@
#include "../../../../include/linux/poison.h"
diff --git a/tools/perf/util/include/linux/prefetch.h b/tools/perf/util/include/linux/prefetch.h
new file mode 100644
index 000000000000..7841e485d8c3
--- /dev/null
+++ b/tools/perf/util/include/linux/prefetch.h
@@ -0,0 +1,6 @@
1#ifndef PERF_LINUX_PREFETCH_H
2#define PERF_LINUX_PREFETCH_H
3
4static inline void prefetch(void *a __attribute__((unused))) { }
5
6#endif
diff --git a/tools/perf/util/include/linux/rbtree.h b/tools/perf/util/include/linux/rbtree.h
new file mode 100644
index 000000000000..7a243a143037
--- /dev/null
+++ b/tools/perf/util/include/linux/rbtree.h
@@ -0,0 +1 @@
#include "../../../../include/linux/rbtree.h"
diff --git a/tools/perf/util/list.h b/tools/perf/util/list.h
deleted file mode 100644
index e2548e8072cf..000000000000
--- a/tools/perf/util/list.h
+++ /dev/null
@@ -1,603 +0,0 @@
1#ifndef _LINUX_LIST_H
2#define _LINUX_LIST_H
3/*
4 Copyright (C) Cast of dozens, comes from the Linux kernel
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of version 2 of the GNU General Public License as
8 published by the Free Software Foundation.
9*/
10
11#include <stddef.h>
12
13/*
14 * These are non-NULL pointers that will result in page faults
15 * under normal circumstances, used to verify that nobody uses
16 * non-initialized list entries.
17 */
18#define LIST_POISON1 ((void *)0x00100100)
19#define LIST_POISON2 ((void *)0x00200200)
20
21/**
22 * container_of - cast a member of a structure out to the containing structure
23 * @ptr: the pointer to the member.
24 * @type: the type of the container struct this is embedded in.
25 * @member: the name of the member within the struct.
26 *
27 */
28#define container_of(ptr, type, member) ({ \
29 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
30 (type *)( (char *)__mptr - offsetof(type,member) );})
31
32/*
33 * Simple doubly linked list implementation.
34 *
35 * Some of the internal functions ("__xxx") are useful when
36 * manipulating whole lists rather than single entries, as
37 * sometimes we already know the next/prev entries and we can
38 * generate better code by using them directly rather than
39 * using the generic single-entry routines.
40 */
41
42struct list_head {
43 struct list_head *next, *prev;
44};
45
46#define LIST_HEAD_INIT(name) { &(name), &(name) }
47
48#define LIST_HEAD(name) \
49 struct list_head name = LIST_HEAD_INIT(name)
50
51static inline void INIT_LIST_HEAD(struct list_head *list)
52{
53 list->next = list;
54 list->prev = list;
55}
56
57/*
58 * Insert a new entry between two known consecutive entries.
59 *
60 * This is only for internal list manipulation where we know
61 * the prev/next entries already!
62 */
63static inline void __list_add(struct list_head *new,
64 struct list_head *prev,
65 struct list_head *next)
66{
67 next->prev = new;
68 new->next = next;
69 new->prev = prev;
70 prev->next = new;
71}
72
73/**
74 * list_add - add a new entry
75 * @new: new entry to be added
76 * @head: list head to add it after
77 *
78 * Insert a new entry after the specified head.
79 * This is good for implementing stacks.
80 */
81static inline void list_add(struct list_head *new, struct list_head *head)
82{
83 __list_add(new, head, head->next);
84}
85
86/**
87 * list_add_tail - add a new entry
88 * @new: new entry to be added
89 * @head: list head to add it before
90 *
91 * Insert a new entry before the specified head.
92 * This is useful for implementing queues.
93 */
94static inline void list_add_tail(struct list_head *new, struct list_head *head)
95{
96 __list_add(new, head->prev, head);
97}
98
99/*
100 * Delete a list entry by making the prev/next entries
101 * point to each other.
102 *
103 * This is only for internal list manipulation where we know
104 * the prev/next entries already!
105 */
106static inline void __list_del(struct list_head * prev, struct list_head * next)
107{
108 next->prev = prev;
109 prev->next = next;
110}
111
112/**
113 * list_del - deletes entry from list.
114 * @entry: the element to delete from the list.
115 * Note: list_empty on entry does not return true after this, the entry is
116 * in an undefined state.
117 */
118static inline void list_del(struct list_head *entry)
119{
120 __list_del(entry->prev, entry->next);
121 entry->next = LIST_POISON1;
122 entry->prev = LIST_POISON2;
123}
124
125/**
126 * list_del_range - deletes range of entries from list.
127 * @beging: first element in the range to delete from the list.
128 * @beging: first element in the range to delete from the list.
129 * Note: list_empty on the range of entries does not return true after this,
130 * the entries is in an undefined state.
131 */
132static inline void list_del_range(struct list_head *begin,
133 struct list_head *end)
134{
135 begin->prev->next = end->next;
136 end->next->prev = begin->prev;
137}
138
139/**
140 * list_replace - replace old entry by new one
141 * @old : the element to be replaced
142 * @new : the new element to insert
143 * Note: if 'old' was empty, it will be overwritten.
144 */
145static inline void list_replace(struct list_head *old,
146 struct list_head *new)
147{
148 new->next = old->next;
149 new->next->prev = new;
150 new->prev = old->prev;
151 new->prev->next = new;
152}
153
154static inline void list_replace_init(struct list_head *old,
155 struct list_head *new)
156{
157 list_replace(old, new);
158 INIT_LIST_HEAD(old);
159}
160
161/**
162 * list_del_init - deletes entry from list and reinitialize it.
163 * @entry: the element to delete from the list.
164 */
165static inline void list_del_init(struct list_head *entry)
166{
167 __list_del(entry->prev, entry->next);
168 INIT_LIST_HEAD(entry);
169}
170
171/**
172 * list_move - delete from one list and add as another's head
173 * @list: the entry to move
174 * @head: the head that will precede our entry
175 */
176static inline void list_move(struct list_head *list, struct list_head *head)
177{
178 __list_del(list->prev, list->next);
179 list_add(list, head);
180}
181
182/**
183 * list_move_tail - delete from one list and add as another's tail
184 * @list: the entry to move
185 * @head: the head that will follow our entry
186 */
187static inline void list_move_tail(struct list_head *list,
188 struct list_head *head)
189{
190 __list_del(list->prev, list->next);
191 list_add_tail(list, head);
192}
193
194/**
195 * list_is_last - tests whether @list is the last entry in list @head
196 * @list: the entry to test
197 * @head: the head of the list
198 */
199static inline int list_is_last(const struct list_head *list,
200 const struct list_head *head)
201{
202 return list->next == head;
203}
204
205/**
206 * list_empty - tests whether a list is empty
207 * @head: the list to test.
208 */
209static inline int list_empty(const struct list_head *head)
210{
211 return head->next == head;
212}
213
214/**
215 * list_empty_careful - tests whether a list is empty and not being modified
216 * @head: the list to test
217 *
218 * Description:
219 * tests whether a list is empty _and_ checks that no other CPU might be
220 * in the process of modifying either member (next or prev)
221 *
222 * NOTE: using list_empty_careful() without synchronization
223 * can only be safe if the only activity that can happen
224 * to the list entry is list_del_init(). Eg. it cannot be used
225 * if another CPU could re-list_add() it.
226 */
227static inline int list_empty_careful(const struct list_head *head)
228{
229 struct list_head *next = head->next;
230 return (next == head) && (next == head->prev);
231}
232
233static inline void __list_splice(struct list_head *list,
234 struct list_head *head)
235{
236 struct list_head *first = list->next;
237 struct list_head *last = list->prev;
238 struct list_head *at = head->next;
239
240 first->prev = head;
241 head->next = first;
242
243 last->next = at;
244 at->prev = last;
245}
246
247/**
248 * list_splice - join two lists
249 * @list: the new list to add.
250 * @head: the place to add it in the first list.
251 */
252static inline void list_splice(struct list_head *list, struct list_head *head)
253{
254 if (!list_empty(list))
255 __list_splice(list, head);
256}
257
258/**
259 * list_splice_init - join two lists and reinitialise the emptied list.
260 * @list: the new list to add.
261 * @head: the place to add it in the first list.
262 *
263 * The list at @list is reinitialised
264 */
265static inline void list_splice_init(struct list_head *list,
266 struct list_head *head)
267{
268 if (!list_empty(list)) {
269 __list_splice(list, head);
270 INIT_LIST_HEAD(list);
271 }
272}
273
274/**
275 * list_entry - get the struct for this entry
276 * @ptr: the &struct list_head pointer.
277 * @type: the type of the struct this is embedded in.
278 * @member: the name of the list_struct within the struct.
279 */
280#define list_entry(ptr, type, member) \
281 container_of(ptr, type, member)
282
283/**
284 * list_first_entry - get the first element from a list
285 * @ptr: the list head to take the element from.
286 * @type: the type of the struct this is embedded in.
287 * @member: the name of the list_struct within the struct.
288 *
289 * Note, that list is expected to be not empty.
290 */
291#define list_first_entry(ptr, type, member) \
292 list_entry((ptr)->next, type, member)
293
294/**
295 * list_for_each - iterate over a list
296 * @pos: the &struct list_head to use as a loop cursor.
297 * @head: the head for your list.
298 */
299#define list_for_each(pos, head) \
300 for (pos = (head)->next; pos != (head); \
301 pos = pos->next)
302
303/**
304 * __list_for_each - iterate over a list
305 * @pos: the &struct list_head to use as a loop cursor.
306 * @head: the head for your list.
307 *
308 * This variant differs from list_for_each() in that it's the
309 * simplest possible list iteration code, no prefetching is done.
310 * Use this for code that knows the list to be very short (empty
311 * or 1 entry) most of the time.
312 */
313#define __list_for_each(pos, head) \
314 for (pos = (head)->next; pos != (head); pos = pos->next)
315
316/**
317 * list_for_each_prev - iterate over a list backwards
318 * @pos: the &struct list_head to use as a loop cursor.
319 * @head: the head for your list.
320 */
321#define list_for_each_prev(pos, head) \
322 for (pos = (head)->prev; pos != (head); \
323 pos = pos->prev)
324
325/**
326 * list_for_each_safe - iterate over a list safe against removal of list entry
327 * @pos: the &struct list_head to use as a loop cursor.
328 * @n: another &struct list_head to use as temporary storage
329 * @head: the head for your list.
330 */
331#define list_for_each_safe(pos, n, head) \
332 for (pos = (head)->next, n = pos->next; pos != (head); \
333 pos = n, n = pos->next)
334
335/**
336 * list_for_each_entry - iterate over list of given type
337 * @pos: the type * to use as a loop cursor.
338 * @head: the head for your list.
339 * @member: the name of the list_struct within the struct.
340 */
341#define list_for_each_entry(pos, head, member) \
342 for (pos = list_entry((head)->next, typeof(*pos), member); \
343 &pos->member != (head); \
344 pos = list_entry(pos->member.next, typeof(*pos), member))
345
346/**
347 * list_for_each_entry_reverse - iterate backwards over list of given type.
348 * @pos: the type * to use as a loop cursor.
349 * @head: the head for your list.
350 * @member: the name of the list_struct within the struct.
351 */
352#define list_for_each_entry_reverse(pos, head, member) \
353 for (pos = list_entry((head)->prev, typeof(*pos), member); \
354 &pos->member != (head); \
355 pos = list_entry(pos->member.prev, typeof(*pos), member))
356
357/**
358 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue
359 * @pos: the type * to use as a start point
360 * @head: the head of the list
361 * @member: the name of the list_struct within the struct.
362 *
363 * Prepares a pos entry for use as a start point in list_for_each_entry_continue.
364 */
365#define list_prepare_entry(pos, head, member) \
366 ((pos) ? : list_entry(head, typeof(*pos), member))
367
368/**
369 * list_for_each_entry_continue - continue iteration over list of given type
370 * @pos: the type * to use as a loop cursor.
371 * @head: the head for your list.
372 * @member: the name of the list_struct within the struct.
373 *
374 * Continue to iterate over list of given type, continuing after
375 * the current position.
376 */
377#define list_for_each_entry_continue(pos, head, member) \
378 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
379 &pos->member != (head); \
380 pos = list_entry(pos->member.next, typeof(*pos), member))
381
382/**
383 * list_for_each_entry_from - iterate over list of given type from the current point
384 * @pos: the type * to use as a loop cursor.
385 * @head: the head for your list.
386 * @member: the name of the list_struct within the struct.
387 *
388 * Iterate over list of given type, continuing from current position.
389 */
390#define list_for_each_entry_from(pos, head, member) \
391 for (; &pos->member != (head); \
392 pos = list_entry(pos->member.next, typeof(*pos), member))
393
394/**
395 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
396 * @pos: the type * to use as a loop cursor.
397 * @n: another type * to use as temporary storage
398 * @head: the head for your list.
399 * @member: the name of the list_struct within the struct.
400 */
401#define list_for_each_entry_safe(pos, n, head, member) \
402 for (pos = list_entry((head)->next, typeof(*pos), member), \
403 n = list_entry(pos->member.next, typeof(*pos), member); \
404 &pos->member != (head); \
405 pos = n, n = list_entry(n->member.next, typeof(*n), member))
406
407/**
408 * list_for_each_entry_safe_continue
409 * @pos: the type * to use as a loop cursor.
410 * @n: another type * to use as temporary storage
411 * @head: the head for your list.
412 * @member: the name of the list_struct within the struct.
413 *
414 * Iterate over list of given type, continuing after current point,
415 * safe against removal of list entry.
416 */
417#define list_for_each_entry_safe_continue(pos, n, head, member) \
418 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
419 n = list_entry(pos->member.next, typeof(*pos), member); \
420 &pos->member != (head); \
421 pos = n, n = list_entry(n->member.next, typeof(*n), member))
422
423/**
424 * list_for_each_entry_safe_from
425 * @pos: the type * to use as a loop cursor.
426 * @n: another type * to use as temporary storage
427 * @head: the head for your list.
428 * @member: the name of the list_struct within the struct.
429 *
430 * Iterate over list of given type from current point, safe against
431 * removal of list entry.
432 */
433#define list_for_each_entry_safe_from(pos, n, head, member) \
434 for (n = list_entry(pos->member.next, typeof(*pos), member); \
435 &pos->member != (head); \
436 pos = n, n = list_entry(n->member.next, typeof(*n), member))
437
438/**
439 * list_for_each_entry_safe_reverse
440 * @pos: the type * to use as a loop cursor.
441 * @n: another type * to use as temporary storage
442 * @head: the head for your list.
443 * @member: the name of the list_struct within the struct.
444 *
445 * Iterate backwards over list of given type, safe against removal
446 * of list entry.
447 */
448#define list_for_each_entry_safe_reverse(pos, n, head, member) \
449 for (pos = list_entry((head)->prev, typeof(*pos), member), \
450 n = list_entry(pos->member.prev, typeof(*pos), member); \
451 &pos->member != (head); \
452 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
453
454/*
455 * Double linked lists with a single pointer list head.
456 * Mostly useful for hash tables where the two pointer list head is
457 * too wasteful.
458 * You lose the ability to access the tail in O(1).
459 */
460
461struct hlist_head {
462 struct hlist_node *first;
463};
464
465struct hlist_node {
466 struct hlist_node *next, **pprev;
467};
468
469#define HLIST_HEAD_INIT { .first = NULL }
470#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
471#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
472static inline void INIT_HLIST_NODE(struct hlist_node *h)
473{
474 h->next = NULL;
475 h->pprev = NULL;
476}
477
478static inline int hlist_unhashed(const struct hlist_node *h)
479{
480 return !h->pprev;
481}
482
483static inline int hlist_empty(const struct hlist_head *h)
484{
485 return !h->first;
486}
487
488static inline void __hlist_del(struct hlist_node *n)
489{
490 struct hlist_node *next = n->next;
491 struct hlist_node **pprev = n->pprev;
492 *pprev = next;
493 if (next)
494 next->pprev = pprev;
495}
496
497static inline void hlist_del(struct hlist_node *n)
498{
499 __hlist_del(n);
500 n->next = LIST_POISON1;
501 n->pprev = LIST_POISON2;
502}
503
504static inline void hlist_del_init(struct hlist_node *n)
505{
506 if (!hlist_unhashed(n)) {
507 __hlist_del(n);
508 INIT_HLIST_NODE(n);
509 }
510}
511
512static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
513{
514 struct hlist_node *first = h->first;
515 n->next = first;
516 if (first)
517 first->pprev = &n->next;
518 h->first = n;
519 n->pprev = &h->first;
520}
521
522/* next must be != NULL */
523static inline void hlist_add_before(struct hlist_node *n,
524 struct hlist_node *next)
525{
526 n->pprev = next->pprev;
527 n->next = next;
528 next->pprev = &n->next;
529 *(n->pprev) = n;
530}
531
532static inline void hlist_add_after(struct hlist_node *n,
533 struct hlist_node *next)
534{
535 next->next = n->next;
536 n->next = next;
537 next->pprev = &n->next;
538
539 if(next->next)
540 next->next->pprev = &next->next;
541}
542
543#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
544
545#define hlist_for_each(pos, head) \
546 for (pos = (head)->first; pos; \
547 pos = pos->next)
548
549#define hlist_for_each_safe(pos, n, head) \
550 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
551 pos = n)
552
553/**
554 * hlist_for_each_entry - iterate over list of given type
555 * @tpos: the type * to use as a loop cursor.
556 * @pos: the &struct hlist_node to use as a loop cursor.
557 * @head: the head for your list.
558 * @member: the name of the hlist_node within the struct.
559 */
560#define hlist_for_each_entry(tpos, pos, head, member) \
561 for (pos = (head)->first; \
562 pos && \
563 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
564 pos = pos->next)
565
566/**
567 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
568 * @tpos: the type * to use as a loop cursor.
569 * @pos: the &struct hlist_node to use as a loop cursor.
570 * @member: the name of the hlist_node within the struct.
571 */
572#define hlist_for_each_entry_continue(tpos, pos, member) \
573 for (pos = (pos)->next; \
574 pos && \
575 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
576 pos = pos->next)
577
578/**
579 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
580 * @tpos: the type * to use as a loop cursor.
581 * @pos: the &struct hlist_node to use as a loop cursor.
582 * @member: the name of the hlist_node within the struct.
583 */
584#define hlist_for_each_entry_from(tpos, pos, member) \
585 for (; pos && \
586 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
587 pos = pos->next)
588
589/**
590 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
591 * @tpos: the type * to use as a loop cursor.
592 * @pos: the &struct hlist_node to use as a loop cursor.
593 * @n: another &struct hlist_node to use as temporary storage
594 * @head: the head for your list.
595 * @member: the name of the hlist_node within the struct.
596 */
597#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
598 for (pos = (head)->first; \
599 pos && ({ n = pos->next; 1; }) && \
600 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
601 pos = n)
602
603#endif
diff --git a/tools/perf/util/module.c b/tools/perf/util/module.c
new file mode 100644
index 000000000000..ddabe925d65d
--- /dev/null
+++ b/tools/perf/util/module.c
@@ -0,0 +1,509 @@
1#include "util.h"
2#include "../perf.h"
3#include "string.h"
4#include "module.h"
5
6#include <libelf.h>
7#include <gelf.h>
8#include <elf.h>
9#include <dirent.h>
10#include <sys/utsname.h>
11
12static unsigned int crc32(const char *p, unsigned int len)
13{
14 int i;
15 unsigned int crc = 0;
16
17 while (len--) {
18 crc ^= *p++;
19 for (i = 0; i < 8; i++)
20 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
21 }
22 return crc;
23}
24
25/* module section methods */
26
27struct sec_dso *sec_dso__new_dso(const char *name)
28{
29 struct sec_dso *self = malloc(sizeof(*self) + strlen(name) + 1);
30
31 if (self != NULL) {
32 strcpy(self->name, name);
33 self->secs = RB_ROOT;
34 self->find_section = sec_dso__find_section;
35 }
36
37 return self;
38}
39
40static void sec_dso__delete_section(struct section *self)
41{
42 free(((void *)self));
43}
44
45void sec_dso__delete_sections(struct sec_dso *self)
46{
47 struct section *pos;
48 struct rb_node *next = rb_first(&self->secs);
49
50 while (next) {
51 pos = rb_entry(next, struct section, rb_node);
52 next = rb_next(&pos->rb_node);
53 rb_erase(&pos->rb_node, &self->secs);
54 sec_dso__delete_section(pos);
55 }
56}
57
58void sec_dso__delete_self(struct sec_dso *self)
59{
60 sec_dso__delete_sections(self);
61 free(self);
62}
63
64static void sec_dso__insert_section(struct sec_dso *self, struct section *sec)
65{
66 struct rb_node **p = &self->secs.rb_node;
67 struct rb_node *parent = NULL;
68 const u64 hash = sec->hash;
69 struct section *s;
70
71 while (*p != NULL) {
72 parent = *p;
73 s = rb_entry(parent, struct section, rb_node);
74 if (hash < s->hash)
75 p = &(*p)->rb_left;
76 else
77 p = &(*p)->rb_right;
78 }
79 rb_link_node(&sec->rb_node, parent, p);
80 rb_insert_color(&sec->rb_node, &self->secs);
81}
82
83struct section *sec_dso__find_section(struct sec_dso *self, const char *name)
84{
85 struct rb_node *n;
86 u64 hash;
87 int len;
88
89 if (self == NULL)
90 return NULL;
91
92 len = strlen(name);
93 hash = crc32(name, len);
94
95 n = self->secs.rb_node;
96
97 while (n) {
98 struct section *s = rb_entry(n, struct section, rb_node);
99
100 if (hash < s->hash)
101 n = n->rb_left;
102 else if (hash > s->hash)
103 n = n->rb_right;
104 else {
105 if (!strcmp(name, s->name))
106 return s;
107 else
108 n = rb_next(&s->rb_node);
109 }
110 }
111
112 return NULL;
113}
114
115static size_t sec_dso__fprintf_section(struct section *self, FILE *fp)
116{
117 return fprintf(fp, "name:%s vma:%llx path:%s\n",
118 self->name, self->vma, self->path);
119}
120
121size_t sec_dso__fprintf(struct sec_dso *self, FILE *fp)
122{
123 size_t ret = fprintf(fp, "dso: %s\n", self->name);
124
125 struct rb_node *nd;
126 for (nd = rb_first(&self->secs); nd; nd = rb_next(nd)) {
127 struct section *pos = rb_entry(nd, struct section, rb_node);
128 ret += sec_dso__fprintf_section(pos, fp);
129 }
130
131 return ret;
132}
133
134static struct section *section__new(const char *name, const char *path)
135{
136 struct section *self = calloc(1, sizeof(*self));
137
138 if (!self)
139 goto out_failure;
140
141 self->name = calloc(1, strlen(name) + 1);
142 if (!self->name)
143 goto out_failure;
144
145 self->path = calloc(1, strlen(path) + 1);
146 if (!self->path)
147 goto out_failure;
148
149 strcpy(self->name, name);
150 strcpy(self->path, path);
151 self->hash = crc32(self->name, strlen(name));
152
153 return self;
154
155out_failure:
156 if (self) {
157 if (self->name)
158 free(self->name);
159 if (self->path)
160 free(self->path);
161 free(self);
162 }
163
164 return NULL;
165}
166
167/* module methods */
168
169struct mod_dso *mod_dso__new_dso(const char *name)
170{
171 struct mod_dso *self = malloc(sizeof(*self) + strlen(name) + 1);
172
173 if (self != NULL) {
174 strcpy(self->name, name);
175 self->mods = RB_ROOT;
176 self->find_module = mod_dso__find_module;
177 }
178
179 return self;
180}
181
182static void mod_dso__delete_module(struct module *self)
183{
184 free(((void *)self));
185}
186
187void mod_dso__delete_modules(struct mod_dso *self)
188{
189 struct module *pos;
190 struct rb_node *next = rb_first(&self->mods);
191
192 while (next) {
193 pos = rb_entry(next, struct module, rb_node);
194 next = rb_next(&pos->rb_node);
195 rb_erase(&pos->rb_node, &self->mods);
196 mod_dso__delete_module(pos);
197 }
198}
199
200void mod_dso__delete_self(struct mod_dso *self)
201{
202 mod_dso__delete_modules(self);
203 free(self);
204}
205
206static void mod_dso__insert_module(struct mod_dso *self, struct module *mod)
207{
208 struct rb_node **p = &self->mods.rb_node;
209 struct rb_node *parent = NULL;
210 const u64 hash = mod->hash;
211 struct module *m;
212
213 while (*p != NULL) {
214 parent = *p;
215 m = rb_entry(parent, struct module, rb_node);
216 if (hash < m->hash)
217 p = &(*p)->rb_left;
218 else
219 p = &(*p)->rb_right;
220 }
221 rb_link_node(&mod->rb_node, parent, p);
222 rb_insert_color(&mod->rb_node, &self->mods);
223}
224
225struct module *mod_dso__find_module(struct mod_dso *self, const char *name)
226{
227 struct rb_node *n;
228 u64 hash;
229 int len;
230
231 if (self == NULL)
232 return NULL;
233
234 len = strlen(name);
235 hash = crc32(name, len);
236
237 n = self->mods.rb_node;
238
239 while (n) {
240 struct module *m = rb_entry(n, struct module, rb_node);
241
242 if (hash < m->hash)
243 n = n->rb_left;
244 else if (hash > m->hash)
245 n = n->rb_right;
246 else {
247 if (!strcmp(name, m->name))
248 return m;
249 else
250 n = rb_next(&m->rb_node);
251 }
252 }
253
254 return NULL;
255}
256
257static size_t mod_dso__fprintf_module(struct module *self, FILE *fp)
258{
259 return fprintf(fp, "name:%s path:%s\n", self->name, self->path);
260}
261
262size_t mod_dso__fprintf(struct mod_dso *self, FILE *fp)
263{
264 struct rb_node *nd;
265 size_t ret;
266
267 ret = fprintf(fp, "dso: %s\n", self->name);
268
269 for (nd = rb_first(&self->mods); nd; nd = rb_next(nd)) {
270 struct module *pos = rb_entry(nd, struct module, rb_node);
271
272 ret += mod_dso__fprintf_module(pos, fp);
273 }
274
275 return ret;
276}
277
278static struct module *module__new(const char *name, const char *path)
279{
280 struct module *self = calloc(1, sizeof(*self));
281
282 if (!self)
283 goto out_failure;
284
285 self->name = calloc(1, strlen(name) + 1);
286 if (!self->name)
287 goto out_failure;
288
289 self->path = calloc(1, strlen(path) + 1);
290 if (!self->path)
291 goto out_failure;
292
293 strcpy(self->name, name);
294 strcpy(self->path, path);
295 self->hash = crc32(self->name, strlen(name));
296
297 return self;
298
299out_failure:
300 if (self) {
301 if (self->name)
302 free(self->name);
303 if (self->path)
304 free(self->path);
305 free(self);
306 }
307
308 return NULL;
309}
310
311static int mod_dso__load_sections(struct module *mod)
312{
313 int count = 0, path_len;
314 struct dirent *entry;
315 char *line = NULL;
316 char *dir_path;
317 DIR *dir;
318 size_t n;
319
320 path_len = strlen("/sys/module/");
321 path_len += strlen(mod->name);
322 path_len += strlen("/sections/");
323
324 dir_path = calloc(1, path_len + 1);
325 if (dir_path == NULL)
326 goto out_failure;
327
328 strcat(dir_path, "/sys/module/");
329 strcat(dir_path, mod->name);
330 strcat(dir_path, "/sections/");
331
332 dir = opendir(dir_path);
333 if (dir == NULL)
334 goto out_free;
335
336 while ((entry = readdir(dir))) {
337 struct section *section;
338 char *path, *vma;
339 int line_len;
340 FILE *file;
341
342 if (!strcmp(".", entry->d_name) || !strcmp("..", entry->d_name))
343 continue;
344
345 path = calloc(1, path_len + strlen(entry->d_name) + 1);
346 if (path == NULL)
347 break;
348 strcat(path, dir_path);
349 strcat(path, entry->d_name);
350
351 file = fopen(path, "r");
352 if (file == NULL) {
353 free(path);
354 break;
355 }
356
357 line_len = getline(&line, &n, file);
358 if (line_len < 0) {
359 free(path);
360 fclose(file);
361 break;
362 }
363
364 if (!line) {
365 free(path);
366 fclose(file);
367 break;
368 }
369
370 line[--line_len] = '\0'; /* \n */
371
372 vma = strstr(line, "0x");
373 if (!vma) {
374 free(path);
375 fclose(file);
376 break;
377 }
378 vma += 2;
379
380 section = section__new(entry->d_name, path);
381 if (!section) {
382 fprintf(stderr, "load_sections: allocation error\n");
383 free(path);
384 fclose(file);
385 break;
386 }
387
388 hex2u64(vma, &section->vma);
389 sec_dso__insert_section(mod->sections, section);
390
391 free(path);
392 fclose(file);
393 count++;
394 }
395
396 closedir(dir);
397 free(line);
398 free(dir_path);
399
400 return count;
401
402out_free:
403 free(dir_path);
404
405out_failure:
406 return count;
407}
408
409static int mod_dso__load_module_paths(struct mod_dso *self)
410{
411 struct utsname uts;
412 int count = 0, len;
413 char *line = NULL;
414 FILE *file;
415 char *path;
416 size_t n;
417
418 if (uname(&uts) < 0)
419 goto out_failure;
420
421 len = strlen("/lib/modules/");
422 len += strlen(uts.release);
423 len += strlen("/modules.dep");
424
425 path = calloc(1, len);
426 if (path == NULL)
427 goto out_failure;
428
429 strcat(path, "/lib/modules/");
430 strcat(path, uts.release);
431 strcat(path, "/modules.dep");
432
433 file = fopen(path, "r");
434 free(path);
435 if (file == NULL)
436 goto out_failure;
437
438 while (!feof(file)) {
439 char *path, *name, *tmp;
440 struct module *module;
441 int line_len, len;
442
443 line_len = getline(&line, &n, file);
444 if (line_len < 0)
445 break;
446
447 if (!line)
448 goto out_failure;
449
450 line[--line_len] = '\0'; /* \n */
451
452 path = strtok(line, ":");
453 if (!path)
454 goto out_failure;
455
456 name = strdup(path);
457 name = strtok(name, "/");
458
459 tmp = name;
460
461 while (tmp) {
462 tmp = strtok(NULL, "/");
463 if (tmp)
464 name = tmp;
465 }
466 name = strsep(&name, ".");
467
468 /* Quirk: replace '-' with '_' in sound modules */
469 for (len = strlen(name); len; len--) {
470 if (*(name+len) == '-')
471 *(name+len) = '_';
472 }
473
474 module = module__new(name, path);
475 if (!module) {
476 fprintf(stderr, "load_module_paths: allocation error\n");
477 goto out_failure;
478 }
479 mod_dso__insert_module(self, module);
480
481 module->sections = sec_dso__new_dso("sections");
482 if (!module->sections) {
483 fprintf(stderr, "load_module_paths: allocation error\n");
484 goto out_failure;
485 }
486
487 module->active = mod_dso__load_sections(module);
488
489 if (module->active > 0)
490 count++;
491 }
492
493 free(line);
494 fclose(file);
495
496 return count;
497
498out_failure:
499 return -1;
500}
501
502int mod_dso__load_modules(struct mod_dso *dso)
503{
504 int err;
505
506 err = mod_dso__load_module_paths(dso);
507
508 return err;
509}
diff --git a/tools/perf/util/module.h b/tools/perf/util/module.h
new file mode 100644
index 000000000000..8a592ef641ca
--- /dev/null
+++ b/tools/perf/util/module.h
@@ -0,0 +1,53 @@
1#ifndef _PERF_MODULE_
2#define _PERF_MODULE_ 1
3
4#include <linux/types.h>
5#include "../types.h"
6#include <linux/list.h>
7#include <linux/rbtree.h>
8
9struct section {
10 struct rb_node rb_node;
11 u64 hash;
12 u64 vma;
13 char *name;
14 char *path;
15};
16
17struct sec_dso {
18 struct list_head node;
19 struct rb_root secs;
20 struct section *(*find_section)(struct sec_dso *, const char *name);
21 char name[0];
22};
23
24struct module {
25 struct rb_node rb_node;
26 u64 hash;
27 char *name;
28 char *path;
29 struct sec_dso *sections;
30 int active;
31};
32
33struct mod_dso {
34 struct list_head node;
35 struct rb_root mods;
36 struct module *(*find_module)(struct mod_dso *, const char *name);
37 char name[0];
38};
39
40struct sec_dso *sec_dso__new_dso(const char *name);
41void sec_dso__delete_sections(struct sec_dso *self);
42void sec_dso__delete_self(struct sec_dso *self);
43size_t sec_dso__fprintf(struct sec_dso *self, FILE *fp);
44struct section *sec_dso__find_section(struct sec_dso *self, const char *name);
45
46struct mod_dso *mod_dso__new_dso(const char *name);
47void mod_dso__delete_modules(struct mod_dso *self);
48void mod_dso__delete_self(struct mod_dso *self);
49size_t mod_dso__fprintf(struct mod_dso *self, FILE *fp);
50struct module *mod_dso__find_module(struct mod_dso *self, const char *name);
51int mod_dso__load_modules(struct mod_dso *dso);
52
53#endif /* _PERF_MODULE_ */
diff --git a/tools/perf/util/pager.c b/tools/perf/util/pager.c
index a28bccae5458..1915de20dcac 100644
--- a/tools/perf/util/pager.c
+++ b/tools/perf/util/pager.c
@@ -9,7 +9,6 @@
9 9
10static int spawned_pager; 10static int spawned_pager;
11 11
12#ifndef __MINGW32__
13static void pager_preexec(void) 12static void pager_preexec(void)
14{ 13{
15 /* 14 /*
@@ -24,7 +23,6 @@ static void pager_preexec(void)
24 23
25 setenv("LESS", "FRSX", 0); 24 setenv("LESS", "FRSX", 0);
26} 25}
27#endif
28 26
29static const char *pager_argv[] = { "sh", "-c", NULL, NULL }; 27static const char *pager_argv[] = { "sh", "-c", NULL, NULL };
30static struct child_process pager_process; 28static struct child_process pager_process;
@@ -70,9 +68,8 @@ void setup_pager(void)
70 pager_argv[2] = pager; 68 pager_argv[2] = pager;
71 pager_process.argv = pager_argv; 69 pager_process.argv = pager_argv;
72 pager_process.in = -1; 70 pager_process.in = -1;
73#ifndef __MINGW32__
74 pager_process.preexec_cb = pager_preexec; 71 pager_process.preexec_cb = pager_preexec;
75#endif 72
76 if (start_command(&pager_process)) 73 if (start_command(&pager_process))
77 return; 74 return;
78 75
diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index 35d04da38d6a..5184959e0615 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -16,32 +16,28 @@ struct event_symbol {
16 u8 type; 16 u8 type;
17 u64 config; 17 u64 config;
18 char *symbol; 18 char *symbol;
19 char *alias;
19}; 20};
20 21
21#define C(x, y) .type = PERF_TYPE_##x, .config = PERF_COUNT_##y 22#define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
22#define CR(x, y) .type = PERF_TYPE_##x, .config = y 23#define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
23 24
24static struct event_symbol event_symbols[] = { 25static struct event_symbol event_symbols[] = {
25 { C(HARDWARE, HW_CPU_CYCLES), "cpu-cycles", }, 26 { CHW(CPU_CYCLES), "cpu-cycles", "cycles" },
26 { C(HARDWARE, HW_CPU_CYCLES), "cycles", }, 27 { CHW(INSTRUCTIONS), "instructions", "" },
27 { C(HARDWARE, HW_INSTRUCTIONS), "instructions", }, 28 { CHW(CACHE_REFERENCES), "cache-references", "" },
28 { C(HARDWARE, HW_CACHE_REFERENCES), "cache-references", }, 29 { CHW(CACHE_MISSES), "cache-misses", "" },
29 { C(HARDWARE, HW_CACHE_MISSES), "cache-misses", }, 30 { CHW(BRANCH_INSTRUCTIONS), "branch-instructions", "branches" },
30 { C(HARDWARE, HW_BRANCH_INSTRUCTIONS),"branch-instructions", }, 31 { CHW(BRANCH_MISSES), "branch-misses", "" },
31 { C(HARDWARE, HW_BRANCH_INSTRUCTIONS),"branches", }, 32 { CHW(BUS_CYCLES), "bus-cycles", "" },
32 { C(HARDWARE, HW_BRANCH_MISSES), "branch-misses", }, 33
33 { C(HARDWARE, HW_BUS_CYCLES), "bus-cycles", }, 34 { CSW(CPU_CLOCK), "cpu-clock", "" },
34 35 { CSW(TASK_CLOCK), "task-clock", "" },
35 { C(SOFTWARE, SW_CPU_CLOCK), "cpu-clock", }, 36 { CSW(PAGE_FAULTS), "page-faults", "faults" },
36 { C(SOFTWARE, SW_TASK_CLOCK), "task-clock", }, 37 { CSW(PAGE_FAULTS_MIN), "minor-faults", "" },
37 { C(SOFTWARE, SW_PAGE_FAULTS), "page-faults", }, 38 { CSW(PAGE_FAULTS_MAJ), "major-faults", "" },
38 { C(SOFTWARE, SW_PAGE_FAULTS), "faults", }, 39 { CSW(CONTEXT_SWITCHES), "context-switches", "cs" },
39 { C(SOFTWARE, SW_PAGE_FAULTS_MIN), "minor-faults", }, 40 { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" },
40 { C(SOFTWARE, SW_PAGE_FAULTS_MAJ), "major-faults", },
41 { C(SOFTWARE, SW_CONTEXT_SWITCHES), "context-switches", },
42 { C(SOFTWARE, SW_CONTEXT_SWITCHES), "cs", },
43 { C(SOFTWARE, SW_CPU_MIGRATIONS), "cpu-migrations", },
44 { C(SOFTWARE, SW_CPU_MIGRATIONS), "migrations", },
45}; 41};
46 42
47#define __PERF_COUNTER_FIELD(config, name) \ 43#define __PERF_COUNTER_FIELD(config, name) \
@@ -74,26 +70,70 @@ static char *sw_event_names[] = {
74 70
75#define MAX_ALIASES 8 71#define MAX_ALIASES 8
76 72
77static char *hw_cache [][MAX_ALIASES] = { 73static char *hw_cache[][MAX_ALIASES] = {
78 { "L1-data" , "l1-d", "l1d" }, 74 { "L1-d$", "l1-d", "l1d", "L1-data", },
79 { "L1-instruction" , "l1-i", "l1i" }, 75 { "L1-i$", "l1-i", "l1i", "L1-instruction", },
80 { "L2" , "l2" }, 76 { "LLC", "L2" },
81 { "Data-TLB" , "dtlb", "d-tlb" }, 77 { "dTLB", "d-tlb", "Data-TLB", },
82 { "Instruction-TLB" , "itlb", "i-tlb" }, 78 { "iTLB", "i-tlb", "Instruction-TLB", },
83 { "Branch" , "bpu" , "btb", "bpc" }, 79 { "branch", "branches", "bpu", "btb", "bpc", },
84}; 80};
85 81
86static char *hw_cache_op [][MAX_ALIASES] = { 82static char *hw_cache_op[][MAX_ALIASES] = {
87 { "Load" , "read" }, 83 { "load", "loads", "read", },
88 { "Store" , "write" }, 84 { "store", "stores", "write", },
89 { "Prefetch" , "speculative-read", "speculative-load" }, 85 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
90}; 86};
91 87
92static char *hw_cache_result [][MAX_ALIASES] = { 88static char *hw_cache_result[][MAX_ALIASES] = {
93 { "Reference" , "ops", "access" }, 89 { "refs", "Reference", "ops", "access", },
94 { "Miss" }, 90 { "misses", "miss", },
95}; 91};
96 92
93#define C(x) PERF_COUNT_HW_CACHE_##x
94#define CACHE_READ (1 << C(OP_READ))
95#define CACHE_WRITE (1 << C(OP_WRITE))
96#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
97#define COP(x) (1 << x)
98
99/*
100 * cache operartion stat
101 * L1I : Read and prefetch only
102 * ITLB and BPU : Read-only
103 */
104static unsigned long hw_cache_stat[C(MAX)] = {
105 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
106 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
107 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
108 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
109 [C(ITLB)] = (CACHE_READ),
110 [C(BPU)] = (CACHE_READ),
111};
112
113static int is_cache_op_valid(u8 cache_type, u8 cache_op)
114{
115 if (hw_cache_stat[cache_type] & COP(cache_op))
116 return 1; /* valid */
117 else
118 return 0; /* invalid */
119}
120
121static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
122{
123 static char name[50];
124
125 if (cache_result) {
126 sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
127 hw_cache_op[cache_op][0],
128 hw_cache_result[cache_result][0]);
129 } else {
130 sprintf(name, "%s-%s", hw_cache[cache_type][0],
131 hw_cache_op[cache_op][1]);
132 }
133
134 return name;
135}
136
97char *event_name(int counter) 137char *event_name(int counter)
98{ 138{
99 u64 config = attrs[counter].config; 139 u64 config = attrs[counter].config;
@@ -113,7 +153,6 @@ char *event_name(int counter)
113 153
114 case PERF_TYPE_HW_CACHE: { 154 case PERF_TYPE_HW_CACHE: {
115 u8 cache_type, cache_op, cache_result; 155 u8 cache_type, cache_op, cache_result;
116 static char name[100];
117 156
118 cache_type = (config >> 0) & 0xff; 157 cache_type = (config >> 0) & 0xff;
119 if (cache_type > PERF_COUNT_HW_CACHE_MAX) 158 if (cache_type > PERF_COUNT_HW_CACHE_MAX)
@@ -127,12 +166,10 @@ char *event_name(int counter)
127 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX) 166 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
128 return "unknown-ext-hardware-cache-result"; 167 return "unknown-ext-hardware-cache-result";
129 168
130 sprintf(name, "%s-Cache-%s-%ses", 169 if (!is_cache_op_valid(cache_type, cache_op))
131 hw_cache[cache_type][0], 170 return "invalid-cache";
132 hw_cache_op[cache_op][0],
133 hw_cache_result[cache_result][0]);
134 171
135 return name; 172 return event_cache_name(cache_type, cache_op, cache_result);
136 } 173 }
137 174
138 case PERF_TYPE_SOFTWARE: 175 case PERF_TYPE_SOFTWARE:
@@ -147,43 +184,74 @@ char *event_name(int counter)
147 return "unknown"; 184 return "unknown";
148} 185}
149 186
150static int parse_aliases(const char *str, char *names[][MAX_ALIASES], int size) 187static int parse_aliases(const char **str, char *names[][MAX_ALIASES], int size)
151{ 188{
152 int i, j; 189 int i, j;
190 int n, longest = -1;
153 191
154 for (i = 0; i < size; i++) { 192 for (i = 0; i < size; i++) {
155 for (j = 0; j < MAX_ALIASES; j++) { 193 for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
156 if (!names[i][j]) 194 n = strlen(names[i][j]);
157 break; 195 if (n > longest && !strncasecmp(*str, names[i][j], n))
158 if (strcasestr(str, names[i][j])) 196 longest = n;
159 return i; 197 }
198 if (longest > 0) {
199 *str += longest;
200 return i;
160 } 201 }
161 } 202 }
162 203
163 return -1; 204 return -1;
164} 205}
165 206
166static int parse_generic_hw_symbols(const char *str, struct perf_counter_attr *attr) 207static int
208parse_generic_hw_event(const char **str, struct perf_counter_attr *attr)
167{ 209{
168 int cache_type = -1, cache_op = 0, cache_result = 0; 210 const char *s = *str;
211 int cache_type = -1, cache_op = -1, cache_result = -1;
169 212
170 cache_type = parse_aliases(str, hw_cache, PERF_COUNT_HW_CACHE_MAX); 213 cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX);
171 /* 214 /*
172 * No fallback - if we cannot get a clear cache type 215 * No fallback - if we cannot get a clear cache type
173 * then bail out: 216 * then bail out:
174 */ 217 */
175 if (cache_type == -1) 218 if (cache_type == -1)
176 return -EINVAL; 219 return 0;
220
221 while ((cache_op == -1 || cache_result == -1) && *s == '-') {
222 ++s;
223
224 if (cache_op == -1) {
225 cache_op = parse_aliases(&s, hw_cache_op,
226 PERF_COUNT_HW_CACHE_OP_MAX);
227 if (cache_op >= 0) {
228 if (!is_cache_op_valid(cache_type, cache_op))
229 return 0;
230 continue;
231 }
232 }
233
234 if (cache_result == -1) {
235 cache_result = parse_aliases(&s, hw_cache_result,
236 PERF_COUNT_HW_CACHE_RESULT_MAX);
237 if (cache_result >= 0)
238 continue;
239 }
240
241 /*
242 * Can't parse this as a cache op or result, so back up
243 * to the '-'.
244 */
245 --s;
246 break;
247 }
177 248
178 cache_op = parse_aliases(str, hw_cache_op, PERF_COUNT_HW_CACHE_OP_MAX);
179 /* 249 /*
180 * Fall back to reads: 250 * Fall back to reads:
181 */ 251 */
182 if (cache_op == -1) 252 if (cache_op == -1)
183 cache_op = PERF_COUNT_HW_CACHE_OP_READ; 253 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
184 254
185 cache_result = parse_aliases(str, hw_cache_result,
186 PERF_COUNT_HW_CACHE_RESULT_MAX);
187 /* 255 /*
188 * Fall back to accesses: 256 * Fall back to accesses:
189 */ 257 */
@@ -193,82 +261,154 @@ static int parse_generic_hw_symbols(const char *str, struct perf_counter_attr *a
193 attr->config = cache_type | (cache_op << 8) | (cache_result << 16); 261 attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
194 attr->type = PERF_TYPE_HW_CACHE; 262 attr->type = PERF_TYPE_HW_CACHE;
195 263
264 *str = s;
265 return 1;
266}
267
268static int check_events(const char *str, unsigned int i)
269{
270 int n;
271
272 n = strlen(event_symbols[i].symbol);
273 if (!strncmp(str, event_symbols[i].symbol, n))
274 return n;
275
276 n = strlen(event_symbols[i].alias);
277 if (n)
278 if (!strncmp(str, event_symbols[i].alias, n))
279 return n;
196 return 0; 280 return 0;
197} 281}
198 282
199/* 283static int
200 * Each event can have multiple symbolic names. 284parse_symbolic_event(const char **strp, struct perf_counter_attr *attr)
201 * Symbolic names are (almost) exactly matched.
202 */
203static int parse_event_symbols(const char *str, struct perf_counter_attr *attr)
204{ 285{
205 u64 config, id; 286 const char *str = *strp;
206 int type;
207 unsigned int i; 287 unsigned int i;
208 const char *sep, *pstr; 288 int n;
209 289
210 if (str[0] == 'r' && hex2u64(str + 1, &config) > 0) { 290 for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
211 attr->type = PERF_TYPE_RAW; 291 n = check_events(str, i);
212 attr->config = config; 292 if (n > 0) {
293 attr->type = event_symbols[i].type;
294 attr->config = event_symbols[i].config;
295 *strp = str + n;
296 return 1;
297 }
298 }
299 return 0;
300}
301
302static int parse_raw_event(const char **strp, struct perf_counter_attr *attr)
303{
304 const char *str = *strp;
305 u64 config;
306 int n;
213 307
308 if (*str != 'r')
214 return 0; 309 return 0;
310 n = hex2u64(str + 1, &config);
311 if (n > 0) {
312 *strp = str + n + 1;
313 attr->type = PERF_TYPE_RAW;
314 attr->config = config;
315 return 1;
215 } 316 }
317 return 0;
318}
216 319
217 pstr = str; 320static int
218 sep = strchr(pstr, ':'); 321parse_numeric_event(const char **strp, struct perf_counter_attr *attr)
219 if (sep) { 322{
220 type = atoi(pstr); 323 const char *str = *strp;
221 pstr = sep + 1; 324 char *endp;
222 id = atoi(pstr); 325 unsigned long type;
223 sep = strchr(pstr, ':'); 326 u64 config;
224 if (sep) { 327
225 pstr = sep + 1; 328 type = strtoul(str, &endp, 0);
226 if (strchr(pstr, 'k')) 329 if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
227 attr->exclude_user = 1; 330 str = endp + 1;
228 if (strchr(pstr, 'u')) 331 config = strtoul(str, &endp, 0);
229 attr->exclude_kernel = 1; 332 if (endp > str) {
333 attr->type = type;
334 attr->config = config;
335 *strp = endp;
336 return 1;
230 } 337 }
231 attr->type = type; 338 }
232 attr->config = id; 339 return 0;
340}
341
342static int
343parse_event_modifier(const char **strp, struct perf_counter_attr *attr)
344{
345 const char *str = *strp;
346 int eu = 1, ek = 1, eh = 1;
233 347
348 if (*str++ != ':')
234 return 0; 349 return 0;
350 while (*str) {
351 if (*str == 'u')
352 eu = 0;
353 else if (*str == 'k')
354 ek = 0;
355 else if (*str == 'h')
356 eh = 0;
357 else
358 break;
359 ++str;
235 } 360 }
361 if (str >= *strp + 2) {
362 *strp = str;
363 attr->exclude_user = eu;
364 attr->exclude_kernel = ek;
365 attr->exclude_hv = eh;
366 return 1;
367 }
368 return 0;
369}
236 370
237 for (i = 0; i < ARRAY_SIZE(event_symbols); i++) { 371/*
238 if (!strncmp(str, event_symbols[i].symbol, 372 * Each event can have multiple symbolic names.
239 strlen(event_symbols[i].symbol))) { 373 * Symbolic names are (almost) exactly matched.
240 374 */
241 attr->type = event_symbols[i].type; 375static int parse_event_symbols(const char **str, struct perf_counter_attr *attr)
242 attr->config = event_symbols[i].config; 376{
377 if (!(parse_raw_event(str, attr) ||
378 parse_numeric_event(str, attr) ||
379 parse_symbolic_event(str, attr) ||
380 parse_generic_hw_event(str, attr)))
381 return 0;
243 382
244 return 0; 383 parse_event_modifier(str, attr);
245 }
246 }
247 384
248 return parse_generic_hw_symbols(str, attr); 385 return 1;
249} 386}
250 387
251int parse_events(const struct option *opt, const char *str, int unset) 388int parse_events(const struct option *opt __used, const char *str, int unset __used)
252{ 389{
253 struct perf_counter_attr attr; 390 struct perf_counter_attr attr;
254 int ret;
255 391
256 memset(&attr, 0, sizeof(attr)); 392 for (;;) {
257again: 393 if (nr_counters == MAX_COUNTERS)
258 if (nr_counters == MAX_COUNTERS) 394 return -1;
259 return -1; 395
396 memset(&attr, 0, sizeof(attr));
397 if (!parse_event_symbols(&str, &attr))
398 return -1;
260 399
261 ret = parse_event_symbols(str, &attr); 400 if (!(*str == 0 || *str == ',' || isspace(*str)))
262 if (ret < 0) 401 return -1;
263 return ret;
264 402
265 attrs[nr_counters] = attr; 403 attrs[nr_counters] = attr;
266 nr_counters++; 404 nr_counters++;
267 405
268 str = strstr(str, ","); 406 if (*str == 0)
269 if (str) { 407 break;
270 str++; 408 if (*str == ',')
271 goto again; 409 ++str;
410 while (isspace(*str))
411 ++str;
272 } 412 }
273 413
274 return 0; 414 return 0;
@@ -288,7 +428,8 @@ static const char * const event_type_descriptors[] = {
288void print_events(void) 428void print_events(void)
289{ 429{
290 struct event_symbol *syms = event_symbols; 430 struct event_symbol *syms = event_symbols;
291 unsigned int i, type, prev_type = -1; 431 unsigned int i, type, op, prev_type = -1;
432 char name[40];
292 433
293 fprintf(stderr, "\n"); 434 fprintf(stderr, "\n");
294 fprintf(stderr, "List of pre-defined events (to be used in -e):\n"); 435 fprintf(stderr, "List of pre-defined events (to be used in -e):\n");
@@ -301,14 +442,33 @@ void print_events(void)
301 if (type != prev_type) 442 if (type != prev_type)
302 fprintf(stderr, "\n"); 443 fprintf(stderr, "\n");
303 444
304 fprintf(stderr, " %-30s [%s]\n", syms->symbol, 445 if (strlen(syms->alias))
446 sprintf(name, "%s OR %s", syms->symbol, syms->alias);
447 else
448 strcpy(name, syms->symbol);
449 fprintf(stderr, " %-40s [%s]\n", name,
305 event_type_descriptors[type]); 450 event_type_descriptors[type]);
306 451
307 prev_type = type; 452 prev_type = type;
308 } 453 }
309 454
310 fprintf(stderr, "\n"); 455 fprintf(stderr, "\n");
311 fprintf(stderr, " %-30s [raw hardware event descriptor]\n", 456 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
457 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
458 /* skip invalid cache type */
459 if (!is_cache_op_valid(type, op))
460 continue;
461
462 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
463 fprintf(stderr, " %-40s [%s]\n",
464 event_cache_name(type, op, i),
465 event_type_descriptors[4]);
466 }
467 }
468 }
469
470 fprintf(stderr, "\n");
471 fprintf(stderr, " %-40s [raw hardware event descriptor]\n",
312 "rNNN"); 472 "rNNN");
313 fprintf(stderr, "\n"); 473 fprintf(stderr, "\n");
314 474
diff --git a/tools/perf/util/parse-options.c b/tools/perf/util/parse-options.c
index b3affb1658d2..1bf67190c820 100644
--- a/tools/perf/util/parse-options.c
+++ b/tools/perf/util/parse-options.c
@@ -20,7 +20,8 @@ static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
20 if (p->opt) { 20 if (p->opt) {
21 *arg = p->opt; 21 *arg = p->opt;
22 p->opt = NULL; 22 p->opt = NULL;
23 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) { 23 } else if ((opt->flags & PARSE_OPT_LASTARG_DEFAULT) && (p->argc == 1 ||
24 **(p->argv + 1) == '-')) {
24 *arg = (const char *)opt->defval; 25 *arg = (const char *)opt->defval;
25 } else if (p->argc > 1) { 26 } else if (p->argc > 1) {
26 p->argc--; 27 p->argc--;
@@ -485,7 +486,7 @@ int parse_options_usage(const char * const *usagestr,
485} 486}
486 487
487 488
488int parse_opt_verbosity_cb(const struct option *opt, const char *arg, 489int parse_opt_verbosity_cb(const struct option *opt, const char *arg __used,
489 int unset) 490 int unset)
490{ 491{
491 int *target = opt->value; 492 int *target = opt->value;
diff --git a/tools/perf/util/parse-options.h b/tools/perf/util/parse-options.h
index a1039a6ce0eb..8aa3464c7090 100644
--- a/tools/perf/util/parse-options.h
+++ b/tools/perf/util/parse-options.h
@@ -90,21 +90,22 @@ struct option {
90 intptr_t defval; 90 intptr_t defval;
91}; 91};
92 92
93#define OPT_END() { OPTION_END } 93#define OPT_END() { .type = OPTION_END }
94#define OPT_ARGUMENT(l, h) { OPTION_ARGUMENT, 0, (l), NULL, NULL, (h) } 94#define OPT_ARGUMENT(l, h) { .type = OPTION_ARGUMENT, .long_name = (l), .help = (h) }
95#define OPT_GROUP(h) { OPTION_GROUP, 0, NULL, NULL, NULL, (h) } 95#define OPT_GROUP(h) { .type = OPTION_GROUP, .help = (h) }
96#define OPT_BIT(s, l, v, h, b) { OPTION_BIT, (s), (l), (v), NULL, (h), 0, NULL, (b) } 96#define OPT_BIT(s, l, v, h, b) { .type = OPTION_BIT, .short_name = (s), .long_name = (l), .value = (v), .help = (h), .defval = (b) }
97#define OPT_BOOLEAN(s, l, v, h) { OPTION_BOOLEAN, (s), (l), (v), NULL, (h) } 97#define OPT_BOOLEAN(s, l, v, h) { .type = OPTION_BOOLEAN, .short_name = (s), .long_name = (l), .value = (v), .help = (h) }
98#define OPT_SET_INT(s, l, v, h, i) { OPTION_SET_INT, (s), (l), (v), NULL, (h), 0, NULL, (i) } 98#define OPT_SET_INT(s, l, v, h, i) { .type = OPTION_SET_INT, .short_name = (s), .long_name = (l), .value = (v), .help = (h), .defval = (i) }
99#define OPT_SET_PTR(s, l, v, h, p) { OPTION_SET_PTR, (s), (l), (v), NULL, (h), 0, NULL, (p) } 99#define OPT_SET_PTR(s, l, v, h, p) { .type = OPTION_SET_PTR, .short_name = (s), .long_name = (l), .value = (v), .help = (h), .defval = (p) }
100#define OPT_INTEGER(s, l, v, h) { OPTION_INTEGER, (s), (l), (v), NULL, (h) } 100#define OPT_INTEGER(s, l, v, h) { .type = OPTION_INTEGER, .short_name = (s), .long_name = (l), .value = (v), .help = (h) }
101#define OPT_LONG(s, l, v, h) { OPTION_LONG, (s), (l), (v), NULL, (h) } 101#define OPT_LONG(s, l, v, h) { .type = OPTION_LONG, .short_name = (s), .long_name = (l), .value = (v), .help = (h) }
102#define OPT_STRING(s, l, v, a, h) { OPTION_STRING, (s), (l), (v), (a), (h) } 102#define OPT_STRING(s, l, v, a, h) { .type = OPTION_STRING, .short_name = (s), .long_name = (l), .value = (v), (a), .help = (h) }
103#define OPT_DATE(s, l, v, h) \ 103#define OPT_DATE(s, l, v, h) \
104 { OPTION_CALLBACK, (s), (l), (v), "time",(h), 0, \ 104 { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), .argh = "time", .help = (h), .callback = parse_opt_approxidate_cb }
105 parse_opt_approxidate_cb }
106#define OPT_CALLBACK(s, l, v, a, h, f) \ 105#define OPT_CALLBACK(s, l, v, a, h, f) \
107 { OPTION_CALLBACK, (s), (l), (v), (a), (h), 0, (f) } 106 { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), (a), .help = (h), .callback = (f) }
107#define OPT_CALLBACK_DEFAULT(s, l, v, a, h, f, d) \
108 { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), (a), .help = (h), .callback = (f), .defval = (intptr_t)d, .flags = PARSE_OPT_LASTARG_DEFAULT }
108 109
109/* parse_options() will filter out the processed options and leave the 110/* parse_options() will filter out the processed options and leave the
110 * non-option argments in argv[]. 111 * non-option argments in argv[].
diff --git a/tools/perf/util/quote.c b/tools/perf/util/quote.c
index f18c5212bc92..c6e5dc0dc82f 100644
--- a/tools/perf/util/quote.c
+++ b/tools/perf/util/quote.c
@@ -162,12 +162,16 @@ static inline int sq_must_quote(char c)
162 return sq_lookup[(unsigned char)c] + quote_path_fully > 0; 162 return sq_lookup[(unsigned char)c] + quote_path_fully > 0;
163} 163}
164 164
165/* returns the longest prefix not needing a quote up to maxlen if positive. 165/*
166 This stops at the first \0 because it's marked as a character needing an 166 * Returns the longest prefix not needing a quote up to maxlen if
167 escape */ 167 * positive.
168static size_t next_quote_pos(const char *s, ssize_t maxlen) 168 * This stops at the first \0 because it's marked as a character
169 * needing an escape.
170 */
171static ssize_t next_quote_pos(const char *s, ssize_t maxlen)
169{ 172{
170 size_t len; 173 ssize_t len;
174
171 if (maxlen < 0) { 175 if (maxlen < 0) {
172 for (len = 0; !sq_must_quote(s[len]); len++); 176 for (len = 0; !sq_must_quote(s[len]); len++);
173 } else { 177 } else {
@@ -192,22 +196,22 @@ static size_t next_quote_pos(const char *s, ssize_t maxlen)
192static size_t quote_c_style_counted(const char *name, ssize_t maxlen, 196static size_t quote_c_style_counted(const char *name, ssize_t maxlen,
193 struct strbuf *sb, FILE *fp, int no_dq) 197 struct strbuf *sb, FILE *fp, int no_dq)
194{ 198{
195#undef EMIT 199#define EMIT(c) \
196#define EMIT(c) \ 200 do { \
197 do { \ 201 if (sb) strbuf_addch(sb, (c)); \
198 if (sb) strbuf_addch(sb, (c)); \ 202 if (fp) fputc((c), fp); \
199 if (fp) fputc((c), fp); \ 203 count++; \
200 count++; \
201 } while (0) 204 } while (0)
202#define EMITBUF(s, l) \ 205
203 do { \ 206#define EMITBUF(s, l) \
204 int __ret; \ 207 do { \
205 if (sb) strbuf_add(sb, (s), (l)); \ 208 int __ret; \
206 if (fp) __ret = fwrite((s), (l), 1, fp); \ 209 if (sb) strbuf_add(sb, (s), (l)); \
207 count += (l); \ 210 if (fp) __ret = fwrite((s), (l), 1, fp); \
211 count += (l); \
208 } while (0) 212 } while (0)
209 213
210 size_t len, count = 0; 214 ssize_t len, count = 0;
211 const char *p = name; 215 const char *p = name;
212 216
213 for (;;) { 217 for (;;) {
@@ -273,8 +277,8 @@ void write_name_quoted(const char *name, FILE *fp, int terminator)
273 fputc(terminator, fp); 277 fputc(terminator, fp);
274} 278}
275 279
276extern void write_name_quotedpfx(const char *pfx, size_t pfxlen, 280void write_name_quotedpfx(const char *pfx, ssize_t pfxlen,
277 const char *name, FILE *fp, int terminator) 281 const char *name, FILE *fp, int terminator)
278{ 282{
279 int needquote = 0; 283 int needquote = 0;
280 284
@@ -306,7 +310,7 @@ char *quote_path_relative(const char *in, int len,
306 len = strlen(in); 310 len = strlen(in);
307 311
308 /* "../" prefix itself does not need quoting, but "in" might. */ 312 /* "../" prefix itself does not need quoting, but "in" might. */
309 needquote = next_quote_pos(in, len) < len; 313 needquote = (next_quote_pos(in, len) < len);
310 strbuf_setlen(out, 0); 314 strbuf_setlen(out, 0);
311 strbuf_grow(out, len); 315 strbuf_grow(out, len);
312 316
diff --git a/tools/perf/util/quote.h b/tools/perf/util/quote.h
index 5dfad89816db..a5454a1d1c13 100644
--- a/tools/perf/util/quote.h
+++ b/tools/perf/util/quote.h
@@ -53,7 +53,7 @@ extern size_t quote_c_style(const char *name, struct strbuf *, FILE *, int no_dq
53extern void quote_two_c_style(struct strbuf *, const char *, const char *, int); 53extern void quote_two_c_style(struct strbuf *, const char *, const char *, int);
54 54
55extern void write_name_quoted(const char *name, FILE *, int terminator); 55extern void write_name_quoted(const char *name, FILE *, int terminator);
56extern void write_name_quotedpfx(const char *pfx, size_t pfxlen, 56extern void write_name_quotedpfx(const char *pfx, ssize_t pfxlen,
57 const char *name, FILE *, int terminator); 57 const char *name, FILE *, int terminator);
58 58
59/* quote path as relative to the given prefix */ 59/* quote path as relative to the given prefix */
diff --git a/tools/perf/util/rbtree.c b/tools/perf/util/rbtree.c
deleted file mode 100644
index b15ba9c7cb3f..000000000000
--- a/tools/perf/util/rbtree.c
+++ /dev/null
@@ -1,383 +0,0 @@
1/*
2 Red Black Trees
3 (C) 1999 Andrea Arcangeli <andrea@suse.de>
4 (C) 2002 David Woodhouse <dwmw2@infradead.org>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20 linux/lib/rbtree.c
21*/
22
23#include "rbtree.h"
24
25static void __rb_rotate_left(struct rb_node *node, struct rb_root *root)
26{
27 struct rb_node *right = node->rb_right;
28 struct rb_node *parent = rb_parent(node);
29
30 if ((node->rb_right = right->rb_left))
31 rb_set_parent(right->rb_left, node);
32 right->rb_left = node;
33
34 rb_set_parent(right, parent);
35
36 if (parent)
37 {
38 if (node == parent->rb_left)
39 parent->rb_left = right;
40 else
41 parent->rb_right = right;
42 }
43 else
44 root->rb_node = right;
45 rb_set_parent(node, right);
46}
47
48static void __rb_rotate_right(struct rb_node *node, struct rb_root *root)
49{
50 struct rb_node *left = node->rb_left;
51 struct rb_node *parent = rb_parent(node);
52
53 if ((node->rb_left = left->rb_right))
54 rb_set_parent(left->rb_right, node);
55 left->rb_right = node;
56
57 rb_set_parent(left, parent);
58
59 if (parent)
60 {
61 if (node == parent->rb_right)
62 parent->rb_right = left;
63 else
64 parent->rb_left = left;
65 }
66 else
67 root->rb_node = left;
68 rb_set_parent(node, left);
69}
70
71void rb_insert_color(struct rb_node *node, struct rb_root *root)
72{
73 struct rb_node *parent, *gparent;
74
75 while ((parent = rb_parent(node)) && rb_is_red(parent))
76 {
77 gparent = rb_parent(parent);
78
79 if (parent == gparent->rb_left)
80 {
81 {
82 register struct rb_node *uncle = gparent->rb_right;
83 if (uncle && rb_is_red(uncle))
84 {
85 rb_set_black(uncle);
86 rb_set_black(parent);
87 rb_set_red(gparent);
88 node = gparent;
89 continue;
90 }
91 }
92
93 if (parent->rb_right == node)
94 {
95 register struct rb_node *tmp;
96 __rb_rotate_left(parent, root);
97 tmp = parent;
98 parent = node;
99 node = tmp;
100 }
101
102 rb_set_black(parent);
103 rb_set_red(gparent);
104 __rb_rotate_right(gparent, root);
105 } else {
106 {
107 register struct rb_node *uncle = gparent->rb_left;
108 if (uncle && rb_is_red(uncle))
109 {
110 rb_set_black(uncle);
111 rb_set_black(parent);
112 rb_set_red(gparent);
113 node = gparent;
114 continue;
115 }
116 }
117
118 if (parent->rb_left == node)
119 {
120 register struct rb_node *tmp;
121 __rb_rotate_right(parent, root);
122 tmp = parent;
123 parent = node;
124 node = tmp;
125 }
126
127 rb_set_black(parent);
128 rb_set_red(gparent);
129 __rb_rotate_left(gparent, root);
130 }
131 }
132
133 rb_set_black(root->rb_node);
134}
135
136static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
137 struct rb_root *root)
138{
139 struct rb_node *other;
140
141 while ((!node || rb_is_black(node)) && node != root->rb_node)
142 {
143 if (parent->rb_left == node)
144 {
145 other = parent->rb_right;
146 if (rb_is_red(other))
147 {
148 rb_set_black(other);
149 rb_set_red(parent);
150 __rb_rotate_left(parent, root);
151 other = parent->rb_right;
152 }
153 if ((!other->rb_left || rb_is_black(other->rb_left)) &&
154 (!other->rb_right || rb_is_black(other->rb_right)))
155 {
156 rb_set_red(other);
157 node = parent;
158 parent = rb_parent(node);
159 }
160 else
161 {
162 if (!other->rb_right || rb_is_black(other->rb_right))
163 {
164 rb_set_black(other->rb_left);
165 rb_set_red(other);
166 __rb_rotate_right(other, root);
167 other = parent->rb_right;
168 }
169 rb_set_color(other, rb_color(parent));
170 rb_set_black(parent);
171 rb_set_black(other->rb_right);
172 __rb_rotate_left(parent, root);
173 node = root->rb_node;
174 break;
175 }
176 }
177 else
178 {
179 other = parent->rb_left;
180 if (rb_is_red(other))
181 {
182 rb_set_black(other);
183 rb_set_red(parent);
184 __rb_rotate_right(parent, root);
185 other = parent->rb_left;
186 }
187 if ((!other->rb_left || rb_is_black(other->rb_left)) &&
188 (!other->rb_right || rb_is_black(other->rb_right)))
189 {
190 rb_set_red(other);
191 node = parent;
192 parent = rb_parent(node);
193 }
194 else
195 {
196 if (!other->rb_left || rb_is_black(other->rb_left))
197 {
198 rb_set_black(other->rb_right);
199 rb_set_red(other);
200 __rb_rotate_left(other, root);
201 other = parent->rb_left;
202 }
203 rb_set_color(other, rb_color(parent));
204 rb_set_black(parent);
205 rb_set_black(other->rb_left);
206 __rb_rotate_right(parent, root);
207 node = root->rb_node;
208 break;
209 }
210 }
211 }
212 if (node)
213 rb_set_black(node);
214}
215
216void rb_erase(struct rb_node *node, struct rb_root *root)
217{
218 struct rb_node *child, *parent;
219 int color;
220
221 if (!node->rb_left)
222 child = node->rb_right;
223 else if (!node->rb_right)
224 child = node->rb_left;
225 else
226 {
227 struct rb_node *old = node, *left;
228
229 node = node->rb_right;
230 while ((left = node->rb_left) != NULL)
231 node = left;
232 child = node->rb_right;
233 parent = rb_parent(node);
234 color = rb_color(node);
235
236 if (child)
237 rb_set_parent(child, parent);
238 if (parent == old) {
239 parent->rb_right = child;
240 parent = node;
241 } else
242 parent->rb_left = child;
243
244 node->rb_parent_color = old->rb_parent_color;
245 node->rb_right = old->rb_right;
246 node->rb_left = old->rb_left;
247
248 if (rb_parent(old))
249 {
250 if (rb_parent(old)->rb_left == old)
251 rb_parent(old)->rb_left = node;
252 else
253 rb_parent(old)->rb_right = node;
254 } else
255 root->rb_node = node;
256
257 rb_set_parent(old->rb_left, node);
258 if (old->rb_right)
259 rb_set_parent(old->rb_right, node);
260 goto color;
261 }
262
263 parent = rb_parent(node);
264 color = rb_color(node);
265
266 if (child)
267 rb_set_parent(child, parent);
268 if (parent)
269 {
270 if (parent->rb_left == node)
271 parent->rb_left = child;
272 else
273 parent->rb_right = child;
274 }
275 else
276 root->rb_node = child;
277
278 color:
279 if (color == RB_BLACK)
280 __rb_erase_color(child, parent, root);
281}
282
283/*
284 * This function returns the first node (in sort order) of the tree.
285 */
286struct rb_node *rb_first(const struct rb_root *root)
287{
288 struct rb_node *n;
289
290 n = root->rb_node;
291 if (!n)
292 return NULL;
293 while (n->rb_left)
294 n = n->rb_left;
295 return n;
296}
297
298struct rb_node *rb_last(const struct rb_root *root)
299{
300 struct rb_node *n;
301
302 n = root->rb_node;
303 if (!n)
304 return NULL;
305 while (n->rb_right)
306 n = n->rb_right;
307 return n;
308}
309
310struct rb_node *rb_next(const struct rb_node *node)
311{
312 struct rb_node *parent;
313
314 if (rb_parent(node) == node)
315 return NULL;
316
317 /* If we have a right-hand child, go down and then left as far
318 as we can. */
319 if (node->rb_right) {
320 node = node->rb_right;
321 while (node->rb_left)
322 node=node->rb_left;
323 return (struct rb_node *)node;
324 }
325
326 /* No right-hand children. Everything down and left is
327 smaller than us, so any 'next' node must be in the general
328 direction of our parent. Go up the tree; any time the
329 ancestor is a right-hand child of its parent, keep going
330 up. First time it's a left-hand child of its parent, said
331 parent is our 'next' node. */
332 while ((parent = rb_parent(node)) && node == parent->rb_right)
333 node = parent;
334
335 return parent;
336}
337
338struct rb_node *rb_prev(const struct rb_node *node)
339{
340 struct rb_node *parent;
341
342 if (rb_parent(node) == node)
343 return NULL;
344
345 /* If we have a left-hand child, go down and then right as far
346 as we can. */
347 if (node->rb_left) {
348 node = node->rb_left;
349 while (node->rb_right)
350 node=node->rb_right;
351 return (struct rb_node *)node;
352 }
353
354 /* No left-hand children. Go up till we find an ancestor which
355 is a right-hand child of its parent */
356 while ((parent = rb_parent(node)) && node == parent->rb_left)
357 node = parent;
358
359 return parent;
360}
361
362void rb_replace_node(struct rb_node *victim, struct rb_node *new,
363 struct rb_root *root)
364{
365 struct rb_node *parent = rb_parent(victim);
366
367 /* Set the surrounding nodes to point to the replacement */
368 if (parent) {
369 if (victim == parent->rb_left)
370 parent->rb_left = new;
371 else
372 parent->rb_right = new;
373 } else {
374 root->rb_node = new;
375 }
376 if (victim->rb_left)
377 rb_set_parent(victim->rb_left, new);
378 if (victim->rb_right)
379 rb_set_parent(victim->rb_right, new);
380
381 /* Copy the pointers/colour from the victim to the replacement */
382 *new = *victim;
383}
diff --git a/tools/perf/util/rbtree.h b/tools/perf/util/rbtree.h
deleted file mode 100644
index 6bdc488a47fb..000000000000
--- a/tools/perf/util/rbtree.h
+++ /dev/null
@@ -1,171 +0,0 @@
1/*
2 Red Black Trees
3 (C) 1999 Andrea Arcangeli <andrea@suse.de>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 linux/include/linux/rbtree.h
20
21 To use rbtrees you'll have to implement your own insert and search cores.
22 This will avoid us to use callbacks and to drop drammatically performances.
23 I know it's not the cleaner way, but in C (not in C++) to get
24 performances and genericity...
25
26 Some example of insert and search follows here. The search is a plain
27 normal search over an ordered tree. The insert instead must be implemented
28 int two steps: as first thing the code must insert the element in
29 order as a red leaf in the tree, then the support library function
30 rb_insert_color() must be called. Such function will do the
31 not trivial work to rebalance the rbtree if necessary.
32
33-----------------------------------------------------------------------
34static inline struct page * rb_search_page_cache(struct inode * inode,
35 unsigned long offset)
36{
37 struct rb_node * n = inode->i_rb_page_cache.rb_node;
38 struct page * page;
39
40 while (n)
41 {
42 page = rb_entry(n, struct page, rb_page_cache);
43
44 if (offset < page->offset)
45 n = n->rb_left;
46 else if (offset > page->offset)
47 n = n->rb_right;
48 else
49 return page;
50 }
51 return NULL;
52}
53
54static inline struct page * __rb_insert_page_cache(struct inode * inode,
55 unsigned long offset,
56 struct rb_node * node)
57{
58 struct rb_node ** p = &inode->i_rb_page_cache.rb_node;
59 struct rb_node * parent = NULL;
60 struct page * page;
61
62 while (*p)
63 {
64 parent = *p;
65 page = rb_entry(parent, struct page, rb_page_cache);
66
67 if (offset < page->offset)
68 p = &(*p)->rb_left;
69 else if (offset > page->offset)
70 p = &(*p)->rb_right;
71 else
72 return page;
73 }
74
75 rb_link_node(node, parent, p);
76
77 return NULL;
78}
79
80static inline struct page * rb_insert_page_cache(struct inode * inode,
81 unsigned long offset,
82 struct rb_node * node)
83{
84 struct page * ret;
85 if ((ret = __rb_insert_page_cache(inode, offset, node)))
86 goto out;
87 rb_insert_color(node, &inode->i_rb_page_cache);
88 out:
89 return ret;
90}
91-----------------------------------------------------------------------
92*/
93
94#ifndef _LINUX_RBTREE_H
95#define _LINUX_RBTREE_H
96
97#include <stddef.h>
98
99/**
100 * container_of - cast a member of a structure out to the containing structure
101 * @ptr: the pointer to the member.
102 * @type: the type of the container struct this is embedded in.
103 * @member: the name of the member within the struct.
104 *
105 */
106#define container_of(ptr, type, member) ({ \
107 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
108 (type *)( (char *)__mptr - offsetof(type,member) );})
109
110struct rb_node
111{
112 unsigned long rb_parent_color;
113#define RB_RED 0
114#define RB_BLACK 1
115 struct rb_node *rb_right;
116 struct rb_node *rb_left;
117} __attribute__((aligned(sizeof(long))));
118 /* The alignment might seem pointless, but allegedly CRIS needs it */
119
120struct rb_root
121{
122 struct rb_node *rb_node;
123};
124
125
126#define rb_parent(r) ((struct rb_node *)((r)->rb_parent_color & ~3))
127#define rb_color(r) ((r)->rb_parent_color & 1)
128#define rb_is_red(r) (!rb_color(r))
129#define rb_is_black(r) rb_color(r)
130#define rb_set_red(r) do { (r)->rb_parent_color &= ~1; } while (0)
131#define rb_set_black(r) do { (r)->rb_parent_color |= 1; } while (0)
132
133static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
134{
135 rb->rb_parent_color = (rb->rb_parent_color & 3) | (unsigned long)p;
136}
137static inline void rb_set_color(struct rb_node *rb, int color)
138{
139 rb->rb_parent_color = (rb->rb_parent_color & ~1) | color;
140}
141
142#define RB_ROOT (struct rb_root) { NULL, }
143#define rb_entry(ptr, type, member) container_of(ptr, type, member)
144
145#define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL)
146#define RB_EMPTY_NODE(node) (rb_parent(node) == node)
147#define RB_CLEAR_NODE(node) (rb_set_parent(node, node))
148
149extern void rb_insert_color(struct rb_node *, struct rb_root *);
150extern void rb_erase(struct rb_node *, struct rb_root *);
151
152/* Find logical next and previous nodes in a tree */
153extern struct rb_node *rb_next(const struct rb_node *);
154extern struct rb_node *rb_prev(const struct rb_node *);
155extern struct rb_node *rb_first(const struct rb_root *);
156extern struct rb_node *rb_last(const struct rb_root *);
157
158/* Fast replacement of a single node without remove/rebalance/add/rebalance */
159extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
160 struct rb_root *root);
161
162static inline void rb_link_node(struct rb_node * node, struct rb_node * parent,
163 struct rb_node ** rb_link)
164{
165 node->rb_parent_color = (unsigned long )parent;
166 node->rb_left = node->rb_right = NULL;
167
168 *rb_link = node;
169}
170
171#endif /* _LINUX_RBTREE_H */
diff --git a/tools/perf/util/run-command.c b/tools/perf/util/run-command.c
index b2f5e854f40a..a3935343091a 100644
--- a/tools/perf/util/run-command.c
+++ b/tools/perf/util/run-command.c
@@ -65,7 +65,6 @@ int start_command(struct child_process *cmd)
65 cmd->err = fderr[0]; 65 cmd->err = fderr[0];
66 } 66 }
67 67
68#ifndef __MINGW32__
69 fflush(NULL); 68 fflush(NULL);
70 cmd->pid = fork(); 69 cmd->pid = fork();
71 if (!cmd->pid) { 70 if (!cmd->pid) {
@@ -118,71 +117,6 @@ int start_command(struct child_process *cmd)
118 } 117 }
119 exit(127); 118 exit(127);
120 } 119 }
121#else
122 int s0 = -1, s1 = -1, s2 = -1; /* backups of stdin, stdout, stderr */
123 const char **sargv = cmd->argv;
124 char **env = environ;
125
126 if (cmd->no_stdin) {
127 s0 = dup(0);
128 dup_devnull(0);
129 } else if (need_in) {
130 s0 = dup(0);
131 dup2(fdin[0], 0);
132 } else if (cmd->in) {
133 s0 = dup(0);
134 dup2(cmd->in, 0);
135 }
136
137 if (cmd->no_stderr) {
138 s2 = dup(2);
139 dup_devnull(2);
140 } else if (need_err) {
141 s2 = dup(2);
142 dup2(fderr[1], 2);
143 }
144
145 if (cmd->no_stdout) {
146 s1 = dup(1);
147 dup_devnull(1);
148 } else if (cmd->stdout_to_stderr) {
149 s1 = dup(1);
150 dup2(2, 1);
151 } else if (need_out) {
152 s1 = dup(1);
153 dup2(fdout[1], 1);
154 } else if (cmd->out > 1) {
155 s1 = dup(1);
156 dup2(cmd->out, 1);
157 }
158
159 if (cmd->dir)
160 die("chdir in start_command() not implemented");
161 if (cmd->env) {
162 env = copy_environ();
163 for (; *cmd->env; cmd->env++)
164 env = env_setenv(env, *cmd->env);
165 }
166
167 if (cmd->perf_cmd) {
168 cmd->argv = prepare_perf_cmd(cmd->argv);
169 }
170
171 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env);
172
173 if (cmd->env)
174 free_environ(env);
175 if (cmd->perf_cmd)
176 free(cmd->argv);
177
178 cmd->argv = sargv;
179 if (s0 >= 0)
180 dup2(s0, 0), close(s0);
181 if (s1 >= 0)
182 dup2(s1, 1), close(s1);
183 if (s2 >= 0)
184 dup2(s2, 2), close(s2);
185#endif
186 120
187 if (cmd->pid < 0) { 121 if (cmd->pid < 0) {
188 int err = errno; 122 int err = errno;
@@ -288,14 +222,6 @@ int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const
288 return run_command(&cmd); 222 return run_command(&cmd);
289} 223}
290 224
291#ifdef __MINGW32__
292static __stdcall unsigned run_thread(void *data)
293{
294 struct async *async = data;
295 return async->proc(async->fd_for_proc, async->data);
296}
297#endif
298
299int start_async(struct async *async) 225int start_async(struct async *async)
300{ 226{
301 int pipe_out[2]; 227 int pipe_out[2];
@@ -304,7 +230,6 @@ int start_async(struct async *async)
304 return error("cannot create pipe: %s", strerror(errno)); 230 return error("cannot create pipe: %s", strerror(errno));
305 async->out = pipe_out[0]; 231 async->out = pipe_out[0];
306 232
307#ifndef __MINGW32__
308 /* Flush stdio before fork() to avoid cloning buffers */ 233 /* Flush stdio before fork() to avoid cloning buffers */
309 fflush(NULL); 234 fflush(NULL);
310 235
@@ -319,33 +244,17 @@ int start_async(struct async *async)
319 exit(!!async->proc(pipe_out[1], async->data)); 244 exit(!!async->proc(pipe_out[1], async->data));
320 } 245 }
321 close(pipe_out[1]); 246 close(pipe_out[1]);
322#else 247
323 async->fd_for_proc = pipe_out[1];
324 async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
325 if (!async->tid) {
326 error("cannot create thread: %s", strerror(errno));
327 close_pair(pipe_out);
328 return -1;
329 }
330#endif
331 return 0; 248 return 0;
332} 249}
333 250
334int finish_async(struct async *async) 251int finish_async(struct async *async)
335{ 252{
336#ifndef __MINGW32__
337 int ret = 0; 253 int ret = 0;
338 254
339 if (wait_or_whine(async->pid)) 255 if (wait_or_whine(async->pid))
340 ret = error("waitpid (async) failed"); 256 ret = error("waitpid (async) failed");
341#else 257
342 DWORD ret = 0;
343 if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
344 ret = error("waiting for thread failed: %lu", GetLastError());
345 else if (!GetExitCodeThread(async->tid, &ret))
346 ret = error("cannot get thread exit code: %lu", GetLastError());
347 CloseHandle(async->tid);
348#endif
349 return ret; 258 return ret;
350} 259}
351 260
diff --git a/tools/perf/util/run-command.h b/tools/perf/util/run-command.h
index 328289f23669..cc1837deba88 100644
--- a/tools/perf/util/run-command.h
+++ b/tools/perf/util/run-command.h
@@ -79,12 +79,7 @@ struct async {
79 int (*proc)(int fd, void *data); 79 int (*proc)(int fd, void *data);
80 void *data; 80 void *data;
81 int out; /* caller reads from here and closes it */ 81 int out; /* caller reads from here and closes it */
82#ifndef __MINGW32__
83 pid_t pid; 82 pid_t pid;
84#else
85 HANDLE tid;
86 int fd_for_proc;
87#endif
88}; 83};
89 84
90int start_async(struct async *async); 85int start_async(struct async *async);
diff --git a/tools/perf/util/strbuf.c b/tools/perf/util/strbuf.c
index eaba09306802..5249d5a1b0c2 100644
--- a/tools/perf/util/strbuf.c
+++ b/tools/perf/util/strbuf.c
@@ -16,7 +16,7 @@ int prefixcmp(const char *str, const char *prefix)
16 */ 16 */
17char strbuf_slopbuf[1]; 17char strbuf_slopbuf[1];
18 18
19void strbuf_init(struct strbuf *sb, size_t hint) 19void strbuf_init(struct strbuf *sb, ssize_t hint)
20{ 20{
21 sb->alloc = sb->len = 0; 21 sb->alloc = sb->len = 0;
22 sb->buf = strbuf_slopbuf; 22 sb->buf = strbuf_slopbuf;
@@ -92,7 +92,8 @@ void strbuf_ltrim(struct strbuf *sb)
92 92
93void strbuf_tolower(struct strbuf *sb) 93void strbuf_tolower(struct strbuf *sb)
94{ 94{
95 int i; 95 unsigned int i;
96
96 for (i = 0; i < sb->len; i++) 97 for (i = 0; i < sb->len; i++)
97 sb->buf[i] = tolower(sb->buf[i]); 98 sb->buf[i] = tolower(sb->buf[i]);
98} 99}
@@ -259,12 +260,12 @@ size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
259 res = fread(sb->buf + sb->len, 1, size, f); 260 res = fread(sb->buf + sb->len, 1, size, f);
260 if (res > 0) 261 if (res > 0)
261 strbuf_setlen(sb, sb->len + res); 262 strbuf_setlen(sb, sb->len + res);
262 else if (res < 0 && oldalloc == 0) 263 else if (oldalloc == 0)
263 strbuf_release(sb); 264 strbuf_release(sb);
264 return res; 265 return res;
265} 266}
266 267
267ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint) 268ssize_t strbuf_read(struct strbuf *sb, int fd, ssize_t hint)
268{ 269{
269 size_t oldlen = sb->len; 270 size_t oldlen = sb->len;
270 size_t oldalloc = sb->alloc; 271 size_t oldalloc = sb->alloc;
@@ -293,7 +294,7 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
293 294
294#define STRBUF_MAXLINK (2*PATH_MAX) 295#define STRBUF_MAXLINK (2*PATH_MAX)
295 296
296int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint) 297int strbuf_readlink(struct strbuf *sb, const char *path, ssize_t hint)
297{ 298{
298 size_t oldalloc = sb->alloc; 299 size_t oldalloc = sb->alloc;
299 300
@@ -301,7 +302,7 @@ int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
301 hint = 32; 302 hint = 32;
302 303
303 while (hint < STRBUF_MAXLINK) { 304 while (hint < STRBUF_MAXLINK) {
304 int len; 305 ssize_t len;
305 306
306 strbuf_grow(sb, hint); 307 strbuf_grow(sb, hint);
307 len = readlink(path, sb->buf, hint); 308 len = readlink(path, sb->buf, hint);
@@ -343,7 +344,7 @@ int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
343 return 0; 344 return 0;
344} 345}
345 346
346int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint) 347int strbuf_read_file(struct strbuf *sb, const char *path, ssize_t hint)
347{ 348{
348 int fd, len; 349 int fd, len;
349 350
diff --git a/tools/perf/util/strbuf.h b/tools/perf/util/strbuf.h
index 9ee908a3ec5d..d2aa86c014c1 100644
--- a/tools/perf/util/strbuf.h
+++ b/tools/perf/util/strbuf.h
@@ -50,7 +50,7 @@ struct strbuf {
50#define STRBUF_INIT { 0, 0, strbuf_slopbuf } 50#define STRBUF_INIT { 0, 0, strbuf_slopbuf }
51 51
52/*----- strbuf life cycle -----*/ 52/*----- strbuf life cycle -----*/
53extern void strbuf_init(struct strbuf *, size_t); 53extern void strbuf_init(struct strbuf *buf, ssize_t hint);
54extern void strbuf_release(struct strbuf *); 54extern void strbuf_release(struct strbuf *);
55extern char *strbuf_detach(struct strbuf *, size_t *); 55extern char *strbuf_detach(struct strbuf *, size_t *);
56extern void strbuf_attach(struct strbuf *, void *, size_t, size_t); 56extern void strbuf_attach(struct strbuf *, void *, size_t, size_t);
@@ -61,7 +61,7 @@ static inline void strbuf_swap(struct strbuf *a, struct strbuf *b) {
61} 61}
62 62
63/*----- strbuf size related -----*/ 63/*----- strbuf size related -----*/
64static inline size_t strbuf_avail(const struct strbuf *sb) { 64static inline ssize_t strbuf_avail(const struct strbuf *sb) {
65 return sb->alloc ? sb->alloc - sb->len - 1 : 0; 65 return sb->alloc ? sb->alloc - sb->len - 1 : 0;
66} 66}
67 67
@@ -122,9 +122,9 @@ extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
122 122
123extern size_t strbuf_fread(struct strbuf *, size_t, FILE *); 123extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
124/* XXX: if read fails, any partial read is undone */ 124/* XXX: if read fails, any partial read is undone */
125extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint); 125extern ssize_t strbuf_read(struct strbuf *, int fd, ssize_t hint);
126extern int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint); 126extern int strbuf_read_file(struct strbuf *sb, const char *path, ssize_t hint);
127extern int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint); 127extern int strbuf_readlink(struct strbuf *sb, const char *path, ssize_t hint);
128 128
129extern int strbuf_getline(struct strbuf *, FILE *, int); 129extern int strbuf_getline(struct strbuf *, FILE *, int);
130 130
diff --git a/tools/perf/util/string.h b/tools/perf/util/string.h
index 37b03255b425..3dca2f654cd0 100644
--- a/tools/perf/util/string.h
+++ b/tools/perf/util/string.h
@@ -1,7 +1,7 @@
1#ifndef _PERF_STRING_H_ 1#ifndef _PERF_STRING_H_
2#define _PERF_STRING_H_ 2#define _PERF_STRING_H_
3 3
4#include "../types.h" 4#include "types.h"
5 5
6int hex2u64(const char *ptr, u64 *val); 6int hex2u64(const char *ptr, u64 *val);
7 7
diff --git a/tools/perf/util/strlist.c b/tools/perf/util/strlist.c
new file mode 100644
index 000000000000..025a78edfffe
--- /dev/null
+++ b/tools/perf/util/strlist.c
@@ -0,0 +1,184 @@
1/*
2 * (c) 2009 Arnaldo Carvalho de Melo <acme@redhat.com>
3 *
4 * Licensed under the GPLv2.
5 */
6
7#include "strlist.h"
8#include <errno.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12
13static struct str_node *str_node__new(const char *s, bool dupstr)
14{
15 struct str_node *self = malloc(sizeof(*self));
16
17 if (self != NULL) {
18 if (dupstr) {
19 s = strdup(s);
20 if (s == NULL)
21 goto out_delete;
22 }
23 self->s = s;
24 }
25
26 return self;
27
28out_delete:
29 free(self);
30 return NULL;
31}
32
33static void str_node__delete(struct str_node *self, bool dupstr)
34{
35 if (dupstr)
36 free((void *)self->s);
37 free(self);
38}
39
40int strlist__add(struct strlist *self, const char *new_entry)
41{
42 struct rb_node **p = &self->entries.rb_node;
43 struct rb_node *parent = NULL;
44 struct str_node *sn;
45
46 while (*p != NULL) {
47 int rc;
48
49 parent = *p;
50 sn = rb_entry(parent, struct str_node, rb_node);
51 rc = strcmp(sn->s, new_entry);
52
53 if (rc > 0)
54 p = &(*p)->rb_left;
55 else if (rc < 0)
56 p = &(*p)->rb_right;
57 else
58 return -EEXIST;
59 }
60
61 sn = str_node__new(new_entry, self->dupstr);
62 if (sn == NULL)
63 return -ENOMEM;
64
65 rb_link_node(&sn->rb_node, parent, p);
66 rb_insert_color(&sn->rb_node, &self->entries);
67
68 return 0;
69}
70
71int strlist__load(struct strlist *self, const char *filename)
72{
73 char entry[1024];
74 int err;
75 FILE *fp = fopen(filename, "r");
76
77 if (fp == NULL)
78 return errno;
79
80 while (fgets(entry, sizeof(entry), fp) != NULL) {
81 const size_t len = strlen(entry);
82
83 if (len == 0)
84 continue;
85 entry[len - 1] = '\0';
86
87 err = strlist__add(self, entry);
88 if (err != 0)
89 goto out;
90 }
91
92 err = 0;
93out:
94 fclose(fp);
95 return err;
96}
97
98void strlist__remove(struct strlist *self, struct str_node *sn)
99{
100 rb_erase(&sn->rb_node, &self->entries);
101 str_node__delete(sn, self->dupstr);
102}
103
104bool strlist__has_entry(struct strlist *self, const char *entry)
105{
106 struct rb_node **p = &self->entries.rb_node;
107 struct rb_node *parent = NULL;
108
109 while (*p != NULL) {
110 struct str_node *sn;
111 int rc;
112
113 parent = *p;
114 sn = rb_entry(parent, struct str_node, rb_node);
115 rc = strcmp(sn->s, entry);
116
117 if (rc > 0)
118 p = &(*p)->rb_left;
119 else if (rc < 0)
120 p = &(*p)->rb_right;
121 else
122 return true;
123 }
124
125 return false;
126}
127
128static int strlist__parse_list_entry(struct strlist *self, const char *s)
129{
130 if (strncmp(s, "file://", 7) == 0)
131 return strlist__load(self, s + 7);
132
133 return strlist__add(self, s);
134}
135
136int strlist__parse_list(struct strlist *self, const char *s)
137{
138 char *sep;
139 int err;
140
141 while ((sep = strchr(s, ',')) != NULL) {
142 *sep = '\0';
143 err = strlist__parse_list_entry(self, s);
144 *sep = ',';
145 if (err != 0)
146 return err;
147 s = sep + 1;
148 }
149
150 return *s ? strlist__parse_list_entry(self, s) : 0;
151}
152
153struct strlist *strlist__new(bool dupstr, const char *slist)
154{
155 struct strlist *self = malloc(sizeof(*self));
156
157 if (self != NULL) {
158 self->entries = RB_ROOT;
159 self->dupstr = dupstr;
160 if (slist && strlist__parse_list(self, slist) != 0)
161 goto out_error;
162 }
163
164 return self;
165out_error:
166 free(self);
167 return NULL;
168}
169
170void strlist__delete(struct strlist *self)
171{
172 if (self != NULL) {
173 struct str_node *pos;
174 struct rb_node *next = rb_first(&self->entries);
175
176 while (next) {
177 pos = rb_entry(next, struct str_node, rb_node);
178 next = rb_next(&pos->rb_node);
179 strlist__remove(self, pos);
180 }
181 self->entries = RB_ROOT;
182 free(self);
183 }
184}
diff --git a/tools/perf/util/strlist.h b/tools/perf/util/strlist.h
new file mode 100644
index 000000000000..2fdcfee87586
--- /dev/null
+++ b/tools/perf/util/strlist.h
@@ -0,0 +1,32 @@
1#ifndef STRLIST_H_
2#define STRLIST_H_
3
4#include <linux/rbtree.h>
5#include <stdbool.h>
6
7struct str_node {
8 struct rb_node rb_node;
9 const char *s;
10};
11
12struct strlist {
13 struct rb_root entries;
14 bool dupstr;
15};
16
17struct strlist *strlist__new(bool dupstr, const char *slist);
18void strlist__delete(struct strlist *self);
19
20void strlist__remove(struct strlist *self, struct str_node *sn);
21int strlist__load(struct strlist *self, const char *filename);
22int strlist__add(struct strlist *self, const char *str);
23
24bool strlist__has_entry(struct strlist *self, const char *entry);
25
26static inline bool strlist__empty(const struct strlist *self)
27{
28 return rb_first(&self->entries) == NULL;
29}
30
31int strlist__parse_list(struct strlist *self, const char *s);
32#endif /* STRLIST_H_ */
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index 86e14375e74e..4683b67b5ee4 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -35,7 +35,7 @@ static struct symbol *symbol__new(u64 start, u64 len,
35 self = ((void *)self) + priv_size; 35 self = ((void *)self) + priv_size;
36 } 36 }
37 self->start = start; 37 self->start = start;
38 self->end = start + len - 1; 38 self->end = len ? start + len - 1 : start;
39 memcpy(self->name, name, namelen); 39 memcpy(self->name, name, namelen);
40 40
41 return self; 41 return self;
@@ -48,8 +48,12 @@ static void symbol__delete(struct symbol *self, unsigned int priv_size)
48 48
49static size_t symbol__fprintf(struct symbol *self, FILE *fp) 49static size_t symbol__fprintf(struct symbol *self, FILE *fp)
50{ 50{
51 return fprintf(fp, " %llx-%llx %s\n", 51 if (!self->module)
52 return fprintf(fp, " %llx-%llx %s\n",
52 self->start, self->end, self->name); 53 self->start, self->end, self->name);
54 else
55 return fprintf(fp, " %llx-%llx %s \t[%s]\n",
56 self->start, self->end, self->name, self->module->name);
53} 57}
54 58
55struct dso *dso__new(const char *name, unsigned int sym_priv_size) 59struct dso *dso__new(const char *name, unsigned int sym_priv_size)
@@ -146,6 +150,7 @@ static int dso__load_kallsyms(struct dso *self, symbol_filter_t filter, int verb
146 char *line = NULL; 150 char *line = NULL;
147 size_t n; 151 size_t n;
148 FILE *file = fopen("/proc/kallsyms", "r"); 152 FILE *file = fopen("/proc/kallsyms", "r");
153 int count = 0;
149 154
150 if (file == NULL) 155 if (file == NULL)
151 goto out_failure; 156 goto out_failure;
@@ -188,8 +193,10 @@ static int dso__load_kallsyms(struct dso *self, symbol_filter_t filter, int verb
188 193
189 if (filter && filter(self, sym)) 194 if (filter && filter(self, sym))
190 symbol__delete(sym, self->sym_priv_size); 195 symbol__delete(sym, self->sym_priv_size);
191 else 196 else {
192 dso__insert_symbol(self, sym); 197 dso__insert_symbol(self, sym);
198 count++;
199 }
193 } 200 }
194 201
195 /* 202 /*
@@ -212,7 +219,7 @@ static int dso__load_kallsyms(struct dso *self, symbol_filter_t filter, int verb
212 free(line); 219 free(line);
213 fclose(file); 220 fclose(file);
214 221
215 return 0; 222 return count;
216 223
217out_delete_line: 224out_delete_line:
218 free(line); 225 free(line);
@@ -307,6 +314,26 @@ static inline int elf_sym__is_function(const GElf_Sym *sym)
307 sym->st_size != 0; 314 sym->st_size != 0;
308} 315}
309 316
317static inline int elf_sym__is_label(const GElf_Sym *sym)
318{
319 return elf_sym__type(sym) == STT_NOTYPE &&
320 sym->st_name != 0 &&
321 sym->st_shndx != SHN_UNDEF &&
322 sym->st_shndx != SHN_ABS;
323}
324
325static inline const char *elf_sec__name(const GElf_Shdr *shdr,
326 const Elf_Data *secstrs)
327{
328 return secstrs->d_buf + shdr->sh_name;
329}
330
331static inline int elf_sec__is_text(const GElf_Shdr *shdr,
332 const Elf_Data *secstrs)
333{
334 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
335}
336
310static inline const char *elf_sym__name(const GElf_Sym *sym, 337static inline const char *elf_sym__name(const GElf_Sym *sym,
311 const Elf_Data *symstrs) 338 const Elf_Data *symstrs)
312{ 339{
@@ -448,9 +475,9 @@ static int dso__synthesize_plt_symbols(struct dso *self, Elf *elf,
448} 475}
449 476
450static int dso__load_sym(struct dso *self, int fd, const char *name, 477static int dso__load_sym(struct dso *self, int fd, const char *name,
451 symbol_filter_t filter, int verbose) 478 symbol_filter_t filter, int verbose, struct module *mod)
452{ 479{
453 Elf_Data *symstrs; 480 Elf_Data *symstrs, *secstrs;
454 uint32_t nr_syms; 481 uint32_t nr_syms;
455 int err = -1; 482 int err = -1;
456 uint32_t index; 483 uint32_t index;
@@ -458,7 +485,7 @@ static int dso__load_sym(struct dso *self, int fd, const char *name,
458 GElf_Shdr shdr; 485 GElf_Shdr shdr;
459 Elf_Data *syms; 486 Elf_Data *syms;
460 GElf_Sym sym; 487 GElf_Sym sym;
461 Elf_Scn *sec, *sec_dynsym; 488 Elf_Scn *sec, *sec_dynsym, *sec_strndx;
462 Elf *elf; 489 Elf *elf;
463 size_t dynsym_idx; 490 size_t dynsym_idx;
464 int nr = 0; 491 int nr = 0;
@@ -517,15 +544,29 @@ static int dso__load_sym(struct dso *self, int fd, const char *name,
517 if (symstrs == NULL) 544 if (symstrs == NULL)
518 goto out_elf_end; 545 goto out_elf_end;
519 546
547 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
548 if (sec_strndx == NULL)
549 goto out_elf_end;
550
551 secstrs = elf_getdata(sec_strndx, NULL);
552 if (symstrs == NULL)
553 goto out_elf_end;
554
520 nr_syms = shdr.sh_size / shdr.sh_entsize; 555 nr_syms = shdr.sh_size / shdr.sh_entsize;
521 556
522 memset(&sym, 0, sizeof(sym)); 557 memset(&sym, 0, sizeof(sym));
523 558 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
559 elf_section_by_name(elf, &ehdr, &shdr,
560 ".gnu.prelink_undo",
561 NULL) != NULL);
524 elf_symtab__for_each_symbol(syms, nr_syms, index, sym) { 562 elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
525 struct symbol *f; 563 struct symbol *f;
526 u64 obj_start; 564 u64 obj_start;
565 struct section *section = NULL;
566 int is_label = elf_sym__is_label(&sym);
567 const char *section_name;
527 568
528 if (!elf_sym__is_function(&sym)) 569 if (!is_label && !elf_sym__is_function(&sym))
529 continue; 570 continue;
530 571
531 sec = elf_getscn(elf, sym.st_shndx); 572 sec = elf_getscn(elf, sym.st_shndx);
@@ -533,9 +574,31 @@ static int dso__load_sym(struct dso *self, int fd, const char *name,
533 goto out_elf_end; 574 goto out_elf_end;
534 575
535 gelf_getshdr(sec, &shdr); 576 gelf_getshdr(sec, &shdr);
577
578 if (is_label && !elf_sec__is_text(&shdr, secstrs))
579 continue;
580
581 section_name = elf_sec__name(&shdr, secstrs);
536 obj_start = sym.st_value; 582 obj_start = sym.st_value;
537 583
538 sym.st_value -= shdr.sh_addr - shdr.sh_offset; 584 if (self->adjust_symbols) {
585 if (verbose >= 2)
586 printf("adjusting symbol: st_value: %Lx sh_addr: %Lx sh_offset: %Lx\n",
587 (u64)sym.st_value, (u64)shdr.sh_addr, (u64)shdr.sh_offset);
588
589 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
590 }
591
592 if (mod) {
593 section = mod->sections->find_section(mod->sections, section_name);
594 if (section)
595 sym.st_value += section->vma;
596 else {
597 fprintf(stderr, "dso__load_sym() module %s lookup of %s failed\n",
598 mod->name, section_name);
599 goto out_elf_end;
600 }
601 }
539 602
540 f = symbol__new(sym.st_value, sym.st_size, 603 f = symbol__new(sym.st_value, sym.st_size,
541 elf_sym__name(&sym, symstrs), 604 elf_sym__name(&sym, symstrs),
@@ -546,6 +609,7 @@ static int dso__load_sym(struct dso *self, int fd, const char *name,
546 if (filter && filter(self, f)) 609 if (filter && filter(self, f))
547 symbol__delete(f, self->sym_priv_size); 610 symbol__delete(f, self->sym_priv_size);
548 else { 611 else {
612 f->module = mod;
549 dso__insert_symbol(self, f); 613 dso__insert_symbol(self, f);
550 nr++; 614 nr++;
551 } 615 }
@@ -569,6 +633,8 @@ int dso__load(struct dso *self, symbol_filter_t filter, int verbose)
569 if (!name) 633 if (!name)
570 return -1; 634 return -1;
571 635
636 self->adjust_symbols = 0;
637
572 if (strncmp(self->name, "/tmp/perf-", 10) == 0) 638 if (strncmp(self->name, "/tmp/perf-", 10) == 0)
573 return dso__load_perf_map(self, filter, verbose); 639 return dso__load_perf_map(self, filter, verbose);
574 640
@@ -593,7 +659,7 @@ more:
593 fd = open(name, O_RDONLY); 659 fd = open(name, O_RDONLY);
594 } while (fd < 0); 660 } while (fd < 0);
595 661
596 ret = dso__load_sym(self, fd, name, filter, verbose); 662 ret = dso__load_sym(self, fd, name, filter, verbose, NULL);
597 close(fd); 663 close(fd);
598 664
599 /* 665 /*
@@ -607,6 +673,86 @@ out:
607 return ret; 673 return ret;
608} 674}
609 675
676static int dso__load_module(struct dso *self, struct mod_dso *mods, const char *name,
677 symbol_filter_t filter, int verbose)
678{
679 struct module *mod = mod_dso__find_module(mods, name);
680 int err = 0, fd;
681
682 if (mod == NULL || !mod->active)
683 return err;
684
685 fd = open(mod->path, O_RDONLY);
686
687 if (fd < 0)
688 return err;
689
690 err = dso__load_sym(self, fd, name, filter, verbose, mod);
691 close(fd);
692
693 return err;
694}
695
696int dso__load_modules(struct dso *self, symbol_filter_t filter, int verbose)
697{
698 struct mod_dso *mods = mod_dso__new_dso("modules");
699 struct module *pos;
700 struct rb_node *next;
701 int err;
702
703 err = mod_dso__load_modules(mods);
704
705 if (err <= 0)
706 return err;
707
708 /*
709 * Iterate over modules, and load active symbols.
710 */
711 next = rb_first(&mods->mods);
712 while (next) {
713 pos = rb_entry(next, struct module, rb_node);
714 err = dso__load_module(self, mods, pos->name, filter, verbose);
715
716 if (err < 0)
717 break;
718
719 next = rb_next(&pos->rb_node);
720 }
721
722 if (err < 0) {
723 mod_dso__delete_modules(mods);
724 mod_dso__delete_self(mods);
725 }
726
727 return err;
728}
729
730static inline void dso__fill_symbol_holes(struct dso *self)
731{
732 struct symbol *prev = NULL;
733 struct rb_node *nd;
734
735 for (nd = rb_last(&self->syms); nd; nd = rb_prev(nd)) {
736 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
737
738 if (prev) {
739 u64 hole = 0;
740 int alias = pos->start == prev->start;
741
742 if (!alias)
743 hole = prev->start - pos->end - 1;
744
745 if (hole || alias) {
746 if (alias)
747 pos->end = prev->end;
748 else if (hole)
749 pos->end = prev->start - 1;
750 }
751 }
752 prev = pos;
753 }
754}
755
610static int dso__load_vmlinux(struct dso *self, const char *vmlinux, 756static int dso__load_vmlinux(struct dso *self, const char *vmlinux,
611 symbol_filter_t filter, int verbose) 757 symbol_filter_t filter, int verbose)
612{ 758{
@@ -615,21 +761,28 @@ static int dso__load_vmlinux(struct dso *self, const char *vmlinux,
615 if (fd < 0) 761 if (fd < 0)
616 return -1; 762 return -1;
617 763
618 err = dso__load_sym(self, fd, vmlinux, filter, verbose); 764 err = dso__load_sym(self, fd, vmlinux, filter, verbose, NULL);
765
766 if (err > 0)
767 dso__fill_symbol_holes(self);
768
619 close(fd); 769 close(fd);
620 770
621 return err; 771 return err;
622} 772}
623 773
624int dso__load_kernel(struct dso *self, const char *vmlinux, 774int dso__load_kernel(struct dso *self, const char *vmlinux,
625 symbol_filter_t filter, int verbose) 775 symbol_filter_t filter, int verbose, int modules)
626{ 776{
627 int err = -1; 777 int err = -1;
628 778
629 if (vmlinux) 779 if (vmlinux) {
630 err = dso__load_vmlinux(self, vmlinux, filter, verbose); 780 err = dso__load_vmlinux(self, vmlinux, filter, verbose);
781 if (err > 0 && modules)
782 err = dso__load_modules(self, filter, verbose);
783 }
631 784
632 if (err) 785 if (err <= 0)
633 err = dso__load_kallsyms(self, filter, verbose); 786 err = dso__load_kallsyms(self, filter, verbose);
634 787
635 return err; 788 return err;
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index ea332e56e458..7918cffb23cd 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -2,9 +2,10 @@
2#define _PERF_SYMBOL_ 1 2#define _PERF_SYMBOL_ 1
3 3
4#include <linux/types.h> 4#include <linux/types.h>
5#include "../types.h" 5#include "types.h"
6#include "list.h" 6#include <linux/list.h>
7#include "rbtree.h" 7#include <linux/rbtree.h>
8#include "module.h"
8 9
9struct symbol { 10struct symbol {
10 struct rb_node rb_node; 11 struct rb_node rb_node;
@@ -13,6 +14,7 @@ struct symbol {
13 u64 obj_start; 14 u64 obj_start;
14 u64 hist_sum; 15 u64 hist_sum;
15 u64 *hist; 16 u64 *hist;
17 struct module *module;
16 void *priv; 18 void *priv;
17 char name[0]; 19 char name[0];
18}; 20};
@@ -20,8 +22,9 @@ struct symbol {
20struct dso { 22struct dso {
21 struct list_head node; 23 struct list_head node;
22 struct rb_root syms; 24 struct rb_root syms;
23 unsigned int sym_priv_size;
24 struct symbol *(*find_symbol)(struct dso *, u64 ip); 25 struct symbol *(*find_symbol)(struct dso *, u64 ip);
26 unsigned int sym_priv_size;
27 unsigned char adjust_symbols;
25 char name[0]; 28 char name[0];
26}; 29};
27 30
@@ -40,7 +43,8 @@ static inline void *dso__sym_priv(struct dso *self, struct symbol *sym)
40struct symbol *dso__find_symbol(struct dso *self, u64 ip); 43struct symbol *dso__find_symbol(struct dso *self, u64 ip);
41 44
42int dso__load_kernel(struct dso *self, const char *vmlinux, 45int dso__load_kernel(struct dso *self, const char *vmlinux,
43 symbol_filter_t filter, int verbose); 46 symbol_filter_t filter, int verbose, int modules);
47int dso__load_modules(struct dso *self, symbol_filter_t filter, int verbose);
44int dso__load(struct dso *self, symbol_filter_t filter, int verbose); 48int dso__load(struct dso *self, symbol_filter_t filter, int verbose);
45 49
46size_t dso__fprintf(struct dso *self, FILE *fp); 50size_t dso__fprintf(struct dso *self, FILE *fp);
diff --git a/tools/perf/types.h b/tools/perf/util/types.h
index 5e75f9005940..5e75f9005940 100644
--- a/tools/perf/types.h
+++ b/tools/perf/util/types.h
diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
index b8cfed776d81..b4be6071c105 100644
--- a/tools/perf/util/util.h
+++ b/tools/perf/util/util.h
@@ -67,7 +67,6 @@
67#include <assert.h> 67#include <assert.h>
68#include <regex.h> 68#include <regex.h>
69#include <utime.h> 69#include <utime.h>
70#ifndef __MINGW32__
71#include <sys/wait.h> 70#include <sys/wait.h>
72#include <sys/poll.h> 71#include <sys/poll.h>
73#include <sys/socket.h> 72#include <sys/socket.h>
@@ -81,20 +80,6 @@
81#include <netdb.h> 80#include <netdb.h>
82#include <pwd.h> 81#include <pwd.h>
83#include <inttypes.h> 82#include <inttypes.h>
84#if defined(__CYGWIN__)
85#undef _XOPEN_SOURCE
86#include <grp.h>
87#define _XOPEN_SOURCE 600
88#include "compat/cygwin.h"
89#else
90#undef _ALL_SOURCE /* AIX 5.3L defines a struct list with _ALL_SOURCE. */
91#include <grp.h>
92#define _ALL_SOURCE 1
93#endif
94#else /* __MINGW32__ */
95/* pull in Windows compatibility stuff */
96#include "compat/mingw.h"
97#endif /* __MINGW32__ */
98 83
99#ifndef NO_ICONV 84#ifndef NO_ICONV
100#include <iconv.h> 85#include <iconv.h>
diff --git a/tools/perf/util/wrapper.c b/tools/perf/util/wrapper.c
index 6350d65f6d9e..4574ac28396f 100644
--- a/tools/perf/util/wrapper.c
+++ b/tools/perf/util/wrapper.c
@@ -7,7 +7,7 @@
7 * There's no pack memory to release - but stay close to the Git 7 * There's no pack memory to release - but stay close to the Git
8 * version so wrap this away: 8 * version so wrap this away:
9 */ 9 */
10static inline void release_pack_memory(size_t size, int flag) 10static inline void release_pack_memory(size_t size __used, int flag __used)
11{ 11{
12} 12}
13 13
@@ -59,7 +59,8 @@ void *xmemdupz(const void *data, size_t len)
59char *xstrndup(const char *str, size_t len) 59char *xstrndup(const char *str, size_t len)
60{ 60{
61 char *p = memchr(str, '\0', len); 61 char *p = memchr(str, '\0', len);
62 return xmemdupz(str, p ? p - str : len); 62
63 return xmemdupz(str, p ? (size_t)(p - str) : len);
63} 64}
64 65
65void *xrealloc(void *ptr, size_t size) 66void *xrealloc(void *ptr, size_t size)