forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSCLegacyProfiler.mm
311 lines (257 loc) · 7.95 KB
/
JSCLegacyProfiler.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "JSCLegacyProfiler.h"
#include "APICast.h"
#include "LegacyProfiler.h"
#include "OpaqueJSString.h"
#include "JSProfilerPrivate.h"
#include "JSStringRef.h"
#include "String.h"
#include "Options.h"
enum json_gen_status {
json_gen_status_ok = 0,
json_gen_status_error = 1,
};
enum json_entry {
json_entry_key,
json_entry_value,
};
namespace {
struct json_state {
FILE *fileOut;
bool hasFirst;
};
}
typedef json_state *json_gen;
static void json_escaped_cstring_printf(json_gen gen, const char *str) {
const char *cursor = str;
fputc('"', gen->fileOut);
while (*cursor) {
const char *escape = nullptr;
switch (*cursor) {
case '"':
escape = "\\\"";
break;
case '\b':
escape = "\\b";
break;
case '\f':
escape = "\\f";
break;
case '\n':
escape = "\\n";
break;
case '\r':
escape = "\\r";
break;
case '\t':
escape = "\\t";
break;
case '\\':
escape = "\\\\";
break;
default:
break;
}
if (escape != nullptr) {
fwrite(escape, 1, strlen(escape), gen->fileOut);
} else {
fputc(*cursor, gen->fileOut);
}
cursor++;
}
fputc('"', gen->fileOut);
}
static json_gen_status json_gen_key_cstring(json_gen gen, const char *buffer) {
if (gen->fileOut == nullptr) {
return json_gen_status_error;
}
if (gen->hasFirst) {
fprintf(gen->fileOut, ",");
}
gen->hasFirst = true;
json_escaped_cstring_printf(gen, buffer);
return json_gen_status_ok;
}
static json_gen_status json_gen_map_open(json_gen gen, json_entry entryType) {
if (gen->fileOut == nullptr) {
return json_gen_status_error;
}
if (entryType == json_entry_value) {
fprintf(gen->fileOut, ":");
} else if (entryType == json_entry_key) {
if (gen->hasFirst) {
fprintf(gen->fileOut, ",");
}
}
fprintf(gen->fileOut, "{");
gen->hasFirst = false;
return json_gen_status_ok;
}
static json_gen_status json_gen_map_close(json_gen gen) {
if (gen->fileOut == nullptr) {
return json_gen_status_error;
}
fprintf(gen->fileOut, "}");
gen->hasFirst = true;
return json_gen_status_ok;
}
static json_gen_status json_gen_array_open(json_gen gen, json_entry entryType) {
if (gen->fileOut == nullptr) {
return json_gen_status_error;
}
if (entryType == json_entry_value) {
fprintf(gen->fileOut, ":");
} else if (entryType == json_entry_key) {
if (gen->hasFirst) {
fprintf(gen->fileOut, ",");
}
}
fprintf(gen->fileOut, "[");
gen->hasFirst = false;
return json_gen_status_ok;
}
static json_gen_status json_gen_array_close(json_gen gen) {
if (gen->fileOut == nullptr) {
return json_gen_status_error;
}
fprintf(gen->fileOut, "]");
gen->hasFirst = true;
return json_gen_status_ok;
}
static json_gen_status json_gen_keyvalue_cstring(json_gen gen, const char *key, const char *value) {
if (gen->fileOut == nullptr) {
return json_gen_status_error;
}
if (gen->hasFirst) {
fprintf(gen->fileOut, ",");
}
gen->hasFirst = true;
fprintf(gen->fileOut, "\"%s\" : ", key);
json_escaped_cstring_printf(gen, value);
return json_gen_status_ok;
}
static json_gen_status json_gen_keyvalue_integer(json_gen gen, const char *key, int value) {
if (gen->fileOut == nullptr) {
return json_gen_status_error;
}
if (gen->hasFirst) {
fprintf(gen->fileOut, ",");
}
gen->hasFirst = true;
fprintf(gen->fileOut, "\"%s\": %d", key, value);
return json_gen_status_ok;
}
static json_gen_status json_gen_keyvalue_double(json_gen gen, const char *key, double value) {
if (gen->fileOut == nullptr) {
return json_gen_status_error;
}
if (gen->hasFirst) {
fprintf(gen->fileOut, ",");
}
gen->hasFirst = true;
fprintf(gen->fileOut, "\"%s\": %.20g", key, value);
return json_gen_status_ok;
}
static json_gen json_gen_alloc(const char *fileName) {
json_gen gen = (json_gen)malloc(sizeof(json_state));
memset(gen, 0, sizeof(json_state));
gen->fileOut = fopen(fileName, "wb");
return gen;
}
static void json_gen_free(json_gen gen) {
if (gen->fileOut) {
fclose(gen->fileOut);
}
free(gen);
}
#define GEN_AND_CHECK(expr) \
do { \
json_gen_status GEN_AND_CHECK_status = (expr); \
if (GEN_AND_CHECK_status != json_gen_status_ok) { \
return GEN_AND_CHECK_status; \
} \
} while (false)
static json_gen_status append_children_array_json(json_gen gen, const JSC::ProfileNode *node);
static json_gen_status append_node_json(json_gen gen, const JSC::ProfileNode *node);
static json_gen_status append_root_json(json_gen gen, const JSC::Profile *profile) {
GEN_AND_CHECK(json_gen_map_open(gen, json_entry_key));
GEN_AND_CHECK(json_gen_key_cstring(gen, "rootNodes"));
#if IOS8
GEN_AND_CHECK(append_children_array_json(gen, profile->head()));
#else
GEN_AND_CHECK(append_children_array_json(gen, profile->rootNode()));
#endif
GEN_AND_CHECK(json_gen_map_close(gen));
return json_gen_status_ok;
}
static json_gen_status append_children_array_json(json_gen gen, const JSC::ProfileNode *node) {
GEN_AND_CHECK(json_gen_array_open(gen, json_entry_value));
for (RefPtr<JSC::ProfileNode> child : node->children()) {
GEN_AND_CHECK(append_node_json(gen, child.get()));
}
GEN_AND_CHECK(json_gen_array_close(gen));
return json_gen_status_ok;
}
static json_gen_status append_node_json(json_gen gen, const JSC::ProfileNode *node) {
GEN_AND_CHECK(json_gen_map_open(gen, json_entry_key));
GEN_AND_CHECK(json_gen_keyvalue_integer(gen, "id", node->id()));
if (!node->functionName().isEmpty()) {
GEN_AND_CHECK(json_gen_keyvalue_cstring(gen, "functionName", node->functionName().utf8().data()));
}
if (!node->url().isEmpty()) {
GEN_AND_CHECK(json_gen_keyvalue_cstring(gen, "url", node->url().utf8().data()));
GEN_AND_CHECK(json_gen_keyvalue_integer(gen, "lineNumber", node->lineNumber()));
GEN_AND_CHECK(json_gen_keyvalue_integer(gen, "columnNumber", node->columnNumber()));
}
GEN_AND_CHECK(json_gen_key_cstring(gen, "calls"));
GEN_AND_CHECK(json_gen_array_open(gen, json_entry_value));
for (const JSC::ProfileNode::Call &call : node->calls()) {
GEN_AND_CHECK(json_gen_map_open(gen, json_entry_key));
GEN_AND_CHECK(json_gen_keyvalue_double(gen, "startTime", call.startTime()));
#if IOS8
GEN_AND_CHECK(json_gen_keyvalue_double(gen, "totalTime", call.totalTime()));
#else
GEN_AND_CHECK(json_gen_keyvalue_double(gen, "totalTime", call.elapsedTime()));
#endif
GEN_AND_CHECK(json_gen_map_close(gen));
}
GEN_AND_CHECK(json_gen_array_close(gen));
if (!node->children().isEmpty()) {
GEN_AND_CHECK(json_gen_key_cstring(gen, "children"));
GEN_AND_CHECK(append_children_array_json(gen, node));
}
GEN_AND_CHECK(json_gen_map_close(gen));
return json_gen_status_ok;
}
static void convert_to_json(const JSC::Profile *profile, const char *filename) {
json_gen_status status;
json_gen gen = json_gen_alloc(filename);
status = append_root_json(gen, profile);
if (status != json_gen_status_ok) {
FILE *fileOut = fopen(filename, "wb");
if (fileOut != nullptr) {
fprintf(fileOut, "{\"error\": %d}", (int)status);
fclose(fileOut);
}
}
json_gen_free(gen);
}
// Based on JSEndProfiling, with a little extra code to return the profile as JSON.
static void JSEndProfilingAndRender(JSContextRef ctx, const char *title, const char *filename)
{
JSC::ExecState *exec = toJS(ctx);
JSC::LegacyProfiler *profiler = JSC::LegacyProfiler::profiler();
RefPtr<JSC::Profile> rawProfile = profiler->stopProfiling(exec, WTF::String(title));
convert_to_json(rawProfile.get(), filename);
}
extern "C" {
void nativeProfilerEnableBytecode(void)
{
JSC::Options::setOption("forceProfilerBytecodeGeneration=true");
}
void nativeProfilerStart(JSContextRef ctx, const char *title) {
JSStartProfiling(ctx, JSStringCreateWithUTF8CString(title));
}
void nativeProfilerEnd(JSContextRef ctx, const char *title, const char *filename) {
JSEndProfilingAndRender(ctx, title, filename);
}
}