1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef ECMASCRIPT_SNAPSHOT_MEM_SNAPSHOT_PROCESSOR_H 17 #define ECMASCRIPT_SNAPSHOT_MEM_SNAPSHOT_PROCESSOR_H 18 19 #include <iostream> 20 #include <fstream> 21 #include <sstream> 22 23 #include "ecmascript/snapshot/mem/encode_bit.h" 24 #include "ecmascript/jspandafile/method_literal.h" 25 #include "ecmascript/js_tagged_value.h" 26 #include "ecmascript/mem/object_xray.h" 27 28 #include "libpandabase/macros.h" 29 30 namespace panda::ecmascript { 31 class EcmaVM; 32 class JSPandaFile; 33 class AOTFileManager; 34 35 enum class SnapshotType { 36 VM_ROOT, 37 BUILTINS, 38 AI 39 }; 40 41 using ObjectEncode = std::pair<uint64_t, ecmascript::EncodeBit>; 42 43 class SnapshotProcessor final { 44 public: 45 explicit SnapshotProcessor(EcmaVM *vm, const CString &fileName = "") vm_(vm)46 : vm_(vm), objXRay_(vm), fileName_(fileName) {} 47 ~SnapshotProcessor(); 48 49 void Initialize(); 50 void StopAllocate(); 51 void WriteObjectToFile(std::fstream &write); 52 std::vector<uint32_t> StatisticsObjectSize(); 53 void ProcessObjectQueue(CQueue<TaggedObject *> *queue, std::unordered_map<uint64_t, ObjectEncode> *data); 54 void SerializeObject(TaggedObject *objectHeader, CQueue<TaggedObject *> *queue, 55 std::unordered_map<uint64_t, ObjectEncode> *data); 56 void Relocate(SnapshotType type, const JSPandaFile *jsPandaFile, 57 uint64_t rootObjSize); 58 void RelocateSpaceObject(const JSPandaFile *jsPandaFile, Space* space, SnapshotType type, MethodLiteral* methods, 59 size_t methodNums, size_t rootObjSize); 60 void SerializePandaFileMethod(); 61 EncodeBit EncodeTaggedObject(TaggedObject *objectHeader, CQueue<TaggedObject *> *queue, 62 std::unordered_map<uint64_t, ObjectEncode> *data); 63 EncodeBit GetObjectEncode(JSTaggedValue object, CQueue<TaggedObject *> *queue, 64 std::unordered_map<uint64_t, ObjectEncode> *data); 65 void EncodeTaggedObjectRange(ObjectSlot start, ObjectSlot end, CQueue<TaggedObject *> *queue, 66 std::unordered_map<uint64_t, ObjectEncode> *data); 67 void DeserializeObjectExcludeString(uintptr_t oldSpaceBegin, size_t oldSpaceObjSize, size_t nonMovableObjSize, 68 size_t machineCodeObjSize, size_t snapshotObjSize, size_t hugeSpaceObjSize); 69 void DeserializeString(uintptr_t stringBegin, uintptr_t stringEnd); 70 SetProgramSerializeStart()71 void SetProgramSerializeStart() 72 { 73 programSerialize_ = true; 74 } 75 SetBuiltinsSerializeStart()76 void SetBuiltinsSerializeStart() 77 { 78 builtinsSerialize_ = true; 79 } 80 SetBuiltinsDeserializeStart()81 void SetBuiltinsDeserializeStart() 82 { 83 builtinsDeserialize_ = true; 84 } 85 GetStringVector()86 const CVector<uintptr_t> GetStringVector() const 87 { 88 return stringVector_; 89 } 90 GetOldLocalSpace()91 LocalSpace* GetOldLocalSpace() const 92 { 93 return oldLocalSpace_; 94 } 95 96 size_t GetNativeTableSize() const; 97 98 private: GetMarkGCBitSetSize()99 size_t GetMarkGCBitSetSize() const 100 { 101 return GCBitset::SizeOfGCBitset(DEFAULT_REGION_SIZE - 102 AlignUp(sizeof(Region), static_cast<size_t>(MemAlignment::MEM_ALIGN_REGION))); 103 } 104 105 bool VisitObjectBodyWithRep(TaggedObject *root, ObjectSlot slot, uintptr_t obj, int index, VisitObjectArea area); 106 void SetObjectEncodeField(uintptr_t obj, size_t offset, uint64_t value); 107 108 EncodeBit SerializeObjectHeader(TaggedObject *objectHeader, size_t objectType, CQueue<TaggedObject *> *queue, 109 std::unordered_map<uint64_t, ObjectEncode> *data); 110 uint64_t SerializeTaggedField(JSTaggedType *tagged, CQueue<TaggedObject *> *queue, 111 std::unordered_map<uint64_t, ObjectEncode> *data); 112 void DeserializeField(TaggedObject *objectHeader); 113 void DeserializeTaggedField(uint64_t *value, TaggedObject *root); 114 void DeserializeNativePointer(uint64_t *value); 115 void DeserializeClassWord(TaggedObject *object); 116 void DeserializePandaMethod(uintptr_t begin, uintptr_t end, MethodLiteral *methods, 117 size_t &methodNums, size_t &others); 118 void DeserializeSpaceObject(uintptr_t beginAddr, Space* space, size_t spaceObjSize); 119 void DeserializeHugeSpaceObject(uintptr_t beginAddr, HugeObjectSpace* space, size_t hugeSpaceObjSize); 120 void HandleRootObject(SnapshotType type, uintptr_t rootObjectAddr, size_t objType, size_t &constSpecialIndex); 121 122 EncodeBit NativePointerToEncodeBit(void *nativePointer); 123 size_t SearchNativeMethodIndex(void *nativePointer); 124 uintptr_t TaggedObjectEncodeBitToAddr(EncodeBit taggedBit); 125 void WriteSpaceObjectToFile(Space* space, std::fstream &write); 126 void WriteHugeObjectToFile(HugeObjectSpace* space, std::fstream &writer); 127 uint32_t StatisticsSpaceObjectSize(Space* space); 128 uint32_t StatisticsHugeObjectSize(HugeObjectSpace* space); 129 uintptr_t AllocateObjectToLocalSpace(Space *space, size_t objectSize); 130 131 EcmaVM *vm_ {nullptr}; 132 LocalSpace *oldLocalSpace_ {nullptr}; 133 LocalSpace *nonMovableLocalSpace_ {nullptr}; 134 LocalSpace *machineCodeLocalSpace_ {nullptr}; 135 SnapshotSpace *snapshotLocalSpace_ {nullptr}; 136 HugeObjectSpace *hugeObjectLocalSpace_ {nullptr}; 137 ObjectXRay objXRay_; 138 bool programSerialize_ {false}; 139 bool builtinsSerialize_ {false}; 140 bool builtinsDeserialize_ {false}; 141 CVector<uintptr_t> pandaMethod_; 142 CVector<uintptr_t> stringVector_; 143 std::unordered_map<size_t, Region *> regionIndexMap_; 144 size_t regionIndex_ {0}; 145 bool isRootObjRelocate_ {false}; 146 const CString &fileName_; 147 148 NO_COPY_SEMANTIC(SnapshotProcessor); 149 NO_MOVE_SEMANTIC(SnapshotProcessor); 150 }; 151 152 class SnapshotHelper { 153 public: 154 // when snapshot serialize, huge obj size is writed to region snapshotData_ high 32 bits EncodeHugeObjectSize(uint64_t objSize)155 static inline uint64_t EncodeHugeObjectSize(uint64_t objSize) 156 { 157 return objSize << Constants::UINT_32_BITS_COUNT; 158 } 159 160 // get huge object size which is saved in region snapshotData_ high 32 bits GetHugeObjectSize(uint64_t snapshotData)161 static inline size_t GetHugeObjectSize(uint64_t snapshotData) 162 { 163 return snapshotData >> Constants::UINT_32_BITS_COUNT; 164 } 165 166 // get huge object region index which is saved in region snapshotMark_ low 32 bits GetHugeObjectRegionIndex(uint64_t snapshotData)167 static inline size_t GetHugeObjectRegionIndex(uint64_t snapshotData) 168 { 169 return snapshotData & Constants::MAX_UINT_32; 170 } 171 }; 172 } // namespace panda::ecmascript 173 174 #endif // ECMASCRIPT_SNAPSHOT_MEM_SNAPSHOT_PROCESSOR_H 175