• 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 #ifndef INCLUDE_PERFETTO_TRACE_PROCESSOR_TRACE_BLOB_H_
18 #define INCLUDE_PERFETTO_TRACE_PROCESSOR_TRACE_BLOB_H_
19 
20 #include <stddef.h>
21 #include <stdint.h>
22 
23 #include <memory>
24 #include <utility>
25 
26 #include "perfetto/base/build_config.h"
27 #include "perfetto/base/export.h"
28 #include "perfetto/base/logging.h"
29 #include "perfetto/trace_processor/ref_counted.h"
30 
31 // TODO(primiano): implement file mmap on Windows.
32 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
33     PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE) || \
34     PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
35 #define TRACE_PROCESSOR_HAS_MMAP() 1
36 #else
37 #define TRACE_PROCESSOR_HAS_MMAP() 0
38 #endif
39 
40 namespace perfetto {
41 namespace trace_processor {
42 
43 // TraceBlob is a move-only buffer that owns a portion of memory containing
44 // trace data (not necessarily aligned at trace packet boundaries). Think of
45 // this as a std::pair<std::unique_ptr<uint8_t[]>, size_t>.
46 // TraceBlob can be instantiated and moved around when it's written/altered
47 // by the initial ingestion stages. In this mode, no refcounting is used
48 // (i.e. refcount_ is always == 0).
49 // When it comes to parsing stages, the TraceBlob can be turned into a read-only
50 // object wrapping it in a TraceBlobView. Once wrapped in a TraceBlobView, the
51 // TraceBlob becomes refcounted (TBV handles the inc/dec of refcount).
52 // TraceBlobView allows to have multiple instances pointing at (different
53 // sub-offsets of) the same TraceBlob.
54 // The neat thing about TraceBlob is that it deals transparently with owned
55 // memory (in the case of Allocate and TakeOwnership) and memory-mapped memory.
56 class PERFETTO_EXPORT TraceBlob : public RefCounted {
57  public:
58   static TraceBlob Allocate(size_t size);
59   static TraceBlob CopyFrom(const void*, size_t size);
60   static TraceBlob TakeOwnership(std::unique_ptr<uint8_t[]>, size_t size);
61 
62   // Takes ownership of the mmap region. Will call munmap() on destruction.
63   static TraceBlob FromMmap(void* data, size_t size);
64 
65   ~TraceBlob();
66 
67   // Allow move.
TraceBlob(TraceBlob && other)68   TraceBlob(TraceBlob&& other) noexcept { *this = std::move(other); }
69   TraceBlob& operator=(TraceBlob&&) noexcept;
70 
71   // Disallow copy.
72   TraceBlob(const TraceBlob&) = delete;
73   TraceBlob& operator=(const TraceBlob&) = delete;
74 
data()75   uint8_t* data() const { return data_; }
size()76   size_t size() const { return size_; }
77 
78  private:
79   enum class Ownership { kNull = 0, kHeapBuf, kMmaped };
80 
TraceBlob(Ownership ownership,uint8_t * data,size_t size)81   TraceBlob(Ownership ownership, uint8_t* data, size_t size)
82       : ownership_(ownership), data_(data), size_(size) {}
83 
84   Ownership ownership_ = Ownership::kNull;
85   uint8_t* data_ = nullptr;
86   size_t size_ = 0;
87 };
88 
89 }  // namespace trace_processor
90 }  // namespace perfetto
91 
92 #endif  // INCLUDE_PERFETTO_TRACE_PROCESSOR_TRACE_BLOB_H_
93