]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/cpp/src/plasma/common.h
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / cpp / src / plasma / common.h
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17
18 #pragma once
19
20 #include <stddef.h>
21
22 #include <cstring>
23 #include <memory>
24 #include <string>
25 // TODO(pcm): Convert getopt and sscanf in the store to use more idiomatic C++
26 // and get rid of the next three lines:
27 #ifndef __STDC_FORMAT_MACROS
28 #define __STDC_FORMAT_MACROS
29 #endif
30 #include <unordered_map>
31
32 #include "plasma/compat.h"
33
34 #include "arrow/status.h"
35 #ifdef PLASMA_CUDA
36 #include "arrow/gpu/cuda_api.h"
37 #endif
38
39 namespace plasma {
40
41 enum class ObjectLocation : int32_t { Local, Remote, NotFound };
42
43 enum class PlasmaErrorCode : int8_t {
44 PlasmaObjectExists = 1,
45 PlasmaObjectNotFound = 2,
46 PlasmaStoreFull = 3,
47 PlasmaObjectAlreadySealed = 4,
48 };
49
50 ARROW_EXPORT arrow::Status MakePlasmaError(PlasmaErrorCode code, std::string message);
51 /// Return true iff the status indicates an already existing Plasma object.
52 ARROW_EXPORT bool IsPlasmaObjectExists(const arrow::Status& status);
53 /// Return true iff the status indicates a nonexistent Plasma object.
54 ARROW_EXPORT bool IsPlasmaObjectNotFound(const arrow::Status& status);
55 /// Return true iff the status indicates an already sealed Plasma object.
56 ARROW_EXPORT bool IsPlasmaObjectAlreadySealed(const arrow::Status& status);
57 /// Return true iff the status indicates the Plasma store reached its capacity limit.
58 ARROW_EXPORT bool IsPlasmaStoreFull(const arrow::Status& status);
59
60 constexpr int64_t kUniqueIDSize = 20;
61
62 class ARROW_EXPORT UniqueID {
63 public:
64 static UniqueID from_binary(const std::string& binary);
65 bool operator==(const UniqueID& rhs) const;
66 const uint8_t* data() const;
67 uint8_t* mutable_data();
68 std::string binary() const;
69 std::string hex() const;
70 size_t hash() const;
71 static int64_t size() { return kUniqueIDSize; }
72
73 private:
74 uint8_t id_[kUniqueIDSize];
75 };
76
77 static_assert(std::is_pod<UniqueID>::value, "UniqueID must be plain old data");
78
79 typedef UniqueID ObjectID;
80
81 /// Size of object hash digests.
82 constexpr int64_t kDigestSize = sizeof(uint64_t);
83
84 enum class ObjectState : int {
85 /// Object was created but not sealed in the local Plasma Store.
86 PLASMA_CREATED = 1,
87 /// Object is sealed and stored in the local Plasma Store.
88 PLASMA_SEALED = 2,
89 /// Object is evicted to external store.
90 PLASMA_EVICTED = 3,
91 };
92
93 namespace internal {
94
95 struct CudaIpcPlaceholder {};
96
97 } // namespace internal
98
99 /// This type is used by the Plasma store. It is here because it is exposed to
100 /// the eviction policy.
101 struct ObjectTableEntry {
102 ObjectTableEntry();
103
104 ~ObjectTableEntry();
105
106 /// Memory mapped file containing the object.
107 int fd;
108 /// Device number.
109 int device_num;
110 /// Size of the underlying map.
111 int64_t map_size;
112 /// Offset from the base of the mmap.
113 ptrdiff_t offset;
114 /// Pointer to the object data. Needed to free the object.
115 uint8_t* pointer;
116 /// Size of the object in bytes.
117 int64_t data_size;
118 /// Size of the object metadata in bytes.
119 int64_t metadata_size;
120 /// Number of clients currently using this object.
121 int ref_count;
122 /// Unix epoch of when this object was created.
123 int64_t create_time;
124 /// How long creation of this object took.
125 int64_t construct_duration;
126
127 /// The state of the object, e.g., whether it is open or sealed.
128 ObjectState state;
129 /// The digest of the object. Used to see if two objects are the same.
130 unsigned char digest[kDigestSize];
131
132 #ifdef PLASMA_CUDA
133 /// IPC GPU handle to share with clients.
134 std::shared_ptr<::arrow::cuda::CudaIpcMemHandle> ipc_handle;
135 #else
136 std::shared_ptr<internal::CudaIpcPlaceholder> ipc_handle;
137 #endif
138 };
139
140 /// Mapping from ObjectIDs to information about the object.
141 typedef std::unordered_map<ObjectID, std::unique_ptr<ObjectTableEntry>> ObjectTable;
142
143 /// Globally accessible reference to plasma store configuration.
144 /// TODO(pcm): This can be avoided with some refactoring of existing code
145 /// by making it possible to pass a context object through dlmalloc.
146 struct PlasmaStoreInfo;
147 extern const PlasmaStoreInfo* plasma_config;
148 } // namespace plasma
149
150 namespace std {
151 template <>
152 struct hash<::plasma::UniqueID> {
153 size_t operator()(const ::plasma::UniqueID& id) const { return id.hash(); }
154 };
155 } // namespace std