Commit cc340be9 authored by mstarzinger's avatar mstarzinger Committed by Commit bot

[turbofan] Simplify handling of JSON tracing files.

R=bmeurer@chromium.org

Review-Url: https://codereview.chromium.org/1977843002
Cr-Commit-Position: refs/heads/master@{#36242}
parent 0a60924e
...@@ -25,9 +25,8 @@ namespace v8 { ...@@ -25,9 +25,8 @@ namespace v8 {
namespace internal { namespace internal {
namespace compiler { namespace compiler {
base::SmartArrayPointer<const char> GetVisualizerLogFileName(
FILE* OpenVisualizerLogFile(CompilationInfo* info, const char* phase, CompilationInfo* info, const char* phase, const char* suffix) {
const char* suffix, const char* mode) {
EmbeddedVector<char, 256> filename(0); EmbeddedVector<char, 256> filename(0);
base::SmartArrayPointer<char> debug_name = info->GetDebugName(); base::SmartArrayPointer<char> debug_name = info->GetDebugName();
if (strlen(debug_name.get()) > 0) { if (strlen(debug_name.get()) > 0) {
...@@ -46,7 +45,11 @@ FILE* OpenVisualizerLogFile(CompilationInfo* info, const char* phase, ...@@ -46,7 +45,11 @@ FILE* OpenVisualizerLogFile(CompilationInfo* info, const char* phase,
} else { } else {
SNPrintF(full_filename, "%s-%s.%s", filename.start(), phase, suffix); SNPrintF(full_filename, "%s-%s.%s", filename.start(), phase, suffix);
} }
return base::OS::FOpen(full_filename.start(), mode);
char* buffer = new char[full_filename.length() + 1];
memcpy(buffer, full_filename.start(), full_filename.length());
buffer[full_filename.length()] = '\0';
return base::SmartArrayPointer<const char>(buffer);
} }
......
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
#include <stdio.h> #include <stdio.h>
#include <iosfwd> #include <iosfwd>
#include "src/base/smart-pointers.h"
namespace v8 { namespace v8 {
namespace internal { namespace internal {
...@@ -21,8 +23,8 @@ class RegisterAllocationData; ...@@ -21,8 +23,8 @@ class RegisterAllocationData;
class Schedule; class Schedule;
class SourcePositionTable; class SourcePositionTable;
FILE* OpenVisualizerLogFile(CompilationInfo* info, const char* phase, base::SmartArrayPointer<const char> GetVisualizerLogFileName(
const char* suffix, const char* mode); CompilationInfo* info, const char* phase, const char* suffix);
struct AsJSON { struct AsJSON {
AsJSON(const Graph& g, SourcePositionTable* p) : graph(g), positions(p) {} AsJSON(const Graph& g, SourcePositionTable* p) : graph(g), positions(p) {}
......
...@@ -400,23 +400,24 @@ struct TurboCfgFile : public std::ofstream { ...@@ -400,23 +400,24 @@ struct TurboCfgFile : public std::ofstream {
std::ios_base::app) {} std::ios_base::app) {}
}; };
struct TurboJsonFile : public std::ofstream {
TurboJsonFile(CompilationInfo* info, std::ios_base::openmode mode)
: std::ofstream(GetVisualizerLogFileName(info, nullptr, "json").get(),
mode) {}
};
void TraceSchedule(CompilationInfo* info, Schedule* schedule) { void TraceSchedule(CompilationInfo* info, Schedule* schedule) {
if (FLAG_trace_turbo) { if (FLAG_trace_turbo) {
AllowHandleDereference allow_deref; AllowHandleDereference allow_deref;
FILE* json_file = OpenVisualizerLogFile(info, nullptr, "json", "a+"); TurboJsonFile json_of(info, std::ios_base::app);
if (json_file != nullptr) { json_of << "{\"name\":\"Schedule\",\"type\":\"schedule\",\"data\":\"";
OFStream json_of(json_file); std::stringstream schedule_stream;
json_of << "{\"name\":\"Schedule\",\"type\":\"schedule\",\"data\":\""; schedule_stream << *schedule;
std::stringstream schedule_stream; std::string schedule_string(schedule_stream.str());
schedule_stream << *schedule; for (const auto& c : schedule_string) {
std::string schedule_string(schedule_stream.str()); json_of << AsEscapedUC16ForJSON(c);
for (const auto& c : schedule_string) {
json_of << AsEscapedUC16ForJSON(c);
}
json_of << "\"},\n";
fclose(json_file);
} }
json_of << "\"},\n";
} }
if (FLAG_trace_turbo_graph || FLAG_trace_turbo_scheduler) { if (FLAG_trace_turbo_graph || FLAG_trace_turbo_scheduler) {
AllowHandleDereference allow_deref; AllowHandleDereference allow_deref;
...@@ -526,27 +527,22 @@ PipelineStatistics* CreatePipelineStatistics(CompilationInfo* info, ...@@ -526,27 +527,22 @@ PipelineStatistics* CreatePipelineStatistics(CompilationInfo* info,
} }
if (FLAG_trace_turbo) { if (FLAG_trace_turbo) {
FILE* json_file = OpenVisualizerLogFile(info, nullptr, "json", "w+"); TurboJsonFile json_of(info, std::ios_base::trunc);
if (json_file != nullptr) { Handle<Script> script = info->script();
OFStream json_of(json_file); base::SmartArrayPointer<char> function_name = info->GetDebugName();
Handle<Script> script = info->script(); int pos = info->shared_info()->start_position();
base::SmartArrayPointer<char> function_name = info->GetDebugName(); json_of << "{\"function\":\"" << function_name.get()
int pos = info->shared_info()->start_position(); << "\", \"sourcePosition\":" << pos << ", \"source\":\"";
json_of << "{\"function\":\"" << function_name.get() if (!script->IsUndefined() && !script->source()->IsUndefined()) {
<< "\", \"sourcePosition\":" << pos << ", \"source\":\""; DisallowHeapAllocation no_allocation;
if (!script->IsUndefined() && !script->source()->IsUndefined()) { int start = info->shared_info()->start_position();
DisallowHeapAllocation no_allocation; int len = info->shared_info()->end_position() - start;
int start = info->shared_info()->start_position(); String::SubStringRange source(String::cast(script->source()), start, len);
int len = info->shared_info()->end_position() - start; for (const auto& c : source) {
String::SubStringRange source(String::cast(script->source()), start, json_of << AsEscapedUC16ForJSON(c);
len);
for (const auto& c : source) {
json_of << AsEscapedUC16ForJSON(c);
}
} }
json_of << "\",\n\"phases\":[";
fclose(json_file);
} }
json_of << "\",\n\"phases\":[";
} }
return pipeline_statistics; return pipeline_statistics;
...@@ -667,13 +663,9 @@ PipelineWasmCompilationJob::CreateGraphImpl() { ...@@ -667,13 +663,9 @@ PipelineWasmCompilationJob::CreateGraphImpl() {
PipelineWasmCompilationJob::Status PipelineWasmCompilationJob::Status
PipelineWasmCompilationJob::OptimizeGraphImpl() { PipelineWasmCompilationJob::OptimizeGraphImpl() {
if (FLAG_trace_turbo) { if (FLAG_trace_turbo) {
FILE* json_file = OpenVisualizerLogFile(info(), nullptr, "json", "w+"); TurboJsonFile json_of(info(), std::ios_base::trunc);
if (json_file != nullptr) { json_of << "{\"function\":\"" << info()->GetDebugName().get()
OFStream json_of(json_file); << "\", \"source\":\"\",\n\"phases\":[";
json_of << "{\"function\":\"" << info()->GetDebugName().get()
<< "\", \"source\":\"\",\n\"phases\":[";
fclose(json_file);
}
} }
pipeline_.RunPrintAndVerify("Machine", true); pipeline_.RunPrintAndVerify("Machine", true);
...@@ -1309,12 +1301,9 @@ struct PrintGraphPhase { ...@@ -1309,12 +1301,9 @@ struct PrintGraphPhase {
{ // Print JSON. { // Print JSON.
AllowHandleDereference allow_deref; AllowHandleDereference allow_deref;
FILE* json_file = OpenVisualizerLogFile(info, nullptr, "json", "a+"); TurboJsonFile json_of(info, std::ios_base::app);
if (json_file == nullptr) return;
OFStream json_of(json_file);
json_of << "{\"name\":\"" << phase << "\",\"type\":\"graph\",\"data\":" json_of << "{\"name\":\"" << phase << "\",\"type\":\"graph\",\"data\":"
<< AsJSON(*graph, data->source_positions()) << "},\n"; << AsJSON(*graph, data->source_positions()) << "},\n";
fclose(json_file);
} }
if (FLAG_trace_turbo_graph) { // Simple textual RPO. if (FLAG_trace_turbo_graph) { // Simple textual RPO.
...@@ -1509,13 +1498,9 @@ Handle<Code> Pipeline::GenerateCodeForCodeStub(Isolate* isolate, ...@@ -1509,13 +1498,9 @@ Handle<Code> Pipeline::GenerateCodeForCodeStub(Isolate* isolate,
DCHECK_NOT_NULL(data.schedule()); DCHECK_NOT_NULL(data.schedule());
if (FLAG_trace_turbo) { if (FLAG_trace_turbo) {
FILE* json_file = OpenVisualizerLogFile(&info, nullptr, "json", "w+"); TurboJsonFile json_of(&info, std::ios_base::trunc);
if (json_file != nullptr) { json_of << "{\"function\":\"" << info.GetDebugName().get()
OFStream json_of(json_file); << "\", \"source\":\"\",\n\"phases\":[";
json_of << "{\"function\":\"" << info.GetDebugName().get()
<< "\", \"source\":\"\",\n\"phases\":[";
fclose(json_file);
}
pipeline.Run<PrintGraphPhase>("Machine"); pipeline.Run<PrintGraphPhase>("Machine");
} }
...@@ -1564,13 +1549,9 @@ Handle<Code> Pipeline::GenerateCodeForTesting(CompilationInfo* info, ...@@ -1564,13 +1549,9 @@ Handle<Code> Pipeline::GenerateCodeForTesting(CompilationInfo* info,
PipelineImpl pipeline(&data); PipelineImpl pipeline(&data);
if (FLAG_trace_turbo) { if (FLAG_trace_turbo) {
FILE* json_file = OpenVisualizerLogFile(info, nullptr, "json", "w+"); TurboJsonFile json_of(info, std::ios_base::trunc);
if (json_file != nullptr) { json_of << "{\"function\":\"" << info->GetDebugName().get()
OFStream json_of(json_file); << "\", \"source\":\"\",\n\"phases\":[";
json_of << "{\"function\":\"" << info->GetDebugName().get()
<< "\", \"source\":\"\",\n\"phases\":[";
fclose(json_file);
}
} }
// TODO(rossberg): Should this really be untyped? // TODO(rossberg): Should this really be untyped?
pipeline.RunPrintAndVerify("Machine", true); pipeline.RunPrintAndVerify("Machine", true);
...@@ -1689,25 +1670,21 @@ Handle<Code> PipelineImpl::GenerateCode(Linkage* linkage) { ...@@ -1689,25 +1670,21 @@ Handle<Code> PipelineImpl::GenerateCode(Linkage* linkage) {
v8::internal::CodeGenerator::PrintCode(code, info()); v8::internal::CodeGenerator::PrintCode(code, info());
if (FLAG_trace_turbo) { if (FLAG_trace_turbo) {
FILE* json_file = OpenVisualizerLogFile(info(), nullptr, "json", "a+"); TurboJsonFile json_of(info(), std::ios_base::app);
if (json_file != nullptr) { json_of << "{\"name\":\"disassembly\",\"type\":\"disassembly\",\"data\":\"";
OFStream json_of(json_file);
json_of
<< "{\"name\":\"disassembly\",\"type\":\"disassembly\",\"data\":\"";
#if ENABLE_DISASSEMBLER #if ENABLE_DISASSEMBLER
std::stringstream disassembly_stream; std::stringstream disassembly_stream;
code->Disassemble(nullptr, disassembly_stream); code->Disassemble(nullptr, disassembly_stream);
std::string disassembly_string(disassembly_stream.str()); std::string disassembly_string(disassembly_stream.str());
for (const auto& c : disassembly_string) { for (const auto& c : disassembly_string) {
json_of << AsEscapedUC16ForJSON(c); json_of << AsEscapedUC16ForJSON(c);
}
#endif // ENABLE_DISASSEMBLER
json_of << "\"}\n],\n";
json_of << "\"nodePositions\":";
json_of << data->source_position_output();
json_of << "}";
fclose(json_file);
} }
#endif // ENABLE_DISASSEMBLER
json_of << "\"}\n],\n";
json_of << "\"nodePositions\":";
json_of << data->source_position_output();
json_of << "}";
OFStream os(stdout); OFStream os(stdout);
os << "---------------------------------------------------\n" os << "---------------------------------------------------\n"
<< "Finished compiling method " << info()->GetDebugName().get() << "Finished compiling method " << info()->GetDebugName().get()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment