blob: 4c5e603b56d2a640c14e2a98c5d07819044427db [file] [log] [blame]
Florian Mayerab86d6d2020-11-11 13:30:40 +00001/*
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
Florian Mayerab86d6d2020-11-11 13:30:40 +000017#include <stdio.h>
18#include <stdlib.h>
19#include <unistd.h>
20
21#include <atomic>
Primiano Tucci58d2dc62021-06-24 16:03:24 +010022#include <cinttypes>
Florian Mayer63caf452020-11-11 13:31:24 +000023#include <condition_variable>
Florian Mayerab86d6d2020-11-11 13:30:40 +000024#include <iterator>
Florian Mayer63caf452020-11-11 13:31:24 +000025#include <mutex>
Florian Mayerab86d6d2020-11-11 13:30:40 +000026#include <thread>
27#include <vector>
28
29#include "perfetto/base/logging.h"
30#include "perfetto/base/time.h"
Primiano Tucciccaf6db2021-01-05 22:32:26 +010031#include "perfetto/ext/base/getopt.h"
Florian Mayerab86d6d2020-11-11 13:30:40 +000032#include "perfetto/ext/base/optional.h"
33#include "perfetto/ext/base/string_utils.h"
Florian Mayer5d09f5e2021-02-19 14:59:49 +000034#include "perfetto/heap_profile.h"
Florian Mayerab86d6d2020-11-11 13:30:40 +000035
36namespace {
37
Florian Mayer63caf452020-11-11 13:31:24 +000038void EnabledCallback(void*, const AHeapProfileEnableCallbackInfo*);
39
Florian Mayerab86d6d2020-11-11 13:30:40 +000040std::atomic<bool> done;
41std::atomic<uint64_t> allocs{0};
42
43#pragma GCC diagnostic push
44#pragma GCC diagnostic ignored "-Wglobal-constructors"
Florian Mayer63caf452020-11-11 13:31:24 +000045#pragma GCC diagnostic ignored "-Wexit-time-destructors"
46std::mutex g_wake_up_mutex;
47std::condition_variable g_wake_up_cv;
48uint64_t g_rate = 0;
49
50uint32_t g_heap_id = AHeapProfile_registerHeap(
51 AHeapInfo_setEnabledCallback(AHeapInfo_create("test_heap"),
52 EnabledCallback,
53 nullptr));
54
Florian Mayerab86d6d2020-11-11 13:30:40 +000055#pragma GCC diagnostic pop
56
Florian Mayer63caf452020-11-11 13:31:24 +000057void 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
Florian Mayer2e4bc9e2020-11-16 18:01:01 +000063uint64_t ScrambleAllocId(uint64_t alloc_id, uint32_t thread_idx) {
64 return thread_idx | (~alloc_id << 24);
65}
66
67void Thread(uint32_t thread_idx, uint64_t pending_allocs) {
Florian Mayerab86d6d2020-11-11 13:30:40 +000068 PERFETTO_CHECK(thread_idx < 1 << 24);
Florian Mayer2e4bc9e2020-11-16 18:01:01 +000069 uint64_t alloc_id = 0;
Florian Mayerab86d6d2020-11-11 13:30:40 +000070 size_t thread_allocs = 0;
71 while (!done.load(std::memory_order_relaxed)) {
Florian Mayer2e4bc9e2020-11-16 18:01:01 +000072 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++;
Florian Mayerab86d6d2020-11-11 13:30:40 +000078 thread_allocs++;
79 }
80 allocs.fetch_add(thread_allocs, std::memory_order_relaxed);
81}
82
83} // namespace
84
85int main(int argc, char** argv) {
Florian Mayer2e4bc9e2020-11-16 18:01:01 +000086 if (argc != 4) {
87 PERFETTO_FATAL("%s NUMBER_THREADS RUNTIME_MS PENDING_ALLOCS", argv[0]);
Florian Mayerab86d6d2020-11-11 13:30:40 +000088 }
89
90 perfetto::base::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 perfetto::base::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
Florian Mayer2e4bc9e2020-11-16 18:01:01 +0000104 perfetto::base::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
Florian Mayer63caf452020-11-11 13:31:24 +0000111 std::unique_lock<std::mutex> l(g_wake_up_mutex);
112 g_wake_up_cv.wait(l, [] { return g_rate > 0; });
113
Florian Mayerab86d6d2020-11-11 13:30:40 +0000114 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)
Florian Mayer2e4bc9e2020-11-16 18:01:01 +0000118 threads.emplace_back(Thread, i, pending_allocs);
Florian Mayerab86d6d2020-11-11 13:30:40 +0000119
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
Florian Mayer2e4bc9e2020-11-16 18:01:01 +0000131 printf("%" PRIu64 ",%" PRIu64 ",%" PRIu64 ",%" PRIu64 ",%" PRIu64 "\n",
132 no_threads, runtime_ms, pending_allocs, g_rate,
133 allocs.load(std::memory_order_relaxed));
Florian Mayerab86d6d2020-11-11 13:30:40 +0000134 return 0;
135}