Skip to content

Commit 9ac70ce

Browse files
committed
Basic: Use getrusage() to read memory usage
On macOS 26, malloc_zone_statistics() always returns a malloc_statistics_t with the max_size_in_use field set to zero. This was an intentional change, so let's remove calls to this API. Instead, use the proc_pid_rusage() API and return the ri_lifetime_max_phys_footprint field of rusage_info_v4. Finally, remove the llvm::sys::Process::GetMallocUsage() call on other platforms altogether. It returns the current value and not the max value, so its misleading.
1 parent b570120 commit 9ac70ce

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

lib/Basic/Statistic.cpp

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
#ifdef HAVE_PROC_PID_RUSAGE
4040
#include <libproc.h>
4141
#endif
42-
#ifdef HAVE_MALLOC_MALLOC_H
43-
#include <malloc/malloc.h>
44-
#endif
4542
#if defined(_WIN32)
4643
#define NOMINMAX
4744
#include "Windows.h"
@@ -536,25 +533,16 @@ FrontendStatsTracer::~FrontendStatsTracer()
536533
// associated fields in the provided AlwaysOnFrontendCounters.
537534
void updateProcessWideFrontendCounters(
538535
UnifiedStatsReporter::AlwaysOnFrontendCounters &C) {
539-
if (auto instrExecuted = getInstructionsExecuted()) {
540-
C.NumInstructionsExecuted = instrExecuted;
536+
#if defined(HAVE_PROC_PID_RUSAGE) && defined(RUSAGE_INFO_V4)
537+
struct rusage_info_v4 ru;
538+
if (proc_pid_rusage(getpid(), RUSAGE_INFO_V4, (rusage_info_t *)&ru) == 0) {
539+
C.NumInstructionsExecuted = ru.ri_instructions;
540+
C.MaxMallocUsage = ru.ri_lifetime_max_phys_footprint;
541+
return;
541542
}
542-
543-
#if defined(HAVE_MALLOC_ZONE_STATISTICS) && defined(HAVE_MALLOC_MALLOC_H)
544-
// On Darwin we have a lifetime max that's maintained by malloc we can
545-
// just directly query, even if we only make one query on shutdown.
546-
malloc_statistics_t Stats;
547-
// Query all zones.
548-
malloc_zone_statistics(/*zone=*/NULL, &Stats);
549-
C.MaxMallocUsage = (int64_t)Stats.max_size_in_use;
550-
#else
551-
// If we don't have a malloc-tracked max-usage counter, we have to rely
552-
// on taking the max over current-usage samples while running and hoping
553-
// we get called often enough. This will happen when profiling/tracing,
554-
// but not while doing single-query-on-shutdown collection.
555-
C.MaxMallocUsage = std::max(C.MaxMallocUsage,
556-
(int64_t)llvm::sys::Process::GetMallocUsage());
557543
#endif
544+
545+
// FIXME: Do something useful when the above API is not available.
558546
}
559547

560548
static inline void

0 commit comments

Comments
 (0)