diff options
author | Ingo Molnar <mingo@elte.hu> | 2009-11-24 10:36:03 -0500 |
---|---|---|
committer | Ingo Molnar <mingo@elte.hu> | 2009-11-24 10:36:07 -0500 |
commit | 1263d736a9031f3d943819662d4bad727d64bf24 (patch) | |
tree | c740e74d19a0ecadd00bbdb8ad4b5a6b8d297480 | |
parent | 184d3da8ef0ca552dffa0fdd35c046e058a2cf9a (diff) | |
parent | 12eac0bf0461910ae6dd7f071f156f75461a37cf (diff) |
Merge branch 'perf/bench' into perf/core
Merge reason: Looks mergable - ready it for the merge window.
Signed-off-by: Ingo Molnar <mingo@elte.hu>
-rw-r--r-- | tools/perf/Makefile | 1 | ||||
-rw-r--r-- | tools/perf/bench/bench.h | 1 | ||||
-rw-r--r-- | tools/perf/bench/mem-memcpy.c | 193 | ||||
-rw-r--r-- | tools/perf/builtin-bench.c | 15 |
4 files changed, 209 insertions, 1 deletions
diff --git a/tools/perf/Makefile b/tools/perf/Makefile index 3ef6621bf6cd..f1537a94a05f 100644 --- a/tools/perf/Makefile +++ b/tools/perf/Makefile | |||
@@ -419,6 +419,7 @@ BUILTIN_OBJS += builtin-bench.o | |||
419 | # Benchmark modules | 419 | # Benchmark modules |
420 | BUILTIN_OBJS += bench/sched-messaging.o | 420 | BUILTIN_OBJS += bench/sched-messaging.o |
421 | BUILTIN_OBJS += bench/sched-pipe.o | 421 | BUILTIN_OBJS += bench/sched-pipe.o |
422 | BUILTIN_OBJS += bench/mem-memcpy.o | ||
422 | 423 | ||
423 | BUILTIN_OBJS += builtin-help.o | 424 | BUILTIN_OBJS += builtin-help.o |
424 | BUILTIN_OBJS += builtin-sched.o | 425 | BUILTIN_OBJS += builtin-sched.o |
diff --git a/tools/perf/bench/bench.h b/tools/perf/bench/bench.h index 9fbd8d745fa1..f7781c6267c0 100644 --- a/tools/perf/bench/bench.h +++ b/tools/perf/bench/bench.h | |||
@@ -3,6 +3,7 @@ | |||
3 | 3 | ||
4 | extern int bench_sched_messaging(int argc, const char **argv, const char *prefix); | 4 | extern int bench_sched_messaging(int argc, const char **argv, const char *prefix); |
5 | extern int bench_sched_pipe(int argc, const char **argv, const char *prefix); | 5 | extern int bench_sched_pipe(int argc, const char **argv, const char *prefix); |
6 | extern int bench_mem_memcpy(int argc, const char **argv, const char *prefix __used); | ||
6 | 7 | ||
7 | #define BENCH_FORMAT_DEFAULT_STR "default" | 8 | #define BENCH_FORMAT_DEFAULT_STR "default" |
8 | #define BENCH_FORMAT_DEFAULT 0 | 9 | #define BENCH_FORMAT_DEFAULT 0 |
diff --git a/tools/perf/bench/mem-memcpy.c b/tools/perf/bench/mem-memcpy.c new file mode 100644 index 000000000000..5165fd1d8d2c --- /dev/null +++ b/tools/perf/bench/mem-memcpy.c | |||
@@ -0,0 +1,193 @@ | |||
1 | /* | ||
2 | * mem-memcpy.c | ||
3 | * | ||
4 | * memcpy: Simple memory copy in various ways | ||
5 | * | ||
6 | * Written by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp> | ||
7 | */ | ||
8 | #include <ctype.h> | ||
9 | |||
10 | #include "../perf.h" | ||
11 | #include "../util/util.h" | ||
12 | #include "../util/parse-options.h" | ||
13 | #include "../util/string.h" | ||
14 | #include "../util/header.h" | ||
15 | #include "bench.h" | ||
16 | |||
17 | #include <stdio.h> | ||
18 | #include <stdlib.h> | ||
19 | #include <string.h> | ||
20 | #include <sys/time.h> | ||
21 | #include <errno.h> | ||
22 | |||
23 | #define K 1024 | ||
24 | |||
25 | static const char *length_str = "1MB"; | ||
26 | static const char *routine = "default"; | ||
27 | static int use_clock = 0; | ||
28 | static int clock_fd; | ||
29 | |||
30 | static const struct option options[] = { | ||
31 | OPT_STRING('l', "length", &length_str, "1MB", | ||
32 | "Specify length of memory to copy. " | ||
33 | "available unit: B, MB, GB (upper and lower)"), | ||
34 | OPT_STRING('r', "routine", &routine, "default", | ||
35 | "Specify routine to copy"), | ||
36 | OPT_BOOLEAN('c', "clock", &use_clock, | ||
37 | "Use CPU clock for measuring"), | ||
38 | OPT_END() | ||
39 | }; | ||
40 | |||
41 | struct routine { | ||
42 | const char *name; | ||
43 | const char *desc; | ||
44 | void * (*fn)(void *dst, const void *src, size_t len); | ||
45 | }; | ||
46 | |||
47 | struct routine routines[] = { | ||
48 | { "default", | ||
49 | "Default memcpy() provided by glibc", | ||
50 | memcpy }, | ||
51 | { NULL, | ||
52 | NULL, | ||
53 | NULL } | ||
54 | }; | ||
55 | |||
56 | static const char * const bench_mem_memcpy_usage[] = { | ||
57 | "perf bench mem memcpy <options>", | ||
58 | NULL | ||
59 | }; | ||
60 | |||
61 | static struct perf_event_attr clock_attr = { | ||
62 | .type = PERF_TYPE_HARDWARE, | ||
63 | .config = PERF_COUNT_HW_CPU_CYCLES | ||
64 | }; | ||
65 | |||
66 | static void init_clock(void) | ||
67 | { | ||
68 | clock_fd = sys_perf_event_open(&clock_attr, getpid(), -1, -1, 0); | ||
69 | |||
70 | if (clock_fd < 0 && errno == ENOSYS) | ||
71 | die("No CONFIG_PERF_EVENTS=y kernel support configured?\n"); | ||
72 | else | ||
73 | BUG_ON(clock_fd < 0); | ||
74 | } | ||
75 | |||
76 | static u64 get_clock(void) | ||
77 | { | ||
78 | int ret; | ||
79 | u64 clk; | ||
80 | |||
81 | ret = read(clock_fd, &clk, sizeof(u64)); | ||
82 | BUG_ON(ret != sizeof(u64)); | ||
83 | |||
84 | return clk; | ||
85 | } | ||
86 | |||
87 | static double timeval2double(struct timeval *ts) | ||
88 | { | ||
89 | return (double)ts->tv_sec + | ||
90 | (double)ts->tv_usec / (double)1000000; | ||
91 | } | ||
92 | |||
93 | int bench_mem_memcpy(int argc, const char **argv, | ||
94 | const char *prefix __used) | ||
95 | { | ||
96 | int i; | ||
97 | void *dst, *src; | ||
98 | size_t length; | ||
99 | double bps = 0.0; | ||
100 | struct timeval tv_start, tv_end, tv_diff; | ||
101 | u64 clock_start, clock_end, clock_diff; | ||
102 | |||
103 | clock_start = clock_end = clock_diff = 0ULL; | ||
104 | argc = parse_options(argc, argv, options, | ||
105 | bench_mem_memcpy_usage, 0); | ||
106 | |||
107 | tv_diff.tv_sec = 0; | ||
108 | tv_diff.tv_usec = 0; | ||
109 | length = (size_t)perf_atoll((char *)length_str); | ||
110 | |||
111 | if ((s64)length <= 0) { | ||
112 | fprintf(stderr, "Invalid length:%s\n", length_str); | ||
113 | return 1; | ||
114 | } | ||
115 | |||
116 | for (i = 0; routines[i].name; i++) { | ||
117 | if (!strcmp(routines[i].name, routine)) | ||
118 | break; | ||
119 | } | ||
120 | if (!routines[i].name) { | ||
121 | printf("Unknown routine:%s\n", routine); | ||
122 | printf("Available routines...\n"); | ||
123 | for (i = 0; routines[i].name; i++) { | ||
124 | printf("\t%s ... %s\n", | ||
125 | routines[i].name, routines[i].desc); | ||
126 | } | ||
127 | return 1; | ||
128 | } | ||
129 | |||
130 | dst = calloc(length, sizeof(char)); | ||
131 | if (!dst) | ||
132 | die("memory allocation failed - maybe length is too large?\n"); | ||
133 | |||
134 | src = calloc(length, sizeof(char)); | ||
135 | if (!src) | ||
136 | die("memory allocation failed - maybe length is too large?\n"); | ||
137 | |||
138 | if (bench_format == BENCH_FORMAT_DEFAULT) { | ||
139 | printf("# Copying %s Bytes from %p to %p ...\n\n", | ||
140 | length_str, src, dst); | ||
141 | } | ||
142 | |||
143 | if (use_clock) { | ||
144 | init_clock(); | ||
145 | clock_start = get_clock(); | ||
146 | } else { | ||
147 | BUG_ON(gettimeofday(&tv_start, NULL)); | ||
148 | } | ||
149 | |||
150 | routines[i].fn(dst, src, length); | ||
151 | |||
152 | if (use_clock) { | ||
153 | clock_end = get_clock(); | ||
154 | clock_diff = clock_end - clock_start; | ||
155 | } else { | ||
156 | BUG_ON(gettimeofday(&tv_end, NULL)); | ||
157 | timersub(&tv_end, &tv_start, &tv_diff); | ||
158 | bps = (double)((double)length / timeval2double(&tv_diff)); | ||
159 | } | ||
160 | |||
161 | switch (bench_format) { | ||
162 | case BENCH_FORMAT_DEFAULT: | ||
163 | if (use_clock) { | ||
164 | printf(" %14lf Clock/Byte\n", | ||
165 | (double)clock_diff / (double)length); | ||
166 | } else { | ||
167 | if (bps < K) | ||
168 | printf(" %14lf B/Sec\n", bps); | ||
169 | else if (bps < K * K) | ||
170 | printf(" %14lfd KB/Sec\n", bps / 1024); | ||
171 | else if (bps < K * K * K) | ||
172 | printf(" %14lf MB/Sec\n", bps / 1024 / 1024); | ||
173 | else { | ||
174 | printf(" %14lf GB/Sec\n", | ||
175 | bps / 1024 / 1024 / 1024); | ||
176 | } | ||
177 | } | ||
178 | break; | ||
179 | case BENCH_FORMAT_SIMPLE: | ||
180 | if (use_clock) { | ||
181 | printf("%14lf\n", | ||
182 | (double)clock_diff / (double)length); | ||
183 | } else | ||
184 | printf("%lf\n", bps); | ||
185 | break; | ||
186 | default: | ||
187 | /* reaching this means there's some disaster: */ | ||
188 | die("unknown format: %d\n", bench_format); | ||
189 | break; | ||
190 | } | ||
191 | |||
192 | return 0; | ||
193 | } | ||
diff --git a/tools/perf/builtin-bench.c b/tools/perf/builtin-bench.c index 90c39baae0de..e043eb83092a 100644 --- a/tools/perf/builtin-bench.c +++ b/tools/perf/builtin-bench.c | |||
@@ -12,6 +12,7 @@ | |||
12 | * | 12 | * |
13 | * Available subsystem list: | 13 | * Available subsystem list: |
14 | * sched ... scheduler and IPC mechanism | 14 | * sched ... scheduler and IPC mechanism |
15 | * mem ... memory access performance | ||
15 | * | 16 | * |
16 | */ | 17 | */ |
17 | 18 | ||
@@ -43,6 +44,15 @@ static struct bench_suite sched_suites[] = { | |||
43 | NULL } | 44 | NULL } |
44 | }; | 45 | }; |
45 | 46 | ||
47 | static struct bench_suite mem_suites[] = { | ||
48 | { "memcpy", | ||
49 | "Simple memory copy in various ways", | ||
50 | bench_mem_memcpy }, | ||
51 | { NULL, | ||
52 | NULL, | ||
53 | NULL } | ||
54 | }; | ||
55 | |||
46 | struct bench_subsys { | 56 | struct bench_subsys { |
47 | const char *name; | 57 | const char *name; |
48 | const char *summary; | 58 | const char *summary; |
@@ -53,9 +63,12 @@ static struct bench_subsys subsystems[] = { | |||
53 | { "sched", | 63 | { "sched", |
54 | "scheduler and IPC mechanism", | 64 | "scheduler and IPC mechanism", |
55 | sched_suites }, | 65 | sched_suites }, |
66 | { "mem", | ||
67 | "memory access performance", | ||
68 | mem_suites }, | ||
56 | { NULL, | 69 | { NULL, |
57 | NULL, | 70 | NULL, |
58 | NULL } | 71 | NULL } |
59 | }; | 72 | }; |
60 | 73 | ||
61 | static void dump_suites(int subsys_index) | 74 | static void dump_suites(int subsys_index) |