blob: f0a308959e200ff32ff4982fd34be4408f0569a4 [file] [log] [blame]
Elliott Hughesb28e4902014-03-11 11:19:06 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
Christopher Ferris89379352020-01-29 15:21:09 -080017#include <errno.h>
18#include <string.h>
Elliott Hughes7634db52014-06-09 18:35:21 -070019#include <sys/syscall.h>
Christopher Ferris89379352020-01-29 15:21:09 -080020#include <sys/types.h>
21#include <sys/wait.h>
Elliott Hughesb28e4902014-03-11 11:19:06 -070022#include <unistd.h>
23
Christopher Ferris89379352020-01-29 15:21:09 -080024#include <string>
25
26#include <android-base/stringprintf.h>
Elliott Hughes281e06b2016-02-17 10:23:52 -080027#include <benchmark/benchmark.h>
Anders Lewisa7b0f882017-07-24 20:01:13 -070028#include "util.h"
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080029
Elliott Hughes96705e32019-09-26 07:42:23 -070030BIONIC_TRIVIAL_BENCHMARK(BM_unistd_getpid, getpid());
31BIONIC_TRIVIAL_BENCHMARK(BM_unistd_getpid_syscall, syscall(__NR_getpid));
Elliott Hughes5d9a7ba2014-05-30 19:00:03 -070032
Elliott Hughes96705e32019-09-26 07:42:23 -070033// TODO: glibc 2.30 added gettid() too.
Elliott Hughes212e0e32014-12-01 16:43:51 -080034#if defined(__BIONIC__)
Elliott Hughes96705e32019-09-26 07:42:23 -070035BIONIC_TRIVIAL_BENCHMARK(BM_unistd_gettid, gettid());
Elliott Hughes212e0e32014-12-01 16:43:51 -080036#endif
Elliott Hughes96705e32019-09-26 07:42:23 -070037BIONIC_TRIVIAL_BENCHMARK(BM_unistd_gettid_syscall, syscall(__NR_gettid));
Christopher Ferris89379352020-01-29 15:21:09 -080038
39// Many native allocators have custom prefork and postfork functions.
40// Measure the fork call to make sure nothing takes too long.
41void BM_unistd_fork_call(benchmark::State& state) {
42 for (auto _ : state) {
43 pid_t pid;
44 if ((pid = fork()) == 0) {
45 // Sleep for a little while so that the parent is not interrupted
46 // right away when the process exits.
47 usleep(100);
48 _exit(1);
49 }
50 state.PauseTiming();
51 if (pid == -1) {
52 std::string err = android::base::StringPrintf("Fork failed: %s", strerror(errno));
53 state.SkipWithError(err.c_str());
54 }
55 pid_t wait_pid = waitpid(pid, 0, 0);
56 if (wait_pid != pid) {
57 if (wait_pid == -1) {
58 std::string err = android::base::StringPrintf("waitpid call failed: %s", strerror(errno));
59 state.SkipWithError(err.c_str());
60 } else {
61 std::string err = android::base::StringPrintf(
62 "waitpid return an unknown pid, expected %d, actual %d", pid, wait_pid);
63 state.SkipWithError(err.c_str());
64 }
65 }
66 state.ResumeTiming();
67 }
68}
69BIONIC_BENCHMARK(BM_unistd_fork_call);