1 /*
2 * Copyright (c) 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 #include "allocation_inspector.h"
17
18 #include "ecmascript/dfx/hprof/heap_sampling.h"
19 #include "ecmascript/mem/assert_scope.h"
20 #include "ecmascript/mem/heap.h"
21
22 namespace panda::ecmascript {
Step(Address object,size_t size)23 void AllocationInspector::Step([[maybe_unused]] Address object, [[maybe_unused]] size_t size)
24 {
25 #ifdef ECMASCRIPT_SUPPORT_HEAPSAMPLING
26 ASSERT(!heap_->GetJSThread()->GetGcState());
27 if (object) {
28 profiler_->ImplementSampling(object, size);
29 }
30 #endif
31 }
32
33 // Sampling follows a Poisson distribution.
34 // According to the relationship between Poisson distribution and exponential probability distribution,
35 // the stepSize follows the exponential probability distribution with parameter λ = 1/rate where rate
36 // is average bytes between samples.
37 // Let random be a uniformly distributed random number between 0 and 1, beacuse Inverse transform sampling
38 // can generate exponential probability distribution with a uniform distribution, so nextSample = (- ln random) / λ.
GetNextStepSize()39 size_t AllocationInspector::GetNextStepSize()
40 {
41 double random = base::RandomGenerator::NextDouble();
42 double stepSize = -std::log(random) * rate_;
43 return stepSize < TaggedObject::TaggedObjectSize()
44 ? TaggedObject::TaggedObjectSize()
45 : (stepSize > INT_MAX ? INT_MAX : static_cast<size_t>(stepSize));
46 }
47
AddAllocationInspector(AllocationInspector * inspector)48 void AllocationCounter::AddAllocationInspector(AllocationInspector *inspector)
49 {
50 ASSERT(inspector != nullptr);
51 size_t stepSize = inspector->GetNextStepSize();
52 size_t nextCounter = currentCounter_ + stepSize;
53 inspector_ = inspector;
54
55 ASSERT(currentCounter_ == nextCounter_);
56 nextCounter_ = nextCounter;
57 }
58
ClearAllocationInspector()59 void AllocationCounter::ClearAllocationInspector()
60 {
61 inspector_ = nullptr;
62 currentCounter_ = 0;
63 nextCounter_ = 0;
64 }
65
AdvanceAllocationInspector(size_t allocated)66 void AllocationCounter::AdvanceAllocationInspector(size_t allocated)
67 {
68 if (!IsActive()) {
69 return;
70 }
71 ASSERT(allocated < nextCounter_ - currentCounter_);
72 currentCounter_ += allocated;
73 }
74
InvokeAllocationInspector(Address object,size_t objectSize,size_t alignedObjectSize)75 void AllocationCounter::InvokeAllocationInspector(Address object, size_t objectSize, size_t alignedObjectSize)
76 {
77 if (!IsActive()) {
78 return;
79 }
80
81 ASSERT(alignedObjectSize > nextCounter_ - currentCounter_);
82 ASSERT(object != 0);
83 {
84 DISALLOW_GARBAGE_COLLECTION;
85 inspector_->Step(object, objectSize);
86 }
87 size_t nextStepSize = inspector_->GetNextStepSize();
88 // because next allocate or advance can add currentCounter_ to real allocated size,
89 // so need add alignedObjectSize here.
90 nextCounter_ = currentCounter_ + alignedObjectSize + nextStepSize;
91 }
92 } // namespace panda::ecmascript