• 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/utils.h"
23 #include "perfetto/protozero/proto_utils.h"
24 #include "perfetto/trace_processor/trace_processor.h"
25 
26 #include "perfetto/trace_processor/trace_blob.h"
27 #include "perfetto/trace_processor/trace_blob_view.h"
28 #include "src/trace_processor/forwarding_trace_parser.h"
29 #include "src/trace_processor/importers/gzip/gzip_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 #if TRACE_PROCESSOR_HAS_MMAP()
38 #include <stdlib.h>
39 #include <sys/mman.h>
40 #include <unistd.h>
41 #endif
42 namespace perfetto {
43 namespace trace_processor {
44 namespace {
45 
46 // 1MB chunk size seems the best tradeoff on a MacBook Pro 2013 - i7 2.8 GHz.
47 constexpr size_t kChunkSize = 1024 * 1024;
48 
ReadTraceUsingRead(TraceProcessor * tp,int fd,uint64_t * file_size,const std::function<void (uint64_t parsed_size)> & progress_callback)49 util::Status ReadTraceUsingRead(
50     TraceProcessor* tp,
51     int fd,
52     uint64_t* file_size,
53     const std::function<void(uint64_t parsed_size)>& progress_callback) {
54   // Load the trace in chunks using ordinary read().
55   for (int i = 0;; i++) {
56     if (progress_callback && i % 128 == 0)
57       progress_callback(*file_size);
58 
59     TraceBlob blob = TraceBlob::Allocate(kChunkSize);
60     auto rsize = base::Read(fd, blob.data(), blob.size());
61     if (rsize == 0)
62       break;
63 
64     if (rsize < 0) {
65       return util::ErrStatus("Reading trace file failed (errno: %d, %s)", errno,
66                              strerror(errno));
67     }
68 
69     *file_size += static_cast<uint64_t>(rsize);
70     TraceBlobView blob_view(std::move(blob), 0, static_cast<size_t>(rsize));
71     RETURN_IF_ERROR(tp->Parse(std::move(blob_view)));
72   }
73   return util::OkStatus();
74 }
75 }  // namespace
76 
ReadTraceUnfinalized(TraceProcessor * tp,const char * filename,const std::function<void (uint64_t parsed_size)> & progress_callback)77 util::Status ReadTraceUnfinalized(
78     TraceProcessor* tp,
79     const char* filename,
80     const std::function<void(uint64_t parsed_size)>& progress_callback) {
81   base::ScopedFile fd(base::OpenFile(filename, O_RDONLY));
82   if (!fd)
83     return util::ErrStatus("Could not open trace file (path: %s)", filename);
84 
85   uint64_t bytes_read = 0;
86 
87 #if TRACE_PROCESSOR_HAS_MMAP()
88   char* no_mmap = getenv("TRACE_PROCESSOR_NO_MMAP");
89   uint64_t whole_size_64 = static_cast<uint64_t>(lseek(*fd, 0, SEEK_END));
90   lseek(*fd, 0, SEEK_SET);
91   bool use_mmap = !no_mmap || *no_mmap != '1';
92   if (sizeof(size_t) < 8 && whole_size_64 > 2147483648ULL)
93     use_mmap = false;  // Cannot use mmap on 32-bit systems for files > 2GB.
94 
95   if (use_mmap) {
96     const size_t whole_size = static_cast<size_t>(whole_size_64);
97     void* file_mm = mmap(nullptr, whole_size, PROT_READ, MAP_PRIVATE, *fd, 0);
98     if (file_mm != MAP_FAILED) {
99       TraceBlobView whole_mmap(TraceBlob::FromMmap(file_mm, whole_size));
100       // Parse the file in chunks so we get some status update on stdio.
101       static constexpr size_t kMmapChunkSize = 128ul * 1024 * 1024;
102       while (bytes_read < whole_size_64) {
103         progress_callback(bytes_read);
104         const size_t bytes_read_z = static_cast<size_t>(bytes_read);
105         size_t slice_size = std::min(whole_size - bytes_read_z, kMmapChunkSize);
106         TraceBlobView slice = whole_mmap.slice_off(bytes_read_z, slice_size);
107         RETURN_IF_ERROR(tp->Parse(std::move(slice)));
108         bytes_read += slice_size;
109       }  // while (slices)
110     }    // if (!MAP_FAILED)
111   }      // if (use_mmap)
112   if (bytes_read == 0)
113     PERFETTO_LOG("Cannot use mmap on this system. Falling back on read()");
114 #endif  // TRACE_PROCESSOR_HAS_MMAP()
115   if (bytes_read == 0) {
116     RETURN_IF_ERROR(
117         ReadTraceUsingRead(tp, *fd, &bytes_read, progress_callback));
118   }
119   tp->SetCurrentTraceName(filename);
120 
121   if (progress_callback)
122     progress_callback(bytes_read);
123   return util::OkStatus();
124 }
125 }  // namespace trace_processor
126 }  // namespace perfetto
127