1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker * Copyright (C) 2014 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker *
4*8d67ca89SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*8d67ca89SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*8d67ca89SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*8d67ca89SAndroid Build Coastguard Worker *
8*8d67ca89SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*8d67ca89SAndroid Build Coastguard Worker *
10*8d67ca89SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*8d67ca89SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*8d67ca89SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8d67ca89SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*8d67ca89SAndroid Build Coastguard Worker * limitations under the License.
15*8d67ca89SAndroid Build Coastguard Worker */
16*8d67ca89SAndroid Build Coastguard Worker
17*8d67ca89SAndroid Build Coastguard Worker #include <errno.h>
18*8d67ca89SAndroid Build Coastguard Worker #include <string.h>
19*8d67ca89SAndroid Build Coastguard Worker #include <sys/syscall.h>
20*8d67ca89SAndroid Build Coastguard Worker #include <sys/types.h>
21*8d67ca89SAndroid Build Coastguard Worker #include <sys/wait.h>
22*8d67ca89SAndroid Build Coastguard Worker #include <unistd.h>
23*8d67ca89SAndroid Build Coastguard Worker
24*8d67ca89SAndroid Build Coastguard Worker #include <string>
25*8d67ca89SAndroid Build Coastguard Worker
26*8d67ca89SAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
27*8d67ca89SAndroid Build Coastguard Worker #include <benchmark/benchmark.h>
28*8d67ca89SAndroid Build Coastguard Worker #include "util.h"
29*8d67ca89SAndroid Build Coastguard Worker
30*8d67ca89SAndroid Build Coastguard Worker BIONIC_TRIVIAL_BENCHMARK(BM_unistd_getpid, getpid());
31*8d67ca89SAndroid Build Coastguard Worker BIONIC_TRIVIAL_BENCHMARK(BM_unistd_getpid_syscall, syscall(__NR_getpid));
32*8d67ca89SAndroid Build Coastguard Worker
33*8d67ca89SAndroid Build Coastguard Worker // TODO: glibc 2.30 added gettid() too.
34*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
35*8d67ca89SAndroid Build Coastguard Worker BIONIC_TRIVIAL_BENCHMARK(BM_unistd_gettid, gettid());
36*8d67ca89SAndroid Build Coastguard Worker #endif
37*8d67ca89SAndroid Build Coastguard Worker BIONIC_TRIVIAL_BENCHMARK(BM_unistd_gettid_syscall, syscall(__NR_gettid));
38*8d67ca89SAndroid Build Coastguard Worker
39*8d67ca89SAndroid Build Coastguard Worker // Many native allocators have custom prefork and postfork functions.
40*8d67ca89SAndroid Build Coastguard Worker // Measure the fork call to make sure nothing takes too long.
BM_unistd_fork_call(benchmark::State & state)41*8d67ca89SAndroid Build Coastguard Worker void BM_unistd_fork_call(benchmark::State& state) {
42*8d67ca89SAndroid Build Coastguard Worker for (auto _ : state) {
43*8d67ca89SAndroid Build Coastguard Worker pid_t pid;
44*8d67ca89SAndroid Build Coastguard Worker if ((pid = fork()) == 0) {
45*8d67ca89SAndroid Build Coastguard Worker // Sleep for a little while so that the parent is not interrupted
46*8d67ca89SAndroid Build Coastguard Worker // right away when the process exits.
47*8d67ca89SAndroid Build Coastguard Worker usleep(100);
48*8d67ca89SAndroid Build Coastguard Worker _exit(1);
49*8d67ca89SAndroid Build Coastguard Worker }
50*8d67ca89SAndroid Build Coastguard Worker state.PauseTiming();
51*8d67ca89SAndroid Build Coastguard Worker if (pid == -1) {
52*8d67ca89SAndroid Build Coastguard Worker std::string err = android::base::StringPrintf("Fork failed: %s", strerror(errno));
53*8d67ca89SAndroid Build Coastguard Worker state.SkipWithError(err.c_str());
54*8d67ca89SAndroid Build Coastguard Worker }
55*8d67ca89SAndroid Build Coastguard Worker pid_t wait_pid = waitpid(pid, 0, 0);
56*8d67ca89SAndroid Build Coastguard Worker if (wait_pid != pid) {
57*8d67ca89SAndroid Build Coastguard Worker if (wait_pid == -1) {
58*8d67ca89SAndroid Build Coastguard Worker std::string err = android::base::StringPrintf("waitpid call failed: %s", strerror(errno));
59*8d67ca89SAndroid Build Coastguard Worker state.SkipWithError(err.c_str());
60*8d67ca89SAndroid Build Coastguard Worker } else {
61*8d67ca89SAndroid Build Coastguard Worker std::string err = android::base::StringPrintf(
62*8d67ca89SAndroid Build Coastguard Worker "waitpid return an unknown pid, expected %d, actual %d", pid, wait_pid);
63*8d67ca89SAndroid Build Coastguard Worker state.SkipWithError(err.c_str());
64*8d67ca89SAndroid Build Coastguard Worker }
65*8d67ca89SAndroid Build Coastguard Worker }
66*8d67ca89SAndroid Build Coastguard Worker state.ResumeTiming();
67*8d67ca89SAndroid Build Coastguard Worker }
68*8d67ca89SAndroid Build Coastguard Worker }
69*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_unistd_fork_call);
70