• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "perfetto/ext/trace_processor/export_json.h"
18 #include "src/trace_processor/export_json.h"
19 
20 #include <stdio.h>
21 #include <sstream>
22 
23 #include <algorithm>
24 #include <cinttypes>
25 #include <cmath>
26 #include <cstring>
27 #include <deque>
28 #include <limits>
29 #include <memory>
30 
31 #include "perfetto/base/build_config.h"
32 #include "perfetto/ext/base/string_splitter.h"
33 #include "perfetto/ext/base/string_utils.h"
34 #include "src/trace_processor/importers/json/json_utils.h"
35 #include "src/trace_processor/storage/metadata.h"
36 #include "src/trace_processor/storage/trace_storage.h"
37 #include "src/trace_processor/trace_processor_storage_impl.h"
38 #include "src/trace_processor/types/trace_processor_context.h"
39 
40 #if PERFETTO_BUILDFLAG(PERFETTO_TP_JSON)
41 #include <json/reader.h>
42 #include <json/writer.h>
43 #endif
44 
45 namespace perfetto {
46 namespace trace_processor {
47 namespace json {
48 
49 namespace {
50 
51 class FileWriter : public OutputWriter {
52  public:
FileWriter(FILE * file)53   FileWriter(FILE* file) : file_(file) {}
~FileWriter()54   ~FileWriter() override { fflush(file_); }
55 
AppendString(const std::string & s)56   util::Status AppendString(const std::string& s) override {
57     size_t written =
58         fwrite(s.data(), sizeof(std::string::value_type), s.size(), file_);
59     if (written != s.size())
60       return util::ErrStatus("Error writing to file: %d", ferror(file_));
61     return util::OkStatus();
62   }
63 
64  private:
65   FILE* file_;
66 };
67 
68 #if PERFETTO_BUILDFLAG(PERFETTO_TP_JSON)
69 using IndexMap = perfetto::trace_processor::TraceStorage::Stats::IndexMap;
70 
71 const char kLegacyEventArgsKey[] = "legacy_event";
72 const char kLegacyEventPassthroughUtidKey[] = "passthrough_utid";
73 const char kLegacyEventCategoryKey[] = "category";
74 const char kLegacyEventNameKey[] = "name";
75 const char kLegacyEventPhaseKey[] = "phase";
76 const char kLegacyEventDurationNsKey[] = "duration_ns";
77 const char kLegacyEventThreadTimestampNsKey[] = "thread_timestamp_ns";
78 const char kLegacyEventThreadDurationNsKey[] = "thread_duration_ns";
79 const char kLegacyEventThreadInstructionCountKey[] = "thread_instruction_count";
80 const char kLegacyEventThreadInstructionDeltaKey[] = "thread_instruction_delta";
81 const char kLegacyEventUseAsyncTtsKey[] = "use_async_tts";
82 const char kLegacyEventUnscopedIdKey[] = "unscoped_id";
83 const char kLegacyEventGlobalIdKey[] = "global_id";
84 const char kLegacyEventLocalIdKey[] = "local_id";
85 const char kLegacyEventIdScopeKey[] = "id_scope";
86 const char kStrippedArgument[] = "__stripped__";
87 
GetNonNullString(const TraceStorage * storage,std::optional<StringId> id)88 const char* GetNonNullString(const TraceStorage* storage,
89                              std::optional<StringId> id) {
90   return id == std::nullopt || *id == kNullStringId
91              ? ""
92              : storage->GetString(*id).c_str();
93 }
94 
95 class JsonExporter {
96  public:
JsonExporter(const TraceStorage * storage,OutputWriter * output,ArgumentFilterPredicate argument_filter,MetadataFilterPredicate metadata_filter,LabelFilterPredicate label_filter)97   JsonExporter(const TraceStorage* storage,
98                OutputWriter* output,
99                ArgumentFilterPredicate argument_filter,
100                MetadataFilterPredicate metadata_filter,
101                LabelFilterPredicate label_filter)
102       : storage_(storage),
103         args_builder_(storage_),
104         writer_(output, argument_filter, metadata_filter, label_filter) {}
105 
Export()106   util::Status Export() {
107     util::Status status = MapUniquePidsAndTids();
108     if (!status.ok())
109       return status;
110 
111     status = ExportThreadNames();
112     if (!status.ok())
113       return status;
114 
115     status = ExportProcessNames();
116     if (!status.ok())
117       return status;
118 
119     status = ExportProcessUptimes();
120     if (!status.ok())
121       return status;
122 
123     status = ExportSlices();
124     if (!status.ok())
125       return status;
126 
127     status = ExportFlows();
128     if (!status.ok())
129       return status;
130 
131     status = ExportRawEvents();
132     if (!status.ok())
133       return status;
134 
135     status = ExportCpuProfileSamples();
136     if (!status.ok())
137       return status;
138 
139     status = ExportMetadata();
140     if (!status.ok())
141       return status;
142 
143     status = ExportStats();
144     if (!status.ok())
145       return status;
146 
147     status = ExportMemorySnapshots();
148     if (!status.ok())
149       return status;
150 
151     return util::OkStatus();
152   }
153 
154  private:
155   class TraceFormatWriter {
156    public:
TraceFormatWriter(OutputWriter * output,ArgumentFilterPredicate argument_filter,MetadataFilterPredicate metadata_filter,LabelFilterPredicate label_filter)157     TraceFormatWriter(OutputWriter* output,
158                       ArgumentFilterPredicate argument_filter,
159                       MetadataFilterPredicate metadata_filter,
160                       LabelFilterPredicate label_filter)
161         : output_(output),
162           argument_filter_(argument_filter),
163           metadata_filter_(metadata_filter),
164           label_filter_(label_filter),
165           first_event_(true) {
166       Json::StreamWriterBuilder b;
167       b.settings_["indentation"] = "";
168       writer_.reset(b.newStreamWriter());
169       WriteHeader();
170     }
171 
~TraceFormatWriter()172     ~TraceFormatWriter() { WriteFooter(); }
173 
WriteCommonEvent(const Json::Value & event)174     void WriteCommonEvent(const Json::Value& event) {
175       if (label_filter_ && !label_filter_("traceEvents"))
176         return;
177 
178       DoWriteEvent(event);
179     }
180 
AddAsyncBeginEvent(const Json::Value & event)181     void AddAsyncBeginEvent(const Json::Value& event) {
182       if (label_filter_ && !label_filter_("traceEvents"))
183         return;
184 
185       async_begin_events_.push_back(event);
186     }
187 
AddAsyncInstantEvent(const Json::Value & event)188     void AddAsyncInstantEvent(const Json::Value& event) {
189       if (label_filter_ && !label_filter_("traceEvents"))
190         return;
191 
192       async_instant_events_.push_back(event);
193     }
194 
AddAsyncEndEvent(const Json::Value & event)195     void AddAsyncEndEvent(const Json::Value& event) {
196       if (label_filter_ && !label_filter_("traceEvents"))
197         return;
198 
199       async_end_events_.push_back(event);
200     }
201 
SortAndEmitAsyncEvents()202     void SortAndEmitAsyncEvents() {
203       // Catapult doesn't handle out-of-order begin/end events well, especially
204       // when their timestamps are the same, but their order is incorrect. Since
205       // we process events sorted by begin timestamp, |async_begin_events_| and
206       // |async_instant_events_| are already sorted. We now only have to sort
207       // |async_end_events_| and merge-sort all events into a single sequence.
208 
209       // Sort |async_end_events_|. Note that we should order by ascending
210       // timestamp, but in reverse-stable order. This way, a child slices's end
211       // is emitted before its parent's end event, even if both end events have
212       // the same timestamp. To accomplish this, we perform a stable sort in
213       // descending order and later iterate via reverse iterators.
214       struct {
215         bool operator()(const Json::Value& a, const Json::Value& b) const {
216           return a["ts"].asInt64() > b["ts"].asInt64();
217         }
218       } CompareEvents;
219       std::stable_sort(async_end_events_.begin(), async_end_events_.end(),
220                        CompareEvents);
221 
222       // Merge sort by timestamp. If events share the same timestamp, prefer
223       // instant events, then end events, so that old slices close before new
224       // ones are opened, but instant events remain in their deepest nesting
225       // level.
226       auto instant_event_it = async_instant_events_.begin();
227       auto end_event_it = async_end_events_.rbegin();
228       auto begin_event_it = async_begin_events_.begin();
229 
230       auto has_instant_event = instant_event_it != async_instant_events_.end();
231       auto has_end_event = end_event_it != async_end_events_.rend();
232       auto has_begin_event = begin_event_it != async_begin_events_.end();
233 
234       auto emit_next_instant = [&instant_event_it, &has_instant_event, this]() {
235         DoWriteEvent(*instant_event_it);
236         instant_event_it++;
237         has_instant_event = instant_event_it != async_instant_events_.end();
238       };
239       auto emit_next_end = [&end_event_it, &has_end_event, this]() {
240         DoWriteEvent(*end_event_it);
241         end_event_it++;
242         has_end_event = end_event_it != async_end_events_.rend();
243       };
244       auto emit_next_begin = [&begin_event_it, &has_begin_event, this]() {
245         DoWriteEvent(*begin_event_it);
246         begin_event_it++;
247         has_begin_event = begin_event_it != async_begin_events_.end();
248       };
249 
250       auto emit_next_instant_or_end = [&instant_event_it, &end_event_it,
251                                        &emit_next_instant, &emit_next_end]() {
252         if ((*instant_event_it)["ts"].asInt64() <=
253             (*end_event_it)["ts"].asInt64()) {
254           emit_next_instant();
255         } else {
256           emit_next_end();
257         }
258       };
259       auto emit_next_instant_or_begin = [&instant_event_it, &begin_event_it,
260                                          &emit_next_instant,
261                                          &emit_next_begin]() {
262         if ((*instant_event_it)["ts"].asInt64() <=
263             (*begin_event_it)["ts"].asInt64()) {
264           emit_next_instant();
265         } else {
266           emit_next_begin();
267         }
268       };
269       auto emit_next_end_or_begin = [&end_event_it, &begin_event_it,
270                                      &emit_next_end, &emit_next_begin]() {
271         if ((*end_event_it)["ts"].asInt64() <=
272             (*begin_event_it)["ts"].asInt64()) {
273           emit_next_end();
274         } else {
275           emit_next_begin();
276         }
277       };
278 
279       // While we still have events in all iterators, consider each.
280       while (has_instant_event && has_end_event && has_begin_event) {
281         if ((*instant_event_it)["ts"].asInt64() <=
282             (*end_event_it)["ts"].asInt64()) {
283           emit_next_instant_or_begin();
284         } else {
285           emit_next_end_or_begin();
286         }
287       }
288 
289       // Only instant and end events left.
290       while (has_instant_event && has_end_event) {
291         emit_next_instant_or_end();
292       }
293 
294       // Only instant and begin events left.
295       while (has_instant_event && has_begin_event) {
296         emit_next_instant_or_begin();
297       }
298 
299       // Only end and begin events left.
300       while (has_end_event && has_begin_event) {
301         emit_next_end_or_begin();
302       }
303 
304       // Remaining instant events.
305       while (has_instant_event) {
306         emit_next_instant();
307       }
308 
309       // Remaining end events.
310       while (has_end_event) {
311         emit_next_end();
312       }
313 
314       // Remaining begin events.
315       while (has_begin_event) {
316         emit_next_begin();
317       }
318     }
319 
WriteMetadataEvent(const char * metadata_type,const char * metadata_arg_name,const char * metadata_arg_value,uint32_t pid,uint32_t tid)320     void WriteMetadataEvent(const char* metadata_type,
321                             const char* metadata_arg_name,
322                             const char* metadata_arg_value,
323                             uint32_t pid,
324                             uint32_t tid) {
325       if (label_filter_ && !label_filter_("traceEvents"))
326         return;
327 
328       std::ostringstream ss;
329       if (!first_event_)
330         ss << ",\n";
331 
332       Json::Value value;
333       value["ph"] = "M";
334       value["cat"] = "__metadata";
335       value["ts"] = 0;
336       value["name"] = metadata_type;
337       value["pid"] = Json::Int(pid);
338       value["tid"] = Json::Int(tid);
339 
340       Json::Value args;
341       args[metadata_arg_name] = metadata_arg_value;
342       value["args"] = args;
343 
344       writer_->write(value, &ss);
345       output_->AppendString(ss.str());
346       first_event_ = false;
347     }
348 
MergeMetadata(const Json::Value & value)349     void MergeMetadata(const Json::Value& value) {
350       for (const auto& member : value.getMemberNames()) {
351         metadata_[member] = value[member];
352       }
353     }
354 
AppendTelemetryMetadataString(const char * key,const char * value)355     void AppendTelemetryMetadataString(const char* key, const char* value) {
356       metadata_["telemetry"][key].append(value);
357     }
358 
AppendTelemetryMetadataInt(const char * key,int64_t value)359     void AppendTelemetryMetadataInt(const char* key, int64_t value) {
360       metadata_["telemetry"][key].append(Json::Int64(value));
361     }
362 
AppendTelemetryMetadataBool(const char * key,bool value)363     void AppendTelemetryMetadataBool(const char* key, bool value) {
364       metadata_["telemetry"][key].append(value);
365     }
366 
SetTelemetryMetadataTimestamp(const char * key,int64_t value)367     void SetTelemetryMetadataTimestamp(const char* key, int64_t value) {
368       metadata_["telemetry"][key] = static_cast<double>(value) / 1000.0;
369     }
370 
SetStats(const char * key,int64_t value)371     void SetStats(const char* key, int64_t value) {
372       metadata_["trace_processor_stats"][key] = Json::Int64(value);
373     }
374 
SetStats(const char * key,const IndexMap & indexed_values)375     void SetStats(const char* key, const IndexMap& indexed_values) {
376       constexpr const char* kBufferStatsPrefix = "traced_buf_";
377 
378       // Stats for the same buffer should be grouped together in the JSON.
379       if (strncmp(kBufferStatsPrefix, key, strlen(kBufferStatsPrefix)) == 0) {
380         for (const auto& value : indexed_values) {
381           metadata_["trace_processor_stats"]["traced_buf"][value.first]
382                    [key + strlen(kBufferStatsPrefix)] =
383                        Json::Int64(value.second);
384         }
385         return;
386       }
387 
388       // Other indexed value stats are exported as array under their key.
389       for (const auto& value : indexed_values) {
390         metadata_["trace_processor_stats"][key][value.first] =
391             Json::Int64(value.second);
392       }
393     }
394 
AddSystemTraceData(const std::string & data)395     void AddSystemTraceData(const std::string& data) {
396       system_trace_data_ += data;
397     }
398 
AddUserTraceData(const std::string & data)399     void AddUserTraceData(const std::string& data) {
400       if (user_trace_data_.empty())
401         user_trace_data_ = "[";
402       user_trace_data_ += data;
403     }
404 
405    private:
WriteHeader()406     void WriteHeader() {
407       if (!label_filter_)
408         output_->AppendString("{\"traceEvents\":[\n");
409     }
410 
WriteFooter()411     void WriteFooter() {
412       SortAndEmitAsyncEvents();
413 
414       // Filter metadata entries.
415       if (metadata_filter_) {
416         for (const auto& member : metadata_.getMemberNames()) {
417           if (!metadata_filter_(member.c_str()))
418             metadata_[member] = kStrippedArgument;
419         }
420       }
421 
422       if ((!label_filter_ || label_filter_("traceEvents")) &&
423           !user_trace_data_.empty()) {
424         user_trace_data_ += "]";
425 
426         Json::CharReaderBuilder builder;
427         auto reader =
428             std::unique_ptr<Json::CharReader>(builder.newCharReader());
429         Json::Value result;
430         if (reader->parse(user_trace_data_.data(),
431                           user_trace_data_.data() + user_trace_data_.length(),
432                           &result, nullptr)) {
433           for (const auto& event : result) {
434             WriteCommonEvent(event);
435           }
436         } else {
437           PERFETTO_DLOG(
438               "can't parse legacy user json trace export, skipping. data: %s",
439               user_trace_data_.c_str());
440         }
441       }
442 
443       std::ostringstream ss;
444       if (!label_filter_)
445         ss << "]";
446 
447       if ((!label_filter_ || label_filter_("systemTraceEvents")) &&
448           !system_trace_data_.empty()) {
449         ss << ",\"systemTraceEvents\":\n";
450         writer_->write(Json::Value(system_trace_data_), &ss);
451       }
452 
453       if ((!label_filter_ || label_filter_("metadata")) && !metadata_.empty()) {
454         ss << ",\"metadata\":\n";
455         writer_->write(metadata_, &ss);
456       }
457 
458       if (!label_filter_)
459         ss << "}";
460 
461       output_->AppendString(ss.str());
462     }
463 
DoWriteEvent(const Json::Value & event)464     void DoWriteEvent(const Json::Value& event) {
465       std::ostringstream ss;
466       if (!first_event_)
467         ss << ",\n";
468 
469       ArgumentNameFilterPredicate argument_name_filter;
470       bool strip_args =
471           argument_filter_ &&
472           !argument_filter_(event["cat"].asCString(), event["name"].asCString(),
473                             &argument_name_filter);
474       if ((strip_args || argument_name_filter) && event.isMember("args")) {
475         Json::Value event_copy = event;
476         if (strip_args) {
477           event_copy["args"] = kStrippedArgument;
478         } else {
479           auto& args = event_copy["args"];
480           for (const auto& member : event["args"].getMemberNames()) {
481             if (!argument_name_filter(member.c_str()))
482               args[member] = kStrippedArgument;
483           }
484         }
485         writer_->write(event_copy, &ss);
486       } else {
487         writer_->write(event, &ss);
488       }
489       first_event_ = false;
490 
491       output_->AppendString(ss.str());
492     }
493 
494     OutputWriter* output_;
495     ArgumentFilterPredicate argument_filter_;
496     MetadataFilterPredicate metadata_filter_;
497     LabelFilterPredicate label_filter_;
498 
499     std::unique_ptr<Json::StreamWriter> writer_;
500     bool first_event_;
501     Json::Value metadata_;
502     std::string system_trace_data_;
503     std::string user_trace_data_;
504     std::vector<Json::Value> async_begin_events_;
505     std::vector<Json::Value> async_instant_events_;
506     std::vector<Json::Value> async_end_events_;
507   };
508 
509   class ArgsBuilder {
510    public:
ArgsBuilder(const TraceStorage * storage)511     explicit ArgsBuilder(const TraceStorage* storage)
512         : storage_(storage),
513           empty_value_(Json::objectValue),
514           nan_value_(Json::StaticString("NaN")),
515           inf_value_(Json::StaticString("Infinity")),
516           neg_inf_value_(Json::StaticString("-Infinity")) {
517       const auto& arg_table = storage_->arg_table();
518       uint32_t count = arg_table.row_count();
519       if (count == 0) {
520         args_sets_.resize(1, empty_value_);
521         return;
522       }
523       args_sets_.resize(arg_table.arg_set_id()[count - 1] + 1, empty_value_);
524 
525       for (uint32_t i = 0; i < count; ++i) {
526         ArgSetId set_id = arg_table.arg_set_id()[i];
527         const char* key = arg_table.key().GetString(i).c_str();
528         Variadic value = storage_->GetArgValue(i);
529         AppendArg(set_id, key, VariadicToJson(value));
530       }
531       PostprocessArgs();
532     }
533 
GetArgs(ArgSetId set_id) const534     const Json::Value& GetArgs(ArgSetId set_id) const {
535       // If |set_id| was empty and added to the storage last, it may not be in
536       // args_sets_.
537       if (set_id > args_sets_.size())
538         return empty_value_;
539       return args_sets_[set_id];
540     }
541 
542    private:
VariadicToJson(Variadic variadic)543     Json::Value VariadicToJson(Variadic variadic) {
544       switch (variadic.type) {
545         case Variadic::kInt:
546           return Json::Int64(variadic.int_value);
547         case Variadic::kUint:
548           return Json::UInt64(variadic.uint_value);
549         case Variadic::kString:
550           return GetNonNullString(storage_, variadic.string_value);
551         case Variadic::kReal:
552           if (std::isnan(variadic.real_value)) {
553             return nan_value_;
554           } else if (std::isinf(variadic.real_value) &&
555                      variadic.real_value > 0) {
556             return inf_value_;
557           } else if (std::isinf(variadic.real_value) &&
558                      variadic.real_value < 0) {
559             return neg_inf_value_;
560           } else {
561             return variadic.real_value;
562           }
563         case Variadic::kPointer:
564           return base::Uint64ToHexString(variadic.pointer_value);
565         case Variadic::kBool:
566           return variadic.bool_value;
567         case Variadic::kNull:
568           return base::Uint64ToHexString(0);
569         case Variadic::kJson:
570           Json::CharReaderBuilder b;
571           auto reader = std::unique_ptr<Json::CharReader>(b.newCharReader());
572 
573           Json::Value result;
574           std::string v = GetNonNullString(storage_, variadic.json_value);
575           reader->parse(v.data(), v.data() + v.length(), &result, nullptr);
576           return result;
577       }
578       PERFETTO_FATAL("Not reached");  // For gcc.
579     }
580 
AppendArg(ArgSetId set_id,const std::string & key,const Json::Value & value)581     void AppendArg(ArgSetId set_id,
582                    const std::string& key,
583                    const Json::Value& value) {
584       Json::Value* target = &args_sets_[set_id];
585       for (base::StringSplitter parts(key, '.'); parts.Next();) {
586         if (PERFETTO_UNLIKELY(!target->isNull() && !target->isObject())) {
587           PERFETTO_DLOG("Malformed arguments. Can't append %s to %s.",
588                         key.c_str(),
589                         args_sets_[set_id].toStyledString().c_str());
590           return;
591         }
592         std::string key_part = parts.cur_token();
593         size_t bracketpos = key_part.find('[');
594         if (bracketpos == key_part.npos) {  // A single item
595           target = &(*target)[key_part];
596         } else {  // A list item
597           target = &(*target)[key_part.substr(0, bracketpos)];
598           while (bracketpos != key_part.npos) {
599             // We constructed this string from an int earlier in trace_processor
600             // so it shouldn't be possible for this (or the StringToUInt32
601             // below) to fail.
602             std::string s =
603                 key_part.substr(bracketpos + 1, key_part.find(']', bracketpos) -
604                                                     bracketpos - 1);
605             if (PERFETTO_UNLIKELY(!target->isNull() && !target->isArray())) {
606               PERFETTO_DLOG("Malformed arguments. Can't append %s to %s.",
607                             key.c_str(),
608                             args_sets_[set_id].toStyledString().c_str());
609               return;
610             }
611             std::optional<uint32_t> index = base::StringToUInt32(s);
612             if (PERFETTO_UNLIKELY(!index)) {
613               PERFETTO_ELOG("Expected to be able to extract index from %s",
614                             key_part.c_str());
615               return;
616             }
617             target = &(*target)[index.value()];
618             bracketpos = key_part.find('[', bracketpos + 1);
619           }
620         }
621       }
622       *target = value;
623     }
624 
PostprocessArgs()625     void PostprocessArgs() {
626       for (Json::Value& args : args_sets_) {
627         // Move all fields from "debug" key to upper level.
628         if (args.isMember("debug")) {
629           Json::Value debug = args["debug"];
630           args.removeMember("debug");
631           for (const auto& member : debug.getMemberNames()) {
632             args[member] = debug[member];
633           }
634         }
635 
636         // Rename source fields.
637         if (args.isMember("task")) {
638           if (args["task"].isMember("posted_from")) {
639             Json::Value posted_from = args["task"]["posted_from"];
640             args["task"].removeMember("posted_from");
641             if (posted_from.isMember("function_name")) {
642               args["src_func"] = posted_from["function_name"];
643               args["src_file"] = posted_from["file_name"];
644             } else if (posted_from.isMember("file_name")) {
645               args["src"] = posted_from["file_name"];
646             }
647           }
648           if (args["task"].empty())
649             args.removeMember("task");
650         }
651         if (args.isMember("source")) {
652           Json::Value source = args["source"];
653           if (source.isObject() && source.isMember("function_name")) {
654             args["function_name"] = source["function_name"];
655             args["file_name"] = source["file_name"];
656             args.removeMember("source");
657           }
658         }
659       }
660     }
661 
662     const TraceStorage* storage_;
663     std::vector<Json::Value> args_sets_;
664     const Json::Value empty_value_;
665     const Json::Value nan_value_;
666     const Json::Value inf_value_;
667     const Json::Value neg_inf_value_;
668   };
669 
MapUniquePidsAndTids()670   util::Status MapUniquePidsAndTids() {
671     const auto& process_table = storage_->process_table();
672     for (UniquePid upid = 0; upid < process_table.row_count(); upid++) {
673       uint32_t exported_pid = process_table.pid()[upid];
674       auto it_and_inserted =
675           exported_pids_to_upids_.emplace(exported_pid, upid);
676       if (!it_and_inserted.second) {
677         exported_pid = NextExportedPidOrTidForDuplicates();
678         it_and_inserted = exported_pids_to_upids_.emplace(exported_pid, upid);
679       }
680       upids_to_exported_pids_.emplace(upid, exported_pid);
681     }
682 
683     const auto& thread_table = storage_->thread_table();
684     for (UniqueTid utid = 0; utid < thread_table.row_count(); utid++) {
685       uint32_t exported_pid = 0;
686       std::optional<UniquePid> upid = thread_table.upid()[utid];
687       if (upid) {
688         auto exported_pid_it = upids_to_exported_pids_.find(*upid);
689         PERFETTO_DCHECK(exported_pid_it != upids_to_exported_pids_.end());
690         exported_pid = exported_pid_it->second;
691       }
692 
693       uint32_t exported_tid = thread_table.tid()[utid];
694       auto it_and_inserted = exported_pids_and_tids_to_utids_.emplace(
695           std::make_pair(exported_pid, exported_tid), utid);
696       if (!it_and_inserted.second) {
697         exported_tid = NextExportedPidOrTidForDuplicates();
698         it_and_inserted = exported_pids_and_tids_to_utids_.emplace(
699             std::make_pair(exported_pid, exported_tid), utid);
700       }
701       utids_to_exported_pids_and_tids_.emplace(
702           utid, std::make_pair(exported_pid, exported_tid));
703     }
704 
705     return util::OkStatus();
706   }
707 
ExportThreadNames()708   util::Status ExportThreadNames() {
709     const auto& thread_table = storage_->thread_table();
710     for (UniqueTid utid = 0; utid < thread_table.row_count(); ++utid) {
711       auto opt_name = thread_table.name()[utid];
712       if (opt_name.has_value()) {
713         const char* thread_name = GetNonNullString(storage_, opt_name);
714         auto pid_and_tid = UtidToPidAndTid(utid);
715         writer_.WriteMetadataEvent("thread_name", "name", thread_name,
716                                    pid_and_tid.first, pid_and_tid.second);
717       }
718     }
719     return util::OkStatus();
720   }
721 
ExportProcessNames()722   util::Status ExportProcessNames() {
723     const auto& process_table = storage_->process_table();
724     for (UniquePid upid = 0; upid < process_table.row_count(); ++upid) {
725       auto opt_name = process_table.name()[upid];
726       if (opt_name.has_value()) {
727         const char* process_name = GetNonNullString(storage_, opt_name);
728         writer_.WriteMetadataEvent("process_name", "name", process_name,
729                                    UpidToPid(upid), /*tid=*/0);
730       }
731     }
732     return util::OkStatus();
733   }
734 
735   // For each process it writes an approximate uptime, based on the process'
736   // start time and the last slice in the entire trace. This same last slice is
737   // used with all processes, so the process could have ended earlier.
ExportProcessUptimes()738   util::Status ExportProcessUptimes() {
739     int64_t last_timestamp_ns = FindLastSliceTimestamp();
740     if (last_timestamp_ns <= 0)
741       return util::OkStatus();
742 
743     const auto& process_table = storage_->process_table();
744     for (UniquePid upid = 0; upid < process_table.row_count(); ++upid) {
745       std::optional<int64_t> start_timestamp_ns =
746           process_table.start_ts()[upid];
747       if (!start_timestamp_ns.has_value())
748         continue;
749 
750       int64_t process_uptime_seconds =
751           (last_timestamp_ns - start_timestamp_ns.value()) /
752           (1000 * 1000 * 1000);
753 
754       writer_.WriteMetadataEvent("process_uptime_seconds", "uptime",
755                                  std::to_string(process_uptime_seconds).c_str(),
756                                  UpidToPid(upid), /*tid=*/0);
757     }
758 
759     return util::OkStatus();
760   }
761 
762   // Returns the last slice's end timestamp for the entire trace. If no slices
763   // are found 0 is returned.
FindLastSliceTimestamp()764   int64_t FindLastSliceTimestamp() {
765     int64_t last_ts = 0;
766     const auto& slices = storage_->slice_table();
767     for (uint32_t i = 0; i < slices.row_count(); ++i) {
768       int64_t duration_ns = slices.dur()[i];
769       int64_t timestamp_ns = slices.ts()[i];
770 
771       if (duration_ns + timestamp_ns > last_ts) {
772         last_ts = duration_ns + timestamp_ns;
773       }
774     }
775     return last_ts;
776   }
777 
ExportSlices()778   util::Status ExportSlices() {
779     const auto& slices = storage_->slice_table();
780     for (auto it = slices.IterateRows(); it; ++it) {
781       // Skip slices with empty category - these are ftrace/system slices that
782       // were also imported into the raw table and will be exported from there
783       // by trace_to_text.
784       // TODO(b/153609716): Add a src column or do_not_export flag instead.
785       if (!it.category())
786         continue;
787       auto cat = storage_->GetString(*it.category());
788       if (cat.c_str() == nullptr || cat == "binder")
789         continue;
790 
791       Json::Value event;
792       event["ts"] = Json::Int64(it.ts() / 1000);
793       event["cat"] = GetNonNullString(storage_, it.category());
794       event["name"] = GetNonNullString(storage_, it.name());
795       event["pid"] = 0;
796       event["tid"] = 0;
797 
798       std::optional<UniqueTid> legacy_utid;
799       std::string legacy_phase;
800 
801       event["args"] = args_builder_.GetArgs(it.arg_set_id());  // Makes a copy.
802       if (event["args"].isMember(kLegacyEventArgsKey)) {
803         const auto& legacy_args = event["args"][kLegacyEventArgsKey];
804 
805         if (legacy_args.isMember(kLegacyEventPassthroughUtidKey)) {
806           legacy_utid = legacy_args[kLegacyEventPassthroughUtidKey].asUInt();
807         }
808         if (legacy_args.isMember(kLegacyEventPhaseKey)) {
809           legacy_phase = legacy_args[kLegacyEventPhaseKey].asString();
810         }
811 
812         event["args"].removeMember(kLegacyEventArgsKey);
813       }
814 
815       // To prevent duplicate export of slices, only export slices on descriptor
816       // or chrome tracks (i.e. TrackEvent slices). Slices on other tracks may
817       // also be present as raw events and handled by trace_to_text. Only add
818       // more track types here if they are not already covered by trace_to_text.
819       TrackId track_id = it.track_id();
820 
821       const auto& track_table = storage_->track_table();
822 
823       auto track_row_ref = *track_table.FindById(track_id);
824       auto track_args_id = track_row_ref.source_arg_set_id();
825       const Json::Value* track_args = nullptr;
826       bool legacy_chrome_track = false;
827       bool is_child_track = false;
828       if (track_args_id) {
829         track_args = &args_builder_.GetArgs(*track_args_id);
830         legacy_chrome_track = (*track_args)["source"].asString() == "chrome";
831         is_child_track = track_args->isMember("is_root_in_scope") &&
832                          !(*track_args)["is_root_in_scope"].asBool();
833       }
834 
835       const auto& thread_track = storage_->thread_track_table();
836       const auto& process_track = storage_->process_track_table();
837       const auto& virtual_track_slices = storage_->virtual_track_slices();
838 
839       int64_t duration_ns = it.dur();
840       std::optional<int64_t> thread_ts_ns;
841       std::optional<int64_t> thread_duration_ns;
842       std::optional<int64_t> thread_instruction_count;
843       std::optional<int64_t> thread_instruction_delta;
844 
845       if (it.thread_dur()) {
846         thread_ts_ns = it.thread_ts();
847         thread_duration_ns = it.thread_dur();
848         thread_instruction_count = it.thread_instruction_count();
849         thread_instruction_delta = it.thread_instruction_delta();
850       } else {
851         SliceId id = it.id();
852         std::optional<uint32_t> vtrack_slice_row =
853             virtual_track_slices.FindRowForSliceId(id);
854         if (vtrack_slice_row) {
855           thread_ts_ns =
856               virtual_track_slices.thread_timestamp_ns()[*vtrack_slice_row];
857           thread_duration_ns =
858               virtual_track_slices.thread_duration_ns()[*vtrack_slice_row];
859           thread_instruction_count =
860               virtual_track_slices
861                   .thread_instruction_counts()[*vtrack_slice_row];
862           thread_instruction_delta =
863               virtual_track_slices
864                   .thread_instruction_deltas()[*vtrack_slice_row];
865         }
866       }
867 
868       auto opt_thread_track_row = thread_track.id().IndexOf(TrackId{track_id});
869 
870       if (opt_thread_track_row && !is_child_track) {
871         // Synchronous (thread) slice or instant event.
872         UniqueTid utid = thread_track.utid()[*opt_thread_track_row];
873         auto pid_and_tid = UtidToPidAndTid(utid);
874         event["pid"] = Json::Int(pid_and_tid.first);
875         event["tid"] = Json::Int(pid_and_tid.second);
876 
877         if (duration_ns == 0) {
878           if (legacy_phase.empty()) {
879             // Use "I" instead of "i" phase for backwards-compat with old
880             // consumers.
881             event["ph"] = "I";
882           } else {
883             event["ph"] = legacy_phase;
884           }
885           if (thread_ts_ns && thread_ts_ns > 0) {
886             event["tts"] = Json::Int64(*thread_ts_ns / 1000);
887           }
888           if (thread_instruction_count && *thread_instruction_count > 0) {
889             event["ticount"] = Json::Int64(*thread_instruction_count);
890           }
891           event["s"] = "t";
892         } else {
893           if (duration_ns > 0) {
894             event["ph"] = "X";
895             event["dur"] = Json::Int64(duration_ns / 1000);
896           } else {
897             // If the slice didn't finish, the duration may be negative. Only
898             // write a begin event without end event in this case.
899             event["ph"] = "B";
900           }
901           if (thread_ts_ns && *thread_ts_ns > 0) {
902             event["tts"] = Json::Int64(*thread_ts_ns / 1000);
903             // Only write thread duration for completed events.
904             if (duration_ns > 0 && thread_duration_ns)
905               event["tdur"] = Json::Int64(*thread_duration_ns / 1000);
906           }
907           if (thread_instruction_count && *thread_instruction_count > 0) {
908             event["ticount"] = Json::Int64(*thread_instruction_count);
909             // Only write thread instruction delta for completed events.
910             if (duration_ns > 0 && thread_instruction_delta)
911               event["tidelta"] = Json::Int64(*thread_instruction_delta);
912           }
913         }
914         writer_.WriteCommonEvent(event);
915       } else if (is_child_track ||
916                  (legacy_chrome_track && track_args->isMember("source_id"))) {
917         // Async event slice.
918         auto opt_process_row = process_track.id().IndexOf(TrackId{track_id});
919         if (legacy_chrome_track) {
920           // Legacy async tracks are always process-associated and have args.
921           PERFETTO_DCHECK(opt_process_row);
922           PERFETTO_DCHECK(track_args);
923           uint32_t upid = process_track.upid()[*opt_process_row];
924           uint32_t exported_pid = UpidToPid(upid);
925           event["pid"] = Json::Int(exported_pid);
926           event["tid"] =
927               Json::Int(legacy_utid ? UtidToPidAndTid(*legacy_utid).second
928                                     : exported_pid);
929 
930           // Preserve original event IDs for legacy tracks. This is so that e.g.
931           // memory dump IDs show up correctly in the JSON trace.
932           PERFETTO_DCHECK(track_args->isMember("source_id"));
933           PERFETTO_DCHECK(track_args->isMember("source_id_is_process_scoped"));
934           PERFETTO_DCHECK(track_args->isMember("source_scope"));
935           uint64_t source_id =
936               static_cast<uint64_t>((*track_args)["source_id"].asInt64());
937           std::string source_scope = (*track_args)["source_scope"].asString();
938           if (!source_scope.empty())
939             event["scope"] = source_scope;
940           bool source_id_is_process_scoped =
941               (*track_args)["source_id_is_process_scoped"].asBool();
942           if (source_id_is_process_scoped) {
943             event["id2"]["local"] = base::Uint64ToHexString(source_id);
944           } else {
945             // Some legacy importers don't understand "id2" fields, so we use
946             // the "usually" global "id" field instead. This works as long as
947             // the event phase is not in {'N', 'D', 'O', '(', ')'}, see
948             // "LOCAL_ID_PHASES" in catapult.
949             event["id"] = base::Uint64ToHexString(source_id);
950           }
951         } else {
952           if (opt_thread_track_row) {
953             UniqueTid utid = thread_track.utid()[*opt_thread_track_row];
954             auto pid_and_tid = UtidToPidAndTid(utid);
955             event["pid"] = Json::Int(pid_and_tid.first);
956             event["tid"] = Json::Int(pid_and_tid.second);
957             event["id2"]["local"] = base::Uint64ToHexString(track_id.value);
958           } else if (opt_process_row) {
959             uint32_t upid = process_track.upid()[*opt_process_row];
960             uint32_t exported_pid = UpidToPid(upid);
961             event["pid"] = Json::Int(exported_pid);
962             event["tid"] =
963                 Json::Int(legacy_utid ? UtidToPidAndTid(*legacy_utid).second
964                                       : exported_pid);
965             event["id2"]["local"] = base::Uint64ToHexString(track_id.value);
966           } else {
967             if (legacy_utid) {
968               auto pid_and_tid = UtidToPidAndTid(*legacy_utid);
969               event["pid"] = Json::Int(pid_and_tid.first);
970               event["tid"] = Json::Int(pid_and_tid.second);
971             }
972 
973             // Some legacy importers don't understand "id2" fields, so we use
974             // the "usually" global "id" field instead. This works as long as
975             // the event phase is not in {'N', 'D', 'O', '(', ')'}, see
976             // "LOCAL_ID_PHASES" in catapult.
977             event["id"] = base::Uint64ToHexString(track_id.value);
978           }
979         }
980 
981         if (thread_ts_ns && *thread_ts_ns > 0) {
982           event["tts"] = Json::Int64(*thread_ts_ns / 1000);
983           event["use_async_tts"] = Json::Int(1);
984         }
985         if (thread_instruction_count && *thread_instruction_count > 0) {
986           event["ticount"] = Json::Int64(*thread_instruction_count);
987           event["use_async_tts"] = Json::Int(1);
988         }
989 
990         if (duration_ns == 0) {
991           if (legacy_phase.empty()) {
992             // Instant async event.
993             event["ph"] = "n";
994             writer_.AddAsyncInstantEvent(event);
995           } else {
996             // Async step events.
997             event["ph"] = legacy_phase;
998             writer_.AddAsyncBeginEvent(event);
999           }
1000         } else {  // Async start and end.
1001           event["ph"] = legacy_phase.empty() ? "b" : legacy_phase;
1002           writer_.AddAsyncBeginEvent(event);
1003           // If the slice didn't finish, the duration may be negative. Don't
1004           // write the end event in this case.
1005           if (duration_ns > 0) {
1006             event["ph"] = legacy_phase.empty() ? "e" : "F";
1007             event["ts"] = Json::Int64((it.ts() + duration_ns) / 1000);
1008             if (thread_ts_ns && thread_duration_ns && *thread_ts_ns > 0) {
1009               event["tts"] =
1010                   Json::Int64((*thread_ts_ns + *thread_duration_ns) / 1000);
1011             }
1012             if (thread_instruction_count && thread_instruction_delta &&
1013                 *thread_instruction_count > 0) {
1014               event["ticount"] = Json::Int64(
1015                   (*thread_instruction_count + *thread_instruction_delta));
1016             }
1017             event["args"].clear();
1018             writer_.AddAsyncEndEvent(event);
1019           }
1020         }
1021       } else {
1022         // Global or process-scoped instant event.
1023         PERFETTO_DCHECK(legacy_chrome_track || !is_child_track);
1024         if (duration_ns != 0) {
1025           // We don't support exporting slices on the default global or process
1026           // track to JSON (JSON only supports instant events on these tracks).
1027           PERFETTO_DLOG(
1028               "skipping non-instant slice on global or process track");
1029         } else {
1030           if (legacy_phase.empty()) {
1031             // Use "I" instead of "i" phase for backwards-compat with old
1032             // consumers.
1033             event["ph"] = "I";
1034           } else {
1035             event["ph"] = legacy_phase;
1036           }
1037 
1038           auto opt_process_row = process_track.id().IndexOf(TrackId{track_id});
1039           if (opt_process_row.has_value()) {
1040             uint32_t upid = process_track.upid()[*opt_process_row];
1041             uint32_t exported_pid = UpidToPid(upid);
1042             event["pid"] = Json::Int(exported_pid);
1043             event["tid"] =
1044                 Json::Int(legacy_utid ? UtidToPidAndTid(*legacy_utid).second
1045                                       : exported_pid);
1046             event["s"] = "p";
1047           } else {
1048             event["s"] = "g";
1049           }
1050           writer_.WriteCommonEvent(event);
1051         }
1052       }
1053     }
1054     return util::OkStatus();
1055   }
1056 
CreateFlowEventV1(uint32_t flow_id,SliceId slice_id,std::string name,std::string cat,Json::Value args,bool flow_begin)1057   std::optional<Json::Value> CreateFlowEventV1(uint32_t flow_id,
1058                                                SliceId slice_id,
1059                                                std::string name,
1060                                                std::string cat,
1061                                                Json::Value args,
1062                                                bool flow_begin) {
1063     const auto& slices = storage_->slice_table();
1064     const auto& thread_tracks = storage_->thread_track_table();
1065 
1066     auto opt_slice_idx = slices.id().IndexOf(slice_id);
1067     if (!opt_slice_idx)
1068       return std::nullopt;
1069     uint32_t slice_idx = opt_slice_idx.value();
1070 
1071     TrackId track_id = storage_->slice_table().track_id()[slice_idx];
1072     auto opt_thread_track_idx = thread_tracks.id().IndexOf(track_id);
1073     // catapult only supports flow events attached to thread-track slices
1074     if (!opt_thread_track_idx)
1075       return std::nullopt;
1076 
1077     UniqueTid utid = thread_tracks.utid()[opt_thread_track_idx.value()];
1078     auto pid_and_tid = UtidToPidAndTid(utid);
1079     Json::Value event;
1080     event["id"] = flow_id;
1081     event["pid"] = Json::Int(pid_and_tid.first);
1082     event["tid"] = Json::Int(pid_and_tid.second);
1083     event["cat"] = cat;
1084     event["name"] = name;
1085     event["ph"] = (flow_begin ? "s" : "f");
1086     event["ts"] = Json::Int64(slices.ts()[slice_idx] / 1000);
1087     if (!flow_begin) {
1088       event["bp"] = "e";
1089     }
1090     event["args"] = std::move(args);
1091     return std::move(event);
1092   }
1093 
ExportFlows()1094   util::Status ExportFlows() {
1095     const auto& flow_table = storage_->flow_table();
1096     const auto& slice_table = storage_->slice_table();
1097     for (uint32_t i = 0; i < flow_table.row_count(); i++) {
1098       SliceId slice_out = flow_table.slice_out()[i];
1099       SliceId slice_in = flow_table.slice_in()[i];
1100       uint32_t arg_set_id = flow_table.arg_set_id()[i];
1101 
1102       std::string cat;
1103       std::string name;
1104       auto args = args_builder_.GetArgs(arg_set_id);
1105       if (arg_set_id != kInvalidArgSetId) {
1106         cat = args["cat"].asString();
1107         name = args["name"].asString();
1108         // Don't export these args since they are only used for this export and
1109         // weren't part of the original event.
1110         args.removeMember("name");
1111         args.removeMember("cat");
1112       } else {
1113         auto opt_slice_out_idx = slice_table.id().IndexOf(slice_out);
1114         PERFETTO_DCHECK(opt_slice_out_idx.has_value());
1115         std::optional<StringId> cat_id =
1116             slice_table.category()[opt_slice_out_idx.value()];
1117         std::optional<StringId> name_id =
1118             slice_table.name()[opt_slice_out_idx.value()];
1119         cat = GetNonNullString(storage_, cat_id);
1120         name = GetNonNullString(storage_, name_id);
1121       }
1122 
1123       auto out_event = CreateFlowEventV1(i, slice_out, name, cat, args,
1124                                          /* flow_begin = */ true);
1125       auto in_event = CreateFlowEventV1(i, slice_in, name, cat, std::move(args),
1126                                         /* flow_begin = */ false);
1127 
1128       if (out_event && in_event) {
1129         writer_.WriteCommonEvent(out_event.value());
1130         writer_.WriteCommonEvent(in_event.value());
1131       }
1132     }
1133     return util::OkStatus();
1134   }
1135 
ConvertLegacyRawEventToJson(uint32_t index)1136   Json::Value ConvertLegacyRawEventToJson(uint32_t index) {
1137     const auto& events = storage_->raw_table();
1138 
1139     Json::Value event;
1140     event["ts"] = Json::Int64(events.ts()[index] / 1000);
1141 
1142     UniqueTid utid = static_cast<UniqueTid>(events.utid()[index]);
1143     auto pid_and_tid = UtidToPidAndTid(utid);
1144     event["pid"] = Json::Int(pid_and_tid.first);
1145     event["tid"] = Json::Int(pid_and_tid.second);
1146 
1147     // Raw legacy events store all other params in the arg set. Make a copy of
1148     // the converted args here, parse, and then remove the legacy params.
1149     event["args"] = args_builder_.GetArgs(events.arg_set_id()[index]);
1150     const Json::Value& legacy_args = event["args"][kLegacyEventArgsKey];
1151 
1152     PERFETTO_DCHECK(legacy_args.isMember(kLegacyEventCategoryKey));
1153     event["cat"] = legacy_args[kLegacyEventCategoryKey];
1154 
1155     PERFETTO_DCHECK(legacy_args.isMember(kLegacyEventNameKey));
1156     event["name"] = legacy_args[kLegacyEventNameKey];
1157 
1158     PERFETTO_DCHECK(legacy_args.isMember(kLegacyEventPhaseKey));
1159     event["ph"] = legacy_args[kLegacyEventPhaseKey];
1160 
1161     // Object snapshot events are supposed to have a mandatory "snapshot" arg,
1162     // which may be removed in trace processor if it is empty.
1163     if (legacy_args[kLegacyEventPhaseKey] == "O" &&
1164         !event["args"].isMember("snapshot")) {
1165       event["args"]["snapshot"] = Json::Value(Json::objectValue);
1166     }
1167 
1168     if (legacy_args.isMember(kLegacyEventDurationNsKey))
1169       event["dur"] = legacy_args[kLegacyEventDurationNsKey].asInt64() / 1000;
1170 
1171     if (legacy_args.isMember(kLegacyEventThreadTimestampNsKey)) {
1172       event["tts"] =
1173           legacy_args[kLegacyEventThreadTimestampNsKey].asInt64() / 1000;
1174     }
1175 
1176     if (legacy_args.isMember(kLegacyEventThreadDurationNsKey)) {
1177       event["tdur"] =
1178           legacy_args[kLegacyEventThreadDurationNsKey].asInt64() / 1000;
1179     }
1180 
1181     if (legacy_args.isMember(kLegacyEventThreadInstructionCountKey))
1182       event["ticount"] = legacy_args[kLegacyEventThreadInstructionCountKey];
1183 
1184     if (legacy_args.isMember(kLegacyEventThreadInstructionDeltaKey))
1185       event["tidelta"] = legacy_args[kLegacyEventThreadInstructionDeltaKey];
1186 
1187     if (legacy_args.isMember(kLegacyEventUseAsyncTtsKey))
1188       event["use_async_tts"] = legacy_args[kLegacyEventUseAsyncTtsKey];
1189 
1190     if (legacy_args.isMember(kLegacyEventUnscopedIdKey)) {
1191       event["id"] = base::Uint64ToHexString(
1192           legacy_args[kLegacyEventUnscopedIdKey].asUInt64());
1193     }
1194 
1195     if (legacy_args.isMember(kLegacyEventGlobalIdKey)) {
1196       event["id2"]["global"] = base::Uint64ToHexString(
1197           legacy_args[kLegacyEventGlobalIdKey].asUInt64());
1198     }
1199 
1200     if (legacy_args.isMember(kLegacyEventLocalIdKey)) {
1201       event["id2"]["local"] = base::Uint64ToHexString(
1202           legacy_args[kLegacyEventLocalIdKey].asUInt64());
1203     }
1204 
1205     if (legacy_args.isMember(kLegacyEventIdScopeKey))
1206       event["scope"] = legacy_args[kLegacyEventIdScopeKey];
1207 
1208     event["args"].removeMember(kLegacyEventArgsKey);
1209 
1210     return event;
1211   }
1212 
ExportRawEvents()1213   util::Status ExportRawEvents() {
1214     std::optional<StringId> raw_legacy_event_key_id =
1215         storage_->string_pool().GetId("track_event.legacy_event");
1216     std::optional<StringId> raw_legacy_system_trace_event_id =
1217         storage_->string_pool().GetId("chrome_event.legacy_system_trace");
1218     std::optional<StringId> raw_legacy_user_trace_event_id =
1219         storage_->string_pool().GetId("chrome_event.legacy_user_trace");
1220     std::optional<StringId> raw_chrome_metadata_event_id =
1221         storage_->string_pool().GetId("chrome_event.metadata");
1222 
1223     const auto& events = storage_->raw_table();
1224     for (uint32_t i = 0; i < events.row_count(); ++i) {
1225       if (raw_legacy_event_key_id &&
1226           events.name()[i] == *raw_legacy_event_key_id) {
1227         Json::Value event = ConvertLegacyRawEventToJson(i);
1228         writer_.WriteCommonEvent(event);
1229       } else if (raw_legacy_system_trace_event_id &&
1230                  events.name()[i] == *raw_legacy_system_trace_event_id) {
1231         Json::Value args = args_builder_.GetArgs(events.arg_set_id()[i]);
1232         PERFETTO_DCHECK(args.isMember("data"));
1233         writer_.AddSystemTraceData(args["data"].asString());
1234       } else if (raw_legacy_user_trace_event_id &&
1235                  events.name()[i] == *raw_legacy_user_trace_event_id) {
1236         Json::Value args = args_builder_.GetArgs(events.arg_set_id()[i]);
1237         PERFETTO_DCHECK(args.isMember("data"));
1238         writer_.AddUserTraceData(args["data"].asString());
1239       } else if (raw_chrome_metadata_event_id &&
1240                  events.name()[i] == *raw_chrome_metadata_event_id) {
1241         Json::Value args = args_builder_.GetArgs(events.arg_set_id()[i]);
1242         writer_.MergeMetadata(args);
1243       }
1244     }
1245     return util::OkStatus();
1246   }
1247 
1248   class MergedProfileSamplesEmitter {
1249    public:
1250     // The TraceFormatWriter must outlive this instance.
MergedProfileSamplesEmitter(TraceFormatWriter & writer)1251     MergedProfileSamplesEmitter(TraceFormatWriter& writer) : writer_(writer) {}
1252 
AddEventForUtid(UniqueTid utid,int64_t ts,CallsiteId callsite_id,const Json::Value & event)1253     uint64_t AddEventForUtid(UniqueTid utid,
1254                              int64_t ts,
1255                              CallsiteId callsite_id,
1256                              const Json::Value& event) {
1257       auto current_sample = current_events_.find(utid);
1258 
1259       // If there's a current entry for our thread and it matches the callsite
1260       // of the new sample, update the entry with the new timestamp. Otherwise
1261       // create a new entry.
1262       if (current_sample != current_events_.end() &&
1263           current_sample->second.callsite_id() == callsite_id) {
1264         current_sample->second.UpdateWithNewSample(ts);
1265         return current_sample->second.event_id();
1266       } else {
1267         if (current_sample != current_events_.end())
1268           current_events_.erase(current_sample);
1269 
1270         auto new_entry = current_events_.emplace(
1271             std::piecewise_construct, std::forward_as_tuple(utid),
1272             std::forward_as_tuple(writer_, callsite_id, ts, event));
1273         return new_entry.first->second.event_id();
1274       }
1275     }
1276 
GenerateNewEventId()1277     static uint64_t GenerateNewEventId() {
1278       // "n"-phase events are nestable async events which get tied together
1279       // with their id, so we need to give each one a unique ID as we only
1280       // want the samples to show up on their own track in the trace-viewer
1281       // but not nested together (unless they're nested under a merged event).
1282       static size_t g_id_counter = 0;
1283       return ++g_id_counter;
1284     }
1285 
1286    private:
1287     class Sample {
1288      public:
Sample(TraceFormatWriter & writer,CallsiteId callsite_id,int64_t ts,const Json::Value & event)1289       Sample(TraceFormatWriter& writer,
1290              CallsiteId callsite_id,
1291              int64_t ts,
1292              const Json::Value& event)
1293           : writer_(writer),
1294             callsite_id_(callsite_id),
1295             begin_ts_(ts),
1296             end_ts_(ts),
1297             event_(event),
1298             event_id_(MergedProfileSamplesEmitter::GenerateNewEventId()),
1299             sample_count_(1) {}
1300 
~Sample()1301       ~Sample() {
1302         // No point writing a merged event if we only got a single sample
1303         // as ExportCpuProfileSamples will already be writing the instant event.
1304         if (sample_count_ == 1)
1305           return;
1306 
1307         event_["id"] = base::Uint64ToHexString(event_id_);
1308 
1309         // Write the BEGIN event.
1310         event_["ph"] = "b";
1311         // We subtract 1us as a workaround for the first async event not
1312         // nesting underneath the parent event if the timestamp is identical.
1313         int64_t begin_in_us_ = begin_ts_ / 1000;
1314         event_["ts"] = Json::Int64(std::min(begin_in_us_ - 1, begin_in_us_));
1315         writer_.WriteCommonEvent(event_);
1316 
1317         // Write the END event.
1318         event_["ph"] = "e";
1319         event_["ts"] = Json::Int64(end_ts_ / 1000);
1320         // No need for args for the end event; remove them to save some space.
1321         event_["args"].clear();
1322         writer_.WriteCommonEvent(event_);
1323       }
1324 
UpdateWithNewSample(int64_t ts)1325       void UpdateWithNewSample(int64_t ts) {
1326         // We assume samples for a given thread will appear in timestamp
1327         // order; if this assumption stops holding true, we'll have to sort the
1328         // samples first.
1329         if (ts < end_ts_ || begin_ts_ > ts) {
1330           PERFETTO_ELOG(
1331               "Got an timestamp out of sequence while merging stack samples "
1332               "during JSON export!\n");
1333           PERFETTO_DCHECK(false);
1334         }
1335 
1336         end_ts_ = ts;
1337         sample_count_++;
1338       }
1339 
event_id() const1340       uint64_t event_id() const { return event_id_; }
callsite_id() const1341       CallsiteId callsite_id() const { return callsite_id_; }
1342 
1343      public:
1344       Sample(const Sample&) = delete;
1345       Sample& operator=(const Sample&) = delete;
1346       Sample& operator=(Sample&& value) = delete;
1347 
1348       TraceFormatWriter& writer_;
1349       CallsiteId callsite_id_;
1350       int64_t begin_ts_;
1351       int64_t end_ts_;
1352       Json::Value event_;
1353       uint64_t event_id_;
1354       size_t sample_count_;
1355     };
1356 
1357     MergedProfileSamplesEmitter(const MergedProfileSamplesEmitter&) = delete;
1358     MergedProfileSamplesEmitter& operator=(const MergedProfileSamplesEmitter&) =
1359         delete;
1360     MergedProfileSamplesEmitter& operator=(
1361         MergedProfileSamplesEmitter&& value) = delete;
1362 
1363     std::unordered_map<UniqueTid, Sample> current_events_;
1364     TraceFormatWriter& writer_;
1365   };
1366 
ExportCpuProfileSamples()1367   util::Status ExportCpuProfileSamples() {
1368     MergedProfileSamplesEmitter merged_sample_emitter(writer_);
1369 
1370     const tables::CpuProfileStackSampleTable& samples =
1371         storage_->cpu_profile_stack_sample_table();
1372     for (uint32_t i = 0; i < samples.row_count(); ++i) {
1373       Json::Value event;
1374       event["ts"] = Json::Int64(samples.ts()[i] / 1000);
1375 
1376       UniqueTid utid = static_cast<UniqueTid>(samples.utid()[i]);
1377       auto pid_and_tid = UtidToPidAndTid(utid);
1378       event["pid"] = Json::Int(pid_and_tid.first);
1379       event["tid"] = Json::Int(pid_and_tid.second);
1380 
1381       event["ph"] = "n";
1382       event["cat"] = "disabled-by-default-cpu_profiler";
1383       event["name"] = "StackCpuSampling";
1384       event["s"] = "t";
1385 
1386       // Add a dummy thread timestamp to this event to match the format of
1387       // instant events. Useful in the UI to view args of a selected group of
1388       // samples.
1389       event["tts"] = Json::Int64(1);
1390 
1391       const auto& callsites = storage_->stack_profile_callsite_table();
1392       const auto& frames = storage_->stack_profile_frame_table();
1393       const auto& mappings = storage_->stack_profile_mapping_table();
1394 
1395       std::vector<std::string> callstack;
1396       std::optional<CallsiteId> opt_callsite_id = samples.callsite_id()[i];
1397 
1398       while (opt_callsite_id) {
1399         CallsiteId callsite_id = *opt_callsite_id;
1400         uint32_t callsite_row = *callsites.id().IndexOf(callsite_id);
1401 
1402         FrameId frame_id = callsites.frame_id()[callsite_row];
1403         uint32_t frame_row = *frames.id().IndexOf(frame_id);
1404 
1405         MappingId mapping_id = frames.mapping()[frame_row];
1406         uint32_t mapping_row = *mappings.id().IndexOf(mapping_id);
1407 
1408         NullTermStringView symbol_name;
1409         auto opt_symbol_set_id = frames.symbol_set_id()[frame_row];
1410         if (opt_symbol_set_id) {
1411           symbol_name = storage_->GetString(
1412               storage_->symbol_table().name()[*opt_symbol_set_id]);
1413         }
1414 
1415         base::StackString<1024> frame_entry(
1416             "%s - %s [%s]\n",
1417             (symbol_name.empty()
1418                  ? base::Uint64ToHexString(
1419                        static_cast<uint64_t>(frames.rel_pc()[frame_row]))
1420                        .c_str()
1421                  : symbol_name.c_str()),
1422             GetNonNullString(storage_, mappings.name()[mapping_row]),
1423             GetNonNullString(storage_, mappings.build_id()[mapping_row]));
1424 
1425         callstack.emplace_back(frame_entry.ToStdString());
1426 
1427         opt_callsite_id = callsites.parent_id()[callsite_row];
1428       }
1429 
1430       std::string merged_callstack;
1431       for (auto entry = callstack.rbegin(); entry != callstack.rend();
1432            ++entry) {
1433         merged_callstack += *entry;
1434       }
1435 
1436       event["args"]["frames"] = merged_callstack;
1437       event["args"]["process_priority"] = samples.process_priority()[i];
1438 
1439       // TODO(oysteine): Used for backwards compatibility with the memlog
1440       // pipeline, should remove once we've switched to looking directly at the
1441       // tid.
1442       event["args"]["thread_id"] = Json::Int(pid_and_tid.second);
1443 
1444       // Emit duration events for adjacent samples with the same callsite.
1445       // For now, only do this when the trace has already been symbolized i.e.
1446       // are not directly output by Chrome, to avoid interfering with other
1447       // processing pipelines.
1448       std::optional<CallsiteId> opt_current_callsite_id =
1449           samples.callsite_id()[i];
1450 
1451       if (opt_current_callsite_id && storage_->symbol_table().row_count() > 0) {
1452         uint64_t parent_event_id = merged_sample_emitter.AddEventForUtid(
1453             utid, samples.ts()[i], *opt_current_callsite_id, event);
1454         event["id"] = base::Uint64ToHexString(parent_event_id);
1455       } else {
1456         event["id"] = base::Uint64ToHexString(
1457             MergedProfileSamplesEmitter::GenerateNewEventId());
1458       }
1459 
1460       writer_.WriteCommonEvent(event);
1461     }
1462 
1463     return util::OkStatus();
1464   }
1465 
ExportMetadata()1466   util::Status ExportMetadata() {
1467     const auto& trace_metadata = storage_->metadata_table();
1468     const auto& keys = trace_metadata.name();
1469     const auto& int_values = trace_metadata.int_value();
1470     const auto& str_values = trace_metadata.str_value();
1471 
1472     // Create a mapping from key string ids to keys.
1473     std::unordered_map<StringId, metadata::KeyId> key_map;
1474     for (uint32_t i = 0; i < metadata::kNumKeys; ++i) {
1475       auto id = *storage_->string_pool().GetId(metadata::kNames[i]);
1476       key_map[id] = static_cast<metadata::KeyId>(i);
1477     }
1478 
1479     for (uint32_t pos = 0; pos < trace_metadata.row_count(); pos++) {
1480       auto key_it = key_map.find(keys[pos]);
1481       // Skip exporting dynamic entries; the cr-xxx entries that come from
1482       // the ChromeMetadata proto message are already exported from the raw
1483       // table.
1484       if (key_it == key_map.end())
1485         continue;
1486 
1487       // Cast away from enum type, as otherwise -Wswitch-enum will demand an
1488       // exhaustive list of cases, even if there's a default case.
1489       metadata::KeyId key = key_it->second;
1490       switch (static_cast<size_t>(key)) {
1491         case metadata::benchmark_description:
1492           writer_.AppendTelemetryMetadataString(
1493               "benchmarkDescriptions", str_values.GetString(pos).c_str());
1494           break;
1495 
1496         case metadata::benchmark_name:
1497           writer_.AppendTelemetryMetadataString(
1498               "benchmarks", str_values.GetString(pos).c_str());
1499           break;
1500 
1501         case metadata::benchmark_start_time_us:
1502           writer_.SetTelemetryMetadataTimestamp("benchmarkStart",
1503                                                 *int_values[pos]);
1504           break;
1505 
1506         case metadata::benchmark_had_failures:
1507           writer_.AppendTelemetryMetadataBool("hadFailures", *int_values[pos]);
1508           break;
1509 
1510         case metadata::benchmark_label:
1511           writer_.AppendTelemetryMetadataString(
1512               "labels", str_values.GetString(pos).c_str());
1513           break;
1514 
1515         case metadata::benchmark_story_name:
1516           writer_.AppendTelemetryMetadataString(
1517               "stories", str_values.GetString(pos).c_str());
1518           break;
1519 
1520         case metadata::benchmark_story_run_index:
1521           writer_.AppendTelemetryMetadataInt("storysetRepeats",
1522                                              *int_values[pos]);
1523           break;
1524 
1525         case metadata::benchmark_story_run_time_us:
1526           writer_.SetTelemetryMetadataTimestamp("traceStart", *int_values[pos]);
1527           break;
1528 
1529         case metadata::benchmark_story_tags:  // repeated
1530           writer_.AppendTelemetryMetadataString(
1531               "storyTags", str_values.GetString(pos).c_str());
1532           break;
1533 
1534         default:
1535           PERFETTO_DLOG("Ignoring metadata key %zu", static_cast<size_t>(key));
1536           break;
1537       }
1538     }
1539     return util::OkStatus();
1540   }
1541 
ExportStats()1542   util::Status ExportStats() {
1543     const auto& stats = storage_->stats();
1544 
1545     for (size_t idx = 0; idx < stats::kNumKeys; idx++) {
1546       if (stats::kTypes[idx] == stats::kSingle) {
1547         writer_.SetStats(stats::kNames[idx], stats[idx].value);
1548       } else {
1549         PERFETTO_DCHECK(stats::kTypes[idx] == stats::kIndexed);
1550         writer_.SetStats(stats::kNames[idx], stats[idx].indexed_values);
1551       }
1552     }
1553 
1554     return util::OkStatus();
1555   }
1556 
ExportMemorySnapshots()1557   util::Status ExportMemorySnapshots() {
1558     const auto& memory_snapshots = storage_->memory_snapshot_table();
1559     std::optional<StringId> private_footprint_id =
1560         storage_->string_pool().GetId("chrome.private_footprint_kb");
1561     std::optional<StringId> peak_resident_set_id =
1562         storage_->string_pool().GetId("chrome.peak_resident_set_kb");
1563 
1564     for (uint32_t memory_index = 0; memory_index < memory_snapshots.row_count();
1565          ++memory_index) {
1566       Json::Value event_base;
1567 
1568       event_base["ph"] = "v";
1569       event_base["cat"] = "disabled-by-default-memory-infra";
1570       auto snapshot_id = memory_snapshots.id()[memory_index].value;
1571       event_base["id"] = base::Uint64ToHexString(snapshot_id);
1572       int64_t snapshot_ts = memory_snapshots.timestamp()[memory_index];
1573       event_base["ts"] = Json::Int64(snapshot_ts / 1000);
1574       // TODO(crbug:1116359): Add dump type to the snapshot proto
1575       // to properly fill event_base["name"]
1576       event_base["name"] = "periodic_interval";
1577       event_base["args"]["dumps"]["level_of_detail"] = GetNonNullString(
1578           storage_, memory_snapshots.detail_level()[memory_index]);
1579 
1580       // Export OS dump events for processes with relevant data.
1581       const auto& process_table = storage_->process_table();
1582       for (UniquePid upid = 0; upid < process_table.row_count(); ++upid) {
1583         Json::Value event =
1584             FillInProcessEventDetails(event_base, process_table.pid()[upid]);
1585         Json::Value& totals = event["args"]["dumps"]["process_totals"];
1586 
1587         const auto& process_counters = storage_->process_counter_track_table();
1588 
1589         for (uint32_t counter_index = 0;
1590              counter_index < process_counters.row_count(); ++counter_index) {
1591           if (process_counters.upid()[counter_index] != upid)
1592             continue;
1593           TrackId track_id = process_counters.id()[counter_index];
1594           if (private_footprint_id && (process_counters.name()[counter_index] ==
1595                                        private_footprint_id)) {
1596             totals["private_footprint_bytes"] = base::Uint64ToHexStringNoPrefix(
1597                 GetCounterValue(track_id, snapshot_ts));
1598           } else if (peak_resident_set_id &&
1599                      (process_counters.name()[counter_index] ==
1600                       peak_resident_set_id)) {
1601             totals["peak_resident_set_size"] = base::Uint64ToHexStringNoPrefix(
1602                 GetCounterValue(track_id, snapshot_ts));
1603           }
1604         }
1605 
1606         auto process_args_id = process_table.arg_set_id()[upid];
1607         if (process_args_id) {
1608           const Json::Value* process_args =
1609               &args_builder_.GetArgs(process_args_id);
1610           if (process_args->isMember("is_peak_rss_resettable")) {
1611             totals["is_peak_rss_resettable"] =
1612                 (*process_args)["is_peak_rss_resettable"];
1613           }
1614         }
1615 
1616         const auto& smaps_table = storage_->profiler_smaps_table();
1617         // Do not create vm_regions without memory maps, since catapult expects
1618         // to have rows.
1619         Json::Value* smaps =
1620             smaps_table.row_count() > 0
1621                 ? &event["args"]["dumps"]["process_mmaps"]["vm_regions"]
1622                 : nullptr;
1623         for (uint32_t smaps_index = 0; smaps_index < smaps_table.row_count();
1624              ++smaps_index) {
1625           if (smaps_table.upid()[smaps_index] != upid)
1626             continue;
1627           if (smaps_table.ts()[smaps_index] != snapshot_ts)
1628             continue;
1629           Json::Value region;
1630           region["mf"] =
1631               GetNonNullString(storage_, smaps_table.file_name()[smaps_index]);
1632           region["pf"] =
1633               Json::Int64(smaps_table.protection_flags()[smaps_index]);
1634           region["sa"] = base::Uint64ToHexStringNoPrefix(
1635               static_cast<uint64_t>(smaps_table.start_address()[smaps_index]));
1636           region["sz"] = base::Uint64ToHexStringNoPrefix(
1637               static_cast<uint64_t>(smaps_table.size_kb()[smaps_index]) * 1024);
1638           region["ts"] =
1639               Json::Int64(smaps_table.module_timestamp()[smaps_index]);
1640           region["id"] = GetNonNullString(
1641               storage_, smaps_table.module_debugid()[smaps_index]);
1642           region["df"] = GetNonNullString(
1643               storage_, smaps_table.module_debug_path()[smaps_index]);
1644           region["bs"]["pc"] = base::Uint64ToHexStringNoPrefix(
1645               static_cast<uint64_t>(
1646                   smaps_table.private_clean_resident_kb()[smaps_index]) *
1647               1024);
1648           region["bs"]["pd"] = base::Uint64ToHexStringNoPrefix(
1649               static_cast<uint64_t>(
1650                   smaps_table.private_dirty_kb()[smaps_index]) *
1651               1024);
1652           region["bs"]["pss"] = base::Uint64ToHexStringNoPrefix(
1653               static_cast<uint64_t>(
1654                   smaps_table.proportional_resident_kb()[smaps_index]) *
1655               1024);
1656           region["bs"]["sc"] = base::Uint64ToHexStringNoPrefix(
1657               static_cast<uint64_t>(
1658                   smaps_table.shared_clean_resident_kb()[smaps_index]) *
1659               1024);
1660           region["bs"]["sd"] = base::Uint64ToHexStringNoPrefix(
1661               static_cast<uint64_t>(
1662                   smaps_table.shared_dirty_resident_kb()[smaps_index]) *
1663               1024);
1664           region["bs"]["sw"] = base::Uint64ToHexStringNoPrefix(
1665               static_cast<uint64_t>(smaps_table.swap_kb()[smaps_index]) * 1024);
1666           smaps->append(region);
1667         }
1668 
1669         if (!totals.empty() || (smaps && !smaps->empty()))
1670           writer_.WriteCommonEvent(event);
1671       }
1672 
1673       // Export chrome dump events for process snapshots in current memory
1674       // snapshot.
1675       const auto& process_snapshots = storage_->process_memory_snapshot_table();
1676 
1677       for (uint32_t process_index = 0;
1678            process_index < process_snapshots.row_count(); ++process_index) {
1679         if (process_snapshots.snapshot_id()[process_index].value != snapshot_id)
1680           continue;
1681 
1682         auto process_snapshot_id = process_snapshots.id()[process_index].value;
1683         uint32_t pid = UpidToPid(process_snapshots.upid()[process_index]);
1684 
1685         // Shared memory nodes are imported into a fake process with pid 0.
1686         // Catapult expects them to be associated with one of the real processes
1687         // of the snapshot, so we choose the first one we can find and replace
1688         // the pid.
1689         if (pid == 0) {
1690           for (uint32_t i = 0; i < process_snapshots.row_count(); ++i) {
1691             if (process_snapshots.snapshot_id()[i].value != snapshot_id)
1692               continue;
1693             uint32_t new_pid = UpidToPid(process_snapshots.upid()[i]);
1694             if (new_pid != 0) {
1695               pid = new_pid;
1696               break;
1697             }
1698           }
1699         }
1700 
1701         Json::Value event = FillInProcessEventDetails(event_base, pid);
1702 
1703         const auto& snapshot_nodes = storage_->memory_snapshot_node_table();
1704 
1705         for (uint32_t node_index = 0; node_index < snapshot_nodes.row_count();
1706              ++node_index) {
1707           if (snapshot_nodes.process_snapshot_id()[node_index].value !=
1708               process_snapshot_id) {
1709             continue;
1710           }
1711           const char* path =
1712               GetNonNullString(storage_, snapshot_nodes.path()[node_index]);
1713           event["args"]["dumps"]["allocators"][path]["guid"] =
1714               base::Uint64ToHexStringNoPrefix(
1715                   static_cast<uint64_t>(snapshot_nodes.id()[node_index].value));
1716           if (snapshot_nodes.size()[node_index]) {
1717             AddAttributeToMemoryNode(&event, path, "size",
1718                                      snapshot_nodes.size()[node_index],
1719                                      "bytes");
1720           }
1721           if (snapshot_nodes.effective_size()[node_index]) {
1722             AddAttributeToMemoryNode(
1723                 &event, path, "effective_size",
1724                 snapshot_nodes.effective_size()[node_index], "bytes");
1725           }
1726 
1727           auto node_args_id = snapshot_nodes.arg_set_id()[node_index];
1728           if (!node_args_id)
1729             continue;
1730           const Json::Value* node_args =
1731               &args_builder_.GetArgs(node_args_id.value());
1732           for (const auto& arg_name : node_args->getMemberNames()) {
1733             const Json::Value& arg_value = (*node_args)[arg_name]["value"];
1734             if (arg_value.empty())
1735               continue;
1736             if (arg_value.isString()) {
1737               AddAttributeToMemoryNode(&event, path, arg_name,
1738                                        arg_value.asString());
1739             } else if (arg_value.isInt64()) {
1740               Json::Value unit = (*node_args)[arg_name]["unit"];
1741               if (unit.empty())
1742                 unit = "unknown";
1743               AddAttributeToMemoryNode(&event, path, arg_name,
1744                                        arg_value.asInt64(), unit.asString());
1745             }
1746           }
1747         }
1748 
1749         const auto& snapshot_edges = storage_->memory_snapshot_edge_table();
1750 
1751         for (uint32_t edge_index = 0; edge_index < snapshot_edges.row_count();
1752              ++edge_index) {
1753           SnapshotNodeId source_node_id =
1754               snapshot_edges.source_node_id()[edge_index];
1755           uint32_t source_node_row =
1756               *snapshot_nodes.id().IndexOf(source_node_id);
1757 
1758           if (snapshot_nodes.process_snapshot_id()[source_node_row].value !=
1759               process_snapshot_id) {
1760             continue;
1761           }
1762           Json::Value edge;
1763           edge["source"] = base::Uint64ToHexStringNoPrefix(
1764               snapshot_edges.source_node_id()[edge_index].value);
1765           edge["target"] = base::Uint64ToHexStringNoPrefix(
1766               snapshot_edges.target_node_id()[edge_index].value);
1767           edge["importance"] =
1768               Json::Int(snapshot_edges.importance()[edge_index]);
1769           edge["type"] = "ownership";
1770           event["args"]["dumps"]["allocators_graph"].append(edge);
1771         }
1772         writer_.WriteCommonEvent(event);
1773       }
1774     }
1775     return util::OkStatus();
1776   }
1777 
UpidToPid(UniquePid upid)1778   uint32_t UpidToPid(UniquePid upid) {
1779     auto pid_it = upids_to_exported_pids_.find(upid);
1780     PERFETTO_DCHECK(pid_it != upids_to_exported_pids_.end());
1781     return pid_it->second;
1782   }
1783 
UtidToPidAndTid(UniqueTid utid)1784   std::pair<uint32_t, uint32_t> UtidToPidAndTid(UniqueTid utid) {
1785     auto pid_and_tid_it = utids_to_exported_pids_and_tids_.find(utid);
1786     PERFETTO_DCHECK(pid_and_tid_it != utids_to_exported_pids_and_tids_.end());
1787     return pid_and_tid_it->second;
1788   }
1789 
NextExportedPidOrTidForDuplicates()1790   uint32_t NextExportedPidOrTidForDuplicates() {
1791     // Ensure that the exported substitute value does not represent a valid
1792     // pid/tid. This would be very unlikely in practice.
1793     while (IsValidPidOrTid(next_exported_pid_or_tid_for_duplicates_))
1794       next_exported_pid_or_tid_for_duplicates_--;
1795     return next_exported_pid_or_tid_for_duplicates_--;
1796   }
1797 
IsValidPidOrTid(uint32_t pid_or_tid)1798   bool IsValidPidOrTid(uint32_t pid_or_tid) {
1799     const auto& process_table = storage_->process_table();
1800     for (UniquePid upid = 0; upid < process_table.row_count(); upid++) {
1801       if (process_table.pid()[upid] == pid_or_tid)
1802         return true;
1803     }
1804 
1805     const auto& thread_table = storage_->thread_table();
1806     for (UniqueTid utid = 0; utid < thread_table.row_count(); utid++) {
1807       if (thread_table.tid()[utid] == pid_or_tid)
1808         return true;
1809     }
1810 
1811     return false;
1812   }
1813 
FillInProcessEventDetails(const Json::Value & event,uint32_t pid)1814   Json::Value FillInProcessEventDetails(const Json::Value& event,
1815                                         uint32_t pid) {
1816     Json::Value output = event;
1817     output["pid"] = Json::Int(pid);
1818     output["tid"] = Json::Int(-1);
1819     return output;
1820   }
1821 
AddAttributeToMemoryNode(Json::Value * event,const std::string & path,const std::string & key,int64_t value,const std::string & units)1822   void AddAttributeToMemoryNode(Json::Value* event,
1823                                 const std::string& path,
1824                                 const std::string& key,
1825                                 int64_t value,
1826                                 const std::string& units) {
1827     (*event)["args"]["dumps"]["allocators"][path]["attrs"][key]["value"] =
1828         base::Uint64ToHexStringNoPrefix(static_cast<uint64_t>(value));
1829     (*event)["args"]["dumps"]["allocators"][path]["attrs"][key]["type"] =
1830         "scalar";
1831     (*event)["args"]["dumps"]["allocators"][path]["attrs"][key]["units"] =
1832         units;
1833   }
1834 
AddAttributeToMemoryNode(Json::Value * event,const std::string & path,const std::string & key,const std::string & value,const std::string & units="")1835   void AddAttributeToMemoryNode(Json::Value* event,
1836                                 const std::string& path,
1837                                 const std::string& key,
1838                                 const std::string& value,
1839                                 const std::string& units = "") {
1840     (*event)["args"]["dumps"]["allocators"][path]["attrs"][key]["value"] =
1841         value;
1842     (*event)["args"]["dumps"]["allocators"][path]["attrs"][key]["type"] =
1843         "string";
1844     (*event)["args"]["dumps"]["allocators"][path]["attrs"][key]["units"] =
1845         units;
1846   }
1847 
GetCounterValue(TrackId track_id,int64_t ts)1848   uint64_t GetCounterValue(TrackId track_id, int64_t ts) {
1849     const auto& counter_table = storage_->counter_table();
1850     auto begin = counter_table.ts().begin();
1851     auto end = counter_table.ts().end();
1852     PERFETTO_DCHECK(counter_table.ts().IsSorted() &&
1853                     counter_table.ts().IsColumnType<int64_t>());
1854     // The timestamp column is sorted, so we can binary search for a matching
1855     // timestamp. Note that we don't use RowMap operations like FilterInto()
1856     // here because they bloat trace processor's binary size in Chrome too much.
1857     auto it = std::lower_bound(begin, end, ts,
1858                                [](const SqlValue& value, int64_t expected_ts) {
1859                                  return value.AsLong() < expected_ts;
1860                                });
1861     for (; it < end; ++it) {
1862       if ((*it).AsLong() != ts)
1863         break;
1864       if (counter_table.track_id()[it.row()].value == track_id.value)
1865         return static_cast<uint64_t>(counter_table.value()[it.row()]);
1866     }
1867     return 0;
1868   }
1869 
1870   const TraceStorage* storage_;
1871   ArgsBuilder args_builder_;
1872   TraceFormatWriter writer_;
1873 
1874   // If a pid/tid is duplicated between two or more  different processes/threads
1875   // (pid/tid reuse), we export the subsequent occurrences with different
1876   // pids/tids that is visibly different from regular pids/tids - counting down
1877   // from uint32_t max.
1878   uint32_t next_exported_pid_or_tid_for_duplicates_ =
1879       std::numeric_limits<uint32_t>::max();
1880 
1881   std::map<UniquePid, uint32_t> upids_to_exported_pids_;
1882   std::map<uint32_t, UniquePid> exported_pids_to_upids_;
1883   std::map<UniqueTid, std::pair<uint32_t, uint32_t>>
1884       utids_to_exported_pids_and_tids_;
1885   std::map<std::pair<uint32_t, uint32_t>, UniqueTid>
1886       exported_pids_and_tids_to_utids_;
1887 };
1888 
1889 #endif  // PERFETTO_BUILDFLAG(PERFETTO_TP_JSON)
1890 
1891 }  // namespace
1892 
1893 OutputWriter::OutputWriter() = default;
1894 OutputWriter::~OutputWriter() = default;
1895 
ExportJson(const TraceStorage * storage,OutputWriter * output,ArgumentFilterPredicate argument_filter,MetadataFilterPredicate metadata_filter,LabelFilterPredicate label_filter)1896 util::Status ExportJson(const TraceStorage* storage,
1897                         OutputWriter* output,
1898                         ArgumentFilterPredicate argument_filter,
1899                         MetadataFilterPredicate metadata_filter,
1900                         LabelFilterPredicate label_filter) {
1901 #if PERFETTO_BUILDFLAG(PERFETTO_TP_JSON)
1902   JsonExporter exporter(storage, output, std::move(argument_filter),
1903                         std::move(metadata_filter), std::move(label_filter));
1904   return exporter.Export();
1905 #else
1906   perfetto::base::ignore_result(storage);
1907   perfetto::base::ignore_result(output);
1908   perfetto::base::ignore_result(argument_filter);
1909   perfetto::base::ignore_result(metadata_filter);
1910   perfetto::base::ignore_result(label_filter);
1911   return util::ErrStatus("JSON support is not compiled in this build");
1912 #endif  // PERFETTO_BUILDFLAG(PERFETTO_TP_JSON)
1913 }
1914 
ExportJson(TraceProcessorStorage * tp,OutputWriter * output,ArgumentFilterPredicate argument_filter,MetadataFilterPredicate metadata_filter,LabelFilterPredicate label_filter)1915 util::Status ExportJson(TraceProcessorStorage* tp,
1916                         OutputWriter* output,
1917                         ArgumentFilterPredicate argument_filter,
1918                         MetadataFilterPredicate metadata_filter,
1919                         LabelFilterPredicate label_filter) {
1920   const TraceStorage* storage = reinterpret_cast<TraceProcessorStorageImpl*>(tp)
1921                                     ->context()
1922                                     ->storage.get();
1923   return ExportJson(storage, output, argument_filter, metadata_filter,
1924                     label_filter);
1925 }
1926 
ExportJson(const TraceStorage * storage,FILE * output)1927 util::Status ExportJson(const TraceStorage* storage, FILE* output) {
1928   FileWriter writer(output);
1929   return ExportJson(storage, &writer, nullptr, nullptr, nullptr);
1930 }
1931 
1932 }  // namespace json
1933 }  // namespace trace_processor
1934 }  // namespace perfetto
1935