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_HPROF_HEAP_SNAPSHOT_SERIALIZER_H 17 #define ECMASCRIPT_HPROF_HEAP_SNAPSHOT_SERIALIZER_H 18 19 #include <fstream> 20 #include <sstream> 21 22 #include "ecmascript/mem/c_string.h" 23 #include "os/mem.h" 24 25 namespace panda::ecmascript { 26 using fstream = std::fstream; 27 using stringstream = std::stringstream; 28 29 class HeapSnapShot; 30 31 class HeapSnapShotJSONSerializer { 32 public: 33 explicit HeapSnapShotJSONSerializer() = default; 34 ~HeapSnapShotJSONSerializer() = default; 35 NO_MOVE_SEMANTIC(HeapSnapShotJSONSerializer); 36 NO_COPY_SEMANTIC(HeapSnapShotJSONSerializer); 37 bool Serialize(HeapSnapShot *snapShot, const CString &fileName); 38 39 private: 40 void SerializeSnapShotHeader(); 41 void SerializeNodes(); 42 void SerializeEdges(); 43 void SerializeTraceFunctionInfo(); 44 void SerializeTraceTree(); 45 void SerializeSamples(); 46 void SerializeLocations(); 47 void SerializeStringTable(); 48 void SerializerSnapShotClosure(); 49 50 void WriteJSON(const CString &fileName); 51 fstream outputStream_; 52 HeapSnapShot *snapShot_{nullptr}; 53 stringstream stringBuffer_; 54 }; 55 } // namespace panda::ecmascript 56 #endif // ECMASCRIPT_HPROF_HEAP_SNAPSHOT_SERIALIZER_H 57