88using namespace proton ;
99using json = nlohmann::json;
1010
11+ namespace {
12+
13+ uint64_t getMinInitTime (const std::vector<KernelTrace> &streamTrace) {
14+ uint64_t minInitTime = std::numeric_limits<uint64_t >::max ();
15+ for (const auto &kernelTrace : streamTrace)
16+ for (const auto &bt : kernelTrace.first ->blockTraces ) {
17+ if (bt.initTime < minInitTime) {
18+ minInitTime = bt.initTime ;
19+ }
20+ }
21+ return minInitTime;
22+ }
23+
24+ } // namespace
25+
1126StreamTraceWriter::StreamTraceWriter (
1227 const std::vector<KernelTrace> &streamTrace, const std::string &path)
1328 : streamTrace(streamTrace), path(path) {}
@@ -43,8 +58,10 @@ void StreamChromeTraceWriter::write(std::ostream &outfile) {
4358
4459 json object = {{" displayTimeUnit" , " ns" }, {" traceEvents" , json::array ()}};
4560
61+ const auto minInitTime = getMinInitTime (streamTrace);
62+
4663 for (const auto &kernelTrace : streamTrace) {
47- writeKernel (object, kernelTrace);
64+ writeKernel (object, kernelTrace, minInitTime );
4865 }
4966 outfile << object.dump () << " \n " ;
5067}
@@ -144,7 +161,8 @@ std::vector<int> assignLineIds(
144161} // namespace
145162
146163void StreamChromeTraceWriter::writeKernel (json &object,
147- const KernelTrace &kernelTrace) {
164+ const KernelTrace &kernelTrace,
165+ const uint64_t minInitTime) {
148166 auto result = kernelTrace.first ;
149167 auto metadata = kernelTrace.second ;
150168
@@ -192,16 +210,17 @@ void StreamChromeTraceWriter::writeKernel(json &object,
192210 else
193211 name = metadata->scopeName .at (scopeId);
194212
195- double freq = 1000 ; // Unit: MHz
196-
197- double startCycle = event.first ->cycle ; // Unit: cycle
198- double endCycle = event.second ->cycle ; // Unit: cycle
199- double dur = (endCycle - startCycle) / freq; // Unit: us
213+ // Unit: MHz, we assume freq is 1000MHz (1GHz)
214+ double freq = 1000.0 ;
200215
201- double startCycleRel =
202- startCycle - blockToMinCycle[ctaId]; // Unit: cycle
203- double startTimeRel = startCycleRel / freq; // Unit: us
204- double ts = bt->initTime / 1e3 + startTimeRel; // Unit: us
216+ // Global time is in `ns` unit. With 1GHz assumption, we
217+ // could subtract with blockToMInCycle: (ns - ns) / 1GHz - cycle
218+ int64_t cycleAdjust =
219+ static_cast <int64_t >(bt->initTime - minInitTime) -
220+ static_cast <int64_t >(blockToMinCycle[ctaId]);
221+ int64_t ts = static_cast <int64_t >(event.first ->cycle ) + cycleAdjust;
222+ int64_t dur =
223+ static_cast <int64_t >(event.second ->cycle ) - event.first ->cycle ;
205224
206225 json element;
207226 element[" cname" ] = color;
@@ -210,8 +229,8 @@ void StreamChromeTraceWriter::writeKernel(json &object,
210229 element[" ph" ] = " X" ;
211230 element[" pid" ] = pid;
212231 element[" tid" ] = tid;
213- element[" ts" ] = ts ;
214- element[" dur" ] = dur;
232+ element[" ts" ] = static_cast < double >(ts) / freq ;
233+ element[" dur" ] = static_cast < double >( dur) / freq ;
215234 json args;
216235 args[" Finalization Time (ns)" ] = bt->postFinalTime - bt->preFinalTime ;
217236 args[" Frequency (MHz)" ] = freq;
0 commit comments