1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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 <signal.h>
17 #include <sys/file.h>
18 #include <unistd.h>
19
20 #include "common.h"
21 #include "command_poller.h"
22 #include "logging.h"
23 #include "plugin_manager.h"
24 #include "plugin_service_types.pb.h"
25 #include "plugin_watcher.h"
26 #include "schedule_task_manager.h"
27 #include "writer_adapter.h"
28 #include "ffrt_profiler_manager.h"
29 #include "network_profiler_manager.h"
30
31 namespace {
32 #if defined(__i386__) || defined(__x86_64__)
33 const char DEFAULT_PLUGIN_PATH[] = "./";
34 #else
35 const char DEFAULT_PLUGIN_PATH[] = "/data/local/tmp/";
36 #endif
37
38 const int SLEEP_ONE_SECOND = 1000;
39
40 std::vector<std::string> presetPluginVec = {
41 "libcpudataplugin.z.so",
42 "libgpudataplugin.z.so",
43 "libdiskiodataplugin.z.so",
44 "libftrace_plugin.z.so",
45 "libhidumpplugin.z.so",
46 "libhiebpfplugin.z.so",
47 "libhilogplugin.z.so",
48 "libhiperfplugin.z.so",
49 "libhisyseventplugin.z.so",
50 "libmemdataplugin.z.so",
51 "libnetworkplugin.z.so",
52 "libprocessplugin.z.so",
53 "libarktsplugin.z.so",
54 "libxpowerplugin.z.so",
55 };
56
57 volatile sig_atomic_t g_isRunning = true;
SignalSigtermHandler(int sig)58 void SignalSigtermHandler(int sig)
59 {
60 g_isRunning = false;
61 }
62 } // namespace
63
main(int argc,char * argv[])64 int main(int argc, char* argv[])
65 {
66 int lockFileFd = -1;
67 if ((!COMMON::GetDeveloperMode()) || COMMON::IsProcessRunning(lockFileFd)) { // process is running
68 return 0;
69 }
70 signal(SIGTERM, SignalSigtermHandler);
71 const int connectRetrySeconds = 3;
72 std::string pluginDir(DEFAULT_PLUGIN_PATH);
73 if (argv[1] != nullptr) {
74 PROFILER_LOG_DEBUG(LOG_CORE, "%s:pluginDir = %s", __func__, argv[1]);
75 pluginDir = argv[1];
76 }
77
78 auto pluginManager = std::make_shared<PluginManager>();
79 CHECK_NOTNULL(pluginManager, 1, "create PluginManager FAILED!");
80
81 auto commandPoller = std::make_shared<CommandPoller>(pluginManager);
82 CHECK_NOTNULL(commandPoller, 1, "create CommandPoller FAILED!");
83
84 while (true) {
85 if (commandPoller->OnConnect()) {
86 break;
87 }
88 PROFILER_LOG_DEBUG(LOG_CORE, "%s:connect failed, try again", __func__);
89 sleep(connectRetrySeconds);
90 }
91 pluginManager->SetCommandPoller(commandPoller);
92
93 // add preset plugin
94 for (size_t i = 0; i < presetPluginVec.size(); i++) {
95 const std::string pluginPath = presetPluginVec[i];
96 if (pluginManager->AddPlugin(pluginPath)) {
97 PROFILER_LOG_INFO(LOG_CORE, "add preset plugin %s success!", pluginPath.c_str());
98 } else {
99 PROFILER_LOG_INFO(LOG_CORE, "add preset plugin %s failed!", pluginPath.c_str());
100 }
101 }
102
103 using namespace OHOS::Developtools::Profiler;
104 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>();
105 ffrtProfilerMgr->Init();
106 std::shared_ptr<NetworkProfilerManager> networkProfilerMgr = std::make_shared<NetworkProfilerManager>();
107 networkProfilerMgr->Init();
108
109 while (g_isRunning) {
110 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_ONE_SECOND));
111 }
112 if (flock(lockFileFd, LOCK_UN) == -1) {
113 PROFILER_LOG_INFO(LOG_CORE, "release lockfile failed!");
114 }
115 close(lockFileFd);
116 pluginManager->StopAllPluginSession();
117 return 0;
118 }
119