blob: 57e564346c636d56b4d4d48d8bd797b992ac6435 [file]
/*
* Copyright (C) 2026 The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <fcntl.h>
#include <malloc.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string>
#include <android-base/file.h>
#include <android-base/stringprintf.h>
#include <benchmark/benchmark.h>
#include "util.h"
#if defined(__BIONIC__)
#include <errno.h>
#include <string.h>
static constexpr char kBenchDispatch[] = "bench-dispatch";
static void BM_malloc_dispatch(benchmark::State& state) {
int fds[2];
std::string dispatch_executable(android::base::GetExecutableDirectory() + "/" + kBenchDispatch);
#if defined(__LP64__)
dispatch_executable += "64";
#else
dispatch_executable += "32";
#endif
char read_value;
int status;
for (auto _ : state) {
state.PauseTiming();
if (pipe(fds) == -1) {
state.SkipWithError("pipe failed");
}
if (setenv("LIBC_HOOKS_ENABLE", "1", true) == -1) {
state.SkipWithError("setenv failed");
}
state.ResumeTiming();
int pid = fork();
if (pid == 0) {
close(fds[0]);
if (dup2(fds[1], STDERR_FILENO) == -1) {
_exit(2);
}
execl(dispatch_executable.c_str(), kBenchDispatch, nullptr);
printf("execl failed %s %s\n", dispatch_executable.c_str(), strerrorname_np(errno));
_exit(2);
} else if (pid == -1) {
state.SkipWithError(android::base::StringPrintf("fork failed: %s", strerrorname_np(errno)));
}
close(fds[1]);
ssize_t bytes_read = read(fds[0], &read_value, 1);
if (bytes_read != 1) {
if (bytes_read <= 0) {
state.SkipWithError(android::base::StringPrintf("read value not 1: %zd", bytes_read));
} else {
state.SkipWithError(
android::base::StringPrintf("read failed: %zd %s", bytes_read, strerrorname_np(errno)));
}
}
if (read_value != 'Y') {
state.SkipWithError(
android::base::StringPrintf("value read from process is not Y, actual %c", read_value));
}
state.PauseTiming();
// Don't include the waiting and process termination to try and reduce
// the amount of variability.
pid_t wait_pid = waitpid(pid, &status, 0);
if (wait_pid != pid) {
state.SkipWithError(android::base::StringPrintf(
"waitpid didn't return expected value: expected %d, actual %d", pid, wait_pid));
}
if (!WIFEXITED(status)) {
state.SkipWithError("forked process did not exit properly");
}
if (WEXITSTATUS(status) != 0) {
state.SkipWithError(android::base::StringPrintf(
"forked process did not exit with 0 status: %d", WEXITSTATUS(status)));
}
state.ResumeTiming();
}
}
BIONIC_BENCHMARK(BM_malloc_dispatch);
#endif