xref: /aosp_15_r20/external/perfetto/src/trace_processor/read_trace_internal.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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/trace_processor/read_trace_internal.h"
18 
19 #include "perfetto/base/logging.h"
20 #include "perfetto/ext/base/file_utils.h"
21 #include "perfetto/ext/base/scoped_file.h"
22 #include "perfetto/ext/base/scoped_mmap.h"
23 #include "perfetto/ext/base/utils.h"
24 #include "perfetto/protozero/proto_utils.h"
25 #include "perfetto/trace_processor/trace_processor.h"
26 
27 #include "perfetto/trace_processor/trace_blob.h"
28 #include "perfetto/trace_processor/trace_blob_view.h"
29 #include "src/trace_processor/forwarding_trace_parser.h"
30 #include "src/trace_processor/importers/proto/proto_trace_tokenizer.h"
31 #include "src/trace_processor/util/gzip_utils.h"
32 #include "src/trace_processor/util/status_macros.h"
33 
34 #include "protos/perfetto/trace/trace.pbzero.h"
35 #include "protos/perfetto/trace/trace_packet.pbzero.h"
36 
37 namespace perfetto {
38 namespace trace_processor {
39 namespace {
40 
41 // 1MB chunk size seems the best tradeoff on a MacBook Pro 2013 - i7 2.8 GHz.
42 constexpr size_t kChunkSize = 1024 * 1024;
43 
ReadTraceUsingRead(TraceProcessor * tp,int fd,uint64_t * file_size,const std::function<void (uint64_t parsed_size)> & progress_callback)44 util::Status ReadTraceUsingRead(
45     TraceProcessor* tp,
46     int fd,
47     uint64_t* file_size,
48     const std::function<void(uint64_t parsed_size)>& progress_callback) {
49   // Load the trace in chunks using ordinary read().
50   for (int i = 0;; i++) {
51     if (progress_callback && i % 128 == 0)
52       progress_callback(*file_size);
53 
54     TraceBlob blob = TraceBlob::Allocate(kChunkSize);
55     auto rsize = base::Read(fd, blob.data(), blob.size());
56     if (rsize == 0)
57       break;
58 
59     if (rsize < 0) {
60       return util::ErrStatus("Reading trace file failed (errno: %d, %s)", errno,
61                              strerror(errno));
62     }
63 
64     *file_size += static_cast<uint64_t>(rsize);
65     TraceBlobView blob_view(std::move(blob), 0, static_cast<size_t>(rsize));
66     RETURN_IF_ERROR(tp->Parse(std::move(blob_view)));
67   }
68   return util::OkStatus();
69 }
70 }  // namespace
71 
ReadTraceUnfinalized(TraceProcessor * tp,const char * filename,const std::function<void (uint64_t parsed_size)> & progress_callback)72 util::Status ReadTraceUnfinalized(
73     TraceProcessor* tp,
74     const char* filename,
75     const std::function<void(uint64_t parsed_size)>& progress_callback) {
76   uint64_t bytes_read = 0;
77 
78 #if PERFETTO_HAS_MMAP()
79   char* no_mmap = getenv("TRACE_PROCESSOR_NO_MMAP");
80   bool use_mmap = !no_mmap || *no_mmap != '1';
81 
82   if (use_mmap) {
83     base::ScopedMmap mapped = base::ReadMmapWholeFile(filename);
84     if (mapped.IsValid()) {
85       size_t length = mapped.length();
86       TraceBlobView whole_mmap(TraceBlob::FromMmap(std::move(mapped)));
87       // Parse the file in chunks so we get some status update on stdio.
88       static constexpr size_t kMmapChunkSize = 128ul * 1024 * 1024;
89       while (bytes_read < length) {
90         progress_callback(bytes_read);
91         const size_t bytes_read_z = static_cast<size_t>(bytes_read);
92         size_t slice_size = std::min(length - bytes_read_z, kMmapChunkSize);
93         TraceBlobView slice = whole_mmap.slice_off(bytes_read_z, slice_size);
94         RETURN_IF_ERROR(tp->Parse(std::move(slice)));
95         bytes_read += slice_size;
96       }  // while (slices)
97     }    // if (mapped.IsValid())
98   }      // if (use_mmap)
99   if (bytes_read == 0)
100     PERFETTO_LOG("Cannot use mmap on this system. Falling back on read()");
101 #endif  // PERFETTO_HAS_MMAP()
102   if (bytes_read == 0) {
103     base::ScopedFile fd(base::OpenFile(filename, O_RDONLY));
104     if (!fd)
105       return util::ErrStatus("Could not open trace file (path: %s)", filename);
106     RETURN_IF_ERROR(
107         ReadTraceUsingRead(tp, *fd, &bytes_read, progress_callback));
108   }
109   tp->SetCurrentTraceName(filename);
110 
111   if (progress_callback)
112     progress_callback(bytes_read);
113   return util::OkStatus();
114 }
115 }  // namespace trace_processor
116 }  // namespace perfetto
117