| Ryan Prichard | 2c236bc | 2019-09-26 12:47:47 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2019 The Android Open Source Project | 
|  | 3 | * All rights reserved. | 
|  | 4 | * | 
|  | 5 | * Redistribution and use in source and binary forms, with or without | 
|  | 6 | * modification, are permitted provided that the following conditions | 
|  | 7 | * are met: | 
|  | 8 | *  * Redistributions of source code must retain the above copyright | 
|  | 9 | *    notice, this list of conditions and the following disclaimer. | 
|  | 10 | *  * Redistributions in binary form must reproduce the above copyright | 
|  | 11 | *    notice, this list of conditions and the following disclaimer in | 
|  | 12 | *    the documentation and/or other materials provided with the | 
|  | 13 | *    distribution. | 
|  | 14 | * | 
|  | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|  | 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|  | 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 
|  | 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | 
|  | 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|  | 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | 
|  | 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | 
|  | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | 
|  | 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
|  | 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | 
|  | 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
|  | 26 | * SUCH DAMAGE. | 
|  | 27 | */ | 
|  | 28 |  | 
|  | 29 | #include <errno.h> | 
|  | 30 | #include <spawn.h> | 
|  | 31 | #include <stdlib.h> | 
|  | 32 | #include <string.h> | 
|  | 33 | #include <sys/types.h> | 
|  | 34 | #include <sys/wait.h> | 
|  | 35 | #include <unistd.h> | 
|  | 36 |  | 
|  | 37 | #include <android-base/file.h> | 
|  | 38 | #include <android-base/stringprintf.h> | 
|  | 39 | #include <benchmark/benchmark.h> | 
|  | 40 |  | 
|  | 41 | static std::string test_program(const char* name) { | 
|  | 42 | #if defined(__LP64__) | 
|  | 43 | return android::base::GetExecutableDirectory() + "/" + name + "64"; | 
|  | 44 | #else | 
|  | 45 | return android::base::GetExecutableDirectory() + "/" + name + "32"; | 
|  | 46 | #endif | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | extern char** environ; | 
|  | 50 |  | 
|  | 51 | static void BM_spawn_test(benchmark::State& state, const char* const* argv) { | 
|  | 52 | for (auto _ : state) { | 
|  | 53 | pid_t child = 0; | 
|  | 54 | if (int spawn_err = posix_spawn(&child, argv[0], nullptr, nullptr, const_cast<char**>(argv), | 
|  | 55 | environ)) { | 
|  | 56 | state.SkipWithError(android::base::StringPrintf( | 
|  | 57 | "posix_spawn of %s failed: %s", argv[0], strerror(spawn_err)).c_str()); | 
|  | 58 | break; | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | int wstatus = 0; | 
|  | 62 | const pid_t wait_result = TEMP_FAILURE_RETRY(waitpid(child, &wstatus, 0)); | 
|  | 63 | if (wait_result != child) { | 
|  | 64 | state.SkipWithError(android::base::StringPrintf( | 
|  | 65 | "waitpid on pid %d for %s failed: %s", | 
|  | 66 | static_cast<int>(child), argv[0], strerror(errno)).c_str()); | 
|  | 67 | break; | 
|  | 68 | } | 
|  | 69 | if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) == 127) { | 
|  | 70 | state.SkipWithError(android::base::StringPrintf("could not exec %s", argv[0]).c_str()); | 
|  | 71 | break; | 
|  | 72 | } | 
|  | 73 | } | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | #define SPAWN_BENCHMARK(name, ...)                                                    \ | 
|  | 77 | BENCHMARK_CAPTURE(BM_spawn_test, name, (const char*[]) { __VA_ARGS__, nullptr })  \ | 
|  | 78 | ->UseRealTime()                                                               \ | 
|  | 79 | ->Unit(benchmark::kMicrosecond)                                               \ | 
|  | 80 |  | 
|  | 81 | SPAWN_BENCHMARK(noop, test_program("bench_noop").c_str()); | 
|  | 82 | SPAWN_BENCHMARK(noop_nostl, test_program("bench_noop_nostl").c_str()); | 
|  | 83 | SPAWN_BENCHMARK(noop_static, test_program("bench_noop_static").c_str()); | 
|  | 84 |  | 
|  | 85 | // Android has a /bin -> /system/bin symlink, but use /system/bin explicitly so we can more easily | 
|  | 86 | // compare Bionic-vs-glibc on a Linux desktop machine. | 
|  | 87 | #if defined(__GLIBC__) | 
|  | 88 |  | 
|  | 89 | SPAWN_BENCHMARK(bin_true, "/bin/true"); | 
|  | 90 | SPAWN_BENCHMARK(sh_true, "/bin/sh", "-c", "true"); | 
|  | 91 |  | 
|  | 92 | #elif defined(__ANDROID__) | 
|  | 93 |  | 
|  | 94 | SPAWN_BENCHMARK(system_bin_true, "/system/bin/true"); | 
|  | 95 | SPAWN_BENCHMARK(vendor_bin_true, "/vendor/bin/true"); | 
|  | 96 | SPAWN_BENCHMARK(system_sh_true, "/system/bin/sh", "-c", "true"); | 
|  | 97 | SPAWN_BENCHMARK(vendor_sh_true, "/vendor/bin/sh", "-c", "true"); | 
|  | 98 |  | 
|  | 99 | #endif | 
|  | 100 |  | 
|  | 101 | BENCHMARK_MAIN(); |