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