xref: /aosp_15_r20/external/perfetto/src/tools/multithreaded_alloc.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2020 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 <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 
21 #include <atomic>
22 #include <cinttypes>
23 #include <condition_variable>
24 #include <iterator>
25 #include <mutex>
26 #include <optional>
27 #include <thread>
28 #include <vector>
29 
30 #include "perfetto/base/logging.h"
31 #include "perfetto/base/time.h"
32 #include "perfetto/ext/base/getopt.h"
33 #include "perfetto/ext/base/string_utils.h"
34 #include "perfetto/heap_profile.h"
35 
36 namespace {
37 
38 void EnabledCallback(void*, const AHeapProfileEnableCallbackInfo*);
39 
40 std::atomic<bool> done;
41 std::atomic<uint64_t> allocs{0};
42 
43 #pragma GCC diagnostic push
44 #pragma GCC diagnostic ignored "-Wglobal-constructors"
45 #pragma GCC diagnostic ignored "-Wexit-time-destructors"
46 std::mutex g_wake_up_mutex;
47 std::condition_variable g_wake_up_cv;
48 uint64_t g_rate = 0;
49 
50 uint32_t g_heap_id = AHeapProfile_registerHeap(
51     AHeapInfo_setEnabledCallback(AHeapInfo_create("test_heap"),
52                                  EnabledCallback,
53                                  nullptr));
54 
55 #pragma GCC diagnostic pop
56 
EnabledCallback(void *,const AHeapProfileEnableCallbackInfo * info)57 void EnabledCallback(void*, const AHeapProfileEnableCallbackInfo* info) {
58   std::lock_guard<std::mutex> l(g_wake_up_mutex);
59   g_rate = AHeapProfileEnableCallbackInfo_getSamplingInterval(info);
60   g_wake_up_cv.notify_all();
61 }
62 
ScrambleAllocId(uint64_t alloc_id,uint32_t thread_idx)63 uint64_t ScrambleAllocId(uint64_t alloc_id, uint32_t thread_idx) {
64   return thread_idx | (~alloc_id << 24);
65 }
66 
Thread(uint32_t thread_idx,uint64_t pending_allocs)67 void Thread(uint32_t thread_idx, uint64_t pending_allocs) {
68   PERFETTO_CHECK(thread_idx < 1 << 24);
69   uint64_t alloc_id = 0;
70   size_t thread_allocs = 0;
71   while (!done.load(std::memory_order_relaxed)) {
72     AHeapProfile_reportAllocation(g_heap_id,
73                                   ScrambleAllocId(alloc_id, thread_idx), 1);
74     if (alloc_id > pending_allocs)
75       AHeapProfile_reportFree(
76           g_heap_id, ScrambleAllocId(alloc_id - pending_allocs, thread_idx));
77     alloc_id++;
78     thread_allocs++;
79   }
80   allocs.fetch_add(thread_allocs, std::memory_order_relaxed);
81 }
82 
83 }  // namespace
84 
main(int argc,char ** argv)85 int main(int argc, char** argv) {
86   if (argc != 4) {
87     PERFETTO_FATAL("%s NUMBER_THREADS RUNTIME_MS PENDING_ALLOCS", argv[0]);
88   }
89 
90   std::optional<uint64_t> opt_no_threads =
91       perfetto::base::CStringToUInt64(argv[1]);
92   if (!opt_no_threads) {
93     PERFETTO_FATAL("Invalid number of threads: %s", argv[1]);
94   }
95   uint64_t no_threads = *opt_no_threads;
96 
97   std::optional<uint64_t> opt_runtime_ms =
98       perfetto::base::CStringToUInt64(argv[2]);
99   if (!opt_runtime_ms) {
100     PERFETTO_FATAL("Invalid runtime: %s", argv[2]);
101   }
102   uint64_t runtime_ms = *opt_runtime_ms;
103 
104   std::optional<uint64_t> opt_pending_allocs =
105       perfetto::base::CStringToUInt64(argv[3]);
106   if (!opt_runtime_ms) {
107     PERFETTO_FATAL("Invalid number of pending allocs: %s", argv[3]);
108   }
109   uint64_t pending_allocs = *opt_pending_allocs;
110 
111   std::unique_lock<std::mutex> l(g_wake_up_mutex);
112   g_wake_up_cv.wait(l, [] { return g_rate > 0; });
113 
114   perfetto::base::TimeMillis end =
115       perfetto::base::GetWallTimeMs() + perfetto::base::TimeMillis(runtime_ms);
116   std::vector<std::thread> threads;
117   for (size_t i = 0; i < static_cast<size_t>(no_threads); ++i)
118     threads.emplace_back(Thread, i, pending_allocs);
119 
120   perfetto::base::TimeMillis current = perfetto::base::GetWallTimeMs();
121   while (current < end) {
122     usleep(useconds_t((end - current).count()) * 1000);
123     current = perfetto::base::GetWallTimeMs();
124   }
125 
126   done.store(true, std::memory_order_relaxed);
127 
128   for (std::thread& th : threads)
129     th.join();
130 
131   printf("%" PRIu64 ",%" PRIu64 ",%" PRIu64 ",%" PRIu64 ",%" PRIu64 "\n",
132          no_threads, runtime_ms, pending_allocs, g_rate,
133          allocs.load(std::memory_order_relaxed));
134   return 0;
135 }
136