106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci
1606f6ba60Sopenharmony_ci#include <array>
1706f6ba60Sopenharmony_ci#include <dlfcn.h>
1806f6ba60Sopenharmony_ci#include <fcntl.h>
1906f6ba60Sopenharmony_ci#include <gtest/gtest.h>
2006f6ba60Sopenharmony_ci#include <sys/stat.h>
2106f6ba60Sopenharmony_ci#include <sys/syscall.h>
2206f6ba60Sopenharmony_ci#include <unistd.h>
2306f6ba60Sopenharmony_ci
2406f6ba60Sopenharmony_ci#include "logging.h"
2506f6ba60Sopenharmony_ci#include "openssl/sha.h"
2606f6ba60Sopenharmony_ci
2706f6ba60Sopenharmony_ciusing namespace testing::ext;
2806f6ba60Sopenharmony_ci
2906f6ba60Sopenharmony_ci#define HHB(v) (((v) & 0xF0) >> 4)
3006f6ba60Sopenharmony_ci#define LHB(v)  ((v) & 0x0F)
3106f6ba60Sopenharmony_ci
3206f6ba60Sopenharmony_cinamespace {
3306f6ba60Sopenharmony_ci#if defined(__LP64__)
3406f6ba60Sopenharmony_ciconst std::string DEFAULT_SO_PATH("/system/lib64/");
3506f6ba60Sopenharmony_ci#else
3606f6ba60Sopenharmony_ciconst std::string DEFAULT_SO_PATH("/system/lib/");
3706f6ba60Sopenharmony_ci#endif
3806f6ba60Sopenharmony_ciconst std::string DEFAULT_HIPROFILERD_PATH("/system/bin/hiprofilerd");
3906f6ba60Sopenharmony_ciconst std::string DEFAULT_HIPROFILER_PLUGINS_PATH("/system/bin/hiprofiler_plugins");
4006f6ba60Sopenharmony_ciconst std::string DEFAULT_HIPROFILERD_NAME("hiprofilerd");
4106f6ba60Sopenharmony_ciconst std::string DEFAULT_HIPROFILER_PLUGINS_NAME("hiprofiler_plugins");
4206f6ba60Sopenharmony_ci
4306f6ba60Sopenharmony_ciconst std::string DEFAULT_HIPROFILER_CMD_PATH("/system/bin/hiprofiler_cmd");
4406f6ba60Sopenharmony_ciconst std::string FTRACE_PLUGIN_PATH("/data/local/tmp/libftrace_plugin.z.so");
4506f6ba60Sopenharmony_ciconst std::string HIPERF_PLUGIN_PATH("/data/local/tmp/libhiperfplugin.z.so");
4606f6ba60Sopenharmony_cistd::string DEFAULT_PATH("/data/local/tmp/");
4706f6ba60Sopenharmony_ciconstexpr uint32_t READ_BUFFER_SIZE = 1024;
4806f6ba60Sopenharmony_ciconstexpr int SLEEP_TIME = 3;
4906f6ba60Sopenharmony_ciconstexpr int FILE_READ_CHUNK_SIZE = 4096;
5006f6ba60Sopenharmony_ciconstexpr char HEX_CHARS[] = "0123456789abcdef";
5106f6ba60Sopenharmony_ciconstexpr int LINE_SIZE = 1000;
5206f6ba60Sopenharmony_ci
5306f6ba60Sopenharmony_ci
5406f6ba60Sopenharmony_ciclass HiprofilerCmdTest : public ::testing::Test {
5506f6ba60Sopenharmony_cipublic:
5606f6ba60Sopenharmony_ci    static void SetUpTestCase() {}
5706f6ba60Sopenharmony_ci    static void TearDownTestCase() {}
5806f6ba60Sopenharmony_ci
5906f6ba60Sopenharmony_ci    void StartServerStub(std::string name)
6006f6ba60Sopenharmony_ci    {
6106f6ba60Sopenharmony_ci        if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
6206f6ba60Sopenharmony_ci            return;
6306f6ba60Sopenharmony_ci        }
6406f6ba60Sopenharmony_ci        int processNum = fork();
6506f6ba60Sopenharmony_ci        if (processNum == 0) {
6606f6ba60Sopenharmony_ci            if (DEFAULT_HIPROFILERD_PATH == name) {
6706f6ba60Sopenharmony_ci                // start running hiprofilerd
6806f6ba60Sopenharmony_ci                execl(name.c_str(), nullptr, nullptr);
6906f6ba60Sopenharmony_ci            } else if (DEFAULT_HIPROFILER_PLUGINS_PATH == name) {
7006f6ba60Sopenharmony_ci                // start running hiprofiler_plugins
7106f6ba60Sopenharmony_ci                execl(name.c_str(), DEFAULT_PATH.c_str(), nullptr);
7206f6ba60Sopenharmony_ci            }
7306f6ba60Sopenharmony_ci            _exit(1);
7406f6ba60Sopenharmony_ci        } else if (DEFAULT_HIPROFILERD_PATH == name) {
7506f6ba60Sopenharmony_ci            hiprofilerdPid_ = processNum;
7606f6ba60Sopenharmony_ci        } else if (DEFAULT_HIPROFILER_PLUGINS_PATH == name) {
7706f6ba60Sopenharmony_ci            hiprofilerPluginsPid_ = processNum;
7806f6ba60Sopenharmony_ci        }
7906f6ba60Sopenharmony_ci    }
8006f6ba60Sopenharmony_ci
8106f6ba60Sopenharmony_ci    void StopProcessStub(int processNum)
8206f6ba60Sopenharmony_ci    {
8306f6ba60Sopenharmony_ci        std::string stopCmd = "kill " + std::to_string(processNum);
8406f6ba60Sopenharmony_ci        std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(stopCmd.c_str(), "r"), pclose);
8506f6ba60Sopenharmony_ci        sleep(SLEEP_TIME); // wait process exit
8606f6ba60Sopenharmony_ci    }
8706f6ba60Sopenharmony_ci
8806f6ba60Sopenharmony_ci    bool RunCommand(const std::string& cmd, std::string& content)
8906f6ba60Sopenharmony_ci    {
9006f6ba60Sopenharmony_ci        std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
9106f6ba60Sopenharmony_ci        CHECK_TRUE(pipe, false, "RunCommand: create popen FAILED!");
9206f6ba60Sopenharmony_ci        std::array<char, READ_BUFFER_SIZE> buffer;
9306f6ba60Sopenharmony_ci        while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
9406f6ba60Sopenharmony_ci            content += buffer.data();
9506f6ba60Sopenharmony_ci        }
9606f6ba60Sopenharmony_ci        return true;
9706f6ba60Sopenharmony_ci    }
9806f6ba60Sopenharmony_ci
9906f6ba60Sopenharmony_ci    std::string ComputeFileSha256(const std::string& path)
10006f6ba60Sopenharmony_ci    {
10106f6ba60Sopenharmony_ci        uint8_t out[SHA256_DIGEST_LENGTH];
10206f6ba60Sopenharmony_ci        uint8_t buffer[FILE_READ_CHUNK_SIZE];
10306f6ba60Sopenharmony_ci        char realPath[PATH_MAX + 1] = {0};
10406f6ba60Sopenharmony_ci
10506f6ba60Sopenharmony_ci        SHA256_CTX sha;
10606f6ba60Sopenharmony_ci        SHA256_Init(&sha);
10706f6ba60Sopenharmony_ci
10806f6ba60Sopenharmony_ci        size_t nbytes = 0;
10906f6ba60Sopenharmony_ci
11006f6ba60Sopenharmony_ci        if ((strlen(path.c_str()) >= PATH_MAX) || (realpath(path.c_str(), realPath) == nullptr)) {
11106f6ba60Sopenharmony_ci            PROFILER_LOG_ERROR(LOG_CORE, "%s:path is invalid: %s, errno=%d", __func__, path.c_str(), errno);
11206f6ba60Sopenharmony_ci            return "";
11306f6ba60Sopenharmony_ci        }
11406f6ba60Sopenharmony_ci        FILE* file = fopen(realPath, "rb");
11506f6ba60Sopenharmony_ci        if (file == nullptr) {
11606f6ba60Sopenharmony_ci            return "";
11706f6ba60Sopenharmony_ci        }
11806f6ba60Sopenharmony_ci
11906f6ba60Sopenharmony_ci        std::unique_ptr<FILE, decltype(fclose)*> fptr(file, fclose);
12006f6ba60Sopenharmony_ci        if (fptr == nullptr) {
12106f6ba60Sopenharmony_ci            return "";
12206f6ba60Sopenharmony_ci        }
12306f6ba60Sopenharmony_ci
12406f6ba60Sopenharmony_ci        while ((nbytes = fread(buffer, 1, sizeof(buffer), fptr.get())) > 0) {
12506f6ba60Sopenharmony_ci            SHA256_Update(&sha, buffer, nbytes);
12606f6ba60Sopenharmony_ci        }
12706f6ba60Sopenharmony_ci        SHA256_Final(out, &sha);
12806f6ba60Sopenharmony_ci
12906f6ba60Sopenharmony_ci        std::string result;
13006f6ba60Sopenharmony_ci        result.reserve(SHA256_DIGEST_LENGTH + SHA256_DIGEST_LENGTH);
13106f6ba60Sopenharmony_ci        for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
13206f6ba60Sopenharmony_ci            result.push_back(HEX_CHARS[HHB(out[i])]);
13306f6ba60Sopenharmony_ci            result.push_back(HEX_CHARS[LHB(out[i])]);
13406f6ba60Sopenharmony_ci        }
13506f6ba60Sopenharmony_ci
13606f6ba60Sopenharmony_ci        PROFILER_LOG_DEBUG(LOG_CORE, "%s:%s-(%s)", __func__, path.c_str(), result.c_str());
13706f6ba60Sopenharmony_ci        return result;
13806f6ba60Sopenharmony_ci    }
13906f6ba60Sopenharmony_ci
14006f6ba60Sopenharmony_ci    void CreateConfigFile(const std::string configFile)
14106f6ba60Sopenharmony_ci    {
14206f6ba60Sopenharmony_ci        // 构建config文件
14306f6ba60Sopenharmony_ci        std::string configStr =
14406f6ba60Sopenharmony_ci            "request_id: 26\n"
14506f6ba60Sopenharmony_ci            "session_config {\n"
14606f6ba60Sopenharmony_ci            "  buffers {\n"
14706f6ba60Sopenharmony_ci            "    pages: 1000\n"
14806f6ba60Sopenharmony_ci            "  }\n"
14906f6ba60Sopenharmony_ci            "  result_file: \"/data/local/tmp/hiprofiler_data.htrace\"\n"
15006f6ba60Sopenharmony_ci            "  sample_duration: 10000\n"
15106f6ba60Sopenharmony_ci            "}\n"
15206f6ba60Sopenharmony_ci            "plugin_configs {\n"
15306f6ba60Sopenharmony_ci            "  plugin_name: \"ftrace-plugin\"\n"
15406f6ba60Sopenharmony_ci            "  sample_interval: 2000\n"
15506f6ba60Sopenharmony_ci            "  config_data: {\n"
15606f6ba60Sopenharmony_ci            "    ftrace_events: \"sched/sched_switch\"\n"
15706f6ba60Sopenharmony_ci            "    ftrace_events: \"sched/sched_wakeup\"\n"
15806f6ba60Sopenharmony_ci            "    ftrace_events: \"sched/sched_wakeup_new\"\n"
15906f6ba60Sopenharmony_ci            "    ftrace_events: \"sched/sched_waking\"\n"
16006f6ba60Sopenharmony_ci            "    ftrace_events: \"sched/sched_process_exit\"\n"
16106f6ba60Sopenharmony_ci            "    ftrace_events: \"sched/sched_process_free\"\n"
16206f6ba60Sopenharmony_ci            "    buffer_size_kb: 51200\n"
16306f6ba60Sopenharmony_ci            "    flush_interval_ms: 1000\n"
16406f6ba60Sopenharmony_ci            "    flush_threshold_kb: 4096\n"
16506f6ba60Sopenharmony_ci            "    parse_ksyms: true\n"
16606f6ba60Sopenharmony_ci            "    clock: \"mono\"\n"
16706f6ba60Sopenharmony_ci            "    trace_period_ms: 200\n"
16806f6ba60Sopenharmony_ci            "    debug_on: false\n"
16906f6ba60Sopenharmony_ci            "  }\n"
17006f6ba60Sopenharmony_ci            "}\n";
17106f6ba60Sopenharmony_ci
17206f6ba60Sopenharmony_ci        // 根据构建的config写文件
17306f6ba60Sopenharmony_ci        FILE* writeFp = fopen(configFile.c_str(), "w");
17406f6ba60Sopenharmony_ci        if (writeFp == nullptr) {
17506f6ba60Sopenharmony_ci            const int bufSize = 256;
17606f6ba60Sopenharmony_ci            char buf[bufSize] = { 0 };
17706f6ba60Sopenharmony_ci            strerror_r(errno, buf, bufSize);
17806f6ba60Sopenharmony_ci            PROFILER_LOG_ERROR(LOG_CORE, "CreateConfigFile: fopen() error = %s", buf);
17906f6ba60Sopenharmony_ci            return;
18006f6ba60Sopenharmony_ci        }
18106f6ba60Sopenharmony_ci
18206f6ba60Sopenharmony_ci        size_t len = fwrite(const_cast<char*>(configStr.c_str()), 1, configStr.length(), writeFp);
18306f6ba60Sopenharmony_ci        if (len < 0) {
18406f6ba60Sopenharmony_ci            const int bufSize = 256;
18506f6ba60Sopenharmony_ci            char buf[bufSize] = { 0 };
18606f6ba60Sopenharmony_ci            strerror_r(errno, buf, bufSize);
18706f6ba60Sopenharmony_ci            PROFILER_LOG_ERROR(LOG_CORE, "CreateConfigFile: fwrite() error = %s", buf);
18806f6ba60Sopenharmony_ci            if (fclose(writeFp) != 0) {
18906f6ba60Sopenharmony_ci                PROFILER_LOG_ERROR(LOG_CORE, "fclose() error");
19006f6ba60Sopenharmony_ci            }
19106f6ba60Sopenharmony_ci            return;
19206f6ba60Sopenharmony_ci        }
19306f6ba60Sopenharmony_ci
19406f6ba60Sopenharmony_ci        int ret = fflush(writeFp);
19506f6ba60Sopenharmony_ci        if (ret == EOF) {
19606f6ba60Sopenharmony_ci            const int bufSize = 256;
19706f6ba60Sopenharmony_ci            char buf[bufSize] = { 0 };
19806f6ba60Sopenharmony_ci            strerror_r(errno, buf, bufSize);
19906f6ba60Sopenharmony_ci            PROFILER_LOG_ERROR(LOG_CORE, "CreateConfigFile: fflush() error = %s", buf);
20006f6ba60Sopenharmony_ci            if (fclose(writeFp) != 0) {
20106f6ba60Sopenharmony_ci                PROFILER_LOG_ERROR(LOG_CORE, "fclose() error");
20206f6ba60Sopenharmony_ci            }
20306f6ba60Sopenharmony_ci            return;
20406f6ba60Sopenharmony_ci        }
20506f6ba60Sopenharmony_ci
20606f6ba60Sopenharmony_ci        fsync(fileno(writeFp));
20706f6ba60Sopenharmony_ci        ret = fclose(writeFp);
20806f6ba60Sopenharmony_ci        if (ret != 0) {
20906f6ba60Sopenharmony_ci            const int bufSize = 256;
21006f6ba60Sopenharmony_ci            char buf[bufSize] = { 0 };
21106f6ba60Sopenharmony_ci            strerror_r(errno, buf, bufSize);
21206f6ba60Sopenharmony_ci            PROFILER_LOG_ERROR(LOG_CORE, "CreateConfigFile: fclose() error = %s", buf);
21306f6ba60Sopenharmony_ci            return;
21406f6ba60Sopenharmony_ci        }
21506f6ba60Sopenharmony_ci    }
21606f6ba60Sopenharmony_ci
21706f6ba60Sopenharmony_ci    std::string CreateCommand(const std::string &outFile, int time) const
21806f6ba60Sopenharmony_ci    {
21906f6ba60Sopenharmony_ci        std::string cmdStr =
22006f6ba60Sopenharmony_ci            "hiprofiler_cmd \\\n"
22106f6ba60Sopenharmony_ci            "-c - \\\n";
22206f6ba60Sopenharmony_ci        cmdStr += "-o " + outFile + " \\\n";
22306f6ba60Sopenharmony_ci        cmdStr += "-t " + std::to_string(time) + " \\\n";
22406f6ba60Sopenharmony_ci        cmdStr += "-k \\\n"
22506f6ba60Sopenharmony_ci            "<<CONFIG\n"
22606f6ba60Sopenharmony_ci            "request_id: 1\n"
22706f6ba60Sopenharmony_ci            "session_config {\n"
22806f6ba60Sopenharmony_ci            "  buffers {\n"
22906f6ba60Sopenharmony_ci            "    pages: 1000\n"
23006f6ba60Sopenharmony_ci            "  }\n"
23106f6ba60Sopenharmony_ci            "  result_file: \"/data/local/tmp/hiprofiler_data.htrace\"\n"
23206f6ba60Sopenharmony_ci            "  sample_duration: 1000\n"
23306f6ba60Sopenharmony_ci            "}\n"
23406f6ba60Sopenharmony_ci            "plugin_configs {\n"
23506f6ba60Sopenharmony_ci            "  plugin_name: \"ftrace-plugin\"\n"
23606f6ba60Sopenharmony_ci            "  sample_interval: 1000\n"
23706f6ba60Sopenharmony_ci            "  is_protobuf_serialize: true\n"
23806f6ba60Sopenharmony_ci            "  config_data {\n"
23906f6ba60Sopenharmony_ci            "    ftrace_events: \"sched/sched_switch\"\n"
24006f6ba60Sopenharmony_ci            "    ftrace_events: \"sched/sched_wakeup\"\n"
24106f6ba60Sopenharmony_ci            "    ftrace_events: \"sched/sched_wakeup_new\"\n"
24206f6ba60Sopenharmony_ci            "    ftrace_events: \"sched/sched_waking\"\n"
24306f6ba60Sopenharmony_ci            "    ftrace_events: \"sched/sched_process_exit\"\n"
24406f6ba60Sopenharmony_ci            "    ftrace_events: \"sched/sched_process_free\"\n"
24506f6ba60Sopenharmony_ci            "    hitrace_categories: \"ability\"\n"
24606f6ba60Sopenharmony_ci            "    hitrace_categories: \"ace\"\n"
24706f6ba60Sopenharmony_ci            "    buffer_size_kb: 51200\n"
24806f6ba60Sopenharmony_ci            "    flush_interval_ms: 1000\n"
24906f6ba60Sopenharmony_ci            "    flush_threshold_kb: 4096\n"
25006f6ba60Sopenharmony_ci            "    parse_ksyms: true\n"
25106f6ba60Sopenharmony_ci            "    clock: \"mono\"\n"
25206f6ba60Sopenharmony_ci            "    trace_period_ms: 200\n"
25306f6ba60Sopenharmony_ci            "    debug_on: false\n"
25406f6ba60Sopenharmony_ci            "  }\n"
25506f6ba60Sopenharmony_ci            "}\n"
25606f6ba60Sopenharmony_ci            "CONFIG\n";
25706f6ba60Sopenharmony_ci        return cmdStr;
25806f6ba60Sopenharmony_ci    }
25906f6ba60Sopenharmony_ci
26006f6ba60Sopenharmony_ci    std::string CreateHiperfCommand(const std::string &outFile, int time) const
26106f6ba60Sopenharmony_ci    {
26206f6ba60Sopenharmony_ci        std::string cmdStr =
26306f6ba60Sopenharmony_ci            "hiprofiler_cmd \\\n"
26406f6ba60Sopenharmony_ci            "-c - \\\n";
26506f6ba60Sopenharmony_ci        cmdStr += "-o " + outFile + " \\\n";
26606f6ba60Sopenharmony_ci        cmdStr += "-t " + std::to_string(time) + " \\\n";
26706f6ba60Sopenharmony_ci        cmdStr += "-k \\\n"
26806f6ba60Sopenharmony_ci            "<<CONFIG\n"
26906f6ba60Sopenharmony_ci            "request_id: 1\n"
27006f6ba60Sopenharmony_ci            "session_config {\n"
27106f6ba60Sopenharmony_ci            "  buffers {\n"
27206f6ba60Sopenharmony_ci            "    pages: 1000\n"
27306f6ba60Sopenharmony_ci            "  }\n"
27406f6ba60Sopenharmony_ci            "  result_file: \"/data/local/tmp/hiprofiler_data.htrace\"\n"
27506f6ba60Sopenharmony_ci            "  sample_duration: 1000\n"
27606f6ba60Sopenharmony_ci            "}\n"
27706f6ba60Sopenharmony_ci            "plugin_configs {\n"
27806f6ba60Sopenharmony_ci            "  plugin_name: \"hiperf-plugin\"\n"
27906f6ba60Sopenharmony_ci            "  sample_interval: 1000\n"
28006f6ba60Sopenharmony_ci            "  is_protobuf_serialize: true\n"
28106f6ba60Sopenharmony_ci            "  config_data {\n"
28206f6ba60Sopenharmony_ci            "    is_root: false\n"
28306f6ba60Sopenharmony_ci            "    outfile_name: \"/data/local/tmp/perf.data\"\n"
28406f6ba60Sopenharmony_ci            "    record_args: \"-f 1000 -a --call-stack dwarf\"\n"
28506f6ba60Sopenharmony_ci            "  }\n"
28606f6ba60Sopenharmony_ci            "}\n"
28706f6ba60Sopenharmony_ci            "CONFIG\n";
28806f6ba60Sopenharmony_ci        return cmdStr;
28906f6ba60Sopenharmony_ci    }
29006f6ba60Sopenharmony_ci
29106f6ba60Sopenharmony_ci    std::string CreateEncoderCommand(const std::string &outFile, int time) const
29206f6ba60Sopenharmony_ci    {
29306f6ba60Sopenharmony_ci        std::string cmdStr =
29406f6ba60Sopenharmony_ci            "hiprofiler_cmd \\\n"
29506f6ba60Sopenharmony_ci            "-c - \\\n";
29606f6ba60Sopenharmony_ci        cmdStr += "-k \\\n";
29706f6ba60Sopenharmony_ci        cmdStr += "-o " + outFile + " \\\n";
29806f6ba60Sopenharmony_ci        cmdStr += "-t " + std::to_string(time) + " \\\n"
29906f6ba60Sopenharmony_ci            "<<CONFIG\n"
30006f6ba60Sopenharmony_ci            "request_id: 1\n"
30106f6ba60Sopenharmony_ci            "session_config {\n"
30206f6ba60Sopenharmony_ci            "  buffers {\n"
30306f6ba60Sopenharmony_ci            "    pages: 1000\n"
30406f6ba60Sopenharmony_ci            "  }\n"
30506f6ba60Sopenharmony_ci            "  result_file: \"/data/local/tmp/hiprofiler_data.htrace\"\n"
30606f6ba60Sopenharmony_ci            "  sample_duration: 3000\n"
30706f6ba60Sopenharmony_ci            "}\n"
30806f6ba60Sopenharmony_ci            "plugin_configs {\n"
30906f6ba60Sopenharmony_ci            "  plugin_name: \"ftrace-plugin\"\n"
31006f6ba60Sopenharmony_ci            "  sample_interval: 1000\n"
31106f6ba60Sopenharmony_ci            "  config_data {\n"
31206f6ba60Sopenharmony_ci            "    ftrace_events: \"sched/sched_switch\"\n"
31306f6ba60Sopenharmony_ci            "    ftrace_events: \"sched/sched_wakeup\"\n"
31406f6ba60Sopenharmony_ci            "    ftrace_events: \"sched/sched_wakeup_new\"\n"
31506f6ba60Sopenharmony_ci            "    ftrace_events: \"sched/sched_waking\"\n"
31606f6ba60Sopenharmony_ci            "    ftrace_events: \"sched/sched_process_exit\"\n"
31706f6ba60Sopenharmony_ci            "    ftrace_events: \"sched/sched_process_free\"\n"
31806f6ba60Sopenharmony_ci            "    hitrace_categories: \"ability\"\n"
31906f6ba60Sopenharmony_ci            "    hitrace_categories: \"ace\"\n"
32006f6ba60Sopenharmony_ci            "    buffer_size_kb: 51200\n"
32106f6ba60Sopenharmony_ci            "    flush_interval_ms: 1000\n"
32206f6ba60Sopenharmony_ci            "    flush_threshold_kb: 4096\n"
32306f6ba60Sopenharmony_ci            "    parse_ksyms: true\n"
32406f6ba60Sopenharmony_ci            "    clock: \"mono\"\n"
32506f6ba60Sopenharmony_ci            "    trace_period_ms: 200\n"
32606f6ba60Sopenharmony_ci            "    debug_on: false\n"
32706f6ba60Sopenharmony_ci            "  }\n"
32806f6ba60Sopenharmony_ci            "}\n"
32906f6ba60Sopenharmony_ci            "CONFIG\n";
33006f6ba60Sopenharmony_ci        return cmdStr;
33106f6ba60Sopenharmony_ci    }
33206f6ba60Sopenharmony_ci
33306f6ba60Sopenharmony_ci    std::string CreateSplitHtraceCommand(const std::string &outFile, int time) const
33406f6ba60Sopenharmony_ci    {
33506f6ba60Sopenharmony_ci        std::string cmdStr =
33606f6ba60Sopenharmony_ci            "hiprofiler_cmd -s -k \\\n"
33706f6ba60Sopenharmony_ci            "-c - \\\n";
33806f6ba60Sopenharmony_ci        cmdStr += "-o " + outFile + " \\\n";
33906f6ba60Sopenharmony_ci        cmdStr += "-t " + std::to_string(time) + " \\\n"
34006f6ba60Sopenharmony_ci            "<<CONFIG\n"
34106f6ba60Sopenharmony_ci            "request_id: 1\n"
34206f6ba60Sopenharmony_ci            "session_config {\n"
34306f6ba60Sopenharmony_ci            "  buffers {\n"
34406f6ba60Sopenharmony_ci            "    pages: 16384\n"
34506f6ba60Sopenharmony_ci            "  }\n"
34606f6ba60Sopenharmony_ci            "  split_file: true\n"
34706f6ba60Sopenharmony_ci            "}\n"
34806f6ba60Sopenharmony_ci            "plugin_configs {\n"
34906f6ba60Sopenharmony_ci            "  plugin_name: \"ftrace-plugin\"\n"
35006f6ba60Sopenharmony_ci            "  sample_interval: 1000\n"
35106f6ba60Sopenharmony_ci            "  config_data {\n"
35206f6ba60Sopenharmony_ci            "    ftrace_events: \"sched/sched_switch\"\n"
35306f6ba60Sopenharmony_ci            "    ftrace_events: \"sched/sched_wakeup\"\n"
35406f6ba60Sopenharmony_ci            "    ftrace_events: \"sched/sched_wakeup_new\"\n"
35506f6ba60Sopenharmony_ci            "    ftrace_events: \"sched/sched_waking\"\n"
35606f6ba60Sopenharmony_ci            "    ftrace_events: \"sched/sched_process_exit\"\n"
35706f6ba60Sopenharmony_ci            "    ftrace_events: \"sched/sched_process_free\"\n"
35806f6ba60Sopenharmony_ci            "    buffer_size_kb: 51200\n"
35906f6ba60Sopenharmony_ci            "    flush_interval_ms: 1000\n"
36006f6ba60Sopenharmony_ci            "    flush_threshold_kb: 4096\n"
36106f6ba60Sopenharmony_ci            "    parse_ksyms: true\n"
36206f6ba60Sopenharmony_ci            "    clock: \"mono\"\n"
36306f6ba60Sopenharmony_ci            "    trace_period_ms: 200\n"
36406f6ba60Sopenharmony_ci            "    debug_on: false\n"
36506f6ba60Sopenharmony_ci            "  }\n"
36606f6ba60Sopenharmony_ci            "}\n"
36706f6ba60Sopenharmony_ci            "CONFIG\n";
36806f6ba60Sopenharmony_ci        return cmdStr;
36906f6ba60Sopenharmony_ci    }
37006f6ba60Sopenharmony_ci
37106f6ba60Sopenharmony_ci    std::string CreateSplitHiperfCommand(const std::string &outFile, const std::string &perfFile,
37206f6ba60Sopenharmony_ci                                    const std::string &perfSplitFile, int time) const
37306f6ba60Sopenharmony_ci    {
37406f6ba60Sopenharmony_ci        std::string cmdStr =
37506f6ba60Sopenharmony_ci            "hiprofiler_cmd -s -k \\\n"
37606f6ba60Sopenharmony_ci            "-c - \\\n";
37706f6ba60Sopenharmony_ci        cmdStr += "-o " + outFile + " \\\n";
37806f6ba60Sopenharmony_ci        cmdStr += "-t " + std::to_string(time) + " \\\n"
37906f6ba60Sopenharmony_ci            "<<CONFIG\n"
38006f6ba60Sopenharmony_ci            "request_id: 1\n"
38106f6ba60Sopenharmony_ci            "session_config {\n"
38206f6ba60Sopenharmony_ci            "  buffers {\n"
38306f6ba60Sopenharmony_ci            "    pages: 16384\n"
38406f6ba60Sopenharmony_ci            "  }\n"
38506f6ba60Sopenharmony_ci            "  split_file: true\n"
38606f6ba60Sopenharmony_ci            "}\n"
38706f6ba60Sopenharmony_ci            "plugin_configs {\n"
38806f6ba60Sopenharmony_ci            "  plugin_name: \"hiperf-plugin\"\n"
38906f6ba60Sopenharmony_ci            "  config_data {\n"
39006f6ba60Sopenharmony_ci            "    is_root: false\n"
39106f6ba60Sopenharmony_ci            "    outfile_name: \"" + perfFile + "\"\n"
39206f6ba60Sopenharmony_ci            "    record_args: \"-f 1000 -a  --cpu-limit 100 -e hw-cpu-cycles,sched:sched_waking --call-stack dwarf --clockid monotonic --offcpu -m 256\"\n"
39306f6ba60Sopenharmony_ci            "    split_outfile_name: \"" + perfSplitFile + "\"\n"
39406f6ba60Sopenharmony_ci            "  }\n"
39506f6ba60Sopenharmony_ci            "}\n"
39606f6ba60Sopenharmony_ci            "CONFIG\n";
39706f6ba60Sopenharmony_ci        return cmdStr;
39806f6ba60Sopenharmony_ci    }
39906f6ba60Sopenharmony_ci
40006f6ba60Sopenharmony_ci    std::string CreateSplitHiebpfCommand(const std::string &outFile, const std::string &ebpfFile,
40106f6ba60Sopenharmony_ci                                            const std::string &ebpfSplitFile, int time) const
40206f6ba60Sopenharmony_ci    {
40306f6ba60Sopenharmony_ci        std::string cmdStr =
40406f6ba60Sopenharmony_ci            "hiprofiler_cmd -s -k \\\n"
40506f6ba60Sopenharmony_ci            "-c - \\\n";
40606f6ba60Sopenharmony_ci        cmdStr += "-o " + outFile + " \\\n";
40706f6ba60Sopenharmony_ci        cmdStr += "-t " + std::to_string(time) + " \\\n"
40806f6ba60Sopenharmony_ci            "<<CONFIG\n"
40906f6ba60Sopenharmony_ci            "request_id: 1\n"
41006f6ba60Sopenharmony_ci            "session_config {\n"
41106f6ba60Sopenharmony_ci            "  buffers {\n"
41206f6ba60Sopenharmony_ci            "    pages: 16384\n"
41306f6ba60Sopenharmony_ci            "  }\n"
41406f6ba60Sopenharmony_ci            "  split_file: true\n"
41506f6ba60Sopenharmony_ci            "}\n"
41606f6ba60Sopenharmony_ci            "plugin_configs {\n"
41706f6ba60Sopenharmony_ci            "  plugin_name: \"hiebpf-plugin\"\n"
41806f6ba60Sopenharmony_ci            "  config_data {\n"
41906f6ba60Sopenharmony_ci            "    cmd_line: \"hiebpf --events fs,ptrace,bio --duration 200 --max_stack_depth 10\"\n"
42006f6ba60Sopenharmony_ci            "    outfile_name: \"" + ebpfFile + "\"\n"
42106f6ba60Sopenharmony_ci            "    split_outfile_name: \"" + ebpfSplitFile + "\"\n"
42206f6ba60Sopenharmony_ci            "  }\n"
42306f6ba60Sopenharmony_ci            "}\n"
42406f6ba60Sopenharmony_ci            "CONFIG\n";
42506f6ba60Sopenharmony_ci        return cmdStr;
42606f6ba60Sopenharmony_ci    }
42706f6ba60Sopenharmony_ci
42806f6ba60Sopenharmony_ci    unsigned long GetFileSize(const char* filename)
42906f6ba60Sopenharmony_ci    {
43006f6ba60Sopenharmony_ci        struct stat buf;
43106f6ba60Sopenharmony_ci
43206f6ba60Sopenharmony_ci        if (stat(filename, &buf) < 0) {
43306f6ba60Sopenharmony_ci            return 0;
43406f6ba60Sopenharmony_ci        }
43506f6ba60Sopenharmony_ci        return static_cast<unsigned long>(buf.st_size);
43606f6ba60Sopenharmony_ci    }
43706f6ba60Sopenharmony_ci
43806f6ba60Sopenharmony_ci    void KillProcess(const std::string processName)
43906f6ba60Sopenharmony_ci    {
44006f6ba60Sopenharmony_ci        int pid = -1;
44106f6ba60Sopenharmony_ci        std::string findpid = "pidof " + processName;
44206f6ba60Sopenharmony_ci        PROFILER_LOG_INFO(LOG_CORE, "find pid command : %s", findpid.c_str());
44306f6ba60Sopenharmony_ci        std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(findpid.c_str(), "r"), pclose);
44406f6ba60Sopenharmony_ci
44506f6ba60Sopenharmony_ci        char line[LINE_SIZE];
44606f6ba60Sopenharmony_ci        do {
44706f6ba60Sopenharmony_ci            if (fgets(line, sizeof(line), pipe.get()) == nullptr) {
44806f6ba60Sopenharmony_ci                PROFILER_LOG_INFO(LOG_CORE, "not find processName : %s", processName.c_str());
44906f6ba60Sopenharmony_ci                return;
45006f6ba60Sopenharmony_ci            } else if (strlen(line) > 0 && isdigit(static_cast<unsigned char>(line[0]))) {
45106f6ba60Sopenharmony_ci                pid = atoi(line);
45206f6ba60Sopenharmony_ci                PROFILER_LOG_INFO(LOG_CORE, "find processName : %s, pid: %d", processName.c_str(), pid);
45306f6ba60Sopenharmony_ci                break;
45406f6ba60Sopenharmony_ci            }
45506f6ba60Sopenharmony_ci        } while (1);
45606f6ba60Sopenharmony_ci
45706f6ba60Sopenharmony_ci        if (pid != -1) {
45806f6ba60Sopenharmony_ci            StopProcessStub(pid);
45906f6ba60Sopenharmony_ci        }
46006f6ba60Sopenharmony_ci    }
46106f6ba60Sopenharmony_ciprivate:
46206f6ba60Sopenharmony_ci    int hiprofilerdPid_ = -1;
46306f6ba60Sopenharmony_ci    int hiprofilerPluginsPid_ = -1;
46406f6ba60Sopenharmony_ci};
46506f6ba60Sopenharmony_ci
46606f6ba60Sopenharmony_ci/**
46706f6ba60Sopenharmony_ci * @tc.name: hiprofiler_cmd
46806f6ba60Sopenharmony_ci * @tc.desc: Test hiprofiler_cmd with -h -q.
46906f6ba60Sopenharmony_ci * @tc.type: FUNC
47006f6ba60Sopenharmony_ci */
47106f6ba60Sopenharmony_ciHWTEST_F(HiprofilerCmdTest, DFX_DFR_Hiprofiler_0110, Function | MediumTest | Level1)
47206f6ba60Sopenharmony_ci{
47306f6ba60Sopenharmony_ci    KillProcess(DEFAULT_HIPROFILERD_NAME);
47406f6ba60Sopenharmony_ci    KillProcess(DEFAULT_HIPROFILER_PLUGINS_NAME);
47506f6ba60Sopenharmony_ci
47606f6ba60Sopenharmony_ci    std::string cmd = DEFAULT_HIPROFILER_CMD_PATH + " -h";
47706f6ba60Sopenharmony_ci    std::string content = "";
47806f6ba60Sopenharmony_ci    EXPECT_TRUE(RunCommand(cmd, content));
47906f6ba60Sopenharmony_ci    std::string destStr = "help";
48006f6ba60Sopenharmony_ci    EXPECT_EQ(strncmp(content.c_str(), destStr.c_str(), strlen(destStr.c_str())), 0);
48106f6ba60Sopenharmony_ci
48206f6ba60Sopenharmony_ci    content = "";
48306f6ba60Sopenharmony_ci    cmd = DEFAULT_HIPROFILER_CMD_PATH + " -q";
48406f6ba60Sopenharmony_ci    EXPECT_TRUE(RunCommand(cmd, content));
48506f6ba60Sopenharmony_ci    destStr = "Service not started";
48606f6ba60Sopenharmony_ci    EXPECT_EQ(strncmp(content.c_str(), destStr.c_str(), strlen(destStr.c_str())), 0);
48706f6ba60Sopenharmony_ci
48806f6ba60Sopenharmony_ci    StartServerStub(DEFAULT_HIPROFILERD_PATH);
48906f6ba60Sopenharmony_ci    sleep(1);
49006f6ba60Sopenharmony_ci    content = "";
49106f6ba60Sopenharmony_ci    EXPECT_TRUE(RunCommand(cmd, content));
49206f6ba60Sopenharmony_ci    destStr = "OK";
49306f6ba60Sopenharmony_ci    EXPECT_EQ(strncmp(content.c_str(), destStr.c_str(), strlen(destStr.c_str())), 0);
49406f6ba60Sopenharmony_ci    StopProcessStub(hiprofilerdPid_);
49506f6ba60Sopenharmony_ci}
49606f6ba60Sopenharmony_ci
49706f6ba60Sopenharmony_ci/**
49806f6ba60Sopenharmony_ci * @tc.name: hiprofiler_cmd
49906f6ba60Sopenharmony_ci * @tc.desc: Test hiprofiler_cmd with -c file.
50006f6ba60Sopenharmony_ci * @tc.type: FUNC
50106f6ba60Sopenharmony_ci */
50206f6ba60Sopenharmony_ciHWTEST_F(HiprofilerCmdTest, DFX_DFR_Hiprofiler_0120, Function | MediumTest | Level1)
50306f6ba60Sopenharmony_ci{
50406f6ba60Sopenharmony_ci    KillProcess(DEFAULT_HIPROFILERD_NAME);
50506f6ba60Sopenharmony_ci    KillProcess(DEFAULT_HIPROFILER_PLUGINS_NAME);
50606f6ba60Sopenharmony_ci
50706f6ba60Sopenharmony_ci    // 测试不存在的config文件
50806f6ba60Sopenharmony_ci    std::string configTestFile = DEFAULT_PATH + "1234.txt";
50906f6ba60Sopenharmony_ci    std::string outFile = DEFAULT_PATH + "trace.htrace";
51006f6ba60Sopenharmony_ci    std::string content = "";
51106f6ba60Sopenharmony_ci    std::string cmd = DEFAULT_HIPROFILER_CMD_PATH + " -c " + configTestFile + " -o " + outFile + " -t 3";
51206f6ba60Sopenharmony_ci    EXPECT_TRUE(RunCommand(cmd, content));
51306f6ba60Sopenharmony_ci    std::string destStr = "Read " + configTestFile + " fail";
51406f6ba60Sopenharmony_ci    EXPECT_TRUE(content.find(destStr) != std::string::npos);
51506f6ba60Sopenharmony_ci
51606f6ba60Sopenharmony_ci    // 创建有效的config文件
51706f6ba60Sopenharmony_ci    const std::string configFile = DEFAULT_PATH + "ftrace.config";
51806f6ba60Sopenharmony_ci    CreateConfigFile(configFile);
51906f6ba60Sopenharmony_ci
52006f6ba60Sopenharmony_ci    // 测试有效的config文件,不开启hiprofilerd和hiprofiler_plugin进程
52106f6ba60Sopenharmony_ci    content = "";
52206f6ba60Sopenharmony_ci    cmd = DEFAULT_HIPROFILER_CMD_PATH + " -c " + configFile + " -o " + outFile + " -t 3";
52306f6ba60Sopenharmony_ci    EXPECT_TRUE(RunCommand(cmd, content));
52406f6ba60Sopenharmony_ci    sleep(SLEEP_TIME);
52506f6ba60Sopenharmony_ci    EXPECT_NE(access(outFile.c_str(), F_OK), 0);
52606f6ba60Sopenharmony_ci
52706f6ba60Sopenharmony_ci    // 开启hiprofilerd和hiprofiler_plugin进程,可以生成trace文件
52806f6ba60Sopenharmony_ci    content = "";
52906f6ba60Sopenharmony_ci    StartServerStub(DEFAULT_HIPROFILERD_PATH);
53006f6ba60Sopenharmony_ci    sleep(1);
53106f6ba60Sopenharmony_ci    StartServerStub(DEFAULT_HIPROFILER_PLUGINS_PATH);
53206f6ba60Sopenharmony_ci    sleep(1);
53306f6ba60Sopenharmony_ci    EXPECT_TRUE(RunCommand(cmd, content));
53406f6ba60Sopenharmony_ci    sleep(SLEEP_TIME);
53506f6ba60Sopenharmony_ci    EXPECT_EQ(access(outFile.c_str(), F_OK), 0);
53606f6ba60Sopenharmony_ci
53706f6ba60Sopenharmony_ci    // 删除资源文件和生成的trace文件
53806f6ba60Sopenharmony_ci    cmd = "rm " + configFile + " " + outFile;
53906f6ba60Sopenharmony_ci    system(cmd.c_str());
54006f6ba60Sopenharmony_ci    StopProcessStub(hiprofilerPluginsPid_);
54106f6ba60Sopenharmony_ci    StopProcessStub(hiprofilerdPid_);
54206f6ba60Sopenharmony_ci}
54306f6ba60Sopenharmony_ci
54406f6ba60Sopenharmony_ci/**
54506f6ba60Sopenharmony_ci * @tc.name: hiprofiler_cmd
54606f6ba60Sopenharmony_ci * @tc.desc: Test hiprofiler_cmd with -c string.
54706f6ba60Sopenharmony_ci * @tc.type: FUNC
54806f6ba60Sopenharmony_ci */
54906f6ba60Sopenharmony_ciHWTEST_F(HiprofilerCmdTest, DFX_DFR_Hiprofiler_0130, Function | MediumTest | Level1)
55006f6ba60Sopenharmony_ci{
55106f6ba60Sopenharmony_ci    std::string cmd = "cp " + DEFAULT_SO_PATH + "libftrace_plugin.z.so " + DEFAULT_PATH;
55206f6ba60Sopenharmony_ci    system(cmd.c_str());
55306f6ba60Sopenharmony_ci
55406f6ba60Sopenharmony_ci    // 开启hiprofilerd和hiprofiler_plugin进程,验证字符串格式的config
55506f6ba60Sopenharmony_ci    std::string content = "";
55606f6ba60Sopenharmony_ci    StartServerStub(DEFAULT_HIPROFILERD_PATH);
55706f6ba60Sopenharmony_ci    sleep(1);
55806f6ba60Sopenharmony_ci    StartServerStub(DEFAULT_HIPROFILER_PLUGINS_PATH);
55906f6ba60Sopenharmony_ci    sleep(1);
56006f6ba60Sopenharmony_ci    std::string outFile = DEFAULT_PATH + "trace.htrace";
56106f6ba60Sopenharmony_ci    int time = 3;
56206f6ba60Sopenharmony_ci    cmd = CreateCommand(outFile, time);
56306f6ba60Sopenharmony_ci    EXPECT_TRUE(RunCommand(cmd, content));
56406f6ba60Sopenharmony_ci    sleep(time);
56506f6ba60Sopenharmony_ci    EXPECT_EQ(access(outFile.c_str(), F_OK), 0);
56606f6ba60Sopenharmony_ci
56706f6ba60Sopenharmony_ci    // 删除资源文件和生成的trace文件
56806f6ba60Sopenharmony_ci    cmd = "rm " + FTRACE_PLUGIN_PATH + " " + outFile;
56906f6ba60Sopenharmony_ci    system(cmd.c_str());
57006f6ba60Sopenharmony_ci    StopProcessStub(hiprofilerPluginsPid_);
57106f6ba60Sopenharmony_ci    StopProcessStub(hiprofilerdPid_);
57206f6ba60Sopenharmony_ci}
57306f6ba60Sopenharmony_ci
57406f6ba60Sopenharmony_ci/**
57506f6ba60Sopenharmony_ci * @tc.name: hiprofiler_cmd
57606f6ba60Sopenharmony_ci * @tc.desc: Test hiprofiler_cmd with -s -l -k.
57706f6ba60Sopenharmony_ci * @tc.type: FUNC
57806f6ba60Sopenharmony_ci */
57906f6ba60Sopenharmony_ciHWTEST_F(HiprofilerCmdTest, DFX_DFR_Hiprofiler_0140, Function | MediumTest | Level1)
58006f6ba60Sopenharmony_ci{
58106f6ba60Sopenharmony_ci    KillProcess(DEFAULT_HIPROFILERD_NAME);
58206f6ba60Sopenharmony_ci    KillProcess(DEFAULT_HIPROFILER_PLUGINS_NAME);
58306f6ba60Sopenharmony_ci
58406f6ba60Sopenharmony_ci    std::string cmd = DEFAULT_HIPROFILER_CMD_PATH + " -s -l -k";
58506f6ba60Sopenharmony_ci    std::string content = "";
58606f6ba60Sopenharmony_ci    EXPECT_TRUE(RunCommand(cmd, content));
58706f6ba60Sopenharmony_ci    std::string destStr = "plugin";
58806f6ba60Sopenharmony_ci    EXPECT_TRUE(content.find(destStr) != std::string::npos);
58906f6ba60Sopenharmony_ci}
59006f6ba60Sopenharmony_ci
59106f6ba60Sopenharmony_ci/**
59206f6ba60Sopenharmony_ci * @tc.name: hiprofiler_cmd
59306f6ba60Sopenharmony_ci * @tc.desc: Test hiprofiler_cmd with -l -k.
59406f6ba60Sopenharmony_ci * @tc.type: FUNC
59506f6ba60Sopenharmony_ci */
59606f6ba60Sopenharmony_ciHWTEST_F(HiprofilerCmdTest, DFX_DFR_Hiprofiler_0150, Function | MediumTest | Level1)
59706f6ba60Sopenharmony_ci{
59806f6ba60Sopenharmony_ci    KillProcess(DEFAULT_HIPROFILERD_NAME);
59906f6ba60Sopenharmony_ci    KillProcess(DEFAULT_HIPROFILER_PLUGINS_NAME);
60006f6ba60Sopenharmony_ci
60106f6ba60Sopenharmony_ci    std::string cmd = DEFAULT_HIPROFILER_CMD_PATH + " -l -k";
60206f6ba60Sopenharmony_ci    std::string content = "";
60306f6ba60Sopenharmony_ci    EXPECT_TRUE(RunCommand(cmd, content));
60406f6ba60Sopenharmony_ci}
60506f6ba60Sopenharmony_ci
60606f6ba60Sopenharmony_ci/**
60706f6ba60Sopenharmony_ci * @tc.name: hiprofiler_cmd
60806f6ba60Sopenharmony_ci * @tc.desc: Test hiprofiler_cmd with -k.
60906f6ba60Sopenharmony_ci * @tc.type: FUNC
61006f6ba60Sopenharmony_ci */
61106f6ba60Sopenharmony_ciHWTEST_F(HiprofilerCmdTest, DFX_DFR_Hiprofiler_0160, Function | MediumTest | Level1)
61206f6ba60Sopenharmony_ci{
61306f6ba60Sopenharmony_ci    KillProcess(DEFAULT_HIPROFILERD_NAME);
61406f6ba60Sopenharmony_ci    KillProcess(DEFAULT_HIPROFILER_PLUGINS_NAME);
61506f6ba60Sopenharmony_ci
61606f6ba60Sopenharmony_ci    std::string cmd = DEFAULT_HIPROFILER_CMD_PATH + " -k";
61706f6ba60Sopenharmony_ci    std::string content = "";
61806f6ba60Sopenharmony_ci    EXPECT_TRUE(RunCommand(cmd, content));
61906f6ba60Sopenharmony_ci}
62006f6ba60Sopenharmony_ci
62106f6ba60Sopenharmony_ci/**
62206f6ba60Sopenharmony_ci * @tc.name: hiprofiler_cmd
62306f6ba60Sopenharmony_ci * @tc.desc: Test hiprofiler_cmd with proto encoder.
62406f6ba60Sopenharmony_ci * @tc.type: FUNC
62506f6ba60Sopenharmony_ci */
62606f6ba60Sopenharmony_ciHWTEST_F(HiprofilerCmdTest, DFX_DFR_Hiprofiler_0170, Function | MediumTest | Level1)
62706f6ba60Sopenharmony_ci{
62806f6ba60Sopenharmony_ci    std::string cmd = "cp " + DEFAULT_SO_PATH + "libftrace_plugin.z.so " + DEFAULT_PATH;
62906f6ba60Sopenharmony_ci    system(cmd.c_str());
63006f6ba60Sopenharmony_ci
63106f6ba60Sopenharmony_ci    // 开启hiprofilerd和hiprofiler_plugin进程,验证字符串格式的config
63206f6ba60Sopenharmony_ci    std::string content = "";
63306f6ba60Sopenharmony_ci    StartServerStub(DEFAULT_HIPROFILERD_PATH);
63406f6ba60Sopenharmony_ci    sleep(1);
63506f6ba60Sopenharmony_ci    StartServerStub(DEFAULT_HIPROFILER_PLUGINS_PATH);
63606f6ba60Sopenharmony_ci    sleep(1);
63706f6ba60Sopenharmony_ci    std::string outFile = DEFAULT_PATH + "trace_encoder.htrace";
63806f6ba60Sopenharmony_ci    int time = 3;
63906f6ba60Sopenharmony_ci    cmd = CreateEncoderCommand(outFile, time);
64006f6ba60Sopenharmony_ci    EXPECT_TRUE(RunCommand(cmd, content));
64106f6ba60Sopenharmony_ci    sleep(time);
64206f6ba60Sopenharmony_ci    EXPECT_EQ(access(outFile.c_str(), F_OK), 0);
64306f6ba60Sopenharmony_ci
64406f6ba60Sopenharmony_ci    // 删除资源文件和生成的trace文件
64506f6ba60Sopenharmony_ci    cmd = "rm " + FTRACE_PLUGIN_PATH + " " + outFile;
64606f6ba60Sopenharmony_ci    system(cmd.c_str());
64706f6ba60Sopenharmony_ci    StopProcessStub(hiprofilerPluginsPid_);
64806f6ba60Sopenharmony_ci    StopProcessStub(hiprofilerdPid_);
64906f6ba60Sopenharmony_ci}
65006f6ba60Sopenharmony_ci
65106f6ba60Sopenharmony_ci/**
65206f6ba60Sopenharmony_ci * @tc.name: hiprofiler_cmd
65306f6ba60Sopenharmony_ci * @tc.desc: Test hiprofiler_cmd with ctrl+c.
65406f6ba60Sopenharmony_ci * @tc.type: FUNC
65506f6ba60Sopenharmony_ci */
65606f6ba60Sopenharmony_ciHWTEST_F(HiprofilerCmdTest, DFX_DFR_Hiprofiler_0180, Function | MediumTest | Level1)
65706f6ba60Sopenharmony_ci{
65806f6ba60Sopenharmony_ci    std::string content = "";
65906f6ba60Sopenharmony_ci    std::string cmd = DEFAULT_HIPROFILER_CMD_PATH + " -s";
66006f6ba60Sopenharmony_ci    EXPECT_TRUE(RunCommand(cmd, content));
66106f6ba60Sopenharmony_ci    sleep(2); // 2: wait hiprofilerd start
66206f6ba60Sopenharmony_ci    pid_t pid = fork();
66306f6ba60Sopenharmony_ci    EXPECT_GE(pid, 0);
66406f6ba60Sopenharmony_ci    if (pid == 0) {
66506f6ba60Sopenharmony_ci        content = "";
66606f6ba60Sopenharmony_ci        const int time = 20;
66706f6ba60Sopenharmony_ci        std::string outFile = DEFAULT_PATH + "trace.htrace";
66806f6ba60Sopenharmony_ci        cmd = CreateCommand(outFile, time);
66906f6ba60Sopenharmony_ci        EXPECT_TRUE(RunCommand(cmd, content));
67006f6ba60Sopenharmony_ci        EXPECT_EQ(access(outFile.c_str(), F_OK), 0);
67106f6ba60Sopenharmony_ci        // 删除生成的trace文件
67206f6ba60Sopenharmony_ci        cmd = "rm " + outFile;
67306f6ba60Sopenharmony_ci        system(cmd.c_str());
67406f6ba60Sopenharmony_ci        _exit(0);
67506f6ba60Sopenharmony_ci    } else if (pid > 0) {
67606f6ba60Sopenharmony_ci        sleep(1); // 1: wait child process start
67706f6ba60Sopenharmony_ci        content = "";
67806f6ba60Sopenharmony_ci        cmd = "pidof hiprofiler_cmd";
67906f6ba60Sopenharmony_ci        EXPECT_TRUE(RunCommand(cmd, content));
68006f6ba60Sopenharmony_ci        ASSERT_STRNE(content.c_str(), "");
68106f6ba60Sopenharmony_ci        cmd = "kill -2 " + content;
68206f6ba60Sopenharmony_ci        content = "";
68306f6ba60Sopenharmony_ci        EXPECT_TRUE(RunCommand(cmd, content));
68406f6ba60Sopenharmony_ci        EXPECT_STREQ(content.c_str(), "");
68506f6ba60Sopenharmony_ci        // 等待子进程结束
68606f6ba60Sopenharmony_ci        waitpid(pid, nullptr, 0);
68706f6ba60Sopenharmony_ci        cmd = DEFAULT_HIPROFILER_CMD_PATH + " -k";
68806f6ba60Sopenharmony_ci        EXPECT_TRUE(RunCommand(cmd, content));
68906f6ba60Sopenharmony_ci        sleep(5); // 5: wait hiprofilerd exit
69006f6ba60Sopenharmony_ci        content = "";
69106f6ba60Sopenharmony_ci        cmd = "pidof " + DEFAULT_HIPROFILERD_NAME;
69206f6ba60Sopenharmony_ci        EXPECT_TRUE(RunCommand(cmd, content));
69306f6ba60Sopenharmony_ci        EXPECT_STREQ(content.c_str(), "");
69406f6ba60Sopenharmony_ci    }
69506f6ba60Sopenharmony_ci}
69606f6ba60Sopenharmony_ci
69706f6ba60Sopenharmony_ci/**
69806f6ba60Sopenharmony_ci * @tc.name: hiprofiler_cmd
69906f6ba60Sopenharmony_ci * @tc.desc: Test hiprofiler_cmd with -c string.
70006f6ba60Sopenharmony_ci * @tc.type: FUNC
70106f6ba60Sopenharmony_ci */
70206f6ba60Sopenharmony_ciHWTEST_F(HiprofilerCmdTest, DFX_DFR_Hiprofiler_0190, Function | MediumTest | Level1)
70306f6ba60Sopenharmony_ci{
70406f6ba60Sopenharmony_ci    std::string cmd = "cp " + DEFAULT_SO_PATH + "libhiperfplugin.z.so " + DEFAULT_PATH;
70506f6ba60Sopenharmony_ci    system(cmd.c_str());
70606f6ba60Sopenharmony_ci
70706f6ba60Sopenharmony_ci    // 开启hiprofilerd和hiprofiler_plugin进程,验证字符串格式的config
70806f6ba60Sopenharmony_ci    std::string content = "";
70906f6ba60Sopenharmony_ci    StartServerStub(DEFAULT_HIPROFILERD_PATH);
71006f6ba60Sopenharmony_ci    sleep(1);
71106f6ba60Sopenharmony_ci    StartServerStub(DEFAULT_HIPROFILER_PLUGINS_PATH);
71206f6ba60Sopenharmony_ci    sleep(1);
71306f6ba60Sopenharmony_ci    std::string outFile = DEFAULT_PATH + "trace.htrace";
71406f6ba60Sopenharmony_ci    int time = 10;
71506f6ba60Sopenharmony_ci    cmd = CreateHiperfCommand(outFile, time);
71606f6ba60Sopenharmony_ci    EXPECT_TRUE(RunCommand(cmd, content));
71706f6ba60Sopenharmony_ci    sleep(time);
71806f6ba60Sopenharmony_ci    EXPECT_EQ(access(outFile.c_str(), F_OK), 0);
71906f6ba60Sopenharmony_ci
72006f6ba60Sopenharmony_ci    // 删除资源文件和生成的trace文件
72106f6ba60Sopenharmony_ci    cmd = "rm " + HIPERF_PLUGIN_PATH + " " + outFile;
72206f6ba60Sopenharmony_ci    system(cmd.c_str());
72306f6ba60Sopenharmony_ci    StopProcessStub(hiprofilerPluginsPid_);
72406f6ba60Sopenharmony_ci    StopProcessStub(hiprofilerdPid_);
72506f6ba60Sopenharmony_ci}
72606f6ba60Sopenharmony_ci}
72706f6ba60Sopenharmony_ci
72806f6ba60Sopenharmony_ci/**
72906f6ba60Sopenharmony_ci * @tc.name: hiprofiler_cmd
73006f6ba60Sopenharmony_ci * @tc.desc: Test hiprofiler_cmd with split Htrace file.
73106f6ba60Sopenharmony_ci * @tc.type: FUNC
73206f6ba60Sopenharmony_ci */
73306f6ba60Sopenharmony_ciHWTEST_F(HiprofilerCmdTest, DFX_DFR_Hiprofiler_0200, Function | MediumTest | Level1)
73406f6ba60Sopenharmony_ci{
73506f6ba60Sopenharmony_ci    std::string outFileName = "split_htrace";
73606f6ba60Sopenharmony_ci    std::string outFile = DEFAULT_PATH + outFileName + ".htrace";
73706f6ba60Sopenharmony_ci    std::string content = "";
73806f6ba60Sopenharmony_ci    int time = 10;
73906f6ba60Sopenharmony_ci    std::string cmd = CreateSplitHtraceCommand(outFile, time);
74006f6ba60Sopenharmony_ci    EXPECT_TRUE(RunCommand(cmd, content));
74106f6ba60Sopenharmony_ci
74206f6ba60Sopenharmony_ci    EXPECT_NE(access(outFile.c_str(), F_OK), 0);
74306f6ba60Sopenharmony_ci
74406f6ba60Sopenharmony_ci    cmd = "ls " + DEFAULT_PATH + outFileName + "*_1.htrace";
74506f6ba60Sopenharmony_ci    EXPECT_TRUE(RunCommand(cmd, content));
74606f6ba60Sopenharmony_ci    EXPECT_STRNE(content.c_str(), "");
74706f6ba60Sopenharmony_ci
74806f6ba60Sopenharmony_ci    cmd = "rm " + DEFAULT_PATH + outFileName + "*.htrace";
74906f6ba60Sopenharmony_ci    system(cmd.c_str());
75006f6ba60Sopenharmony_ci}
75106f6ba60Sopenharmony_ci
75206f6ba60Sopenharmony_ci/**
75306f6ba60Sopenharmony_ci * @tc.name: hiprofiler_cmd
75406f6ba60Sopenharmony_ci * @tc.desc: Test hiprofiler_cmd with split hiperf file.
75506f6ba60Sopenharmony_ci * @tc.type: FUNC
75606f6ba60Sopenharmony_ci */
75706f6ba60Sopenharmony_ciHWTEST_F(HiprofilerCmdTest, DFX_DFR_Hiprofiler_0220, Function | MediumTest | Level1)
75806f6ba60Sopenharmony_ci{
75906f6ba60Sopenharmony_ci    std::string outFileName = "split_hiperf";
76006f6ba60Sopenharmony_ci    std::string outFile = DEFAULT_PATH + outFileName + ".htrace";
76106f6ba60Sopenharmony_ci    std::string perfFile = "/data/local/tmp/perf.data";
76206f6ba60Sopenharmony_ci    std::string perfSplitFile = "/data/local/tmp/split_perf_data.htrace";
76306f6ba60Sopenharmony_ci    std::string content = "";
76406f6ba60Sopenharmony_ci    int time = 10;
76506f6ba60Sopenharmony_ci    std::string cmd = CreateSplitHiperfCommand(outFile, perfFile, perfSplitFile, time);
76606f6ba60Sopenharmony_ci    EXPECT_TRUE(RunCommand(cmd, content));
76706f6ba60Sopenharmony_ci
76806f6ba60Sopenharmony_ci    EXPECT_NE(access(outFile.c_str(), F_OK), 0);
76906f6ba60Sopenharmony_ci
77006f6ba60Sopenharmony_ci    cmd = "ls " + DEFAULT_PATH + outFileName + "*_1.htrace";
77106f6ba60Sopenharmony_ci    EXPECT_TRUE(RunCommand(cmd, content));
77206f6ba60Sopenharmony_ci    EXPECT_STRNE(content.c_str(), "");
77306f6ba60Sopenharmony_ci
77406f6ba60Sopenharmony_ci    EXPECT_EQ(access(perfSplitFile.c_str(), F_OK), 0);
77506f6ba60Sopenharmony_ci    if (access(perfFile.c_str(), F_OK) == 0) {
77606f6ba60Sopenharmony_ci        const int headerSize = 1024 + 1024; // htrace header + hiperf header
77706f6ba60Sopenharmony_ci        auto perfFileSize = GetFileSize(perfFile.c_str());
77806f6ba60Sopenharmony_ci        auto perfSplitFileSize = GetFileSize(perfSplitFile.c_str());
77906f6ba60Sopenharmony_ci        EXPECT_GT(perfSplitFileSize, perfFileSize + headerSize);
78006f6ba60Sopenharmony_ci    }
78106f6ba60Sopenharmony_ci
78206f6ba60Sopenharmony_ci    cmd = "rm " + DEFAULT_PATH + outFileName + "*.htrace " + perfFile + " " + perfSplitFile;
78306f6ba60Sopenharmony_ci    system(cmd.c_str());
78406f6ba60Sopenharmony_ci}
78506f6ba60Sopenharmony_ci/**
78606f6ba60Sopenharmony_ci * @tc.name: hiprofiler_cmd
78706f6ba60Sopenharmony_ci * @tc.desc: Test hiprofiler_cmd with split hiebpf file.
78806f6ba60Sopenharmony_ci * @tc.type: FUNC
78906f6ba60Sopenharmony_ci */
79006f6ba60Sopenharmony_ciHWTEST_F(HiprofilerCmdTest, DFX_DFR_Hiprofiler_0230, Function | MediumTest | Level1)
79106f6ba60Sopenharmony_ci{
79206f6ba60Sopenharmony_ci#if defined(__LP64__)  // just support 64bit
79306f6ba60Sopenharmony_ci    std::string outFileName = "split_hiebpf";
79406f6ba60Sopenharmony_ci    std::string outFile = DEFAULT_PATH + outFileName + ".htrace";
79506f6ba60Sopenharmony_ci    std::string ebpfFile = "/data/local/tmp/ebpf.data";
79606f6ba60Sopenharmony_ci    std::string ebpfSplitFile = "/data/local/tmp/split_ebpf_data.htrace";
79706f6ba60Sopenharmony_ci    std::string content = "";
79806f6ba60Sopenharmony_ci    int time = 10;
79906f6ba60Sopenharmony_ci    std::string cmd = CreateSplitHiebpfCommand(outFile, ebpfFile, ebpfSplitFile, time);
80006f6ba60Sopenharmony_ci    EXPECT_TRUE(RunCommand(cmd, content));
80106f6ba60Sopenharmony_ci
80206f6ba60Sopenharmony_ci    EXPECT_NE(access(outFile.c_str(), F_OK), 0);
80306f6ba60Sopenharmony_ci
80406f6ba60Sopenharmony_ci    cmd = "ls " + DEFAULT_PATH + outFileName + "*_1.htrace";
80506f6ba60Sopenharmony_ci    EXPECT_TRUE(RunCommand(cmd, content));
80606f6ba60Sopenharmony_ci    EXPECT_STRNE(content.c_str(), "");
80706f6ba60Sopenharmony_ci
80806f6ba60Sopenharmony_ci    EXPECT_EQ(access(ebpfSplitFile.c_str(), F_OK), 0);
80906f6ba60Sopenharmony_ci    if (access(ebpfFile.c_str(), F_OK) == 0) {
81006f6ba60Sopenharmony_ci        const int headerSize = 1024 + 1024; // htrace header + hiebpf header
81106f6ba60Sopenharmony_ci        auto ebpfFileSize = GetFileSize(ebpfFile.c_str());
81206f6ba60Sopenharmony_ci        auto ebpfSplitFileSize = GetFileSize(ebpfSplitFile.c_str());
81306f6ba60Sopenharmony_ci        EXPECT_GT(ebpfSplitFileSize, ebpfFileSize + headerSize);
81406f6ba60Sopenharmony_ci    }
81506f6ba60Sopenharmony_ci
81606f6ba60Sopenharmony_ci    cmd = "rm " + DEFAULT_PATH + outFileName + "*.htrace " + ebpfFile + " " + ebpfSplitFile;
81706f6ba60Sopenharmony_ci    system(cmd.c_str());
81806f6ba60Sopenharmony_ci#endif
81906f6ba60Sopenharmony_ci}
820