• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "hiperf_module.h"
16 
17 #include <array>
18 #include <poll.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <sys/wait.h>
22 #include <unistd.h>
23 #include <vector>
24 
25 #include "hiperf_plugin_config.pb.h"
26 #include "logging.h"
27 #include "securec.h"
28 #include "common.h"
29 #include "trace_file_writer.h"
30 
31 namespace {
32 constexpr uint32_t MAX_BUFFER_SIZE = 4 * 1024 * 1024;
33 const std::string SU_ROOT = "su root";
34 const std::string HIPERF_CMD = " hiperf";
35 const std::string HIPERF_RECORD_CMD = " record";
36 const std::string HIPERF_RECORD_PREPARE = " --control prepare";
37 const std::string HIPERF_RECORD_START = " --control start";
38 const std::string HIPERF_RECORD_STOP = " --control stop";
39 const std::string HIPERF_RECORD_OK = "sampling success";
40 const int WAIT_HIPERF_TIME = 10;
41 const std::string HIPERF_BIN_PATH = "/system/bin/hiperf";
42 
43 std::mutex g_taskMutex;
44 bool g_isRoot = false;
45 std::string g_logLevel = "";
46 HiperfPluginConfig g_config;
47 std::shared_ptr<TraceFileWriter> g_splitTraceWriter {nullptr};
48 
ParseConfigToCmd(const HiperfPluginConfig & config,std::vector<std::string> & cmds)49 bool ParseConfigToCmd(const HiperfPluginConfig& config, std::vector<std::string>& cmds)
50 {
51     g_isRoot = config.is_root();
52     auto logLevel = config.log_level();
53     if (logLevel == HiperfPluginConfig_LogLevel_MUCH) {
54         g_logLevel = " --hilog --much";
55     } else if (logLevel == HiperfPluginConfig_LogLevel_VERBOSE) {
56         g_logLevel = " --hilog --verbose";
57     } else if (logLevel == HiperfPluginConfig_LogLevel_DEBUG) {
58         g_logLevel = " --hilog --debug";
59     } else {
60         g_logLevel = " --nodebug";
61     }
62 
63     // command of prepare
64     std::string traceCmd;
65     auto &prepareCmd = cmds.emplace_back();
66     prepareCmd = g_isRoot ? SU_ROOT : "";
67     prepareCmd += HIPERF_CMD + g_logLevel + HIPERF_RECORD_CMD + HIPERF_RECORD_PREPARE;
68     if (!config.outfile_name().empty()) {
69         prepareCmd += " -o " + config.outfile_name();
70         size_t fileSize = sizeof(g_pluginModule.outFileName);
71         int ret = strncpy_s(g_pluginModule.outFileName, fileSize, config.outfile_name().c_str(), fileSize - 1);
72         CHECK_TRUE(ret == EOK, false, "strncpy_s error! outfile is %s", config.outfile_name().c_str());
73     }
74     if (!config.record_args().empty()) {
75         prepareCmd += " " + config.record_args();
76     }
77 
78     // command of start
79     auto &startCmd = cmds.emplace_back();
80     startCmd = g_isRoot ? SU_ROOT : "";
81     startCmd += HIPERF_CMD + g_logLevel + HIPERF_RECORD_CMD + HIPERF_RECORD_START;
82     return true;
83 }
84 
RunCommand(const std::string & cmd)85 bool RunCommand(const std::string& cmd)
86 {
87     PROFILER_LOG_INFO(LOG_CORE, "run command: %s", cmd.c_str());
88     bool res = false;
89     std::vector<std::string> cmdArg;
90     COMMON::SplitString(cmd, " ", cmdArg);
91     cmdArg.emplace(cmdArg.begin(), HIPERF_BIN_PATH);
92 
93     volatile pid_t childPid = -1;
94     int pipeFds[2] = {-1, -1};
95     FILE* fp = COMMON::CustomPopen(cmdArg, "r", pipeFds, childPid);
96     CHECK_NOTNULL(fp, false, "HiperfPlugin::RunCommand CustomPopen FAILED!r");
97     constexpr uint32_t readBufferSize = 4096;
98     std::array<char, readBufferSize> buffer;
99     std::string result;
100     usleep(WAIT_HIPERF_TIME);
101     while (fgets(buffer.data(), buffer.size(), fp) != nullptr) {
102         result += buffer.data();
103         res = result.find(HIPERF_RECORD_OK) != std::string::npos;
104         if (res) {
105             break;
106         }
107     }
108     COMMON::CustomPclose(fp, pipeFds, childPid);
109     PROFILER_LOG_INFO(LOG_CORE, "run command result: %s", result.c_str());
110     CHECK_TRUE(res, false, "HiperfPlugin::RunCommand: execute command FAILED!");
111     return true;
112 }
113 } // namespace
114 
HiperfPluginSessionStart(const uint8_t * configData,const uint32_t configSize)115 int HiperfPluginSessionStart(const uint8_t* configData, const uint32_t configSize)
116 {
117     std::lock_guard<std::mutex> guard(g_taskMutex);
118     (void)remove("/data/local/tmp/perf.data");
119     bool res = g_config.ParseFromArray(configData, configSize);
120     CHECK_TRUE(res, -1, "HiperfPluginSessionStart, parse config from array FAILED! configSize: %u", configSize);
121 
122     if (!g_config.split_outfile_name().empty()) {
123         g_splitTraceWriter = std::make_shared<TraceFileWriter>(g_config.split_outfile_name());
124         g_splitTraceWriter->WriteStandalonePluginData(
125             std::string(g_pluginModule.name) + "_config",
126             std::string(reinterpret_cast<const char *>(configData),
127                         configSize));
128         g_splitTraceWriter->SetTimeSource();
129     }
130 
131     std::vector<std::string> cmds;
132     res = ParseConfigToCmd(g_config, cmds);
133     CHECK_TRUE(res, -1, "HiperfPluginSessionStart, parse config FAILED!");
134 
135     for (const auto &cmd : cmds) {
136         res = RunCommand(cmd);
137         CHECK_TRUE(res, -1, "HiperfPluginSessionStart, RunCommand(%s) FAILED!", cmd.c_str());
138     }
139 
140     return 0;
141 }
142 
HiperfPluginSessionStop(void)143 int HiperfPluginSessionStop(void)
144 {
145     std::lock_guard<std::mutex> guard(g_taskMutex);
146     if (!g_config.split_outfile_name().empty()) {
147         CHECK_NOTNULL(g_splitTraceWriter, -1, "%s: writer is nullptr, SetDurationTime failed", __func__);
148         g_splitTraceWriter->SetDurationTime();
149     }
150 
151     std::string cmd;
152     if (g_isRoot) {
153         cmd = SU_ROOT;
154     }
155     cmd += HIPERF_CMD + g_logLevel + HIPERF_RECORD_CMD;
156     cmd += HIPERF_RECORD_STOP;
157     RunCommand(cmd);
158     usleep(250000); // 250000: wait for perf.data
159 
160     if (!g_config.split_outfile_name().empty()) { // write split file.
161         CHECK_NOTNULL(g_splitTraceWriter, -1, "%s: writer is nullptr, WriteStandaloneFile failed", __func__);
162         g_splitTraceWriter->WriteStandalonePluginFile(std::string(g_pluginModule.outFileName),
163             std::string(g_pluginModule.name), std::string(g_pluginModule.version), DataType::HIPERF_DATA);
164         g_splitTraceWriter->Finish();
165         g_splitTraceWriter.reset();
166         g_splitTraceWriter = nullptr;
167     }
168     return 0;
169 }
170 
HiperfRegisterWriterStruct(const WriterStruct * writer)171 int HiperfRegisterWriterStruct(const WriterStruct* writer)
172 {
173     PROFILER_LOG_INFO(LOG_CORE, "%s:writer", __func__);
174     return 0;
175 }
176 
177 static PluginModuleCallbacks g_callbacks = {
178     .onPluginSessionStart = HiperfPluginSessionStart,
179     .onPluginReportResult = 0,
180     .onPluginSessionStop = HiperfPluginSessionStop,
181     .onRegisterWriterStruct = HiperfRegisterWriterStruct,
182 };
183 
184 EXPORT_API PluginModuleStruct g_pluginModule = {
185     .callbacks = &g_callbacks,
186     .name = "hiperf-plugin",
187     .version = "1.02",
188     .resultBufferSizeHint = MAX_BUFFER_SIZE,
189     .isStandaloneFileData = true,
190     .outFileName = "/data/local/tmp/perf.data",
191 };