1From 880ad50536799f214c98d040fbc74cf707b991a9 Mon Sep 17 00:00:00 2001
2From: Khem Raj <raj.khem@gmail.com>
3Date: Wed, 13 Feb 2019 09:51:14 -0800
4Subject: [PATCH] mallinfo implementation is glibc specific
5
6Upstream-Status: Pending
7Signed-off-by: Randy MacLeod <randy.macleod@windriver.com>
8Signed-off-by: Khem Raj <raj.khem@gmail.com>
9
10---
11 base/process/process_metrics_posix.cc                     | 8 ++++----
12 base/trace_event/malloc_dump_provider.cc                  | 3 +++
13 .../llvm-10.0/configs/linux/include/llvm/Config/config.h  | 2 ++
14 .../third_party/llvm-subzero/lib/Support/Unix/Process.inc | 4 ++--
15 .../tflite/src/tensorflow/lite/profiling/memory_info.cc   | 2 +-
16 5 files changed, 12 insertions(+), 7 deletions(-)
17
18diff --git a/base/process/process_metrics_posix.cc b/base/process/process_metrics_posix.cc
19index 873a328aa2..5d7d8c11ad 100644
20--- a/base/process/process_metrics_posix.cc
21+++ b/base/process/process_metrics_posix.cc
22@@ -107,7 +107,7 @@ void IncreaseFdLimitTo(unsigned int max_descriptors) {
23
24 #endif  // !BUILDFLAG(IS_FUCHSIA)
25
26-#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
27+#if (BUILDFLAG(IS_LINUX) && defined(__GLIBC__)) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
28 namespace {
29
30 size_t GetMallocUsageMallinfo() {
31@@ -125,7 +125,7 @@ size_t GetMallocUsageMallinfo() {
32 }
33
34 }  // namespace
35-#endif  // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) ||
36+#endif  // (BUILDFLAG(IS_LINUX) && defined(__GLIBC__)) || BUILDFLAG(IS_CHROMEOS) ||
37         // BUILDFLAG(IS_ANDROID)
38
39 size_t ProcessMetrics::GetMallocUsage() {
40@@ -133,9 +133,9 @@ size_t ProcessMetrics::GetMallocUsage() {
41   malloc_statistics_t stats = {0};
42   malloc_zone_statistics(nullptr, &stats);
43   return stats.size_in_use;
44-#elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
45+#elif (BUILDFLAG(IS_LINUX) && defined(__GLIBC__)) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
46   return GetMallocUsageMallinfo();
47-#elif BUILDFLAG(IS_FUCHSIA)
48+#else
49   // TODO(fuchsia): Not currently exposed. https://crbug.com/735087.
50   return 0;
51 #endif
52diff --git a/base/trace_event/malloc_dump_provider.cc b/base/trace_event/malloc_dump_provider.cc
53index f056277d4b..515d779cce 100644
54--- a/base/trace_event/malloc_dump_provider.cc
55+++ b/base/trace_event/malloc_dump_provider.cc
56@@ -186,6 +186,7 @@ void ReportAppleAllocStats(size_t* total_virtual_size,
57
58 #if (BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) && BUILDFLAG(IS_ANDROID)) || \
59     (!BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) && !BUILDFLAG(IS_WIN) &&    \
60+     defined(__GLIBC__) && \
61      !BUILDFLAG(IS_APPLE) && !BUILDFLAG(IS_FUCHSIA))
62 void ReportMallinfoStats(ProcessMemoryDump* pmd,
63                          size_t* total_virtual_size,
64@@ -358,8 +359,10 @@ bool MallocDumpProvider::OnMemoryDump(const MemoryDumpArgs& args,
65 #elif BUILDFLAG(IS_FUCHSIA)
66 // TODO(fuchsia): Port, see https://crbug.com/706592.
67 #else
68+#ifdef __GLIBC__
69   ReportMallinfoStats(/*pmd=*/nullptr, &total_virtual_size, &resident_size,
70                       &allocated_objects_size, &allocated_objects_count);
71+#endif
72 #endif
73
74   MemoryAllocatorDump* outer_dump = pmd->CreateAllocatorDump("malloc");
75diff --git a/third_party/swiftshader/third_party/llvm-10.0/configs/linux/include/llvm/Config/config.h b/third_party/swiftshader/third_party/llvm-10.0/configs/linux/include/llvm/Config/config.h
76index 7392898797..0bf7c4ad99 100644
77--- a/third_party/swiftshader/third_party/llvm-10.0/configs/linux/include/llvm/Config/config.h
78+++ b/third_party/swiftshader/third_party/llvm-10.0/configs/linux/include/llvm/Config/config.h
79@@ -125,7 +125,9 @@
80 /* #undef HAVE_MALLCTL */
81
82 /* Define to 1 if you have the `mallinfo' function. */
83+#if defined(__GLIBC__)
84 #define HAVE_MALLINFO 1
85+#endif
86
87 /* Define to 1 if you have the <malloc/malloc.h> header file. */
88 /* #undef HAVE_MALLOC_MALLOC_H */
89diff --git a/third_party/swiftshader/third_party/llvm-subzero/lib/Support/Unix/Process.inc b/third_party/swiftshader/third_party/llvm-subzero/lib/Support/Unix/Process.inc
90index 1a767bcbd5..1ba48de49b 100644
91--- a/third_party/swiftshader/third_party/llvm-subzero/lib/Support/Unix/Process.inc
92+++ b/third_party/swiftshader/third_party/llvm-subzero/lib/Support/Unix/Process.inc
93@@ -86,11 +86,11 @@ unsigned Process::getPageSize() {
94 }
95
96 size_t Process::GetMallocUsage() {
97-#if defined(HAVE_MALLINFO2)
98+#if defined(HAVE_MALLINFO2) && defined(__GLIBC__)
99   struct mallinfo2 mi;
100   mi = ::mallinfo2();
101   return mi.uordblks;
102-#elif defined(HAVE_MALLINFO)
103+#elif defined(HAVE_MALLINFO) && defined(__GLIBC__)
104   struct mallinfo mi;
105   mi = ::mallinfo();
106   return mi.uordblks;
107diff --git a/third_party/tflite/src/tensorflow/lite/profiling/memory_info.cc b/third_party/tflite/src/tensorflow/lite/profiling/memory_info.cc
108index 4f1d517869..0afd270817 100644
109--- a/third_party/tflite/src/tensorflow/lite/profiling/memory_info.cc
110+++ b/third_party/tflite/src/tensorflow/lite/profiling/memory_info.cc
111@@ -38,7 +38,7 @@ bool MemoryUsage::IsSupported() {
112
113 MemoryUsage GetMemoryUsage() {
114   MemoryUsage result;
115-#ifdef __linux__
116+#if defined(__linux__) && defined(__GLIBC__)
117   rusage res;
118   if (getrusage(RUSAGE_SELF, &res) == 0) {
119     result.mem_footprint_kb = res.ru_maxrss;
120