!2002 增加锁频接口

Merge pull request !2002 from harrysunv9x/master
This commit is contained in:
openharmony_ci 2024-11-18 03:21:29 +00:00 committed by Gitee
commit 84b7f6d177
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
13 changed files with 271 additions and 43 deletions

View File

@ -25,6 +25,7 @@
"ram": "2000KB",
"deps": {
"components": [
"arkxtest",
"ability_runtime",
"ability_base",
"access_token",

View File

@ -50,6 +50,7 @@ ohos_executable("SP_daemon") {
"control_call_cmd.cpp",
"editor_command.cpp",
"heartbeat.cpp",
"lock_frequency.cpp",
"navigation.cpp",
"parse_click_complete_trace.cpp",
"parse_click_response_trace.cpp",
@ -73,6 +74,8 @@ ohos_executable("SP_daemon") {
subsystem_name = "${OHOS_PROFILER_SUBSYS_NAME}"
part_name = "${OHOS_PROFILER_PART_NAME}"
external_deps = [
"ability_base:want",
"arkxtest:test_server_client",
"c_utils:utils",
"hilog:libhilog",
"hiview:libucollection_utility",

View File

@ -32,7 +32,7 @@ private:
static const int arraySize = 1024;
char buffer[arraySize] = {0};
int protNumber = 8284;
std::string message = "init::-SESSIONID 1 -INTERVAL 1000 ";
std::string message = "init:::-SESSIONID 1 -INTERVAL 1000 ";
const char *message1 = "start:::";
const char *message2 = "stop::";
int numBuff = 1024;

View File

@ -96,6 +96,7 @@ enum class CommandType {
CT_GC,
CT_NAV,
CT_O,
CT_LF,
};
enum class CommandHelp {
HELP,
@ -130,6 +131,7 @@ const std::unordered_map<std::string, CommandType> COMMAND_MAP = {
{ std::string("-gc"), CommandType::CT_GC },
{ std::string("-nav"), CommandType::CT_NAV },
{ std::string("-o"), CommandType::CT_O },
{ std::string("-lockfreq"), CommandType::CT_LF },
};
const std::unordered_map<CommandType, std::string> COMMAND_MAP_REVERSE = {
@ -156,6 +158,7 @@ const std::unordered_map<CommandType, std::string> COMMAND_MAP_REVERSE = {
{ CommandType::CT_GC, std::string("-gc") },
{ CommandType::CT_NAV, std::string("-nav") },
{ CommandType::CT_O, std::string("-o") },
{ CommandType::CT_LF, std::string("-lockfreq") },
};

View File

@ -0,0 +1,42 @@
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef LOCK_FREQUENCY_H
#define LOCK_FREQUENCY_H
#include "sp_profiler.h"
#include <string>
#include <map>
namespace OHOS {
namespace SmartPerf {
class LockFrequency : public SpProfiler {
public:
static LockFrequency &GetInstance()
{
static LockFrequency instance;
return instance;
}
std::map<std::string, std::string> ItemData() override;
void LockingThread();
void SetIsCollecting(bool state);
private:
LockFrequency() {};
LockFrequency(const LockFrequency &);
LockFrequency &operator = (const LockFrequency &);
bool isCollecting = false;
};
}
}
#endif

View File

@ -27,7 +27,7 @@ namespace SmartPerf {
class SmartPerfCommand {
public:
const std::string smartPerfExeName = "SP_daemon";
const std::string smartPerfVersion = "1.0.4\n";
const std::string smartPerfVersion = "1.0.5\n";
const std::string smartPerfMsgErr = "error input!\n use command '--help' get more information\n";
const std::string smartPerfMsg = "OpenHarmony performance testing tool SmartPerf command-line version\n"
"Usage: SP_daemon [options] [arguments]\n\n"
@ -87,7 +87,7 @@ public:
~SmartPerfCommand() {};
static void InitSomething();
std::string ExecCommand();
void HelpCommand(CommandHelp type) const;
void HelpCommand(CommandHelp type, std::string token) const;
void HandleCommand(std::string argStr, const std::string &argStr1);
int GetItemInfo(std::multimap<std::string, std::string, decltype(SPUtils::Cmp) *> &spMap);
void PrintfExecCommand(const std::map<std::string, std::string> data) const;

View File

@ -26,6 +26,7 @@
#include "sp_csv_util.h"
#include "sdk_data_recv.h"
#include "GpuCounter.h"
#include "lock_frequency.h"
namespace OHOS {
namespace SmartPerf {
enum class ExceptionMsg {
@ -68,7 +69,7 @@ public:
}
ErrCode InitTask(const std::string &recvStr);
ErrCode StartTask(std::function<void(std::string data)> msgTask);
void StopTask();
ErrCode StopTask();
std::string GetCurrentTimeAsString();
std::map<std::string, std::string> DetectionAndGrab();
bool CheckTcpParam(std::string str, std::string &errorInfo);
@ -90,11 +91,13 @@ public:
void InitDataFile();
void AsyncGetDataMap(std::function<void(std::string data)> msgTask);
void StopGetInfo();
void StartRecord();
void StopRecord();
ErrCode StartRecord();
ErrCode StopRecord();
bool GetRecordState();
void SaveScreenShot();
time_t GetRealStartTime() const;
void SetTcpToken(std::string token);
std::string GetTcpToken();
private:
std::thread ThreadGetHiperf(long long timeStamp);
@ -120,11 +123,14 @@ private:
int requestId = 1;
bool sdkData = false;
std::thread sdk;
std::thread lockFreqThread;
std::vector<std::string> sdkvec;
GpuCounter &gpuCounter = GpuCounter::GetInstance();
LockFrequency &lockFreq = LockFrequency::GetInstance();
bool recordState = false;
bool screenshotFlag = false;
time_t realTimeStart = 0;
std::string tcpToken = "";
std::string strOne = R"(hiprofiler_cmd \
-c - \

View File

@ -31,6 +31,20 @@ class SpThreadSocket {
public:
static bool flagRunning;
static std::string resultFPS;
enum SocketConnectType {
CMD_SOCKET,
EDITOR_SOCKET,
};
enum SocketErrorType {
OK,
TOKEN_CHECK_FAILED,
INIT_FAILED,
START_FAILED,
STOP_FAILED,
START_RECORD_FAILED,
STOP_RECORD_FAILED,
};
std::string MapToString(std::map<std::string, std::string> dataMap) const
{
std::string result;
@ -72,6 +86,21 @@ public:
std::cout << "Socket Process finished!" << std::endl;
spSocket.Close();
}
SocketErrorType CheckToken(std::string recvStr, SpServerSocket &spSocket) const
{
std::string token = recvStr.substr(recvStr.find_last_of(":") + 1);
token = token.substr(0, token.find(' '));
std::string tcpToken = SPTask::GetInstance().GetTcpToken();
if (tcpToken == "" && token == "-SESSIONID") {
return OK;
}
if (token != tcpToken) {
return TOKEN_CHECK_FAILED;
}
return OK;
}
void TypeTcp(SpServerSocket &spSocket) const
{
SocketHeartbeat();
@ -86,23 +115,42 @@ public:
std::string recvStr = spSocket.RecvBuf();
std::cout << "Socket TCP Recv: " << recvStr << std::endl;
// 解析消息 分发处理
DealMsg(recvStr, spSocket);
const SocketErrorType tokenStatus = CheckToken(recvStr, spSocket);
DealMsg(recvStr, spSocket, tokenStatus);
}
}
}
// TCP
void InitRecv(std::string recvStr, SpServerSocket &spSocket) const
void InitRecv(std::string recvStr, SpServerSocket &spSocket, SocketConnectType type) const
{
std::string errorInfo;
std::string checkStr = recvStr.substr(std::string("init::").length());
if (!SPTask::GetInstance().CheckTcpParam(checkStr, errorInfo)) {
LOGD("init error(%s) recvStr(%s)", errorInfo.c_str(), recvStr.c_str());
if (!SPTask::GetInstance().CheckTcpParam(checkStr, errorInfo) &&
checkStr.find(SPTask::GetInstance().GetTcpToken()) == std::string::npos) {
LOGE("init error(%s) recvStr(%s)", errorInfo.c_str(), recvStr.c_str());
if (type == CMD_SOCKET) {
spSocket.Send("init::False,\"error\":" + errorInfo);
} else {
spSocket.Send(std::string("init::") + std::to_string(INIT_FAILED));
}
return;
}
if (recvStr.find("-lockfreq") != std::string::npos &&
SPTask::GetInstance().GetTcpToken() == "") {
LOGE("lockfreq must have token");
return;
}
ErrCode code = SPTask::GetInstance().InitTask(SplitMsg(recvStr));
LOGD("init::%s", (code == ErrCode::OK) ? "True" : "False");
if (type == CMD_SOCKET) {
spSocket.Send(std::string("init::") + ((code == ErrCode::OK) ? "True" : "False"));
return;
}
if (code == ErrCode::OK) {
spSocket.Send("init::True");
} else {
spSocket.Send(std::string("init::") + std::to_string(INIT_FAILED));
}
}
void StartRecv(SpServerSocket &spSocket) const
{
@ -131,36 +179,76 @@ public:
if (code == ErrCode::OK) {
spSocket.Send("start::True");
} else if (code == ErrCode::FAILED) {
spSocket.Send("start::False");
spSocket.Send(std::string("start::") + std::to_string(START_FAILED));
}
}
void StopRecvRealtime(SpServerSocket &spSocket) const
{
SPTask::GetInstance().StopTask();
ErrCode code = SPTask::GetInstance().StopTask();
if (code == ErrCode::OK) {
spSocket.Send("stop::True");
flagRunning = false;
spSocket.Close();
} else if (code == ErrCode::FAILED) {
spSocket.Send(std::string("stop::") + std::to_string(STOP_FAILED));
}
}
void StartRecvRecord(SpServerSocket &spSocket) const
{
SPTask::GetInstance().StartRecord();
LOGI("startRecord::True");
ErrCode code = SPTask::GetInstance().StartRecord();
if (code == ErrCode::OK) {
spSocket.Send("startRecord::True");
} else {
spSocket.Send(std::string("startRecord::") + std::to_string(START_RECORD_FAILED));
}
SPTask::GetInstance().SaveScreenShot();
spSocket.Send("startRecord::True");
}
void StopRecvRecord(SpServerSocket &spSocket) const
{
SPTask::GetInstance().StopRecord();
ErrCode code = SPTask::GetInstance().StopRecord();
if (code == ErrCode::OK) {
spSocket.Send("stopRecord::True");
} else {
spSocket.Send(std::string("stopRecord::") + std::to_string(STOP_RECORD_FAILED));
}
void DealMsg(std::string recvStr, SpServerSocket &spSocket) const
}
void SendTokenFailedMessage(SpServerSocket &socket, std::string &message) const
{
if (message.find("init:::") != std::string::npos ||
message.find("start:::") != std::string::npos) {
return;
}
const std::vector<std::string> messageType = {
"init::",
"start::",
"stop::",
"startRecord::",
"stopRecord::",
};
for (auto it : messageType) {
if (message.find(it) != std::string::npos) {
LOGD((it + std::to_string(TOKEN_CHECK_FAILED)).c_str());
socket.Send(it + std::to_string(TOKEN_CHECK_FAILED));
return;
}
}
}
void DealMsg(std::string recvStr, SpServerSocket &spSocket, SocketErrorType tokenStatus) const
{
SocketHeartbeat();
if (recvStr.find("init::") != std::string::npos) {
InitRecv(recvStr, spSocket);
if (tokenStatus == TOKEN_CHECK_FAILED) {
SendTokenFailedMessage(spSocket, recvStr);
return;
}
if (recvStr.find("init:::") != std::string::npos) {
InitRecv(recvStr, spSocket, CMD_SOCKET);
} else if (recvStr.find("start:::") != std::string::npos) {
StartRecv(spSocket);
} else if (recvStr.find("init::") != std::string::npos) {
InitRecv(recvStr, spSocket, EDITOR_SOCKET);
} else if (recvStr.find("start::") != std::string::npos) {
StartRecvRealtime(spSocket);
} else if (recvStr.find("stop::") != std::string::npos) {

View File

@ -0,0 +1,46 @@
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "include/lock_frequency.h"
#include <thread>
#include <chrono>
#include "include/sp_log.h"
#include "test_server_client.h"
namespace OHOS {
namespace SmartPerf {
std::map<std::string, std::string> LockFrequency::ItemData()
{
return std::map<std::string, std::string>();
}
void LockFrequency::LockingThread()
{
const int loopLockTime = 4000;
LOGI("Lock frequency thread create");
while (isCollecting) {
OHOS::testserver::TestServerClient::GetInstance().FrequencyLock();
std::this_thread::sleep_for(std::chrono::milliseconds(loopLockTime));
}
LOGI("Lock frequency thread end");
}
void LockFrequency::SetIsCollecting(bool state)
{
isCollecting = state;
}
}
}

View File

@ -48,9 +48,14 @@ SmartPerfCommand::SmartPerfCommand(std::vector<std::string> argv)
auto iterator = COMMAND_HELP_MAP.begin();
while (iterator != COMMAND_HELP_MAP.end()) {
if (iterator->second.compare(argv[1]) == 0) {
HelpCommand(iterator->first);
HelpCommand(iterator->first, "");
break;
}
if (argv[1].find("-editorServer") != std::string::npos) {
const size_t tokenStartPosition = 14;
std::string token = argv[1].substr(tokenStartPosition, argv[1].length() - tokenStartPosition);
HelpCommand(CommandHelp::EDITORSERVER, token);
}
++iterator;
}
}
@ -67,7 +72,7 @@ SmartPerfCommand::SmartPerfCommand(std::vector<std::string> argv)
}
}
}
void SmartPerfCommand::HelpCommand(CommandHelp type) const
void SmartPerfCommand::HelpCommand(CommandHelp type, std::string token) const
{
LOGD("SmartPerfCommand::HelpCommand type(%d)", type);
if (type == CommandHelp::HELP) {
@ -92,6 +97,8 @@ void SmartPerfCommand::HelpCommand(CommandHelp type) const
SPUtils::LoadCmd(cmdStr + pidStr, result);
if (type == CommandHelp::SERVER) {
daemon(0, 0);
} else {
SPTask::GetInstance().SetTcpToken(token);
}
CreateSocketThread();
}

View File

@ -47,7 +47,21 @@ static std::string GetOptions(const std::vector<std::string> &argv)
}
return str;
}
static void keyInsert(std::set<std::string> &keysMap)
{
keysMap.insert("editor");
keysMap.insert("profilerfps");
keysMap.insert("start");
keysMap.insert("stop");
keysMap.insert("screen");
keysMap.insert("clear");
keysMap.insert("server");
keysMap.insert("sections");
keysMap.insert("deviceinfo");
keysMap.insert("ohtestfps");
keysMap.insert("editorServer");
keysMap.insert("recordcapacity");
}
static bool g_checkCmdParam(std::vector<std::string> &argv, std::string &errorInfo)
{
std::string str = GetOptions(argv);
@ -66,18 +80,10 @@ static bool g_checkCmdParam(std::vector<std::string> &argv, std::string &errorIn
return true;
}
}
keys.insert("editor");
keys.insert("profilerfps");
keys.insert("start");
keys.insert("stop");
keys.insert("screen");
keys.insert("clear");
keys.insert("server");
keys.insert("sections");
keys.insert("deviceinfo");
keys.insert("ohtestfps");
keys.insert("editorServer");
keys.insert("recordcapacity");
keyInsert(keys);
if (argv[1].find("editorServer:") != std::string::npos) {
keys.insert(argv[1].substr(1).c_str());
}
for (auto a : OHOS::SmartPerf::COMMAND_MAP) {
keys.insert(a.first.substr(1)); // No prefix required '-'
}

View File

@ -210,7 +210,9 @@ void SPTask::GetItemData(std::map<std::string, std::string> &dataMap)
dataMap.insert(captureMap.begin(), captureMap.end());
}
if (itConfig.find("-gc") != std::string::npos || itConfig.find("-o") != std::string::npos) {
if (itConfig.find("-gc") != std::string::npos ||
itConfig.find("-o") != std::string::npos ||
itConfig.find("-lockfreq") != std::string::npos) {
continue;
}
@ -232,6 +234,11 @@ void SPTask::ConfigDataThread()
if (itConfig.find("-gc") != std::string::npos) {
gpuCounter.StartCollect(GpuCounter::GC_START);
}
if (itConfig.find("-lockfreq") != std::string::npos) {
lockFreq.SetIsCollecting(true);
lockFreqThread = std::thread([this]() { this->lockFreq.LockingThread(); });
}
}
}
@ -444,7 +451,7 @@ void SPTask::StopGpuCounterRecv()
gpuCounter.GetInstance().SaveData(outGpuCounterDataPath);
}
}
void SPTask::StopTask()
ErrCode SPTask::StopTask()
{
if (GetRecordState()) {
StopGetInfo();
@ -460,6 +467,10 @@ void SPTask::StopTask()
ResetSdkParam();
gpuCounter.StopCollect();
lockFreq.SetIsCollecting(false);
if (lockFreqThread.joinable()) {
lockFreqThread.join();
}
isRunning = false;
isInit = false;
@ -469,6 +480,7 @@ void SPTask::StopTask()
thread.join();
}
KillHiperfCmd();
return ErrCode::OK;
}
std::map<std::string, std::string> SPTask::DetectionAndGrab()
@ -603,14 +615,15 @@ bool SPTask::GetRecordState()
{
return recordState;
}
void SPTask::StartRecord()
ErrCode SPTask::StartRecord()
{
startTime = SPUtils::GetCurTime();
InitDataFile();
recordState = true;
return ErrCode::OK;
}
void SPTask::StopRecord()
ErrCode SPTask::StopRecord()
{
std::string outGpuCounterDataPath = baseOutPath + "/" + curTaskInfo.sessionId;
@ -628,6 +641,8 @@ void SPTask::StopRecord()
recordState = false;
screenshotFlag = false;
return ErrCode::OK;
}
void SPTask::SaveScreenShot()
@ -638,5 +653,13 @@ time_t SPTask::GetRealStartTime() const
{
return realTimeStart;
}
void SPTask::SetTcpToken(std::string token)
{
tcpToken = token;
}
std::string SPTask::GetTcpToken()
{
return tcpToken;
}
}
}

View File

@ -35,6 +35,7 @@ ohos_unittest("sp_daemon_ut") {
"../Power.cpp",
"../RAM.cpp",
"../Temperature.cpp",
"../lock_frequency.cpp",
"../parse_click_complete_trace.cpp",
"../parse_click_response_trace.cpp",
"../parse_radar.cpp",
@ -65,6 +66,8 @@ ohos_unittest("sp_daemon_ut") {
"//third_party/libpng:libpng",
]
external_deps = [
"ability_base:want",
"arkxtest:test_server_client",
"c_utils:utils",
"hilog:libhilog",
"hiview:libucollection_utility",