2015-07-20 16:14:53 +00:00
|
|
|
#include "JSCLegacyProfiler.h"
|
|
|
|
|
|
|
|
#include "APICast.h"
|
|
|
|
#include "LegacyProfiler.h"
|
|
|
|
#include "OpaqueJSString.h"
|
|
|
|
#include "JSProfilerPrivate.h"
|
|
|
|
#include "JSStringRef.h"
|
2015-08-28 17:11:02 +00:00
|
|
|
#include "String.h"
|
2015-09-10 16:03:03 +00:00
|
|
|
#include "Options.h"
|
2015-07-20 16:14:53 +00:00
|
|
|
|
2015-09-10 16:03:03 +00:00
|
|
|
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);
|
|
|
|
}
|
2015-07-20 16:14:53 +00:00
|
|
|
|
|
|
|
#define GEN_AND_CHECK(expr) \
|
|
|
|
do { \
|
2015-09-10 16:03:03 +00:00
|
|
|
json_gen_status GEN_AND_CHECK_status = (expr); \
|
|
|
|
if (GEN_AND_CHECK_status != json_gen_status_ok) { \
|
2015-07-20 16:14:53 +00:00
|
|
|
return GEN_AND_CHECK_status; \
|
|
|
|
} \
|
|
|
|
} while (false)
|
|
|
|
|
|
|
|
|
2015-09-10 16:03:03 +00:00
|
|
|
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);
|
2015-07-20 16:14:53 +00:00
|
|
|
|
2015-09-10 16:03:03 +00:00
|
|
|
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"));
|
2015-11-16 11:34:39 +00:00
|
|
|
#if IOS8
|
2015-07-20 16:14:53 +00:00
|
|
|
GEN_AND_CHECK(append_children_array_json(gen, profile->head()));
|
2015-11-16 11:34:39 +00:00
|
|
|
#else
|
|
|
|
GEN_AND_CHECK(append_children_array_json(gen, profile->rootNode()));
|
|
|
|
#endif
|
2015-09-10 16:03:03 +00:00
|
|
|
GEN_AND_CHECK(json_gen_map_close(gen));
|
2015-07-20 16:14:53 +00:00
|
|
|
|
2015-09-10 16:03:03 +00:00
|
|
|
return json_gen_status_ok;
|
2015-07-20 16:14:53 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 16:03:03 +00:00
|
|
|
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));
|
2015-07-20 16:14:53 +00:00
|
|
|
for (RefPtr<JSC::ProfileNode> child : node->children()) {
|
|
|
|
GEN_AND_CHECK(append_node_json(gen, child.get()));
|
|
|
|
}
|
2015-09-10 16:03:03 +00:00
|
|
|
GEN_AND_CHECK(json_gen_array_close(gen));
|
2015-07-20 16:14:53 +00:00
|
|
|
|
2015-09-10 16:03:03 +00:00
|
|
|
return json_gen_status_ok;
|
2015-07-20 16:14:53 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 16:03:03 +00:00
|
|
|
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()));
|
2015-07-20 16:14:53 +00:00
|
|
|
|
|
|
|
if (!node->functionName().isEmpty()) {
|
2015-09-10 16:03:03 +00:00
|
|
|
GEN_AND_CHECK(json_gen_keyvalue_cstring(gen, "functionName", node->functionName().utf8().data()));
|
2015-07-20 16:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!node->url().isEmpty()) {
|
2015-09-10 16:03:03 +00:00
|
|
|
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()));
|
2015-07-20 16:14:53 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 16:03:03 +00:00
|
|
|
GEN_AND_CHECK(json_gen_key_cstring(gen, "calls"));
|
|
|
|
GEN_AND_CHECK(json_gen_array_open(gen, json_entry_value));
|
2015-07-20 16:14:53 +00:00
|
|
|
for (const JSC::ProfileNode::Call &call : node->calls()) {
|
2015-09-10 16:03:03 +00:00
|
|
|
GEN_AND_CHECK(json_gen_map_open(gen, json_entry_key));
|
|
|
|
GEN_AND_CHECK(json_gen_keyvalue_double(gen, "startTime", call.startTime()));
|
2015-11-16 11:34:39 +00:00
|
|
|
#if IOS8
|
2015-09-10 16:03:03 +00:00
|
|
|
GEN_AND_CHECK(json_gen_keyvalue_double(gen, "totalTime", call.totalTime()));
|
2015-11-16 11:34:39 +00:00
|
|
|
#else
|
|
|
|
GEN_AND_CHECK(json_gen_keyvalue_double(gen, "totalTime", call.elapsedTime()));
|
|
|
|
#endif
|
2015-09-10 16:03:03 +00:00
|
|
|
GEN_AND_CHECK(json_gen_map_close(gen));
|
2015-07-20 16:14:53 +00:00
|
|
|
}
|
2015-09-10 16:03:03 +00:00
|
|
|
GEN_AND_CHECK(json_gen_array_close(gen));
|
2015-07-20 16:14:53 +00:00
|
|
|
|
|
|
|
if (!node->children().isEmpty()) {
|
2015-09-10 16:03:03 +00:00
|
|
|
GEN_AND_CHECK(json_gen_key_cstring(gen, "children"));
|
2015-07-20 16:14:53 +00:00
|
|
|
GEN_AND_CHECK(append_children_array_json(gen, node));
|
|
|
|
}
|
|
|
|
|
2015-09-10 16:03:03 +00:00
|
|
|
GEN_AND_CHECK(json_gen_map_close(gen));
|
2015-07-20 16:14:53 +00:00
|
|
|
|
2015-09-10 16:03:03 +00:00
|
|
|
return json_gen_status_ok;
|
2015-07-20 16:14:53 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 16:03:03 +00:00
|
|
|
static void convert_to_json(const JSC::Profile *profile, const char *filename) {
|
|
|
|
json_gen_status status;
|
|
|
|
json_gen gen = json_gen_alloc(filename);
|
2015-07-20 16:14:53 +00:00
|
|
|
|
|
|
|
status = append_root_json(gen, profile);
|
2015-09-10 16:03:03 +00:00
|
|
|
if (status != json_gen_status_ok) {
|
|
|
|
FILE *fileOut = fopen(filename, "wb");
|
|
|
|
if (fileOut != nullptr) {
|
|
|
|
fprintf(fileOut, "{\"error\": %d}", (int)status);
|
|
|
|
fclose(fileOut);
|
|
|
|
}
|
2015-07-20 16:14:53 +00:00
|
|
|
}
|
2015-09-10 16:03:03 +00:00
|
|
|
json_gen_free(gen);
|
2015-07-20 16:14:53 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 16:03:03 +00:00
|
|
|
// 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)
|
2015-07-20 16:14:53 +00:00
|
|
|
{
|
|
|
|
JSC::ExecState *exec = toJS(ctx);
|
|
|
|
JSC::LegacyProfiler *profiler = JSC::LegacyProfiler::profiler();
|
2015-08-28 17:11:02 +00:00
|
|
|
RefPtr<JSC::Profile> rawProfile = profiler->stopProfiling(exec, WTF::String(title));
|
2015-09-10 16:03:03 +00:00
|
|
|
convert_to_json(rawProfile.get(), filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
void nativeProfilerEnableBytecode(void)
|
|
|
|
{
|
|
|
|
JSC::Options::setOption("forceProfilerBytecodeGeneration=true");
|
2015-07-20 16:14:53 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 17:11:02 +00:00
|
|
|
void nativeProfilerStart(JSContextRef ctx, const char *title) {
|
|
|
|
JSStartProfiling(ctx, JSStringCreateWithUTF8CString(title));
|
2015-07-20 16:14:53 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 16:03:03 +00:00
|
|
|
void nativeProfilerEnd(JSContextRef ctx, const char *title, const char *filename) {
|
|
|
|
JSEndProfilingAndRender(ctx, title, filename);
|
|
|
|
}
|
|
|
|
|
2015-07-20 16:14:53 +00:00
|
|
|
}
|