Home | History | Annotate | Download | only in heap
      1 // Copyright 2015 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef V8_HEAP_OBJECT_STATS_H_
      6 #define V8_HEAP_OBJECT_STATS_H_
      7 
      8 #include "src/heap/heap.h"
      9 #include "src/heap/objects-visiting.h"
     10 #include "src/objects.h"
     11 
     12 namespace v8 {
     13 namespace internal {
     14 
     15 class ObjectStats {
     16  public:
     17   explicit ObjectStats(Heap* heap) : heap_(heap) {}
     18 
     19   // ObjectStats are kept in two arrays, counts and sizes. Related stats are
     20   // stored in a contiguous linear buffer. Stats groups are stored one after
     21   // another.
     22   enum {
     23     FIRST_CODE_KIND_SUB_TYPE = LAST_TYPE + 1,
     24     FIRST_FIXED_ARRAY_SUB_TYPE =
     25         FIRST_CODE_KIND_SUB_TYPE + Code::NUMBER_OF_KINDS,
     26     FIRST_CODE_AGE_SUB_TYPE =
     27         FIRST_FIXED_ARRAY_SUB_TYPE + LAST_FIXED_ARRAY_SUB_TYPE + 1,
     28     OBJECT_STATS_COUNT = FIRST_CODE_AGE_SUB_TYPE + Code::kCodeAgeCount + 1
     29   };
     30 
     31   void ClearObjectStats(bool clear_last_time_stats = false);
     32 
     33   void TraceObjectStats();
     34   void TraceObjectStat(const char* name, int count, int size, double time);
     35   void CheckpointObjectStats();
     36 
     37   void RecordObjectStats(InstanceType type, size_t size) {
     38     DCHECK(type <= LAST_TYPE);
     39     object_counts_[type]++;
     40     object_sizes_[type] += size;
     41   }
     42 
     43   void RecordCodeSubTypeStats(int code_sub_type, int code_age, size_t size) {
     44     int code_sub_type_index = FIRST_CODE_KIND_SUB_TYPE + code_sub_type;
     45     int code_age_index =
     46         FIRST_CODE_AGE_SUB_TYPE + code_age - Code::kFirstCodeAge;
     47     DCHECK(code_sub_type_index >= FIRST_CODE_KIND_SUB_TYPE &&
     48            code_sub_type_index < FIRST_CODE_AGE_SUB_TYPE);
     49     DCHECK(code_age_index >= FIRST_CODE_AGE_SUB_TYPE &&
     50            code_age_index < OBJECT_STATS_COUNT);
     51     object_counts_[code_sub_type_index]++;
     52     object_sizes_[code_sub_type_index] += size;
     53     object_counts_[code_age_index]++;
     54     object_sizes_[code_age_index] += size;
     55   }
     56 
     57   void RecordFixedArraySubTypeStats(int array_sub_type, size_t size) {
     58     DCHECK(array_sub_type <= LAST_FIXED_ARRAY_SUB_TYPE);
     59     object_counts_[FIRST_FIXED_ARRAY_SUB_TYPE + array_sub_type]++;
     60     object_sizes_[FIRST_FIXED_ARRAY_SUB_TYPE + array_sub_type] += size;
     61   }
     62 
     63   size_t object_count_last_gc(size_t index) {
     64     return object_counts_last_time_[index];
     65   }
     66 
     67   size_t object_size_last_gc(size_t index) {
     68     return object_sizes_last_time_[index];
     69   }
     70 
     71   Isolate* isolate();
     72   Heap* heap() { return heap_; }
     73 
     74  private:
     75   Heap* heap_;
     76 
     77   // Object counts and used memory by InstanceType
     78   size_t object_counts_[OBJECT_STATS_COUNT];
     79   size_t object_counts_last_time_[OBJECT_STATS_COUNT];
     80   size_t object_sizes_[OBJECT_STATS_COUNT];
     81   size_t object_sizes_last_time_[OBJECT_STATS_COUNT];
     82 };
     83 
     84 
     85 class ObjectStatsVisitor : public StaticMarkingVisitor<ObjectStatsVisitor> {
     86  public:
     87   static void Initialize(VisitorDispatchTable<Callback>* original);
     88 
     89   static void VisitBase(VisitorId id, Map* map, HeapObject* obj);
     90 
     91   static void CountFixedArray(FixedArrayBase* fixed_array,
     92                               FixedArraySubInstanceType fast_type,
     93                               FixedArraySubInstanceType dictionary_type);
     94 
     95   template <VisitorId id>
     96   static inline void Visit(Map* map, HeapObject* obj);
     97 };
     98 
     99 }  // namespace internal
    100 }  // namespace v8
    101 
    102 #endif  // V8_HEAP_OBJECT_STATS_H_
    103