Florian Mayer | ab86d6d | 2020-11-11 13:30:40 +0000 | [diff] [blame] | 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 | |
Florian Mayer | ab86d6d | 2020-11-11 13:30:40 +0000 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <unistd.h> |
| 20 | |
| 21 | #include <atomic> |
Primiano Tucci | 58d2dc6 | 2021-06-24 16:03:24 +0100 | [diff] [blame] | 22 | #include <cinttypes> |
Florian Mayer | 63caf45 | 2020-11-11 13:31:24 +0000 | [diff] [blame] | 23 | #include <condition_variable> |
Florian Mayer | ab86d6d | 2020-11-11 13:30:40 +0000 | [diff] [blame] | 24 | #include <iterator> |
Florian Mayer | 63caf45 | 2020-11-11 13:31:24 +0000 | [diff] [blame] | 25 | #include <mutex> |
Florian Mayer | ab86d6d | 2020-11-11 13:30:40 +0000 | [diff] [blame] | 26 | #include <thread> |
| 27 | #include <vector> |
| 28 | |
| 29 | #include "perfetto/base/logging.h" |
| 30 | #include "perfetto/base/time.h" |
Primiano Tucci | ccaf6db | 2021-01-05 22:32:26 +0100 | [diff] [blame] | 31 | #include "perfetto/ext/base/getopt.h" |
Florian Mayer | ab86d6d | 2020-11-11 13:30:40 +0000 | [diff] [blame] | 32 | #include "perfetto/ext/base/optional.h" |
| 33 | #include "perfetto/ext/base/string_utils.h" |
Florian Mayer | 5d09f5e | 2021-02-19 14:59:49 +0000 | [diff] [blame] | 34 | #include "perfetto/heap_profile.h" |
Florian Mayer | ab86d6d | 2020-11-11 13:30:40 +0000 | [diff] [blame] | 35 | |
| 36 | namespace { |
| 37 | |
Florian Mayer | 63caf45 | 2020-11-11 13:31:24 +0000 | [diff] [blame] | 38 | void EnabledCallback(void*, const AHeapProfileEnableCallbackInfo*); |
| 39 | |
Florian Mayer | ab86d6d | 2020-11-11 13:30:40 +0000 | [diff] [blame] | 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" |
Florian Mayer | 63caf45 | 2020-11-11 13:31:24 +0000 | [diff] [blame] | 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 | |
Florian Mayer | ab86d6d | 2020-11-11 13:30:40 +0000 | [diff] [blame] | 55 | #pragma GCC diagnostic pop |
| 56 | |
Florian Mayer | 63caf45 | 2020-11-11 13:31:24 +0000 | [diff] [blame] | 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 | |
Florian Mayer | 2e4bc9e | 2020-11-16 18:01:01 +0000 | [diff] [blame] | 63 | uint64_t ScrambleAllocId(uint64_t alloc_id, uint32_t thread_idx) { |
| 64 | return thread_idx | (~alloc_id << 24); |
| 65 | } |
| 66 | |
| 67 | void Thread(uint32_t thread_idx, uint64_t pending_allocs) { |
Florian Mayer | ab86d6d | 2020-11-11 13:30:40 +0000 | [diff] [blame] | 68 | PERFETTO_CHECK(thread_idx < 1 << 24); |
Florian Mayer | 2e4bc9e | 2020-11-16 18:01:01 +0000 | [diff] [blame] | 69 | uint64_t alloc_id = 0; |
Florian Mayer | ab86d6d | 2020-11-11 13:30:40 +0000 | [diff] [blame] | 70 | size_t thread_allocs = 0; |
| 71 | while (!done.load(std::memory_order_relaxed)) { |
Florian Mayer | 2e4bc9e | 2020-11-16 18:01:01 +0000 | [diff] [blame] | 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++; |
Florian Mayer | ab86d6d | 2020-11-11 13:30:40 +0000 | [diff] [blame] | 78 | thread_allocs++; |
| 79 | } |
| 80 | allocs.fetch_add(thread_allocs, std::memory_order_relaxed); |
| 81 | } |
| 82 | |
| 83 | } // namespace |
| 84 | |
| 85 | int main(int argc, char** argv) { |
Florian Mayer | 2e4bc9e | 2020-11-16 18:01:01 +0000 | [diff] [blame] | 86 | if (argc != 4) { |
| 87 | PERFETTO_FATAL("%s NUMBER_THREADS RUNTIME_MS PENDING_ALLOCS", argv[0]); |
Florian Mayer | ab86d6d | 2020-11-11 13:30:40 +0000 | [diff] [blame] | 88 | } |
| 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 Mayer | 2e4bc9e | 2020-11-16 18:01:01 +0000 | [diff] [blame] | 104 | 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 Mayer | 63caf45 | 2020-11-11 13:31:24 +0000 | [diff] [blame] | 111 | std::unique_lock<std::mutex> l(g_wake_up_mutex); |
| 112 | g_wake_up_cv.wait(l, [] { return g_rate > 0; }); |
| 113 | |
Florian Mayer | ab86d6d | 2020-11-11 13:30:40 +0000 | [diff] [blame] | 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) |
Florian Mayer | 2e4bc9e | 2020-11-16 18:01:01 +0000 | [diff] [blame] | 118 | threads.emplace_back(Thread, i, pending_allocs); |
Florian Mayer | ab86d6d | 2020-11-11 13:30:40 +0000 | [diff] [blame] | 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 | |
Florian Mayer | 2e4bc9e | 2020-11-16 18:01:01 +0000 | [diff] [blame] | 131 | printf("%" PRIu64 ",%" PRIu64 ",%" PRIu64 ",%" PRIu64 ",%" PRIu64 "\n", |
| 132 | no_threads, runtime_ms, pending_allocs, g_rate, |
| 133 | allocs.load(std::memory_order_relaxed)); |
Florian Mayer | ab86d6d | 2020-11-11 13:30:40 +0000 | [diff] [blame] | 134 | return 0; |
| 135 | } |