xref: /aosp_15_r20/system/unwinding/libunwindstack/benchmarks/main.cpp (revision eb293b8f56ee8303637c5595cfcdeef8039e85c6)
1 /*
2  * Copyright (C) 2021 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 
17 #include <benchmark/benchmark.h>
18 
19 #include <limits.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 
23 #include <android-base/strings.h>
24 
25 #include <string>
26 #include <vector>
27 
28 #if defined(__APPLE__)
29 
30 // Darwin doesn't support this, so do nothing.
LockToCPU(int)31 bool LockToCPU(int) {
32   return false;
33 }
34 
35 #else
36 
37 #include <errno.h>
38 #include <sched.h>
39 
LockToCPU(int lock_cpu)40 bool LockToCPU(int lock_cpu) {
41   cpu_set_t cpuset;
42 
43   CPU_ZERO(&cpuset);
44   CPU_SET(lock_cpu, &cpuset);
45   if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
46     if (errno == EINVAL) {
47       printf("Invalid cpu %d\n", lock_cpu);
48     } else {
49       perror("sched_setaffinity failed");
50     }
51     return false;
52   }
53 
54   printf("Locked to cpu %d\n", lock_cpu);
55   return true;
56 }
57 
58 #endif
59 
main(int argc,char ** argv)60 int main(int argc, char** argv) {
61 #if defined(__BIONIC__)
62   // Enable decay time option to allow frees to run faster at the cost of slightly increasing RSS.
63   // All applications on Android run with this option enabled.
64   mallopt(M_DECAY_TIME, 1);
65 #endif
66   std::vector<char*> new_argv;
67   // The first argument is not an option, so add it as is.
68   new_argv.push_back(argv[0]);
69 
70   // Look for the special option --benchmark_lock_cpu.
71   int lock_cpu = -1;
72   for (int i = 1; i < argc; i++) {
73     if (android::base::StartsWith(argv[i], "--benchmark_cpu=")) {
74       char* endptr;
75       long cpu = strtol(&argv[i][16], &endptr, 10);
76       if (endptr == nullptr || *endptr != '\0' || cpu > INT_MAX || cpu < 0) {
77         printf("Malformed value for --benchmark_cpu, requires a valid positive number.\n");
78         return 1;
79       }
80       lock_cpu = cpu;
81     } else {
82       new_argv.push_back(argv[i]);
83     }
84   }
85   new_argv.push_back(nullptr);
86 
87   if (lock_cpu != -1 && !LockToCPU(lock_cpu)) {
88     return 1;
89   }
90 
91   int new_argc = new_argv.size() - 1;
92   ::benchmark::Initialize(&new_argc, new_argv.data());
93   if (::benchmark::ReportUnrecognizedArguments(new_argc, new_argv.data())) return 1;
94   ::benchmark::RunSpecifiedBenchmarks();
95 }
96