2 * Copyright (C) 2010 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* A simple test of emmc random read and write performance. When testing write
18 * performance, try it twice, once with O_SYNC compiled in, and once with it commented
19 * out. Without O_SYNC, the close(2) blocks until all the dirty buffers are written
20 * out, but the numbers tend to be higher.
23 #define _LARGEFILE64_SOURCE
26 #include <sys/types.h>
34 #define TST_BLK_SIZE 4096
35 /* Number of seconds to run the test */
44 static void usage(void) {
45 fprintf(stderr, "Usage: rand_emmc_perf [ -r | -w ] [-o] [-s count] [-f full_stats_filename] <size_in_mb> <block_dev>\n");
49 static void print_stats(struct stats *stats_buf, int stats_count,
50 char * full_stats_file)
54 struct timeval sum = { 0 };
55 struct timeval max = { 0 };
56 long long total_usecs;
59 long long variance = 0;;
62 FILE *full_stats = NULL;
64 if (full_stats_file) {
65 full_stats = fopen(full_stats_file, "w");
66 if (full_stats == NULL) {
67 fprintf(stderr, "Cannot open full stats output file %s, ignoring\n",
72 for (i = 0; i < stats_count; i++) {
73 timersub(&stats_buf[i].end, &stats_buf[i].start, &t);
74 if (timercmp(&t, &max, >)) {
78 fprintf(full_stats, "%lld\n", (t.tv_sec * 1000000LL) + t.tv_usec);
80 timeradd(&sum, &t, &sum);
87 max_usecs = (max.tv_sec * 1000000LL) + max.tv_usec;
88 total_usecs = (sum.tv_sec * 1000000LL) + sum.tv_usec;
89 avg_usecs = total_usecs / stats_count;
90 printf("average random %d byte iop time = %lld usecs\n",
91 TST_BLK_SIZE, avg_usecs);
92 printf("maximum random %d byte iop time = %lld usecs\n",
93 TST_BLK_SIZE, max_usecs);
95 /* Now that we have the average (aka mean) go through the data
96 * again and compute the standard deviation.
97 * The formula is sqrt(sum_1_to_n((Xi - avg)^2)/n)
99 for (i = 0; i < stats_count; i++) {
100 timersub(&stats_buf[i].end, &stats_buf[i].start, &t); /* Xi */
101 x = (t.tv_sec * 1000000LL) + t.tv_usec; /* Convert to long long */
102 x = x - avg_usecs; /* Xi - avg */
103 x = x * x; /* (Xi - avg) ^ 2 */
104 variance += x; /* Summation */
106 sdev = sqrt((double)variance/(double)stats_count);
107 printf("standard deviation of iops is %.2f\n", sdev);
110 static void stats_test(int fd, int write_mode, off64_t max_blocks, int stats_count,
111 char *full_stats_file)
113 struct stats *stats_buf;
114 char buf[TST_BLK_SIZE] = { 0 };
117 stats_buf = malloc(stats_count * sizeof(struct stats));
118 if (stats_buf == NULL) {
119 fprintf(stderr, "Cannot allocate stats_buf\n");
123 for (i = 0; i < stats_count; i++) {
124 gettimeofday(&stats_buf[i].start, NULL);
126 if (lseek64(fd, (rand() % max_blocks) * TST_BLK_SIZE, SEEK_SET) < 0) {
127 fprintf(stderr, "lseek64 failed\n");
131 if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {
132 fprintf(stderr, "Short write\n");
135 if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
136 fprintf(stderr, "Short read\n");
140 gettimeofday(&stats_buf[i].end, NULL);
143 print_stats(stats_buf, stats_count, full_stats_file);
146 static void perf_test(int fd, int write_mode, off64_t max_blocks)
148 struct timeval start, end, res;
149 char buf[TST_BLK_SIZE] = { 0 };
154 gettimeofday(&start, NULL);
155 while (res.tv_sec < TEST_LEN) {
156 if (lseek64(fd, (rand() % max_blocks) * TST_BLK_SIZE, SEEK_SET) < 0) {
157 fprintf(stderr, "lseek64 failed\n");
160 if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {
161 fprintf(stderr, "Short write\n");
164 if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
165 fprintf(stderr, "Short read\n");
169 gettimeofday(&end, NULL);
170 timersub(&end, &start, &res);
174 /* The close can take a while when in write_mode as buffers are flushed.
175 * So get the time again. */
176 gettimeofday(&end, NULL);
177 timersub(&end, &start, &res);
179 msecs = (res.tv_sec * 1000) + (res.tv_usec / 1000);
180 printf("%.0f %dbyte iops/sec\n", (float)iops * 1000 / msecs, TST_BLK_SIZE);
183 int main(int argc, char *argv[])
190 char *full_stats_file = NULL;
195 while ((c = getopt(argc, argv, "+rwos:f:")) != -1) {
203 /* Do nothing, read mode is the default */
216 stats_count = atoi(optarg);
220 full_stats_file = strdup(optarg);
221 if (full_stats_file == NULL) {
222 fprintf(stderr, "Cannot get full stats filename\n");
228 if (o_sync && !write_mode) {
229 /* Can only specify o_sync in write mode. Probably doesn't matter,
230 * but clear o_sync if in read mode */
234 if ((argc - optind) != 2) {
238 /* Size is given in megabytes, so compute the number of TST_BLK_SIZE blocks. */
239 max_blocks = atoll(argv[optind]) * ((1024*1024) / TST_BLK_SIZE);
241 if ((fd = open(argv[optind + 1], O_RDWR | o_sync)) < 0) {
242 fprintf(stderr, "Cannot open block device %s\n", argv[optind + 1]);
246 fd2 = open("/dev/urandom", O_RDONLY);
248 fprintf(stderr, "Cannot open /dev/urandom\n");
250 if (read(fd2, &seed, sizeof(seed)) != sizeof(seed)) {
251 fprintf(stderr, "Cannot read /dev/urandom\n");
257 stats_test(fd, write_mode, max_blocks, stats_count, full_stats_file);
259 perf_test(fd, write_mode, max_blocks);