• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(g_path);
285     g_path = g_testPath + "/proc/";
286     cpuPlugin.SetPath(g_path);
287     EXPECT_TRUE(PluginCpuinfoStub(cpuPlugin, cpuData, 1872, true, false));
288 
289     TestCpuUsage cpuUsage;
290     int64_t systemCpuTime = 0;
291     int64_t systemBootTime = 0;
292     int64_t Hz = cpuPlugin.GetUserHz();
293     int64_t processCpuTime = (g_pidStat.utime + g_pidStat.stime) * Hz;
294     GetSystemCpuTime(g_systemStat[0], Hz, systemCpuTime, systemBootTime);
295     GetCpuUsage(cpuUsage, Hz);
296 
297     CpuUsageInfo cpuUsageInfo = cpuData.cpu_usage_info();
298     EXPECT_EQ(cpuUsageInfo.prev_process_cpu_time_ms(), 0);
299     EXPECT_EQ(cpuUsageInfo.prev_system_cpu_time_ms(), 0);
300     EXPECT_EQ(cpuUsageInfo.prev_system_boot_time_ms(), 0);
301     EXPECT_EQ(cpuUsageInfo.process_cpu_time_ms(), processCpuTime);
302     EXPECT_EQ(cpuUsageInfo.system_cpu_time_ms(), systemCpuTime);
303     EXPECT_EQ(cpuUsageInfo.system_boot_time_ms(), systemBootTime);
304     EXPECT_EQ(cpuData.process_num(), 1);
305     EXPECT_FLOAT_EQ(cpuData.user_load(), cpuUsage.userLoad);
306     EXPECT_FLOAT_EQ(cpuData.sys_load(), cpuUsage.sysLoad);
307     EXPECT_FLOAT_EQ(cpuData.total_load(), cpuUsage.totalLoad);
308 
309     ASSERT_EQ(cpuUsageInfo.cores_size(), 6);
310     for (int i = 1; i <= CORE_NUM; i++) {
311         CpuCoreUsageInfo cpuCoreUsageInfo = cpuUsageInfo.cores()[i - 1];
312         GetSystemCpuTime(g_systemStat[i], Hz, systemCpuTime, systemBootTime);
313         EXPECT_EQ(cpuCoreUsageInfo.cpu_core(), g_systemStat[i].core);
314         EXPECT_EQ(cpuCoreUsageInfo.prev_system_cpu_time_ms(), 0);
315         EXPECT_EQ(cpuCoreUsageInfo.prev_system_boot_time_ms(), 0);
316         EXPECT_EQ(cpuCoreUsageInfo.system_cpu_time_ms(), systemCpuTime);
317         EXPECT_EQ(cpuCoreUsageInfo.system_boot_time_ms(), systemBootTime);
318 
319         EXPECT_EQ(cpuCoreUsageInfo.frequency().min_frequency_khz(), g_freq[i - 1].minFreq);
320         EXPECT_EQ(cpuCoreUsageInfo.frequency().max_frequency_khz(), g_freq[i - 1].maxFreq);
321         EXPECT_EQ(cpuCoreUsageInfo.frequency().cur_frequency_khz(), g_freq[i - 1].curFreq);
322         if (i == 1) { // cpu0为大核
323             EXPECT_EQ(cpuCoreUsageInfo.is_little_core(), false);
324         } else {
325             EXPECT_EQ(cpuCoreUsageInfo.is_little_core(), true);
326         }
327     }
328 }
329 
330 /**
331  * @tc.name: cpu plugin
332  * @tc.desc: cpu information test for specific pid.
333  * @tc.type: FUNC
334  */
335 HWTEST_F(CpuDataPluginTest, TestPlugin, TestSize.Level1)
336 {
337     CpuDataPlugin cpuPlugin;
338     CpuData cpuData;
339     g_path = g_testPath;
340     cpuPlugin.SetFreqPath(g_path);
341     g_path = g_testPath + "/proc/";
342     cpuPlugin.SetPath(g_path);
343     EXPECT_TRUE(PluginCpuinfoStub(cpuPlugin, cpuData, 1872, false, false));
344 
345     int64_t Hz = cpuPlugin.GetUserHz();
346     int64_t threadCpuTime;
347     ASSERT_EQ(cpuData.thread_info_size(), 7);
348     for (int i = 0; i < THREAD_NUM && i < cpuData.thread_info().size(); i++) {
349         threadCpuTime = (g_tidStat[i].stat.utime + g_tidStat[i].stat.stime) * Hz;
350         ThreadInfo threadInfo = cpuData.thread_info()[i];
351         EXPECT_EQ(threadInfo.tid(), g_tidStat[i].tid);
352         EXPECT_STREQ(threadInfo.thread_name().c_str(), g_tidStat[i].name.c_str());
353         EXPECT_EQ(threadInfo.thread_state(), g_tidStat[i].state);
354         EXPECT_EQ(threadInfo.thread_cpu_time_ms(), threadCpuTime);
355         EXPECT_EQ(threadInfo.prev_thread_cpu_time_ms(), 0);
356     }
357 
358     EXPECT_EQ(cpuPlugin.Stop(), 0);
359 
360     // 缓冲区异常
361     EXPECT_FALSE(PluginCpuinfoStub(cpuPlugin, cpuData, 1872, false, true));
362     EXPECT_EQ(cpuPlugin.Stop(), 0);
363 }
364 
365 /**
366  * @tc.name: cpu plugin
367  * @tc.desc: cpu information test for unusual path.
368  * @tc.type: FUNC
369  */
370 HWTEST_F(CpuDataPluginTest, TestPluginBoundary, TestSize.Level1)
371 {
372     CpuDataPlugin cpuPlugin;
373     CpuData cpuData;
374     g_path = g_testPath + "/proc/";
375     cpuPlugin.SetPath(g_path);
376     EXPECT_FALSE(PluginCpuinfoStub(cpuPlugin, cpuData, -1, false, false));
377     EXPECT_FALSE(PluginCpuinfoStub(cpuPlugin, cpuData, 12345, false, false));
378 
379     CpuDataPlugin cpuPlugin2;
380     cpuPlugin2.SetPath("123");
381     EXPECT_FALSE(PluginCpuinfoStub(cpuPlugin2, cpuData, 1872, false, false));
382     EXPECT_FALSE(PluginCpuinfoStub(cpuPlugin2, cpuData, -1, false, false));
383     EXPECT_FALSE(PluginCpuinfoStub(cpuPlugin2, cpuData, 12345, false, false));
384 }
385 
386 /**
387  * @tc.name: cpu plugin
388  * @tc.desc: cpu plugin registration test.
389  * @tc.type: FUNC
390  */
391 HWTEST_F(CpuDataPluginTest, TestPluginRegister, TestSize.Level1)
392 {
393     void* handle = dlopen(SO_PATH.c_str(), RTLD_LAZY);
394     ASSERT_NE(handle, nullptr);
395     PluginModuleStruct* cpuPlugin = (PluginModuleStruct*)dlsym(handle, "g_pluginModule");
396     ASSERT_NE(cpuPlugin, nullptr);
397     EXPECT_STREQ(cpuPlugin->name, "cpu-plugin");
398     EXPECT_EQ(cpuPlugin->resultBufferSizeHint, BUF_SIZE);
399 
400     // Serialize config
401     CpuConfig protoConfig;
402     protoConfig.set_pid(TEST_PID);
403     int configLength = protoConfig.ByteSizeLong();
404     ASSERT_GT(configLength, 0);
405     std::vector<uint8_t> configBuffer(configLength);
406     EXPECT_TRUE(protoConfig.SerializeToArray(configBuffer.data(), configLength));
407 
408     // run plugin
409     std::vector<uint8_t> dataBuffer(cpuPlugin->resultBufferSizeHint);
410     EXPECT_EQ(cpuPlugin->callbacks->onPluginSessionStart(configBuffer.data(), configLength), RET_SUCC);
411     ASSERT_GT(cpuPlugin->callbacks->onPluginReportResult(dataBuffer.data(), cpuPlugin->resultBufferSizeHint), 0);
412     EXPECT_EQ(cpuPlugin->callbacks->onPluginSessionStop(), RET_SUCC);
413 
414     // 反序列化失败导致的start失败
415     EXPECT_EQ(cpuPlugin->callbacks->onPluginSessionStart(configBuffer.data(), configLength+1), RET_FAIL);
416 }
417 
418 /**
419  * @tc.name: cpu plugin
420  * @tc.desc: a part of cpu process information test.
421  * @tc.type: FUNC
422  */
423 HWTEST_F(CpuDataPluginTest, TestPluginProcessInfo, TestSize.Level1)
424 {
425     CpuData cpuData;
426     CpuDataPlugin cpuPlugin;
427     int64_t Hz = cpuPlugin.GetUserHz();
428     std::string proStr1 = "2000 (ibus-x11) S 1 1865 1780 1025 1780 4194304 3233 0 457 0 50 10 40 10 20 0 7";
429     std::string proStr2 = "2000 (ibus-x12) R 1 1865 1780 1025 1780 4194304 3233 0 457 0 70 0 10 20 20 0 7";
430     int64_t processCpuTime1 = (g_pidStat1.utime + g_pidStat1.stime) * Hz;
431     int64_t processCpuTime2 = (g_pidStat2.utime + g_pidStat2.stime) * Hz;
432 
433     // 存入proStr1
434     auto* cpuUsageInfo = cpuData.mutable_cpu_usage_info();
435     cpuPlugin.WriteProcessCpuUsage(*cpuUsageInfo, proStr1.c_str(), proStr1.length());
436     CpuUsageInfo cpuUsageInfo1 = cpuData.cpu_usage_info();
437     EXPECT_EQ(cpuUsageInfo1.prev_process_cpu_time_ms(), 0);
438     EXPECT_EQ(cpuUsageInfo1.process_cpu_time_ms(), processCpuTime1);
439 
440     // 存入proStr2
441     cpuUsageInfo = cpuData.mutable_cpu_usage_info();
442     cpuPlugin.WriteProcessCpuUsage(*cpuUsageInfo, proStr2.c_str(), proStr2.length());
443     CpuUsageInfo cpuUsageInfo2 = cpuData.cpu_usage_info();
444     EXPECT_EQ(cpuUsageInfo2.prev_process_cpu_time_ms(), processCpuTime1);
445     EXPECT_EQ(cpuUsageInfo2.process_cpu_time_ms(), processCpuTime2);
446 
447     // 重复存入proStr2
448     cpuUsageInfo = cpuData.mutable_cpu_usage_info();
449     cpuPlugin.WriteProcessCpuUsage(*cpuUsageInfo, proStr2.c_str(), proStr2.length());
450     CpuUsageInfo cpuUsageInfo3 = cpuData.cpu_usage_info();
451     EXPECT_EQ(cpuUsageInfo3.prev_process_cpu_time_ms(), processCpuTime2);
452     EXPECT_EQ(cpuUsageInfo3.process_cpu_time_ms(), processCpuTime2);
453 }
454 
455 /**
456  * @tc.name: cpu plugin
457  * @tc.desc: cpu process information test for abnormal parameters.
458  * @tc.type: FUNC
459  */
460 HWTEST_F(CpuDataPluginTest, TestPluginProcessBoundary, TestSize.Level1)
461 {
462     CpuData cpuData;
463     CpuDataPlugin cpuPlugin;
464     std::string proStr;
465     int64_t Hz = cpuPlugin.GetUserHz();
466     int64_t processCpuTime = (g_pidStat1.utime + g_pidStat1.stime) * Hz;
467 
468     // 空字符串
469     proStr = "";
470     auto* cpuUsageInfo = cpuData.mutable_cpu_usage_info();
471     cpuPlugin.WriteProcessCpuUsage(*cpuUsageInfo, proStr.c_str(), proStr.length());
472     CpuUsageInfo cpuUsageInfo1 = cpuData.cpu_usage_info();
473     EXPECT_EQ(cpuUsageInfo1.prev_process_cpu_time_ms(), 0);
474     EXPECT_EQ(cpuUsageInfo1.process_cpu_time_ms(), 0);
475 
476     // 空格字符串
477     proStr = " ";
478     cpuUsageInfo = cpuData.mutable_cpu_usage_info();
479     cpuPlugin.WriteProcessCpuUsage(*cpuUsageInfo, proStr.c_str(), proStr.length());
480     cpuUsageInfo1 = cpuData.cpu_usage_info();
481     EXPECT_EQ(cpuUsageInfo1.prev_process_cpu_time_ms(), 0);
482     EXPECT_EQ(cpuUsageInfo1.process_cpu_time_ms(), 0);
483 
484     // 数据错误
485     proStr = "2000";
486     cpuUsageInfo = cpuData.mutable_cpu_usage_info();
487     cpuPlugin.WriteProcessCpuUsage(*cpuUsageInfo, proStr.c_str(), proStr.length());
488     cpuUsageInfo1 = cpuData.cpu_usage_info();
489     EXPECT_EQ(cpuUsageInfo1.prev_process_cpu_time_ms(), 0);
490     EXPECT_EQ(cpuUsageInfo1.process_cpu_time_ms(), 0);
491 
492     // 数据不够("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)
493     proStr = "2000 (ibus-x11) S 1 1865 1780 1025 1780 4194304 3233 0 457 0 50 10";
494     cpuUsageInfo = cpuData.mutable_cpu_usage_info();
495     cpuPlugin.WriteProcessCpuUsage(*cpuUsageInfo, proStr.c_str(), proStr.length());
496     cpuUsageInfo1 = cpuData.cpu_usage_info();
497     EXPECT_EQ(cpuUsageInfo1.prev_process_cpu_time_ms(), 0);
498     EXPECT_EQ(cpuUsageInfo1.process_cpu_time_ms(), 0);
499 
500     // 多个空格,可以取出正确数据
501     proStr = "2000 (ibus-x11) S 1 1865 1780 1025 1780 4194304 3233 0 457    0 50   10 40 10 20     ";
502     cpuUsageInfo = cpuData.mutable_cpu_usage_info();
503     cpuPlugin.WriteProcessCpuUsage(*cpuUsageInfo, proStr.c_str(), proStr.length());
504     cpuUsageInfo1 = cpuData.cpu_usage_info();
505     EXPECT_EQ(cpuUsageInfo1.prev_process_cpu_time_ms(), 0);
506     EXPECT_EQ(cpuUsageInfo1.process_cpu_time_ms(), processCpuTime);
507 
508     // 最后一个数据之后没有空格,可以取出正确数据
509     proStr = "2000 (ibus-x11) S 1 1865 1780 1025 1780 4194304 3233 0 457 0 50 10 40 10 20";
510     cpuUsageInfo = cpuData.mutable_cpu_usage_info();
511     cpuPlugin.WriteProcessCpuUsage(*cpuUsageInfo, proStr.c_str(), proStr.length());
512     cpuUsageInfo1 = cpuData.cpu_usage_info();
513     EXPECT_EQ(cpuUsageInfo1.prev_process_cpu_time_ms(), processCpuTime);
514     EXPECT_EQ(cpuUsageInfo1.process_cpu_time_ms(), processCpuTime);
515 }
516 
517 /**
518  * @tc.name: cpu plugin
519  * @tc.desc: a part of cpu system information test.
520  * @tc.type: FUNC
521  */
522 HWTEST_F(CpuDataPluginTest, TestPluginSystemInfo, TestSize.Level1)
523 {
524     std::string sysStr1 = "cpu  1234567 2345678 1111111 1193291234 3546789 0 2345678 0 0\n"
525                           "cpu0 2345678 662862 1966195 1111111 3571925 0 817371 0 0\n"
526                           "cpu1 3861506 2345678 1702753 199535158 1752008 0 401639 0 0\n"
527                           "cpu2 3549890 1111111 2345678 1234567 1133743 0 205972 0 0\n"
528                           "cpu3 3336646 676939 1458898 2345678 854578 0 2345678 0 0\n"
529                           "cpu4 1111111 601107 2305309 3546789 929594 0 1007959 0 0\n"
530                           "cpu5 3546789 658673 1234567 197791346 738811 0 49496 0 0\n";
531     std::string sysStr2 = "cpu  3546789 2345678 1111111 1193291234 3546789 0 2345678 0 0\n"
532                           "cpu0 3546789 662862 1966195 2345678 3571925 0 817371 0 0\n"
533                           "cpu1 3861506 1111111 1702753 199535158 1752008 0 401639 0 0\n"
534                           "cpu2 3549890 2345678 2345678 3546789 1111111 0 205972 0 0\n"
535                           "cpu3 3336646 676939 1458898 2345678 854578 0 2345678 0 0\n"
536                           "cpu4 2345678 601107 2305309 3546789 929594 0 1111111 0 0\n"
537                           "cpu5 3546789 658673 3546789 197791346 738811 0 49496 0 0\n";
538     CpuData cpuData;
539     CpuDataPlugin cpuPlugin;
540     int64_t Hz = cpuPlugin.GetUserHz();
541 
542     // 存入sysStr1
543     int64_t systemCpuTime1 = 0;
544     int64_t systemBootTime1 = 0;
545     CpuLoadData cpuLoadData;
546     GetSystemCpuTime(g_systemStat1[0], Hz, systemCpuTime1, systemBootTime1);
547     cpuPlugin.WriteSystemCpuUsage(*cpuData.mutable_cpu_usage_info(), cpuLoadData, sysStr1.c_str(), sysStr1.length());
548     CpuUsageInfo cpuUsageInfo1 = cpuData.cpu_usage_info();
549     EXPECT_EQ(cpuUsageInfo1.prev_system_cpu_time_ms(), 0);
550     EXPECT_EQ(cpuUsageInfo1.prev_system_boot_time_ms(), 0);
551     EXPECT_EQ(cpuUsageInfo1.system_cpu_time_ms(), systemCpuTime1);
552     EXPECT_EQ(cpuUsageInfo1.system_boot_time_ms(), systemBootTime1);
553     ASSERT_EQ(cpuUsageInfo1.cores_size(), CORE_NUM);
554     std::vector<int64_t> systemCpuTimeVec1(CORE_NUM);
555     std::vector<int64_t> systemBootTimeVec1(CORE_NUM);
556     for (int i = 1; i <= CORE_NUM; i++) {
557         CpuCoreUsageInfo cpuCoreUsageInfo1 = cpuUsageInfo1.cores()[i - 1];
558         GetSystemCpuTime(g_systemStat1[i], Hz, systemCpuTimeVec1[i-1], systemBootTimeVec1[i-1]);
559         EXPECT_EQ(cpuCoreUsageInfo1.cpu_core(), i-1);
560         EXPECT_EQ(cpuCoreUsageInfo1.prev_system_cpu_time_ms(), 0);
561         EXPECT_EQ(cpuCoreUsageInfo1.prev_system_boot_time_ms(), 0);
562         EXPECT_EQ(cpuCoreUsageInfo1.system_cpu_time_ms(), systemCpuTimeVec1[i-1]);
563         EXPECT_EQ(cpuCoreUsageInfo1.system_boot_time_ms(), systemBootTimeVec1[i-1]);
564     }
565 
566     // 存入sysStr2
567     int64_t systemCpuTime2 = 0;
568     int64_t systemBootTime2 = 0;
569     GetSystemCpuTime(g_systemStat2[0], Hz, systemCpuTime2, systemBootTime2);
570     cpuPlugin.WriteSystemCpuUsage(*cpuData.mutable_cpu_usage_info(), cpuLoadData, sysStr2.c_str(), sysStr2.length());
571     CpuUsageInfo cpuUsageInfo2 = cpuData.cpu_usage_info();
572     EXPECT_EQ(cpuUsageInfo2.prev_system_cpu_time_ms(), systemCpuTime1);
573     EXPECT_EQ(cpuUsageInfo2.prev_system_boot_time_ms(), systemBootTime1);
574     EXPECT_EQ(cpuUsageInfo2.system_cpu_time_ms(), systemCpuTime2);
575     EXPECT_EQ(cpuUsageInfo2.system_boot_time_ms(), systemBootTime2);
576     ASSERT_EQ(cpuUsageInfo2.cores_size(), CORE_NUM*2);
577     std::vector<int64_t> systemCpuTimeVec2(CORE_NUM);
578     std::vector<int64_t> systemBootTimeVec2(CORE_NUM);
579     for (int i = 1; i <= CORE_NUM; i++) {
580         CpuCoreUsageInfo cpuCoreUsageInfo2 = cpuUsageInfo2.cores()[CORE_NUM+i-1];
581         GetSystemCpuTime(g_systemStat2[i], Hz, systemCpuTimeVec2[i-1], systemBootTimeVec2[i-1]);
582         EXPECT_EQ(cpuCoreUsageInfo2.cpu_core(), i-1);
583         EXPECT_EQ(cpuCoreUsageInfo2.prev_system_cpu_time_ms(), systemCpuTimeVec1[i-1]);
584         EXPECT_EQ(cpuCoreUsageInfo2.prev_system_boot_time_ms(), systemBootTimeVec1[i-1]);
585         EXPECT_EQ(cpuCoreUsageInfo2.system_cpu_time_ms(), systemCpuTimeVec2[i-1]);
586         EXPECT_EQ(cpuCoreUsageInfo2.system_boot_time_ms(), systemBootTimeVec2[i-1]);
587     }
588 
589     // 重复存入sysStr2
590     cpuPlugin.WriteSystemCpuUsage(*cpuData.mutable_cpu_usage_info(), cpuLoadData, sysStr2.c_str(), sysStr2.length());
591     CpuUsageInfo cpuUsageInfo3 = cpuData.cpu_usage_info();
592     EXPECT_EQ(cpuUsageInfo3.prev_system_cpu_time_ms(), systemCpuTime2);
593     EXPECT_EQ(cpuUsageInfo3.prev_system_boot_time_ms(), systemBootTime2);
594     EXPECT_EQ(cpuUsageInfo3.system_cpu_time_ms(), systemCpuTime2);
595     EXPECT_EQ(cpuUsageInfo3.system_boot_time_ms(), systemBootTime2);
596     ASSERT_EQ(cpuUsageInfo3.cores_size(), CORE_NUM*3);
597     std::vector<int64_t> systemCpuTimeVec3(CORE_NUM);
598     std::vector<int64_t> systemBootTimeVec3(CORE_NUM);
599     for (int i = 1; i <= CORE_NUM; i++) {
600         CpuCoreUsageInfo cpuCoreUsageInfo3 = cpuUsageInfo3.cores()[CORE_NUM*2+i-1];
601         GetSystemCpuTime(g_systemStat2[i], Hz, systemCpuTimeVec3[i-1], systemBootTimeVec3[i-1]);
602         EXPECT_EQ(cpuCoreUsageInfo3.cpu_core(), i-1);
603         EXPECT_EQ(cpuCoreUsageInfo3.prev_system_cpu_time_ms(), systemCpuTimeVec2[i-1]);
604         EXPECT_EQ(cpuCoreUsageInfo3.prev_system_boot_time_ms(), systemBootTimeVec2[i-1]);
605         EXPECT_EQ(cpuCoreUsageInfo3.system_cpu_time_ms(), systemCpuTimeVec3[i-1]);
606         EXPECT_EQ(cpuCoreUsageInfo3.system_boot_time_ms(), systemBootTimeVec3[i-1]);
607     }
608 }
609 
610 /**
611  * @tc.name: cpu plugin
612  * @tc.desc: cpu system information test for abnormal parameters.
613  * @tc.type: FUNC
614  */
615 HWTEST_F(CpuDataPluginTest, TestPluginSystemBoundary, TestSize.Level1)
616 {
617     CpuData cpuData;
618     CpuDataPlugin cpuPlugin;
619     std::string sysStr;
620     int64_t Hz = cpuPlugin.GetUserHz();
621     int64_t systemCpuTime = 0;
622     int64_t systemBootTime = 0;
623     GetSystemCpuTime(g_systemStat1[0], Hz, systemCpuTime, systemBootTime);
624     // 空字符串
625     sysStr = "";
626     CpuLoadData cpuLoadData;
627     cpuPlugin.WriteSystemCpuUsage(*cpuData.mutable_cpu_usage_info(), cpuLoadData, sysStr.c_str(), sysStr.length());
628     CpuUsageInfo cpuUsageInfo1 = cpuData.cpu_usage_info();
629     EXPECT_EQ(cpuUsageInfo1.prev_system_cpu_time_ms(), 0);
630     EXPECT_EQ(cpuUsageInfo1.prev_system_boot_time_ms(), 0);
631     EXPECT_EQ(cpuUsageInfo1.system_cpu_time_ms(), 0);
632     EXPECT_EQ(cpuUsageInfo1.system_boot_time_ms(), 0);
633     ASSERT_EQ(cpuUsageInfo1.cores_size(), 0);
634 
635     // 空格字符串
636     sysStr = " ";
637     cpuPlugin.WriteSystemCpuUsage(*cpuData.mutable_cpu_usage_info(), cpuLoadData, sysStr.c_str(), sysStr.length());
638     cpuUsageInfo1 = cpuData.cpu_usage_info();
639     EXPECT_EQ(cpuUsageInfo1.prev_system_cpu_time_ms(), 0);
640     EXPECT_EQ(cpuUsageInfo1.prev_system_boot_time_ms(), 0);
641     EXPECT_EQ(cpuUsageInfo1.system_cpu_time_ms(), 0);
642     EXPECT_EQ(cpuUsageInfo1.system_boot_time_ms(), 0);
643     ASSERT_EQ(cpuUsageInfo1.cores_size(), 0);
644 
645     // 数据错误
646     sysStr = "1000";
647     cpuPlugin.WriteSystemCpuUsage(*cpuData.mutable_cpu_usage_info(), cpuLoadData, sysStr.c_str(), sysStr.length());
648     cpuUsageInfo1 = cpuData.cpu_usage_info();
649     EXPECT_EQ(cpuUsageInfo1.prev_system_cpu_time_ms(), 0);
650     EXPECT_EQ(cpuUsageInfo1.prev_system_boot_time_ms(), 0);
651     EXPECT_EQ(cpuUsageInfo1.system_cpu_time_ms(), 0);
652     EXPECT_EQ(cpuUsageInfo1.system_boot_time_ms(), 0);
653     ASSERT_EQ(cpuUsageInfo1.cores_size(), 0);
654 
655     // 数据不够
656     sysStr = "cpu  1234567 2345678 1111111 1193291234 3546789\n"
657              "cpu0 2345678 662862 1966195 1111111 3571925\n"
658              "cpu1 3861506 2345678 1702753 199535158 1752008\n"
659              "cpu2 3549890 1111111 2345678 1234567 1133743\n"
660              "cpu3 3336646 676939 1458898 2345678 854578\n"
661              "cpu4 1111111 601107 2305309 3546789 929594\n"
662              "cpu5 3546789 658673 1234567 197791346 738811\n";
663     cpuPlugin.WriteSystemCpuUsage(*cpuData.mutable_cpu_usage_info(), cpuLoadData, sysStr.c_str(), sysStr.length());
664     cpuUsageInfo1 = cpuData.cpu_usage_info();
665     EXPECT_EQ(cpuUsageInfo1.prev_system_cpu_time_ms(), 0);
666     EXPECT_EQ(cpuUsageInfo1.prev_system_boot_time_ms(), 0);
667     EXPECT_EQ(cpuUsageInfo1.system_cpu_time_ms(), 0);
668     EXPECT_EQ(cpuUsageInfo1.system_boot_time_ms(), 0);
669     ASSERT_EQ(cpuUsageInfo1.cores_size(), 0);
670 
671     // 多个空格,可以取出正确数据
672     sysStr = "cpu  1234567 2345678 1111111    1193291234 3546789 0 2345678 0 0\n"
673              "cpu0 2345678    662862 1966195 1111111 3571925 0 817371 0 0\n"
674              "cpu1 3861506 2345678 1702753   199535158     1752008 0 401639 0 0\n"
675              "cpu2 3549890    1111111  2345678 1234567 1133743 0 205972     0 0\n"
676              "cpu3 3336646 676939     1458898 2345678 854578 0 2345678 0 0\n"
677              "cpu4 1111111   601107 2305309 3546789 929594     0 1007959 0 0\n"
678              "cpu5 3546789 658673 1234567     197791346 738811 0 49496 0 0\n";
679     cpuPlugin.WriteSystemCpuUsage(*cpuData.mutable_cpu_usage_info(), cpuLoadData, sysStr.c_str(), sysStr.length());
680     cpuUsageInfo1 = cpuData.cpu_usage_info();
681     EXPECT_EQ(cpuUsageInfo1.prev_system_cpu_time_ms(), 0);
682     EXPECT_EQ(cpuUsageInfo1.prev_system_boot_time_ms(), 0);
683     EXPECT_EQ(cpuUsageInfo1.system_cpu_time_ms(), systemCpuTime);
684     EXPECT_EQ(cpuUsageInfo1.system_boot_time_ms(), systemBootTime);
685     ASSERT_EQ(cpuUsageInfo1.cores_size(), CORE_NUM);
686     std::vector<int64_t> systemCpuTimeVec1(CORE_NUM);
687     std::vector<int64_t> systemBootTimeVec1(CORE_NUM);
688     for (int i = 1; i <= CORE_NUM; i++) {
689         CpuCoreUsageInfo cpuCoreUsageInfo = cpuUsageInfo1.cores()[i - 1];
690         GetSystemCpuTime(g_systemStat1[i], Hz, systemCpuTimeVec1[i-1], systemBootTimeVec1[i-1]);
691         EXPECT_EQ(cpuCoreUsageInfo.cpu_core(), i-1);
692         EXPECT_EQ(cpuCoreUsageInfo.prev_system_cpu_time_ms(), 0);
693         EXPECT_EQ(cpuCoreUsageInfo.prev_system_boot_time_ms(), 0);
694         EXPECT_EQ(cpuCoreUsageInfo.system_cpu_time_ms(), systemCpuTimeVec1[i-1]);
695         EXPECT_EQ(cpuCoreUsageInfo.system_boot_time_ms(), systemBootTimeVec1[i-1]);
696     }
697 
698     // 最后一个数据之后没有空格,可以取出正确数据
699     sysStr = "cpu  1234567 2345678 1111111 1193291234 3546789 0 2345678 0 0\n"
700              "cpu0 2345678 662862 1966195 1111111 3571925 0 817371 0 0\n"
701              "cpu1 3861506 2345678 1702753 199535158 1752008 0 401639 0 0\n"
702              "cpu2 3549890 1111111 2345678 1234567 1133743 0 205972 0 0\n"
703              "cpu3 3336646 676939 1458898 2345678 854578 0 2345678 0 0\n"
704              "cpu4 1111111 601107 2305309 3546789 929594 0 1007959 0 0\n"
705              "cpu5 3546789 658673 1234567 197791346 738811 0 49496 0 0\n";
706     cpuPlugin.WriteSystemCpuUsage(*cpuData.mutable_cpu_usage_info(), cpuLoadData, sysStr.c_str(), sysStr.length());
707     cpuUsageInfo1 = cpuData.cpu_usage_info();
708     EXPECT_EQ(cpuUsageInfo1.prev_system_cpu_time_ms(), systemCpuTime);
709     EXPECT_EQ(cpuUsageInfo1.prev_system_boot_time_ms(), systemBootTime);
710     EXPECT_EQ(cpuUsageInfo1.system_cpu_time_ms(), systemCpuTime);
711     EXPECT_EQ(cpuUsageInfo1.system_boot_time_ms(), systemBootTime);
712     ASSERT_EQ(cpuUsageInfo1.cores_size(), CORE_NUM*2);
713     std::vector<int64_t> systemCpuTimeVec2(CORE_NUM);
714     std::vector<int64_t> systemBootTimeVec2(CORE_NUM);
715     for (int i = 1; i <= CORE_NUM; i++) {
716         CpuCoreUsageInfo cpuCoreUsageInfo = cpuUsageInfo1.cores()[CORE_NUM+i-1];
717         GetSystemCpuTime(g_systemStat1[i], Hz, systemCpuTimeVec2[i-1], systemBootTimeVec2[i-1]);
718         EXPECT_EQ(cpuCoreUsageInfo.cpu_core(), i-1);
719         EXPECT_EQ(cpuCoreUsageInfo.prev_system_cpu_time_ms(), systemCpuTimeVec1[i-1]);
720         EXPECT_EQ(cpuCoreUsageInfo.prev_system_boot_time_ms(), systemBootTimeVec1[i-1]);
721         EXPECT_EQ(cpuCoreUsageInfo.system_cpu_time_ms(), systemCpuTimeVec2[i-1]);
722         EXPECT_EQ(cpuCoreUsageInfo.system_boot_time_ms(), systemBootTimeVec2[i-1]);
723     }
724 }
725 
726 /**
727  * @tc.name: cpu plugin
728  * @tc.desc: a part of cpu thread information test.
729  * @tc.type: FUNC
730  */
731 HWTEST_F(CpuDataPluginTest, TestPluginThreadInfo, TestSize.Level1)
732 {
733     std::vector<std::string> threadVec1;
734     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");
735     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");
736     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");
737     threadVec1.push_back("1655 (HeapTaskDaemon) S 1746 1746 0 0 -1 1077936192 1515 0 0 0 3 0 0 0 24 4 19 0");
738     threadVec1.push_back("1656 (ReferenceQueueD) D 1746 1746 0 0 -1 1077936192 24 0 0 0 0 0 0 0 24 4 19 0");
739 
740     std::vector<std::string> threadVec2;
741     threadVec2.push_back("1662 (FinalizerDaemon) T 1746 1746 0 0 -1 1077936192 18 0 0 0 0 0 0 0 24 4 19 0");
742     threadVec2.push_back("1663 (FinalizerWatchd) R 1746 1746 0 0 -1 1077936192 5 0 0 0 0 0 0 0 24 4 19 0");
743     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");
744     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");
745     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");
746     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");
747     CpuData cpuData;
748     CpuDataPlugin cpuPlugin;
749     int64_t Hz = cpuPlugin.GetUserHz();
750 
751     // 存入threadVec0-4
752     int32_t tid;
753     for (int i = 0; i < FIRST_THREAD_NUM; i++) {
754         auto* threadInfo = cpuData.add_thread_info();
755         tid = atoi(threadVec1[i].substr(0, threadVec1[i].find(" ")).c_str());
756         cpuPlugin.WriteThread(*threadInfo, threadVec1[i].c_str(), threadVec1[i].length(), tid);
757     }
758     ASSERT_EQ(cpuData.thread_info_size(), FIRST_THREAD_NUM);
759     int64_t threadCpuTime1;
760     for (int i = 0; i < FIRST_THREAD_NUM; i++) {
761         threadCpuTime1 = (g_tidStat1[i].stat.utime + g_tidStat1[i].stat.stime) * Hz;
762         ThreadInfo threadInfo1 = cpuData.thread_info()[i];
763         EXPECT_EQ(threadInfo1.tid(), g_tidStat1[i].tid);
764         EXPECT_STREQ(threadInfo1.thread_name().c_str(), g_tidStat1[i].name.c_str());
765         EXPECT_EQ(threadInfo1.thread_state(), g_tidStat1[i].state);
766         EXPECT_EQ(threadInfo1.prev_thread_cpu_time_ms(), 0);
767         EXPECT_EQ(threadInfo1.thread_cpu_time_ms(), threadCpuTime1);
768     }
769 
770     // 存入threadVec5-10
771     for (int i = 0; i < SECOND_THREAD_NUM; i++) {
772         auto* threadInfo = cpuData.add_thread_info();
773         tid = atoi(threadVec2[i].substr(0, threadVec2[i].find(" ")).c_str());
774         cpuPlugin.WriteThread(*threadInfo, threadVec2[i].c_str(), threadVec2[i].length(), tid);
775     }
776     ASSERT_EQ(cpuData.thread_info_size(), FIRST_THREAD_NUM+SECOND_THREAD_NUM);
777     std::vector<int64_t> threadCpuTime2;
778     for (int i = 0; i < SECOND_THREAD_NUM; i++) {
779         threadCpuTime2.push_back((g_tidStat2[i].stat.utime + g_tidStat2[i].stat.stime) * Hz);
780         ThreadInfo threadInfo2 = cpuData.thread_info()[i+FIRST_THREAD_NUM];
781         EXPECT_EQ(threadInfo2.tid(), g_tidStat2[i].tid);
782         EXPECT_STREQ(threadInfo2.thread_name().c_str(), g_tidStat2[i].name.c_str());
783         EXPECT_EQ(threadInfo2.thread_state(), g_tidStat2[i].state);
784         EXPECT_EQ(threadInfo2.prev_thread_cpu_time_ms(), 0);
785         EXPECT_EQ(threadInfo2.thread_cpu_time_ms(), threadCpuTime2[i]);
786     }
787 
788     // 重复存入threadVec5-10
789     for (int i = 0; i < SECOND_THREAD_NUM; i++) {
790         auto* threadInfo = cpuData.add_thread_info();
791         tid = atoi(threadVec2[i].substr(0, threadVec2[i].find(" ")).c_str());
792         cpuPlugin.WriteThread(*threadInfo, threadVec2[i].c_str(), threadVec2[i].length(), tid);
793     }
794 
795     ASSERT_EQ(cpuData.thread_info_size(), FIRST_THREAD_NUM+SECOND_THREAD_NUM*2);
796     int64_t threadCpuTime3;
797     for (int i = 0; i < SECOND_THREAD_NUM; i++) {
798         threadCpuTime3 = (g_tidStat2[i].stat.utime + g_tidStat2[i].stat.stime) * Hz;
799         ThreadInfo threadInfo3 = cpuData.thread_info()[i+FIRST_THREAD_NUM+SECOND_THREAD_NUM];
800         EXPECT_EQ(threadInfo3.tid(), g_tidStat2[i].tid);
801         EXPECT_STREQ(threadInfo3.thread_name().c_str(), g_tidStat2[i].name.c_str());
802         EXPECT_EQ(threadInfo3.thread_state(), g_tidStat2[i].state);
803         EXPECT_EQ(threadInfo3.prev_thread_cpu_time_ms(), threadCpuTime2[i]);
804         EXPECT_EQ(threadInfo3.thread_cpu_time_ms(), threadCpuTime3);
805     }
806 }
807 
808 /**
809  * @tc.name: cpu plugin
810  * @tc.desc: cpu thread information test for abnormal parameters.
811  * @tc.type: FUNC
812  */
813 HWTEST_F(CpuDataPluginTest, TestPluginThreadBoundary, TestSize.Level1)
814 {
815     CpuData cpuData;
816     CpuDataPlugin cpuPlugin;
817     std::string threadStr;
818     int thread_info_count = 0;
819     int64_t Hz = cpuPlugin.GetUserHz();
820     int64_t threadCpuTime = (g_tidStat1[0].stat.utime + g_tidStat1[0].stat.stime) * Hz;
821 
822     // 空字符串
823     threadStr = "";
824     auto* threadInfo = cpuData.add_thread_info();
825     thread_info_count++;
826     cpuPlugin.WriteThread(*threadInfo, threadStr.c_str(), threadStr.length(), 0);
827     ASSERT_EQ(cpuData.thread_info_size(), thread_info_count);
828 
829     // 空格字符串
830     threadStr = " ";
831     threadInfo = cpuData.add_thread_info();
832     thread_info_count++;
833     cpuPlugin.WriteThread(*threadInfo, threadStr.c_str(), threadStr.length(), 0);
834     ASSERT_EQ(cpuData.thread_info_size(), thread_info_count);
835 
836     // 数据错误
837     threadStr = "1000";
838     threadInfo = cpuData.add_thread_info();
839     thread_info_count++;
840     cpuPlugin.WriteThread(*threadInfo, threadStr.c_str(), threadStr.length(), 0);
841     ASSERT_EQ(cpuData.thread_info_size(), thread_info_count);
842 
843     // 数据不够
844     threadStr = "1209 (skytone:service) R 1746 1746 0 0 -1 1077936448 10399 0 1 0 22 8";
845     threadInfo = cpuData.add_thread_info();
846     thread_info_count++;
847     cpuPlugin.WriteThread(*threadInfo, threadStr.c_str(), threadStr.length(), 0);
848     ASSERT_EQ(cpuData.thread_info_size(), thread_info_count);
849 
850     // 线程名缺失左括号
851     threadStr = "1209 skytone:service) R 1746 1746 0 0 -1 1077936448 10399 0 1 0 22 8 0 0 20 0 19 0";
852     int32_t tid = atoi(threadStr.substr(0, threadStr.find(" ")).c_str());
853     threadInfo = cpuData.add_thread_info();
854     thread_info_count++;
855     cpuPlugin.WriteThread(*threadInfo, threadStr.c_str(), threadStr.length(), tid);
856     ASSERT_EQ(cpuData.thread_info_size(), thread_info_count);
857     ThreadInfo threadInfo1 = cpuData.thread_info()[thread_info_count-1];
858     EXPECT_EQ(threadInfo1.tid(), g_tidStat1[0].tid);
859     EXPECT_STRNE(threadInfo1.thread_name().c_str(), g_tidStat1[0].name.c_str());
860     EXPECT_EQ(threadInfo1.thread_state(), g_tidStat1[0].state);
861     EXPECT_EQ(threadInfo1.prev_thread_cpu_time_ms(), 0);
862     EXPECT_EQ(threadInfo1.thread_cpu_time_ms(), threadCpuTime);
863 
864     // 多个空格,可以取出正确数据
865     threadStr = "1209 (skytone:service)    R 1746 1746 0 0  -1 1077936448   10399 0 1 0 22 8 0 0    20 0 19   0";
866     tid = atoi(threadStr.substr(0, threadStr.find(" ")).c_str());
867     threadInfo = cpuData.add_thread_info();
868     thread_info_count++;
869     cpuPlugin.WriteThread(*threadInfo, threadStr.c_str(), threadStr.length(), tid);
870     ASSERT_EQ(cpuData.thread_info_size(), thread_info_count);
871     threadInfo1 = cpuData.thread_info()[thread_info_count-1];
872     EXPECT_EQ(threadInfo1.tid(), g_tidStat1[0].tid);
873     EXPECT_STREQ(threadInfo1.thread_name().c_str(), g_tidStat1[0].name.c_str());
874     EXPECT_EQ(threadInfo1.thread_state(), g_tidStat1[0].state);
875     EXPECT_EQ(threadInfo1.prev_thread_cpu_time_ms(), threadCpuTime);
876     EXPECT_EQ(threadInfo1.thread_cpu_time_ms(), threadCpuTime);
877 
878     // 最后一个数据之后没有空格,可以取出正确数据
879     threadStr = "1209 (skytone:service) R 1746 1746 0 0 -1 1077936448 10399 0 1 0 22 8 0 0 20";
880     tid = atoi(threadStr.substr(0, threadStr.find(" ")).c_str());
881     threadInfo = cpuData.add_thread_info();
882     thread_info_count++;
883     cpuPlugin.WriteThread(*threadInfo, threadStr.c_str(), threadStr.length(), tid);
884     ASSERT_EQ(cpuData.thread_info_size(), thread_info_count);
885     threadInfo1 = cpuData.thread_info()[thread_info_count-1];
886     EXPECT_EQ(threadInfo1.tid(), g_tidStat1[0].tid);
887     EXPECT_STREQ(threadInfo1.thread_name().c_str(), g_tidStat1[0].name.c_str());
888     EXPECT_EQ(threadInfo1.thread_state(), g_tidStat1[0].state);
889     EXPECT_EQ(threadInfo1.prev_thread_cpu_time_ms(), threadCpuTime);
890     EXPECT_EQ(threadInfo1.thread_cpu_time_ms(), threadCpuTime);
891 }
892 
ExecuteBin(const std::string & bin,const std::vector<std::string> & args)893 bool ExecuteBin(const std::string& bin, const std::vector<std::string>& args)
894 {
895     std::vector<char*> argv;
896     for (size_t i = 0; i < args.size(); i++) {
897         argv.push_back(const_cast<char*>(args[i].c_str()));
898     }
899     argv.push_back(nullptr); // last item in argv must be NULL
900 
901     int retval = execvp(bin.c_str(), argv.data());
902     CHECK_TRUE(retval != -1, false, "execv %s failed, %d!", bin.c_str(), errno);
903     _exit(EXIT_FAILURE);
904     abort(); // never should be here.
905     return true;
906 }
907 
908 /**
909  * @tc.name: cpu plugin
910  * @tc.desc: test ParseCpuInfo for pid
911  * @tc.type: FUNC
912  */
913 HWTEST_F(CpuDataPluginTest, TestPid, TestSize.Level1)
914 {
915     CpuDataPlugin plugin1, plugin2;
916     CpuData cpuData1, cpuData2;
917 
918     std::string cmd = "chmod 777 " + DEFAULT_BIN_PATH;
919     system(cmd.c_str());
920 
921     pid_t pid1 = fork();
922     if (pid1 == 0) {
923         // set 0, not to do
924         std::vector<std::string> argv = {"childpidtest1", "0"};
925         ASSERT_TRUE(ExecuteBin(DEFAULT_BIN_PATH, argv));
926     }
927     pid_t pid2 = fork();
928     if (pid2 == 0) {
929         // set 1, consume cpu
930         std::vector<std::string> argv = {"childpidtest2", "1"};
931         ASSERT_TRUE(ExecuteBin(DEFAULT_BIN_PATH, argv));
932     }
933 
934 #ifdef COVERAGE_TEST
935     const int coverageSleepTime = 5; // sleep 5s
936     sleep(coverageSleepTime);
937 #else
938     sleep(1); // 睡眠1s,确保pid1进入睡眠状态,pid2进入while循环
939 #endif // COVERAGE_TEST
940     EXPECT_TRUE(PluginCpuinfoStub(plugin1, cpuData1, static_cast<int>(pid1), false, false));
941     EXPECT_TRUE(PluginCpuinfoStub(plugin2, cpuData2, static_cast<int>(pid2), false, false));
942     EXPECT_LT(cpuData1.cpu_usage_info().process_cpu_time_ms(), cpuData2.cpu_usage_info().process_cpu_time_ms());
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