OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 // Some portions Copyright (c) 2009 The Chromium Authors. |
| 5 // |
| 6 // Microbenchmark of basic read/write throughput |
| 7 #include "microbenchmark/microbenchmark.h" |
| 8 |
| 9 #include <fcntl.h> |
| 10 #include <stdio.h> |
| 11 #include <stdlib.h> |
| 12 #include <sys/stat.h> |
| 13 #include <sys/types.h> |
| 14 #include <sys/unistd.h> |
| 15 #include <syscall.h> |
| 16 #include <time.h> |
| 17 |
| 18 namespace chromeos { |
| 19 namespace benchmarks { |
| 20 |
| 21 #define IO_SIZE 4096 |
| 22 #define CALL_SYSCALL(_SCAF, syscallargs...) \ |
| 23 (_SCAF ? IO_SIZE : syscall(syscallargs)) |
| 24 static long kDefaultAmount = 0x4000; |
| 25 static const char *kDevUrandom = "/dev/urandom"; |
| 26 static const char *kDevNull = "/dev/null"; |
| 27 |
| 28 // Lovely globals used from Setup->Test. |
| 29 static int rfd = -1; |
| 30 static int wfd = -1; |
| 31 static long amount = kDefaultAmount; |
| 32 |
| 33 static void ReadWriteSetup(uint64 runs) { |
| 34 CommandLine *cl = CommandLine::ForCurrentProcess(); |
| 35 std::string amount_str = cl->GetSwitchValueASCII("readwrite-amount"); |
| 36 std::string source = cl->GetSwitchValueASCII("readwrite-source"); |
| 37 std::string destination = cl->GetSwitchValueASCII("readwrite-destination"); |
| 38 |
| 39 if (amount_str.empty()) { |
| 40 LOG(INFO) << "--readwrite-amount is missing."; |
| 41 LOG(INFO) << "Defaulting to " << amount << " bytes"; |
| 42 } else { |
| 43 amount = strtol(amount_str.c_str(), NULL, 0); |
| 44 if (amount < 0 || amount > 0xffffff) { |
| 45 LOG(ERROR) << "Amount specified was too large: " << amount; |
| 46 LOG(INFO) << "Using default amount of " << kDefaultAmount; |
| 47 } |
| 48 amount = kDefaultAmount; |
| 49 } |
| 50 if (source.empty()) { |
| 51 LOG(INFO) << "--readwrite-source missing."; |
| 52 LOG(INFO) << "Defaulting to /dev/urandom as a source"; |
| 53 source = kDevUrandom; |
| 54 } |
| 55 if (destination.empty()) { |
| 56 LOG(INFO) << "--readwrite-destination missing."; |
| 57 LOG(INFO) << "Defaulting to /dev/null as a destination"; |
| 58 destination = kDevNull; |
| 59 } |
| 60 wfd = open("/dev/null", O_WRONLY); |
| 61 rfd = open("/dev/zero", O_RDONLY); |
| 62 LOG_IF(FATAL, wfd < 0 || rfd < 0) << "Failed to open /dev/null or /dev/zero"; |
| 63 } |
| 64 |
| 65 static void ReadWrite(bool scaffold_only) { |
| 66 ssize_t so_far = 0; |
| 67 ssize_t bytes = 0; |
| 68 char buf[IO_SIZE]; |
| 69 while (so_far < amount) { |
| 70 bytes = CALL_SYSCALL(scaffold_only, __NR_read, rfd, buf, sizeof(buf)); |
| 71 PLOG_IF(FATAL, bytes < 0) << "An unexpected error occurred during read."; |
| 72 so_far += bytes; |
| 73 PLOG_IF(FATAL, |
| 74 CALL_SYSCALL(scaffold_only, __NR_write, wfd, buf, bytes) != bytes) |
| 75 << "An unexpected error occurred during write."; |
| 76 } |
| 77 } |
| 78 CHROMEOS_MICROBENCHMARK_WITH_SETUP(ReadWriteSetup, ReadWrite, 1000); |
| 79 #undef IO_SIZE |
| 80 #undef CALL_SYSCALL |
| 81 |
| 82 } // namespace benchmarks |
| 83 } // namespace chromeos |
OLD | NEW |