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
31namespace {
32const int SLEEP_ONE_SECOND = 1000;
33
34std::vector<std::string> presetPluginVec = {
35    "libcpudataplugin.z.so",
36    "libgpudataplugin.z.so",
37    "libdiskiodataplugin.z.so",
38    "libftrace_plugin.z.so",
39    "libhidumpplugin.z.so",
40    "libhiebpfplugin.z.so",
41    "libhilogplugin.z.so",
42    "libhiperfplugin.z.so",
43    "libhisyseventplugin.z.so",
44    "libmemdataplugin.z.so",
45    "libnetworkplugin.z.so",
46    "libprocessplugin.z.so",
47    "libarktsplugin.z.so",
48    "libxpowerplugin.z.so",
49};
50
51volatile sig_atomic_t g_isRunning = true;
52void SignalSigtermHandler(int sig)
53{
54    g_isRunning = false;
55}
56} // namespace
57
58int main(int argc, char* argv[])
59{
60    int lockFileFd = -1;
61    if ((!COMMON::GetDeveloperMode()) || COMMON::IsProcessRunning(lockFileFd)) { // process is running
62        return 0;
63    }
64    signal(SIGTERM, SignalSigtermHandler);
65    const int connectRetrySeconds = 3;
66    auto pluginManager = std::make_shared<PluginManager>();
67    CHECK_NOTNULL(pluginManager, 1, "create PluginManager FAILED!");
68
69    auto commandPoller = std::make_shared<CommandPoller>(pluginManager);
70    CHECK_NOTNULL(commandPoller, 1, "create CommandPoller FAILED!");
71
72    while (true) {
73        if (commandPoller->OnConnect()) {
74            break;
75        }
76        PROFILER_LOG_DEBUG(LOG_CORE, "%s:connect failed, try again", __func__);
77        sleep(connectRetrySeconds);
78    }
79    pluginManager->SetCommandPoller(commandPoller);
80
81    // add preset plugin
82    for (size_t i = 0; i < presetPluginVec.size(); i++) {
83        const std::string pluginPath = presetPluginVec[i];
84        if (pluginManager->AddPlugin(pluginPath)) {
85            PROFILER_LOG_INFO(LOG_CORE, "add preset plugin %s success!", pluginPath.c_str());
86        } else {
87            PROFILER_LOG_INFO(LOG_CORE, "add preset plugin %s failed!", pluginPath.c_str());
88        }
89    }
90
91    using namespace OHOS::Developtools::Profiler;
92    std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>();
93    ffrtProfilerMgr->Init();
94    std::shared_ptr<NetworkProfilerManager> networkProfilerMgr = std::make_shared<NetworkProfilerManager>();
95    networkProfilerMgr->Init();
96
97    while (g_isRunning) {
98        std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_ONE_SECOND));
99    }
100    if (flock(lockFileFd, LOCK_UN) == -1) {
101        PROFILER_LOG_INFO(LOG_CORE, "release lockfile failed!");
102    }
103    close(lockFileFd);
104    pluginManager->StopAllPluginSession();
105    return 0;
106}
107