Home | History | Annotate | Download | only in src
      1 // Copyright 2013 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 #ifndef V8_ALLOCATION_TRACKER_H_
     29 #define V8_ALLOCATION_TRACKER_H_
     30 
     31 namespace v8 {
     32 namespace internal {
     33 
     34 class HeapObjectsMap;
     35 
     36 class AllocationTraceTree;
     37 
     38 class AllocationTraceNode {
     39  public:
     40   AllocationTraceNode(AllocationTraceTree* tree,
     41                       SnapshotObjectId shared_function_info_id);
     42   ~AllocationTraceNode();
     43   AllocationTraceNode* FindChild(SnapshotObjectId shared_function_info_id);
     44   AllocationTraceNode* FindOrAddChild(SnapshotObjectId shared_function_info_id);
     45   void AddAllocation(unsigned size);
     46 
     47   SnapshotObjectId function_id() const { return function_id_; }
     48   unsigned allocation_size() const { return total_size_; }
     49   unsigned allocation_count() const { return allocation_count_; }
     50   unsigned id() const { return id_; }
     51   Vector<AllocationTraceNode*> children() const { return children_.ToVector(); }
     52 
     53   void Print(int indent, AllocationTracker* tracker);
     54 
     55  private:
     56   AllocationTraceTree* tree_;
     57   SnapshotObjectId function_id_;
     58   unsigned total_size_;
     59   unsigned allocation_count_;
     60   unsigned id_;
     61   List<AllocationTraceNode*> children_;
     62 
     63   DISALLOW_COPY_AND_ASSIGN(AllocationTraceNode);
     64 };
     65 
     66 
     67 class AllocationTraceTree {
     68  public:
     69   AllocationTraceTree();
     70   ~AllocationTraceTree();
     71   AllocationTraceNode* AddPathFromEnd(const Vector<SnapshotObjectId>& path);
     72   AllocationTraceNode* root() { return &root_; }
     73   unsigned next_node_id() { return next_node_id_++; }
     74   void Print(AllocationTracker* tracker);
     75 
     76  private:
     77   unsigned next_node_id_;
     78   AllocationTraceNode root_;
     79 
     80   DISALLOW_COPY_AND_ASSIGN(AllocationTraceTree);
     81 };
     82 
     83 
     84 class AllocationTracker {
     85  public:
     86   struct FunctionInfo {
     87     FunctionInfo();
     88     const char* name;
     89     const char* script_name;
     90     int script_id;
     91     int line;
     92     int column;
     93   };
     94 
     95   AllocationTracker(HeapObjectsMap* ids, StringsStorage* names);
     96   ~AllocationTracker();
     97 
     98   void PrepareForSerialization();
     99   void AllocationEvent(Address addr, int size);
    100 
    101   AllocationTraceTree* trace_tree() { return &trace_tree_; }
    102   HashMap* id_to_function_info() { return &id_to_function_info_; }
    103   FunctionInfo* GetFunctionInfo(SnapshotObjectId id);
    104 
    105  private:
    106   void AddFunctionInfo(SharedFunctionInfo* info, SnapshotObjectId id);
    107 
    108   class UnresolvedLocation {
    109    public:
    110     UnresolvedLocation(Script* script, int start, FunctionInfo* info);
    111     ~UnresolvedLocation();
    112     void Resolve();
    113 
    114    private:
    115     static void HandleWeakScript(v8::Isolate* isolate,
    116                                  v8::Persistent<v8::Value>* obj,
    117                                  void* data);
    118     Handle<Script> script_;
    119     int start_position_;
    120     FunctionInfo* info_;
    121   };
    122   static void DeleteUnresolvedLocation(UnresolvedLocation** location);
    123 
    124   static const int kMaxAllocationTraceLength = 64;
    125   HeapObjectsMap* ids_;
    126   StringsStorage* names_;
    127   AllocationTraceTree trace_tree_;
    128   SnapshotObjectId allocation_trace_buffer_[kMaxAllocationTraceLength];
    129   HashMap id_to_function_info_;
    130   List<UnresolvedLocation*> unresolved_locations_;
    131 
    132   DISALLOW_COPY_AND_ASSIGN(AllocationTracker);
    133 };
    134 
    135 } }  // namespace v8::internal
    136 
    137 #endif  // V8_ALLOCATION_TRACKER_H_
    138