Unified use of init to manage profiler

Signed-off-by: lutao <lutao31@huawei.com>
This commit is contained in:
lutao 2024-11-19 10:39:36 +08:00
parent 116dc9a58e
commit 415445e691
11 changed files with 22 additions and 168 deletions

View File

@ -54,9 +54,6 @@ const std::string HIPROFILER_PLUGINS_NAME("hiprofiler_plugins");
const std::string NATIVE_DAEMON_NAME("native_daemon");
uint32_t g_sampleDuration = 0;
int g_hiprofilerdPid = -1;
int g_hiprofilerPluginsPid = -1;
int g_nativeDaemonPid = -1;
std::condition_variable g_sessionCv;
std::condition_variable g_keepSessionCv;
bool g_exitProcessFlag = false;
@ -396,45 +393,16 @@ bool StartDependentProcess()
constexpr int waitProcMills = 300;
OHOS::system::SetParameter("hiviewdfx.hiprofiler.memprofiler.start", "0");
std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills));
if (getuid() == 0) {
if (!COMMON::IsProcessExist(HIPROFILERD_NAME, g_hiprofilerdPid)) {
// need start hiprofilerd
std::vector<char*> argvVec;
argvVec.push_back(const_cast<char*>(HIPROFILERD_NAME.c_str()));
g_hiprofilerdPid = COMMON::StartProcess("/system/bin/hiprofilerd", argvVec);
// Wait for the hiprofilerd to start
std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills));
}
OHOS::system::SetParameter("hiviewdfx.hiprofiler.profilerd.start", "0");
OHOS::system::SetParameter("hiviewdfx.hiprofiler.plugins.start", "0");
OHOS::system::SetParameter("hiviewdfx.hiprofiler.native_memoryd.start", "0");
if (!COMMON::IsProcessExist(HIPROFILER_PLUGINS_NAME, g_hiprofilerPluginsPid)) {
// need start hiprofiler_plugins
std::vector<char*> argvVec;
argvVec.push_back(const_cast<char*>(HIPROFILER_PLUGINS_NAME.c_str()));
g_hiprofilerPluginsPid = COMMON::StartProcess("/system/bin/hiprofiler_plugins", argvVec);
// Wait for the hiprofiler_plugins add preset plugin
std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills));
}
if (!COMMON::IsProcessExist(NATIVE_DAEMON_NAME, g_nativeDaemonPid)) {
// need start native_daemon
std::vector<char*> argvVec;
argvVec.push_back(const_cast<char*>(NATIVE_DAEMON_NAME.c_str()));
g_nativeDaemonPid = COMMON::StartProcess("/system/bin/native_daemon", argvVec);
// Wait for the native_daemon to start
std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills));
}
} else {
OHOS::system::SetParameter("hiviewdfx.hiprofiler.profilerd.start", "0");
OHOS::system::SetParameter("hiviewdfx.hiprofiler.plugins.start", "0");
OHOS::system::SetParameter("hiviewdfx.hiprofiler.native_memoryd.start", "0");
OHOS::system::SetParameter("hiviewdfx.hiprofiler.profilerd.start", "1");
std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills));
OHOS::system::SetParameter("hiviewdfx.hiprofiler.plugins.start", "1");
std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills));
OHOS::system::SetParameter("hiviewdfx.hiprofiler.native_memoryd.start", "1");
std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills));
}
OHOS::system::SetParameter("hiviewdfx.hiprofiler.profilerd.start", "1");
std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills));
OHOS::system::SetParameter("hiviewdfx.hiprofiler.plugins.start", "1");
std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills));
OHOS::system::SetParameter("hiviewdfx.hiprofiler.native_memoryd.start", "1");
std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills));
std::string content = "";
GetCapabilities(content, true);
@ -446,60 +414,14 @@ bool StartDependentProcess()
return true;
}
bool CheckProcessExit(const std::string& processName, int& pid)
{
int nCount = 5; // 5: try 5 times
constexpr int waitExitMills = 1000;
while (nCount > 0) {
if (COMMON::IsProcessExist(processName, pid)) {
nCount--;
std::this_thread::sleep_for(std::chrono::milliseconds(waitExitMills));
} else {
break;
}
}
return nCount > 0;
}
void KillDependentProcess()
{
constexpr int waitProcMills = 300;
OHOS::system::SetParameter("hiviewdfx.hiprofiler.memprofiler.start", "0");
std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills));
if (getuid() == 0) {
// if pid is equal to -1, need to get pid first.
if (g_nativeDaemonPid == -1) {
COMMON::IsProcessExist(NATIVE_DAEMON_NAME, g_nativeDaemonPid);
}
if (g_hiprofilerPluginsPid == -1) {
COMMON::IsProcessExist(HIPROFILER_PLUGINS_NAME, g_hiprofilerPluginsPid);
}
if (g_hiprofilerdPid == -1) {
COMMON::IsProcessExist(HIPROFILERD_NAME, g_hiprofilerdPid);
}
COMMON::KillProcess(g_nativeDaemonPid);
if (CheckProcessExit(NATIVE_DAEMON_NAME, g_nativeDaemonPid)) {
printf("process native_daemon exits successfully\n");
} else {
printf("process native_daemon exits failed\n");
}
COMMON::KillProcess(g_hiprofilerPluginsPid);
if (CheckProcessExit(HIPROFILER_PLUGINS_NAME, g_hiprofilerPluginsPid)) {
printf("process hiprofiler_plugins exits successfully\n");
} else {
printf("process hiprofiler_plugins exits failed\n");
}
COMMON::KillProcess(g_hiprofilerdPid);
if (CheckProcessExit(HIPROFILERD_NAME, g_hiprofilerdPid)) {
printf("process hiprofilerd exits successfully\n");
} else {
printf("process hiprofilerd exits failed\n");
}
} else {
OHOS::system::SetParameter("hiviewdfx.hiprofiler.profilerd.start", "0");
OHOS::system::SetParameter("hiviewdfx.hiprofiler.plugins.start", "0");
OHOS::system::SetParameter("hiviewdfx.hiprofiler.native_memoryd.start", "0");
}
OHOS::system::SetParameter("hiviewdfx.hiprofiler.profilerd.start", "0");
OHOS::system::SetParameter("hiviewdfx.hiprofiler.plugins.start", "0");
OHOS::system::SetParameter("hiviewdfx.hiprofiler.native_memoryd.start", "0");
}
bool ParseConfig(const std::string& configFile, std::string& config)

View File

@ -27,11 +27,7 @@ FfrtProfilerSocketService::FfrtProfilerSocketService(std::shared_ptr<FfrtProfile
: ffrtMgr_(ffrtMgr)
{
serviceName_ = "FfrtProfilerService";
if (getuid() == 0) {
StartService(FFRT_PROFILER_UNIX_SOCKET_FULL_PATH);
} else {
StartService(FFRT_PROFILER_UNIX_SOCKET_PATH);
}
StartService(FFRT_PROFILER_UNIX_SOCKET_PATH);
}
FfrtProfilerSocketService::~FfrtProfilerSocketService()

View File

@ -296,7 +296,6 @@ float Percentage(const T &a, const T &b)
return static_cast<float>(a) / static_cast<float>(b) * FULL_PERCENTAGE;
}
bool IsRoot();
bool PowerOfTwo(int n);
#define INDENT_ONE_LEVEL (indent + 1)

View File

@ -29,11 +29,7 @@ HookService::HookService(const ClientConfig& clientConfig, std::shared_ptr<HookM
: clientConfig_(clientConfig), hookMgr_(hook), multipleProcesses_(multipleProcesses)
{
serviceName_ = "HookService";
if (getuid() == 0) {
StartService(DEFAULT_UNIX_SOCKET_HOOK_FULL_PATH);
} else {
StartService(DEFAULT_UNIX_SOCKET_HOOK_PATH);
}
StartService(DEFAULT_UNIX_SOCKET_HOOK_PATH);
}
HookService::~HookService()

View File

@ -32,6 +32,7 @@
#include <cstdlib>
#include <unistd.h>
#include "common.h"
#include "dfx_ark.h"
#include "dfx_extractor_utils.h"
#include "dfx_symbols.h"
@ -520,7 +521,7 @@ public:
bool LoadKernelSyms()
{
if (!IsRoot()) {
if (COMMON::IsUserMode()) {
HLOGE("only root mode can access kernel symbols");
return false;
}

View File

@ -286,16 +286,6 @@ bool WriteStringToFile(const std::string &fileName, const std::string &value)
return output.good();
}
bool IsRoot()
{
#if is_linux || is_ohos
static bool isRoot = (getuid() == 0);
return isRoot;
#else
return true;
#endif
}
bool PowerOfTwo(int n)
{
return n && (!(n & (n - 1)));

View File

@ -17,6 +17,7 @@
#include <gtest/gtest.h>
#include <random>
#include <unistd.h>
#include "common.h"
using namespace testing::ext;
using namespace std;
@ -391,7 +392,7 @@ HWTEST_F(SymbolsFileTest, GetSymbolWithVaddr, TestSize.Level1)
{
auto symbols = SymbolsFile::CreateSymbolsFile(SYMBOL_KERNEL_FILE);
if ((getuid() == 0)) {
if (!COMMON::IsUserMode()) {
HLOGD("in root mode");
EXPECT_EQ(symbols->LoadSymbols(), true);
CheckSymbols(symbols);

View File

@ -596,20 +596,6 @@ HWTEST_F(UtilitiesTest, Percentage, TestSize.Level1)
EXPECT_EQ(Percentage(99, 100), 99);
}
/**
* @tc.name: IsRoot
* @tc.desc:
* @tc.type: FUNC
*/
HWTEST_F(UtilitiesTest, IsRoot, TestSize.Level1)
{
bool isRoot = true;
#if is_linux || is_ohos
isRoot = (getuid() == 0);
#endif
EXPECT_EQ(IsRoot(), isRoot);
}
/**
* @tc.name: PowerOfTwo
* @tc.desc:

View File

@ -27,11 +27,7 @@ NetworkProfilerSocketService::NetworkProfilerSocketService(std::shared_ptr<Netwo
: networkMgr_(networkMgr)
{
serviceName_ = "NetworkProfilerService";
if (getuid() == 0) {
StartService(NETWORK_PROFILER_UNIX_SOCKET_FULL_PATH);
} else {
StartService(NETWORK_PROFILER_UNIX_SOCKET_PATH);
}
StartService(NETWORK_PROFILER_UNIX_SOCKET_PATH);
}
NetworkProfilerSocketService::~NetworkProfilerSocketService()

View File

@ -42,13 +42,6 @@ UnixSocketServer::UnixSocketServer()
UnixSocketServer::~UnixSocketServer()
{
if (getuid() == 0) {
if (socketHandle_ != -1) {
PROFILER_LOG_DEBUG(LOG_CORE, "close UnixSocketServer");
close(socketHandle_);
unlink(sAddrName_.c_str());
}
}
socketHandle_ = -1;
if (acceptThread_.joinable()) {
@ -121,30 +114,8 @@ void UnixSocketServer::UnixSocketAccept()
bool UnixSocketServer::StartServer(const std::string& addrname, ServiceEntry& p)
{
CHECK_TRUE(socketHandle_ == -1, false, "StartServer FAIL socketHandle_ != -1");
int sock = -1;
if (getuid() == 0) {
struct sockaddr_un addr;
sock = socket(AF_UNIX, SOCK_STREAM, 0);
CHECK_TRUE(sock != -1, false, "StartServer FAIL create socket ERR : %d", errno);
if (memset_s(&addr, sizeof(struct sockaddr_un), 0, sizeof(struct sockaddr_un)) != EOK) {
PROFILER_LOG_ERROR(LOG_CORE, "memset_s error!");
}
addr.sun_family = AF_UNIX;
if (strncpy_s(addr.sun_path, sizeof(addr.sun_path), addrname.c_str(), sizeof(addr.sun_path) - 1) != EOK) {
PROFILER_LOG_ERROR(LOG_CORE, "strncpy_s error!");
}
unlink(addrname.c_str());
CHECK_TRUE(bind(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr_un)) == 0, close(sock) != 0,
"StartServer FAIL bind ERR : %d", errno);
std::string chmodCmd = "chmod 666 " + addrname;
PROFILER_LOG_INFO(LOG_CORE, "chmod command : %s", chmodCmd.c_str());
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(chmodCmd.c_str(), "r"), pclose);
} else {
sock = GetControlSocket(addrname.c_str());
CHECK_TRUE(sock != -1, false, "StartServer FAIL GetControlSocket return : %d", sock);
}
int sock = GetControlSocket(addrname.c_str());
CHECK_TRUE(sock != -1, false, "StartServer FAIL GetControlSocket return : %d", sock);
CHECK_TRUE(listen(sock, UNIX_SOCKET_LISTEN_COUNT) != -1, close(sock) != 0 && unlink(addrname.c_str()) == 0,
"StartServer FAIL listen ERR : %d", errno);

View File

@ -39,11 +39,7 @@ constexpr uint32_t STOP_BASELINE = (1U << 22); // need to stop take data size wi
PluginService::PluginService()
{
pluginIdCounter_ = 0;
if (getuid() == 0) {
StartService(DEFAULT_UNIX_SOCKET_FULL_PATH);
} else {
StartService(DEFAULT_UNIX_SOCKET_PATH);
}
StartService(DEFAULT_UNIX_SOCKET_PATH);
pluginCommandBuilder_ = std::make_shared<PluginCommandBuilder>();