1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <hwext/gtest-ext.h> 17 #include <hwext/gtest-tag.h> 18 19 #include "process_filter.h" 20 21 using namespace testing::ext; 22 using namespace SysTuning::TraceStreamer; 23 namespace SysTuning { 24 namespace TraceStreamer { 25 class ProcessFilterTest : public ::testing::Test { 26 public: SetUp()27 void SetUp() 28 { 29 streamFilters_.processFilter_ = std::make_unique<ProcessFilter>(&traceDataCache_, &streamFilters_); 30 } 31 TearDown()32 void TearDown() {} 33 34 public: 35 TraceStreamerFilters streamFilters_; 36 TraceDataCache traceDataCache_; 37 }; 38 39 /** 40 * @tc.name: UpdateOrCreateThread 41 * @tc.desc: Test UpdateOrCreateThread interface 42 * @tc.type: FUNC 43 */ 44 HWTEST_F(ProcessFilterTest, UpdateOrCreateThread, TestSize.Level1) 45 { 46 TS_LOGI("test26-1"); 47 uint64_t ts = 168758662877000; 48 uint32_t tid0 = 2716; 49 uint32_t iTid0 = streamFilters_.processFilter_->UpdateOrCreateThread(ts, tid0); 50 EXPECT_TRUE(iTid0 == 1); 51 52 uint32_t tid1 = 2519; 53 uint32_t iTid1 = streamFilters_.processFilter_->UpdateOrCreateThread(0, tid1); 54 EXPECT_TRUE(iTid1 == 2); 55 56 Thread *thread = traceDataCache_.GetThreadData(iTid0); 57 EXPECT_TRUE(thread->tid_ == tid0); 58 59 thread = traceDataCache_.GetThreadData(iTid1); 60 EXPECT_TRUE(thread->tid_ == tid1); 61 } 62 63 /** 64 * @tc.name: UpdateOrCreateProcessWithName 65 * @tc.desc: Test UpdateOrCreateProcessWithName interface 66 * @tc.type: FUNC 67 */ 68 HWTEST_F(ProcessFilterTest, UpdateOrCreateProcessWithName, TestSize.Level1) 69 { 70 TS_LOGI("test26-2"); 71 uint32_t pid0 = 8629; 72 std::string_view processName = "RenderThread"; 73 uint32_t iPid0 = streamFilters_.processFilter_->UpdateOrCreateProcessWithName(pid0, processName); 74 EXPECT_TRUE(iPid0 == 1); 75 76 uint32_t pid1 = 8709; 77 uint32_t iPid1 = streamFilters_.processFilter_->UpdateOrCreateProcessWithName(pid1, processName); 78 EXPECT_TRUE(iPid1 == 2); 79 80 Process *process = traceDataCache_.GetProcessData(iPid0); 81 EXPECT_TRUE(process->pid_ == pid0); 82 83 process = traceDataCache_.GetProcessData(iPid1); 84 EXPECT_TRUE(process->pid_ == pid1); 85 } 86 87 /** 88 * @tc.name: UpdateOrCreateProcessWithNameSingleIpid 89 * @tc.desc: Test whether the internal PID of the generated single process is as expected. 90 * @tc.type: FUNC 91 */ 92 HWTEST_F(ProcessFilterTest, UpdateOrCreateProcessWithNameSingleIpid, TestSize.Level1) 93 { 94 TS_LOGI("test26-3"); 95 uint32_t pid = 8629; 96 std::string_view processName = "RenderThread"; 97 uint32_t iPid0 = streamFilters_.processFilter_->UpdateOrCreateProcessWithName(pid, processName); 98 EXPECT_TRUE(iPid0 == 1); 99 } 100 101 /** 102 * @tc.name: UpdateOrCreateProcessWithNameMultiIpid 103 * @tc.desc: Test genarated multi ipid with UpdateOrCreateProcessWithName interface 104 * @tc.type: FUNC 105 */ 106 HWTEST_F(ProcessFilterTest, UpdateOrCreateProcessWithNameMultiIpid, TestSize.Level1) 107 { 108 TS_LOGI("test26-4"); 109 uint32_t pid0 = 8629; 110 std::string_view processName = "RenderThread"; 111 uint32_t iPid0 = streamFilters_.processFilter_->UpdateOrCreateProcessWithName(pid0, processName); 112 EXPECT_TRUE(iPid0 == 1); 113 114 uint32_t pid1 = 8709; 115 uint32_t iPid1 = streamFilters_.processFilter_->UpdateOrCreateProcessWithName(pid1, processName); 116 EXPECT_TRUE(iPid1 == 2); 117 } 118 119 /** 120 * @tc.name: UpdateOrCreateProcessWithNameSinglePid 121 * @tc.desc: Test genarated single pid with UpdateOrCreateProcessWithName interface 122 * @tc.type: FUNC 123 */ 124 HWTEST_F(ProcessFilterTest, UpdateOrCreateProcessWithNameSinglePid, TestSize.Level1) 125 { 126 TS_LOGI("test26-5"); 127 uint32_t pid = 8629; 128 std::string_view processName = "RenderThread"; 129 uint32_t iPid0 = streamFilters_.processFilter_->UpdateOrCreateProcessWithName(pid, processName); 130 EXPECT_TRUE(iPid0 == 1); 131 132 Process *process = traceDataCache_.GetProcessData(iPid0); 133 EXPECT_TRUE(process->pid_ == pid); 134 } 135 136 /** 137 * @tc.name: UpdateOrCreateProcessWithNameMultiPid 138 * @tc.desc: est genarated multi pid with UpdateOrCreateProcessWithName interface 139 * @tc.type: FUNC 140 */ 141 HWTEST_F(ProcessFilterTest, UpdateOrCreateProcessWithNameMultiPid, TestSize.Level1) 142 { 143 TS_LOGI("test26-6"); 144 uint32_t pid0 = 8629; 145 std::string_view processName = "RenderThread"; 146 uint32_t iPid0 = streamFilters_.processFilter_->UpdateOrCreateProcessWithName(pid0, processName); 147 EXPECT_TRUE(iPid0 == 1); 148 149 uint32_t pid1 = 8709; 150 uint32_t iPid1 = streamFilters_.processFilter_->UpdateOrCreateProcessWithName(pid1, processName); 151 EXPECT_TRUE(iPid1 == 2); 152 153 uint32_t pid3 = 87091; 154 uint32_t iPid2 = streamFilters_.processFilter_->UpdateOrCreateProcessWithName(87091, processName); 155 EXPECT_TRUE(iPid2 == 3); 156 157 Process *process = traceDataCache_.GetProcessData(iPid0); 158 EXPECT_TRUE(process->pid_ == pid0); 159 160 process = traceDataCache_.GetProcessData(iPid1); 161 EXPECT_TRUE(process->pid_ == pid1); 162 163 process = traceDataCache_.GetProcessData(iPid2); 164 EXPECT_TRUE(process->pid_ == pid3); 165 } 166 167 /** 168 * @tc.name: UpdateOrCreateThreadWithName 169 * @tc.desc: Test UpdateOrCreateThreadWithName interface 170 * @tc.type: FUNC 171 */ 172 HWTEST_F(ProcessFilterTest, UpdateOrCreateThreadWithName, TestSize.Level1) 173 { 174 TS_LOGI("test26-7"); 175 uint64_t ts = 168758662957020; 176 uint32_t tid = 123; 177 std::string_view threadName = "RenderThread"; 178 uint32_t iTid0 = streamFilters_.processFilter_->UpdateOrCreateThreadWithName(ts, tid, threadName); 179 EXPECT_TRUE(iTid0 == 1); 180 uint64_t ts2 = 168758663957020; 181 uint32_t tid2 = 2519; 182 std::string_view threadName2 = "RenderThread2"; 183 uint32_t iTid1 = streamFilters_.processFilter_->UpdateOrCreateThreadWithName(ts2, tid2, threadName2); 184 EXPECT_TRUE(iTid1 == 2); 185 186 Thread *thread = traceDataCache_.GetThreadData(iTid0); 187 EXPECT_TRUE(thread->tid_ == tid); 188 EXPECT_TRUE(thread->nameIndex_ == traceDataCache_.GetDataIndex(threadName)); 189 190 thread = traceDataCache_.GetThreadData(iTid1); 191 EXPECT_TRUE(thread->tid_ == tid2); 192 EXPECT_TRUE(thread->nameIndex_ == traceDataCache_.GetDataIndex(threadName2)); 193 } 194 195 /** 196 * @tc.name: UpdateOrCreateThreadWithNameSingleItid 197 * @tc.desc: Test genarated single itid with UpdateOrCreateThreadWithName 198 * @tc.type: FUNC 199 */ 200 HWTEST_F(ProcessFilterTest, UpdateOrCreateThreadWithNameSingleItid, TestSize.Level1) 201 { 202 TS_LOGI("test26-8"); 203 uint64_t ts = 168758662957020; 204 uint32_t tid = 123; 205 std::string_view threadName = "RenderThread"; 206 uint32_t iTid0 = streamFilters_.processFilter_->UpdateOrCreateThreadWithName(ts, tid, threadName); 207 EXPECT_TRUE(iTid0 == 1); 208 } 209 210 /** 211 * @tc.name: UpdateOrCreateThreadWithNameGenarateTidAndItid 212 * @tc.desc: Test genarated single itid and tid with UpdateOrCreateThreadWithName 213 * @tc.type: FUNC 214 */ 215 HWTEST_F(ProcessFilterTest, UpdateOrCreateThreadWithNameGenarateTidAndItid, TestSize.Level1) 216 { 217 TS_LOGI("test26-9"); 218 uint64_t ts = 168758662957020; 219 uint32_t tid = 123; 220 std::string_view threadName = "RenderThread2"; 221 uint32_t iTid0 = streamFilters_.processFilter_->UpdateOrCreateThreadWithName(ts, tid, threadName); 222 EXPECT_TRUE(iTid0 == 1); 223 Thread *thread = traceDataCache_.GetThreadData(iTid0); 224 EXPECT_TRUE(thread->tid_ == tid); 225 EXPECT_TRUE(thread->nameIndex_ == traceDataCache_.GetDataIndex(threadName)); 226 } 227 228 /** 229 * @tc.name: UpdateOrCreateThreadWithNameDoubleItid 230 * @tc.desc: Test genarate double itid with UpdateOrCreateThreadWithName interface 231 * @tc.type: FUNC 232 */ 233 HWTEST_F(ProcessFilterTest, UpdateOrCreateThreadWithNameDoubleItid, TestSize.Level1) 234 { 235 TS_LOGI("test26-10"); 236 uint64_t ts = 168758662957020; 237 uint32_t tid = 123; 238 std::string_view threadName = "RenderThread"; 239 uint32_t iTid0 = streamFilters_.processFilter_->UpdateOrCreateThreadWithName(ts, tid, threadName); 240 EXPECT_TRUE(iTid0 == 1); 241 uint64_t ts2 = 168758663957020; 242 uint32_t tid2 = 2519; 243 std::string_view threadName2 = "RenderThread2"; 244 uint32_t iTid1 = streamFilters_.processFilter_->UpdateOrCreateThreadWithName(ts2, tid2, threadName2); 245 EXPECT_TRUE(iTid1 == 2); 246 auto thread = traceDataCache_.GetThreadData(iTid1); 247 EXPECT_TRUE(thread->tid_ == tid2); 248 EXPECT_TRUE(thread->nameIndex_ == traceDataCache_.GetDataIndex(threadName2)); 249 } 250 251 /** 252 * @tc.name: UpdateOrCreateThreadWithNameTripleItid 253 * @tc.desc: Test genarate triple itid with UpdateOrCreateThreadWithName interface 254 * @tc.type: FUNC 255 */ 256 HWTEST_F(ProcessFilterTest, UpdateOrCreateThreadWithNameTripleItid, TestSize.Level1) 257 { 258 TS_LOGI("test26-11"); 259 uint64_t ts = 168758662957020; 260 uint32_t tid = 123; 261 std::string_view threadName = "RenderThread"; 262 uint32_t iTid0 = streamFilters_.processFilter_->UpdateOrCreateThreadWithName(ts, tid, threadName); 263 EXPECT_TRUE(iTid0 == 1); 264 uint64_t ts2 = 168758663957020; 265 uint32_t tid2 = 2519; 266 std::string_view threadName2 = "RenderThread2"; 267 uint32_t iTid1 = streamFilters_.processFilter_->UpdateOrCreateThreadWithName(ts2, tid2, threadName2); 268 EXPECT_TRUE(iTid1 == 2); 269 uint64_t ts3 = 168758663957020; 270 uint32_t tid3 = 25191; 271 std::string_view threadName3 = "RenderThread3"; 272 uint32_t iTid2 = streamFilters_.processFilter_->UpdateOrCreateThreadWithName(ts3, tid3, threadName3); 273 EXPECT_TRUE(iTid2 == 3); 274 auto thread = traceDataCache_.GetThreadData(iTid2); 275 EXPECT_TRUE(thread->tid_ == tid3); 276 EXPECT_TRUE(thread->nameIndex_ == traceDataCache_.GetDataIndex(threadName3)); 277 } 278 279 /** 280 * @tc.name: UpdateOrCreateThreadWithPidAndName 281 * @tc.desc: Test UpdateOrCreateThreadWithPidAndName interface 282 * @tc.type: FUNC 283 */ 284 HWTEST_F(ProcessFilterTest, UpdateOrCreateThreadWithPidAndName, TestSize.Level1) 285 { 286 TS_LOGI("test26-12"); 287 uint32_t tid = 869; 288 uint32_t pid = 123; 289 std::string_view threadName = "RenderThread"; 290 streamFilters_.processFilter_->UpdateOrCreateThreadWithPidAndName(tid, pid, threadName); 291 auto itid = streamFilters_.processFilter_->GetInternalTid(tid); 292 EXPECT_TRUE(itid != INVALID_ID); 293 294 Thread *thread = traceDataCache_.GetThreadData(itid); 295 EXPECT_TRUE(thread->nameIndex_ == traceDataCache_.GetDataIndex(threadName)); 296 } 297 298 /** 299 * @tc.name: UpdateOrCreateThreadWithPidAndNameAbnomal 300 * @tc.desc: Test UpdateOrCreateThreadWithPidAndName interface 301 * @tc.type: FUNC 302 */ 303 HWTEST_F(ProcessFilterTest, UpdateOrCreateThreadWithPidAndNameAbnomal, TestSize.Level1) 304 { 305 TS_LOGI("test26-13"); 306 uint32_t tid = 869; 307 uint32_t pid = 123; 308 std::string_view threadName = "RenderThread"; 309 streamFilters_.processFilter_->UpdateOrCreateThreadWithPidAndName(tid, pid, threadName); 310 uint32_t tid2 = 969; 311 auto itid = streamFilters_.processFilter_->GetInternalTid(tid2); 312 EXPECT_TRUE(itid == INVALID_ID); 313 } 314 315 /** 316 * @tc.name: UpdateOrCreateThreadWithPidAndNameSingleItid 317 * @tc.desc: Test UpdateOrCreateThreadWithPidAndName interface 318 * @tc.type: FUNC 319 */ 320 HWTEST_F(ProcessFilterTest, UpdateOrCreateThreadWithPidAndNameSingleItid, TestSize.Level1) 321 { 322 TS_LOGI("test26-14"); 323 uint32_t tid = 869; 324 uint32_t pid = 123; 325 std::string_view threadName = "RenderThread"; 326 streamFilters_.processFilter_->UpdateOrCreateThreadWithPidAndName(tid, pid, threadName); 327 auto itid = streamFilters_.processFilter_->GetInternalPid(pid); 328 EXPECT_TRUE(itid != INVALID_ID); 329 } 330 331 /** 332 * @tc.name: UpdateOrCreateThreadWithPidAndNameAbnomalPid 333 * @tc.desc: Test UpdateOrCreateThreadWithPidAndName interface 334 * @tc.type: FUNC 335 */ 336 HWTEST_F(ProcessFilterTest, UpdateOrCreateThreadWithPidAndNameAbnomalPid, TestSize.Level1) 337 { 338 TS_LOGI("test26-15"); 339 uint32_t tid = 869; 340 uint32_t pid0 = 123; 341 std::string_view threadName = "RenderThread"; 342 streamFilters_.processFilter_->UpdateOrCreateThreadWithPidAndName(tid, pid0, threadName); 343 uint32_t pid1 = 124; 344 auto itid = streamFilters_.processFilter_->GetInternalPid(pid1); 345 EXPECT_TRUE(itid == INVALID_ID); 346 } 347 348 /** 349 * @tc.name: UpdateThreadWithName 350 * @tc.desc: Test update thread name 351 * @tc.type: FUNC 352 */ 353 HWTEST_F(ProcessFilterTest, UpdateThreadWithName, TestSize.Level1) 354 { 355 TS_LOGI("test26-16"); 356 uint32_t tid = 869; 357 uint64_t timeStamp = 168758662957020; 358 std::string_view threadName = "RenderThread"; 359 streamFilters_.processFilter_->UpdateOrCreateThread(timeStamp, tid); 360 streamFilters_.processFilter_->UpdateOrCreateThreadWithName(timeStamp, tid, threadName); 361 auto itid = streamFilters_.processFilter_->GetInternalTid(tid); 362 EXPECT_TRUE(itid != INVALID_ID); 363 Thread *thread = traceDataCache_.GetThreadData(itid); 364 EXPECT_TRUE(thread->nameIndex_ == traceDataCache_.GetDataIndex(threadName)); 365 } 366 367 /** 368 * @tc.name: UpdateProcessWithName 369 * @tc.desc: Test update process name 370 * @tc.type: FUNC 371 */ 372 HWTEST_F(ProcessFilterTest, UpdateProcessWithName, TestSize.Level1) 373 { 374 TS_LOGI("test26-17"); 375 uint32_t pid = 869; 376 uint64_t timeStamp = 168758662957020; 377 std::string_view processName = "RenderProcess"; 378 auto ipid = streamFilters_.processFilter_->GetOrCreateInternalPid(timeStamp, pid); 379 EXPECT_TRUE(ipid != INVALID_ID); 380 streamFilters_.processFilter_->UpdateOrCreateProcessWithName(pid, processName); 381 Process *process = traceDataCache_.GetProcessData(ipid); 382 EXPECT_TRUE(process->cmdLine_ == processName); 383 } 384 } // namespace TraceStreamer 385 } // namespace SysTuning 386