1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2024. 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 <cinttypes> 17 #include <dlfcn.h> 18 #include <gtest/gtest.h> 19 #include <csignal> 20 #include <filesystem> 21 22 #include "command_poller.h" 23 #include "ffrt_profiler_common.h" 24 #include "ffrt_profiler_manager.h" 25 #include "socket_context.h" 26 27 namespace fs = std::filesystem; 28 using namespace testing::ext; 29 using namespace OHOS::Developtools::Profiler; 30 31 namespace { 32 const std::string OUTPUT_PATH = "/data/local/tmp/hiprofiler_data.htrace"; 33 const std::string FFRT_TEST_EXE = "/data/local/tmp/ffrt_profiler_test_exe"; 34 constexpr uint32_t BUFFER_SIZE = (1UL << 23); 35 constexpr int FILE_SIZE = 2000; 36 constexpr int MOBILE_BIT = 32; 37 constexpr int32_t SMB_SIZE = 409600; 38 39 class FfrtPofilerTest : public ::testing::Test { 40 public: FfrtPofilerTest()41 FfrtPofilerTest() {} ~FfrtPofilerTest()42 ~FfrtPofilerTest() {} SetUpTestCase()43 static void SetUpTestCase() {} TearDownTestCase()44 static void TearDownTestCase() {} 45 CreateCommand(const std::string & outputFile,int32_t time,const std::string & model,const std::string & procedure) const46 std::string CreateCommand(const std::string& outputFile, int32_t time, const std::string& model, 47 const std::string& procedure) const 48 { 49 std::string cmdStr = 50 "hiprofiler_cmd \\\n" 51 "-c - \\\n"; 52 cmdStr += "-o " + outputFile + " \\\n"; 53 cmdStr += "-t " + std::to_string(time) + " \\\n"; 54 cmdStr += "-s \\\n"; 55 cmdStr += "-k \\\n" 56 "<<CONFIG\n" 57 "request_id: 1\n" 58 "session_config {\n" 59 " buffers {\n" 60 " pages: 32768\n" 61 " }\n" 62 " result_file: \"/data/local/tmp/hiprofiler_data.htrace\"\n" 63 " sample_duration: 30000\n" 64 "}\n" 65 "plugin_configs {\n" 66 " plugin_name: \"ffrt-profiler\"\n" 67 " config_data {\n"; 68 cmdStr += model + ": " + procedure + '\n'; 69 cmdStr += "smb_pages: 16384\n" 70 "flush_interval: 5\n" 71 "block: true\n" 72 "clock_id: BOOTTIME\n" 73 " }\n" 74 "}\n" 75 "CONFIG\n"; 76 return cmdStr; 77 } 78 StartProcess(const std::string & name,const std::string & args)79 void StartProcess(const std::string& name, const std::string& args) 80 { 81 if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) { 82 return; 83 } 84 85 int processNum = fork(); 86 if (processNum == 0) { 87 execl(name.c_str(), name.c_str(), args.c_str(), NULL); 88 _exit(1); 89 } else if (processNum < 0) { 90 PROFILER_LOG_ERROR(LOG_CORE, "Failed to fork process"); 91 } else { 92 PROFILER_LOG_ERROR(LOG_CORE, "sub process PID: %d", processNum); 93 ffrtPrfolerExePid_ = processNum; 94 } 95 } 96 RunCommand(const std::string & cmd,std::string & content)97 bool RunCommand(const std::string& cmd, std::string& content) 98 { 99 std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose); 100 CHECK_TRUE(pipe, false, "RunCommand: create popen FAILED!"); 101 static constexpr int buffSize = 1024; 102 std::array<char, buffSize> buffer; 103 while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) { 104 content += buffer.data(); 105 } 106 return true; 107 } 108 CheckFileSize(const std::string & filePath)109 bool CheckFileSize(const std::string& filePath) 110 { 111 if (!fs::exists(filePath)) { 112 return false; 113 } 114 if (fs::file_size(filePath) < FILE_SIZE) { 115 return false; 116 } 117 return true; 118 } 119 120 int ffrtPrfolerExePid_{0}; 121 }; 122 123 HWTEST_F(FfrtPofilerTest, TestFfrtProfilerRuntime, TestSize.Level1) 124 { 125 StartProcess(FFRT_TEST_EXE, "100"); 126 std::string cmd = CreateCommand(OUTPUT_PATH, 10, "pid", std::to_string(ffrtPrfolerExePid_)); 127 std::string ret; 128 fs::remove(OUTPUT_PATH); 129 EXPECT_TRUE(RunCommand(cmd, ret)); 130 EXPECT_TRUE(ret.find("FAIL") == std::string::npos); 131 } 132 133 HWTEST_F(FfrtPofilerTest, TestFfrtProfilerError, TestSize.Level1) 134 { 135 std::string cmd = CreateCommand(OUTPUT_PATH, 10, "pid", std::to_string(ffrtPrfolerExePid_)); 136 std::string ret; 137 fs::remove(OUTPUT_PATH); 138 EXPECT_TRUE(RunCommand(cmd, ret)); 139 EXPECT_TRUE(ret.find("FAIL") == std::string::npos); 140 EXPECT_FALSE(CheckFileSize(OUTPUT_PATH)); 141 } 142 143 /** 144 * @tc.name: ffrt plugin 145 * @tc.desc: FfrtPofiler CheckConfig Function return false 146 * @tc.type: FUNC 147 */ 148 HWTEST_F(FfrtPofilerTest, TestFunction001, TestSize.Level1) 149 { 150 using namespace OHOS::Developtools::Profiler; 151 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>(); 152 std::string cmd = CreateCommand(OUTPUT_PATH, 10, "pid", std::to_string(ffrtPrfolerExePid_)); 153 std::string ret; 154 EXPECT_TRUE(RunCommand(cmd, ret)); 155 ffrtProfilerMgr->Init(); 156 EXPECT_FALSE(ffrtProfilerMgr->CheckConfig()); 157 } 158 159 /** 160 * @tc.name: ffrt plugin 161 * @tc.desc: FfrtPofiler CheckConfig Function startup_process is exit return false 162 * @tc.type: FUNC 163 */ 164 HWTEST_F(FfrtPofilerTest, TestFunction002, TestSize.Level1) 165 { 166 std::string cmd = CreateCommand(OUTPUT_PATH, 10, "pid", std::to_string(ffrtPrfolerExePid_)); 167 std::string ret; 168 EXPECT_TRUE(RunCommand(cmd, ret)); 169 std::string pidService; 170 RunCommand("pidof render_service", pidService); 171 172 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>(); 173 FfrtProfilerConfig config; 174 int32_t pid = 1; 175 config.add_pid(pid); 176 config.add_pid(stoi(pidService)); 177 config.add_pid(ffrtPrfolerExePid_); 178 config.add_startup_process_name("test_name001"); 179 config.add_startup_process_name("render_service"); 180 config.add_startup_process_name("test_name003"); 181 config.set_clock_id(FfrtProfilerConfig::REALTIME_COARSE); 182 ffrtProfilerMgr->SetConfig(config); 183 EXPECT_FALSE(ffrtProfilerMgr->CheckConfig()); 184 } 185 186 /** 187 * @tc.name: ffrt plugin 188 * @tc.desc: FfrtPofiler CheckConfig Function startup_process is not exit return true 189 * @tc.type: FUNC 190 */ 191 HWTEST_F(FfrtPofilerTest, TestFunction003, TestSize.Level1) 192 { 193 std::string cmd = CreateCommand(OUTPUT_PATH, 10, "pid", std::to_string(ffrtPrfolerExePid_)); 194 std::string ret; 195 EXPECT_TRUE(RunCommand(cmd, ret)); 196 197 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>(); 198 FfrtProfilerConfig config; 199 int32_t pid = 1; 200 config.add_pid(pid); 201 config.add_pid(ffrtPrfolerExePid_); 202 config.add_startup_process_name("test_name001"); 203 config.add_startup_process_name("test_name002"); 204 config.set_clock_id(FfrtProfilerConfig::REALTIME_COARSE); 205 ffrtProfilerMgr->SetConfig(config); 206 EXPECT_TRUE(ffrtProfilerMgr->CheckConfig()); 207 } 208 209 /** 210 * @tc.name: ffrt plugin 211 * @tc.desc: FfrtPofiler CheckConfig Function startup_process_name is not exit return flase 212 * @tc.type: FUNC 213 */ 214 HWTEST_F(FfrtPofilerTest, TestFunction004, TestSize.Level1) 215 { 216 StartProcess(FFRT_TEST_EXE, "100"); 217 std::string cmd = CreateCommand(OUTPUT_PATH, 10, "pid", std::to_string(ffrtPrfolerExePid_)); 218 std::string ret; 219 EXPECT_TRUE(RunCommand(cmd, ret)); 220 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>(); 221 FfrtProfilerConfig config; 222 int32_t pid = 1; 223 224 std::string cont; 225 RunCommand("pidof render_service", cont); 226 auto pidService = stoi(cont); 227 228 config.add_pid(pid); 229 config.add_pid(pidService); 230 config.add_startup_process_name("test_name"); 231 config.add_startup_process_name("test_name001"); 232 config.add_restart_process_name("test_name1"); 233 config.add_restart_process_name("render_service2"); 234 config.set_clock_id(FfrtProfilerConfig::MONOTONIC_COARSE); 235 ffrtProfilerMgr->SetConfig(config); 236 EXPECT_FALSE(ffrtProfilerMgr->CheckConfig()); 237 } 238 239 /** 240 * @tc.name: ffrt plugin 241 * @tc.desc: FfrtPofiler CheckConfig Function startup_process_name is exit return flase 242 * @tc.type: FUNC 243 */ 244 HWTEST_F(FfrtPofilerTest, TestFunction005, TestSize.Level1) 245 { 246 StartProcess(FFRT_TEST_EXE, "100"); 247 std::string cmd = CreateCommand(OUTPUT_PATH, 10, "pid", std::to_string(ffrtPrfolerExePid_)); 248 std::string ret; 249 EXPECT_TRUE(RunCommand(cmd, ret)); 250 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>(); 251 FfrtProfilerConfig config; 252 int32_t pid = 1; 253 254 std::string cont; 255 RunCommand("pidof render_service", cont); 256 auto pidService = stoi(cont); 257 258 config.add_pid(pid); 259 config.add_pid(pidService); 260 config.add_startup_process_name("test_name"); 261 config.add_startup_process_name("test_name001"); 262 config.add_restart_process_name("render_service"); 263 config.add_restart_process_name("test_name2"); 264 config.set_clock_id(FfrtProfilerConfig::MONOTONIC_COARSE); 265 ffrtProfilerMgr->SetConfig(config); 266 EXPECT_FALSE(ffrtProfilerMgr->CheckConfig()); 267 } 268 269 /** 270 * @tc.name: ffrt plugin 271 * @tc.desc: FfrtPofiler StartFfrtProfiler Function Test return false 272 * @tc.type: FUNC 273 */ 274 HWTEST_F(FfrtPofilerTest, TestFunction006, TestSize.Level1) 275 { 276 std::string cmd = CreateCommand(OUTPUT_PATH, 10, "pid", std::to_string(ffrtPrfolerExePid_)); 277 std::string ret; 278 EXPECT_TRUE(RunCommand(cmd, ret)); 279 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>(); 280 281 FfrtProfilerConfig config; 282 config.add_startup_process_name("test_name001"); 283 config.add_startup_process_name("test_name002"); 284 config.set_clock_id(FfrtProfilerConfig::REALTIME_COARSE); 285 ffrtProfilerMgr->Init(); 286 EXPECT_FALSE(ffrtProfilerMgr->StartFfrtProfiler()); 287 } 288 289 /** 290 * @tc.name: ffrt plugin 291 * @tc.desc: FfrtPofiler StartFfrtProfiler Function set smb_pages Test return true 292 * @tc.type: FUNC 293 */ 294 HWTEST_F(FfrtPofilerTest, TestFunction007, TestSize.Level1) 295 { 296 std::string cmd = CreateCommand(OUTPUT_PATH, 10, "pid", std::to_string(ffrtPrfolerExePid_)); 297 std::string ret; 298 EXPECT_TRUE(RunCommand(cmd, ret)); 299 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>(); 300 FfrtProfilerConfig config; 301 int32_t pid = 1; 302 config.add_pid(pid); 303 config.add_pid(ffrtPrfolerExePid_); 304 config.add_startup_process_name("test_name001"); 305 config.add_startup_process_name("test_name002"); 306 config.set_clock_id(FfrtProfilerConfig::MONOTONIC_COARSE); 307 config.set_smb_pages(4096); 308 ffrtProfilerMgr->SetConfig(config); 309 ffrtProfilerMgr->Init(); 310 EXPECT_TRUE(ffrtProfilerMgr->StartFfrtProfiler()); 311 } 312 313 /** 314 * @tc.name: ffrt plugin 315 * @tc.desc: FfrtPofiler GetFfrtProfilerCtx Function Test 316 * @tc.type: FUNC 317 */ 318 HWTEST_F(FfrtPofilerTest, TestFunction008, TestSize.Level1) 319 { 320 StartProcess(FFRT_TEST_EXE, "100"); 321 std::string cmd = CreateCommand(OUTPUT_PATH, 10, "pid", std::to_string(ffrtPrfolerExePid_)); 322 std::string ret; 323 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>(); 324 FfrtProfilerConfig config; 325 int32_t pid = 1; 326 std::string pidStr; 327 RunCommand("pidof render_service", pidStr); 328 auto pidService = stoi(pidStr); 329 config.add_pid(pidService); 330 config.add_startup_process_name("render_service"); 331 config.set_clock_id(FfrtProfilerConfig::REALTIME_COARSE); 332 ffrtProfilerMgr->SetConfig(config); 333 ffrtProfilerMgr->Init(); 334 ffrtProfilerMgr->CheckConfig(); 335 ffrtProfilerMgr->GetFfrtProfilerCtx(pid, "test_name005"); 336 EXPECT_TRUE(RunCommand(cmd, ret)); 337 } 338 339 // /** 340 // * @tc.name: ffrt plugin 341 // * @tc.desc: FfrtPofiler RegisterAgentPlugin Function Test return false 342 // * @tc.type: FUNC 343 // */ 344 HWTEST_F(FfrtPofilerTest, TestFunction011, TestSize.Level1) 345 { 346 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>(); 347 ASSERT_TRUE(ffrtProfilerMgr != nullptr); 348 std::shared_ptr<CommandPoller> commandPoller = std::make_shared<CommandPoller>(ffrtProfilerMgr); 349 ASSERT_TRUE(commandPoller != nullptr); 350 EXPECT_FALSE(commandPoller->OnConnect()); 351 ffrtProfilerMgr->SetCommandPoller(commandPoller); 352 EXPECT_FALSE(ffrtProfilerMgr->RegisterAgentPlugin("ffrt-profiler")); 353 } 354 355 // /** 356 // * @tc.name: ffrt plugin 357 // * @tc.desc: FfrtPofiler LoadPlugin Function Test return true 358 // * @tc.type: FUNC 359 // */ 360 HWTEST_F(FfrtPofilerTest, TestFunction012, TestSize.Level1) 361 { 362 std::string cmd = CreateCommand(OUTPUT_PATH, 10, "pid", std::to_string(ffrtPrfolerExePid_)); 363 std::string ret; 364 EXPECT_TRUE(RunCommand(cmd, ret)); 365 std::string pluginPath = std::string("libffrt_profiler.z.so"); 366 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>(); 367 EXPECT_TRUE(ffrtProfilerMgr->LoadPlugin(pluginPath)); 368 } 369 370 // /** 371 // * @tc.name: ffrt plugin 372 // * @tc.desc: FfrtPofiler UnloadPlugin Function Test return true 373 // * @tc.type: FUNC 374 // */ 375 HWTEST_F(FfrtPofilerTest, TestFunction013, TestSize.Level1) 376 { 377 std::string cmd = CreateCommand(OUTPUT_PATH, 10, "pid", std::to_string(ffrtPrfolerExePid_)); 378 std::string ret; 379 EXPECT_TRUE(RunCommand(cmd, ret)); 380 std::string pluginPath = std::string("libffrt_profiler.z.so"); 381 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>(); 382 EXPECT_TRUE(ffrtProfilerMgr->UnloadPlugin(pluginPath)); 383 } 384 385 // /** 386 // * @tc.name: ffrt plugin 387 // * @tc.desc: FfrtPofiler UnloadPlugin Function Test return true 388 // * @tc.type: FUNC 389 // */ 390 HWTEST_F(FfrtPofilerTest, TestFunction014, TestSize.Level1) 391 { 392 std::string cmd = CreateCommand(OUTPUT_PATH, 10, "pid", std::to_string(ffrtPrfolerExePid_)); 393 std::string ret; 394 EXPECT_TRUE(RunCommand(cmd, ret)); 395 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>(); 396 EXPECT_TRUE(ffrtProfilerMgr->UnloadPlugin(ffrtPrfolerExePid_)); 397 } 398 399 // /** 400 // * @tc.name: ffrt plugin 401 // * @tc.desc: FfrtPofiler UnloadPlugin Function Test return true 402 // * @tc.type: FUNC 403 // */ 404 HWTEST_F(FfrtPofilerTest, TestFunction015, TestSize.Level1) 405 { 406 std::string cmd = CreateCommand(OUTPUT_PATH, 10, "pid", std::to_string(ffrtPrfolerExePid_)); 407 std::string ret; 408 EXPECT_TRUE(RunCommand(cmd, ret)); 409 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>(); 410 EXPECT_TRUE(ffrtProfilerMgr->UnloadPlugin(ffrtPrfolerExePid_)); 411 } 412 413 // /** 414 // * @tc.name: ffrt plugin 415 // * @tc.desc: systemdata test 416 // * @tc.type: FUNC 417 // */ 418 HWTEST_F(FfrtPofilerTest, TestSystemData, TestSize.Level1) 419 { 420 std::string cmd = CreateCommand(OUTPUT_PATH, 10, "pid", std::to_string(ffrtPrfolerExePid_)); 421 std::string ret; 422 EXPECT_TRUE(RunCommand(cmd, ret)); 423 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>(); 424 FfrtProfilerConfig config; 425 int32_t pid = 1; 426 config.add_pid(pid); 427 config.add_pid(ffrtPrfolerExePid_); 428 int size = config.ByteSizeLong(); 429 EXPECT_GT(size, 0); 430 } 431 432 // /** 433 // * @tc.name: ffrt plugin 434 // * @tc.desc: FfrtPofiler CheckConfig Function Test return true 435 // * @tc.type: FUNC 436 // */ 437 HWTEST_F(FfrtPofilerTest, TestFunction016, TestSize.Level1) 438 { 439 std::string cmd = CreateCommand(OUTPUT_PATH, 10, "pid", std::to_string(ffrtPrfolerExePid_)); 440 std::string ret; 441 EXPECT_TRUE(RunCommand(cmd, ret)); 442 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>(); 443 FfrtProfilerConfig config; 444 int32_t pid = -1; 445 config.add_pid(pid); 446 config.add_pid(ffrtPrfolerExePid_); 447 config.add_startup_process_name(""); 448 config.add_startup_process_name("test_name002"); 449 config.add_restart_process_name(""); 450 config.set_clock_id(FfrtProfilerConfig::MONOTONIC); 451 ffrtProfilerMgr->SetConfig(config); 452 EXPECT_TRUE(ffrtProfilerMgr->CheckConfig()); 453 } 454 455 // /** 456 // * @tc.name: ffrt plugin 457 // * @tc.desc: FfrtPofiler StopFfrtProfiler Function Test 458 // * @tc.type: FUNC 459 // */ 460 HWTEST_F(FfrtPofilerTest, TestFunction017, TestSize.Level1) 461 { 462 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>(); 463 FfrtProfilerConfig config; 464 int32_t pid = -1; 465 config.add_pid(pid); 466 config.add_pid(ffrtPrfolerExePid_); 467 config.add_startup_process_name(""); 468 config.add_startup_process_name("test_name002"); 469 config.set_clock_id(FfrtProfilerConfig::MONOTONIC); 470 ffrtProfilerMgr->SetConfig(config); 471 ffrtProfilerMgr->StopFfrtProfiler(); 472 EXPECT_EQ(ffrtProfilerMgr->ffrtCtx_.size(), 0); 473 } 474 475 // /** 476 // * @tc.name: ffrt plugin 477 // * @tc.desc: FfrtPofiler ReportPluginBasicData Function Test 478 // * @tc.type: FUNC 479 // */ 480 HWTEST_F(FfrtPofilerTest, TestFunction019, TestSize.Level1) 481 { 482 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>(); 483 std::vector<uint32_t> pluginIds = {1, 2, 3}; 484 EXPECT_TRUE(ffrtProfilerMgr->ReportPluginBasicData(pluginIds)); 485 } 486 487 // /** 488 // * @tc.name: ffrt plugin 489 // * @tc.desc: FfrtPofiler StopPluginSession Function Test 490 // * @tc.type: FUNC 491 // */ 492 HWTEST_F(FfrtPofilerTest, TestFunction020, TestSize.Level1) 493 { 494 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>(); 495 std::vector<uint32_t> pluginIds = {1, 2, 3}; 496 EXPECT_TRUE(ffrtProfilerMgr->StopPluginSession(pluginIds)); 497 } 498 499 // /** 500 // * @tc.name: ffrt plugin 501 // * @tc.desc: FfrtPofiler DestroyPluginSession Function Test 502 // * @tc.type: FUNC 503 // */ 504 HWTEST_F(FfrtPofilerTest, TestFunction021, TestSize.Level1) 505 { 506 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>(); 507 std::vector<uint32_t> pluginIds = {1, 2, 3}; 508 EXPECT_TRUE(ffrtProfilerMgr->DestroyPluginSession(pluginIds)); 509 } 510 511 // /** 512 // * @tc.name: ffrt plugin 513 // * @tc.desc: FfrtPofiler CreatePluginSession Function Test 514 // * @tc.type: FUNC 515 // */ 516 HWTEST_F(FfrtPofilerTest, TestFunction022, TestSize.Level1) 517 { 518 const uint8_t configData[] = {0x30, 0x01, 0x38, 0x01, 0x42, 0x01, 0x01}; 519 ProfilerPluginConfig ppc; 520 std::vector<ProfilerPluginConfig> config; 521 522 std::string pluginName = "ffrt-plugin"; 523 const std::vector<uint32_t> pluginIdsVector = {2}; 524 ppc.set_name(pluginName); 525 ppc.set_config_data((const void*)configData, 7); 526 config.push_back(ppc); 527 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>(); 528 EXPECT_TRUE(ffrtProfilerMgr->CreatePluginSession(config)); 529 } 530 531 // /** 532 // * @tc.name: ffrt plugin 533 // * @tc.desc: FfrtPofiler GetProcessName Function Test 534 // * @tc.type: FUNC 535 // */ 536 HWTEST_F(FfrtPofilerTest, TestFunction023, TestSize.Level1) 537 { 538 std::string pid_str; 539 RunCommand("pidof render_service", pid_str); 540 auto pid_service = stoi(pid_str); 541 auto res = GetProcessName(pid_service); 542 EXPECT_EQ(res, "render_service"); 543 } 544 545 // /** 546 // * @tc.name: ffrt plugin 547 // * @tc.desc: FfrtPofiler SplitString Function Test 548 // * @tc.type: FUNC 549 // */ 550 HWTEST_F(FfrtPofilerTest, TestFunction024, TestSize.Level1) 551 { 552 StartProcess(FFRT_TEST_EXE, "100"); 553 std::string cmd = CreateCommand(OUTPUT_PATH, 10, "pid", std::to_string(ffrtPrfolerExePid_)); 554 string str = "ffrt_plugin_test_string"; 555 string seq = "_"; 556 std::vector<string> ret; 557 SplitString(str, seq, ret); 558 EXPECT_EQ(ret.size(), 4); 559 } 560 561 // /** 562 // * @tc.name: ffrt plugin 563 // * @tc.desc: FfrtPofiler SplitString Function test string is empty 564 // * @tc.type: FUNC 565 // */ 566 HWTEST_F(FfrtPofilerTest, TestFunction025, TestSize.Level1) 567 { 568 StartProcess(FFRT_TEST_EXE, "100"); 569 std::string cmd = CreateCommand(OUTPUT_PATH, 10, "pid", std::to_string(ffrtPrfolerExePid_)); 570 string str = ""; 571 string seq = "_"; 572 std::vector<string> ret; 573 SplitString(str, seq, ret); 574 EXPECT_EQ(ret.size(), 0); 575 } 576 577 /** 578 * @tc.name: ffrt plugin 579 * @tc.desc: FfrtPofiler ProtocolProc Function Test 580 * @tc.type: FUNC 581 */ 582 HWTEST_F(FfrtPofilerTest, TestFunction026, TestSize.Level1) 583 { 584 uint64_t config = FILE_SIZE; 585 config <<= MOBILE_BIT; 586 config |= SMB_SIZE; 587 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>(); 588 std::shared_ptr<FfrtProfilerSocketService> socketService_ = 589 std::make_shared<FfrtProfilerSocketService>(ffrtProfilerMgr); 590 591 SocketContext socketContext; 592 auto ptr = reinterpret_cast<const int8_t*>(&config); 593 auto size = sizeof(uint64_t); 594 ASSERT_FALSE(socketService_->ProtocolProc(socketContext, 0, ptr, size)); 595 } 596 597 // /** 598 // * @tc.name: ffrt plugin 599 // * @tc.desc: FfrtPofiler ProtocolProc Function Test 600 // * @tc.type: FUNC 601 // */ 602 HWTEST_F(FfrtPofilerTest, TestFunction027, TestSize.Level1) 603 { 604 uint64_t config = FILE_SIZE; 605 config <<= MOBILE_BIT; 606 config |= SMB_SIZE; 607 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>(); 608 std::shared_ptr<FfrtProfilerSocketService> socketService_ = 609 std::make_shared<FfrtProfilerSocketService>(ffrtProfilerMgr); 610 611 SocketContext socketContext; 612 auto ptr = reinterpret_cast<const int8_t*>(&config); 613 auto size = sizeof(int); 614 socketService_->SetConfig(sizeof(uint64_t), sizeof(uint64_t), true, 0); 615 ASSERT_FALSE(socketService_->ProtocolProc(socketContext, 0, ptr, size)); 616 } 617 618 /** 619 * @tc.name: ffrt plugin 620 * @tc.desc: FfrtPofiler ProtocolProc Function Test 621 * @tc.type: FUNC 622 */ 623 HWTEST_F(FfrtPofilerTest, TestFunction028, TestSize.Level1) 624 { 625 uint64_t config = FILE_SIZE; 626 config <<= MOBILE_BIT; 627 config |= SMB_SIZE; 628 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>(); 629 std::shared_ptr<FfrtProfilerSocketService> socketService_ = 630 std::make_shared<FfrtProfilerSocketService>(ffrtProfilerMgr); 631 632 SocketContext socketContext; 633 auto ptr = reinterpret_cast<const int8_t*>(&config); 634 auto size = sizeof(int); 635 ASSERT_FALSE(socketService_->ProtocolProc(socketContext, 0, ptr, size)); 636 } 637 638 /** 639 * @tc.name: ffrt plugin 640 * @tc.desc: FfrtPofiler StartService Function Test 641 * @tc.type: FUNC 642 */ 643 HWTEST_F(FfrtPofilerTest, TestFunction029, TestSize.Level1) 644 { 645 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>(); 646 std::shared_ptr<FfrtProfilerSocketService> socketService_ = 647 std::make_shared<FfrtProfilerSocketService>(ffrtProfilerMgr); 648 ASSERT_FALSE(socketService_->StartService("ffrt_profiler_unix_socket")); 649 } 650 651 /** 652 * @tc.name: ffrt plugin 653 * @tc.desc: FfrtPofiler FfrtProfilerManager SerializeData Function Test 654 * @tc.type: FUNC 655 */ 656 HWTEST_F(FfrtPofilerTest, TestFunction030, TestSize.Level1) 657 { 658 std::shared_ptr<FfrtProfilerManager> ffrtProfilerMgr = std::make_shared<FfrtProfilerManager>(); 659 std::shared_ptr<FfrtProfilerHandle> handle = std::make_shared<FfrtProfilerHandle>(BUFFER_SIZE, true); 660 const int8_t data[] = {-1, 0, 1, 2, 3, 4}; 661 handle->SerializeData(data, MOBILE_BIT); 662 FfrtProfilerConfig config; 663 int32_t pid = -1; 664 config.add_pid(pid); 665 config.add_startup_process_name("test_name002"); 666 config.set_clock_id(FfrtProfilerConfig::MONOTONIC); 667 ffrtProfilerMgr->SetConfig(config); 668 EXPECT_TRUE(ffrtProfilerMgr->CheckConfig()); 669 } 670 }