!632 增加hiperf命令统计打点

Merge pull request !632 from yuanye/hisysevent
This commit is contained in:
openharmony_ci 2024-11-06 01:35:46 +00:00 committed by Gitee
commit fbf6cd6dfe
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
12 changed files with 272 additions and 8 deletions

View File

@ -116,6 +116,7 @@ config("hiperf_inner_config") {
sources_platform_common = [
"./src/command.cpp",
"./src/command_reporter.cpp",
"./src/dwarf_encoding.cpp",
"./src/ipc_utilities.cpp",
"./src/option.cpp",
@ -202,6 +203,7 @@ ohos_source_set("hiperf_platform_common") {
"c_utils:utils",
"faultloggerd:libunwinder_static",
"hilog:libhilog",
"hisysevent:libhisysevent",
"init:libbegetutil",
"ipc:ipc_core",
"samgr:samgr_proxy",

View File

@ -19,16 +19,17 @@
"ram": "2000KB",
"deps": {
"components": [
"hilog",
"napi",
"samgr",
"ipc",
"c_utils",
"bundle_framework",
"faultloggerd",
"init",
"ability_base",
"bounds_checking_function",
"bundle_framework",
"c_utils",
"faultloggerd",
"hilog",
"hisysevent",
"init",
"ipc",
"napi",
"samgr",
"zlib"
],
"third_party": [

View File

@ -0,0 +1,55 @@
/*
* Copyright (c) 2024-2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef COMMAND_REPORTER_H_
#define COMMAND_REPORTER_H_
#include <string>
#include <gtest/gtest_prod.h>
namespace OHOS::Developtools::HiPerf {
class CommandReporter {
public:
explicit CommandReporter(const std::string& fullArgument);
~CommandReporter();
void ReportCommand();
std::string mainCommand_ = "";
std::string subCommand_ = "";
std::string caller_ = "";
std::string targetProcess_ = "";
int32_t errorCode_ = 0;
std::string errorMessage_ = "";
private:
bool isReported_ = false;
CommandReporter(const CommandReporter&) = delete;
CommandReporter& operator=(const CommandReporter&) = delete;
CommandReporter(CommandReporter&&) = delete;
CommandReporter& operator=(CommandReporter&&) = delete;
FRIEND_TEST(SubCommandRecordTest, ReportSampleAll);
FRIEND_TEST(SubCommandRecordTest, ReportSamplePid);
FRIEND_TEST(SubCommandRecordTest, ReportSampleApp);
FRIEND_TEST(SubCommandStatTest, ReportSampleAll);
FRIEND_TEST(SubCommandStatTest, ReportSamplePid);
FRIEND_TEST(SubCommandStatTest, ReportSampleApp);
};
} // namespace OHOS::Developtools::HiPerf
#endif // COMMAND_REPORTER_H_

View File

@ -15,6 +15,8 @@
#ifndef HIPERF_SUBCOMMAND_H_
#define HIPERF_SUBCOMMAND_H_
#include <string>
#include "command_reporter.h"
#include "utilities.h"
#include "virtual_runtime.h"
@ -65,6 +67,9 @@ public:
return true;
}
// add args for hisysevent
virtual void AddReportArgs(CommandReporter& reporter) {};
// return false means cmd failed
virtual bool OnSubCommand(std::vector<std::string> &args) = 0;
// some test code will use this for simple

View File

@ -197,6 +197,9 @@ public:
bool ParseOption(std::vector<std::string> &args) override;
void DumpOptions(void) const override;
// add args for hisysevent
void AddReportArgs(CommandReporter& reporter) override;
static bool RegisterSubCommandRecord(void);
std::map<const std::string, unsigned long long> speOptMap_ = {
{"branch_filter", 0}, {"load_filter", 0},
@ -363,6 +366,10 @@ private:
void SetSavedCmdlinesSize();
void RecoverSavedCmdlinesSize();
bool OnlineReportData();
FRIEND_TEST(SubCommandRecordTest, ReportSampleAll);
FRIEND_TEST(SubCommandRecordTest, ReportSamplePid);
FRIEND_TEST(SubCommandRecordTest, ReportSampleApp);
};
} // namespace HiPerf
} // namespace Developtools

View File

@ -93,6 +93,9 @@ public:
bool ParseSpecialOption(std::vector<std::string> &args);
void DumpOptions(void) const override;
// add args for hisysevent
void AddReportArgs(CommandReporter& reporter) override;
private:
PerfEvents perfEvents_;
bool targetSystemWide_ {false};
@ -154,6 +157,10 @@ private:
bool CheckSelectCpuPidOption();
void SetReportFlags(bool cpuFlag, bool threadFlag);
void SetPerfEvent();
FRIEND_TEST(SubCommandStatTest, ReportSampleAll);
FRIEND_TEST(SubCommandStatTest, ReportSamplePid);
FRIEND_TEST(SubCommandStatTest, ReportSampleApp);
};
bool RegisterSubCommandStat(void);

View File

@ -28,6 +28,7 @@ bool Command::DispatchCommands(std::vector<std::string> arguments)
fullArgument.append(" ");
fullArgument.append(arg);
}
CommandReporter reporter(fullArgument);
HLOGD("args:%s", VectorToString(arguments).c_str());
while (!arguments.empty()) {
// we need found main command args first
@ -48,6 +49,7 @@ bool Command::DispatchCommands(std::vector<std::string> arguments)
// if it is an sub command
auto subCommand = SubCommand::FindSubCommand(arguments.front());
if (subCommand != nullptr) {
reporter.mainCommand_ = arguments.front();
// this is an sub command which we support
// remove the subcmd name
@ -56,6 +58,8 @@ bool Command::DispatchCommands(std::vector<std::string> arguments)
// if we found the sub command , after it processed , we will exit
HLOGD("OnSubCommandOptions -> %s", subCommand->Name().c_str());
if (subCommand->OnSubCommandOptions(arguments)) {
subCommand->AddReportArgs(reporter);
reporter.ReportCommand();
// if some help cmd ?
if (subCommand->OnPreSubCommand()) {
return true;

62
src/command_reporter.cpp Normal file
View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 2024-2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "command_reporter.h"
#if defined(is_ohos) && is_ohos
#include "hiperf_hilog.h"
#include "hisysevent.h"
#include "utilities.h"
#endif
namespace OHOS::Developtools::HiPerf {
CommandReporter::CommandReporter(const std::string& fullArgument) : subCommand_(fullArgument)
{
#if defined(is_ohos) && is_ohos
caller_ = GetProcessName(getppid());
#endif
}
CommandReporter::~CommandReporter()
{
ReportCommand();
}
void CommandReporter::ReportCommand()
{
#if defined(is_ohos) && is_ohos
if (isReported_) {
HIPERF_HILOGD(MODULE_DEFAULT, "command has been reported");
return;
}
int32_t ret = HiSysEventWrite(
OHOS::HiviewDFX::HiSysEvent::Domain::PROFILER, "HIPERF_USAGE",
OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
"MAIN_CMD", mainCommand_,
"SUB_CMD", subCommand_,
"CALLER", caller_,
"TARGET_PROCESS", targetProcess_,
"ERROR_CODE", errorCode_,
"ERROR_MESSAGE", errorMessage_);
if (ret != 0) {
HIPERF_HILOGE(MODULE_DEFAULT, "hisysevent report failed, err:%{public}d", ret);
}
#endif
isReported_ = true;
}
} // namespace OHOS::Developtools::HiPerf

View File

@ -1930,6 +1930,18 @@ std::string SubCommandRecord::HandleAppInfo()
}
return err;
}
void SubCommandRecord::AddReportArgs(CommandReporter& reporter)
{
if (targetSystemWide_) {
reporter.targetProcess_ = "ALL";
} else if (!appPackage_.empty()) {
reporter.targetProcess_ = appPackage_;
} else {
reporter.targetProcess_ = VectorToString<pid_t>(selectPids_);
}
}
} // namespace HiPerf
} // namespace Developtools
} // namespace OHOS

View File

@ -805,6 +805,18 @@ bool SubCommandStat::CheckOptions(const std::vector<pid_t> &pids)
}
return true;
}
void SubCommandStat::AddReportArgs(CommandReporter& reporter)
{
if (targetSystemWide_) {
reporter.targetProcess_ = "ALL";
} else if (!appPackage_.empty()) {
reporter.targetProcess_ = appPackage_;
} else {
reporter.targetProcess_ = VectorToString<pid_t>(selectPids_);
}
}
} // namespace HiPerf
} // namespace Developtools
} // namespace OHOS

View File

@ -1580,6 +1580,55 @@ HWTEST_F(SubCommandRecordTest, TestCmdlineSizeErr, TestSize.Level1)
{
TestRecordCommand("-d 2 -a -f 2000 --cmdline-size", false, false);
}
/**
* @tc.name: AddReportArgs
* @tc.desc: Test AddReportArgs with -a
* @tc.type: FUNC
*/
HWTEST_F(SubCommandRecordTest, ReportSampleAll, TestSize.Level1)
{
SubCommandRecord command;
command.targetSystemWide_ = true;
CommandReporter reporter("record");
reporter.isReported_ = true;
command.AddReportArgs(reporter);
EXPECT_EQ(reporter.targetProcess_, "ALL");
}
/**
* @tc.name: AddReportArgs
* @tc.desc: Test AddReportArgs with -p
* @tc.type: FUNC
*/
HWTEST_F(SubCommandRecordTest, ReportSamplePid, TestSize.Level1)
{
SubCommandRecord command;
command.selectPids_ = {1, 2, 3};
CommandReporter reporter("record");
reporter.isReported_ = true;
command.AddReportArgs(reporter);
EXPECT_EQ(reporter.targetProcess_, "1,2,3");
}
/**
* @tc.name: AddReportArgs
* @tc.desc: Test AddReportArgs with --app
* @tc.type: FUNC
*/
HWTEST_F(SubCommandRecordTest, ReportSampleApp, TestSize.Level1)
{
SubCommandRecord command;
command.appPackage_ = "com.test.app";
CommandReporter reporter("record");
reporter.isReported_ = true;
command.AddReportArgs(reporter);
EXPECT_EQ(reporter.targetProcess_, "com.test.app");
}
} // namespace HiPerf
} // namespace Developtools
} // namespace OHOS

View File

@ -2258,6 +2258,54 @@ HWTEST_F(SubCommandStatTest, CheckPidAndApp, TestSize.Level1)
printf("output:\n%s", stringOut.c_str());
}
}
/**
* @tc.name: AddReportArgs
* @tc.desc: Test AddReportArgs with -a
* @tc.type: FUNC
*/
HWTEST_F(SubCommandStatTest, ReportSampleAll, TestSize.Level1)
{
SubCommandStat command;
command.targetSystemWide_ = true;
CommandReporter reporter("stat");
reporter.isReported_ = true;
command.AddReportArgs(reporter);
EXPECT_EQ(reporter.targetProcess_, "ALL");
}
/**
* @tc.name: AddReportArgs
* @tc.desc: Test AddReportArgs with -p
* @tc.type: FUNC
*/
HWTEST_F(SubCommandStatTest, ReportSamplePid, TestSize.Level1)
{
SubCommandStat command;
command.selectPids_ = {1, 2, 3};
CommandReporter reporter("stat");
reporter.isReported_ = true;
command.AddReportArgs(reporter);
EXPECT_EQ(reporter.targetProcess_, "1,2,3");
}
/**
* @tc.name: AddReportArgs
* @tc.desc: Test AddReportArgs with --app
* @tc.type: FUNC
*/
HWTEST_F(SubCommandStatTest, ReportSampleApp, TestSize.Level1)
{
SubCommandStat command;
command.appPackage_ = "com.test.app";
CommandReporter reporter("stat");
reporter.isReported_ = true;
command.AddReportArgs(reporter);
EXPECT_EQ(reporter.targetProcess_, "com.test.app");
}
} // namespace HiPerf
} // namespace Developtools
} // namespace OHOS