Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / v8 / src / xdk-utils.h
1 // Copyright 2014 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 __xdk_utils_h__
6 #define __xdk_utils_h__
7
8 #include <map>
9 #include <set>
10 #include <sstream>
11 #include <string>
12 #include "src/hashmap.h"
13
14 namespace v8 {
15 namespace internal {
16
17 class AggregatedChunks;
18 class StringsStorage;
19 class JavaScriptFrame;
20 class RuntimeInfo;
21
22 // --- ClassNames
23 class ClassNames {
24  public:
25   explicit ClassNames(StringsStorage* names, Heap* heap);
26
27   unsigned registerName(const char* className);
28   std::string SerializeChunk();
29   bool IsEssentialObject(Object* object);
30   void registerNameForDependent(HeapObject* object,
31                                 RuntimeInfo* runtime_info,
32                                 unsigned id);
33   unsigned GetConstructorName(Address address, RuntimeInfo* runtime_info);
34
35
36  private:
37   unsigned counter_;
38   HashMap char_to_idx_;
39   StringsStorage* names_;
40   Heap* heap_;
41
42   unsigned id_native_bind_;
43   unsigned id_conc_string_;
44   unsigned id_sliced_string_;
45   unsigned id_string_;
46   unsigned id_symbol_;
47   unsigned id_code_;
48   unsigned id_system_ncontext_;
49   unsigned id_system_context_;
50   unsigned id_array_;
51   unsigned id_number_;
52   unsigned id_system_;
53   unsigned id_shared_fi_;
54   unsigned id_script_;
55   unsigned id_regexp_;
56   unsigned id_function_bindings_;
57   unsigned id_function_literals_;
58   unsigned id_objects_properties_;
59   unsigned id_objects_elements_;
60   unsigned id_shared_function_info_;
61   unsigned id_context_;
62   unsigned id_code_relocation_info_;
63   unsigned id_code_deopt_data_;
64 };
65
66
67 // --- ShadowStack
68 class CallTree {
69  public:
70   // For quick search we use below member. it is not reasnable to use here
71   // map because it occupies a lot of space even in empty state and such nodes
72   // will be many. In opposite to map, std::map uses binary tree search and
73   // don't store buffer, but allocates it dinamically
74   std::map<unsigned, CallTree*> children_;
75
76   // This is _not_ the same as index in the children_. This index is
77   // incremental value from list of all nodes, but the key in the children_ is
78   // callsite
79   unsigned index_;
80   CallTree* parent_;
81   // the only one field which characterize the call point
82   unsigned callsite_;
83 };
84
85
86 class ShadowStack {
87   CallTree root_;
88
89   // unsigned here is ok, size_t is not required because even 10 millions
90   // objects in this class will lead to the significant memory consumption
91   unsigned last_index_;
92
93   // TODO(amalyshe): rewrite using List, storing nodes and use index in the list
94   // instead pointer to CallTree in the children_
95   std::map<unsigned, CallTree*> allNodes_;
96   unsigned serializedCounter_;
97  public:
98   ShadowStack();
99   ~ShadowStack();
100   // Returns unique stack id. This method can work with incremental stacks when
101   // we have old stack id, new tail and number of functions that we need to
102   // unroll.
103   unsigned registerStack(const List<unsigned>& shadow_stack_);
104   std::string SerializeChunk();
105 };
106
107
108 // --- SymbolsStorage
109 struct SymInfoKey {
110   size_t function_id_;
111   unsigned line_;
112   unsigned column_;
113 };
114
115 bool inline operator == (const SymInfoKey& key1, const SymInfoKey& key2) {
116   return key1.function_id_ == key2.function_id_ &&
117     key1.line_ == key2.line_ &&
118     key1.column_ == key2.column_;
119 }
120
121
122 struct SymInfoValue {
123   unsigned symId_;
124   std::string funcName_;
125   std::string sourceFile_;
126 };
127
128
129 class SymbolsStorage {
130  public:
131   unsigned registerSymInfo(size_t functionId,
132                                std::string functionName,
133                                std::string sourceName, unsigned line,
134                                unsigned column);
135   unsigned FindOrRegisterFrame(JavaScriptFrame* frame);
136   SymbolsStorage(Heap* heap, StringsStorage* names);
137   ~SymbolsStorage();
138   std::string SerializeChunk();
139
140  private:
141   HashMap symbols_;
142   unsigned curSym_;
143   // fast living storage which duplicate info but is cleaned regularly
144   SymInfoKey* reserved_key_;
145   HashMap sym_info_hash_;
146   Heap* heap_;
147   StringsStorage* names_;
148 };
149
150
151 struct PostCollectedInfo {
152   int size_;
153   int timeStamp_;
154   int stackId_;
155   unsigned className_;
156   bool dirty_;
157 };
158
159
160 class RuntimeInfo {
161  public:
162   explicit RuntimeInfo(AggregatedChunks* aggregated_chunks);
163   PostCollectedInfo* FindPostCollectedInfo(Address addr);
164   PostCollectedInfo* AddPostCollectedInfo(Address addr,
165                                           unsigned time_delta = 0,
166                                           PostCollectedInfo* info = NULL);
167   PostCollectedInfo* AddPreCollectionInfo(Address addr, unsigned size);
168   void RemoveInfo(Address addr);
169   void InitABCFrame(unsigned abc_frame);
170   void CollectGarbaged(unsigned ts);
171
172  private:
173   HashMap working_set_hash_;
174   AggregatedChunks* aggregated_chunks_;
175   unsigned AllocatedBeforeCollectionFrame_;
176 };
177
178
179 struct AggregatedKey {
180   int stackId_;
181   // do we need class here? is not it defined by the stack id?
182   unsigned classId_;
183   unsigned tsBegin_;
184   unsigned tsEnd_;
185 };
186
187 bool inline operator == (const AggregatedKey& key1, const AggregatedKey& key2) {
188   return key1.stackId_ == key2.stackId_ &&
189     key1.classId_ == key2.classId_ &&
190     key1.tsBegin_ == key2.tsBegin_ &&
191     key1.tsEnd_ == key2.tsEnd_;
192 }
193
194
195 struct AggregatedValue {
196   unsigned size_;
197   unsigned objects_;
198 };
199
200
201 class AggregatedChunks {
202  public:
203   AggregatedChunks();
204   ~AggregatedChunks();
205   void addObjectToAggregated(PostCollectedInfo* info, unsigned td);
206   std::string SerializeChunk();
207
208  private:
209   HashMap aggregated_map_;
210   int bucketSize_;
211   AggregatedKey* reserved_key_;
212 };
213
214
215 struct RefId {
216   int stackId_;
217   int classId_;
218   std::string field_;
219 };
220
221 inline bool operator < (const RefId& first, const RefId& second ) {
222   if (first.stackId_ < second.stackId_ )
223     return true;
224   else if (first.stackId_ > second.stackId_ )
225     return false;
226   if (first.classId_ < second.classId_ )
227     return true;
228   if (first.classId_ > second.classId_ )
229     return false;
230   if (first.field_.compare(second.field_) < 0 )
231     return true;
232
233   return false;
234 }
235
236 typedef std::set<RefId> REFERENCESET;
237
238
239 struct RefSet {
240   REFERENCESET references_;
241 };
242
243 inline bool operator < (const RefSet& first, const RefSet& second) {
244   // compare the sizes first of all
245   if (first.references_.size() != second.references_.size() )
246     return first.references_.size() < second.references_.size();
247   // iterating by the first
248   REFERENCESET::const_iterator cit1 = first.references_.begin();
249   REFERENCESET::const_iterator cit2 = second.references_.begin();
250   while (cit1 != first.references_.end()) {
251     if (*cit1 < *cit2 )
252       return true;
253     if (*cit2 < *cit1 )
254       return false;
255     cit1++;
256     cit2++;
257   }
258   return false;
259 }
260 typedef std::map<unsigned int, int> TIMETOCOUNT;
261 typedef std::map<RefSet, TIMETOCOUNT> REFERENCESETS;
262 typedef std::map<RefId, REFERENCESETS> PARENTREFMAP;
263
264
265 class References {
266  public:
267   void addReference(const RefId& parent,
268                     const RefSet& refSet,
269                     int parentTime);
270   void clear();
271   std::string serialize() const;
272
273  private:
274   PARENTREFMAP refMap_;
275 };
276
277
278 } }  // namespace v8::internal
279 #endif  // __xdk_utils_h__