• 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("trace_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("trace_id"));
933           PERFETTO_DCHECK(track_args->isMember("trace_id_is_process_scoped"));
934           PERFETTO_DCHECK(track_args->isMember("source_scope"));
935           uint64_t trace_id =
936               static_cast<uint64_t>((*track_args)["trace_id"].asInt64());
937           std::string source_scope = (*track_args)["source_scope"].asString();
938           if (!source_scope.empty())
939             event["scope"] = source_scope;
940           bool trace_id_is_process_scoped =
941               (*track_args)["trace_id_is_process_scoped"].asBool();
942           if (trace_id_is_process_scoped) {
943             event["id2"]["local"] = base::Uint64ToHexString(trace_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(trace_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       }
1267 
1268       if (current_sample != current_events_.end()) {
1269         current_events_.erase(current_sample);
1270       }
1271 
1272       auto new_entry = current_events_.emplace(
1273           std::piecewise_construct, std::forward_as_tuple(utid),
1274           std::forward_as_tuple(writer_, callsite_id, ts, event));
1275       return new_entry.first->second.event_id();
1276     }
1277 
GenerateNewEventId()1278     static uint64_t GenerateNewEventId() {
1279       // "n"-phase events are nestable async events which get tied together
1280       // with their id, so we need to give each one a unique ID as we only
1281       // want the samples to show up on their own track in the trace-viewer
1282       // but not nested together (unless they're nested under a merged event).
1283       static size_t g_id_counter = 0;
1284       return ++g_id_counter;
1285     }
1286 
1287    private:
1288     class Sample {
1289      public:
Sample(TraceFormatWriter & writer,CallsiteId callsite_id,int64_t ts,const Json::Value & event)1290       Sample(TraceFormatWriter& writer,
1291              CallsiteId callsite_id,
1292              int64_t ts,
1293              const Json::Value& event)
1294           : writer_(writer),
1295             callsite_id_(callsite_id),
1296             begin_ts_(ts),
1297             end_ts_(ts),
1298             event_(event),
1299             event_id_(MergedProfileSamplesEmitter::GenerateNewEventId()),
1300             sample_count_(1) {}
1301 
~Sample()1302       ~Sample() {
1303         // No point writing a merged event if we only got a single sample
1304         // as ExportCpuProfileSamples will already be writing the instant event.
1305         if (sample_count_ == 1)
1306           return;
1307 
1308         event_["id"] = base::Uint64ToHexString(event_id_);
1309 
1310         // Write the BEGIN event.
1311         event_["ph"] = "b";
1312         // We subtract 1us as a workaround for the first async event not
1313         // nesting underneath the parent event if the timestamp is identical.
1314         int64_t begin_in_us_ = begin_ts_ / 1000;
1315         event_["ts"] = Json::Int64(std::min(begin_in_us_ - 1, begin_in_us_));
1316         writer_.WriteCommonEvent(event_);
1317 
1318         // Write the END event.
1319         event_["ph"] = "e";
1320         event_["ts"] = Json::Int64(end_ts_ / 1000);
1321         // No need for args for the end event; remove them to save some space.
1322         event_["args"].clear();
1323         writer_.WriteCommonEvent(event_);
1324       }
1325 
UpdateWithNewSample(int64_t ts)1326       void UpdateWithNewSample(int64_t ts) {
1327         // We assume samples for a given thread will appear in timestamp
1328         // order; if this assumption stops holding true, we'll have to sort the
1329         // samples first.
1330         if (ts < end_ts_ || begin_ts_ > ts) {
1331           PERFETTO_ELOG(
1332               "Got an timestamp out of sequence while merging stack samples "
1333               "during JSON export!\n");
1334           PERFETTO_DCHECK(false);
1335         }
1336 
1337         end_ts_ = ts;
1338         sample_count_++;
1339       }
1340 
event_id() const1341       uint64_t event_id() const { return event_id_; }
callsite_id() const1342       CallsiteId callsite_id() const { return callsite_id_; }
1343 
1344      public:
1345       Sample(const Sample&) = delete;
1346       Sample& operator=(const Sample&) = delete;
1347       Sample& operator=(Sample&& value) = delete;
1348 
1349       TraceFormatWriter& writer_;
1350       CallsiteId callsite_id_;
1351       int64_t begin_ts_;
1352       int64_t end_ts_;
1353       Json::Value event_;
1354       uint64_t event_id_;
1355       size_t sample_count_;
1356     };
1357 
1358     MergedProfileSamplesEmitter(const MergedProfileSamplesEmitter&) = delete;
1359     MergedProfileSamplesEmitter& operator=(const MergedProfileSamplesEmitter&) =
1360         delete;
1361     MergedProfileSamplesEmitter& operator=(
1362         MergedProfileSamplesEmitter&& value) = delete;
1363 
1364     std::unordered_map<UniqueTid, Sample> current_events_;
1365     TraceFormatWriter& writer_;
1366   };
1367 
ExportCpuProfileSamples()1368   util::Status ExportCpuProfileSamples() {
1369     MergedProfileSamplesEmitter merged_sample_emitter(writer_);
1370 
1371     const tables::CpuProfileStackSampleTable& samples =
1372         storage_->cpu_profile_stack_sample_table();
1373     for (uint32_t i = 0; i < samples.row_count(); ++i) {
1374       Json::Value event;
1375       event["ts"] = Json::Int64(samples.ts()[i] / 1000);
1376 
1377       UniqueTid utid = static_cast<UniqueTid>(samples.utid()[i]);
1378       auto pid_and_tid = UtidToPidAndTid(utid);
1379       event["pid"] = Json::Int(pid_and_tid.first);
1380       event["tid"] = Json::Int(pid_and_tid.second);
1381 
1382       event["ph"] = "n";
1383       event["cat"] = "disabled-by-default-cpu_profiler";
1384       event["name"] = "StackCpuSampling";
1385       event["s"] = "t";
1386 
1387       // Add a dummy thread timestamp to this event to match the format of
1388       // instant events. Useful in the UI to view args of a selected group of
1389       // samples.
1390       event["tts"] = Json::Int64(1);
1391 
1392       const auto& callsites = storage_->stack_profile_callsite_table();
1393       const auto& frames = storage_->stack_profile_frame_table();
1394       const auto& mappings = storage_->stack_profile_mapping_table();
1395 
1396       std::vector<std::string> callstack;
1397       std::optional<CallsiteId> opt_callsite_id = samples.callsite_id()[i];
1398 
1399       while (opt_callsite_id) {
1400         CallsiteId callsite_id = *opt_callsite_id;
1401         uint32_t callsite_row = *callsites.id().IndexOf(callsite_id);
1402 
1403         FrameId frame_id = callsites.frame_id()[callsite_row];
1404         uint32_t frame_row = *frames.id().IndexOf(frame_id);
1405 
1406         MappingId mapping_id = frames.mapping()[frame_row];
1407         uint32_t mapping_row = *mappings.id().IndexOf(mapping_id);
1408 
1409         NullTermStringView symbol_name;
1410         auto opt_symbol_set_id = frames.symbol_set_id()[frame_row];
1411         if (opt_symbol_set_id) {
1412           symbol_name = storage_->GetString(
1413               storage_->symbol_table().name()[*opt_symbol_set_id]);
1414         }
1415 
1416         base::StackString<1024> frame_entry(
1417             "%s - %s [%s]\n",
1418             (symbol_name.empty()
1419                  ? base::Uint64ToHexString(
1420                        static_cast<uint64_t>(frames.rel_pc()[frame_row]))
1421                        .c_str()
1422                  : symbol_name.c_str()),
1423             GetNonNullString(storage_, mappings.name()[mapping_row]),
1424             GetNonNullString(storage_, mappings.build_id()[mapping_row]));
1425 
1426         callstack.emplace_back(frame_entry.ToStdString());
1427 
1428         opt_callsite_id = callsites.parent_id()[callsite_row];
1429       }
1430 
1431       std::string merged_callstack;
1432       for (auto entry = callstack.rbegin(); entry != callstack.rend();
1433            ++entry) {
1434         merged_callstack += *entry;
1435       }
1436 
1437       event["args"]["frames"] = merged_callstack;
1438       event["args"]["process_priority"] = samples.process_priority()[i];
1439 
1440       // TODO(oysteine): Used for backwards compatibility with the memlog
1441       // pipeline, should remove once we've switched to looking directly at the
1442       // tid.
1443       event["args"]["thread_id"] = Json::Int(pid_and_tid.second);
1444 
1445       // Emit duration events for adjacent samples with the same callsite.
1446       // For now, only do this when the trace has already been symbolized i.e.
1447       // are not directly output by Chrome, to avoid interfering with other
1448       // processing pipelines.
1449       std::optional<CallsiteId> opt_current_callsite_id =
1450           samples.callsite_id()[i];
1451 
1452       if (opt_current_callsite_id && storage_->symbol_table().row_count() > 0) {
1453         uint64_t parent_event_id = merged_sample_emitter.AddEventForUtid(
1454             utid, samples.ts()[i], *opt_current_callsite_id, event);
1455         event["id"] = base::Uint64ToHexString(parent_event_id);
1456       } else {
1457         event["id"] = base::Uint64ToHexString(
1458             MergedProfileSamplesEmitter::GenerateNewEventId());
1459       }
1460 
1461       writer_.WriteCommonEvent(event);
1462     }
1463 
1464     return util::OkStatus();
1465   }
1466 
ExportMetadata()1467   util::Status ExportMetadata() {
1468     const auto& trace_metadata = storage_->metadata_table();
1469     const auto& keys = trace_metadata.name();
1470     const auto& int_values = trace_metadata.int_value();
1471     const auto& str_values = trace_metadata.str_value();
1472 
1473     // Create a mapping from key string ids to keys.
1474     std::unordered_map<StringId, metadata::KeyId> key_map;
1475     for (uint32_t i = 0; i < metadata::kNumKeys; ++i) {
1476       auto id = *storage_->string_pool().GetId(metadata::kNames[i]);
1477       key_map[id] = static_cast<metadata::KeyId>(i);
1478     }
1479 
1480     for (uint32_t pos = 0; pos < trace_metadata.row_count(); pos++) {
1481       auto key_it = key_map.find(keys[pos]);
1482       // Skip exporting dynamic entries; the cr-xxx entries that come from
1483       // the ChromeMetadata proto message are already exported from the raw
1484       // table.
1485       if (key_it == key_map.end())
1486         continue;
1487 
1488       // Cast away from enum type, as otherwise -Wswitch-enum will demand an
1489       // exhaustive list of cases, even if there's a default case.
1490       metadata::KeyId key = key_it->second;
1491       switch (static_cast<size_t>(key)) {
1492         case metadata::benchmark_description:
1493           writer_.AppendTelemetryMetadataString(
1494               "benchmarkDescriptions", str_values.GetString(pos).c_str());
1495           break;
1496 
1497         case metadata::benchmark_name:
1498           writer_.AppendTelemetryMetadataString(
1499               "benchmarks", str_values.GetString(pos).c_str());
1500           break;
1501 
1502         case metadata::benchmark_start_time_us:
1503           writer_.SetTelemetryMetadataTimestamp("benchmarkStart",
1504                                                 *int_values[pos]);
1505           break;
1506 
1507         case metadata::benchmark_had_failures:
1508           writer_.AppendTelemetryMetadataBool("hadFailures", *int_values[pos]);
1509           break;
1510 
1511         case metadata::benchmark_label:
1512           writer_.AppendTelemetryMetadataString(
1513               "labels", str_values.GetString(pos).c_str());
1514           break;
1515 
1516         case metadata::benchmark_story_name:
1517           writer_.AppendTelemetryMetadataString(
1518               "stories", str_values.GetString(pos).c_str());
1519           break;
1520 
1521         case metadata::benchmark_story_run_index:
1522           writer_.AppendTelemetryMetadataInt("storysetRepeats",
1523                                              *int_values[pos]);
1524           break;
1525 
1526         case metadata::benchmark_story_run_time_us:
1527           writer_.SetTelemetryMetadataTimestamp("traceStart", *int_values[pos]);
1528           break;
1529 
1530         case metadata::benchmark_story_tags:  // repeated
1531           writer_.AppendTelemetryMetadataString(
1532               "storyTags", str_values.GetString(pos).c_str());
1533           break;
1534 
1535         default:
1536           PERFETTO_DLOG("Ignoring metadata key %zu", static_cast<size_t>(key));
1537           break;
1538       }
1539     }
1540     return util::OkStatus();
1541   }
1542 
ExportStats()1543   util::Status ExportStats() {
1544     const auto& stats = storage_->stats();
1545 
1546     for (size_t idx = 0; idx < stats::kNumKeys; idx++) {
1547       if (stats::kTypes[idx] == stats::kSingle) {
1548         writer_.SetStats(stats::kNames[idx], stats[idx].value);
1549       } else {
1550         PERFETTO_DCHECK(stats::kTypes[idx] == stats::kIndexed);
1551         writer_.SetStats(stats::kNames[idx], stats[idx].indexed_values);
1552       }
1553     }
1554 
1555     return util::OkStatus();
1556   }
1557 
ExportMemorySnapshots()1558   util::Status ExportMemorySnapshots() {
1559     const auto& memory_snapshots = storage_->memory_snapshot_table();
1560     std::optional<StringId> private_footprint_id =
1561         storage_->string_pool().GetId("chrome.private_footprint_kb");
1562     std::optional<StringId> peak_resident_set_id =
1563         storage_->string_pool().GetId("chrome.peak_resident_set_kb");
1564 
1565     for (uint32_t memory_index = 0; memory_index < memory_snapshots.row_count();
1566          ++memory_index) {
1567       Json::Value event_base;
1568 
1569       event_base["ph"] = "v";
1570       event_base["cat"] = "disabled-by-default-memory-infra";
1571       auto snapshot_id = memory_snapshots.id()[memory_index].value;
1572       event_base["id"] = base::Uint64ToHexString(snapshot_id);
1573       int64_t snapshot_ts = memory_snapshots.timestamp()[memory_index];
1574       event_base["ts"] = Json::Int64(snapshot_ts / 1000);
1575       // TODO(crbug:1116359): Add dump type to the snapshot proto
1576       // to properly fill event_base["name"]
1577       event_base["name"] = "periodic_interval";
1578       event_base["args"]["dumps"]["level_of_detail"] = GetNonNullString(
1579           storage_, memory_snapshots.detail_level()[memory_index]);
1580 
1581       // Export OS dump events for processes with relevant data.
1582       const auto& process_table = storage_->process_table();
1583       for (UniquePid upid = 0; upid < process_table.row_count(); ++upid) {
1584         Json::Value event =
1585             FillInProcessEventDetails(event_base, process_table.pid()[upid]);
1586         Json::Value& totals = event["args"]["dumps"]["process_totals"];
1587 
1588         const auto& process_counters = storage_->process_counter_track_table();
1589 
1590         for (uint32_t counter_index = 0;
1591              counter_index < process_counters.row_count(); ++counter_index) {
1592           if (process_counters.upid()[counter_index] != upid)
1593             continue;
1594           TrackId track_id = process_counters.id()[counter_index];
1595           if (private_footprint_id && (process_counters.name()[counter_index] ==
1596                                        private_footprint_id)) {
1597             totals["private_footprint_bytes"] = base::Uint64ToHexStringNoPrefix(
1598                 GetCounterValue(track_id, snapshot_ts));
1599           } else if (peak_resident_set_id &&
1600                      (process_counters.name()[counter_index] ==
1601                       peak_resident_set_id)) {
1602             totals["peak_resident_set_size"] = base::Uint64ToHexStringNoPrefix(
1603                 GetCounterValue(track_id, snapshot_ts));
1604           }
1605         }
1606 
1607         auto process_args_id = process_table.arg_set_id()[upid];
1608         if (process_args_id) {
1609           const Json::Value* process_args =
1610               &args_builder_.GetArgs(process_args_id);
1611           if (process_args->isMember("is_peak_rss_resettable")) {
1612             totals["is_peak_rss_resettable"] =
1613                 (*process_args)["is_peak_rss_resettable"];
1614           }
1615         }
1616 
1617         const auto& smaps_table = storage_->profiler_smaps_table();
1618         // Do not create vm_regions without memory maps, since catapult expects
1619         // to have rows.
1620         Json::Value* smaps =
1621             smaps_table.row_count() > 0
1622                 ? &event["args"]["dumps"]["process_mmaps"]["vm_regions"]
1623                 : nullptr;
1624         for (uint32_t smaps_index = 0; smaps_index < smaps_table.row_count();
1625              ++smaps_index) {
1626           if (smaps_table.upid()[smaps_index] != upid)
1627             continue;
1628           if (smaps_table.ts()[smaps_index] != snapshot_ts)
1629             continue;
1630           Json::Value region;
1631           region["mf"] =
1632               GetNonNullString(storage_, smaps_table.file_name()[smaps_index]);
1633           region["pf"] =
1634               Json::Int64(smaps_table.protection_flags()[smaps_index]);
1635           region["sa"] = base::Uint64ToHexStringNoPrefix(
1636               static_cast<uint64_t>(smaps_table.start_address()[smaps_index]));
1637           region["sz"] = base::Uint64ToHexStringNoPrefix(
1638               static_cast<uint64_t>(smaps_table.size_kb()[smaps_index]) * 1024);
1639           region["ts"] =
1640               Json::Int64(smaps_table.module_timestamp()[smaps_index]);
1641           region["id"] = GetNonNullString(
1642               storage_, smaps_table.module_debugid()[smaps_index]);
1643           region["df"] = GetNonNullString(
1644               storage_, smaps_table.module_debug_path()[smaps_index]);
1645           region["bs"]["pc"] = base::Uint64ToHexStringNoPrefix(
1646               static_cast<uint64_t>(
1647                   smaps_table.private_clean_resident_kb()[smaps_index]) *
1648               1024);
1649           region["bs"]["pd"] = base::Uint64ToHexStringNoPrefix(
1650               static_cast<uint64_t>(
1651                   smaps_table.private_dirty_kb()[smaps_index]) *
1652               1024);
1653           region["bs"]["pss"] = base::Uint64ToHexStringNoPrefix(
1654               static_cast<uint64_t>(
1655                   smaps_table.proportional_resident_kb()[smaps_index]) *
1656               1024);
1657           region["bs"]["sc"] = base::Uint64ToHexStringNoPrefix(
1658               static_cast<uint64_t>(
1659                   smaps_table.shared_clean_resident_kb()[smaps_index]) *
1660               1024);
1661           region["bs"]["sd"] = base::Uint64ToHexStringNoPrefix(
1662               static_cast<uint64_t>(
1663                   smaps_table.shared_dirty_resident_kb()[smaps_index]) *
1664               1024);
1665           region["bs"]["sw"] = base::Uint64ToHexStringNoPrefix(
1666               static_cast<uint64_t>(smaps_table.swap_kb()[smaps_index]) * 1024);
1667           smaps->append(region);
1668         }
1669 
1670         if (!totals.empty() || (smaps && !smaps->empty()))
1671           writer_.WriteCommonEvent(event);
1672       }
1673 
1674       // Export chrome dump events for process snapshots in current memory
1675       // snapshot.
1676       const auto& process_snapshots = storage_->process_memory_snapshot_table();
1677 
1678       for (uint32_t process_index = 0;
1679            process_index < process_snapshots.row_count(); ++process_index) {
1680         if (process_snapshots.snapshot_id()[process_index].value != snapshot_id)
1681           continue;
1682 
1683         auto process_snapshot_id = process_snapshots.id()[process_index].value;
1684         uint32_t pid = UpidToPid(process_snapshots.upid()[process_index]);
1685 
1686         // Shared memory nodes are imported into a fake process with pid 0.
1687         // Catapult expects them to be associated with one of the real processes
1688         // of the snapshot, so we choose the first one we can find and replace
1689         // the pid.
1690         if (pid == 0) {
1691           for (uint32_t i = 0; i < process_snapshots.row_count(); ++i) {
1692             if (process_snapshots.snapshot_id()[i].value != snapshot_id)
1693               continue;
1694             uint32_t new_pid = UpidToPid(process_snapshots.upid()[i]);
1695             if (new_pid != 0) {
1696               pid = new_pid;
1697               break;
1698             }
1699           }
1700         }
1701 
1702         Json::Value event = FillInProcessEventDetails(event_base, pid);
1703 
1704         const auto& snapshot_nodes = storage_->memory_snapshot_node_table();
1705 
1706         for (uint32_t node_index = 0; node_index < snapshot_nodes.row_count();
1707              ++node_index) {
1708           if (snapshot_nodes.process_snapshot_id()[node_index].value !=
1709               process_snapshot_id) {
1710             continue;
1711           }
1712           const char* path =
1713               GetNonNullString(storage_, snapshot_nodes.path()[node_index]);
1714           event["args"]["dumps"]["allocators"][path]["guid"] =
1715               base::Uint64ToHexStringNoPrefix(
1716                   static_cast<uint64_t>(snapshot_nodes.id()[node_index].value));
1717           if (snapshot_nodes.size()[node_index]) {
1718             AddAttributeToMemoryNode(&event, path, "size",
1719                                      snapshot_nodes.size()[node_index],
1720                                      "bytes");
1721           }
1722           if (snapshot_nodes.effective_size()[node_index]) {
1723             AddAttributeToMemoryNode(
1724                 &event, path, "effective_size",
1725                 snapshot_nodes.effective_size()[node_index], "bytes");
1726           }
1727 
1728           auto node_args_id = snapshot_nodes.arg_set_id()[node_index];
1729           if (!node_args_id)
1730             continue;
1731           const Json::Value* node_args =
1732               &args_builder_.GetArgs(node_args_id.value());
1733           for (const auto& arg_name : node_args->getMemberNames()) {
1734             const Json::Value& arg_value = (*node_args)[arg_name]["value"];
1735             if (arg_value.empty())
1736               continue;
1737             if (arg_value.isString()) {
1738               AddAttributeToMemoryNode(&event, path, arg_name,
1739                                        arg_value.asString());
1740             } else if (arg_value.isInt64()) {
1741               Json::Value unit = (*node_args)[arg_name]["unit"];
1742               if (unit.empty())
1743                 unit = "unknown";
1744               AddAttributeToMemoryNode(&event, path, arg_name,
1745                                        arg_value.asInt64(), unit.asString());
1746             }
1747           }
1748         }
1749 
1750         const auto& snapshot_edges = storage_->memory_snapshot_edge_table();
1751 
1752         for (uint32_t edge_index = 0; edge_index < snapshot_edges.row_count();
1753              ++edge_index) {
1754           SnapshotNodeId source_node_id =
1755               snapshot_edges.source_node_id()[edge_index];
1756           uint32_t source_node_row =
1757               *snapshot_nodes.id().IndexOf(source_node_id);
1758 
1759           if (snapshot_nodes.process_snapshot_id()[source_node_row].value !=
1760               process_snapshot_id) {
1761             continue;
1762           }
1763           Json::Value edge;
1764           edge["source"] = base::Uint64ToHexStringNoPrefix(
1765               snapshot_edges.source_node_id()[edge_index].value);
1766           edge["target"] = base::Uint64ToHexStringNoPrefix(
1767               snapshot_edges.target_node_id()[edge_index].value);
1768           edge["importance"] =
1769               Json::Int(snapshot_edges.importance()[edge_index]);
1770           edge["type"] = "ownership";
1771           event["args"]["dumps"]["allocators_graph"].append(edge);
1772         }
1773         writer_.WriteCommonEvent(event);
1774       }
1775     }
1776     return util::OkStatus();
1777   }
1778 
UpidToPid(UniquePid upid)1779   uint32_t UpidToPid(UniquePid upid) {
1780     auto pid_it = upids_to_exported_pids_.find(upid);
1781     PERFETTO_DCHECK(pid_it != upids_to_exported_pids_.end());
1782     return pid_it->second;
1783   }
1784 
UtidToPidAndTid(UniqueTid utid)1785   std::pair<uint32_t, uint32_t> UtidToPidAndTid(UniqueTid utid) {
1786     auto pid_and_tid_it = utids_to_exported_pids_and_tids_.find(utid);
1787     PERFETTO_DCHECK(pid_and_tid_it != utids_to_exported_pids_and_tids_.end());
1788     return pid_and_tid_it->second;
1789   }
1790 
NextExportedPidOrTidForDuplicates()1791   uint32_t NextExportedPidOrTidForDuplicates() {
1792     // Ensure that the exported substitute value does not represent a valid
1793     // pid/tid. This would be very unlikely in practice.
1794     while (IsValidPidOrTid(next_exported_pid_or_tid_for_duplicates_))
1795       next_exported_pid_or_tid_for_duplicates_--;
1796     return next_exported_pid_or_tid_for_duplicates_--;
1797   }
1798 
IsValidPidOrTid(uint32_t pid_or_tid)1799   bool IsValidPidOrTid(uint32_t pid_or_tid) {
1800     const auto& process_table = storage_->process_table();
1801     for (UniquePid upid = 0; upid < process_table.row_count(); upid++) {
1802       if (process_table.pid()[upid] == pid_or_tid)
1803         return true;
1804     }
1805 
1806     const auto& thread_table = storage_->thread_table();
1807     for (UniqueTid utid = 0; utid < thread_table.row_count(); utid++) {
1808       if (thread_table.tid()[utid] == pid_or_tid)
1809         return true;
1810     }
1811 
1812     return false;
1813   }
1814 
FillInProcessEventDetails(const Json::Value & event,uint32_t pid)1815   Json::Value FillInProcessEventDetails(const Json::Value& event,
1816                                         uint32_t pid) {
1817     Json::Value output = event;
1818     output["pid"] = Json::Int(pid);
1819     output["tid"] = Json::Int(-1);
1820     return output;
1821   }
1822 
AddAttributeToMemoryNode(Json::Value * event,const std::string & path,const std::string & key,int64_t value,const std::string & units)1823   void AddAttributeToMemoryNode(Json::Value* event,
1824                                 const std::string& path,
1825                                 const std::string& key,
1826                                 int64_t value,
1827                                 const std::string& units) {
1828     (*event)["args"]["dumps"]["allocators"][path]["attrs"][key]["value"] =
1829         base::Uint64ToHexStringNoPrefix(static_cast<uint64_t>(value));
1830     (*event)["args"]["dumps"]["allocators"][path]["attrs"][key]["type"] =
1831         "scalar";
1832     (*event)["args"]["dumps"]["allocators"][path]["attrs"][key]["units"] =
1833         units;
1834   }
1835 
AddAttributeToMemoryNode(Json::Value * event,const std::string & path,const std::string & key,const std::string & value,const std::string & units="")1836   void AddAttributeToMemoryNode(Json::Value* event,
1837                                 const std::string& path,
1838                                 const std::string& key,
1839                                 const std::string& value,
1840                                 const std::string& units = "") {
1841     (*event)["args"]["dumps"]["allocators"][path]["attrs"][key]["value"] =
1842         value;
1843     (*event)["args"]["dumps"]["allocators"][path]["attrs"][key]["type"] =
1844         "string";
1845     (*event)["args"]["dumps"]["allocators"][path]["attrs"][key]["units"] =
1846         units;
1847   }
1848 
GetCounterValue(TrackId track_id,int64_t ts)1849   uint64_t GetCounterValue(TrackId track_id, int64_t ts) {
1850     const auto& counter_table = storage_->counter_table();
1851     auto begin = counter_table.ts().begin();
1852     auto end = counter_table.ts().end();
1853     PERFETTO_DCHECK(counter_table.ts().IsSorted() &&
1854                     counter_table.ts().IsColumnType<int64_t>());
1855     // The timestamp column is sorted, so we can binary search for a matching
1856     // timestamp. Note that we don't use RowMap operations like FilterInto()
1857     // here because they bloat trace processor's binary size in Chrome too much.
1858     auto it = std::lower_bound(begin, end, ts,
1859                                [](const SqlValue& value, int64_t expected_ts) {
1860                                  return value.AsLong() < expected_ts;
1861                                });
1862     for (; it < end; ++it) {
1863       if ((*it).AsLong() != ts)
1864         break;
1865       if (counter_table.track_id()[it.row()].value == track_id.value)
1866         return static_cast<uint64_t>(counter_table.value()[it.row()]);
1867     }
1868     return 0;
1869   }
1870 
1871   const TraceStorage* storage_;
1872   ArgsBuilder args_builder_;
1873   TraceFormatWriter writer_;
1874 
1875   // If a pid/tid is duplicated between two or more  different processes/threads
1876   // (pid/tid reuse), we export the subsequent occurrences with different
1877   // pids/tids that is visibly different from regular pids/tids - counting down
1878   // from uint32_t max.
1879   uint32_t next_exported_pid_or_tid_for_duplicates_ =
1880       std::numeric_limits<uint32_t>::max();
1881 
1882   std::map<UniquePid, uint32_t> upids_to_exported_pids_;
1883   std::map<uint32_t, UniquePid> exported_pids_to_upids_;
1884   std::map<UniqueTid, std::pair<uint32_t, uint32_t>>
1885       utids_to_exported_pids_and_tids_;
1886   std::map<std::pair<uint32_t, uint32_t>, UniqueTid>
1887       exported_pids_and_tids_to_utids_;
1888 };
1889 
1890 #endif  // PERFETTO_BUILDFLAG(PERFETTO_TP_JSON)
1891 
1892 }  // namespace
1893 
1894 OutputWriter::OutputWriter() = default;
1895 OutputWriter::~OutputWriter() = default;
1896 
ExportJson(const TraceStorage * storage,OutputWriter * output,ArgumentFilterPredicate argument_filter,MetadataFilterPredicate metadata_filter,LabelFilterPredicate label_filter)1897 util::Status ExportJson(const TraceStorage* storage,
1898                         OutputWriter* output,
1899                         ArgumentFilterPredicate argument_filter,
1900                         MetadataFilterPredicate metadata_filter,
1901                         LabelFilterPredicate label_filter) {
1902 #if PERFETTO_BUILDFLAG(PERFETTO_TP_JSON)
1903   JsonExporter exporter(storage, output, std::move(argument_filter),
1904                         std::move(metadata_filter), std::move(label_filter));
1905   return exporter.Export();
1906 #else
1907   perfetto::base::ignore_result(storage);
1908   perfetto::base::ignore_result(output);
1909   perfetto::base::ignore_result(argument_filter);
1910   perfetto::base::ignore_result(metadata_filter);
1911   perfetto::base::ignore_result(label_filter);
1912   return util::ErrStatus("JSON support is not compiled in this build");
1913 #endif  // PERFETTO_BUILDFLAG(PERFETTO_TP_JSON)
1914 }
1915 
ExportJson(TraceProcessorStorage * tp,OutputWriter * output,ArgumentFilterPredicate argument_filter,MetadataFilterPredicate metadata_filter,LabelFilterPredicate label_filter)1916 util::Status ExportJson(TraceProcessorStorage* tp,
1917                         OutputWriter* output,
1918                         ArgumentFilterPredicate argument_filter,
1919                         MetadataFilterPredicate metadata_filter,
1920                         LabelFilterPredicate label_filter) {
1921   const TraceStorage* storage = reinterpret_cast<TraceProcessorStorageImpl*>(tp)
1922                                     ->context()
1923                                     ->storage.get();
1924   return ExportJson(storage, output, argument_filter, metadata_filter,
1925                     label_filter);
1926 }
1927 
ExportJson(const TraceStorage * storage,FILE * output)1928 util::Status ExportJson(const TraceStorage* storage, FILE* output) {
1929   FileWriter writer(output);
1930   return ExportJson(storage, &writer, nullptr, nullptr, nullptr);
1931 }
1932 
1933 }  // namespace json
1934 }  // namespace trace_processor
1935 }  // namespace perfetto
1936