| 1 | //===-- memprof_mibmap.cpp -----------------------------------------------===// |
|---|---|
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file is a part of MemProfiler, a memory profiler. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "memprof_mibmap.h" |
| 14 | #include "profile/MemProfData.inc" |
| 15 | #include "sanitizer_common/sanitizer_allocator_internal.h" |
| 16 | #include "sanitizer_common/sanitizer_mutex.h" |
| 17 | |
| 18 | namespace __memprof { |
| 19 | using ::llvm::memprof::MemInfoBlock; |
| 20 | |
| 21 | void InsertOrMerge(const uptr Id, const MemInfoBlock &Block, MIBMapTy &Map) { |
| 22 | MIBMapTy::Handle h(&Map, static_cast<uptr>(Id), /*remove=*/false, |
| 23 | /*create=*/true); |
| 24 | if (h.created()) { |
| 25 | LockedMemInfoBlock *lmib = |
| 26 | (LockedMemInfoBlock *)InternalAlloc(size: sizeof(LockedMemInfoBlock)); |
| 27 | lmib->mutex.Init(); |
| 28 | lmib->mib = Block; |
| 29 | *h = lmib; |
| 30 | } else { |
| 31 | LockedMemInfoBlock *lmib = *h; |
| 32 | SpinMutexLock lock(&lmib->mutex); |
| 33 | uintptr_t ShorterHistogram; |
| 34 | if (Block.AccessHistogramSize > lmib->mib.AccessHistogramSize) |
| 35 | ShorterHistogram = lmib->mib.AccessHistogram; |
| 36 | else |
| 37 | ShorterHistogram = Block.AccessHistogram; |
| 38 | |
| 39 | lmib->mib.Merge(newMIB: Block); |
| 40 | // The larger histogram is kept and the shorter histogram is discarded after |
| 41 | // adding the counters to the larger historam. Free only the shorter |
| 42 | // Histogram |
| 43 | if (Block.AccessHistogramSize > 0 || lmib->mib.AccessHistogramSize > 0) |
| 44 | InternalFree(p: (void *)ShorterHistogram); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | } // namespace __memprof |
| 49 |