1 /*
2 * Copyright (C) 2017 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 #pragma once
18
19 #include "interface.h"
20
21 #include "aemu/base/files/Stream.h"
22
23 #include <memory>
24 #include <string>
25 #include <stdint.h>
26
27 struct SnapshotRamBlock {
28 std::string id;
29 int64_t startOffset;
30 uint8_t* hostPtr;
31 int64_t totalSize;
32 int32_t pageSize;
33 uint32_t flags;
34 std::string path;
35 bool readonly;
36 bool needRestoreFromRamFile;
37 };
38
39 namespace android {
40 namespace snapshot {
41
42 class ITextureSaver;
43 class TextureSaver;
44 class ITextureLoader;
45 class TextureLoader;
46 using ITextureSaverPtr = std::shared_ptr<ITextureSaver>;
47 using ITextureLoaderPtr = std::shared_ptr<ITextureLoader>;
48 using ITextureLoaderWPtr = std::weak_ptr<ITextureLoader>;
49
50 struct SnapshotSaveStream {
51 android::base::Stream* stream = nullptr;
52 ITextureSaverPtr textureSaver;
53 };
54
55 struct SnapshotLoadStream {
56 android::base::Stream* stream = nullptr;
57 ITextureLoaderPtr textureLoader;
58 };
59
60 // Taken from exec.c, these #defines
61 // are for the |flags| field in SnapshotRamBlock.
62 #define SNAPSHOT_RAM_MAPPED_SHARED (1 << 1)
63 #define SNAPSHOT_RAM_MAPPED (1 << 3)
64 #define SNAPSHOT_RAM_USER_BACKED (1 << 4)
65
66 using RamBlock = ::SnapshotRamBlock;
67
68 enum class IndexFlags {
69 Empty = 0,
70 CompressedPages = 0x01,
71 SeparateBackingStore = 0x02,
72 };
73
74 enum class OperationStatus {
75 NotStarted = SNAPSHOT_STATUS_NOT_STARTED,
76 Ok = SNAPSHOT_STATUS_OK,
77 Error = SNAPSHOT_STATUS_ERROR,
78 Canceled = SNAPSHOT_STATUS_CANCELED,
79 };
80
81 enum class FailureReason {
82 Empty = 0,
83
84 CorruptedData,
85 NoSnapshotPb,
86 BadSnapshotPb,
87 IncompatibleVersion,
88 NoRamFile,
89 NoTexturesFile,
90 SnapshotsNotSupported,
91 Canceled,
92 Tombstone,
93
94 UnrecoverableErrorLimit = 10000,
95
96 NoSnapshotInImage,
97 ConfigMismatchHostHypervisor,
98 ConfigMismatchHostGpu,
99 ConfigMismatchRenderer,
100 ConfigMismatchFeatures,
101 ConfigMismatchAvd,
102 SystemImageChanged,
103
104 ValidationErrorLimit = 20000,
105
106 InternalError,
107 EmulationEngineFailed,
108 RamFailed,
109 TexturesFailed,
110 AdbOffline,
111 OutOfDiskSpace,
112
113 InProgressLimit = 30000,
114 };
115
116 FailureReason errnoToFailure(int error);
117 const char* failureReasonToString(FailureReason failure,
118 SnapshotOperation op);
119
120 template <class Operation>
isComplete(const Operation & op)121 bool isComplete(const Operation& op) {
122 return op.status() != OperationStatus::NotStarted;
123 }
124
125 bool isBufferZeroed(const void* ptr, int32_t size);
126
127 #if defined(__APPLE__) && defined(__aarch64__)
128 constexpr int32_t kDefaultPageSize = 16384;
129 #else
130 constexpr int32_t kDefaultPageSize = 4096;
131 #endif
132
133 constexpr int32_t kCancelTimeoutMs = 15000;
134
135 // Size in bytes of largest in-flight RAM region for decommitting.
136 constexpr uint64_t kDecommitChunkSize = 4096 * 4096; // 16 MB
137 constexpr const char* kDefaultBootSnapshot = "default_boot";
138 constexpr const char* kRamFileName = "ram.bin";
139 constexpr const char* kTexturesFileName = "textures.bin";
140 constexpr const char* kMappedRamFileName = "ram.img";
141 constexpr const char* kMappedRamFileDirtyName = "ram.img.dirty";
142
143 constexpr const char* kSnapshotProtobufName = "snapshot.pb";
144
145 void resetSnapshotLiveness();
146 bool isSnapshotAlive();
147
148 #ifdef AEMU_MIN
149 #define SNAPSHOT_METRICS 0
150 #else
151 #define SNAPSHOT_METRICS 1
152 #endif
153
154 enum SnapshotterOperation {
155 SNAPSHOTTER_OPERATION_SAVE,
156 SNAPSHOTTER_OPERATION_LOAD,
157 };
158
159 enum SnapshotterStage {
160 SNAPSHOTTER_STAGE_START,
161 SNAPSHOTTER_STAGE_END,
162 };
163
164 } // namespace snapshot
165 } // namespace android
166