1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "src/profiling/memory/sampler.h" 18 19 namespace perfetto { 20 namespace profiling { 21 22 namespace { 23 24 // If the probability of getting less than one sample is less than this, 25 // sidestep the sampler and treat the allocation as a sample. 26 constexpr double kPassthroughError = 0.01; 27 28 } // namespace 29 GetPassthroughThreshold(uint64_t interval)30uint64_t GetPassthroughThreshold(uint64_t interval) { 31 if (interval <= 1) 32 return interval; 33 // (1 - 1 / interval)^x = kPassthroughError 34 // x = log_(1 - 1/interval)(kPassthroughError) 35 return 1 + uint64_t(log(kPassthroughError) / log(1.0 - 1 / double(interval))); 36 } 37 GetGlobalRandomEngineLocked()38std::default_random_engine& GetGlobalRandomEngineLocked() { 39 static std::default_random_engine engine; 40 return engine; 41 } 42 SetSamplingInterval(uint64_t sampling_interval)43void Sampler::SetSamplingInterval(uint64_t sampling_interval) { 44 sampling_interval_ = sampling_interval; 45 passthrough_threshold_ = GetPassthroughThreshold(sampling_interval_); 46 sampling_rate_ = 1.0 / static_cast<double>(sampling_interval_); 47 interval_to_next_sample_ = NextSampleInterval(); 48 } 49 50 } // namespace profiling 51 } // namespace perfetto 52