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