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