1 /** 2 * Copyright (c) 2021-2022 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 "runtime/tooling/tools.h" 17 #include "runtime/tooling/sampler/sampling_profiler.h" 18 19 namespace panda::tooling { 20 GetSamplingProfiler()21sampler::Sampler *Tools::GetSamplingProfiler() 22 { 23 // Singleton instance 24 return sampler_; 25 } 26 CreateSamplingProfiler()27void Tools::CreateSamplingProfiler() 28 { 29 ASSERT(sampler_ == nullptr); 30 sampler_ = sampler::Sampler::Create(); 31 32 const char *samplerSegvOption = std::getenv("ARK_SAMPLER_DISABLE_SEGV_HANDLER"); 33 if (samplerSegvOption != nullptr) { 34 std::string_view option = samplerSegvOption; 35 if (option == "1" || option == "true" || option == "ON") { 36 // SEGV handler for sampler is enable by default 37 sampler_->SetSegvHandlerStatus(false); 38 } 39 } 40 } 41 StartSamplingProfiler(const std::string & asptFilename,uint32_t interval)42bool Tools::StartSamplingProfiler(const std::string &asptFilename, uint32_t interval) 43 { 44 ASSERT(sampler_ != nullptr); 45 sampler_->SetSampleInterval(interval); 46 if (asptFilename.empty()) { 47 std::time_t currentTime = std::time(nullptr); 48 std::tm *localTime = std::localtime(¤tTime); 49 std::string asptFilenameTime = std::to_string(localTime->tm_hour) + "-" + std::to_string(localTime->tm_min) + 50 "-" + std::to_string(localTime->tm_sec) + ".aspt"; 51 return sampler_->Start(asptFilenameTime.c_str()); 52 } 53 return sampler_->Start(asptFilename.c_str()); 54 } 55 StopSamplingProfiler()56void Tools::StopSamplingProfiler() 57 { 58 ASSERT(sampler_ != nullptr); 59 sampler_->Stop(); 60 sampler::Sampler::Destroy(sampler_); 61 sampler_ = nullptr; 62 } 63 64 } // namespace panda::tooling 65