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 <signal.h>
18 #include <stdint.h>
19
20 #include <atomic>
21 #include <thread>
22
23 #include <android-base/threads.h>
24 #include <benchmark/benchmark.h>
25 #include <unwindstack/Memory.h>
26 #include <unwindstack/Unwinder.h>
27
28 constexpr size_t kMaxFrames = 32;
29
ThreadCall6(std::atomic_int * tid,std::atomic_bool * done)30 void ThreadCall6(std::atomic_int* tid, std::atomic_bool* done) {
31 tid->store(android::base::GetThreadId());
32
33 while (!done->load()) {
34 }
35 }
36
ThreadCall5(std::atomic_int * tid,std::atomic_bool * done)37 void ThreadCall5(std::atomic_int* tid, std::atomic_bool* done) {
38 ThreadCall6(tid, done);
39 }
40
ThreadCall4(std::atomic_int * tid,std::atomic_bool * done)41 void ThreadCall4(std::atomic_int* tid, std::atomic_bool* done) {
42 ThreadCall5(tid, done);
43 }
44
ThreadCall3(std::atomic_int * tid,std::atomic_bool * done)45 void ThreadCall3(std::atomic_int* tid, std::atomic_bool* done) {
46 ThreadCall4(tid, done);
47 }
48
ThreadCall2(std::atomic_int * tid,std::atomic_bool * done)49 void ThreadCall2(std::atomic_int* tid, std::atomic_bool* done) {
50 ThreadCall3(tid, done);
51 }
52
ThreadCall1(std::atomic_int * tid,std::atomic_bool * done)53 void ThreadCall1(std::atomic_int* tid, std::atomic_bool* done) {
54 ThreadCall2(tid, done);
55 }
56
BM_thread_unwind(benchmark::State & state)57 static void BM_thread_unwind(benchmark::State& state) {
58 std::atomic_int tid(0);
59 std::atomic_bool done;
60
61 // Create the thread before the unwinder object so all maps are no
62 // longer changing.
63 std::thread thread([&tid, &done] { ThreadCall1(&tid, &done); });
64
65 while (tid.load() == 0) {
66 }
67
68 unwindstack::ThreadUnwinder unwinder(kMaxFrames);
69 if (!unwinder.Init()) {
70 state.SkipWithError("Failed to init.");
71 }
72
73 for (auto _ : state) {
74 unwinder.UnwindWithSignal(SIGRTMIN, tid.load());
75 if (unwinder.NumFrames() < 5) {
76 state.SkipWithError("Failed to unwind.");
77 }
78 }
79
80 done.store(true);
81 thread.join();
82 }
83 BENCHMARK(BM_thread_unwind);
84