14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#include "allocation_inspector.h"
174514f5e3Sopenharmony_ci
184514f5e3Sopenharmony_ci#include "ecmascript/base/number_helper.h"
194514f5e3Sopenharmony_ci#include "ecmascript/dfx/hprof/heap_sampling.h"
204514f5e3Sopenharmony_ci
214514f5e3Sopenharmony_cinamespace panda::ecmascript {
224514f5e3Sopenharmony_civoid AllocationInspector::Step([[maybe_unused]] Address object, [[maybe_unused]] size_t size)
234514f5e3Sopenharmony_ci{
244514f5e3Sopenharmony_ci#ifdef ECMASCRIPT_SUPPORT_HEAPSAMPLING
254514f5e3Sopenharmony_ci    ASSERT(!heap_->GetJSThread()->GetGcState());
264514f5e3Sopenharmony_ci    if (object) {
274514f5e3Sopenharmony_ci        profiler_->ImplementSampling(object, size);
284514f5e3Sopenharmony_ci    }
294514f5e3Sopenharmony_ci#endif
304514f5e3Sopenharmony_ci}
314514f5e3Sopenharmony_ci
324514f5e3Sopenharmony_ci// Sampling follows a Poisson distribution.
334514f5e3Sopenharmony_ci// According to the relationship between Poisson distribution and exponential probability distribution,
344514f5e3Sopenharmony_ci// the stepSize follows the exponential probability distribution with parameter λ = 1/rate where rate
354514f5e3Sopenharmony_ci// is average bytes between samples.
364514f5e3Sopenharmony_ci// Let random be a uniformly distributed random number between 0 and 1, beacuse Inverse transform sampling
374514f5e3Sopenharmony_ci// can generate exponential probability distribution with a uniform distribution, so nextSample = (- ln random) / λ.
384514f5e3Sopenharmony_cisize_t AllocationInspector::GetNextStepSize()
394514f5e3Sopenharmony_ci{
404514f5e3Sopenharmony_ci    double random = base::RandomGenerator::NextDouble();
414514f5e3Sopenharmony_ci    double stepSize = -std::log(random) * rate_;
424514f5e3Sopenharmony_ci    return stepSize < TaggedObject::TaggedObjectSize()
434514f5e3Sopenharmony_ci        ? TaggedObject::TaggedObjectSize()
444514f5e3Sopenharmony_ci        : (stepSize > INT_MAX ? INT_MAX : static_cast<size_t>(stepSize));
454514f5e3Sopenharmony_ci}
464514f5e3Sopenharmony_ci
474514f5e3Sopenharmony_civoid AllocationCounter::AddAllocationInspector(AllocationInspector *inspector)
484514f5e3Sopenharmony_ci{
494514f5e3Sopenharmony_ci    ASSERT(inspector != nullptr);
504514f5e3Sopenharmony_ci    size_t stepSize = inspector->GetNextStepSize();
514514f5e3Sopenharmony_ci    size_t nextCounter = currentCounter_ + stepSize;
524514f5e3Sopenharmony_ci    inspector_ = inspector;
534514f5e3Sopenharmony_ci
544514f5e3Sopenharmony_ci    ASSERT(currentCounter_ == nextCounter_);
554514f5e3Sopenharmony_ci    nextCounter_ = nextCounter;
564514f5e3Sopenharmony_ci}
574514f5e3Sopenharmony_ci
584514f5e3Sopenharmony_civoid AllocationCounter::ClearAllocationInspector()
594514f5e3Sopenharmony_ci{
604514f5e3Sopenharmony_ci    inspector_ = nullptr;
614514f5e3Sopenharmony_ci    currentCounter_ = 0;
624514f5e3Sopenharmony_ci    nextCounter_ = 0;
634514f5e3Sopenharmony_ci}
644514f5e3Sopenharmony_ci
654514f5e3Sopenharmony_civoid AllocationCounter::AdvanceAllocationInspector(size_t allocated)
664514f5e3Sopenharmony_ci{
674514f5e3Sopenharmony_ci    if (!IsActive()) {
684514f5e3Sopenharmony_ci        return;
694514f5e3Sopenharmony_ci    }
704514f5e3Sopenharmony_ci    ASSERT(allocated < nextCounter_ - currentCounter_);
714514f5e3Sopenharmony_ci    currentCounter_ += allocated;
724514f5e3Sopenharmony_ci}
734514f5e3Sopenharmony_ci
744514f5e3Sopenharmony_civoid AllocationCounter::InvokeAllocationInspector(Address object, size_t objectSize, size_t alignedObjectSize)
754514f5e3Sopenharmony_ci{
764514f5e3Sopenharmony_ci    if (!IsActive()) {
774514f5e3Sopenharmony_ci        return;
784514f5e3Sopenharmony_ci    }
794514f5e3Sopenharmony_ci
804514f5e3Sopenharmony_ci    ASSERT(alignedObjectSize >= nextCounter_ - currentCounter_);
814514f5e3Sopenharmony_ci    ASSERT(object != 0);
824514f5e3Sopenharmony_ci    {
834514f5e3Sopenharmony_ci        DISALLOW_GARBAGE_COLLECTION;
844514f5e3Sopenharmony_ci        inspector_->Step(object, objectSize);
854514f5e3Sopenharmony_ci    }
864514f5e3Sopenharmony_ci    size_t nextStepSize = inspector_->GetNextStepSize();
874514f5e3Sopenharmony_ci    // because next allocate or advance can add currentCounter_ to real allocated size,
884514f5e3Sopenharmony_ci    // so need add alignedObjectSize here.
894514f5e3Sopenharmony_ci    nextCounter_ = currentCounter_ + alignedObjectSize + nextStepSize;
904514f5e3Sopenharmony_ci}
914514f5e3Sopenharmony_ci} // namespace panda::ecmascript