mirror of
https://gitee.com/openharmony/developtools_profiler
synced 2024-11-23 15:00:17 +00:00
commit
fb5806f4e2
@ -22,6 +22,7 @@ group("hiprofiler_targets") {
|
||||
"plugins/cpu_plugin:cpudataplugin",
|
||||
"plugins/diskio_plugin:diskiodataplugin",
|
||||
"plugins/ftrace_plugin:ftrace_targets",
|
||||
"plugins/gpu_plugin:gpudataplugin",
|
||||
"plugins/hidump_plugin:hidumpplugin",
|
||||
"plugins/hilog_plugin:hilogplugin",
|
||||
"plugins/hiperf_plugin:hiperfplugin",
|
||||
@ -63,6 +64,7 @@ group("unittest") {
|
||||
"plugins/cpu_plugin/test:unittest",
|
||||
"plugins/diskio_plugin/test:unittest",
|
||||
"plugins/ftrace_plugin/test:unittest",
|
||||
"plugins/gpu_plugin/test:unittest",
|
||||
"plugins/hidump_plugin/test:unittest",
|
||||
"plugins/hilog_plugin/test:unittest",
|
||||
"plugins/hiperf_plugin/test:unittest",
|
||||
|
@ -43,6 +43,7 @@ ohos_executable("hiprofiler_cmd") {
|
||||
"${OHOS_PROFILER_DIR}/protos/types/plugins/cpu_data:cpu_plugin_config_cpp_standard",
|
||||
"${OHOS_PROFILER_DIR}/protos/types/plugins/diskio_data:diskio_plugin_config_cpp_standard",
|
||||
"${OHOS_PROFILER_DIR}/protos/types/plugins/ftrace_data/${device_kernel_version}:ftrace_plugin_config_cpp_standard",
|
||||
"${OHOS_PROFILER_DIR}/protos/types/plugins/gpu_data:gpu_plugin_config_cpp_standard",
|
||||
"${OHOS_PROFILER_DIR}/protos/types/plugins/hidump_data:hidump_plugin_config_cpp_standard",
|
||||
"${OHOS_PROFILER_DIR}/protos/types/plugins/hiebpf_data:hiebpf_data_cpp_standard",
|
||||
"${OHOS_PROFILER_DIR}/protos/types/plugins/hilog_data:hilog_plugin_config_cpp_standard",
|
||||
|
@ -44,6 +44,7 @@ public:
|
||||
bool SetSerializeHisyseventConfig(const std::string& pluginName, ProfilerPluginConfig& pluginConfig);
|
||||
bool SetSerializeArkTSConfig(const std::string& pluginName, ProfilerPluginConfig& pluginConfig);
|
||||
bool SetSerializeXpowerConfig(const std::string& pluginName, ProfilerPluginConfig& pluginConfig);
|
||||
bool SetSerializeGpuConfig(const std::string& pluginName, ProfilerPluginConfig& pluginConfig);
|
||||
|
||||
google::protobuf::TextFormat::Parser& GetParser()
|
||||
{
|
||||
|
@ -16,6 +16,7 @@
|
||||
#include "parse_plugin_config.h"
|
||||
|
||||
#include "cpu_plugin_config_standard.pb.h"
|
||||
#include "gpu_plugin_config_standard.pb.h"
|
||||
#include "diskio_plugin_config_standard.pb.h"
|
||||
#include "hidump_plugin_config_standard.pb.h"
|
||||
#include "hiebpf_plugin_config_standard.pb.h"
|
||||
@ -129,6 +130,8 @@ bool ParsePluginConfig::SetSerializePluginsConfig(const std::string& pluginName,
|
||||
ret = SetSerializeArkTSConfig(pluginName, pluginConfig);
|
||||
} else if (pluginName == "xpower-plugin") {
|
||||
ret = SetSerializeXpowerConfig(pluginName, pluginConfig);
|
||||
} else if (pluginName == "gpu-plugin") {
|
||||
ret = SetSerializeGpuConfig(pluginName, pluginConfig);
|
||||
} else {
|
||||
printf("unsupport plugin: %s\n", pluginName.c_str());
|
||||
}
|
||||
@ -391,3 +394,19 @@ bool ParsePluginConfig::SetSerializeXpowerConfig(const std::string& pluginName,
|
||||
pluginConfig.set_config_data((const void*)configDataVec.data(), configDataVec.size());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ParsePluginConfig::SetSerializeGpuConfig(const std::string& pluginName, ProfilerPluginConfig& pluginConfig)
|
||||
{
|
||||
std::string configData = pluginConfigMap[pluginName];
|
||||
auto gpuConfigNolite = std::make_unique<ForStandard::GpuConfig>();
|
||||
if (!parser_.ParseFromString(configData, gpuConfigNolite.get())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> configDataVec(gpuConfigNolite->ByteSizeLong());
|
||||
if (gpuConfigNolite->SerializeToArray(configDataVec.data(), configDataVec.size()) <= 0) {
|
||||
return false;
|
||||
}
|
||||
pluginConfig.set_config_data((const void*)configDataVec.data(), configDataVec.size());
|
||||
return true;
|
||||
}
|
||||
|
@ -39,6 +39,7 @@ ohos_unittest("hiprofiler_cmd_ut") {
|
||||
"${OHOS_PROFILER_DIR}/protos/types/plugins/cpu_data:cpu_plugin_config_cpp_standard",
|
||||
"${OHOS_PROFILER_DIR}/protos/types/plugins/diskio_data:diskio_plugin_config_cpp_standard",
|
||||
"${OHOS_PROFILER_DIR}/protos/types/plugins/ftrace_data/default:ftrace_plugin_config_cpp_standard",
|
||||
"${OHOS_PROFILER_DIR}/protos/types/plugins/gpu_data:gpu_plugin_config_cpp_standard",
|
||||
"${OHOS_PROFILER_DIR}/protos/types/plugins/hidump_data:hidump_plugin_config_cpp_standard",
|
||||
"${OHOS_PROFILER_DIR}/protos/types/plugins/hiebpf_data:hiebpf_data_cpp_standard",
|
||||
"${OHOS_PROFILER_DIR}/protos/types/plugins/hilog_data:hilog_plugin_config_cpp_standard",
|
||||
|
@ -189,4 +189,10 @@
|
||||
<option name="shell" value="aa force-stop com.taobao.taobao"/>
|
||||
</cleaner>
|
||||
</target>
|
||||
<target name="gpudataplugin_ut">
|
||||
<preparer>
|
||||
<option name="push" value="plugins/gpu_plugin/test/utresources/info/1/gpustat1.txt -> /data/local/tmp/" src="res"/>
|
||||
<option name="push" value="plugins/gpu_plugin/test/utresources/info/2/gpustat2.txt -> /data/local/tmp/" src="res"/>
|
||||
</preparer>
|
||||
</target>
|
||||
</configuration>
|
||||
|
@ -42,6 +42,7 @@ const char DEFAULT_LIB_PATH[] = "/system/lib/";
|
||||
|
||||
std::vector<std::string> presetPluginVec = {
|
||||
"libcpudataplugin.z.so",
|
||||
"libgpudataplugin.z.so",
|
||||
"libdiskiodataplugin.z.so",
|
||||
"libftrace_plugin.z.so",
|
||||
"libhidumpplugin.z.so",
|
||||
|
75
device/plugins/gpu_plugin/BUILD.gn
Normal file
75
device/plugins/gpu_plugin/BUILD.gn
Normal file
@ -0,0 +1,75 @@
|
||||
# Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
|
||||
# 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/ohos.gni")
|
||||
import("../../base/config.gni")
|
||||
|
||||
ohos_source_set("gpudataplugin_source") {
|
||||
part_name = "${OHOS_PROFILER_PART_NAME}"
|
||||
subsystem_name = "${OHOS_PROFILER_SUBSYS_NAME}"
|
||||
sources = [
|
||||
"../memory_plugin/src/buffer_splitter.cpp",
|
||||
"src/gpu_data_plugin.cpp",
|
||||
"src/gpu_module.cpp",
|
||||
]
|
||||
include_dirs = [
|
||||
"include",
|
||||
"../memory_plugin/include",
|
||||
"${OHOS_PROFILER_DIR}/interfaces/kits",
|
||||
"${OHOS_PROFILER_DIR}/device/base/include",
|
||||
"//third_party/bounds_checking_function/include",
|
||||
]
|
||||
deps = [
|
||||
"${OHOS_PROFILER_3RDPARTY_PROTOBUF_DIR}:protobuf_lite",
|
||||
"${OHOS_PROFILER_DIR}/proto_encoder:proto_encoder_source",
|
||||
"${OHOS_PROFILER_DIR}/protos/types/plugins/gpu_data:gpu_data_cpp",
|
||||
"${OHOS_PROFILER_DIR}/protos/types/plugins/gpu_data:gpu_data_encoder",
|
||||
"//third_party/bounds_checking_function:libsec_static",
|
||||
]
|
||||
if (current_toolchain != host_toolchain) {
|
||||
defines = [ "HAVE_HILOG" ]
|
||||
external_deps = [ "hilog:libhilog" ]
|
||||
}
|
||||
public_configs = [ "${OHOS_PROFILER_DIR}/device/base:hiprofiler_test_config" ]
|
||||
}
|
||||
|
||||
ohos_shared_library("gpudataplugin") {
|
||||
output_name = "gpudataplugin"
|
||||
|
||||
deps = [ ":gpudataplugin_source" ]
|
||||
install_enable = true
|
||||
subsystem_name = "${OHOS_PROFILER_SUBSYS_NAME}"
|
||||
part_name = "${OHOS_PROFILER_PART_NAME}"
|
||||
}
|
||||
|
||||
ohos_executable("gpudataplugintest") {
|
||||
output_name = "gpudataplugintest"
|
||||
sources = [ "src/test_main.cpp" ]
|
||||
include_dirs = [
|
||||
"include",
|
||||
"../api/include",
|
||||
"${OHOS_PROFILER_DIR}/interfaces/kits",
|
||||
"${OHOS_PROFILER_DIR}/device/base/include",
|
||||
]
|
||||
deps = [
|
||||
"${OHOS_PROFILER_3RDPARTY_PROTOBUF_DIR}:protobuf_lite",
|
||||
"${OHOS_PROFILER_DIR}/protos/types/plugins/gpu_data:gpu_data_cpp",
|
||||
]
|
||||
if (current_toolchain != host_toolchain) {
|
||||
defines = [ "HAVE_HILOG" ]
|
||||
external_deps = [ "hilog:libhilog" ]
|
||||
}
|
||||
install_enable = false
|
||||
subsystem_name = "${OHOS_PROFILER_SUBSYS_NAME}"
|
||||
part_name = "${OHOS_PROFILER_PART_NAME}"
|
||||
}
|
56
device/plugins/gpu_plugin/include/gpu_data_plugin.h
Normal file
56
device/plugins/gpu_plugin/include/gpu_data_plugin.h
Normal file
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
|
||||
* 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 GPU_DATA_PLUGIN_H
|
||||
#define GPU_DATA_PLUGIN_H
|
||||
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <string>
|
||||
#include <unistd.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
#include "gpu_plugin_config.pb.h"
|
||||
#include "gpu_plugin_result.pb.h"
|
||||
#include "logging.h"
|
||||
#include "plugin_module_api.h"
|
||||
|
||||
enum ErrorType {
|
||||
RET_NULL_ADDR = -2,
|
||||
RET_FAIL = -1,
|
||||
RET_SUCC = 0,
|
||||
};
|
||||
|
||||
class GpuDataPlugin {
|
||||
public:
|
||||
GpuDataPlugin() = default;
|
||||
~GpuDataPlugin() = default;
|
||||
int Start(const uint8_t* configData, uint32_t configSize);
|
||||
int Report(uint8_t* data, uint32_t dataSize);
|
||||
int ReportOptimize(RandomWriteCtx* randomWrite);
|
||||
int Stop();
|
||||
|
||||
private:
|
||||
int ReadFile();
|
||||
template <typename T> void WriteGpuDataInfo(T& gpuData);
|
||||
|
||||
private:
|
||||
GpuConfig protoConfig_;
|
||||
int pid_ = -1;
|
||||
std::ifstream file_;
|
||||
};
|
||||
|
||||
#endif
|
103
device/plugins/gpu_plugin/src/gpu_data_plugin.cpp
Normal file
103
device/plugins/gpu_plugin/src/gpu_data_plugin.cpp
Normal file
@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
|
||||
* 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 "gpu_data_plugin.h"
|
||||
#include <ctime>
|
||||
#include "gpu_plugin_result.pbencoder.h"
|
||||
|
||||
namespace {
|
||||
using namespace OHOS::Developtools::Profiler;
|
||||
const std::string GPU_PATH = "/sys/class/devfreq/gpufreq/gpu_scene_aware/utilisation";
|
||||
} // namespace
|
||||
|
||||
int GpuDataPlugin::Start(const uint8_t* configData, uint32_t configSize)
|
||||
{
|
||||
CHECK_TRUE(protoConfig_.ParseFromArray(configData, configSize) > 0, RET_FAIL,
|
||||
"%s:parseFromArray failed!", __func__);
|
||||
|
||||
if (protoConfig_.pid() > 0) {
|
||||
pid_ = protoConfig_.pid();
|
||||
}
|
||||
|
||||
file_.open(GPU_PATH);
|
||||
if (!file_.is_open()) {
|
||||
HILOG_ERROR(LOG_CORE, "%s:failed to open(%s)", __func__, GPU_PATH.c_str());
|
||||
return RET_FAIL;
|
||||
}
|
||||
HILOG_INFO(LOG_CORE, "%s:start success!", __func__);
|
||||
return RET_SUCC;
|
||||
}
|
||||
|
||||
int GpuDataPlugin::ReportOptimize(RandomWriteCtx* randomWrite)
|
||||
{
|
||||
ProtoEncoder::GpuData dataProto(randomWrite);
|
||||
WriteGpuDataInfo(dataProto);
|
||||
int msgSize = dataProto.Finish();
|
||||
return msgSize;
|
||||
}
|
||||
|
||||
int GpuDataPlugin::Report(uint8_t* data, uint32_t dataSize)
|
||||
{
|
||||
GpuData dataProto;
|
||||
uint32_t length;
|
||||
WriteGpuDataInfo(dataProto);
|
||||
|
||||
length = dataProto.ByteSizeLong();
|
||||
if (length > dataSize) {
|
||||
return -length;
|
||||
}
|
||||
if (dataProto.SerializeToArray(data, length) > 0) {
|
||||
return length;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int GpuDataPlugin::Stop()
|
||||
{
|
||||
file_.close();
|
||||
HILOG_INFO(LOG_CORE, "%s:stop success!", __func__);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int GpuDataPlugin::ReadFile()
|
||||
{
|
||||
file_.clear();
|
||||
file_.seekg(0);
|
||||
std::string line;
|
||||
std::getline(file_, line);
|
||||
for (char charac : line) {
|
||||
if (!isdigit(charac)) {
|
||||
HILOG_ERROR(LOG_CORE, "invalid file content for (%s)", GPU_PATH.c_str());
|
||||
return RET_FAIL;
|
||||
}
|
||||
}
|
||||
return stoi(line);
|
||||
}
|
||||
|
||||
template <typename T> void GpuDataPlugin::WriteGpuDataInfo(T& gpuData)
|
||||
{
|
||||
int ret = ReadFile();
|
||||
if (ret == RET_FAIL) {
|
||||
return;
|
||||
}
|
||||
|
||||
constexpr uint64_t nanoSeconds = 1000000000;
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_BOOTTIME, &ts);
|
||||
uint64_t boottime = (static_cast<uint64_t>(ts.tv_sec) * nanoSeconds +
|
||||
static_cast<uint64_t>(ts.tv_nsec)) / 1000000;
|
||||
gpuData.set_boottime(boottime);
|
||||
gpuData.set_gpu_utilisation(static_cast<uint64_t>(ret));
|
||||
}
|
64
device/plugins/gpu_plugin/src/gpu_module.cpp
Normal file
64
device/plugins/gpu_plugin/src/gpu_module.cpp
Normal file
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
|
||||
* 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 <mutex>
|
||||
#include "gpu_data_plugin.h"
|
||||
|
||||
namespace {
|
||||
constexpr uint32_t MAX_BUFFER_SIZE = 4 * 1024 * 1024;
|
||||
std::unique_ptr<GpuDataPlugin> g_plugin = nullptr;
|
||||
std::mutex g_taskMutex;
|
||||
} // namespace
|
||||
|
||||
static int GpuDataPluginSessionStart(const uint8_t* configData, uint32_t configSize)
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(g_taskMutex);
|
||||
g_plugin = std::make_unique<GpuDataPlugin>();
|
||||
return g_plugin->Start(configData, configSize);
|
||||
}
|
||||
|
||||
static int GpuPluginReportResult(uint8_t* bufferData, uint32_t bufferSize)
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(g_taskMutex);
|
||||
CHECK_NOTNULL(g_plugin, -1, "g_plugin is nullptr");
|
||||
return g_plugin->Report(bufferData, bufferSize);
|
||||
}
|
||||
|
||||
static int GpuPluginReportResultOptimize(RandomWriteCtx* randomWrite)
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(g_taskMutex);
|
||||
return g_plugin->ReportOptimize(randomWrite);
|
||||
}
|
||||
|
||||
static int GpuPluginSessionStop()
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(g_taskMutex);
|
||||
g_plugin->Stop();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static PluginModuleCallbacks g_callbacks = {
|
||||
.onPluginSessionStart = GpuDataPluginSessionStart,
|
||||
.onPluginReportResult = GpuPluginReportResult,
|
||||
.onPluginSessionStop = GpuPluginSessionStop,
|
||||
.onPluginReportResultOptimize = GpuPluginReportResultOptimize,
|
||||
};
|
||||
|
||||
EXPORT_API PluginModuleStruct g_pluginModule = {
|
||||
.callbacks = &g_callbacks,
|
||||
.name = "gpu-plugin",
|
||||
.version = "1.02",
|
||||
.resultBufferSizeHint = MAX_BUFFER_SIZE,
|
||||
};
|
69
device/plugins/gpu_plugin/src/test_main.cpp
Normal file
69
device/plugins/gpu_plugin/src/test_main.cpp
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
|
||||
* 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 <dlfcn.h>
|
||||
#include <unistd.h>
|
||||
#include "gpu_plugin_config.pb.h"
|
||||
#include "gpu_plugin_result.pb.h"
|
||||
#include "plugin_module_api.h"
|
||||
|
||||
namespace {
|
||||
int g_testCount = 10;
|
||||
}
|
||||
|
||||
int main(int agrc, char* agrv[])
|
||||
{
|
||||
GpuConfig protoConfig;
|
||||
PluginModuleStruct* gpuPlugin;
|
||||
void* handle = dlopen("./libgpudataplugin.z.so", RTLD_LAZY);
|
||||
if (handle == nullptr) {
|
||||
std::cout << "test:dlopen err: " << dlerror() << std::endl;
|
||||
return 0;
|
||||
}
|
||||
std::cout << "test:handle = " << handle << std::endl;
|
||||
gpuPlugin = (PluginModuleStruct*)dlsym(handle, "g_pluginModule");
|
||||
std::cout << "test:name = " << gpuPlugin->name << std::endl;
|
||||
std::cout << "test:buffer size = " << gpuPlugin->resultBufferSizeHint << std::endl;
|
||||
|
||||
// Serialize config
|
||||
int configLength = protoConfig.ByteSizeLong();
|
||||
std::vector<uint8_t> configBuffer(configLength);
|
||||
int ret = protoConfig.SerializeToArray(configBuffer.data(), configBuffer.size());
|
||||
std::cout << "test:configLength = " << configLength << std::endl;
|
||||
std::cout << "test:serialize success start plugin ret = " << ret << std::endl;
|
||||
|
||||
// Start
|
||||
std::vector<uint8_t> dataBuffer(gpuPlugin->resultBufferSizeHint);
|
||||
gpuPlugin->callbacks->onPluginSessionStart(configBuffer.data(), configLength);
|
||||
while (g_testCount--) {
|
||||
int len = gpuPlugin->callbacks->onPluginReportResult(dataBuffer.data(),
|
||||
gpuPlugin->resultBufferSizeHint);
|
||||
std::cout << "test:filler buffer length = " << len << std::endl;
|
||||
|
||||
if (len > 0) {
|
||||
GpuData gpuData;
|
||||
gpuData.ParseFromArray(dataBuffer.data(), len);
|
||||
std::cout << "test:ParseFromArray length = " << len << std::endl;
|
||||
std::cout << "gpu_utilisation:" << gpuData.gpu_utilisation() << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "test:sleep...................." << std::endl;
|
||||
sleep(1);
|
||||
}
|
||||
gpuPlugin->callbacks->onPluginSessionStop();
|
||||
dlclose(handle);
|
||||
|
||||
return 0;
|
||||
}
|
57
device/plugins/gpu_plugin/test/BUILD.gn
Normal file
57
device/plugins/gpu_plugin/test/BUILD.gn
Normal file
@ -0,0 +1,57 @@
|
||||
# Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
|
||||
# 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("gpudataplugin_ut") {
|
||||
module_out_path = module_output_path
|
||||
sources = [ "unittest/gpu_data_plugin_unittest.cpp" ]
|
||||
deps = [
|
||||
"${OHOS_PROFILER_DIR}/device/plugins/gpu_plugin:gpudataplugin_source",
|
||||
"${OHOS_PROFILER_DIR}/device/plugins/gpu_plugin:gpudataplugintest",
|
||||
"${OHOS_PROFILER_DIR}/protos/types/plugins/gpu_data:gpu_data_cpp",
|
||||
"//third_party/bounds_checking_function:libsec_static",
|
||||
"//third_party/googletest:gtest_main",
|
||||
]
|
||||
include_dirs = [
|
||||
"../include",
|
||||
"../../../memory_plugin/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 = [ ":gpudataplugin_ut" ]
|
||||
}
|
@ -0,0 +1,187 @@
|
||||
/*
|
||||
* Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
|
||||
* 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 <hwext/gtest-ext.h>
|
||||
#include <hwext/gtest-tag.h>
|
||||
#include <dlfcn.h>
|
||||
#include <fstream>
|
||||
#include "gpu_data_plugin.h"
|
||||
#include "plugin_module_api.h"
|
||||
|
||||
using namespace testing::ext;
|
||||
|
||||
namespace {
|
||||
const std::string DEFAULT_TEST_PATH = "/data/local/tmp/";
|
||||
#if defined(__LP64__)
|
||||
const std::string SO_PATH = "/system/lib64/libgpudataplugin.z.so";
|
||||
#else
|
||||
const std::string SO_PATH = "/system/lib/libgpudataplugin.z.so";
|
||||
#endif
|
||||
const std::string DEFAULT_BIN_PATH("/data/local/tmp/gpudataplugintest");
|
||||
constexpr uint32_t BUF_SIZE = 4 * 1024 * 1024;
|
||||
|
||||
std::string g_path;
|
||||
std::string g_testPath;
|
||||
const unsigned long long EXPECT_VAL = 12;
|
||||
const unsigned long long SLEEP_TIME = 5;
|
||||
|
||||
struct TestVmstat {
|
||||
int64_t pgpgin;
|
||||
int64_t pgpgout;
|
||||
};
|
||||
|
||||
class GpuDataPluginTest : public ::testing::Test {
|
||||
public:
|
||||
static void SetUpTestCase() {}
|
||||
|
||||
static void TearDownTestCase()
|
||||
{
|
||||
if (access(g_testPath.c_str(), F_OK) == 0) {
|
||||
std::string str = "rm -rf " + g_testPath;
|
||||
system(str.c_str());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
string Getexepath()
|
||||
{
|
||||
char buf[PATH_MAX] = "";
|
||||
std::string path = "/proc/self/exe";
|
||||
size_t rslt = readlink(path.c_str(), buf, sizeof(buf));
|
||||
if (rslt < 0 || (rslt >= sizeof(buf))) {
|
||||
return "";
|
||||
}
|
||||
buf[rslt] = '\0';
|
||||
for (int i = rslt; i >= 0; i--) {
|
||||
if (buf[i] == '/') {
|
||||
buf[i + 1] = '\0';
|
||||
break;
|
||||
}
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
std::string GetFullPath(std::string path)
|
||||
{
|
||||
if (path.size() > 0 && path[0] != '/') {
|
||||
return Getexepath() + path;
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
bool PluginGpuInfoStub(GpuDataPlugin& gpuPlugin, GpuData& gpuData, bool unusualBuff)
|
||||
{
|
||||
GpuConfig protoConfig;
|
||||
std::vector<uint8_t> configData(protoConfig.ByteSizeLong());
|
||||
int ret = protoConfig.SerializeToArray(configData.data(), configData.size());
|
||||
if (ret < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// start
|
||||
ret = gpuPlugin.Start(configData.data(), configData.size());
|
||||
if (ret < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
gpuPlugin.file_.close();
|
||||
gpuPlugin.file_.open(g_path);
|
||||
// report
|
||||
std::vector<uint8_t> bufferData(BUF_SIZE);
|
||||
if (unusualBuff) { // buffer异常,调整缓冲区长度为1,测试异常情况
|
||||
bufferData.resize(1, 0);
|
||||
}
|
||||
|
||||
ret = gpuPlugin.Report(bufferData.data(), bufferData.size());
|
||||
if (ret > 0) {
|
||||
gpuData.ParseFromArray(bufferData.data(), ret);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: gpu plugin
|
||||
* @tc.desc: Test whether the path exists.
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
HWTEST_F(GpuDataPluginTest, TestPath, TestSize.Level1)
|
||||
{
|
||||
g_path = GetFullPath(DEFAULT_TEST_PATH);
|
||||
g_testPath = g_path;
|
||||
EXPECT_NE("", g_path);
|
||||
g_path += "/gpustat1.txt";
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: gpu plugin
|
||||
* @tc.desc: gpu information test for specific path.
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
HWTEST_F(GpuDataPluginTest, TestPlugin, TestSize.Level1)
|
||||
{
|
||||
GpuDataPlugin gpuPlugin;
|
||||
GpuData gpuData;
|
||||
|
||||
EXPECT_TRUE(PluginGpuInfoStub(gpuPlugin, gpuData, false));
|
||||
EXPECT_EQ(gpuData.gpu_utilisation(), EXPECT_VAL);
|
||||
|
||||
EXPECT_EQ(gpuPlugin.ReadFile(), EXPECT_VAL);
|
||||
sleep(SLEEP_TIME);
|
||||
EXPECT_EQ(gpuPlugin.ReadFile(), EXPECT_VAL);
|
||||
EXPECT_EQ(gpuPlugin.Stop(), 0);
|
||||
|
||||
// 缓冲区异常
|
||||
EXPECT_FALSE(PluginGpuInfoStub(gpuPlugin, gpuData, true));
|
||||
EXPECT_EQ(gpuPlugin.Stop(), 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @tc.name: gpu plugin
|
||||
* @tc.desc: gpu plugin registration test.
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
HWTEST_F(GpuDataPluginTest, TestPluginRegister, TestSize.Level1)
|
||||
{
|
||||
void* handle = dlopen(SO_PATH.c_str(), RTLD_LAZY);
|
||||
ASSERT_NE(handle, nullptr);
|
||||
PluginModuleStruct* gpuPlugin = (PluginModuleStruct*)dlsym(handle, "g_pluginModule");
|
||||
ASSERT_NE(gpuPlugin, nullptr);
|
||||
EXPECT_STREQ(gpuPlugin->name, "gpu-plugin");
|
||||
EXPECT_EQ(gpuPlugin->resultBufferSizeHint, BUF_SIZE);
|
||||
|
||||
// Serialize config
|
||||
GpuConfig protoConfig;
|
||||
int configLength = protoConfig.ByteSizeLong();
|
||||
ASSERT_EQ(configLength, 0);
|
||||
std::vector<uint8_t> configBuffer(configLength);
|
||||
EXPECT_TRUE(protoConfig.SerializeToArray(configBuffer.data(), configLength));
|
||||
|
||||
// run plugin
|
||||
std::vector<uint8_t> dataBuffer(gpuPlugin->resultBufferSizeHint);
|
||||
EXPECT_EQ(gpuPlugin->callbacks->onPluginSessionStart(configBuffer.data(), configLength), RET_SUCC);
|
||||
ASSERT_GT(gpuPlugin->callbacks->onPluginReportResult(dataBuffer.data(), gpuPlugin->resultBufferSizeHint), 0);
|
||||
EXPECT_EQ(gpuPlugin->callbacks->onPluginSessionStop(), RET_SUCC);
|
||||
|
||||
// 反序列化失败导致的start失败
|
||||
configLength++;
|
||||
std::vector<uint8_t> configBuffer2(configLength);
|
||||
EXPECT_TRUE(protoConfig.SerializeToArray(configBuffer2.data(), configLength));
|
||||
EXPECT_EQ(gpuPlugin->callbacks->onPluginSessionStart(configBuffer2.data(), configLength+1), RET_FAIL);
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
@ -0,0 +1 @@
|
||||
12
|
@ -0,0 +1 @@
|
||||
8
|
130
protos/types/plugins/gpu_data/BUILD.gn
Normal file
130
protos/types/plugins/gpu_data/BUILD.gn
Normal file
@ -0,0 +1,130 @@
|
||||
# Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
|
||||
# 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/ohos.gni")
|
||||
import("../../../protos.gni")
|
||||
|
||||
gpu_data_sources = [
|
||||
"./gpu_plugin_config.proto",
|
||||
"./gpu_plugin_result.proto",
|
||||
]
|
||||
|
||||
#######################################################
|
||||
proto_out_dir = "$root_gen_dir/cpp/" + rebase_path(".", "//")
|
||||
proto_rel_out_dir = rebase_path(proto_out_dir, root_build_dir)
|
||||
|
||||
gpu_data_codegen = []
|
||||
gpu_data_codegen_standard = []
|
||||
gpu_data_codegen_encoder = []
|
||||
gpu_data_codegen_all = []
|
||||
foreach(proto, gpu_data_sources) {
|
||||
name = get_path_info(proto, "name")
|
||||
gpu_data_codegen += [
|
||||
"$proto_out_dir/$name.pb.h",
|
||||
"$proto_out_dir/$name.pb.cc",
|
||||
]
|
||||
gpu_data_codegen_standard += [
|
||||
"$proto_out_dir/${name}_standard.pb.h",
|
||||
"$proto_out_dir/${name}_standard.pb.cc",
|
||||
]
|
||||
gpu_data_codegen_encoder += [
|
||||
"$proto_out_dir/$name.pbencoder.h",
|
||||
"$proto_out_dir/$name.pbencoder.cc",
|
||||
]
|
||||
}
|
||||
gpu_data_codegen_all += gpu_data_codegen
|
||||
gpu_data_codegen_all += gpu_data_codegen_standard
|
||||
gpu_data_codegen_all += gpu_data_codegen_encoder
|
||||
|
||||
gpu_plugin_config_sources = [ "./gpu_plugin_config.proto" ]
|
||||
gpu_plugin_config_codegen_standard = []
|
||||
foreach(proto, gpu_plugin_config_sources) {
|
||||
name = get_path_info(proto, "name")
|
||||
gpu_plugin_config_codegen_standard += [
|
||||
"$proto_out_dir/${name}_standard.pb.h",
|
||||
"$proto_out_dir/${name}_standard.pb.cc",
|
||||
]
|
||||
}
|
||||
|
||||
config("gpu_include_config") {
|
||||
include_dirs = [ "$proto_out_dir" ]
|
||||
}
|
||||
|
||||
#######################################################
|
||||
action("gpu_data_cpp_gen") {
|
||||
script = "${OHOS_PROFILER_DIR}/build/protoc.sh"
|
||||
sources = gpu_data_sources
|
||||
outputs = gpu_data_codegen_all
|
||||
args = [
|
||||
"$libc_dir_proto",
|
||||
"$root_output_dir_proto",
|
||||
"$proto_rel_out_dir", # standard proto file destination path
|
||||
"--cpp_out",
|
||||
"$proto_rel_out_dir",
|
||||
"--proto_path",
|
||||
rebase_path(".", root_build_dir),
|
||||
]
|
||||
args += rebase_path(sources, root_build_dir)
|
||||
deps = [
|
||||
"${OHOS_PROFILER_3RDPARTY_PROTOBUF_DIR}:protoc(${host_toolchain})",
|
||||
"${OHOS_PROFILER_DIR}/device/services/ipc:protoencoder_plugin(${host_toolchain})",
|
||||
]
|
||||
}
|
||||
|
||||
ohos_source_set("gpu_data_cpp") {
|
||||
deps = [ ":gpu_data_cpp_gen" ]
|
||||
public_deps = [
|
||||
"${OHOS_PROFILER_3RDPARTY_PROTOBUF_DIR}:protobuf",
|
||||
"${OHOS_PROFILER_3RDPARTY_PROTOBUF_DIR}:protobuf_lite",
|
||||
]
|
||||
include_dirs = [ "$proto_out_dir" ]
|
||||
public_configs = [ ":gpu_include_config" ]
|
||||
sources = gpu_data_codegen
|
||||
subsystem_name = "${OHOS_PROFILER_SUBSYS_NAME}"
|
||||
part_name = "${OHOS_PROFILER_PART_NAME}"
|
||||
}
|
||||
|
||||
ohos_source_set("gpu_data_cpp_standard") {
|
||||
deps = [ ":gpu_data_cpp_gen" ]
|
||||
public_deps = [
|
||||
"${OHOS_PROFILER_3RDPARTY_PROTOBUF_DIR}:protobuf",
|
||||
"${OHOS_PROFILER_3RDPARTY_PROTOBUF_DIR}:protobuf_lite",
|
||||
]
|
||||
include_dirs = [ "$proto_out_dir" ]
|
||||
public_configs = [ ":gpu_include_config" ]
|
||||
sources = gpu_data_codegen_standard
|
||||
subsystem_name = "${OHOS_PROFILER_SUBSYS_NAME}"
|
||||
part_name = "${OHOS_PROFILER_PART_NAME}"
|
||||
}
|
||||
|
||||
ohos_source_set("gpu_plugin_config_cpp_standard") {
|
||||
deps = [ ":gpu_data_cpp_gen" ]
|
||||
public_deps = [
|
||||
"${OHOS_PROFILER_3RDPARTY_PROTOBUF_DIR}:protobuf",
|
||||
"${OHOS_PROFILER_3RDPARTY_PROTOBUF_DIR}:protobuf_lite",
|
||||
]
|
||||
include_dirs = [ "$proto_out_dir" ]
|
||||
public_configs = [ ":gpu_include_config" ]
|
||||
sources = gpu_plugin_config_codegen_standard
|
||||
subsystem_name = "${OHOS_PROFILER_SUBSYS_NAME}"
|
||||
part_name = "${OHOS_PROFILER_PART_NAME}"
|
||||
}
|
||||
|
||||
ohos_source_set("gpu_data_encoder") {
|
||||
deps = [ ":gpu_data_cpp_gen" ]
|
||||
include_dirs = [ "$proto_out_dir" ]
|
||||
public_configs = [ ":gpu_include_config" ]
|
||||
sources = gpu_data_codegen_encoder
|
||||
subsystem_name = "${OHOS_PROFILER_SUBSYS_NAME}"
|
||||
part_name = "${OHOS_PROFILER_PART_NAME}"
|
||||
}
|
24
protos/types/plugins/gpu_data/gpu_plugin_config.proto
Normal file
24
protos/types/plugins/gpu_data/gpu_plugin_config.proto
Normal file
@ -0,0 +1,24 @@
|
||||
// Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
|
||||
// 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.
|
||||
|
||||
syntax = "proto3";
|
||||
|
||||
option java_package = "ohos.devtools.datasources.transport.grpc.service";
|
||||
option optimize_for = LITE_RUNTIME;
|
||||
|
||||
|
||||
// Cpu plug-in configuration, passed to plug-in by plug-in service.
|
||||
message GpuConfig {
|
||||
int32 pid = 1;
|
||||
bool report_gpu_info = 2;
|
||||
}
|
22
protos/types/plugins/gpu_data/gpu_plugin_result.proto
Normal file
22
protos/types/plugins/gpu_data/gpu_plugin_result.proto
Normal file
@ -0,0 +1,22 @@
|
||||
// Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
|
||||
// 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.
|
||||
|
||||
syntax = "proto3";
|
||||
|
||||
option java_package = "ohos.devtools.datasources.transport.grpc.service";
|
||||
option optimize_for = LITE_RUNTIME;
|
||||
|
||||
message GpuData {
|
||||
uint64 boottime = 1;
|
||||
uint64 gpu_utilisation = 2;
|
||||
}
|
Loading…
Reference in New Issue
Block a user