Commit 06eaaac5 by Jamie Madill Committed by Commit Bot

Fix trace writing in angle_perftests.

We can use this to produce AGI-like reports in standalone desktop. Bug: b/172704839 Change-Id: Ifc510232e3da81210e22429b6cea9800e2723e06 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2524540Reviewed-by: 's avatarCody Northrop <cnorthrop@google.com> Reviewed-by: 's avatarShahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Jamie Madill <jmadill@chromium.org>
parent a5e0a4bc
......@@ -249,7 +249,6 @@ template("angle_perftests_common") {
"test_utils/runner/HistogramWriter.h",
"test_utils/runner/TestSuite.h",
]
deps = [ "$angle_jsoncpp_dir:jsoncpp" ]
public_deps = [
"$angle_root/third_party/rapidjson:rapidjson",
"${invoker.test_utils}",
......
......@@ -26,13 +26,17 @@
#include <iostream>
#include <sstream>
#include <json/json.h>
#include <rapidjson/document.h>
#include <rapidjson/filewritestream.h>
#include <rapidjson/istreamwrapper.h>
#include <rapidjson/prettywriter.h>
#if defined(ANGLE_USE_UTIL_LOADER) && defined(ANGLE_PLATFORM_WINDOWS)
# include "util/windows/WGLWindow.h"
#endif // defined(ANGLE_USE_UTIL_LOADER) &&defined(ANGLE_PLATFORM_WINDOWS)
using namespace angle;
namespace js = rapidjson;
namespace
{
......@@ -124,39 +128,66 @@ double MonotonicallyIncreasingTime(angle::PlatformMethods *platform)
return GetHostTimeSeconds();
}
bool WriteJsonFile(const std::string &outputFile, js::Document *doc)
{
FILE *fp = fopen(outputFile.c_str(), "w");
if (!fp)
{
return false;
}
constexpr size_t kBufferSize = 0xFFFF;
std::vector<char> writeBuffer(kBufferSize);
js::FileWriteStream os(fp, writeBuffer.data(), kBufferSize);
js::PrettyWriter<js::FileWriteStream> writer(os);
if (!doc->Accept(writer))
{
fclose(fp);
return false;
}
fclose(fp);
return true;
}
void DumpTraceEventsToJSONFile(const std::vector<TraceEvent> &traceEvents,
const char *outputFileName)
{
Json::Value eventsValue(Json::arrayValue);
js::Document doc(js::kObjectType);
js::Document::AllocatorType &allocator = doc.GetAllocator();
js::Value events(js::kArrayType);
for (const TraceEvent &traceEvent : traceEvents)
{
Json::Value value(Json::objectValue);
const auto microseconds = static_cast<Json::LargestInt>(traceEvent.timestamp) * 1000 * 1000;
js::Value value(js::kObjectType);
value["name"] = traceEvent.name;
value["cat"] = traceEvent.categoryName;
value["ph"] = std::string(1, traceEvent.phase);
value["ts"] = microseconds;
value["pid"] = strcmp(traceEvent.categoryName, "gpu.angle.gpu") == 0 ? "GPU" : "ANGLE";
value["tid"] = 1;
const uint64_t microseconds = static_cast<uint64_t>(traceEvent.timestamp * 1000.0 * 1000.0);
eventsValue.append(std::move(value));
}
js::Document::StringRefType eventName(traceEvent.name);
js::Document::StringRefType categoryName(traceEvent.categoryName);
js::Document::StringRefType pidName(
strcmp(traceEvent.categoryName, "gpu.angle.gpu") == 0 ? "GPU" : "ANGLE");
Json::Value root(Json::objectValue);
root["traceEvents"] = eventsValue;
value.AddMember("name", eventName, allocator);
value.AddMember("cat", categoryName, allocator);
value.AddMember("ph", std::string(1, traceEvent.phase), allocator);
value.AddMember("ts", microseconds, allocator);
value.AddMember("pid", pidName, allocator);
value.AddMember("tid", 1, allocator);
std::ofstream outFile;
outFile.open(outputFileName);
events.PushBack(value, allocator);
}
Json::StreamWriterBuilder factory;
std::unique_ptr<Json::StreamWriter> const writer(factory.newStreamWriter());
std::ostringstream stream;
writer->write(root, &outFile);
doc.AddMember("traceEvents", events, allocator);
outFile.close();
if (WriteJsonFile(outputFileName, &doc))
{
printf("Wrote trace file to %s\n", outputFileName);
}
else
{
printf("Error writing trace file to %s\n", outputFileName);
}
}
} // anonymous namespace
......
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