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