From cba76414e15415cea993b8ab2e3b0ce0edb46944 Mon Sep 17 00:00:00 2001 From: baldurk Date: Fri, 24 Nov 2017 11:37:30 +0000 Subject: [PATCH] Add exporter for Chrome's trace profiler from chunk metadata. --- renderdoc/CMakeLists.txt | 1 + renderdoc/renderdoc.vcxproj | 1 + renderdoc/renderdoc.vcxproj.filters | 3 + .../serialise/codecs/chrome_json_codec.cpp | 89 +++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 renderdoc/serialise/codecs/chrome_json_codec.cpp diff --git a/renderdoc/CMakeLists.txt b/renderdoc/CMakeLists.txt index 91a40e286..6cc37d50d 100644 --- a/renderdoc/CMakeLists.txt +++ b/renderdoc/CMakeLists.txt @@ -133,6 +133,7 @@ set(sources serialise/rdcfile.cpp serialise/rdcfile.h serialise/codecs/xml_codec.cpp + serialise/codecs/chrome_json_codec.cpp serialise/comp_io_tests.cpp serialise/serialiser_tests.cpp serialise/streamio_tests.cpp diff --git a/renderdoc/renderdoc.vcxproj b/renderdoc/renderdoc.vcxproj index 7b59a0c3a..6f7e3fd8d 100644 --- a/renderdoc/renderdoc.vcxproj +++ b/renderdoc/renderdoc.vcxproj @@ -396,6 +396,7 @@ + diff --git a/renderdoc/renderdoc.vcxproj.filters b/renderdoc/renderdoc.vcxproj.filters index 2cb99ccfd..2650c2155 100644 --- a/renderdoc/renderdoc.vcxproj.filters +++ b/renderdoc/renderdoc.vcxproj.filters @@ -632,6 +632,9 @@ Common\Serialise + + Common\Serialise\Codecs + diff --git a/renderdoc/serialise/codecs/chrome_json_codec.cpp b/renderdoc/serialise/codecs/chrome_json_codec.cpp new file mode 100644 index 000000000..f9e9bf2b3 --- /dev/null +++ b/renderdoc/serialise/codecs/chrome_json_codec.cpp @@ -0,0 +1,89 @@ +/****************************************************************************** + * The MIT License (MIT) + * + * Copyright (c) 2017 Baldur Karlsson + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + ******************************************************************************/ + +#include +#include "common/common.h" +#include "serialise/rdcfile.h" + +ReplayStatus exportChrome(const char *filename, const RDCFile &rdc, const SDFile &structData, + float *progress) +{ + FILE *f = FileIO::fopen(filename, "w"); + + if(!f) + return ReplayStatus::FileIOFailed; + + std::string str; + + // add header, customise this as needed. + str = R"({ + "displayTimeUnit": "ns", + "traceEvents": [)"; + + const char *category = "Initialisation"; + + // stupid JSON not allowing trailing ,s :( + bool first = true; + + for(const SDChunk *chunk : structData.chunks) + { + if(chunk->metadata.chunkID == (uint32_t)SystemChunk::FirstDriverChunk + 1) + category = "Frame Capture"; + + if(!first) + str += ","; + + first = false; + + const char *fmt = R"( + { "name": "%s", "cat": "%s", "ph": "B", "ts": %llu, "pid": 5, "tid": %u }, + { "ph": "E", "ts": %llu, "pid": 5, "tid": %u })"; + + if(chunk->metadata.durationMicro == 0) + { + fmt = R"( + { "name": "%s", "cat": "%s", "ph": "i", "ts": %llu, "pid": 5, "tid": %u })"; + } + + str += StringFormat::Fmt( + fmt, chunk->name.c_str(), category, chunk->metadata.timestampMicro, chunk->metadata.threadID, + chunk->metadata.timestampMicro + chunk->metadata.durationMicro, chunk->metadata.threadID); + } + + // end trace events + str += "\n ]\n}"; + + FileIO::fwrite(str.data(), 1, str.size(), f); + + FileIO::fclose(f); + + return ReplayStatus::Succeeded; +} + +static ConversionRegistration XMLConversionRegistration("chrome.json", R"(Chrome profiler JSON. + +Exports the chunk threadID, timestamp and duration data to a JSON format that can be loaded by +chrome's profiler at chrome://tracing +)", + &exportChrome); \ No newline at end of file