summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Dmitrii Ishcheikin <ishcheikin@google.com> 2024-01-12 14:08:54 +0000
committer Dmitrii Ishcheikin <ishcheikin@google.com> 2024-01-15 14:33:06 +0000
commita8a55bc8c82ab27a576741917cfdb68ee3724974 (patch)
tree3cb1d800835d38ded001a0d2c2ec3dcc40c5e6df
parent3dccb13f4e92db37a13359e126c5ddc12cb674b5 (diff)
Clean-up dlmalloc callbacks
Move Dlmalloc*Callback functions to dlmalloc_space.cc, the only file where they are used. Move ArtDlMallocMoreCore declaration to dlmalloc_space.h to be consistent with the definition. Test: art/test.py -b --host Change-Id: Ib5462c1a3e6881e118cc84960a059001b7d54550
-rw-r--r--runtime/gc/allocator/art-dlmalloc.cc48
-rw-r--r--runtime/gc/allocator/art-dlmalloc.h22
-rw-r--r--runtime/gc/space/dlmalloc_space.cc50
-rw-r--r--runtime/gc/space/dlmalloc_space.h8
4 files changed, 59 insertions, 69 deletions
diff --git a/runtime/gc/allocator/art-dlmalloc.cc b/runtime/gc/allocator/art-dlmalloc.cc
index 62a768db39..86732324d9 100644
--- a/runtime/gc/allocator/art-dlmalloc.cc
+++ b/runtime/gc/allocator/art-dlmalloc.cc
@@ -19,6 +19,7 @@
#include <android-base/logging.h>
#include "base/bit_utils.h"
+#include "gc/space/dlmalloc_space.h"
// ART specific morecore implementation defined in space.cc.
static void* art_heap_morecore(void* m, intptr_t increment);
@@ -57,50 +58,3 @@ static void art_heap_usage_error(const char* function, void* p) {
LOG(FATAL) << "Incorrect use of function '" << function << "' argument " << p
<< " not expected";
}
-
-#include <sys/mman.h>
-
-#include "base/utils.h"
-#include "runtime_globals.h"
-
-extern "C" void DlmallocMadviseCallback(void* start, void* end, size_t used_bytes, void* arg) {
- // Is this chunk in use?
- if (used_bytes != 0) {
- return;
- }
- // Do we have any whole pages to give back?
- start = reinterpret_cast<void*>(art::RoundUp(reinterpret_cast<uintptr_t>(start), art::gPageSize));
- end = reinterpret_cast<void*>(art::RoundDown(reinterpret_cast<uintptr_t>(end), art::gPageSize));
- if (end > start) {
- size_t length = reinterpret_cast<uint8_t*>(end) - reinterpret_cast<uint8_t*>(start);
- int rc = madvise(start, length, MADV_DONTNEED);
- if (UNLIKELY(rc != 0)) {
- errno = rc;
- PLOG(FATAL) << "madvise failed during heap trimming";
- }
- size_t* reclaimed = reinterpret_cast<size_t*>(arg);
- *reclaimed += length;
- }
-}
-
-extern "C" void DlmallocBytesAllocatedCallback([[maybe_unused]] void* start,
- [[maybe_unused]] void* end,
- size_t used_bytes,
- void* arg) {
- if (used_bytes == 0) {
- return;
- }
- size_t* bytes_allocated = reinterpret_cast<size_t*>(arg);
- *bytes_allocated += used_bytes + sizeof(size_t);
-}
-
-extern "C" void DlmallocObjectsAllocatedCallback([[maybe_unused]] void* start,
- [[maybe_unused]] void* end,
- size_t used_bytes,
- void* arg) {
- if (used_bytes == 0) {
- return;
- }
- size_t* objects_allocated = reinterpret_cast<size_t*>(arg);
- ++(*objects_allocated);
-}
diff --git a/runtime/gc/allocator/art-dlmalloc.h b/runtime/gc/allocator/art-dlmalloc.h
index 296de72c70..cffde4d8a5 100644
--- a/runtime/gc/allocator/art-dlmalloc.h
+++ b/runtime/gc/allocator/art-dlmalloc.h
@@ -36,26 +36,4 @@
#include "dlmalloc.h"
#pragma GCC diagnostic pop
-// Callback for dlmalloc_inspect_all or mspace_inspect_all that will madvise(2) unused
-// pages back to the kernel.
-extern "C" void DlmallocMadviseCallback(void* start, void* end, size_t used_bytes, void* /*arg*/);
-
-// Callbacks for dlmalloc_inspect_all or mspace_inspect_all that will
-// count the number of bytes allocated and objects allocated,
-// respectively.
-extern "C" void DlmallocBytesAllocatedCallback(void* start, void* end, size_t used_bytes, void* arg);
-extern "C" void DlmallocObjectsAllocatedCallback(void* start, void* end, size_t used_bytes, void* arg);
-
-namespace art {
-namespace gc {
-namespace allocator {
-
-// Callback from dlmalloc when it needs to increase the footprint. Must be implemented somewhere
-// else (currently dlmalloc_space.cc).
-void* ArtDlMallocMoreCore(void* mspace, intptr_t increment);
-
-} // namespace allocator
-} // namespace gc
-} // namespace art
-
#endif // ART_RUNTIME_GC_ALLOCATOR_ART_DLMALLOC_H_
diff --git a/runtime/gc/space/dlmalloc_space.cc b/runtime/gc/space/dlmalloc_space.cc
index e5253cd697..dda390faca 100644
--- a/runtime/gc/space/dlmalloc_space.cc
+++ b/runtime/gc/space/dlmalloc_space.cc
@@ -16,6 +16,8 @@
#include "dlmalloc_space-inl.h"
+#include <sys/mman.h>
+
#include "base/logging.h" // For VLOG.
#include "base/time_utils.h"
#include "base/utils.h"
@@ -38,6 +40,54 @@ namespace space {
static constexpr bool kPrefetchDuringDlMallocFreeList = true;
+// Callback for mspace_inspect_all that will madvise(2) unused pages back to
+// the kernel.
+void DlmallocMadviseCallback(void* start, void* end, size_t used_bytes, void* arg) {
+ // Is this chunk in use?
+ if (used_bytes != 0) {
+ return;
+ }
+ // Do we have any whole pages to give back?
+ start = reinterpret_cast<void*>(art::RoundUp(reinterpret_cast<uintptr_t>(start), art::gPageSize));
+ end = reinterpret_cast<void*>(art::RoundDown(reinterpret_cast<uintptr_t>(end), art::gPageSize));
+ if (end > start) {
+ size_t length = reinterpret_cast<uint8_t*>(end) - reinterpret_cast<uint8_t*>(start);
+ int rc = madvise(start, length, MADV_DONTNEED);
+ if (UNLIKELY(rc != 0)) {
+ errno = rc;
+ PLOG(FATAL) << "madvise failed during heap trimming";
+ }
+ size_t* reclaimed = reinterpret_cast<size_t*>(arg);
+ *reclaimed += length;
+ }
+}
+
+// Callback for mspace_inspect_all that will count the number of bytes
+// allocated.
+void DlmallocBytesAllocatedCallback([[maybe_unused]] void* start,
+ [[maybe_unused]] void* end,
+ size_t used_bytes,
+ void* arg) {
+ if (used_bytes == 0) {
+ return;
+ }
+ size_t* bytes_allocated = reinterpret_cast<size_t*>(arg);
+ *bytes_allocated += used_bytes + sizeof(size_t);
+}
+
+// Callback for mspace_inspect_all that will count the number of objects
+// allocated.
+void DlmallocObjectsAllocatedCallback([[maybe_unused]] void* start,
+ [[maybe_unused]] void* end,
+ size_t used_bytes,
+ void* arg) {
+ if (used_bytes == 0) {
+ return;
+ }
+ size_t* objects_allocated = reinterpret_cast<size_t*>(arg);
+ ++(*objects_allocated);
+}
+
DlMallocSpace::DlMallocSpace(MemMap&& mem_map,
size_t initial_size,
const std::string& name,
diff --git a/runtime/gc/space/dlmalloc_space.h b/runtime/gc/space/dlmalloc_space.h
index 429b4d035a..82b3cc0b32 100644
--- a/runtime/gc/space/dlmalloc_space.h
+++ b/runtime/gc/space/dlmalloc_space.h
@@ -188,6 +188,14 @@ class DlMallocSpace : public MallocSpace {
};
} // namespace space
+
+namespace allocator {
+
+// Callback from dlmalloc when it needs to increase the footprint.
+// Must be implemented outside of art-dlmalloc.cc.
+void* ArtDlMallocMoreCore(void* mspace, intptr_t increment);
+
+} // namespace allocator
} // namespace gc
} // namespace art