• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PROFILER_H
17 #define ECMASCRIPT_HPROF_HEAP_PROFILER_H
18 
19 #include "ecmascript/dfx/hprof/heap_profiler_interface.h"
20 #include "ecmascript/dfx/hprof/heap_snapshot_json_serializer.h"
21 #include "ecmascript/dfx/hprof/heap_tracker.h"
22 #include "ecmascript/ecma_macros.h"
23 #include "ecmascript/mem/c_containers.h"
24 #include "ecmascript/dfx/hprof/file_stream.h"
25 #include "ecmascript/dfx/hprof/progress.h"
26 
27 #include "os/mem.h"
28 
29 namespace panda::ecmascript {
30 class HeapSnapshot;
31 class EcmaVM;
32 
33 class HeapProfiler : public HeapProfilerInterface {
34 public:
35     NO_MOVE_SEMANTIC(HeapProfiler);
36     NO_COPY_SEMANTIC(HeapProfiler);
37     explicit HeapProfiler(const EcmaVM *vm);
38     ~HeapProfiler() override;
39 
40     enum class SampleType { ONE_SHOT, REAL_TIME };
41     /**
42      * dump the specific snapshot in target format
43      */
44     bool DumpHeapSnapshot(DumpFormat dumpFormat, Stream *stream, Progress *progress = nullptr,
45                           bool isVmMode = true, bool isPrivate = false) override;
46 
47     void AddSnapshot(HeapSnapshot *snapshot);
48 
49     bool StartHeapTracking(double timeInterval, bool isVmMode = true, Stream *stream = nullptr,
50                            bool traceAllocation = false, bool newThread = true) override;
51     bool StopHeapTracking(Stream *stream, Progress *progress = nullptr, bool newThread = true) override;
52     bool UpdateHeapTracking(Stream *stream) override;
GetChunk()53     Chunk *GetChunk() const
54     {
55         return const_cast<Chunk *>(&chunk_);
56     }
57 private:
58     /**
59      * trigger full gc to make sure no unreachable objects in heap
60      */
61     bool ForceFullGC(const EcmaVM *vm);
62 
63     /**
64      * make a new heap snapshot and put it into a container eg, vector
65      */
66     HeapSnapshot *MakeHeapSnapshot(SampleType sampleType, bool isVmMode = true,
67                                    bool isPrivate = false, bool traceAllocation = false);
68     std::string GenDumpFileName(DumpFormat dumpFormat);
69     CString GetTimeStamp();
70     void UpdateHeapObjects(HeapSnapshot *snapshot);
71     void ClearSnapshot();
72 
73     const size_t MAX_NUM_HPROF = 5;  // ~10MB
74     const EcmaVM *vm_;
75     CVector<HeapSnapshot *> hprofs_;
76     HeapSnapshotJSONSerializer *jsonSerializer_ {nullptr};
77     std::unique_ptr<HeapTracker> heapTracker_;
78     Chunk chunk_;
79 };
80 }  // namespace panda::ecmascript
81 #endif  // ECMASCRIPT_HPROF_HEAP_PROFILER_H
82