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 #include <cinttypes>
16 #include <gtest/gtest.h>
17 #include <dlfcn.h>
18
19 #include "cpu_data_plugin.h"
20 #include "plugin_module_api.h"
21
22 using namespace testing::ext;
23
24 namespace {
25 const std::string DEFAULT_TEST_PATH = "/data/local/tmp/resources";
26 const std::string SO_PATH = "libcpudataplugin.z.so";
27 const std::string DEFAULT_BIN_PATH("/data/local/tmp/cpudataplugintest");
28 constexpr uint32_t BUF_SIZE = 4 * 1024 * 1024;
29 constexpr int TEST_PID = 1;
30
31 std::string g_path;
32 std::string g_testPath;
33 std::vector<int> g_tidList = {1872, 1965, 1966, 1967, 1968, 1995, 1996};
34
35 constexpr int CORE_NUM = 6;
36 constexpr int THREAD_NUM = 7;
37 constexpr int FIRST_THREAD_NUM = 5;
38 constexpr int SECOND_THREAD_NUM = 6;
39 const int PERCENT = 100;
40
41 struct TestSystemStat {
42 int32_t core;
43 int64_t user;
44 int64_t nice;
45 int64_t system;
46 int64_t idle;
47 int64_t iowait;
48 int64_t irq;
49 int64_t softirq;
50 int64_t steal;
51 };
52
53 struct TestCpuUsage {
54 double userLoad;
55 double sysLoad;
56 double totalLoad;
57 };
58
59 struct TestStat {
60 int64_t utime;
61 int64_t stime;
62 int64_t cutime;
63 int64_t cstime;
64 };
65
66 struct TestTidStat {
67 int32_t tid;
68 std::string name;
69 ThreadState state;
70 TestStat stat;
71 };
72
73 struct TestFreq {
74 int32_t curFreq;
75 int32_t maxFreq;
76 int32_t minFreq;
77 };
78
79 TestSystemStat g_systemStat[CORE_NUM + 1] = {
80 {-1, 24875428, 3952448, 11859815, 1193297105, 8980661, 0, 2607250, 0},
81 {0, 4165400, 662862, 1966195, 196987024, 3571925, 0, 817371, 0},
82 {1, 3861506, 676578, 1702753, 199535158, 1752008, 0, 401639, 0},
83 {2, 3549890, 676286, 1544630, 200640747, 1133743, 0, 205972, 0},
84 {3, 3336646, 676939, 1458898, 201176432, 854578, 0, 124812, 0},
85 {4, 4566158, 601107, 2305309, 197166395, 929594, 0, 1007959, 0},
86 {5, 5395826, 658673, 2882028, 197791346, 738811, 0, 49496, 0},
87 };
88 TestSystemStat g_systemStat1[CORE_NUM + 1] = {
89 {-1, 1234567, 2345678, 1111111, 1193291234, 3546789, 0, 2345678, 0},
90 {0, 2345678, 662862, 1966195, 1111111, 3571925, 0, 817371, 0},
91 {1, 3861506, 2345678, 1702753, 199535158, 1752008, 0, 401639, 0},
92 {2, 3549890, 1111111, 2345678, 1234567, 1133743, 0, 205972, 0},
93 {3, 3336646, 676939, 1458898, 2345678, 854578, 0, 2345678, 0},
94 {4, 1111111, 601107, 2305309, 3546789, 929594, 0, 1007959, 0},
95 {5, 3546789, 658673, 1234567, 197791346, 738811, 0, 49496, 0},
96 };
97 TestSystemStat g_systemStat2[CORE_NUM + 1] = {
98 {-1, 3546789, 2345678, 1111111, 1193291234, 3546789, 0, 2345678, 0},
99 {0, 3546789, 662862, 1966195, 2345678, 3571925, 0, 817371, 0},
100 {1, 3861506, 1111111, 1702753, 199535158, 1752008, 0, 401639, 0},
101 {2, 3549890, 2345678, 2345678, 3546789, 1111111, 0, 205972, 0},
102 {3, 3336646, 676939, 1458898, 2345678, 854578, 0, 2345678, 0},
103 {4, 2345678, 601107, 2305309, 3546789, 929594, 0, 1111111, 0},
104 {5, 3546789, 658673, 3546789, 197791346, 738811, 0, 49496, 0},
105 };
106
107 TestStat g_pidStat = {60, 10, 20, 30};
108 TestStat g_pidStat1 = {50, 10, 40, 10};
109 TestStat g_pidStat2 = {70, 0, 10, 20};
110
111 TestTidStat g_tidStat[THREAD_NUM] = {
112 {1872, "ibus-x11", THREAD_RUNNING, {17, 5, 10, 10}},
113 {1965, "ibus-x1:disk$0", THREAD_SLEEPING, {8, 1, 5, 8}},
114 {1966, "ibus-x1:disk$1", THREAD_UNSPECIFIED, {0, 0, 0, 0}},
115 {1967, "ibus-x1:disk$2", THREAD_SLEEPING, {10, 1, 5, 8}},
116 {1968, "ibus-x1:disk$3", THREAD_STOPPED, {7, 0, 0, 0}},
117 {1995, "gmain", THREAD_SLEEPING, {15, 3, 0, 4}},
118 {1996, "gdbus", THREAD_WAITING, {5, 0, 0, 0}},
119 };
120 TestTidStat g_tidStat1[THREAD_NUM] = {
121 {1209, "skytone:service", THREAD_RUNNING, {22, 8, 0, 0}},
122 {1646, "Jit thread pool", THREAD_UNSPECIFIED, {2, 1, 0, 0}},
123 {1654, "Signal Catcher", THREAD_STOPPED, {1, 0, 0, 0}},
124 {1655, "HeapTaskDaemon", THREAD_SLEEPING, {3, 0, 0, 0}},
125 {1656, "ReferenceQueueD", THREAD_WAITING, {0, 0, 0, 0}},
126 };
127 TestTidStat g_tidStat2[THREAD_NUM] = {
128 {1662, "FinalizerDaemon", THREAD_STOPPED, {0, 0, 0, 0}},
129 {1663, "FinalizerWatchd", THREAD_RUNNING, {0, 0, 0, 0}},
130 {1666, "Binder:1209_1", THREAD_RUNNING, {0, 0, 0, 0}},
131 {1679, "Binder:1209_2", THREAD_UNSPECIFIED, {0, 0, 0, 0}},
132 {1680, "Binder:1209_3", THREAD_WAITING, {54, 8, 0, 0}},
133 {1681, "Binder:1209_4", THREAD_SLEEPING, {0, 0, 0, 0}},
134 };
135
136 TestFreq g_freq[CORE_NUM + 1] = {
137 {1018000, 3844000, 509000}, {1023000, 2844000, 509000},
138 {1011000, 3844000, 509000}, {1518000, 3844000, 1018000},
139 {1245000, 1844000, 1018000}, {1767000, 3044000, 1018000},
140 };
141
142 class CpuDataPluginTest : public ::testing::Test {
143 public:
SetUpTestCase()144 static void SetUpTestCase() {}
145
TearDownTestCase()146 static void TearDownTestCase()
147 {
148 if (access(g_testPath.c_str(), F_OK) == 0) {
149 std::string str = "rm -rf " + g_testPath;
150 system(str.c_str());
151 }
152 }
153 };
154
Getexepath()155 string Getexepath()
156 {
157 char buf[PATH_MAX] = "";
158 std::string path = "/proc/self/exe";
159 size_t rslt = readlink(path.c_str(), buf, sizeof(buf));
160 if (rslt < 0 || (rslt >= sizeof(buf))) {
161 return "";
162 }
163 buf[rslt] = '\0';
164 for (int i = rslt; i >= 0; i--) {
165 if (buf[i] == '/') {
166 buf[i + 1] = '\0';
167 break;
168 }
169 }
170 return buf;
171 }
172
GetFullPath(std::string path)173 std::string GetFullPath(std::string path)
174 {
175 if (path.size() > 0 && path[0] != '/') {
176 return Getexepath() + path;
177 }
178 return path;
179 }
180
CheckTid(std::vector<int> & tidListTmp)181 bool CheckTid(std::vector<int>& tidListTmp)
182 {
183 sort(g_tidList.begin(), g_tidList.end());
184 for (size_t i = 0; i < g_tidList.size(); i++) {
185 if (tidListTmp.at(i) != g_tidList.at(i)) {
186 return false;
187 }
188 }
189 return true;
190 }
191
PluginCpuinfoStub(CpuDataPlugin & cpuPlugin,CpuData & cpuData,int pid,bool reportProcess,bool unusualBuff)192 bool PluginCpuinfoStub(CpuDataPlugin& cpuPlugin, CpuData& cpuData, int pid, bool reportProcess, bool unusualBuff)
193 {
194 CpuConfig protoConfig;
195 protoConfig.set_pid(pid);
196 if (reportProcess) {
197 protoConfig.set_report_process_info(true);
198 }
199
200 // serialize
201 std::vector<uint8_t> configData(protoConfig.ByteSizeLong());
202 int ret = protoConfig.SerializeToArray(configData.data(), configData.size());
203
204 // start
205 ret = cpuPlugin.Start(configData.data(), configData.size());
206 if (ret < 0) {
207 return false;
208 }
209
210 // report
211 std::vector<uint8_t> bufferData(BUF_SIZE);
212 if (unusualBuff) { // buffer异常,调整缓冲区长度为1,测试异常情况
213 bufferData.resize(1, 0);
214 }
215
216 ret = cpuPlugin.Report(bufferData.data(), bufferData.size());
217 if (ret > 0) {
218 cpuData.ParseFromArray(bufferData.data(), ret);
219 return true;
220 }
221
222 return false;
223 }
224
GetSystemCpuTime(TestSystemStat & stat,int64_t Hz,int64_t & usageTime,int64_t & time)225 void GetSystemCpuTime(TestSystemStat& stat, int64_t Hz, int64_t& usageTime, int64_t& time)
226 {
227 usageTime = (stat.user + stat.nice + stat.system + stat.irq + stat.softirq + stat.steal) * Hz;
228 time = usageTime + (stat.idle + stat.iowait) * Hz;
229 }
230
GetCpuUsage(TestCpuUsage & cpuUsage,int64_t Hz)231 void GetCpuUsage(TestCpuUsage& cpuUsage, int64_t Hz)
232 {
233 TestSystemStat stat = g_systemStat[0];
234 int64_t userTime = stat.user * Hz;
235 int64_t sysTime = stat.system * Hz;
236 int64_t totalTime = (stat.user + stat.nice + stat.system + stat.irq + stat.softirq + stat.steal) * Hz;
237 int64_t bootTime = totalTime + (stat.idle + stat.iowait) * Hz;
238 cpuUsage.userLoad = (static_cast<double>(userTime) / bootTime) * PERCENT;
239 cpuUsage.sysLoad = (static_cast<double>(sysTime) / bootTime) * PERCENT;
240 cpuUsage.totalLoad = (static_cast<double>(totalTime) / bootTime) * PERCENT;
241 }
242
243 /**
244 * @tc.name: cpu plugin
245 * @tc.desc: Test whether the path exists.
246 * @tc.type: FUNC
247 */
248 HWTEST_F(CpuDataPluginTest, TestPath, TestSize.Level1)
249 {
250 g_path = GetFullPath(DEFAULT_TEST_PATH);
251 g_testPath = g_path;
252 EXPECT_NE("", g_path);
253 }
254
255 /**
256 * @tc.name: cpu plugin
257 * @tc.desc: Tid list test in a specific directory.
258 * @tc.type: FUNC
259 */
260 HWTEST_F(CpuDataPluginTest, TestTidlist, TestSize.Level1)
261 {
262 CpuDataPlugin cpuPlugin;
263 std::string path = g_path + "/proc/1872/task/";
264 DIR* dir = cpuPlugin.OpenDestDir(path);
265 EXPECT_NE(nullptr, dir);
266
267 std::vector<int> tidListTmp;
268 while (int32_t pid = cpuPlugin.GetValidTid(dir)) {
269 tidListTmp.push_back(pid);
270 sort(tidListTmp.begin(), tidListTmp.end());
271 }
272 EXPECT_TRUE(CheckTid(tidListTmp));
273 }
274
275 /**
276 * @tc.name: cpu plugin
277 * @tc.desc: a part of cpu information test for specific pid.
278 * @tc.type: FUNC
279 */
280 HWTEST_F(CpuDataPluginTest, TestPluginInfo, TestSize.Level1)
281 {
282 CpuDataPlugin cpuPlugin;
283 CpuData cpuData;
284 cpuPlugin.SetFreqPath(DEFAULT_TEST_PATH);
285 cpuPlugin.SetPath(DEFAULT_TEST_PATH + "/proc/");
286 EXPECT_TRUE(PluginCpuinfoStub(cpuPlugin, cpuData, 1872, true, false));
287
288 TestCpuUsage cpuUsage;
289 int64_t systemCpuTime = 0;
290 int64_t systemBootTime = 0;
291 int64_t Hz = cpuPlugin.GetUserHz();
292 int64_t processCpuTime = (g_pidStat.utime + g_pidStat.stime) * Hz;
293 GetSystemCpuTime(g_systemStat[0], Hz, systemCpuTime, systemBootTime);
294 GetCpuUsage(cpuUsage, Hz);
295
296 CpuUsageInfo cpuUsageInfo = cpuData.cpu_usage_info();
297 EXPECT_EQ(cpuUsageInfo.prev_process_cpu_time_ms(), 0);
298 EXPECT_EQ(cpuUsageInfo.prev_system_cpu_time_ms(), 0);
299 EXPECT_EQ(cpuUsageInfo.prev_system_boot_time_ms(), 0);
300 EXPECT_EQ(cpuUsageInfo.process_cpu_time_ms(), processCpuTime);
301 EXPECT_EQ(cpuUsageInfo.system_cpu_time_ms(), systemCpuTime);
302 EXPECT_EQ(cpuUsageInfo.system_boot_time_ms(), systemBootTime);
303 EXPECT_EQ(cpuData.process_num(), 1);
304 EXPECT_FLOAT_EQ(cpuData.user_load(), cpuUsage.userLoad);
305 EXPECT_FLOAT_EQ(cpuData.sys_load(), cpuUsage.sysLoad);
306 EXPECT_FLOAT_EQ(cpuData.total_load(), cpuUsage.totalLoad);
307 ASSERT_EQ(cpuUsageInfo.cores_size(), 6);
308 for (int i = 1; i <= CORE_NUM; i++) {
309 CpuCoreUsageInfo cpuCoreUsageInfo = cpuUsageInfo.cores()[i - 1];
310 GetSystemCpuTime(g_systemStat[i], Hz, systemCpuTime, systemBootTime);
311 EXPECT_EQ(cpuCoreUsageInfo.cpu_core(), g_systemStat[i].core);
312 EXPECT_EQ(cpuCoreUsageInfo.prev_system_cpu_time_ms(), 0);
313 EXPECT_EQ(cpuCoreUsageInfo.prev_system_boot_time_ms(), 0);
314 EXPECT_EQ(cpuCoreUsageInfo.system_cpu_time_ms(), systemCpuTime);
315 EXPECT_EQ(cpuCoreUsageInfo.system_boot_time_ms(), systemBootTime);
316
317 EXPECT_EQ(cpuCoreUsageInfo.frequency().min_frequency_khz(), g_freq[i - 1].minFreq);
318 EXPECT_EQ(cpuCoreUsageInfo.frequency().max_frequency_khz(), g_freq[i - 1].maxFreq);
319 EXPECT_EQ(cpuCoreUsageInfo.frequency().cur_frequency_khz(), g_freq[i - 1].curFreq);
320 if (i == 1) { // cpu0为大核
321 EXPECT_EQ(cpuCoreUsageInfo.is_little_core(), false);
322 } else {
323 EXPECT_EQ(cpuCoreUsageInfo.is_little_core(), true);
324 }
325 }
326 }
327
328 /**
329 * @tc.name: cpu plugin
330 * @tc.desc: cpu information test for specific pid.
331 * @tc.type: FUNC
332 */
333 HWTEST_F(CpuDataPluginTest, TestPlugin, TestSize.Level1)
334 {
335 CpuDataPlugin cpuPlugin;
336 CpuData cpuData;
337 g_path = g_testPath;
338 cpuPlugin.SetFreqPath(g_path);
339 g_path = g_testPath + "/proc/";
340 cpuPlugin.SetPath(g_path);
341 EXPECT_TRUE(PluginCpuinfoStub(cpuPlugin, cpuData, 1872, false, false));
342
343 int64_t Hz = cpuPlugin.GetUserHz();
344 int64_t threadCpuTime;
345 ASSERT_EQ(cpuData.thread_info_size(), 7);
346 for (int i = 0; i < THREAD_NUM && i < cpuData.thread_info().size(); i++) {
347 threadCpuTime = (g_tidStat[i].stat.utime + g_tidStat[i].stat.stime) * Hz;
348 ThreadInfo threadInfo = cpuData.thread_info()[i];
349 EXPECT_EQ(threadInfo.tid(), g_tidStat[i].tid);
350 EXPECT_STREQ(threadInfo.thread_name().c_str(), g_tidStat[i].name.c_str());
351 EXPECT_EQ(threadInfo.thread_state(), g_tidStat[i].state);
352 EXPECT_EQ(threadInfo.thread_cpu_time_ms(), threadCpuTime);
353 EXPECT_EQ(threadInfo.prev_thread_cpu_time_ms(), 0);
354 }
355
356 EXPECT_EQ(cpuPlugin.Stop(), 0);
357
358 // 缓冲区异常
359 EXPECT_FALSE(PluginCpuinfoStub(cpuPlugin, cpuData, 1872, false, true));
360 EXPECT_EQ(cpuPlugin.Stop(), 0);
361 }
362
363 /**
364 * @tc.name: cpu plugin
365 * @tc.desc: cpu information test for unusual path.
366 * @tc.type: FUNC
367 */
368 HWTEST_F(CpuDataPluginTest, TestPluginBoundary, TestSize.Level1)
369 {
370 CpuDataPlugin cpuPlugin;
371 CpuData cpuData;
372 g_path = g_testPath + "/proc/";
373 cpuPlugin.SetPath(g_path);
374 EXPECT_FALSE(PluginCpuinfoStub(cpuPlugin, cpuData, -1, false, false));
375 EXPECT_FALSE(PluginCpuinfoStub(cpuPlugin, cpuData, 12345, false, false));
376
377 CpuDataPlugin cpuPlugin2;
378 cpuPlugin2.SetPath("123");
379 EXPECT_FALSE(PluginCpuinfoStub(cpuPlugin2, cpuData, 1872, false, false));
380 EXPECT_FALSE(PluginCpuinfoStub(cpuPlugin2, cpuData, -1, false, false));
381 EXPECT_FALSE(PluginCpuinfoStub(cpuPlugin2, cpuData, 12345, false, false));
382 }
383
384 /**
385 * @tc.name: cpu plugin
386 * @tc.desc: cpu plugin registration test.
387 * @tc.type: FUNC
388 */
389 HWTEST_F(CpuDataPluginTest, TestPluginRegister, TestSize.Level1)
390 {
391 void* handle = dlopen(SO_PATH.c_str(), RTLD_LAZY);
392 ASSERT_NE(handle, nullptr);
393 PluginModuleStruct* cpuPlugin = (PluginModuleStruct*)dlsym(handle, "g_pluginModule");
394 ASSERT_NE(cpuPlugin, nullptr);
395 EXPECT_STREQ(cpuPlugin->name, "cpu-plugin");
396 EXPECT_EQ(cpuPlugin->resultBufferSizeHint, BUF_SIZE);
397
398 // Serialize config
399 CpuConfig protoConfig;
400 protoConfig.set_pid(TEST_PID);
401 int configLength = protoConfig.ByteSizeLong();
402 ASSERT_GT(configLength, 0);
403 std::vector<uint8_t> configBuffer(configLength);
404 EXPECT_TRUE(protoConfig.SerializeToArray(configBuffer.data(), configLength));
405
406 // run plugin
407 std::vector<uint8_t> dataBuffer(cpuPlugin->resultBufferSizeHint);
408 EXPECT_EQ(cpuPlugin->callbacks->onPluginSessionStart(configBuffer.data(), configLength), RET_SUCC);
409 ASSERT_GT(cpuPlugin->callbacks->onPluginReportResult(dataBuffer.data(), cpuPlugin->resultBufferSizeHint), 0);
410 EXPECT_EQ(cpuPlugin->callbacks->onPluginSessionStop(), RET_SUCC);
411
412 // 反序列化失败导致的start失败
413 EXPECT_EQ(cpuPlugin->callbacks->onPluginSessionStart(configBuffer.data(), configLength+1), RET_FAIL);
414 }
415
416 /**
417 * @tc.name: cpu plugin
418 * @tc.desc: a part of cpu process information test.
419 * @tc.type: FUNC
420 */
421 HWTEST_F(CpuDataPluginTest, TestPluginProcessInfo, TestSize.Level1)
422 {
423 CpuData cpuData;
424 CpuDataPlugin cpuPlugin;
425 int64_t Hz = cpuPlugin.GetUserHz();
426 std::string proStr1 = "2000 (ibus-x11) S 1 1865 1780 1025 1780 4194304 3233 0 457 0 50 10 40 10 20 0 7";
427 std::string proStr2 = "2000 (ibus-x12) R 1 1865 1780 1025 1780 4194304 3233 0 457 0 70 0 10 20 20 0 7";
428 int64_t processCpuTime1 = (g_pidStat1.utime + g_pidStat1.stime) * Hz;
429 int64_t processCpuTime2 = (g_pidStat2.utime + g_pidStat2.stime) * Hz;
430
431 // 存入proStr1
432 auto* cpuUsageInfo = cpuData.mutable_cpu_usage_info();
433 cpuPlugin.WriteProcessCpuUsage(*cpuUsageInfo, proStr1.c_str(), proStr1.length());
434 CpuUsageInfo cpuUsageInfo1 = cpuData.cpu_usage_info();
435 EXPECT_EQ(cpuUsageInfo1.prev_process_cpu_time_ms(), 0);
436 EXPECT_EQ(cpuUsageInfo1.process_cpu_time_ms(), processCpuTime1);
437
438 // 存入proStr2
439 cpuUsageInfo = cpuData.mutable_cpu_usage_info();
440 cpuPlugin.WriteProcessCpuUsage(*cpuUsageInfo, proStr2.c_str(), proStr2.length());
441 CpuUsageInfo cpuUsageInfo2 = cpuData.cpu_usage_info();
442 EXPECT_EQ(cpuUsageInfo2.prev_process_cpu_time_ms(), processCpuTime1);
443 EXPECT_EQ(cpuUsageInfo2.process_cpu_time_ms(), processCpuTime2);
444
445 // 重复存入proStr2
446 cpuUsageInfo = cpuData.mutable_cpu_usage_info();
447 cpuPlugin.WriteProcessCpuUsage(*cpuUsageInfo, proStr2.c_str(), proStr2.length());
448 CpuUsageInfo cpuUsageInfo3 = cpuData.cpu_usage_info();
449 EXPECT_EQ(cpuUsageInfo3.prev_process_cpu_time_ms(), processCpuTime2);
450 EXPECT_EQ(cpuUsageInfo3.process_cpu_time_ms(), processCpuTime2);
451 }
452
453 /**
454 * @tc.name: cpu plugin
455 * @tc.desc: cpu process information test for abnormal parameters.
456 * @tc.type: FUNC
457 */
458 HWTEST_F(CpuDataPluginTest, TestPluginProcessBoundary, TestSize.Level1)
459 {
460 CpuData cpuData;
461 CpuDataPlugin cpuPlugin;
462 std::string proStr;
463 int64_t Hz = cpuPlugin.GetUserHz();
464 int64_t processCpuTime = (g_pidStat1.utime + g_pidStat1.stime) * Hz;
465
466 // 空字符串
467 proStr = "";
468 auto* cpuUsageInfo = cpuData.mutable_cpu_usage_info();
469 cpuPlugin.WriteProcessCpuUsage(*cpuUsageInfo, proStr.c_str(), proStr.length());
470 CpuUsageInfo cpuUsageInfo1 = cpuData.cpu_usage_info();
471 EXPECT_EQ(cpuUsageInfo1.prev_process_cpu_time_ms(), 0);
472 EXPECT_EQ(cpuUsageInfo1.process_cpu_time_ms(), 0);
473
474 // 空格字符串
475 proStr = " ";
476 cpuUsageInfo = cpuData.mutable_cpu_usage_info();
477 cpuPlugin.WriteProcessCpuUsage(*cpuUsageInfo, proStr.c_str(), proStr.length());
478 cpuUsageInfo1 = cpuData.cpu_usage_info();
479 EXPECT_EQ(cpuUsageInfo1.prev_process_cpu_time_ms(), 0);
480 EXPECT_EQ(cpuUsageInfo1.process_cpu_time_ms(), 0);
481
482 // 数据错误
483 proStr = "2000";
484 cpuUsageInfo = cpuData.mutable_cpu_usage_info();
485 cpuPlugin.WriteProcessCpuUsage(*cpuUsageInfo, proStr.c_str(), proStr.length());
486 cpuUsageInfo1 = cpuData.cpu_usage_info();
487 EXPECT_EQ(cpuUsageInfo1.prev_process_cpu_time_ms(), 0);
488 EXPECT_EQ(cpuUsageInfo1.process_cpu_time_ms(), 0);
489
490 // 数据不够("2000 (ibus-x11) S 1 1865 1780 1025 1780 4194304 3233 0 457 0 50 10 40 10 20 0 7"需要的字符串是50 10 40 10)
491 proStr = "2000 (ibus-x11) S 1 1865 1780 1025 1780 4194304 3233 0 457 0 50 10";
492 cpuUsageInfo = cpuData.mutable_cpu_usage_info();
493 cpuPlugin.WriteProcessCpuUsage(*cpuUsageInfo, proStr.c_str(), proStr.length());
494 cpuUsageInfo1 = cpuData.cpu_usage_info();
495 EXPECT_EQ(cpuUsageInfo1.prev_process_cpu_time_ms(), 0);
496 EXPECT_EQ(cpuUsageInfo1.process_cpu_time_ms(), 0);
497
498 // 多个空格,可以取出正确数据
499 proStr = "2000 (ibus-x11) S 1 1865 1780 1025 1780 4194304 3233 0 457 0 50 10 40 10 20 ";
500 cpuUsageInfo = cpuData.mutable_cpu_usage_info();
501 cpuPlugin.WriteProcessCpuUsage(*cpuUsageInfo, proStr.c_str(), proStr.length());
502 cpuUsageInfo1 = cpuData.cpu_usage_info();
503 EXPECT_EQ(cpuUsageInfo1.prev_process_cpu_time_ms(), 0);
504 EXPECT_EQ(cpuUsageInfo1.process_cpu_time_ms(), processCpuTime);
505
506 // 最后一个数据之后没有空格,可以取出正确数据
507 proStr = "2000 (ibus-x11) S 1 1865 1780 1025 1780 4194304 3233 0 457 0 50 10 40 10 20";
508 cpuUsageInfo = cpuData.mutable_cpu_usage_info();
509 cpuPlugin.WriteProcessCpuUsage(*cpuUsageInfo, proStr.c_str(), proStr.length());
510 cpuUsageInfo1 = cpuData.cpu_usage_info();
511 EXPECT_EQ(cpuUsageInfo1.prev_process_cpu_time_ms(), processCpuTime);
512 EXPECT_EQ(cpuUsageInfo1.process_cpu_time_ms(), processCpuTime);
513 }
514
515 /**
516 * @tc.name: cpu plugin
517 * @tc.desc: a part of cpu system information test.
518 * @tc.type: FUNC
519 */
520 HWTEST_F(CpuDataPluginTest, TestPluginSystemInfo, TestSize.Level1)
521 {
522 std::string sysStr1 = "cpu 1234567 2345678 1111111 1193291234 3546789 0 2345678 0 0\n"
523 "cpu0 2345678 662862 1966195 1111111 3571925 0 817371 0 0\n"
524 "cpu1 3861506 2345678 1702753 199535158 1752008 0 401639 0 0\n"
525 "cpu2 3549890 1111111 2345678 1234567 1133743 0 205972 0 0\n"
526 "cpu3 3336646 676939 1458898 2345678 854578 0 2345678 0 0\n"
527 "cpu4 1111111 601107 2305309 3546789 929594 0 1007959 0 0\n"
528 "cpu5 3546789 658673 1234567 197791346 738811 0 49496 0 0\n";
529 std::string sysStr2 = "cpu 3546789 2345678 1111111 1193291234 3546789 0 2345678 0 0\n"
530 "cpu0 3546789 662862 1966195 2345678 3571925 0 817371 0 0\n"
531 "cpu1 3861506 1111111 1702753 199535158 1752008 0 401639 0 0\n"
532 "cpu2 3549890 2345678 2345678 3546789 1111111 0 205972 0 0\n"
533 "cpu3 3336646 676939 1458898 2345678 854578 0 2345678 0 0\n"
534 "cpu4 2345678 601107 2305309 3546789 929594 0 1111111 0 0\n"
535 "cpu5 3546789 658673 3546789 197791346 738811 0 49496 0 0\n";
536 CpuData cpuData;
537 CpuDataPlugin cpuPlugin;
538 int64_t Hz = cpuPlugin.GetUserHz();
539
540 // 存入sysStr1
541 int64_t systemCpuTime1 = 0;
542 int64_t systemBootTime1 = 0;
543 CpuLoadData cpuLoadData;
544 GetSystemCpuTime(g_systemStat1[0], Hz, systemCpuTime1, systemBootTime1);
545 cpuPlugin.WriteSystemCpuUsage(*cpuData.mutable_cpu_usage_info(), cpuLoadData, sysStr1.c_str(), sysStr1.length());
546 CpuUsageInfo cpuUsageInfo1 = cpuData.cpu_usage_info();
547 EXPECT_EQ(cpuUsageInfo1.prev_system_cpu_time_ms(), 0);
548 EXPECT_EQ(cpuUsageInfo1.prev_system_boot_time_ms(), 0);
549 EXPECT_EQ(cpuUsageInfo1.system_cpu_time_ms(), systemCpuTime1);
550 EXPECT_EQ(cpuUsageInfo1.system_boot_time_ms(), systemBootTime1);
551 ASSERT_EQ(cpuUsageInfo1.cores_size(), CORE_NUM);
552 std::vector<int64_t> systemCpuTimeVec1(CORE_NUM);
553 std::vector<int64_t> systemBootTimeVec1(CORE_NUM);
554 for (int i = 1; i <= CORE_NUM; i++) {
555 CpuCoreUsageInfo cpuCoreUsageInfo1 = cpuUsageInfo1.cores()[i - 1];
556 GetSystemCpuTime(g_systemStat1[i], Hz, systemCpuTimeVec1[i-1], systemBootTimeVec1[i-1]);
557 EXPECT_EQ(cpuCoreUsageInfo1.cpu_core(), i-1);
558 EXPECT_EQ(cpuCoreUsageInfo1.prev_system_cpu_time_ms(), 0);
559 EXPECT_EQ(cpuCoreUsageInfo1.prev_system_boot_time_ms(), 0);
560 EXPECT_EQ(cpuCoreUsageInfo1.system_cpu_time_ms(), systemCpuTimeVec1[i-1]);
561 EXPECT_EQ(cpuCoreUsageInfo1.system_boot_time_ms(), systemBootTimeVec1[i-1]);
562 }
563
564 // 存入sysStr2
565 int64_t systemCpuTime2 = 0;
566 int64_t systemBootTime2 = 0;
567 GetSystemCpuTime(g_systemStat2[0], Hz, systemCpuTime2, systemBootTime2);
568 cpuPlugin.WriteSystemCpuUsage(*cpuData.mutable_cpu_usage_info(), cpuLoadData, sysStr2.c_str(), sysStr2.length());
569 CpuUsageInfo cpuUsageInfo2 = cpuData.cpu_usage_info();
570 EXPECT_EQ(cpuUsageInfo2.prev_system_cpu_time_ms(), systemCpuTime1);
571 EXPECT_EQ(cpuUsageInfo2.prev_system_boot_time_ms(), systemBootTime1);
572 EXPECT_EQ(cpuUsageInfo2.system_cpu_time_ms(), systemCpuTime2);
573 EXPECT_EQ(cpuUsageInfo2.system_boot_time_ms(), systemBootTime2);
574 ASSERT_EQ(cpuUsageInfo2.cores_size(), CORE_NUM*2);
575 std::vector<int64_t> systemCpuTimeVec2(CORE_NUM);
576 std::vector<int64_t> systemBootTimeVec2(CORE_NUM);
577 for (int i = 1; i <= CORE_NUM; i++) {
578 CpuCoreUsageInfo cpuCoreUsageInfo2 = cpuUsageInfo2.cores()[CORE_NUM+i-1];
579 GetSystemCpuTime(g_systemStat2[i], Hz, systemCpuTimeVec2[i-1], systemBootTimeVec2[i-1]);
580 EXPECT_EQ(cpuCoreUsageInfo2.cpu_core(), i-1);
581 EXPECT_EQ(cpuCoreUsageInfo2.prev_system_cpu_time_ms(), systemCpuTimeVec1[i-1]);
582 EXPECT_EQ(cpuCoreUsageInfo2.prev_system_boot_time_ms(), systemBootTimeVec1[i-1]);
583 EXPECT_EQ(cpuCoreUsageInfo2.system_cpu_time_ms(), systemCpuTimeVec2[i-1]);
584 EXPECT_EQ(cpuCoreUsageInfo2.system_boot_time_ms(), systemBootTimeVec2[i-1]);
585 }
586
587 // 重复存入sysStr2
588 cpuPlugin.WriteSystemCpuUsage(*cpuData.mutable_cpu_usage_info(), cpuLoadData, sysStr2.c_str(), sysStr2.length());
589 CpuUsageInfo cpuUsageInfo3 = cpuData.cpu_usage_info();
590 EXPECT_EQ(cpuUsageInfo3.prev_system_cpu_time_ms(), systemCpuTime2);
591 EXPECT_EQ(cpuUsageInfo3.prev_system_boot_time_ms(), systemBootTime2);
592 EXPECT_EQ(cpuUsageInfo3.system_cpu_time_ms(), systemCpuTime2);
593 EXPECT_EQ(cpuUsageInfo3.system_boot_time_ms(), systemBootTime2);
594 ASSERT_EQ(cpuUsageInfo3.cores_size(), CORE_NUM*3);
595 std::vector<int64_t> systemCpuTimeVec3(CORE_NUM);
596 std::vector<int64_t> systemBootTimeVec3(CORE_NUM);
597 for (int i = 1; i <= CORE_NUM; i++) {
598 CpuCoreUsageInfo cpuCoreUsageInfo3 = cpuUsageInfo3.cores()[CORE_NUM*2+i-1];
599 GetSystemCpuTime(g_systemStat2[i], Hz, systemCpuTimeVec3[i-1], systemBootTimeVec3[i-1]);
600 EXPECT_EQ(cpuCoreUsageInfo3.cpu_core(), i-1);
601 EXPECT_EQ(cpuCoreUsageInfo3.prev_system_cpu_time_ms(), systemCpuTimeVec2[i-1]);
602 EXPECT_EQ(cpuCoreUsageInfo3.prev_system_boot_time_ms(), systemBootTimeVec2[i-1]);
603 EXPECT_EQ(cpuCoreUsageInfo3.system_cpu_time_ms(), systemCpuTimeVec3[i-1]);
604 EXPECT_EQ(cpuCoreUsageInfo3.system_boot_time_ms(), systemBootTimeVec3[i-1]);
605 }
606 }
607
608 /**
609 * @tc.name: cpu plugin
610 * @tc.desc: cpu system information test for abnormal parameters.
611 * @tc.type: FUNC
612 */
613 HWTEST_F(CpuDataPluginTest, TestPluginSystemBoundary, TestSize.Level1)
614 {
615 CpuData cpuData;
616 CpuDataPlugin cpuPlugin;
617 std::string sysStr;
618 int64_t Hz = cpuPlugin.GetUserHz();
619 int64_t systemCpuTime = 0;
620 int64_t systemBootTime = 0;
621 GetSystemCpuTime(g_systemStat1[0], Hz, systemCpuTime, systemBootTime);
622 // 空字符串
623 sysStr = "";
624 CpuLoadData cpuLoadData;
625 cpuPlugin.WriteSystemCpuUsage(*cpuData.mutable_cpu_usage_info(), cpuLoadData, sysStr.c_str(), sysStr.length());
626 CpuUsageInfo cpuUsageInfo1 = cpuData.cpu_usage_info();
627 EXPECT_EQ(cpuUsageInfo1.prev_system_cpu_time_ms(), 0);
628 EXPECT_EQ(cpuUsageInfo1.prev_system_boot_time_ms(), 0);
629 EXPECT_EQ(cpuUsageInfo1.system_cpu_time_ms(), 0);
630 EXPECT_EQ(cpuUsageInfo1.system_boot_time_ms(), 0);
631 ASSERT_EQ(cpuUsageInfo1.cores_size(), 0);
632
633 // 空格字符串
634 sysStr = " ";
635 cpuPlugin.WriteSystemCpuUsage(*cpuData.mutable_cpu_usage_info(), cpuLoadData, sysStr.c_str(), sysStr.length());
636 cpuUsageInfo1 = cpuData.cpu_usage_info();
637 EXPECT_EQ(cpuUsageInfo1.prev_system_cpu_time_ms(), 0);
638 EXPECT_EQ(cpuUsageInfo1.prev_system_boot_time_ms(), 0);
639 EXPECT_EQ(cpuUsageInfo1.system_cpu_time_ms(), 0);
640 EXPECT_EQ(cpuUsageInfo1.system_boot_time_ms(), 0);
641 ASSERT_EQ(cpuUsageInfo1.cores_size(), 0);
642
643 // 数据错误
644 sysStr = "1000";
645 cpuPlugin.WriteSystemCpuUsage(*cpuData.mutable_cpu_usage_info(), cpuLoadData, sysStr.c_str(), sysStr.length());
646 cpuUsageInfo1 = cpuData.cpu_usage_info();
647 EXPECT_EQ(cpuUsageInfo1.prev_system_cpu_time_ms(), 0);
648 EXPECT_EQ(cpuUsageInfo1.prev_system_boot_time_ms(), 0);
649 EXPECT_EQ(cpuUsageInfo1.system_cpu_time_ms(), 0);
650 EXPECT_EQ(cpuUsageInfo1.system_boot_time_ms(), 0);
651 ASSERT_EQ(cpuUsageInfo1.cores_size(), 0);
652
653 // 数据不够
654 sysStr = "cpu 1234567 2345678 1111111 1193291234 3546789\n"
655 "cpu0 2345678 662862 1966195 1111111 3571925\n"
656 "cpu1 3861506 2345678 1702753 199535158 1752008\n"
657 "cpu2 3549890 1111111 2345678 1234567 1133743\n"
658 "cpu3 3336646 676939 1458898 2345678 854578\n"
659 "cpu4 1111111 601107 2305309 3546789 929594\n"
660 "cpu5 3546789 658673 1234567 197791346 738811\n";
661 cpuPlugin.WriteSystemCpuUsage(*cpuData.mutable_cpu_usage_info(), cpuLoadData, sysStr.c_str(), sysStr.length());
662 cpuUsageInfo1 = cpuData.cpu_usage_info();
663 EXPECT_EQ(cpuUsageInfo1.prev_system_cpu_time_ms(), 0);
664 EXPECT_EQ(cpuUsageInfo1.prev_system_boot_time_ms(), 0);
665 EXPECT_EQ(cpuUsageInfo1.system_cpu_time_ms(), 0);
666 EXPECT_EQ(cpuUsageInfo1.system_boot_time_ms(), 0);
667 ASSERT_EQ(cpuUsageInfo1.cores_size(), 0);
668
669 // 多个空格,可以取出正确数据
670 sysStr = "cpu 1234567 2345678 1111111 1193291234 3546789 0 2345678 0 0\n"
671 "cpu0 2345678 662862 1966195 1111111 3571925 0 817371 0 0\n"
672 "cpu1 3861506 2345678 1702753 199535158 1752008 0 401639 0 0\n"
673 "cpu2 3549890 1111111 2345678 1234567 1133743 0 205972 0 0\n"
674 "cpu3 3336646 676939 1458898 2345678 854578 0 2345678 0 0\n"
675 "cpu4 1111111 601107 2305309 3546789 929594 0 1007959 0 0\n"
676 "cpu5 3546789 658673 1234567 197791346 738811 0 49496 0 0\n";
677 cpuPlugin.WriteSystemCpuUsage(*cpuData.mutable_cpu_usage_info(), cpuLoadData, sysStr.c_str(), sysStr.length());
678 cpuUsageInfo1 = cpuData.cpu_usage_info();
679 EXPECT_EQ(cpuUsageInfo1.prev_system_cpu_time_ms(), 0);
680 EXPECT_EQ(cpuUsageInfo1.prev_system_boot_time_ms(), 0);
681 EXPECT_EQ(cpuUsageInfo1.system_cpu_time_ms(), systemCpuTime);
682 EXPECT_EQ(cpuUsageInfo1.system_boot_time_ms(), systemBootTime);
683 ASSERT_EQ(cpuUsageInfo1.cores_size(), CORE_NUM);
684 std::vector<int64_t> systemCpuTimeVec1(CORE_NUM);
685 std::vector<int64_t> systemBootTimeVec1(CORE_NUM);
686 for (int i = 1; i <= CORE_NUM; i++) {
687 CpuCoreUsageInfo cpuCoreUsageInfo = cpuUsageInfo1.cores()[i - 1];
688 GetSystemCpuTime(g_systemStat1[i], Hz, systemCpuTimeVec1[i-1], systemBootTimeVec1[i-1]);
689 EXPECT_EQ(cpuCoreUsageInfo.cpu_core(), i-1);
690 EXPECT_EQ(cpuCoreUsageInfo.prev_system_cpu_time_ms(), 0);
691 EXPECT_EQ(cpuCoreUsageInfo.prev_system_boot_time_ms(), 0);
692 EXPECT_EQ(cpuCoreUsageInfo.system_cpu_time_ms(), systemCpuTimeVec1[i-1]);
693 EXPECT_EQ(cpuCoreUsageInfo.system_boot_time_ms(), systemBootTimeVec1[i-1]);
694 }
695
696 // 最后一个数据之后没有空格,可以取出正确数据
697 sysStr = "cpu 1234567 2345678 1111111 1193291234 3546789 0 2345678 0 0\n"
698 "cpu0 2345678 662862 1966195 1111111 3571925 0 817371 0 0\n"
699 "cpu1 3861506 2345678 1702753 199535158 1752008 0 401639 0 0\n"
700 "cpu2 3549890 1111111 2345678 1234567 1133743 0 205972 0 0\n"
701 "cpu3 3336646 676939 1458898 2345678 854578 0 2345678 0 0\n"
702 "cpu4 1111111 601107 2305309 3546789 929594 0 1007959 0 0\n"
703 "cpu5 3546789 658673 1234567 197791346 738811 0 49496 0 0\n";
704 cpuPlugin.WriteSystemCpuUsage(*cpuData.mutable_cpu_usage_info(), cpuLoadData, sysStr.c_str(), sysStr.length());
705 cpuUsageInfo1 = cpuData.cpu_usage_info();
706 EXPECT_EQ(cpuUsageInfo1.prev_system_cpu_time_ms(), systemCpuTime);
707 EXPECT_EQ(cpuUsageInfo1.prev_system_boot_time_ms(), systemBootTime);
708 EXPECT_EQ(cpuUsageInfo1.system_cpu_time_ms(), systemCpuTime);
709 EXPECT_EQ(cpuUsageInfo1.system_boot_time_ms(), systemBootTime);
710 ASSERT_EQ(cpuUsageInfo1.cores_size(), CORE_NUM*2);
711 std::vector<int64_t> systemCpuTimeVec2(CORE_NUM);
712 std::vector<int64_t> systemBootTimeVec2(CORE_NUM);
713 for (int i = 1; i <= CORE_NUM; i++) {
714 CpuCoreUsageInfo cpuCoreUsageInfo = cpuUsageInfo1.cores()[CORE_NUM+i-1];
715 GetSystemCpuTime(g_systemStat1[i], Hz, systemCpuTimeVec2[i-1], systemBootTimeVec2[i-1]);
716 EXPECT_EQ(cpuCoreUsageInfo.cpu_core(), i-1);
717 EXPECT_EQ(cpuCoreUsageInfo.prev_system_cpu_time_ms(), systemCpuTimeVec1[i-1]);
718 EXPECT_EQ(cpuCoreUsageInfo.prev_system_boot_time_ms(), systemBootTimeVec1[i-1]);
719 EXPECT_EQ(cpuCoreUsageInfo.system_cpu_time_ms(), systemCpuTimeVec2[i-1]);
720 EXPECT_EQ(cpuCoreUsageInfo.system_boot_time_ms(), systemBootTimeVec2[i-1]);
721 }
722 }
723
724 /**
725 * @tc.name: cpu plugin
726 * @tc.desc: a part of cpu thread information test.
727 * @tc.type: FUNC
728 */
729 HWTEST_F(CpuDataPluginTest, TestPluginThreadInfo, TestSize.Level1)
730 {
731 std::vector<std::string> threadVec1;
732 threadVec1.push_back("1209 (skytone:service) R 1746 1746 0 0 -1 1077936448 10399 0 1 0 22 8 0 0 20 0 19 0");
733 threadVec1.push_back("1646 (Jit thread pool) L 1746 1746 0 0 -1 1077936192 1831 0 0 0 2 1 0 0 20 0 19 0");
734 threadVec1.push_back("1654 (Signal Catcher) T 1746 1746 0 0 -1 1077936192 588 0 0 0 1 0 0 0 20 0 19 0");
735 threadVec1.push_back("1655 (HeapTaskDaemon) S 1746 1746 0 0 -1 1077936192 1515 0 0 0 3 0 0 0 24 4 19 0");
736 threadVec1.push_back("1656 (ReferenceQueueD) D 1746 1746 0 0 -1 1077936192 24 0 0 0 0 0 0 0 24 4 19 0");
737
738 std::vector<std::string> threadVec2;
739 threadVec2.push_back("1662 (FinalizerDaemon) T 1746 1746 0 0 -1 1077936192 18 0 0 0 0 0 0 0 24 4 19 0");
740 threadVec2.push_back("1663 (FinalizerWatchd) R 1746 1746 0 0 -1 1077936192 5 0 0 0 0 0 0 0 24 4 19 0");
741 threadVec2.push_back("1666 (Binder:1209_1) R 1746 1746 0 0 -1 1077936192 412 0 0 0 0 0 0 0 20 0 19 0");
742 threadVec2.push_back("1679 (Binder:1209_2) O 1746 1746 0 0 -1 1077936192 14 0 0 0 0 0 0 0 20 0 19 0");
743 threadVec2.push_back("1680 (Binder:1209_3) D 1746 1746 0 0 -1 1077936192 11513 0 4 0 54 8 0 0 20 0 49 0");
744 threadVec2.push_back("1681 (Binder:1209_4) S 1751 1751 0 0 -1 1077936192 17 0 0 0 0 0 0 0 20 0 24 0");
745 CpuData cpuData;
746 CpuDataPlugin cpuPlugin;
747 int64_t Hz = cpuPlugin.GetUserHz();
748
749 // 存入threadVec0-4
750 int32_t tid;
751 for (int i = 0; i < FIRST_THREAD_NUM; i++) {
752 auto* threadInfo = cpuData.add_thread_info();
753 tid = atoi(threadVec1[i].substr(0, threadVec1[i].find(" ")).c_str());
754 cpuPlugin.WriteThread(*threadInfo, threadVec1[i].c_str(), threadVec1[i].length(), tid);
755 }
756 ASSERT_EQ(cpuData.thread_info_size(), FIRST_THREAD_NUM);
757 int64_t threadCpuTime1;
758 for (int i = 0; i < FIRST_THREAD_NUM; i++) {
759 threadCpuTime1 = (g_tidStat1[i].stat.utime + g_tidStat1[i].stat.stime) * Hz;
760 ThreadInfo threadInfo1 = cpuData.thread_info()[i];
761 EXPECT_EQ(threadInfo1.tid(), g_tidStat1[i].tid);
762 EXPECT_STREQ(threadInfo1.thread_name().c_str(), g_tidStat1[i].name.c_str());
763 EXPECT_EQ(threadInfo1.thread_state(), g_tidStat1[i].state);
764 EXPECT_EQ(threadInfo1.prev_thread_cpu_time_ms(), 0);
765 EXPECT_EQ(threadInfo1.thread_cpu_time_ms(), threadCpuTime1);
766 }
767
768 // 存入threadVec5-10
769 for (int i = 0; i < SECOND_THREAD_NUM; i++) {
770 auto* threadInfo = cpuData.add_thread_info();
771 tid = atoi(threadVec2[i].substr(0, threadVec2[i].find(" ")).c_str());
772 cpuPlugin.WriteThread(*threadInfo, threadVec2[i].c_str(), threadVec2[i].length(), tid);
773 }
774 ASSERT_EQ(cpuData.thread_info_size(), FIRST_THREAD_NUM+SECOND_THREAD_NUM);
775 std::vector<int64_t> threadCpuTime2;
776 for (int i = 0; i < SECOND_THREAD_NUM; i++) {
777 threadCpuTime2.push_back((g_tidStat2[i].stat.utime + g_tidStat2[i].stat.stime) * Hz);
778 ThreadInfo threadInfo2 = cpuData.thread_info()[i+FIRST_THREAD_NUM];
779 EXPECT_EQ(threadInfo2.tid(), g_tidStat2[i].tid);
780 EXPECT_STREQ(threadInfo2.thread_name().c_str(), g_tidStat2[i].name.c_str());
781 EXPECT_EQ(threadInfo2.thread_state(), g_tidStat2[i].state);
782 EXPECT_EQ(threadInfo2.prev_thread_cpu_time_ms(), 0);
783 EXPECT_EQ(threadInfo2.thread_cpu_time_ms(), threadCpuTime2[i]);
784 }
785
786 // 重复存入threadVec5-10
787 for (int i = 0; i < SECOND_THREAD_NUM; i++) {
788 auto* threadInfo = cpuData.add_thread_info();
789 tid = atoi(threadVec2[i].substr(0, threadVec2[i].find(" ")).c_str());
790 cpuPlugin.WriteThread(*threadInfo, threadVec2[i].c_str(), threadVec2[i].length(), tid);
791 }
792
793 ASSERT_EQ(cpuData.thread_info_size(), FIRST_THREAD_NUM+SECOND_THREAD_NUM*2);
794 int64_t threadCpuTime3;
795 for (int i = 0; i < SECOND_THREAD_NUM; i++) {
796 threadCpuTime3 = (g_tidStat2[i].stat.utime + g_tidStat2[i].stat.stime) * Hz;
797 ThreadInfo threadInfo3 = cpuData.thread_info()[i+FIRST_THREAD_NUM+SECOND_THREAD_NUM];
798 EXPECT_EQ(threadInfo3.tid(), g_tidStat2[i].tid);
799 EXPECT_STREQ(threadInfo3.thread_name().c_str(), g_tidStat2[i].name.c_str());
800 EXPECT_EQ(threadInfo3.thread_state(), g_tidStat2[i].state);
801 EXPECT_EQ(threadInfo3.prev_thread_cpu_time_ms(), threadCpuTime2[i]);
802 EXPECT_EQ(threadInfo3.thread_cpu_time_ms(), threadCpuTime3);
803 }
804 }
805
806 /**
807 * @tc.name: cpu plugin
808 * @tc.desc: cpu thread information test for abnormal parameters.
809 * @tc.type: FUNC
810 */
811 HWTEST_F(CpuDataPluginTest, TestPluginThreadBoundary, TestSize.Level1)
812 {
813 CpuData cpuData;
814 CpuDataPlugin cpuPlugin;
815 std::string threadStr;
816 int thread_info_count = 0;
817 int64_t Hz = cpuPlugin.GetUserHz();
818 int64_t threadCpuTime = (g_tidStat1[0].stat.utime + g_tidStat1[0].stat.stime) * Hz;
819
820 // 空字符串
821 threadStr = "";
822 auto* threadInfo = cpuData.add_thread_info();
823 thread_info_count++;
824 cpuPlugin.WriteThread(*threadInfo, threadStr.c_str(), threadStr.length(), 0);
825 ASSERT_EQ(cpuData.thread_info_size(), thread_info_count);
826
827 // 空格字符串
828 threadStr = " ";
829 threadInfo = cpuData.add_thread_info();
830 thread_info_count++;
831 cpuPlugin.WriteThread(*threadInfo, threadStr.c_str(), threadStr.length(), 0);
832 ASSERT_EQ(cpuData.thread_info_size(), thread_info_count);
833
834 // 数据错误
835 threadStr = "1000";
836 threadInfo = cpuData.add_thread_info();
837 thread_info_count++;
838 cpuPlugin.WriteThread(*threadInfo, threadStr.c_str(), threadStr.length(), 0);
839 ASSERT_EQ(cpuData.thread_info_size(), thread_info_count);
840
841 // 数据不够
842 threadStr = "1209 (skytone:service) R 1746 1746 0 0 -1 1077936448 10399 0 1 0 22 8";
843 threadInfo = cpuData.add_thread_info();
844 thread_info_count++;
845 cpuPlugin.WriteThread(*threadInfo, threadStr.c_str(), threadStr.length(), 0);
846 ASSERT_EQ(cpuData.thread_info_size(), thread_info_count);
847
848 // 线程名缺失左括号
849 threadStr = "1209 skytone:service) R 1746 1746 0 0 -1 1077936448 10399 0 1 0 22 8 0 0 20 0 19 0";
850 int32_t tid = atoi(threadStr.substr(0, threadStr.find(" ")).c_str());
851 threadInfo = cpuData.add_thread_info();
852 thread_info_count++;
853 cpuPlugin.WriteThread(*threadInfo, threadStr.c_str(), threadStr.length(), tid);
854 ASSERT_EQ(cpuData.thread_info_size(), thread_info_count);
855 ThreadInfo threadInfo1 = cpuData.thread_info()[thread_info_count-1];
856 EXPECT_EQ(threadInfo1.tid(), g_tidStat1[0].tid);
857 EXPECT_STRNE(threadInfo1.thread_name().c_str(), g_tidStat1[0].name.c_str());
858 EXPECT_EQ(threadInfo1.thread_state(), g_tidStat1[0].state);
859 EXPECT_EQ(threadInfo1.prev_thread_cpu_time_ms(), 0);
860 EXPECT_EQ(threadInfo1.thread_cpu_time_ms(), threadCpuTime);
861
862 // 多个空格,可以取出正确数据
863 threadStr = "1209 (skytone:service) R 1746 1746 0 0 -1 1077936448 10399 0 1 0 22 8 0 0 20 0 19 0";
864 tid = atoi(threadStr.substr(0, threadStr.find(" ")).c_str());
865 threadInfo = cpuData.add_thread_info();
866 thread_info_count++;
867 cpuPlugin.WriteThread(*threadInfo, threadStr.c_str(), threadStr.length(), tid);
868 ASSERT_EQ(cpuData.thread_info_size(), thread_info_count);
869 threadInfo1 = cpuData.thread_info()[thread_info_count-1];
870 EXPECT_EQ(threadInfo1.tid(), g_tidStat1[0].tid);
871 EXPECT_STREQ(threadInfo1.thread_name().c_str(), g_tidStat1[0].name.c_str());
872 EXPECT_EQ(threadInfo1.thread_state(), g_tidStat1[0].state);
873 EXPECT_EQ(threadInfo1.prev_thread_cpu_time_ms(), threadCpuTime);
874 EXPECT_EQ(threadInfo1.thread_cpu_time_ms(), threadCpuTime);
875
876 // 最后一个数据之后没有空格,可以取出正确数据
877 threadStr = "1209 (skytone:service) R 1746 1746 0 0 -1 1077936448 10399 0 1 0 22 8 0 0 20";
878 tid = atoi(threadStr.substr(0, threadStr.find(" ")).c_str());
879 threadInfo = cpuData.add_thread_info();
880 thread_info_count++;
881 cpuPlugin.WriteThread(*threadInfo, threadStr.c_str(), threadStr.length(), tid);
882 ASSERT_EQ(cpuData.thread_info_size(), thread_info_count);
883 threadInfo1 = cpuData.thread_info()[thread_info_count-1];
884 EXPECT_EQ(threadInfo1.tid(), g_tidStat1[0].tid);
885 EXPECT_STREQ(threadInfo1.thread_name().c_str(), g_tidStat1[0].name.c_str());
886 EXPECT_EQ(threadInfo1.thread_state(), g_tidStat1[0].state);
887 EXPECT_EQ(threadInfo1.prev_thread_cpu_time_ms(), threadCpuTime);
888 EXPECT_EQ(threadInfo1.thread_cpu_time_ms(), threadCpuTime);
889 }
890
ExecuteBin(const std::string & bin,const std::vector<std::string> & args)891 bool ExecuteBin(const std::string& bin, const std::vector<std::string>& args)
892 {
893 std::vector<char*> argv;
894 for (size_t i = 0; i < args.size(); i++) {
895 argv.push_back(const_cast<char*>(args[i].c_str()));
896 }
897 argv.push_back(nullptr); // last item in argv must be NULL
898
899 int retval = execvp(bin.c_str(), argv.data());
900 CHECK_TRUE(retval != -1, false, "execv %s failed, %d!", bin.c_str(), errno);
901 _exit(EXIT_FAILURE);
902 abort(); // never should be here.
903 return true;
904 }
905
906 /**
907 * @tc.name: cpu plugin
908 * @tc.desc: test ParseCpuInfo for pid
909 * @tc.type: FUNC
910 */
911 HWTEST_F(CpuDataPluginTest, TestPid, TestSize.Level1)
912 {
913 CpuDataPlugin plugin1, plugin2;
914 CpuData cpuData1, cpuData2;
915
916 std::string cmd = "chmod 777 " + DEFAULT_BIN_PATH;
917 system(cmd.c_str());
918
919 pid_t pid1 = fork();
920 if (pid1 == 0) {
921 // set 0, not to do
922 std::vector<std::string> argv = {"childpidtest1", "0"};
923 ASSERT_TRUE(ExecuteBin(DEFAULT_BIN_PATH, argv));
924 }
925 pid_t pid2 = fork();
926 if (pid2 == 0) {
927 // set 1, consume cpu
928 std::vector<std::string> argv = {"childpidtest2", "1"};
929 ASSERT_TRUE(ExecuteBin(DEFAULT_BIN_PATH, argv));
930 }
931
932 #ifdef COVERAGE_TEST
933 const int coverageSleepTime = 5; // sleep 5s
934 sleep(coverageSleepTime);
935 #else
936 sleep(1); // 睡眠1s,确保pid1进入睡眠状态,pid2进入while循环
937 #endif // COVERAGE_TEST
938 EXPECT_TRUE(PluginCpuinfoStub(plugin1, cpuData1, static_cast<int>(pid1), false, false));
939 EXPECT_TRUE(PluginCpuinfoStub(plugin2, cpuData2, static_cast<int>(pid2), false, false));
940 #ifdef __aarch64__
941 EXPECT_LT(cpuData1.cpu_usage_info().process_cpu_time_ms(), cpuData2.cpu_usage_info().process_cpu_time_ms());
942 #endif
943
944 while (waitpid(-1, nullptr, WNOHANG) == 0) {
945 kill(pid1, SIGKILL);
946 kill(pid2, SIGKILL);
947 }
948
949 plugin1.Stop();
950 plugin2.Stop();
951 }
952 } // namespace
953