1 /**
2 * Copyright (c) 2021-2025 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 ark::tooling {
20
StartSamplingProfiler(const char * asptFilename,int interva)21 extern "C" PANDA_PUBLIC_API int StartSamplingProfiler(const char *asptFilename, int interva)
22 {
23 auto *runtime = Runtime::GetCurrent();
24 if (runtime == nullptr) {
25 LOG(DEBUG, PROFILER) << "That runtime is not created.";
26 return 1;
27 }
28 return static_cast<int>(
29 !runtime->GetTools().StartSamplingProfiler(std::make_unique<sampler::FileStreamWriter>(asptFilename), interva));
30 }
31
StopSamplingProfiler()32 extern "C" PANDA_PUBLIC_API void StopSamplingProfiler()
33 {
34 auto *runtime = Runtime::GetCurrent();
35 if (runtime == nullptr) {
36 LOG(DEBUG, PROFILER) << "That runtime is not created.";
37 return;
38 }
39 runtime->GetTools().StopSamplingProfiler();
40 }
41
GetSamplingProfiler()42 sampler::Sampler *Tools::GetSamplingProfiler()
43 {
44 // Singleton instance
45 return sampler_;
46 }
47
CreateSamplingProfiler()48 void Tools::CreateSamplingProfiler()
49 {
50 ASSERT(sampler_ == nullptr);
51 sampler_ = sampler::Sampler::Create();
52
53 const char *samplerSegvOption = std::getenv("ARK_SAMPLER_DISABLE_SEGV_HANDLER");
54 if (samplerSegvOption != nullptr) {
55 std::string_view option = samplerSegvOption;
56 if (option == "1" || option == "true" || option == "ON") {
57 // SEGV handler for sampler is enable by default
58 sampler_->SetSegvHandlerStatus(false);
59 }
60 }
61 }
62
StartSamplingProfiler(std::unique_ptr<sampler::StreamWriter> streamWriter,uint32_t interval)63 bool Tools::StartSamplingProfiler(std::unique_ptr<sampler::StreamWriter> streamWriter, uint32_t interval)
64 {
65 ASSERT(sampler_ != nullptr);
66 sampler_->SetSampleInterval(interval);
67 return sampler_->Start(std::move(streamWriter));
68 }
69
StopSamplingProfiler()70 void Tools::StopSamplingProfiler()
71 {
72 ASSERT(sampler_ != nullptr);
73 sampler_->Stop();
74 }
DestroySamplingProfiler()75 void Tools::DestroySamplingProfiler()
76 {
77 ASSERT(sampler_ != nullptr);
78 sampler::Sampler::Destroy(sampler_);
79 sampler_ = nullptr;
80 }
81
IsSamplingProfilerCreate()82 bool Tools::IsSamplingProfilerCreate()
83 {
84 return sampler_ != nullptr;
85 }
86
87 } // namespace ark::tooling
88