• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
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 
24 namespace {
25 int g_testCount = 100;
26 constexpr int CONSUME_CPU_SLEEP_TIME = 500 * 1000;
27 constexpr int PROCESS_SLEEP_TIME = 10;
28 #if defined(__LP64__)
29 const std::string SO_PATH = "/system/lib64/libcpudataplugin.z.so";
30 #else
31 const std::string SO_PATH = "/system/lib/libcpudataplugin.z.so";
32 #endif
33 } // namespace
34 
Report(PluginModuleStruct * & cpuPlugin,std::vector<uint8_t> & dataBuffer)35 static void Report(PluginModuleStruct*& cpuPlugin, std::vector<uint8_t>& dataBuffer)
36 {
37     while (g_testCount--) {
38         int len = cpuPlugin->callbacks->onPluginReportResult(dataBuffer.data(), cpuPlugin->resultBufferSizeHint);
39         std::cout << "test:filler buffer length = " << len << std::endl;
40 
41         if (len > 0) {
42             CpuData cpuData;
43             cpuData.ParseFromArray(dataBuffer.data(), len);
44             std::cout << "test:ParseFromArray length = " << len << std::endl;
45 
46             CpuUsageInfo cpuUsageInfo = cpuData.cpu_usage_info();
47             std::cout << "prev_process_cpu_time_ms:" << cpuUsageInfo.prev_process_cpu_time_ms() << std::endl;
48             std::cout << "prev_system_cpu_time_ms:" << cpuUsageInfo.prev_system_cpu_time_ms() << std::endl;
49             std::cout << "prev_system_boot_time_ms:" << cpuUsageInfo.prev_system_boot_time_ms() << std::endl;
50             std::cout << "process_cpu_time_ms:" << cpuUsageInfo.process_cpu_time_ms() << std::endl;
51             std::cout << "system_cpu_time_ms:" << cpuUsageInfo.system_cpu_time_ms() << std::endl;
52             std::cout << "system_boot_time_ms:" << cpuUsageInfo.system_boot_time_ms() << std::endl;
53 
54             for (int i = 0; i < cpuUsageInfo.cores_size(); i++) {
55                 CpuCoreUsageInfo cpuCoreUsageInfo = cpuUsageInfo.cores()[i];
56                 std::cout << "cpu_core:" << cpuCoreUsageInfo.cpu_core() << std::endl;
57                 std::cout << "prev_system_cpu_time_ms:" << cpuCoreUsageInfo.prev_system_cpu_time_ms() << std::endl;
58                 std::cout << "prev_system_boot_time_ms:" << cpuCoreUsageInfo.prev_system_boot_time_ms() << std::endl;
59                 std::cout << "system_cpu_time_ms:" << cpuCoreUsageInfo.system_cpu_time_ms() << std::endl;
60                 std::cout << "system_boot_time_ms:" << cpuCoreUsageInfo.system_boot_time_ms() << std::endl;
61             }
62 
63             for (int i = 0; i < cpuData.thread_info_size(); i++) {
64                 ThreadInfo threadInfo = cpuData.thread_info()[i];
65                 std::cout << "tid : " << threadInfo.tid() << std::endl;
66                 std::cout << "thread_name : " << threadInfo.thread_name() << std::endl;
67                 std::cout << "thread_state : " << threadInfo.thread_state() << std::endl;
68                 std::cout << "prev_thread_cpu_time_ms : " << threadInfo.prev_thread_cpu_time_ms() << std::endl;
69                 std::cout << "thread_cpu_time_ms : " << threadInfo.thread_cpu_time_ms() << std::endl;
70             }
71         }
72 
73         std::cout << "test:sleep...................." << std::endl;
74         usleep(CONSUME_CPU_SLEEP_TIME);
75     }
76 }
77 
main(int agrc,char * agrv[])78 int main(int agrc, char* agrv[])
79 {
80     bool isConsumeCpu = false;
81     for (int i = 1; i < agrc; i++) {
82         isConsumeCpu = atoi(agrv[i]);
83     }
84 
85     if (isConsumeCpu || agrc == 1) {
86         CpuConfig protoConfig;
87         void* handle = dlopen(SO_PATH.c_str(), RTLD_LAZY);
88         if (handle == nullptr) {
89             const int bufSize = 256;
90             char buf[bufSize] = { 0 };
91             strerror_r(errno, buf, bufSize);
92             HILOG_ERROR(LOG_CORE, "test:dlopen err, errno(%d:%s)", errno, buf);
93             return 0;
94         }
95 
96         PluginModuleStruct* cpuPlugin = (PluginModuleStruct*)dlsym(handle, "g_pluginModule");
97         if (cpuPlugin == nullptr) {
98             dlclose(handle);
99             return 0;
100         }
101 
102         // Serialize config
103         int pid = getpid();
104         protoConfig.set_pid(pid);
105         int configLength = protoConfig.ByteSizeLong();
106         std::vector<uint8_t> configBuffer(configLength);
107         protoConfig.SerializeToArray(configBuffer.data(), configLength);
108 
109         // run plugin
110         std::vector<uint8_t> dataBuffer(cpuPlugin->resultBufferSizeHint);
111         cpuPlugin->callbacks->onPluginSessionStart(configBuffer.data(), configLength);
112         if (agrc == 1) {
113             Report(cpuPlugin, dataBuffer);
114         } else {
115             // 循环上报数据消耗cpu
116             while (1) {
117                 int len = cpuPlugin->callbacks->onPluginReportResult(dataBuffer.data(),
118                     cpuPlugin->resultBufferSizeHint);
119                 if (len > 0) {
120                     CpuData cpuData;
121                     cpuData.ParseFromArray(dataBuffer.data(), len);
122                 }
123                 const int interval = 100000;
124                 usleep(interval);
125             }
126         }
127         cpuPlugin->callbacks->onPluginSessionStop();
128         dlclose(handle);
129     }
130 
131     sleep(PROCESS_SLEEP_TIME);
132     return 0;
133 }
134