!1063 arkts插件新增cpu profiler功能

Merge pull request !1063 from jc949204098/master
This commit is contained in:
openharmony_ci 2023-09-08 04:39:10 +00:00 committed by Gitee
commit 1c2931a5e7
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
9 changed files with 378 additions and 33 deletions

View File

@ -129,6 +129,7 @@ Note:If the text contains special characters, please escape them according to th
<filteritem type="filepath" name="host/smartperf/trace_streamer/.gitignore" desc="gitignore file, no copyright header"/>
</filefilter>
<filefilter name="binaryFileTypePolicyFilter" desc="Filters for binary file policies" >
<filteritem type="filepath" name="device/plugins/arkts_plugin/test/resources/.*" desc="test binary file, the file is self-developed"/>
<filteritem type="filepath" name="device/plugins/native_daemon/test/unittest/resource/testdata/.*" desc="test binary file, the file is self-developed"/>
<filteritem type="filepath" name="device/plugins/ftrace_plugin/tools/trace_converter/test/unittest/resource/.*" desc="test binary file, the file is self-developed"/>
<filteritem type="filepath" name="host/ohosprofiler/src/main/resources/trace.db" desc="the binary file for host, the file is self-developed"/>

View File

@ -58,6 +58,7 @@ group("unittest") {
"base/test:unittest",
"cmds/test:unittest",
"plugins/api/test:unittest",
"plugins/arkts_plugin/test:unittest",
"plugins/cpu_plugin/test:unittest",
"plugins/diskio_plugin/test:unittest",
"plugins/ftrace_plugin/test:unittest",

View File

@ -155,4 +155,14 @@
<option name="push" value="../hiebpf/test/resources/elf64.stripped -> /data/test/resources/testdata/" src="res"/>
</preparer>
</target>
<target name="arktsplugin_ut">
<preparer>
<option name="push" value="plugins/arkts_plugin/test/resources/entry-default-signed.hap -> /data/local/tmp/" src="res"/>
<option name="shell" value="bm install -r -p /data/local/tmp/entry-default-signed.hap"/>
<option name="shell" value="aa start -a MainAbility -b com.ohos.distributedmusicplayer"/>
</preparer>
<cleaner>
<option name="uninstall" value="com.ohos.distributedmusicplayer"/>
</cleaner>
</target>
</configuration>

View File

@ -39,7 +39,7 @@ public:
private:
void Snapshot();
void FlushData();
void FlushData(const std::string& command = "");
bool ClientConnectUnixWebSocket(const std::string& sockName, uint32_t timeoutLimit = 0);
bool ClientSendWSUpgradeReq();
bool ClientRecvWSUpgradeRsp();
@ -51,6 +51,9 @@ private:
bool DecodeMessage(WebSocketFrame& wsFrame);
uint64_t NetToHostLongLong(char* buf, uint32_t len);
bool Recv(int32_t client, char* buf, size_t totalLen, int32_t flags) const;
int32_t EnableTimeline();
int32_t EnableSnapshot();
int32_t EnableCpuProfiler();
private:
ArkTSConfig protoConfig_;
@ -59,7 +62,6 @@ private:
std::vector<char> buffer_;
std::string snapshotCmd_;
std::string timelineCmd_;
ScheduleTaskManager scheduleTaskManager_;
int32_t client_{-1};
enum SocketState : uint8_t {
UNINITED,
@ -67,6 +69,7 @@ private:
CONNECTED,
};
std::atomic<SocketState> socketState_{SocketState::UNINITED};
uint8_t commandResult_{0};
};
#endif // ARKTS_PLUGIN_H

View File

@ -17,6 +17,7 @@
#include <arpa/inet.h>
#include <cstdlib>
#include <regex>
#include <sys/un.h>
#include <unistd.h>
@ -34,9 +35,19 @@ const std::string TIMELINE_HEAD =
const std::string TIMELINE_TAIL = "}}";
const std::string TIMELINE_STOP =
R"({"id":2,"method":"HeapProfiler.stopTrackingHeapObjects","params":{"reportProgress":true}})";
const std::string START_CMD_RETURN = R"({"id":1,"result":{}})";
const std::string STOP_CMD_RETURN = R"({"id":2,"result":{}})";
const std::string CPU_PROFILER_INTERVAL_HEAD =
R"({"id":3,"method":"Profiler.setSamplingInterval","params":{"interval":)";
const std::string CPU_PROFILER_INTERVAL_TAIL = R"(}})";
const std::string CPU_PROFILER_START = R"({"id":3,"method":"Profiler.start","params":{}})";
const std::string CPU_PROFILER_STOP = R"({"id":3,"method":"Profiler.stop","params":{}})";
constexpr uint8_t TIMELINE_START_SUCCESS = 0x1;
constexpr uint8_t CPU_PROFILER_START_SUCCESS = 0x2;
const std::string RESPONSE_FLAG_HEAD = R"({"id":)";
const std::string RESPONSE_FLAG_TAIL = R"(,"result":{}})";
const std::string REGEX_PATTERN = R"("id":(\d+))";
const std::string ARKTS_SCHEDULE = R"(ArkTS_Snapshot)";
enum class HeapType : int32_t {
INVALID = -1,
SNAPSHOT,
TIMELINE,
};
@ -59,6 +70,7 @@ constexpr int32_t SOCKET_SUCCESS = 0;
constexpr int32_t SOCKET_HEADER_LEN = 2;
constexpr int32_t PAYLOAD_LEN = 2;
constexpr int32_t EXTEND_PAYLOAD_LEN = 8;
constexpr uint32_t CPU_PROFILER_INTERVAL_DEFAULT = 1000;
} // namespace
int32_t ArkTSPlugin::Start(const uint8_t* configData, uint32_t configSize)
@ -85,41 +97,107 @@ int32_t ArkTSPlugin::Start(const uint8_t* configData, uint32_t configSize)
return -1;
}
if (static_cast<int32_t>(protoConfig_.type()) == static_cast<int32_t>(HeapType::SNAPSHOT)) {
snapshotCmd_ = SNAPSHOT_HEAD + (protoConfig_.capture_numeric_value() ? "true" : "false") + SNAPSHOT_TAIL;
if (protoConfig_.enable_cpu_profiler()) {
if (EnableCpuProfiler() != 0) {
HILOG_ERROR(LOG_CORE, "arkts plugin cpu profiler start failed");
return -1;
}
}
auto interval = ScheduleTaskManager::ms(protoConfig_.interval() * MAX_MATCH_CNT);
if (interval.count() == 0) {
HILOG_ERROR(LOG_CORE, "%s:scheduleTask interval == 0 error!", __func__);
switch (static_cast<int32_t>(protoConfig_.type())) {
case static_cast<int32_t>(HeapType::SNAPSHOT): {
return EnableSnapshot();
}
case static_cast<int32_t>(HeapType::TIMELINE): {
return EnableTimeline();
}
case static_cast<int32_t>(HeapType::INVALID): {
HILOG_INFO(LOG_CORE, "arkts plugin memory type is INVALID");
return 0;
}
default: {
HILOG_ERROR(LOG_CORE, "arkts plugin start type error");
return -1;
}
auto callback = std::bind(&ArkTSPlugin::Snapshot, this);
if (!scheduleTaskManager_.ScheduleTask("snapshot", callback, interval)) {
HILOG_ERROR(LOG_CORE, "%s:scheduleTask failed!", __func__);
return -1;
}
} else if (static_cast<int32_t>(protoConfig_.type()) == static_cast<int32_t>(HeapType::TIMELINE)) {
timelineCmd_ = TIMELINE_HEAD + (protoConfig_.track_allocations() ? "true" : "false") + TIMELINE_TAIL;
if (!ClientSendReq(timelineCmd_.c_str())) {
return -1;
}
FlushData();
} else {
}
}
int32_t ArkTSPlugin::EnableTimeline()
{
std::string timelineCmd = TIMELINE_HEAD + (protoConfig_.track_allocations() ? "true" : "false") + TIMELINE_TAIL;
if (!ClientSendReq(timelineCmd)) {
return -1;
}
FlushData(timelineCmd);
commandResult_ |= TIMELINE_START_SUCCESS;
return 0;
}
int32_t ArkTSPlugin::EnableSnapshot()
{
snapshotCmd_ = SNAPSHOT_HEAD + (protoConfig_.capture_numeric_value() ? "true" : "false") + SNAPSHOT_TAIL;
auto interval = ScheduleTaskManager::ms(protoConfig_.interval() * MAX_MATCH_CNT);
if (interval.count() == 0) {
HILOG_ERROR(LOG_CORE, "%s:scheduleTask interval == 0 error!", __func__);
return -1;
}
auto callback = std::bind(&ArkTSPlugin::Snapshot, this);
if (!ScheduleTaskManager::GetInstance().ScheduleTask(
ARKTS_SCHEDULE, callback, interval, ScheduleTaskManager::ms(0))) {
HILOG_ERROR(LOG_CORE, "%s:scheduleTask failed!", __func__);
return -1;
}
return 0;
}
int32_t ArkTSPlugin::EnableCpuProfiler()
{
std::string interval = CPU_PROFILER_INTERVAL_HEAD
+ (protoConfig_.cpu_profiler_interval() == 0 ?
std::to_string(CPU_PROFILER_INTERVAL_DEFAULT) : std::to_string(protoConfig_.cpu_profiler_interval()))
+ CPU_PROFILER_INTERVAL_TAIL;
if (!ClientSendReq(interval)) {
return -1;
}
FlushData(interval);
if (!ClientSendReq(CPU_PROFILER_START)) {
return -1;
}
FlushData(CPU_PROFILER_START);
commandResult_ |= CPU_PROFILER_START_SUCCESS;
return 0;
}
int32_t ArkTSPlugin::Stop()
{
if (static_cast<int32_t>(protoConfig_.type()) == static_cast<int32_t>(HeapType::TIMELINE)) {
if (!ClientSendReq(TIMELINE_STOP.c_str())) {
Close();
return -1;
switch (static_cast<int32_t>(protoConfig_.type())) {
case static_cast<int32_t>(HeapType::SNAPSHOT): {
ScheduleTaskManager::GetInstance().UnscheduleTask(ARKTS_SCHEDULE);
break;
}
case static_cast<int32_t>(HeapType::TIMELINE): {
if (commandResult_ & TIMELINE_START_SUCCESS) {
if (!ClientSendReq(TIMELINE_STOP)) {
break;
}
FlushData(TIMELINE_STOP);
}
break;
}
case static_cast<int32_t>(HeapType::INVALID): {
break;
}
default: {
HILOG_ERROR(LOG_CORE, "arkts plugin stop type error");
break;
}
}
if (protoConfig_.enable_cpu_profiler() && (commandResult_ & CPU_PROFILER_START_SUCCESS)) {
if (ClientSendReq(CPU_PROFILER_STOP)) {
FlushData();
}
FlushData();
} else if (static_cast<int32_t>(protoConfig_.type()) == static_cast<int32_t>(HeapType::SNAPSHOT)) {
scheduleTaskManager_.Shutdown();
}
Close();
return 0;
@ -133,14 +211,23 @@ void ArkTSPlugin::SetWriter(WriterStruct* writer)
void ArkTSPlugin::Snapshot()
{
CHECK_NOTNULL(resultWriter_, NO_RETVAL, "%s: resultWriter_ nullptr", __func__);
if (!ClientSendReq(snapshotCmd_.c_str())) {
if (!ClientSendReq(snapshotCmd_)) {
return;
}
FlushData();
FlushData(snapshotCmd_);
}
void ArkTSPlugin::FlushData()
void ArkTSPlugin::FlushData(const std::string& command)
{
std::string endFlag;
if (!command.empty()) {
std::regex pattern(REGEX_PATTERN);
std::smatch match;
if (std::regex_search(command, match, pattern)) {
endFlag = RESPONSE_FLAG_HEAD + match[1].str() + RESPONSE_FLAG_TAIL;
}
}
while (true) {
std::string recv = Decode();
if (recv.empty()) {
@ -153,8 +240,11 @@ void ArkTSPlugin::FlushData()
data.SerializeToArray(buffer_.data(), buffer_.size());
resultWriter_->write(resultWriter_, buffer_.data(), buffer_.size());
resultWriter_->flush(resultWriter_);
if (recv == START_CMD_RETURN || recv == STOP_CMD_RETURN) {
break;
if (endFlag.empty()) {
return;
}
if (recv == endFlag) {
return;
}
}
}

View File

@ -0,0 +1,55 @@
# Copyright (c) 2021 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.
import("//build/test.gni")
import("../../../base/config.gni")
module_output_path = "${OHOS_PROFILER_TEST_MODULE_OUTPUT_PATH}/device"
config("module_private_config") {
visibility = [ ":*" ]
if (current_toolchain != host_toolchain) {
defines = [ "HAVE_HILOG" ]
}
}
ohos_unittest("arktsplugin_ut") {
module_out_path = module_output_path
sources = [ "unittest/arkts_plugin_unittest.cpp" ]
deps = [
"${OHOS_PROFILER_DIR}/device/plugins/arkts_plugin:arkts_source",
"${OHOS_PROFILER_DIR}/protos/types/plugins/arkts_plugin:arkts_plugin_data_cpp",
"//third_party/bounds_checking_function:libsec_static",
"//third_party/googletest:gtest_main",
]
include_dirs = [
"../include",
"${OHOS_PROFILER_DIR}/interfaces/kits",
"${OHOS_PROFILER_DIR}/device/base/include",
"//third_party/googletest/googletest/include/gtest",
"//third_party/bounds_checking_function/include",
]
cflags = [
"-Wno-inconsistent-missing-override",
"-Dprivate=public", #allow test code access private members
]
external_deps = [ "hilog:libhilog" ]
configs = [ ":module_private_config" ]
subsystem_name = "${OHOS_PROFILER_SUBSYS_NAME}"
part_name = "${OHOS_PROFILER_PART_NAME}"
resource_config_file = "${OHOS_PROFILER_DIR}/device/ohos_test.xml"
}
group("unittest") {
testonly = true
deps = [ ":arktsplugin_ut" ]
}

View File

@ -0,0 +1,177 @@
/*
* Copyright (c) 2023 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 <cinttypes>
#include <hwext/gtest-ext.h>
#include <hwext/gtest-tag.h>
#include <vector>
#include <memory>
#include <fcntl.h>
#include <string>
#include <regex>
#include <unistd.h>
#include "arkts_plugin.h"
#include "plugin_module_api.h"
using namespace testing::ext;
class ArkTSPluginTest : public ::testing::Test {
public:
ArkTSPluginTest()
{
sleep(10); // Wait for the application to start successfully.
std::unique_ptr<FILE, decltype(&pclose)> runCmd(popen("netstat -anp | grep Panda", "r"), pclose);
if (runCmd == nullptr) {
return;
}
constexpr uint32_t readBufferSize = 4096;
std::array<char, readBufferSize> buffer;
std::string result;
while (fgets(buffer.data(), buffer.size(), runCmd.get()) != nullptr) {
result = buffer.data();
if (result.find("com.ohos.dist") == std::string::npos) {
continue;
}
std::regex pattern(R"(\b(\d+)/)");
std::smatch match;
if (std::regex_search(result, match, pattern)) {
std::string matchedString = match[1].str();
pid_ = std::stoi(matchedString);
HILOG_INFO(LOG_CORE, "ArkTSPluginTest: pid_ is %d", pid_);
}
}
}
~ArkTSPluginTest()
{
sleep(5);
}
static void SetUpTestCase() {}
static void TearDownTestCase() {}
int32_t pid_{0};
};
std::vector<uint8_t> SetArkTSConfig(ArkTSConfig& protoConfig, int32_t pid, ArkTSConfig::HeapType type, uint32_t interval, bool capture_numeric_value,
bool track_allocations, bool enable_cpu_profiler, uint32_t cpu_profiler_interval = 1000)
{
protoConfig.set_pid(pid);
protoConfig.set_type(type);
protoConfig.set_interval(interval);
protoConfig.set_capture_numeric_value(capture_numeric_value);
protoConfig.set_track_allocations(track_allocations);
protoConfig.set_enable_cpu_profiler(enable_cpu_profiler);
protoConfig.set_cpu_profiler_interval(cpu_profiler_interval);
std::vector<uint8_t> configData(protoConfig.ByteSizeLong());
protoConfig.SerializeToArray(configData.data(), configData.size());
return configData;
}
long WriteFunc(WriterStruct* writer, const void* data, size_t size)
{
if (writer == nullptr || data == nullptr || size <= 0) {
return -1;
}
return 0;
}
bool FlushFunc(WriterStruct* writer)
{
if (writer == nullptr) {
return false;
}
return true;
}
/**
* @tc.name: arkts plugin
* @tc.desc: arkts plugin test boundary values.
* @tc.type: FUNC
*/
HWTEST_F(ArkTSPluginTest, TestStartFunction, TestSize.Level1)
{
ArkTSPlugin arkTSPlugin;
ArkTSConfig protoConfig;
std::vector<uint8_t> configData = SetArkTSConfig(protoConfig, -1, ArkTSConfig::INVALID, 0, false, false, false);
int32_t ret = arkTSPlugin.Start(configData.data(), configData.size());
EXPECT_EQ(ret, -1);
configData.clear();
configData = SetArkTSConfig(protoConfig, 1, ArkTSConfig::INVALID, 0, false, false, false);
ret = arkTSPlugin.Start(configData.data(), configData.size());
EXPECT_EQ(ret, -1);
configData.clear();
configData = SetArkTSConfig(protoConfig, pid_, ArkTSConfig::INVALID, 0, false, false, false);
ret = arkTSPlugin.Start(configData.data(), configData.size());
EXPECT_EQ(ret, 0);
EXPECT_EQ(arkTSPlugin.Stop(), 0);
}
/**
* @tc.name: arkts plugin
* @tc.desc: arkts plugin test memory snapshot.
* @tc.type: FUNC
*/
HWTEST_F(ArkTSPluginTest, TestSnapshot, TestSize.Level1)
{
ArkTSPlugin arkTSPlugin;
ArkTSConfig protoConfig;
WriterStruct writer = {WriteFunc, FlushFunc};
std::vector<uint8_t> configData = SetArkTSConfig(protoConfig, pid_, ArkTSConfig::SNAPSHOT, 5, false, false, false);
arkTSPlugin.SetWriter(&writer);
EXPECT_EQ(arkTSPlugin.Start(configData.data(), configData.size()), 0);
sleep(10);
EXPECT_EQ(arkTSPlugin.Stop(), 0);
}
/**
* @tc.name: arkts plugin
* @tc.desc: arkts plugin test memory timeline.
* @tc.type: FUNC
*/
HWTEST_F(ArkTSPluginTest, TestTimeline, TestSize.Level1)
{
ArkTSPlugin arkTSPlugin;
ArkTSConfig protoConfig;
WriterStruct writer = {WriteFunc, FlushFunc};
std::vector<uint8_t> configData = SetArkTSConfig(protoConfig, pid_, ArkTSConfig::TIMELINE, 0, false, true, false);
arkTSPlugin.SetWriter(&writer);
EXPECT_EQ(arkTSPlugin.Start(configData.data(), configData.size()), 0);
sleep(5);
EXPECT_EQ(arkTSPlugin.Stop(), 0);
}
/**
* @tc.name: arkts plugin
* @tc.desc: arkts plugin test cpu profiler.
* @tc.type: FUNC
*/
HWTEST_F(ArkTSPluginTest, TestCpuProfiler, TestSize.Level1)
{
ArkTSPlugin arkTSPlugin;
ArkTSConfig protoConfig;
WriterStruct writer = {WriteFunc, FlushFunc};
std::vector<uint8_t> configData = SetArkTSConfig(protoConfig, pid_, ArkTSConfig::INVALID, 0, false, false, true);
arkTSPlugin.SetWriter(&writer);
EXPECT_EQ(arkTSPlugin.Start(configData.data(), configData.size()), 0);
sleep(5);
EXPECT_EQ(arkTSPlugin.Stop(), 0);
}

View File

@ -18,10 +18,18 @@ message ArkTSConfig {
enum HeapType {
SNAPSHOT = 0;
TIMELINE = 1;
INVALID = -1; // Disable capturing memory data.
}
int32 pid = 1;
HeapType type = 2;
// When the 'snapshot' mode is enabled in memory, it denotes the data fetching interval, measured in seconds.
uint32 interval = 3;
bool capture_numeric_value = 4;
bool track_allocations = 5;
bool enable_cpu_profiler = 6;
// When the CPU profiler mode is active, it signifies the data capturing interval, measured in microseconds,
// with a default value of 1000 microseconds.
uint32 cpu_profiler_interval = 7;
}