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