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