aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf
diff options
context:
space:
mode:
Diffstat (limited to 'tools/perf')
-rw-r--r--tools/perf/Makefile1
-rw-r--r--tools/perf/bench/bench.h1
-rw-r--r--tools/perf/bench/mem-memcpy.c186
-rw-r--r--tools/perf/builtin-bench.c15
4 files changed, 202 insertions, 1 deletions
diff --git a/tools/perf/Makefile b/tools/perf/Makefile
index 3f0666af93de..53e663a5fa2f 100644
--- a/tools/perf/Makefile
+++ b/tools/perf/Makefile
@@ -432,6 +432,7 @@ BUILTIN_OBJS += builtin-bench.o
432# Benchmark modules 432# Benchmark modules
433BUILTIN_OBJS += bench/sched-messaging.o 433BUILTIN_OBJS += bench/sched-messaging.o
434BUILTIN_OBJS += bench/sched-pipe.o 434BUILTIN_OBJS += bench/sched-pipe.o
435BUILTIN_OBJS += bench/mem-memcpy.o
435 436
436BUILTIN_OBJS += builtin-help.o 437BUILTIN_OBJS += builtin-help.o
437BUILTIN_OBJS += builtin-sched.o 438BUILTIN_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
4extern int bench_sched_messaging(int argc, const char **argv, const char *prefix); 4extern int bench_sched_messaging(int argc, const char **argv, const char *prefix);
5extern int bench_sched_pipe(int argc, const char **argv, const char *prefix); 5extern int bench_sched_pipe(int argc, const char **argv, const char *prefix);
6extern 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..d4f4f9806ae4
--- /dev/null
+++ b/tools/perf/bench/mem-memcpy.c
@@ -0,0 +1,186 @@
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
25static const char *length_str = "1MB";
26static const char *routine = "default";
27static int use_clock = 0;
28
29static const struct option options[] = {
30 OPT_STRING('l', "length", &length_str, "1MB",
31 "Specify length of memory to copy. "
32 "available unit: B, MB, GB (upper and lower)"),
33 OPT_STRING('r', "routine", &routine, "default",
34 "Specify routine to copy"),
35 OPT_BOOLEAN('c', "clock", &use_clock,
36 "Use CPU clock for measuring"),
37 OPT_END()
38};
39
40struct routine {
41 const char *name;
42 const char *desc;
43 void * (*fn)(void *dst, const void *src, size_t len);
44};
45
46struct routine routines[] = {
47 { "default",
48 "Default memcpy() provided by glibc",
49 memcpy },
50 { NULL,
51 NULL,
52 NULL }
53};
54
55static const char * const bench_mem_memcpy_usage[] = {
56 "perf bench mem memcpy <options>",
57 NULL
58};
59
60static int clock_fd;
61
62static struct perf_event_attr clock_attr = {
63 .type = PERF_TYPE_HARDWARE,
64 .config = PERF_COUNT_HW_CPU_CYCLES
65};
66
67static void init_clock(void)
68{
69 clock_fd = sys_perf_event_open(&clock_attr, getpid(), -1, -1, 0);
70 BUG_ON(clock_fd < 0);
71}
72
73static u64 get_clock(void)
74{
75 int ret;
76 u64 clk;
77
78 ret = read(clock_fd, &clk, sizeof(u64));
79 BUG_ON(ret != sizeof(u64));
80
81 return clk;
82}
83
84static double timeval2double(struct timeval *ts)
85{
86 return (double)ts->tv_sec +
87 (double)ts->tv_usec / (double)1000000;
88}
89
90int bench_mem_memcpy(int argc, const char **argv,
91 const char *prefix __used)
92{
93 int i;
94 void *dst, *src;
95 size_t length;
96 double bps = 0.0;
97 struct timeval tv_start, tv_end, tv_diff;
98 u64 clock_start, clock_end, clock_diff;
99
100 clock_start = clock_end = clock_diff = 0ULL;
101 argc = parse_options(argc, argv, options,
102 bench_mem_memcpy_usage, 0);
103
104 tv_diff.tv_sec = 0;
105 tv_diff.tv_usec = 0;
106 length = (size_t)perf_atoll((char *)length_str);
107 if ((long long int)length <= 0) {
108 fprintf(stderr, "Invalid length:%s\n", length_str);
109 return 1;
110 }
111
112 for (i = 0; routines[i].name; i++) {
113 if (!strcmp(routines[i].name, routine))
114 break;
115 }
116 if (!routines[i].name) {
117 printf("Unknown routine:%s\n", routine);
118 printf("Available routines...\n");
119 for (i = 0; routines[i].name; i++) {
120 printf("\t%s ... %s\n",
121 routines[i].name, routines[i].desc);
122 }
123 return 1;
124 }
125
126 dst = calloc(length, sizeof(char));
127 assert(dst);
128 src = calloc(length, sizeof(char));
129 assert(src);
130
131 if (bench_format == BENCH_FORMAT_DEFAULT) {
132 printf("# Copying %s Bytes from %p to %p ...\n\n",
133 length_str, src, dst);
134 }
135
136 if (use_clock) {
137 init_clock();
138 clock_start = get_clock();
139 } else
140 BUG_ON(gettimeofday(&tv_start, NULL));
141
142 routines[i].fn(dst, src, length);
143
144 if (use_clock) {
145 clock_end = get_clock();
146 clock_diff = clock_end - clock_start;
147 } else {
148 BUG_ON(gettimeofday(&tv_end, NULL));
149 timersub(&tv_end, &tv_start, &tv_diff);
150 bps = (double)((double)length / timeval2double(&tv_diff));
151 }
152
153 switch (bench_format) {
154 case BENCH_FORMAT_DEFAULT:
155 if (use_clock) {
156 printf(" %14lf Clock/Byte\n",
157 (double)clock_diff / (double)length);
158 } else {
159 if (bps < K)
160 printf(" %14lf B/Sec\n", bps);
161 else if (bps < K * K)
162 printf(" %14lfd KB/Sec\n", bps / 1024);
163 else if (bps < K * K * K)
164 printf(" %14lf MB/Sec\n", bps / 1024 / 1024);
165 else {
166 printf(" %14lf GB/Sec\n",
167 bps / 1024 / 1024 / 1024);
168 }
169 }
170 break;
171 case BENCH_FORMAT_SIMPLE:
172 if (use_clock) {
173 printf("%14lf\n",
174 (double)clock_diff / (double)length);
175 } else
176 printf("%lf\n", bps);
177 break;
178 default:
179 /* reaching here is something disaster */
180 fprintf(stderr, "Unknown format:%d\n", bench_format);
181 exit(1);
182 break;
183 }
184
185 return 0;
186}
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
47static 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
46struct bench_subsys { 56struct 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
61static void dump_suites(int subsys_index) 74static void dump_suites(int subsys_index)