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