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 #include "ecmascript/dfx/hprof/heap_tracker.h"
17 
18 #include "ecmascript/dfx/hprof/heap_snapshot.h"
19 
20 namespace panda::ecmascript {
21 static constexpr int32_t MILLI_TO_MICRO = 1000;
22 
Run()23 void HeapTrackerSample::Run()
24 {
25     while (!isInterrupt_) {
26         snapshot_->RecordSampleTime();
27         if (stream_ != nullptr) {
28             snapshot_->PushHeapStat(stream_);
29         }
30         usleep(timeInterval_ * MILLI_TO_MICRO);
31     }
32 }
33 
AllocationEvent(TaggedObject *address, size_t size)34 void HeapTracker::AllocationEvent(TaggedObject *address, size_t size)
35 {
36     if (snapshot_ == nullptr || address == nullptr || prevAllocation_ == address) {
37         return;
38     }
39     prevAllocation_ = address;
40     Node *node = snapshot_->AddNode(address, size);
41     if (node != nullptr && snapshot_->trackAllocations()) {
42         int selfSize = static_cast<int>(node->GetSelfSize());
43         int sequenceId = static_cast<int>(node->GetId());
44         int traceId = snapshot_->AddTraceNode(sequenceId, selfSize);
45         if (traceId != -1) {
46             node->SetTraceId(static_cast<uint64_t>(traceId));
47         }
48     }
49 }
50 
MoveEvent(uintptr_t address, TaggedObject *forwardAddress, size_t size)51 void HeapTracker::MoveEvent(uintptr_t address, TaggedObject *forwardAddress, size_t size)
52 {
53     if (snapshot_ != nullptr) {
54         snapshot_->MoveNode(address, forwardAddress, size);
55     }
56 }
57 }  // namespace panda::ecmascript
58