1 /* 2 * Copyright (C) 2021 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 "include/lock_frequency.h" 17 #include <thread> 18 #include <chrono> 19 #include <dlfcn.h> 20 #include "include/sp_log.h" 21 #include "include/service_plugin.h" 22 23 namespace OHOS { 24 namespace SmartPerf { ItemData()25 std::map<std::string, std::string> LockFrequency::ItemData() 26 { 27 return std::map<std::string, std::string>(); 28 } LockingThread()29 void LockFrequency::LockingThread() 30 { 31 LOGD("Lock frequency thread create"); 32 const int loopLockTime = 4000; 33 34 ServicePluginHandler &servicePluginHandler = ServicePluginHandler::GetInstance(); 35 void* handle = servicePluginHandler.GetSoHandler(ServicePluginHandler::ServicePluginType::TEST_PLUGIN); 36 if (!handle) { 37 WLOGE("open TestServerPlugin so file error."); 38 return; 39 } 40 41 typedef int32_t (*GetLockFreq)(); 42 GetLockFreq testServerPlugin = (GetLockFreq)dlsym(handle, lockFunction.c_str()); 43 if (!testServerPlugin) { 44 WLOGE("Error loading symbol"); 45 return; 46 } 47 while (isCollecting) { 48 testServerPlugin(); 49 std::this_thread::sleep_for(std::chrono::milliseconds(loopLockTime)); 50 } 51 52 LOGD("Lock frequency thread end"); 53 } 54 StartExecutionOnce(bool isPause)55 void LockFrequency::StartExecutionOnce(bool isPause) 56 { 57 if (isPause) { 58 return; 59 } 60 SetIsCollecting(true); 61 th_ = std::thread([this]() { 62 WLOGI("Starting lock frequency locking thread"); 63 LockingThread(); 64 }); 65 } 66 FinishtExecutionOnce(bool isPause)67 void LockFrequency::FinishtExecutionOnce(bool isPause) 68 { 69 if (isPause) { 70 return; 71 } 72 SetIsCollecting(false); 73 if (th_.joinable()) { 74 LOGD("Joining lockFreqThread."); 75 th_.join(); 76 } 77 } 78 SetIsCollecting(bool state)79 void LockFrequency::SetIsCollecting(bool state) 80 { 81 isCollecting = state; 82 83 ServicePluginHandler &servicePluginHandler = ServicePluginHandler::GetInstance(); 84 void* handle = servicePluginHandler.GetSoHandler(ServicePluginHandler::ServicePluginType::PERF_GENIUS_PLUGIN); 85 if (!handle) { 86 WLOGE("Get service plugin handler failed."); 87 return; 88 } 89 90 reportFunc_ = reinterpret_cast<ReportDataFunc>(dlsym(handle, "PerfCmdHandle")); 91 if (!reportFunc_) { 92 WLOGE("PerfCmdHandle Error loading symbol"); 93 return; 94 } 95 96 std::vector<int32_t> resId = {4206}; 97 std::vector<int64_t> endTime = {0}; 98 std::vector<int64_t> value = {110}; 99 if (!state) { 100 value = {100}; 101 } 102 103 int ret = reportFunc_(resId, value, endTime, ""); 104 if (ret < 0) { 105 WLOGE("reportFunc_ failed, ret: %d", ret); 106 } 107 } 108 } 109 } 110