1 /* 2 * Copyright (c) 2021-2023 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_DFX_HPROF_FILE_STREAM_H 17 #define ECMASCRIPT_DFX_HPROF_FILE_STREAM_H 18 19 #include <cstdint> 20 #include <fstream> 21 #include <iosfwd> 22 #include <utility> 23 #include <vector> 24 25 #include "ecmascript/dfx/hprof/stream.h" 26 #include "ecmascript/mem/mem_common.h" 27 28 namespace panda::ecmascript { 29 class FileStream : public Stream { 30 public: 31 explicit FileStream(const std::string &fileName); 32 ~FileStream() override; 33 34 void EndOfStream() override; 35 36 // Get chunk's size GetSize()37 int GetSize() override 38 { 39 const static int fileChunkSize = static_cast<int>(1_MB); 40 return fileChunkSize; 41 } 42 43 // Writes the chunk of data into the stream 44 bool WriteChunk(char* data, int32_t size) override; WriteBinBlock(char * data,int32_t size)45 bool WriteBinBlock(char *data, int32_t size) override 46 { 47 return WriteChunk(data, size); 48 } 49 bool Good() override; UpdateHeapStats(HeapStat * data,int32_t count)50 void UpdateHeapStats([[maybe_unused]] HeapStat* data, [[maybe_unused]] int32_t count) override 51 { 52 } UpdateLastSeenObjectId(int32_t lastSeenObjectId,int64_t timeStampUs)53 void UpdateLastSeenObjectId([[maybe_unused]]int32_t lastSeenObjectId, [[maybe_unused]]int64_t timeStampUs) override 54 { 55 } 56 57 private: 58 void Initialize(const std::string &fileName); 59 std::pair<bool, std::string> FilePathValid(const std::string &fileName); 60 61 std::fstream fileStream_; 62 }; 63 64 class FileDescriptorStream : public Stream { 65 public: 66 explicit FileDescriptorStream(int32_t fd); 67 ~FileDescriptorStream() override; 68 69 void EndOfStream() override; 70 71 // Get chunk's size GetSize()72 int GetSize() override 73 { 74 const static int fileChunkSize = static_cast<int>(1_MB); 75 return fileChunkSize; 76 } 77 78 // Writes the chunk of data into the stream 79 bool WriteChunk(char *data, int32_t size) override; 80 bool WriteBinBlock(char *data, int32_t size) override; 81 bool Good() override; UpdateHeapStats(HeapStat * data,int32_t count)82 void UpdateHeapStats([[maybe_unused]] HeapStat* data, [[maybe_unused]] int32_t count) override 83 { 84 } UpdateLastSeenObjectId(int32_t lastSeenObjectId,int64_t timeStampUs)85 void UpdateLastSeenObjectId([[maybe_unused]]int32_t lastSeenObjectId, [[maybe_unused]]int64_t timeStampUs) override 86 { 87 } 88 89 private: 90 int32_t fd_; 91 }; 92 93 class BinaryWriter { 94 public: 95 explicit BinaryWriter(Stream *stream); 96 ~BinaryWriter(); 97 98 void WriteBinBlock(char *block, int size); 99 100 void WriteUInt64(uint64_t num); 101 102 void WriteUInt32(uint32_t num); 103 104 void WriteUInt16(uint16_t num); 105 106 void WriteUInt8(uint8_t num); 107 108 void EndOfWriteBinBlock(); 109 GetCurrentFileSize()110 int GetCurrentFileSize() 111 { 112 return fileSize_; 113 } 114 115 private: 116 void MaybeWriteBinBlock(); 117 118 void WriteBinBlock(); 119 IncreaseFileIndex(int size)120 void IncreaseFileIndex(int size) 121 { 122 current_ += size; 123 fileSize_ += size; 124 } 125 126 Stream *stream_ {nullptr}; 127 int chunkSize_ {0}; 128 std::vector<char> chunk_; 129 int current_ {0}; 130 int fileSize_ {0}; 131 }; 132 } // namespace panda::ecmascript::tooling 133 134 #endif // ECMASCRIPT_DFX_HPROF_FILE_STREAM_H 135