From 8a35e256a6c5133b65351f4e5bfd414d66289222 Mon Sep 17 00:00:00 2001 From: frank-huangran Date: Thu, 31 Jul 2025 19:41:27 +0800 Subject: [PATCH] add smartperf utils Signed-off-by: frank-huangran --- .../device_command/utils/GetLog.h | 56 ++++++ .../device_command/utils/service_plugin.h | 59 ++++++ .../device_command/utils/sp_log.h | 66 +++++++ .../utils/sp_profiler_factory.h | 34 ++++ .../device_command/utils/sp_utils.h | 176 ++++++++++++++++++ .../device_command/utils/startup_delay.h | 37 ++++ 6 files changed, 428 insertions(+) create mode 100644 smartperf_device/device_command/utils/GetLog.h create mode 100644 smartperf_device/device_command/utils/service_plugin.h create mode 100644 smartperf_device/device_command/utils/sp_log.h create mode 100644 smartperf_device/device_command/utils/sp_profiler_factory.h create mode 100644 smartperf_device/device_command/utils/sp_utils.h create mode 100644 smartperf_device/device_command/utils/startup_delay.h diff --git a/smartperf_device/device_command/utils/GetLog.h b/smartperf_device/device_command/utils/GetLog.h new file mode 100644 index 000000000..0eaaa4dcc --- /dev/null +++ b/smartperf_device/device_command/utils/GetLog.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved. + * 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 GETLOG_H +#define GETLOG_H +#include "sp_profiler.h" +#include "sp_log.h" +#include +#include +namespace OHOS { +namespace SmartPerf { +class GetLog : public SpProfiler { +public: + std::map ItemData() override; + static GetLog &GetInstance() + { + static GetLog instance; + return instance; + } + void RemoveLogFile(); + +private: + GetLog() {}; + GetLog(const GetLog &); + GetLog &operator = (const GetLog &); + void TarLogFile(); + int GetCurrentPath(char *currentPath); + void GetHilogInMemory(std::vector &fileList); + void GetHilogInData(std::vector &otherFiles, std::vector &logFiles); + void GenerateHilogFile(); + void GenerateDaemonLogFile(); + + int logFileSocket = -1; + int logFileSocketPort = -1; + const std::string systemHilogFileDir = "/data/log/hilog/"; + const std::string hilogFileDir = LOG_FILE_DIR + "hilog/"; + const std::string daemonLogFileDir = LOG_FILE_DIR + "daemonLog/"; + const std::string logFilePath = LOG_FILE_DIR + "logfile.tar.gz"; + static const uintmax_t logMaxSize = 52428800; // 50MB = 50 * 1024 * 1024 + uintmax_t currentLogSize = 0; +}; +} +} +#endif // NETWORK_H diff --git a/smartperf_device/device_command/utils/service_plugin.h b/smartperf_device/device_command/utils/service_plugin.h new file mode 100644 index 000000000..f1723ea9b --- /dev/null +++ b/smartperf_device/device_command/utils/service_plugin.h @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2025 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 SERVICE_PLUGIN_H +#define SERVICE_PLUGIN_H + +#include "string" +#include "map" +#include "thread" +#include "sp_profiler.h" + +namespace OHOS { + namespace SmartPerf { + class ServicePluginHandler : public SpProfiler { + public: + enum ServicePluginType { + GAME_PLUGIN, + TEST_PLUGIN, + PERF_GENIUS_PLUGIN, + PLUGIN_COUNT + }; + + std::map ItemData() override; + static ServicePluginHandler &GetInstance() + { + static ServicePluginHandler instance; + return instance; + } + void* GetSoHandler(enum ServicePluginType type); + + private: + ServicePluginHandler(); + ~ServicePluginHandler() override; + ServicePluginHandler(const ServicePluginHandler &); + ServicePluginHandler &operator = (const ServicePluginHandler &); + + const std::vector pluginSoPath = { + "/system/lib64/libgameservice_gpucounter_plugin.z.so", + "/system/lib64/libtest_server_client.z.so", + "/system/lib64/libperfgenius_client.z.so" + }; + std::vector pluginHandle; + }; + } +} + +#endif \ No newline at end of file diff --git a/smartperf_device/device_command/utils/sp_log.h b/smartperf_device/device_command/utils/sp_log.h new file mode 100644 index 000000000..09f8e9d38 --- /dev/null +++ b/smartperf_device/device_command/utils/sp_log.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved. + * 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 OHOS_SP_LOG_H +#define OHOS_SP_LOG_H +#include +namespace OHOS { +namespace SmartPerf { +const std::string LOG_FILE_DIR = "/data/local/tmp/spdaemonlog/"; +typedef enum { + SP_LOG_DEBUG, + SP_LOG_INFO, + SP_LOG_WARN, + SP_LOG_ERROR, +} SpLogLevel; + +void SpLog(SpLogLevel logLevel, bool isWriteLog, const char *fmt, ...); + +#define LOGD(fmt, ...) \ + SpLog(SP_LOG_DEBUG, false, (std::string("[") + "SP_daemon" + "][" + __FUNCTION__ + "]:" + fmt).c_str(), \ + ##__VA_ARGS__) + +#define LOGI(fmt, ...) \ + SpLog(SP_LOG_INFO, false, (std::string("[") + "SP_daemon" + "][" + __FUNCTION__ + "]:" + fmt).c_str(), \ + ##__VA_ARGS__) + +#define LOGW(fmt, ...) \ + SpLog(SP_LOG_WARN, false, (std::string("[") + "SP_daemon" + "][" + __FUNCTION__ + "]:" + fmt).c_str(), \ + ##__VA_ARGS__) + +#define LOGE(fmt, ...) \ + SpLog(SP_LOG_ERROR, false, (std::string("[") + "SP_daemon" + "][" + __FUNCTION__ + "]:" + fmt).c_str(), \ + ##__VA_ARGS__) + +#define WLOGD(fmt, ...) \ + SpLog(SP_LOG_DEBUG, true, (std::string("[Debug][") + "SP_daemon" + "][" + __FUNCTION__ + "]:" + fmt).c_str(), \ + ##__VA_ARGS__) + +#define WLOGI(fmt, ...) \ + SpLog(SP_LOG_INFO, true, (std::string("[Info][") + "SP_daemon" + "][" + __FUNCTION__ + "]:" + fmt).c_str(), \ + ##__VA_ARGS__) + +#define WLOGW(fmt, ...) \ + SpLog(SP_LOG_WARN, true, (std::string("[Warning][") + "SP_daemon" + "][" + __FUNCTION__ + "]:" + fmt).c_str(), \ + ##__VA_ARGS__) + +#define WLOGE(fmt, ...) \ + SpLog(SP_LOG_ERROR, true, (std::string("[Error][") + "SP_daemon" + "][" + __FUNCTION__ + "]:" + fmt).c_str(), \ + ##__VA_ARGS__) + +void EnableWriteLogAndDeleteOldLogFiles(); +void EscapeForCSV(std::string str); +} // namespace SmartPerf +} // namespace OHOS +#endif // OHOS_SP_LOG_H \ No newline at end of file diff --git a/smartperf_device/device_command/utils/sp_profiler_factory.h b/smartperf_device/device_command/utils/sp_profiler_factory.h new file mode 100644 index 000000000..68f6054c8 --- /dev/null +++ b/smartperf_device/device_command/utils/sp_profiler_factory.h @@ -0,0 +1,34 @@ +/* + * 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 SP_PROFILER_FACTORY_H +#define SP_PROFILER_FACTORY_H +#include "common.h" +#include "sp_profiler.h" +namespace OHOS { +namespace SmartPerf { +class SpProfilerFactory { +public: + static SpProfiler *GetProfilerItem(MessageType messageType); + static SpProfiler *GetProfilerItemContinue(MessageType messageType); + static void SetProfilerPkg(const std::string &pkg); + static void SetProfilerPidByPkg(std::string &pid, std::string pids = ""); + static void SetByTrace(const std::string& message); + static SpProfiler *GetCmdProfilerItem(CommandType commandType, bool cmdFlag); + static SpProfiler *GetCmdProfilerItemContinue(CommandType commandType, bool cmdFlag); + static inline bool editorFlag = false; +}; +} +} +#endif \ No newline at end of file diff --git a/smartperf_device/device_command/utils/sp_utils.h b/smartperf_device/device_command/utils/sp_utils.h new file mode 100644 index 000000000..61bbeb14d --- /dev/null +++ b/smartperf_device/device_command/utils/sp_utils.h @@ -0,0 +1,176 @@ +/* + * 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 SP_UTILS_H +#define SP_UTILS_H +#include +#include +#include +#include +#include +#include +namespace OHOS { +namespace SmartPerf { +class SPUtilesTye { +public: + template + static T StringToSometype(const std::string& str) + { + T result; + std::stringstream ss; + ss << str; + ss >> result; + return result; + } +}; +namespace SPUtils { +/** + * @brief Check if the file has permission to access + * + * @param fileName + * @return true + * @return false + */ +bool HasNumber(const std::string &str); +bool Cmp(const std::string &a, const std::string &b); +/** + * @brief Comparison key name + * + * @param a + * @param b + * @return true + * @return false + */ +bool FileAccess(const std::string &fileName); +/** + * @brief Load content from file node + * + * @param filePath + * @param content + * @return true + * @return false + */ +bool LoadFile(const std::string &filePath, std::string &content); +/** + * @brief read command return result + * + * @param cmd + * @param result + * @return true + * @return false + */ +bool LoadCmdWithLinkBreak(const std::string &cmd, bool isClearLinkBreak, std::string &result); +/** + * @brief + * + * @param path + * @return std::string + */ + +bool LoadCmd(const std::string &cmd, std::string &result); +/** + * @brief + * + * @param path + * @return std::string + */ +std::string IncludePathDelimiter(const std::string &path); +/** + * @brief + * @param path + * @param files + */ +void ForDirFiles(const std::string &path, std::vector &files); + +/** + * @brief check if substr in parentstr + * + * @param str + * @param sub + * @return true + * @return false + */ +bool IsSubString(const std::string &str, const std::string &sub); +/** + * @brief split content by delimiter + * + * @param content + * @param sp + * @param out + */ +void StrSplit(const std::string &content, const std::string &sp, std::vector &out); +/** + * @brief extract number from str + * + * @param str + * @return std::string + */ +std::string ExtractNumber(const std::string &str); +/** + * @brief replace '' \r\n from str + * @param res + */ +void ReplaceString(std::string &res); +/** + * @brief get cur Time longlong + * + */ +long long GetCurTime(); +/** + * @brief get top pkg + * + */ +std::string GetTopPkgName(); +std::string GetRadar(); +std::string GetRadarResponse(); +std::string GetRadarComplete(); +std::string GetRadarFrame(); +std::map GetDeviceInfo(); +std::map GetCpuInfo(bool isTcpMessage); +std::map GetGpuInfo(bool isTcpMessage); +std::string GetDeviceInfoMap(); +std::string GetScreen(); +void RemoveSpace(std::string &str); +bool IntegerVerification(const std::string& str, std::string& errorInfo); +bool VeriyParameter(std::set& keys, const std::string& param, std::string& errorInfo); +bool VeriyKey(std::set& keys, std::map& mapInfo, std::string& errorInfo); +bool VerifyValueStr(std::map& mapInfo, std::string& errorInfo); +bool IntegerValueVerification(std::set &keys, std::map &mapInfo, + std::string &errorInfo); +bool IsHmKernel(); +std::string GetCpuNum(); +void GetCurrentTime(int prevTime); +bool IsForeGround(const std::string &pkg); +bool IsFindAbilist(); +void SetRkFlag(); +bool GetPathPermissions(const std::string &path); +bool GetIsGameApp(const std::string& pkg); +bool IsFindForeGround(const std::string &line); +std::string GetVersion(); +int& GetTtyDeviceFd(); +void GetTestsaPlugin(int command); +void KillStartDaemon(); +void CreateDir(const std::string &dirPath); +void RemoveDirOrFile(const std::string &dirPath); +void CopyFiles(const std::string& cpStr); +void TarFiles(const std::string& tarStr); +std::string ExecuteCommand(const std::string& command); +size_t GetFileSize(std::string filePath); +bool IsFindDHGame(const std::string &pkg); +std::string GetSurface(); +}; +} +} + +#endif // SP_UTILS_H diff --git a/smartperf_device/device_command/utils/startup_delay.h b/smartperf_device/device_command/utils/startup_delay.h new file mode 100644 index 000000000..511c484f9 --- /dev/null +++ b/smartperf_device/device_command/utils/startup_delay.h @@ -0,0 +1,37 @@ +/* + * 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 STARTUP_DELAY_H +#define STARTUP_DELAY_H +#include +namespace OHOS { +namespace SmartPerf { +class StartUpDelay { +public: + StartUpDelay(); + ~StartUpDelay(); + void GetTrace(const std::string &traceName) const; + void GetHisysIdAndKill() const; + void GetHisysId() const; + std::string GetPidByPkg(const std::string &curPkgName, std::string* pids = nullptr) const; + bool KillSpProcess() const; + bool GetSpClear(bool isKillTestServer) const; + void ClearOldServer() const; + std::string ExecuteCommand(const std::vector &args) const; + std::vector GetPidParams() const; + void KillTestSpdaemon(const std::string &line, const std::string &curPid) const; +}; +} +} +#endif \ No newline at end of file -- Gitee