1/* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16#include <dlfcn.h> 17#include <unistd.h> 18 19#include "cpu_plugin_config.pb.h" 20#include "cpu_plugin_result.pb.h" 21#include "logging.h" 22#include "plugin_module_api.h" 23 24namespace { 25int g_testCount = 100; 26constexpr int CONSUME_CPU_SLEEP_TIME = 500 * 1000; 27constexpr int PROCESS_SLEEP_TIME = 10; 28const std::string SO_PATH = "libcpudataplugin.z.so"; 29} // namespace 30 31static void Report(PluginModuleStruct*& cpuPlugin, std::vector<uint8_t>& dataBuffer) 32{ 33 while (g_testCount--) { 34 int len = cpuPlugin->callbacks->onPluginReportResult(dataBuffer.data(), cpuPlugin->resultBufferSizeHint); 35 std::cout << "test:filler buffer length = " << len << std::endl; 36 37 if (len > 0) { 38 CpuData cpuData; 39 cpuData.ParseFromArray(dataBuffer.data(), len); 40 std::cout << "test:ParseFromArray length = " << len << std::endl; 41 42 CpuUsageInfo cpuUsageInfo = cpuData.cpu_usage_info(); 43 std::cout << "prev_process_cpu_time_ms:" << cpuUsageInfo.prev_process_cpu_time_ms() << std::endl; 44 std::cout << "prev_system_cpu_time_ms:" << cpuUsageInfo.prev_system_cpu_time_ms() << std::endl; 45 std::cout << "prev_system_boot_time_ms:" << cpuUsageInfo.prev_system_boot_time_ms() << std::endl; 46 std::cout << "process_cpu_time_ms:" << cpuUsageInfo.process_cpu_time_ms() << std::endl; 47 std::cout << "system_cpu_time_ms:" << cpuUsageInfo.system_cpu_time_ms() << std::endl; 48 std::cout << "system_boot_time_ms:" << cpuUsageInfo.system_boot_time_ms() << std::endl; 49 50 for (int i = 0; i < cpuUsageInfo.cores_size(); i++) { 51 CpuCoreUsageInfo cpuCoreUsageInfo = cpuUsageInfo.cores()[i]; 52 std::cout << "cpu_core:" << cpuCoreUsageInfo.cpu_core() << std::endl; 53 std::cout << "prev_system_cpu_time_ms:" << cpuCoreUsageInfo.prev_system_cpu_time_ms() << std::endl; 54 std::cout << "prev_system_boot_time_ms:" << cpuCoreUsageInfo.prev_system_boot_time_ms() << std::endl; 55 std::cout << "system_cpu_time_ms:" << cpuCoreUsageInfo.system_cpu_time_ms() << std::endl; 56 std::cout << "system_boot_time_ms:" << cpuCoreUsageInfo.system_boot_time_ms() << std::endl; 57 } 58 59 for (int i = 0; i < cpuData.thread_info_size(); i++) { 60 ThreadInfo threadInfo = cpuData.thread_info()[i]; 61 std::cout << "tid : " << threadInfo.tid() << std::endl; 62 std::cout << "thread_name : " << threadInfo.thread_name() << std::endl; 63 std::cout << "thread_state : " << threadInfo.thread_state() << std::endl; 64 std::cout << "prev_thread_cpu_time_ms : " << threadInfo.prev_thread_cpu_time_ms() << std::endl; 65 std::cout << "thread_cpu_time_ms : " << threadInfo.thread_cpu_time_ms() << std::endl; 66 } 67 } 68 69 std::cout << "test:sleep...................." << std::endl; 70 usleep(CONSUME_CPU_SLEEP_TIME); 71 } 72} 73 74int main(int agrc, char* agrv[]) 75{ 76 bool isConsumeCpu = false; 77 for (int i = 1; i < agrc; i++) { 78 isConsumeCpu = atoi(agrv[i]); 79 } 80 81 if (isConsumeCpu || agrc == 1) { 82 CpuConfig protoConfig; 83 void* handle = dlopen(SO_PATH.c_str(), RTLD_LAZY); 84 if (handle == nullptr) { 85 const int bufSize = 256; 86 char buf[bufSize] = { 0 }; 87 strerror_r(errno, buf, bufSize); 88 PROFILER_LOG_ERROR(LOG_CORE, "test:dlopen err, errno(%d:%s)", errno, buf); 89 return 0; 90 } 91 92 PluginModuleStruct* cpuPlugin = static_cast<PluginModuleStruct*>(dlsym(handle, "g_pluginModule")); 93 if (cpuPlugin == nullptr) { 94 dlclose(handle); 95 return 0; 96 } 97 98 // Serialize config 99 int pid = getpid(); 100 protoConfig.set_pid(pid); 101 int configLength = protoConfig.ByteSizeLong(); 102 std::vector<uint8_t> configBuffer(configLength); 103 protoConfig.SerializeToArray(configBuffer.data(), configLength); 104 105 // run plugin 106 std::vector<uint8_t> dataBuffer(cpuPlugin->resultBufferSizeHint); 107 cpuPlugin->callbacks->onPluginSessionStart(configBuffer.data(), configLength); 108 if (agrc == 1) { 109 Report(cpuPlugin, dataBuffer); 110 } else { 111 // 循环上报数据消耗cpu 112 while (1) { 113 int len = cpuPlugin->callbacks->onPluginReportResult(dataBuffer.data(), 114 cpuPlugin->resultBufferSizeHint); 115 if (len > 0) { 116 CpuData cpuData; 117 cpuData.ParseFromArray(dataBuffer.data(), len); 118 } 119 const int interval = 100000; 120 usleep(interval); 121 } 122 } 123 cpuPlugin->callbacks->onPluginSessionStop(); 124 dlclose(handle); 125 } 126 127 sleep(PROCESS_SLEEP_TIME); 128 return 0; 129} 130