Add native memory accounting through custom allocator.

Added a custom allocator that lets you pass in a special tag which
specifices where the allocation came from. This is used when
dumping. The performance overhead is low since each allocation only
does a atomic add/sub for each allocation/free.

The measurements are dumped to traces.txt during SIGQUIT.

Example output:
I/art     (27274): AllocatorTagHeap active=120 max=120 total=168
I/art     (27274): AllocatorTagMonitorList active=1572 max=6240 total=11724
I/art     (27274): AllocatorTagClassTable active=185208 max=185208 total=268608
I/art     (27274): AllocatorTagInternTable active=430368 max=430368 total=436080
I/art     (27274): AllocatorTagMaps active=5616 max=6168 total=34392
I/art     (27274): AllocatorTagLOS active=1024 max=1536 total=2044
I/art     (27274): AllocatorTagSafeMap active=0 max=51936 total=533688
I/art     (27274): AllocatorTagLOSMaps active=144 max=1248 total=5760
I/art     (27274): AllocatorTagReferenceTable active=10944 max=11840 total=19136
I/art     (27274): AllocatorTagHeapBitmap active=32 max=40 total=56
I/art     (27274): AllocatorTagHeapBitmapLOS active=8 max=8 total=8
I/art     (27274): AllocatorTagVerifier active=0 max=18844 total=1073156
I/art     (27274): AllocatorTagModUnionCardSet active=5300 max=5920 total=56020
I/art     (27274): AllocatorTagModUnionReferenceArray active=24864 max=24864 total=24864
I/art     (27274): AllocatorTagJNILibrarires active=320 max=320 total=320
I/art     (27274): AllocatorTagOatFile active=1400 max=1400 total=5852

Change-Id: Ibb470ef2e9c9a24563bb46422d46a55799704d82

(cherry picked from commit 5369c40f75fdcb1be7a7c06db212ce965c83a164)
diff --git a/runtime/gc/accounting/gc_allocator.cc b/runtime/gc/accounting/gc_allocator.cc
deleted file mode 100644
index ff6a135..0000000
--- a/runtime/gc/accounting/gc_allocator.cc
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * Copyright (C) 2013 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "gc_allocator.h"
-#include "gc/allocator/dlmalloc.h"
-#include "gc/heap.h"
-#include "runtime.h"
-
-namespace art {
-namespace gc {
-namespace accounting {
-
-void* RegisterGcAllocation(size_t bytes) {
-  gc::Heap* heap = Runtime::Current()->GetHeap();
-  if (heap != nullptr) {
-    heap->RegisterGCAllocation(bytes);
-  }
-  return malloc(bytes);
-}
-
-void RegisterGcDeallocation(void* p, size_t bytes) {
-  gc::Heap* heap = Runtime::Current()->GetHeap();
-  if (heap != nullptr) {
-    heap->RegisterGCDeAllocation(bytes);
-  }
-  free(p);
-}
-
-}  // namespace accounting
-}  // namespace gc
-}  // namespace art
diff --git a/runtime/gc/accounting/gc_allocator.h b/runtime/gc/accounting/gc_allocator.h
deleted file mode 100644
index d4142f8..0000000
--- a/runtime/gc/accounting/gc_allocator.h
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- * Copyright (C) 2013 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef ART_RUNTIME_GC_ACCOUNTING_GC_ALLOCATOR_H_
-#define ART_RUNTIME_GC_ACCOUNTING_GC_ALLOCATOR_H_
-
-#include "utils.h"
-
-#include <cstdlib>
-#include <limits>
-#include <memory>
-
-namespace art {
-namespace gc {
-namespace accounting {
-
-void* RegisterGcAllocation(size_t bytes);
-void RegisterGcDeallocation(void* p, size_t bytes);
-
-static constexpr bool kMeasureGcMemoryOverhead = false;
-
-template <typename T>
-class GcAllocatorImpl : public std::allocator<T> {
- public:
-  typedef typename std::allocator<T>::value_type value_type;
-  typedef typename std::allocator<T>::size_type size_type;
-  typedef typename std::allocator<T>::difference_type difference_type;
-  typedef typename std::allocator<T>::pointer pointer;
-  typedef typename std::allocator<T>::const_pointer const_pointer;
-  typedef typename std::allocator<T>::reference reference;
-  typedef typename std::allocator<T>::const_reference const_reference;
-
-  // Used internally by STL data structures.
-  template <class U>
-  GcAllocatorImpl(const GcAllocatorImpl<U>& alloc) throw() {
-  }
-
-  // Used internally by STL data structures.
-  GcAllocatorImpl() throw() {
-  }
-
-  // Enables an allocator for objects of one type to allocate storage for objects of another type.
-  // Used internally by STL data structures.
-  template <class U>
-  struct rebind {
-    typedef GcAllocatorImpl<U> other;
-  };
-
-  pointer allocate(size_type n, const_pointer hint = 0) {
-    return reinterpret_cast<pointer>(RegisterGcAllocation(n * sizeof(T)));
-  }
-
-  template <typename PT>
-  void deallocate(PT p, size_type n) {
-    RegisterGcDeallocation(p, n * sizeof(T));
-  }
-};
-
-// C++ doesn't allow template typedefs. This is a workaround template typedef which is
-// GCAllocatorImpl<T> if kMeasureGCMemoryOverhead is true, std::allocator<T> otherwise.
-template <typename T>
-class GcAllocator : public TypeStaticIf<kMeasureGcMemoryOverhead, GcAllocatorImpl<T>,
-                                        std::allocator<T>>::type {
-};
-
-}  // namespace accounting
-}  // namespace gc
-}  // namespace art
-
-#endif  // ART_RUNTIME_GC_ACCOUNTING_GC_ALLOCATOR_H_
diff --git a/runtime/gc/accounting/heap_bitmap.h b/runtime/gc/accounting/heap_bitmap.h
index 814dc06..ca6dc46 100644
--- a/runtime/gc/accounting/heap_bitmap.h
+++ b/runtime/gc/accounting/heap_bitmap.h
@@ -17,8 +17,8 @@
 #ifndef ART_RUNTIME_GC_ACCOUNTING_HEAP_BITMAP_H_
 #define ART_RUNTIME_GC_ACCOUNTING_HEAP_BITMAP_H_
 
+#include "base/allocator.h"
 #include "base/logging.h"
-#include "gc_allocator.h"
 #include "object_callbacks.h"
 #include "space_bitmap.h"
 
@@ -68,11 +68,14 @@
   void RemoveLargeObjectBitmap(LargeObjectBitmap* bitmap);
 
   // Bitmaps covering continuous spaces.
-  std::vector<ContinuousSpaceBitmap*, GcAllocator<ContinuousSpaceBitmap*>>
+  std::vector<ContinuousSpaceBitmap*,
+              TrackingAllocator<ContinuousSpaceBitmap*, kAllocatorTagHeapBitmap>>
       continuous_space_bitmaps_;
 
   // Sets covering discontinuous spaces.
-  std::vector<LargeObjectBitmap*, GcAllocator<LargeObjectBitmap*>> large_object_bitmaps_;
+  std::vector<LargeObjectBitmap*,
+              TrackingAllocator<LargeObjectBitmap*, kAllocatorTagHeapBitmapLOS>>
+      large_object_bitmaps_;
 
   friend class art::gc::Heap;
 };
diff --git a/runtime/gc/accounting/mod_union_table.h b/runtime/gc/accounting/mod_union_table.h
index f67dc27..d0e11e0 100644
--- a/runtime/gc/accounting/mod_union_table.h
+++ b/runtime/gc/accounting/mod_union_table.h
@@ -17,7 +17,7 @@
 #ifndef ART_RUNTIME_GC_ACCOUNTING_MOD_UNION_TABLE_H_
 #define ART_RUNTIME_GC_ACCOUNTING_MOD_UNION_TABLE_H_
 
-#include "gc_allocator.h"
+#include "base/allocator.h"
 #include "globals.h"
 #include "object_callbacks.h"
 #include "safe_map.h"
@@ -50,7 +50,8 @@
 // cleared between GC phases, reducing the number of dirty cards that need to be scanned.
 class ModUnionTable {
  public:
-  typedef std::set<byte*, std::less<byte*>, GcAllocator<byte*>> CardSet;
+  typedef std::set<byte*, std::less<byte*>,
+                   TrackingAllocator<byte*, kAllocatorTagModUnionCardSet>> CardSet;
 
   explicit ModUnionTable(const std::string& name, Heap* heap, space::ContinuousSpace* space)
       : name_(name),
@@ -130,9 +131,8 @@
   ModUnionTable::CardSet cleared_cards_;
 
   // Maps from dirty cards to their corresponding alloc space references.
-  SafeMap<const byte*, std::vector<mirror::HeapReference<mirror::Object>*>, std::less<const byte*>,
-      GcAllocator<std::pair<const byte*, std::vector<mirror::HeapReference<mirror::Object>*>>> >
-      references_;
+  AllocationTrackingSafeMap<const byte*, std::vector<mirror::HeapReference<mirror::Object>*>,
+                            kAllocatorTagModUnionReferenceArray> references_;
 };
 
 // Card caching implementation. Keeps track of which cards we cleared and only this information.
diff --git a/runtime/gc/accounting/remembered_set.h b/runtime/gc/accounting/remembered_set.h
index 706cf35..8d66e0e 100644
--- a/runtime/gc/accounting/remembered_set.h
+++ b/runtime/gc/accounting/remembered_set.h
@@ -17,7 +17,7 @@
 #ifndef ART_RUNTIME_GC_ACCOUNTING_REMEMBERED_SET_H_
 #define ART_RUNTIME_GC_ACCOUNTING_REMEMBERED_SET_H_
 
-#include "gc_allocator.h"
+#include "base/allocator.h"
 #include "globals.h"
 #include "object_callbacks.h"
 #include "safe_map.h"
@@ -43,7 +43,8 @@
 // from the free list spaces to the bump pointer spaces.
 class RememberedSet {
  public:
-  typedef std::set<byte*, std::less<byte*>, GcAllocator<byte*>> CardSet;
+  typedef std::set<byte*, std::less<byte*>,
+                   TrackingAllocator<byte*, kAllocatorTagRememberedSet>> CardSet;
 
   explicit RememberedSet(const std::string& name, Heap* heap, space::ContinuousSpace* space)
       : name_(name), heap_(heap), space_(space) {}
diff --git a/runtime/gc/accounting/space_bitmap.h b/runtime/gc/accounting/space_bitmap.h
index a3073bd..f72b30f 100644
--- a/runtime/gc/accounting/space_bitmap.h
+++ b/runtime/gc/accounting/space_bitmap.h
@@ -24,7 +24,6 @@
 #include <vector>
 
 #include "base/mutex.h"
-#include "gc_allocator.h"
 #include "globals.h"
 #include "object_callbacks.h"
 
diff --git a/runtime/gc/heap.cc b/runtime/gc/heap.cc
index 6a91501..cd066fc 100644
--- a/runtime/gc/heap.cc
+++ b/runtime/gc/heap.cc
@@ -23,6 +23,7 @@
 #include <memory>
 #include <vector>
 
+#include "base/allocator.h"
 #include "base/histogram-inl.h"
 #include "base/stl_util.h"
 #include "common_throws.h"
@@ -146,7 +147,6 @@
       total_objects_freed_ever_(0),
       num_bytes_allocated_(0),
       native_bytes_allocated_(0),
-      gc_memory_overhead_(0),
       verify_missing_card_marks_(false),
       verify_system_weaks_(false),
       verify_pre_gc_heap_(verify_pre_gc_heap),
@@ -790,14 +790,6 @@
   }
 }
 
-void Heap::RegisterGCAllocation(size_t bytes) {
-  gc_memory_overhead_.FetchAndAddSequentiallyConsistent(bytes);
-}
-
-void Heap::RegisterGCDeAllocation(size_t bytes) {
-  gc_memory_overhead_.FetchAndSubSequentiallyConsistent(bytes);
-}
-
 void Heap::DumpGcPerformanceInfo(std::ostream& os) {
   // Dump cumulative timings.
   os << "Dumping cumulative Gc timings\n";
@@ -839,7 +831,6 @@
   }
   os << "Total mutator paused time: " << PrettyDuration(total_paused_time) << "\n";
   os << "Total time waiting for GC to complete: " << PrettyDuration(total_wait_time_) << "\n";
-  os << "Approximate GC data structures memory overhead: " << gc_memory_overhead_.LoadRelaxed();
   BaseMutex::DumpAll(os);
 }
 
diff --git a/runtime/gc/heap.h b/runtime/gc/heap.h
index 10ec76f..8f55c46 100644
--- a/runtime/gc/heap.h
+++ b/runtime/gc/heap.h
@@ -810,10 +810,12 @@
   std::unique_ptr<accounting::CardTable> card_table_;
 
   // A mod-union table remembers all of the references from the it's space to other spaces.
-  SafeMap<space::Space*, accounting::ModUnionTable*> mod_union_tables_;
+  AllocationTrackingSafeMap<space::Space*, accounting::ModUnionTable*, kAllocatorTagHeap>
+      mod_union_tables_;
 
   // A remembered set remembers all of the references from the it's space to the target space.
-  SafeMap<space::Space*, accounting::RememberedSet*> remembered_sets_;
+  AllocationTrackingSafeMap<space::Space*, accounting::RememberedSet*, kAllocatorTagHeap>
+      remembered_sets_;
 
   // The current collector type.
   CollectorType collector_type_;
@@ -917,9 +919,6 @@
   // Bytes which are allocated and managed by native code but still need to be accounted for.
   Atomic<size_t> native_bytes_allocated_;
 
-  // Data structure GC overhead.
-  Atomic<size_t> gc_memory_overhead_;
-
   // Info related to the current or previous GC iteration.
   collector::Iteration current_gc_iteration_;
 
diff --git a/runtime/gc/space/large_object_space.h b/runtime/gc/space/large_object_space.h
index b1c20ca..09a0919 100644
--- a/runtime/gc/space/large_object_space.h
+++ b/runtime/gc/space/large_object_space.h
@@ -17,7 +17,7 @@
 #ifndef ART_RUNTIME_GC_SPACE_LARGE_OBJECT_SPACE_H_
 #define ART_RUNTIME_GC_SPACE_LARGE_OBJECT_SPACE_H_
 
-#include "gc/accounting/gc_allocator.h"
+#include "base/allocator.h"
 #include "dlmalloc_space.h"
 #include "safe_map.h"
 #include "space.h"
@@ -135,10 +135,10 @@
 
   // Used to ensure mutual exclusion when the allocation spaces data structures are being modified.
   mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
-  std::vector<mirror::Object*,
-      accounting::GcAllocator<mirror::Object*>> large_objects_ GUARDED_BY(lock_);
+  std::vector<mirror::Object*, TrackingAllocator<mirror::Object*, kAllocatorTagLOS>> large_objects_
+      GUARDED_BY(lock_);
   typedef SafeMap<mirror::Object*, MemMap*, std::less<mirror::Object*>,
-      accounting::GcAllocator<std::pair<mirror::Object*, MemMap*>>> MemMaps;
+      TrackingAllocator<std::pair<mirror::Object*, MemMap*>, kAllocatorTagLOSMaps>> MemMaps;
   MemMaps mem_maps_ GUARDED_BY(lock_);
 };
 
@@ -259,7 +259,7 @@
   AllocationHeader* GetAllocationHeader(const mirror::Object* obj);
 
   typedef std::set<AllocationHeader*, AllocationHeader::SortByPrevFree,
-                   accounting::GcAllocator<AllocationHeader*>> FreeBlocks;
+                   TrackingAllocator<AllocationHeader*, kAllocatorTagLOSFreeList>> FreeBlocks;
 
   // There is not footer for any allocations at the end of the space, so we keep track of how much
   // free space there is at the end manually.