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 {
namespace internal {
namespace compiler {
FILE* OpenVisualizerLogFile(CompilationInfo* info, const char* phase,
const char* suffix, const char* mode) {
base::SmartArrayPointer<const char> GetVisualizerLogFileName(
CompilationInfo* info, const char* phase, const char* suffix) {
EmbeddedVector<char, 256> filename(0);
base::SmartArrayPointer<char> debug_name = info->GetDebugName();
if (strlen(debug_name.get()) > 0) {
......@@ -46,7 +45,11 @@ FILE* OpenVisualizerLogFile(CompilationInfo* info, const char* phase,
} else {
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 @@
#include <stdio.h>
#include <iosfwd>
#include "src/base/smart-pointers.h"
namespace v8 {
namespace internal {
......@@ -21,8 +23,8 @@ class RegisterAllocationData;
class Schedule;
class SourcePositionTable;
FILE* OpenVisualizerLogFile(CompilationInfo* info, const char* phase,
const char* suffix, const char* mode);
base::SmartArrayPointer<const char> GetVisualizerLogFileName(
CompilationInfo* info, const char* phase, const char* suffix);
struct AsJSON {
AsJSON(const Graph& g, SourcePositionTable* p) : graph(g), positions(p) {}
......
......@@ -400,13 +400,16 @@ struct TurboCfgFile : public std::ofstream {
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) {
if (FLAG_trace_turbo) {
AllowHandleDereference allow_deref;
FILE* json_file = OpenVisualizerLogFile(info, nullptr, "json", "a+");
if (json_file != nullptr) {
OFStream json_of(json_file);
TurboJsonFile json_of(info, std::ios_base::app);
json_of << "{\"name\":\"Schedule\",\"type\":\"schedule\",\"data\":\"";
std::stringstream schedule_stream;
schedule_stream << *schedule;
......@@ -415,8 +418,6 @@ void TraceSchedule(CompilationInfo* info, Schedule* schedule) {
json_of << AsEscapedUC16ForJSON(c);
}
json_of << "\"},\n";
fclose(json_file);
}
}
if (FLAG_trace_turbo_graph || FLAG_trace_turbo_scheduler) {
AllowHandleDereference allow_deref;
......@@ -526,9 +527,7 @@ PipelineStatistics* CreatePipelineStatistics(CompilationInfo* info,
}
if (FLAG_trace_turbo) {
FILE* json_file = OpenVisualizerLogFile(info, nullptr, "json", "w+");
if (json_file != nullptr) {
OFStream json_of(json_file);
TurboJsonFile json_of(info, std::ios_base::trunc);
Handle<Script> script = info->script();
base::SmartArrayPointer<char> function_name = info->GetDebugName();
int pos = info->shared_info()->start_position();
......@@ -538,15 +537,12 @@ PipelineStatistics* CreatePipelineStatistics(CompilationInfo* info,
DisallowHeapAllocation no_allocation;
int start = info->shared_info()->start_position();
int len = info->shared_info()->end_position() - start;
String::SubStringRange source(String::cast(script->source()), start,
len);
String::SubStringRange source(String::cast(script->source()), start, len);
for (const auto& c : source) {
json_of << AsEscapedUC16ForJSON(c);
}
}
json_of << "\",\n\"phases\":[";
fclose(json_file);
}
}
return pipeline_statistics;
......@@ -667,13 +663,9 @@ PipelineWasmCompilationJob::CreateGraphImpl() {
PipelineWasmCompilationJob::Status
PipelineWasmCompilationJob::OptimizeGraphImpl() {
if (FLAG_trace_turbo) {
FILE* json_file = OpenVisualizerLogFile(info(), nullptr, "json", "w+");
if (json_file != nullptr) {
OFStream json_of(json_file);
TurboJsonFile json_of(info(), std::ios_base::trunc);
json_of << "{\"function\":\"" << info()->GetDebugName().get()
<< "\", \"source\":\"\",\n\"phases\":[";
fclose(json_file);
}
}
pipeline_.RunPrintAndVerify("Machine", true);
......@@ -1309,12 +1301,9 @@ struct PrintGraphPhase {
{ // Print JSON.
AllowHandleDereference allow_deref;
FILE* json_file = OpenVisualizerLogFile(info, nullptr, "json", "a+");
if (json_file == nullptr) return;
OFStream json_of(json_file);
TurboJsonFile json_of(info, std::ios_base::app);
json_of << "{\"name\":\"" << phase << "\",\"type\":\"graph\",\"data\":"
<< AsJSON(*graph, data->source_positions()) << "},\n";
fclose(json_file);
}
if (FLAG_trace_turbo_graph) { // Simple textual RPO.
......@@ -1509,13 +1498,9 @@ Handle<Code> Pipeline::GenerateCodeForCodeStub(Isolate* isolate,
DCHECK_NOT_NULL(data.schedule());
if (FLAG_trace_turbo) {
FILE* json_file = OpenVisualizerLogFile(&info, nullptr, "json", "w+");
if (json_file != nullptr) {
OFStream json_of(json_file);
TurboJsonFile json_of(&info, std::ios_base::trunc);
json_of << "{\"function\":\"" << info.GetDebugName().get()
<< "\", \"source\":\"\",\n\"phases\":[";
fclose(json_file);
}
pipeline.Run<PrintGraphPhase>("Machine");
}
......@@ -1564,13 +1549,9 @@ Handle<Code> Pipeline::GenerateCodeForTesting(CompilationInfo* info,
PipelineImpl pipeline(&data);
if (FLAG_trace_turbo) {
FILE* json_file = OpenVisualizerLogFile(info, nullptr, "json", "w+");
if (json_file != nullptr) {
OFStream json_of(json_file);
TurboJsonFile json_of(info, std::ios_base::trunc);
json_of << "{\"function\":\"" << info->GetDebugName().get()
<< "\", \"source\":\"\",\n\"phases\":[";
fclose(json_file);
}
}
// TODO(rossberg): Should this really be untyped?
pipeline.RunPrintAndVerify("Machine", true);
......@@ -1689,11 +1670,8 @@ Handle<Code> PipelineImpl::GenerateCode(Linkage* linkage) {
v8::internal::CodeGenerator::PrintCode(code, info());
if (FLAG_trace_turbo) {
FILE* json_file = OpenVisualizerLogFile(info(), nullptr, "json", "a+");
if (json_file != nullptr) {
OFStream json_of(json_file);
json_of
<< "{\"name\":\"disassembly\",\"type\":\"disassembly\",\"data\":\"";
TurboJsonFile json_of(info(), std::ios_base::app);
json_of << "{\"name\":\"disassembly\",\"type\":\"disassembly\",\"data\":\"";
#if ENABLE_DISASSEMBLER
std::stringstream disassembly_stream;
code->Disassemble(nullptr, disassembly_stream);
......@@ -1706,8 +1684,7 @@ Handle<Code> PipelineImpl::GenerateCode(Linkage* linkage) {
json_of << "\"nodePositions\":";
json_of << data->source_position_output();
json_of << "}";
fclose(json_file);
}
OFStream os(stdout);
os << "---------------------------------------------------\n"
<< "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