• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
24 #include "ecmascript/dfx/hprof/stream.h"
25 #include "ecmascript/mem/mem_common.h"
26 
27 namespace panda::ecmascript {
28 class FileStream : public Stream {
29 public:
30     explicit FileStream(const std::string &fileName);
31     ~FileStream() override;
32 
33     void EndOfStream() override;
34 
35     // Get chunk's size
GetSize()36     int GetSize() override
37     {
38         const static int fileChunkSize = static_cast<int>(10_KB);
39         return fileChunkSize;
40     }
41 
42     // Writes the chunk of data into the stream
43     bool WriteChunk(char* data, int32_t size) override;
44     bool Good() override;
UpdateHeapStats(HeapStat * data,int32_t count)45     void UpdateHeapStats([[maybe_unused]] HeapStat* data, [[maybe_unused]] int32_t count) override
46     {
47     }
UpdateLastSeenObjectId(int32_t lastSeenObjectId,int64_t timeStampUs)48     void UpdateLastSeenObjectId([[maybe_unused]]int32_t lastSeenObjectId, [[maybe_unused]]int64_t timeStampUs) override
49     {
50     }
51 
52 private:
53     void Initialize(const std::string &fileName);
54     std::pair<bool, std::string> FilePathValid(const std::string &fileName);
55 
56     std::fstream fileStream_;
57 };
58 
59 class FileDescriptorStream : public Stream {
60 public:
FileDescriptorStream(int32_t fd)61     explicit FileDescriptorStream(int32_t fd): fd_(fd) {}
62     ~FileDescriptorStream() override = default;
63 
64     void EndOfStream() override;
65 
66     // Get chunk's size
GetSize()67     int GetSize() override
68     {
69         const static int fileChunkSize = static_cast<int>(10_KB);
70         return fileChunkSize;
71     }
72 
73     // Writes the chunk of data into the stream
74     bool WriteChunk(char *data, int32_t size) override;
75     bool Good() override;
UpdateHeapStats(HeapStat * data,int32_t count)76     void UpdateHeapStats([[maybe_unused]] HeapStat* data, [[maybe_unused]] int32_t count) override
77     {
78     }
UpdateLastSeenObjectId(int32_t lastSeenObjectId,int64_t timeStampUs)79     void UpdateLastSeenObjectId([[maybe_unused]]int32_t lastSeenObjectId, [[maybe_unused]]int64_t timeStampUs) override
80     {
81     }
82 
83 private:
84     int32_t fd_;
85 };
86 }  // namespace panda::ecmascript::tooling
87 
88 #endif  // ECMASCRIPT_DFX_HPROF_FILE_STREAM_H
89