• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 "sampling.h"
17 
Reset()18 void Sampling::Reset()
19 {
20     sampleInterval_ = 0;
21     sampleRate_ = 0;
22     nextSampleInterval_ = 0;
23     exponentialDist_.reset();
24 }
25 
InitSampling(uint64_t sampleInterval)26 void Sampling::InitSampling(uint64_t sampleInterval)
27 {
28     sampleInterval_ = sampleInterval;
29     sampleRate_ = 1.0 / static_cast<double>(sampleInterval_);
30     exponentialDist_ = std::exponential_distribution<double>(sampleRate_);
31     nextSampleInterval_ = CalcNextSampleInterval();
32 }
33 
CalcSamplings(size_t allocSize)34 size_t Sampling::CalcSamplings(size_t allocSize)
35 {
36     size_t counts = 0;
37     {
38         Spinlock s;
39         for (nextSampleInterval_ -= static_cast<int64_t>(allocSize); nextSampleInterval_ <= 0;
40             nextSampleInterval_ += CalcNextSampleInterval()) {
41             ++counts;
42         }
43     }
44     return counts;
45 }