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