deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / src / profile-generator.h
1 // Copyright 2011 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_PROFILE_GENERATOR_H_
6 #define V8_PROFILE_GENERATOR_H_
7
8 #include <map>
9 #include "include/v8-profiler.h"
10 #include "src/allocation.h"
11 #include "src/compiler.h"
12 #include "src/hashmap.h"
13 #include "src/strings-storage.h"
14
15 namespace v8 {
16 namespace internal {
17
18 struct OffsetRange;
19
20 // Provides a mapping from the offsets within generated code to
21 // the source line.
22 class JITLineInfoTable : public Malloced {
23  public:
24   JITLineInfoTable();
25   ~JITLineInfoTable();
26
27   void SetPosition(int pc_offset, int line);
28   int GetSourceLineNumber(int pc_offset) const;
29
30   bool empty() const { return pc_offset_map_.empty(); }
31
32  private:
33   // pc_offset -> source line
34   typedef std::map<int, int> PcOffsetMap;
35   PcOffsetMap pc_offset_map_;
36   DISALLOW_COPY_AND_ASSIGN(JITLineInfoTable);
37 };
38
39
40 struct DeoptInfo {
41   const char* deopt_reason;
42   struct Frame {
43     int script_id;
44     int position;
45   };
46   std::vector<Frame> stack;
47 };
48
49
50 class CodeEntry {
51  public:
52   // CodeEntry doesn't own name strings, just references them.
53   inline CodeEntry(Logger::LogEventsAndTags tag, const char* name,
54                    const char* name_prefix = CodeEntry::kEmptyNamePrefix,
55                    const char* resource_name = CodeEntry::kEmptyResourceName,
56                    int line_number = v8::CpuProfileNode::kNoLineNumberInfo,
57                    int column_number = v8::CpuProfileNode::kNoColumnNumberInfo,
58                    JITLineInfoTable* line_info = NULL,
59                    Address instruction_start = NULL);
60   ~CodeEntry();
61
62   bool is_js_function() const { return is_js_function_tag(tag()); }
63   const char* name_prefix() const { return name_prefix_; }
64   bool has_name_prefix() const { return name_prefix_[0] != '\0'; }
65   const char* name() const { return name_; }
66   const char* resource_name() const { return resource_name_; }
67   int line_number() const { return line_number_; }
68   int column_number() const { return column_number_; }
69   const JITLineInfoTable* line_info() const { return line_info_; }
70   int script_id() const { return script_id_; }
71   void set_script_id(int script_id) { script_id_ = script_id; }
72   int position() const { return position_; }
73   void set_position(int position) { position_ = position; }
74   void set_bailout_reason(const char* bailout_reason) {
75     bailout_reason_ = bailout_reason;
76   }
77   const char* bailout_reason() const { return bailout_reason_; }
78
79   void set_deopt_info(const char* deopt_reason, SourcePosition position,
80                       size_t pc_offset) {
81     DCHECK(deopt_position_.IsUnknown());
82     deopt_reason_ = deopt_reason;
83     deopt_position_ = position;
84     pc_offset_ = pc_offset;
85   }
86   DeoptInfo GetDeoptInfo();
87   const char* deopt_reason() const { return deopt_reason_; }
88   SourcePosition deopt_position() const { return deopt_position_; }
89   bool has_deopt_info() const { return !deopt_position_.IsUnknown(); }
90   void clear_deopt_info() {
91     deopt_reason_ = kNoDeoptReason;
92     deopt_position_ = SourcePosition::Unknown();
93   }
94
95   void FillFunctionInfo(SharedFunctionInfo* shared);
96
97   static inline bool is_js_function_tag(Logger::LogEventsAndTags tag);
98
99   List<OffsetRange>* no_frame_ranges() const { return no_frame_ranges_; }
100   void set_no_frame_ranges(List<OffsetRange>* ranges) {
101     no_frame_ranges_ = ranges;
102   }
103   void set_inlined_function_infos(
104       const std::vector<InlinedFunctionInfo>& infos) {
105     inlined_function_infos_ = infos;
106   }
107   const std::vector<InlinedFunctionInfo> inlined_function_infos() {
108     return inlined_function_infos_;
109   }
110
111   void SetBuiltinId(Builtins::Name id);
112   Builtins::Name builtin_id() const {
113     return BuiltinIdField::decode(bit_field_);
114   }
115
116   uint32_t GetHash() const;
117   bool IsSameFunctionAs(CodeEntry* entry) const;
118
119   int GetSourceLine(int pc_offset) const;
120
121   Address instruction_start() const { return instruction_start_; }
122
123   static const char* const kEmptyNamePrefix;
124   static const char* const kEmptyResourceName;
125   static const char* const kEmptyBailoutReason;
126   static const char* const kNoDeoptReason;
127
128  private:
129   class TagField : public BitField<Logger::LogEventsAndTags, 0, 8> {};
130   class BuiltinIdField : public BitField<Builtins::Name, 8, 8> {};
131   Logger::LogEventsAndTags tag() const { return TagField::decode(bit_field_); }
132
133   uint32_t bit_field_;
134   const char* name_prefix_;
135   const char* name_;
136   const char* resource_name_;
137   int line_number_;
138   int column_number_;
139   int script_id_;
140   int position_;
141   List<OffsetRange>* no_frame_ranges_;
142   const char* bailout_reason_;
143   const char* deopt_reason_;
144   SourcePosition deopt_position_;
145   size_t pc_offset_;
146   JITLineInfoTable* line_info_;
147   Address instruction_start_;
148
149   std::vector<InlinedFunctionInfo> inlined_function_infos_;
150
151   DISALLOW_COPY_AND_ASSIGN(CodeEntry);
152 };
153
154
155 class ProfileTree;
156
157 class ProfileNode {
158  public:
159   inline ProfileNode(ProfileTree* tree, CodeEntry* entry);
160
161   ProfileNode* FindChild(CodeEntry* entry);
162   ProfileNode* FindOrAddChild(CodeEntry* entry);
163   void IncrementSelfTicks() { ++self_ticks_; }
164   void IncreaseSelfTicks(unsigned amount) { self_ticks_ += amount; }
165   void IncrementLineTicks(int src_line);
166
167   CodeEntry* entry() const { return entry_; }
168   unsigned self_ticks() const { return self_ticks_; }
169   const List<ProfileNode*>* children() const { return &children_list_; }
170   unsigned id() const { return id_; }
171   unsigned function_id() const;
172   unsigned int GetHitLineCount() const { return line_ticks_.occupancy(); }
173   bool GetLineTicks(v8::CpuProfileNode::LineTick* entries,
174                     unsigned int length) const;
175   void CollectDeoptInfo(CodeEntry* entry);
176   const std::vector<DeoptInfo>& deopt_infos() const { return deopt_infos_; }
177
178   void Print(int indent);
179
180   static bool CodeEntriesMatch(void* entry1, void* entry2) {
181     return reinterpret_cast<CodeEntry*>(entry1)
182         ->IsSameFunctionAs(reinterpret_cast<CodeEntry*>(entry2));
183   }
184
185  private:
186   static uint32_t CodeEntryHash(CodeEntry* entry) { return entry->GetHash(); }
187
188   static bool LineTickMatch(void* a, void* b) { return a == b; }
189
190   ProfileTree* tree_;
191   CodeEntry* entry_;
192   unsigned self_ticks_;
193   // Mapping from CodeEntry* to ProfileNode*
194   HashMap children_;
195   List<ProfileNode*> children_list_;
196   unsigned id_;
197   HashMap line_ticks_;
198
199   std::vector<DeoptInfo> deopt_infos_;
200
201   DISALLOW_COPY_AND_ASSIGN(ProfileNode);
202 };
203
204
205 class ProfileTree {
206  public:
207   ProfileTree();
208   ~ProfileTree();
209
210   ProfileNode* AddPathFromEnd(
211       const Vector<CodeEntry*>& path,
212       int src_line = v8::CpuProfileNode::kNoLineNumberInfo);
213   ProfileNode* root() const { return root_; }
214   unsigned next_node_id() { return next_node_id_++; }
215   unsigned GetFunctionId(const ProfileNode* node);
216
217   void Print() {
218     root_->Print(0);
219   }
220
221  private:
222   template <typename Callback>
223   void TraverseDepthFirst(Callback* callback);
224
225   CodeEntry root_entry_;
226   unsigned next_node_id_;
227   ProfileNode* root_;
228
229   unsigned next_function_id_;
230   HashMap function_ids_;
231
232   DISALLOW_COPY_AND_ASSIGN(ProfileTree);
233 };
234
235
236 class CpuProfile {
237  public:
238   CpuProfile(const char* title, bool record_samples);
239
240   // Add pc -> ... -> main() call path to the profile.
241   void AddPath(base::TimeTicks timestamp, const Vector<CodeEntry*>& path,
242                int src_line);
243   void CalculateTotalTicksAndSamplingRate();
244
245   const char* title() const { return title_; }
246   const ProfileTree* top_down() const { return &top_down_; }
247
248   int samples_count() const { return samples_.length(); }
249   ProfileNode* sample(int index) const { return samples_.at(index); }
250   base::TimeTicks sample_timestamp(int index) const {
251     return timestamps_.at(index);
252   }
253
254   base::TimeTicks start_time() const { return start_time_; }
255   base::TimeTicks end_time() const { return end_time_; }
256
257   void UpdateTicksScale();
258
259   void Print();
260
261  private:
262   const char* title_;
263   bool record_samples_;
264   base::TimeTicks start_time_;
265   base::TimeTicks end_time_;
266   List<ProfileNode*> samples_;
267   List<base::TimeTicks> timestamps_;
268   ProfileTree top_down_;
269
270   DISALLOW_COPY_AND_ASSIGN(CpuProfile);
271 };
272
273
274 class CodeMap {
275  public:
276   CodeMap() {}
277   void AddCode(Address addr, CodeEntry* entry, unsigned size);
278   void MoveCode(Address from, Address to);
279   CodeEntry* FindEntry(Address addr, Address* start = NULL);
280   int GetSharedId(Address addr);
281
282   void Print();
283
284  private:
285   struct CodeEntryInfo {
286     CodeEntryInfo(CodeEntry* an_entry, unsigned a_size)
287         : entry(an_entry), size(a_size) { }
288     CodeEntry* entry;
289     unsigned size;
290   };
291
292   struct CodeTreeConfig {
293     typedef Address Key;
294     typedef CodeEntryInfo Value;
295     static const Key kNoKey;
296     static const Value NoValue() { return CodeEntryInfo(NULL, 0); }
297     static int Compare(const Key& a, const Key& b) {
298       return a < b ? -1 : (a > b ? 1 : 0);
299     }
300   };
301   typedef SplayTree<CodeTreeConfig> CodeTree;
302
303   class CodeTreePrinter {
304    public:
305     void Call(const Address& key, const CodeEntryInfo& value);
306   };
307
308   void DeleteAllCoveredCode(Address start, Address end);
309
310   CodeTree tree_;
311
312   DISALLOW_COPY_AND_ASSIGN(CodeMap);
313 };
314
315
316 class CpuProfilesCollection {
317  public:
318   explicit CpuProfilesCollection(Heap* heap);
319   ~CpuProfilesCollection();
320
321   bool StartProfiling(const char* title, bool record_samples);
322   CpuProfile* StopProfiling(const char* title);
323   List<CpuProfile*>* profiles() { return &finished_profiles_; }
324   const char* GetName(Name* name) {
325     return function_and_resource_names_.GetName(name);
326   }
327   const char* GetName(int args_count) {
328     return function_and_resource_names_.GetName(args_count);
329   }
330   const char* GetFunctionName(Name* name) {
331     return function_and_resource_names_.GetFunctionName(name);
332   }
333   const char* GetFunctionName(const char* name) {
334     return function_and_resource_names_.GetFunctionName(name);
335   }
336   bool IsLastProfile(const char* title);
337   void RemoveProfile(CpuProfile* profile);
338
339   CodeEntry* NewCodeEntry(
340       Logger::LogEventsAndTags tag, const char* name,
341       const char* name_prefix = CodeEntry::kEmptyNamePrefix,
342       const char* resource_name = CodeEntry::kEmptyResourceName,
343       int line_number = v8::CpuProfileNode::kNoLineNumberInfo,
344       int column_number = v8::CpuProfileNode::kNoColumnNumberInfo,
345       JITLineInfoTable* line_info = NULL, Address instruction_start = NULL);
346
347   // Called from profile generator thread.
348   void AddPathToCurrentProfiles(base::TimeTicks timestamp,
349                                 const Vector<CodeEntry*>& path, int src_line);
350
351   // Limits the number of profiles that can be simultaneously collected.
352   static const int kMaxSimultaneousProfiles = 100;
353
354  private:
355   StringsStorage function_and_resource_names_;
356   List<CodeEntry*> code_entries_;
357   List<CpuProfile*> finished_profiles_;
358
359   // Accessed by VM thread and profile generator thread.
360   List<CpuProfile*> current_profiles_;
361   base::Semaphore current_profiles_semaphore_;
362
363   DISALLOW_COPY_AND_ASSIGN(CpuProfilesCollection);
364 };
365
366
367 class ProfileGenerator {
368  public:
369   explicit ProfileGenerator(CpuProfilesCollection* profiles);
370
371   void RecordTickSample(const TickSample& sample);
372
373   CodeMap* code_map() { return &code_map_; }
374
375   static const char* const kProgramEntryName;
376   static const char* const kIdleEntryName;
377   static const char* const kGarbageCollectorEntryName;
378   // Used to represent frames for which we have no reliable way to
379   // detect function.
380   static const char* const kUnresolvedFunctionName;
381
382  private:
383   CodeEntry* EntryForVMState(StateTag tag);
384
385   CpuProfilesCollection* profiles_;
386   CodeMap code_map_;
387   CodeEntry* program_entry_;
388   CodeEntry* idle_entry_;
389   CodeEntry* gc_entry_;
390   CodeEntry* unresolved_entry_;
391
392   DISALLOW_COPY_AND_ASSIGN(ProfileGenerator);
393 };
394
395
396 } }  // namespace v8::internal
397
398 #endif  // V8_PROFILE_GENERATOR_H_