• 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 "tools/trace_to_text/trace_to_json.h"
18 
19 #include <stdio.h>
20 
21 #include "perfetto/base/logging.h"
22 #include "perfetto/ext/base/scoped_file.h"
23 #include "perfetto/ext/base/temp_file.h"
24 #include "perfetto/trace_processor/trace_processor.h"
25 #include "tools/trace_to_text/utils.h"
26 
27 namespace perfetto {
28 namespace trace_to_text {
29 
30 namespace {
31 
32 const char kTraceHeader[] = R"({
33   "traceEvents": [],
34 )";
35 
36 const char kTraceFooter[] = R"(,
37   "controllerTraceDataKey": "systraceController"
38 })";
39 
ExportUserspaceEvents(trace_processor::TraceProcessor * tp,TraceWriter * writer)40 bool ExportUserspaceEvents(trace_processor::TraceProcessor* tp,
41                            TraceWriter* writer) {
42   fprintf(stderr, "Converting userspace events%c", kProgressChar);
43   fflush(stderr);
44 
45   // Write userspace trace to a temporary file.
46   // TODO(eseckler): Support streaming the result out of TP directly instead.
47   auto file = base::TempFile::Create();
48   char query[100];
49   sprintf(query, "select export_json(\"%s\")", file.path().c_str());
50   auto it = tp->ExecuteQuery(query);
51 
52   if (!it.Next()) {
53     auto status = it.Status();
54     PERFETTO_CHECK(!status.ok());
55     PERFETTO_ELOG("Could not convert userspace events: %s", status.c_message());
56     return false;
57   }
58 
59   base::ScopedFstream source(fopen(file.path().c_str(), "r"));
60   if (!source) {
61     PERFETTO_ELOG("Could not convert userspace events: Couldn't read file %s",
62                   file.path().c_str());
63     return false;
64   }
65 
66   char buf[BUFSIZ];
67   size_t size;
68   while ((size = fread(buf, sizeof(char), BUFSIZ, *source)) > 0) {
69     // Skip writing the closing brace since we'll append system trace data.
70     if (feof(*source))
71       size--;
72     writer->Write(buf, size);
73   }
74   return true;
75 }
76 
77 }  // namespace
78 
TraceToJson(std::istream * input,std::ostream * output,bool compress,Keep truncate_keep,bool full_sort)79 int TraceToJson(std::istream* input,
80                 std::ostream* output,
81                 bool compress,
82                 Keep truncate_keep,
83                 bool full_sort) {
84   std::unique_ptr<TraceWriter> trace_writer(
85       compress ? new DeflateTraceWriter(output) : new TraceWriter(output));
86 
87   trace_processor::Config config;
88   config.sorting_mode = full_sort
89                             ? trace_processor::SortingMode::kForceFullSort
90                             : trace_processor::SortingMode::kDefaultHeuristics;
91   std::unique_ptr<trace_processor::TraceProcessor> tp =
92       trace_processor::TraceProcessor::CreateInstance(config);
93 
94   if (!ReadTrace(tp.get(), input))
95     return 1;
96   tp->NotifyEndOfFile();
97 
98   // TODO(eseckler): Support truncation of userspace event data.
99   if (ExportUserspaceEvents(tp.get(), trace_writer.get())) {
100     trace_writer->Write(",\n");
101   } else {
102     trace_writer->Write(kTraceHeader);
103   }
104 
105   int ret = ExtractSystrace(tp.get(), trace_writer.get(),
106                             /*wrapped_in_json=*/true, truncate_keep);
107   if (ret)
108     return ret;
109 
110   trace_writer->Write(kTraceFooter);
111   return 0;
112 }
113 
114 }  // namespace trace_to_text
115 }  // namespace perfetto
116