1 // Copyright 2017 The Android Open Source Project
2 //
3 // This software is licensed under the terms of the GNU General Public
4 // License version 2, as published by the Free Software Foundation, and
5 // may be copied, distributed, and modified under those terms.
6 //
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License for more details.
11
12 #pragma once
13
14 #include "interface.h"
15
16 #include <memory>
17 #include <string>
18 #include <stdint.h>
19
20 struct SnapshotRamBlock {
21 const char* id;
22 int64_t startOffset;
23 uint8_t* hostPtr;
24 int64_t totalSize;
25 int32_t pageSize;
26 uint32_t flags;
27 std::string path;
28 bool readonly;
29 bool needRestoreFromRamFile;
30 };
31
32 namespace android {
33 namespace snapshot {
34
35 class ITextureSaver;
36 class TextureSaver;
37 class ITextureLoader;
38 class TextureLoader;
39 using ITextureSaverPtr = std::shared_ptr<ITextureSaver>;
40 using ITextureLoaderPtr = std::shared_ptr<ITextureLoader>;
41 using ITextureLoaderWPtr = std::weak_ptr<ITextureLoader>;
42
43 // Taken from exec.c, these #defines
44 // are for the |flags| field in SnapshotRamBlock.
45 #define SNAPSHOT_RAM_MAPPED_SHARED (1 << 1)
46 #define SNAPSHOT_RAM_MAPPED (1 << 3)
47 #define SNAPSHOT_RAM_USER_BACKED (1 << 4)
48
49 using RamBlock = ::SnapshotRamBlock;
50
51 enum class IndexFlags {
52 Empty = 0,
53 CompressedPages = 0x01,
54 SeparateBackingStore = 0x02,
55 };
56
57 enum class OperationStatus {
58 NotStarted = SNAPSHOT_STATUS_NOT_STARTED,
59 Ok = SNAPSHOT_STATUS_OK,
60 Error = SNAPSHOT_STATUS_ERROR,
61 Canceled = SNAPSHOT_STATUS_CANCELED,
62 };
63
64 enum class FailureReason {
65 Empty = 0,
66
67 CorruptedData,
68 NoSnapshotPb,
69 BadSnapshotPb,
70 IncompatibleVersion,
71 NoRamFile,
72 NoTexturesFile,
73 SnapshotsNotSupported,
74 Canceled,
75 Tombstone,
76
77 UnrecoverableErrorLimit = 10000,
78
79 NoSnapshotInImage,
80 ConfigMismatchHostHypervisor,
81 ConfigMismatchHostGpu,
82 ConfigMismatchRenderer,
83 ConfigMismatchFeatures,
84 ConfigMismatchAvd,
85 SystemImageChanged,
86
87 ValidationErrorLimit = 20000,
88
89 InternalError,
90 EmulationEngineFailed,
91 RamFailed,
92 TexturesFailed,
93 AdbOffline,
94 OutOfDiskSpace,
95
96 InProgressLimit = 30000,
97 };
98
99 FailureReason errnoToFailure(int error);
100 const char* failureReasonToString(FailureReason failure,
101 SnapshotOperation op);
102
103 template <class Operation>
isComplete(const Operation & op)104 bool isComplete(const Operation& op) {
105 return op.status() != OperationStatus::NotStarted;
106 }
107
108 bool isBufferZeroed(const void* ptr, int32_t size);
109
110 #if defined(__APPLE__) && defined(__aarch64__)
111 constexpr int32_t kDefaultPageSize = 16384;
112 #else
113 constexpr int32_t kDefaultPageSize = 4096;
114 #endif
115
116 constexpr int32_t kCancelTimeoutMs = 15000;
117
118 // Size in bytes of largest in-flight RAM region for decommitting.
119 constexpr uint64_t kDecommitChunkSize = 4096 * 4096; // 16 MB
120 constexpr const char* kDefaultBootSnapshot = "default_boot";
121 constexpr const char* kRamFileName = "ram.bin";
122 constexpr const char* kTexturesFileName = "textures.bin";
123 constexpr const char* kMappedRamFileName = "ram.img";
124 constexpr const char* kMappedRamFileDirtyName = "ram.img.dirty";
125
126 constexpr const char* kSnapshotProtobufName = "snapshot.pb";
127
128 void resetSnapshotLiveness();
129 bool isSnapshotAlive();
130
131 #ifdef AEMU_MIN
132 #define SNAPSHOT_METRICS 0
133 #else
134 #define SNAPSHOT_METRICS 1
135 #endif
136
137 enum SnapshotterOperation {
138 SNAPSHOTTER_OPERATION_SAVE,
139 SNAPSHOTTER_OPERATION_LOAD,
140 };
141
142 enum SnapshotterStage {
143 SNAPSHOTTER_STAGE_START,
144 SNAPSHOTTER_STAGE_END,
145 };
146
147 } // namespace snapshot
148 } // namespace android
149