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 16 #include <dlfcn.h> 17 #include <fcntl.h> 18 #include <grpcpp/grpcpp.h> 19 #include <grpcpp/health_check_service_interface.h> 20 #include <gtest/gtest.h> 21 #include <thread> 22 #include <ifaddrs.h> 23 #include <arpa/inet.h> 24 #include <vector> 25 26 #include "common_types.pb.h" 27 #include "cpu_plugin_config.pb.h" 28 #include "cpu_plugin_result.pb.h" 29 #include "diskio_plugin_result.pb.h" 30 #include "hilog_plugin_config.pb.h" 31 #include "hilog_plugin_result.pb.h" 32 #include "logging.h" 33 #include "memory_plugin_common.pb.h" 34 #include "memory_plugin_config.pb.h" 35 #include "memory_plugin_result.pb.h" 36 #include "network_plugin_config.pb.h" 37 #include "network_plugin_result.pb.h" 38 #include "plugin_module_api.h" 39 #include "plugin_service_types.pb.h" 40 #include "process_plugin_config.pb.h" 41 #include "process_plugin_result.pb.h" 42 #include "profiler_service.pb.h" 43 #include "profiler_service.grpc.pb.h" 44 45 #pragma clang optimize off 46 47 using namespace testing::ext; 48 49 namespace { 50 const std::string DEFAULT_HIPROFILERD_PATH("/system/bin/hiprofilerd"); 51 const std::string DEFAULT_HIPROFILER_PLUGINS_PATH("/system/bin/hiprofiler_plugins"); 52 const std::string WRITE_FILE = "/data/local/tmp/diskio_write_test.txt"; 53 const std::string HILOG_RESULT_FILE = "/data/local/tmp/hilog.txt"; 54 constexpr int ROUND_COUNT = 50; 55 constexpr int LOOP_COUNT = 10; 56 constexpr int SAMPLE_INTERVAL = 40; 57 constexpr int TIMEOUT_US = 3 * 1000 * 1000; 58 constexpr int FETCHDATA_TIMEOUT_US = 20 * 1000 * 1000; 59 constexpr int KEEP_SESSION_TIMEOUT_MS = 5 * 1000; 60 constexpr int KEEP_SESSION_TIME_US = KEEP_SESSION_TIMEOUT_MS * 1000; 61 constexpr int KEEP_SESSION_SLEEP_US = 3 * 1000 * 1000; 62 constexpr int ACTIVE_PROCESS_SLEEP_US = 10000; 63 const size_t MB_PER_BYTE = 0x100000; 64 constexpr int BLOCK_LEN = 100 * 1024; 65 constexpr int WRITE_KB = 100; 66 constexpr int DEFAULT_PAGES = 10; 67 constexpr int MEMINFO_SIZE = 2; 68 69 class Timer { 70 public: 71 using Clock = std::chrono::steady_clock; 72 using TimePoint = Clock::time_point; 73 Timer()74 Timer() : startTime_(Now()) {} 75 ~Timer()76 ~Timer() {} 77 ElapsedUs()78 long ElapsedUs() 79 { 80 auto currentTime = Now(); 81 return std::chrono::duration_cast<std::chrono::microseconds>(currentTime - startTime_).count(); 82 } 83 Reset()84 void Reset() 85 { 86 startTime_ = Now(); 87 } 88 89 protected: Now()90 TimePoint Now() 91 { 92 return Clock::now(); 93 } 94 95 private: 96 TimePoint startTime_; 97 }; 98 99 class ProfilerServicePerformanceTest : public ::testing::Test { 100 protected: SetUpTestCase()101 static void SetUpTestCase() {} TearDownTestCase()102 static void TearDownTestCase() {} 103 SetUp()104 void SetUp() override 105 { 106 profilerStub_ = GetProfilerServiceStub(); 107 } 108 StartServerStub(std::string name)109 void StartServerStub(std::string name) 110 { 111 if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) { 112 return; 113 } 114 int processNum = fork(); 115 if (processNum == 0) { 116 if (DEFAULT_HIPROFILERD_PATH == name) { 117 // start running hiprofilerd 118 execl(name.c_str(), nullptr, nullptr); 119 } else if (DEFAULT_HIPROFILER_PLUGINS_PATH == name) { 120 // start running hiprofiler_plugins 121 execl(name.c_str(), "/data/local/tmp/", nullptr); 122 } 123 _exit(1); 124 } else if (DEFAULT_HIPROFILERD_PATH == name) { 125 hiprofilerdPid_ = processNum; 126 } else if (DEFAULT_HIPROFILER_PLUGINS_PATH == name) { 127 hiprofilerPluginsPid_ = processNum; 128 } 129 } StartDependServerStub()130 void StartDependServerStub() 131 { 132 #ifdef COVERAGE_TEST 133 const int coverageSleepTime = 5; // sleep 5s 134 StartServerStub(DEFAULT_HIPROFILERD_PATH); 135 sleep(coverageSleepTime); // UT 覆盖率版本等待5s 136 StartServerStub(DEFAULT_HIPROFILER_PLUGINS_PATH); 137 sleep(coverageSleepTime); 138 #else 139 StartServerStub(DEFAULT_HIPROFILERD_PATH); 140 sleep(1); // 睡眠1s确保hiprofilerd进程启动之后再启动hiprofiler_plugins进程 141 StartServerStub(DEFAULT_HIPROFILER_PLUGINS_PATH); 142 sleep(1); // 睡眠1s确保hiprofiler_plugins进程可以监控到插件 143 #endif 144 } 145 StopProcessStub(int processNum)146 void StopProcessStub(int processNum) 147 { 148 std::string stopCmd = "kill " + std::to_string(processNum); 149 std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(stopCmd.c_str(), "r"), pclose); 150 } 151 StopDependServerStub()152 void StopDependServerStub() 153 { 154 std::string stopCmd = "killall " + DEFAULT_HIPROFILER_PLUGINS_PATH + " " + DEFAULT_HIPROFILERD_PATH; 155 std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(stopCmd.c_str(), "r"), pclose); 156 } 157 GetProfilerServiceStub()158 std::unique_ptr<IProfilerService::Stub> GetProfilerServiceStub() 159 { 160 auto grpcChannel = grpc::CreateChannel("127.0.0.1:50051", grpc::InsecureChannelCredentials()); 161 if (grpcChannel == nullptr) { 162 PROFILER_LOG_INFO(LOG_CORE, "Create GRPC channel failed!"); 163 return nullptr; 164 } 165 return IProfilerService::NewStub(grpcChannel); 166 } 167 SetMemPluginConfig(CreateSessionRequest & createRequest,int requstMemoryPid,bool isFirstConfig)168 void SetMemPluginConfig(CreateSessionRequest& createRequest, int requstMemoryPid, bool isFirstConfig) 169 { 170 ProfilerPluginConfig* ppc = createRequest.add_plugin_configs(); 171 ppc->set_name("memory-plugin"); 172 ppc->set_sample_interval(SAMPLE_INTERVAL); 173 MemoryConfig mc; 174 mc.set_report_process_mem_info(true); 175 mc.set_report_app_mem_info(isFirstConfig); 176 mc.set_report_sysmem_mem_info(true); 177 mc.add_sys_meminfo_counters(SysMeminfoType::PMEM_MEM_TOTAL); 178 mc.add_sys_meminfo_counters(SysMeminfoType::PMEM_MEM_FREE); 179 mc.set_report_sysmem_vmem_info(true); 180 mc.add_sys_vmeminfo_counters(SysVMeminfoType::VMEMINFO_NR_FREE_PAGES); 181 mc.add_sys_vmeminfo_counters(SysVMeminfoType::VMEMINFO_NR_ALLOC_BATCH); 182 mc.add_pid(idlePid_); 183 if (isFirstConfig) { 184 mc.add_pid(requstMemoryPid); 185 } 186 std::vector<uint8_t> configData(mc.ByteSizeLong()); 187 mc.SerializeToArray(configData.data(), configData.size()); 188 ppc->set_config_data((const void*)configData.data(), configData.size()); 189 auto sessionConfig = createRequest.mutable_session_config(); 190 ProfilerSessionConfig::BufferConfig* bfs = sessionConfig->add_buffers(); 191 bfs->set_pages(DEFAULT_PAGES); 192 bfs->set_policy(ProfilerSessionConfig::BufferConfig::RECYCLE); 193 sessionConfig->set_session_mode(ProfilerSessionConfig::ONLINE); 194 } 195 SetCpuPluginConfig(CreateSessionRequest & createRequest,int pid)196 void SetCpuPluginConfig(CreateSessionRequest& createRequest, int pid) 197 { 198 ProfilerPluginConfig* ppc = createRequest.add_plugin_configs(); 199 ppc->set_name("cpu-plugin"); 200 ppc->set_sample_interval(SAMPLE_INTERVAL); 201 CpuConfig cc; 202 cc.set_pid(pid); 203 std::vector<uint8_t> configData(cc.ByteSizeLong()); 204 cc.SerializeToArray(configData.data(), configData.size()); 205 ppc->set_config_data((const void*)configData.data(), configData.size()); 206 auto sessionConfig = createRequest.mutable_session_config(); 207 ProfilerSessionConfig::BufferConfig* bfs = sessionConfig->add_buffers(); 208 bfs->set_pages(DEFAULT_PAGES); 209 bfs->set_policy(ProfilerSessionConfig::BufferConfig::RECYCLE); 210 sessionConfig->set_session_mode(ProfilerSessionConfig::ONLINE); 211 } 212 SetDiskioPluginConfig(CreateSessionRequest & createRequest)213 void SetDiskioPluginConfig(CreateSessionRequest& createRequest) 214 { 215 ProfilerPluginConfig* ppc = createRequest.add_plugin_configs(); 216 ppc->set_name("diskio-plugin"); 217 ppc->set_sample_interval(SAMPLE_INTERVAL); 218 auto sessionConfig = createRequest.mutable_session_config(); 219 ProfilerSessionConfig::BufferConfig* bfs = sessionConfig->add_buffers(); 220 bfs->set_pages(DEFAULT_PAGES); 221 bfs->set_policy(ProfilerSessionConfig::BufferConfig::RECYCLE); 222 sessionConfig->set_session_mode(ProfilerSessionConfig::ONLINE); 223 } 224 SetProcessPluginConfig(CreateSessionRequest & createRequest)225 void SetProcessPluginConfig(CreateSessionRequest& createRequest) 226 { 227 ProfilerPluginConfig* ppc = createRequest.add_plugin_configs(); 228 ppc->set_name("process-plugin"); 229 ppc->set_sample_interval(SAMPLE_INTERVAL); 230 ProcessConfig pc; 231 pc.set_report_process_tree(true); 232 std::vector<uint8_t> configData(pc.ByteSizeLong()); 233 pc.SerializeToArray(configData.data(), configData.size()); 234 ppc->set_config_data((const void*)configData.data(), configData.size()); 235 auto sessionConfig = createRequest.mutable_session_config(); 236 ProfilerSessionConfig::BufferConfig* bfs = sessionConfig->add_buffers(); 237 bfs->set_pages(DEFAULT_PAGES); 238 bfs->set_policy(ProfilerSessionConfig::BufferConfig::RECYCLE); 239 sessionConfig->set_session_mode(ProfilerSessionConfig::ONLINE); 240 } 241 SetHilogPluginConfig(CreateSessionRequest & createRequest,bool isFirstConfig)242 void SetHilogPluginConfig(CreateSessionRequest& createRequest, bool isFirstConfig) 243 { 244 ProfilerPluginConfig* ppc = createRequest.add_plugin_configs(); 245 ppc->set_name("hilog-plugin"); 246 ppc->set_sample_interval(SAMPLE_INTERVAL); 247 HilogConfig hc; 248 hc.set_need_record(true); 249 hc.set_need_clear(true); 250 std::vector<uint8_t> configData(hc.ByteSizeLong()); 251 hc.SerializeToArray(configData.data(), configData.size()); 252 ppc->set_config_data((const void*)configData.data(), configData.size()); 253 auto sessionConfig = createRequest.mutable_session_config(); 254 ProfilerSessionConfig::BufferConfig* bfs = sessionConfig->add_buffers(); 255 bfs->set_pages(DEFAULT_PAGES); 256 bfs->set_policy(ProfilerSessionConfig::BufferConfig::RECYCLE); 257 if (isFirstConfig) { 258 sessionConfig->set_session_mode(ProfilerSessionConfig::ONLINE); 259 } else { 260 sessionConfig->set_session_mode(ProfilerSessionConfig::OFFLINE); 261 sessionConfig->set_result_file(HILOG_RESULT_FILE); 262 } 263 } 264 SetNetworkPluginConfig(CreateSessionRequest & createRequest)265 void SetNetworkPluginConfig(CreateSessionRequest& createRequest) 266 { 267 ProfilerPluginConfig* ppc = createRequest.add_plugin_configs(); 268 ppc->set_name("network-plugin"); 269 ppc->set_sample_interval(SAMPLE_INTERVAL); 270 NetworkConfig nc; 271 nc.add_pid(1); 272 std::vector<uint8_t> configData(nc.ByteSizeLong()); 273 nc.SerializeToArray(configData.data(), configData.size()); 274 ppc->set_config_data((const void*)configData.data(), configData.size()); 275 auto sessionConfig = createRequest.mutable_session_config(); 276 ProfilerSessionConfig::BufferConfig* bfs = sessionConfig->add_buffers(); 277 bfs->set_pages(DEFAULT_PAGES); 278 bfs->set_policy(ProfilerSessionConfig::BufferConfig::RECYCLE); 279 sessionConfig->set_session_mode(ProfilerSessionConfig::ONLINE); 280 } 281 isExistSpecifyPlugin(GetCapabilitiesResponse & capResponse,std::string pluginName)282 bool isExistSpecifyPlugin(GetCapabilitiesResponse& capResponse, std::string pluginName) 283 { 284 bool isExistPlugin = false; 285 for (int i = 0; i < capResponse.capabilities().size(); i++) { 286 ProfilerPluginCapability ppc = capResponse.capabilities()[i]; 287 if (ppc.name() == pluginName) { 288 pluginVec_.push_back(ppc.name()); 289 isExistPlugin = true; 290 } 291 } 292 return isExistPlugin; 293 } 294 GetPluginCapabilities(int count,GetCapabilitiesResponse & capResponse)295 bool GetPluginCapabilities(int count, GetCapabilitiesResponse& capResponse) 296 { 297 grpc::Status status; 298 for (int i = 0; i < count; i++) { 299 GetCapabilitiesRequest request; 300 request.set_request_id(0); 301 grpc::ClientContext context; 302 status = profilerStub_->GetCapabilities(&context, request, &capResponse); 303 if (!status.ok()) { 304 return false; 305 } 306 } 307 return true; 308 } 309 CreatePluginSession(uint32_t & sessionId,int pid,bool isFirstConfig=true)310 bool CreatePluginSession(uint32_t& sessionId, int pid, bool isFirstConfig = true) 311 { 312 CreateSessionResponse createResponse; 313 grpc::ClientContext createContext; 314 CreateSessionRequest createRequest = CreateSessionRequest(); 315 createRequest.set_request_id(0); 316 for (int i = 0; i < pluginVec_.size(); i++) { 317 if (pluginVec_[i] == "memory-plugin") { 318 SetMemPluginConfig(createRequest, pid, isFirstConfig); 319 } else if (pluginVec_[i] == "cpu-plugin") { 320 SetCpuPluginConfig(createRequest, pid); 321 } else if (pluginVec_[i] == "diskio-plugin") { 322 SetDiskioPluginConfig(createRequest); 323 } else if (pluginVec_[i] == "process-plugin") { 324 SetProcessPluginConfig(createRequest); 325 } else if (pluginVec_[i] == "hilog-plugin") { 326 SetHilogPluginConfig(createRequest, isFirstConfig); 327 } else if (pluginVec_[i] == "network-plugin") { 328 SetNetworkPluginConfig(createRequest); 329 } 330 } 331 grpc::Status createStatus = profilerStub_->CreateSession(&createContext, createRequest, &createResponse); 332 sessionId = createResponse.session_id(); 333 return createStatus.ok(); 334 } 335 KeepPluginSession(int count,uint32_t sessionId)336 bool KeepPluginSession(int count, uint32_t sessionId) 337 { 338 grpc::Status status; 339 for (int i = 0; i < count; i++) { 340 KeepSessionRequest request; 341 request.set_request_id(0); 342 request.set_session_id(sessionId); 343 request.set_keep_alive_time(KEEP_SESSION_TIMEOUT_MS); 344 grpc::ClientContext context; 345 KeepSessionResponse ksResponse; 346 status = profilerStub_->KeepSession(&context, request, &ksResponse); 347 if (!status.ok()) { 348 return false; 349 } 350 } 351 return true; 352 } 353 StartPluginSession(uint32_t sessionId)354 bool StartPluginSession(uint32_t sessionId) 355 { 356 StartSessionResponse startResponse; 357 grpc::ClientContext startContext; 358 StartSessionRequest startRequest = StartSessionRequest(); 359 startRequest.set_request_id(0); 360 startRequest.set_session_id(sessionId); 361 grpc::Status startStatus = profilerStub_->StartSession(&startContext, startRequest, &startResponse); 362 return startStatus.ok(); 363 } 364 StopPluginSession(uint32_t sessionId)365 bool StopPluginSession(uint32_t sessionId) 366 { 367 StopSessionResponse stopResponse; 368 grpc::ClientContext stopContext; 369 StopSessionRequest stopRequest = StopSessionRequest(); 370 stopRequest.set_request_id(0); 371 stopRequest.set_session_id(sessionId); 372 grpc::Status stopStatus = profilerStub_->StopSession(&stopContext, stopRequest, &stopResponse); 373 return stopStatus.ok(); 374 } 375 DestroyPluginSession(uint32_t sessionId)376 bool DestroyPluginSession(uint32_t sessionId) 377 { 378 DestroySessionResponse destroyResponse; 379 grpc::ClientContext destroyContext; 380 DestroySessionRequest destroyRequest = DestroySessionRequest(); 381 destroyRequest.set_request_id(0); 382 destroyRequest.set_session_id(sessionId); 383 grpc::Status destroyStatus = profilerStub_->DestroySession(&destroyContext, destroyRequest, &destroyResponse); 384 return destroyStatus.ok(); 385 } 386 StartActiveProcess(int & cpuActivePid)387 bool StartActiveProcess(int& cpuActivePid) 388 { 389 if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) { 390 return false; 391 } 392 int processNum = fork(); 393 if (processNum == 0) { 394 // consume cpu 395 CpuConfig protoConfig; 396 void* handle = dlopen("libcpudataplugin.z.so", RTLD_LAZY); 397 if (handle == nullptr) { 398 const int bufSize = 256; 399 char buf[bufSize] = { 0 }; 400 strerror_r(errno, buf, bufSize); 401 PROFILER_LOG_ERROR(LOG_CORE, "test:dlopen err, errno(%d:%s)", errno, buf); 402 return false; 403 } 404 405 PluginModuleStruct* cpuPlugin = (PluginModuleStruct*)dlsym(handle, "g_pluginModule"); 406 if (cpuPlugin == nullptr) { 407 PROFILER_LOG_ERROR(LOG_CORE, "test: cpuPlugin is null"); 408 return false; 409 } 410 411 // Serialize config 412 protoConfig.set_pid(getpid()); 413 int configLength = protoConfig.ByteSizeLong(); 414 std::vector<uint8_t> configBuffer(configLength); 415 protoConfig.SerializeToArray(configBuffer.data(), configLength); 416 417 // run plugin 418 std::vector<uint8_t> dataBuffer(cpuPlugin->resultBufferSizeHint); 419 cpuPlugin->callbacks->onPluginSessionStart(configBuffer.data(), configLength); 420 421 // 循环上报数据消耗cpu 422 while (1) { 423 int len = cpuPlugin->callbacks->onPluginReportResult(dataBuffer.data(), 424 cpuPlugin->resultBufferSizeHint); 425 if (len > 0) { 426 CpuData cpuData; 427 cpuData.ParseFromArray(dataBuffer.data(), len); 428 } 429 usleep(ACTIVE_PROCESS_SLEEP_US); 430 } 431 432 cpuPlugin->callbacks->onPluginSessionStop(); 433 } else { 434 cpuActivePid = processNum; 435 } 436 return true; 437 } 438 StartIdleProcess()439 void StartIdleProcess() 440 { 441 if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) { 442 return; 443 } 444 int processNum = fork(); 445 if (processNum == 0) { 446 // not to do 447 while (1) { 448 sleep(1); 449 } 450 } else { 451 idlePid_ = processNum; 452 } 453 } 454 StartRequestMemoryProcess(int & requestMemoryPid)455 void StartRequestMemoryProcess(int& requestMemoryPid) 456 { 457 if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) { 458 return; 459 } 460 int processNum = fork(); 461 if (processNum == 0) { 462 auto requestBuff = std::make_unique<char[]>(MB_PER_BYTE); 463 if (requestBuff == nullptr) { 464 const int bufSize = 256; 465 char buf[bufSize] = { 0 }; 466 strerror_r(errno, buf, bufSize); 467 PROFILER_LOG_ERROR(LOG_CORE, "StartRequestMemoryProcess: malloc %zu fail, errno(%d:%s)", 468 MB_PER_BYTE, errno, buf); 469 } 470 while (1) { 471 sleep(1); 472 } 473 } else { 474 requestMemoryPid = processNum; 475 } 476 } 477 IoTest()478 void IoTest() 479 { 480 // 一次累加16B,直至100KB 481 std::string str = ""; 482 while (str.length() < BLOCK_LEN) { 483 str += "this is IO test."; 484 } 485 486 // 写100K数据 487 FILE* writeFp = fopen(WRITE_FILE.c_str(), "w"); 488 if (writeFp == nullptr) { 489 PROFILER_LOG_ERROR(LOG_CORE, "fopen() error"); 490 return; 491 } 492 size_t len = fwrite(const_cast<char*>(str.c_str()), 1, BLOCK_LEN, writeFp); 493 if (len < 0) { 494 PROFILER_LOG_ERROR(LOG_CORE, "fwrite() error"); 495 if (fclose(writeFp) != 0) { 496 PROFILER_LOG_ERROR(LOG_CORE, "fclose() error"); 497 } 498 return; 499 } 500 int ret = fflush(writeFp); 501 if (ret == EOF) { 502 PROFILER_LOG_ERROR(LOG_CORE, "fflush() error"); 503 if (fclose(writeFp) != 0) { 504 PROFILER_LOG_ERROR(LOG_CORE, "fclose() error"); 505 } 506 return; 507 } 508 fsync(fileno(writeFp)); 509 ret = fclose(writeFp); 510 if (ret != 0) { 511 PROFILER_LOG_ERROR(LOG_CORE, "fclose() error"); 512 return; 513 } 514 515 // delete file 516 std::string command = "rm " + WRITE_FILE; 517 system(command.c_str()); 518 } StartTestDiskioProcess(int & diskioPid)519 void StartTestDiskioProcess(int& diskioPid) 520 { 521 if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) { 522 return; 523 } 524 int processNum = fork(); 525 if (processNum == 0) { 526 IoTest(); 527 while (1) { 528 sleep(1); 529 } 530 } else { 531 diskioPid = processNum; 532 } 533 } 534 535 private: 536 std::unique_ptr<IProfilerService::Stub> profilerStub_; 537 std::vector<std::string> pluginVec_; 538 int hiprofilerdPid_ = -1; 539 int hiprofilerPluginsPid_ = -1; 540 pid_t idlePid_ = -1; 541 }; 542 543 /** 544 * @tc.name: server 545 * @tc.desc: RPC interface performance for memory_plugin normal test. 546 * @tc.type: FUNC 547 */ 548 HWTEST_F(ProfilerServicePerformanceTest, DFX_DFR_Hiprofiler_0010, Function | MediumTest | Level1) 549 { 550 StopDependServerStub(); 551 ASSERT_TRUE(profilerStub_ != nullptr); 552 int requestMemoryPid = -1; 553 StartIdleProcess(); 554 StartRequestMemoryProcess(requestMemoryPid); 555 556 StartDependServerStub(); 557 Timer timer = {}; 558 GetCapabilitiesResponse capResponse; 559 EXPECT_TRUE(GetPluginCapabilities(ROUND_COUNT, capResponse)); 560 auto timeCost = timer.ElapsedUs(); 561 printf("GetCapabilities %d time cost %ldus.\n", ROUND_COUNT, timeCost); 562 EXPECT_LE(timeCost, TIMEOUT_US); 563 564 ASSERT_TRUE(isExistSpecifyPlugin(capResponse, "memory-plugin")); 565 566 timer.Reset(); 567 uint32_t sessionId1; 568 EXPECT_TRUE(CreatePluginSession(sessionId1, requestMemoryPid)); 569 timeCost = timer.ElapsedUs(); 570 printf("CreateSession cost %ldus.\n", timeCost); 571 EXPECT_LE(timeCost, TIMEOUT_US); 572 573 // KeepSession 574 timer.Reset(); 575 EXPECT_TRUE(KeepPluginSession(ROUND_COUNT, sessionId1)); 576 timeCost = timer.ElapsedUs(); 577 printf("KeepSession %d time cost %ldus.\n", ROUND_COUNT, timeCost); 578 EXPECT_LE(timeCost, TIMEOUT_US); 579 580 // 开启每隔4s发一次心跳的线程,确保seesionid不会失效 581 bool sendHeart = true; 582 int loopCount = 0; __anonaf86cf320202() 583 std::thread keepSessionThread1([&]() { 584 while (sendHeart && loopCount < LOOP_COUNT) { 585 ++loopCount; 586 KeepSessionRequest request; 587 request.set_request_id(0); 588 request.set_session_id(sessionId1); 589 request.set_keep_alive_time(KEEP_SESSION_TIMEOUT_MS); 590 grpc::ClientContext context; 591 KeepSessionResponse ksResponse; 592 grpc::Status status = profilerStub_->KeepSession(&context, request, &ksResponse); 593 if (sendHeart) { 594 EXPECT_TRUE(status.ok()); 595 } 596 usleep(KEEP_SESSION_SLEEP_US); 597 } 598 }); 599 600 // StartSession 601 timer.Reset(); 602 EXPECT_TRUE(StartPluginSession(sessionId1)); 603 timeCost = timer.ElapsedUs(); 604 printf("StartSession cost %ldus.\n", timeCost); 605 EXPECT_LE(timeCost, TIMEOUT_US); 606 607 // FetchData 608 timer.Reset(); 609 std::vector<CpuData> cpuDataVec1; 610 std::unique_ptr<grpc::ClientReader<FetchDataResponse>> fetchResponse = nullptr; 611 grpc::ClientContext fetchContext; 612 FetchDataRequest fetchRequest = FetchDataRequest(); 613 fetchRequest.set_request_id(0); 614 fetchRequest.set_session_id(sessionId1); 615 fetchResponse = profilerStub_->FetchData(&fetchContext, fetchRequest); 616 ASSERT_TRUE(fetchResponse != nullptr); __anonaf86cf320302() 617 std::thread fetchDataThread1([&]() { 618 int ict = 0; 619 while (1) { 620 FetchDataResponse rp; 621 fetchResponse.get()->Read(&rp); 622 for (int i = 0; i < rp.plugin_data().size(); i++) { 623 ProfilerPluginData pd = rp.plugin_data(i); 624 if (ict == ROUND_COUNT) { 625 return; 626 } 627 628 ASSERT_STREQ(pd.name().c_str(), "memory-plugin"); 629 630 MemoryData md; 631 auto& data = pd.data(); 632 md.ParseFromArray(data.data(), data.size()); 633 EXPECT_LE(md.processesinfo(0).vm_size_kb(), md.processesinfo(1).vm_size_kb()); 634 635 EXPECT_GT(md.processesinfo(0).pid(), 0); 636 EXPECT_GT(md.processesinfo(1).pid(), 0); 637 EXPECT_STRNE(md.processesinfo(0).name().c_str(), ""); 638 EXPECT_STRNE(md.processesinfo(1).name().c_str(), ""); 639 EXPECT_GE(md.processesinfo(0).vm_size_kb(), 0); 640 EXPECT_GE(md.processesinfo(1).vm_size_kb(), 0); 641 EXPECT_GE(md.processesinfo(0).vm_rss_kb(), 0); 642 EXPECT_GE(md.processesinfo(1).vm_rss_kb(), 0); 643 EXPECT_GE(md.processesinfo(0).rss_anon_kb(), 0); 644 EXPECT_GE(md.processesinfo(1).rss_anon_kb(), 0); 645 EXPECT_GE(md.processesinfo(0).rss_file_kb(), 0); 646 EXPECT_GE(md.processesinfo(1).rss_file_kb(), 0); 647 EXPECT_GE(md.processesinfo(0).rss_shmem_kb(), 0); 648 EXPECT_GE(md.processesinfo(1).rss_shmem_kb(), 0); 649 EXPECT_GE(md.processesinfo(0).vm_swap_kb(), 0); 650 EXPECT_GE(md.processesinfo(1).vm_swap_kb(), 0); 651 EXPECT_GE(md.processesinfo(0).vm_locked_kb(), 0); 652 EXPECT_GE(md.processesinfo(1).vm_locked_kb(), 0); 653 EXPECT_GE(md.processesinfo(0).vm_hwm_kb(), 0); 654 EXPECT_GE(md.processesinfo(1).vm_hwm_kb(), 0); 655 656 EXPECT_TRUE(md.processesinfo(0).has_memsummary()); 657 EXPECT_GE(md.processesinfo(0).memsummary().java_heap(), 0); 658 EXPECT_GE(md.processesinfo(1).memsummary().java_heap(), 0); 659 EXPECT_GE(md.processesinfo(0).memsummary().native_heap(), 0); 660 EXPECT_GE(md.processesinfo(1).memsummary().native_heap(), 0); 661 EXPECT_GE(md.processesinfo(0).memsummary().code(), 0); 662 EXPECT_GE(md.processesinfo(1).memsummary().code(), 0); 663 EXPECT_GE(md.processesinfo(0).memsummary().stack(), 0); 664 EXPECT_GE(md.processesinfo(1).memsummary().stack(), 0); 665 EXPECT_GE(md.processesinfo(0).memsummary().graphics(), 0); 666 EXPECT_GE(md.processesinfo(1).memsummary().graphics(), 0); 667 EXPECT_GE(md.processesinfo(0).memsummary().private_other(), 0); 668 EXPECT_GE(md.processesinfo(1).memsummary().private_other(), 0); 669 EXPECT_GE(md.processesinfo(0).memsummary().system(), 0); 670 EXPECT_GE(md.processesinfo(1).memsummary().system(), 0); 671 672 EXPECT_EQ(md.meminfo().size(), MEMINFO_SIZE); 673 EXPECT_EQ(md.meminfo(0).key(), SysMeminfoType::PMEM_MEM_TOTAL); 674 EXPECT_GE(md.meminfo(0).value(), 0); 675 EXPECT_EQ(md.meminfo(1).key(), SysMeminfoType::PMEM_MEM_FREE); 676 EXPECT_GE(md.meminfo(1).value(), 0); 677 678 EXPECT_EQ(md.vmeminfo().size(), 1); 679 EXPECT_EQ(md.vmeminfo(0).key(), SysVMeminfoType::VMEMINFO_NR_FREE_PAGES); 680 EXPECT_GE(md.vmeminfo(0).value(), 0); 681 682 ict += 1; 683 } 684 } 685 }); 686 fetchDataThread1.join(); 687 timeCost = timer.ElapsedUs(); 688 printf("FetchData cost %ldus.\n", timeCost); 689 EXPECT_LE(timeCost, FETCHDATA_TIMEOUT_US); 690 691 // StopSession 692 timer.Reset(); 693 EXPECT_TRUE(StopPluginSession(sessionId1)); 694 timeCost = timer.ElapsedUs(); 695 printf("StopSession cost %ldus.\n", timeCost); 696 EXPECT_LE(timeCost, TIMEOUT_US); 697 698 // DestroySession 699 timer.Reset(); 700 EXPECT_TRUE(DestroyPluginSession(sessionId1)); 701 timeCost = timer.ElapsedUs(); 702 printf("DestroySession cost %ldus.\n", timeCost); 703 EXPECT_LE(timeCost, TIMEOUT_US); 704 705 // 销毁会话之后停止心跳发送 706 sendHeart = false; 707 keepSessionThread1.join(); 708 709 // 创建不同的memory config 710 uint32_t sessionId2; 711 EXPECT_TRUE(CreatePluginSession(sessionId2, idlePid_, false)); 712 EXPECT_TRUE(KeepPluginSession(ROUND_COUNT, sessionId2)); 713 714 // 开启每隔4s发一次心跳的线程,确保seesionid不会失效 715 bool sendHeart2 = true; 716 loopCount = 0; __anonaf86cf320402() 717 std::thread keepSessionThread2([&]() { 718 while (sendHeart2 && loopCount < LOOP_COUNT) { 719 ++loopCount; 720 KeepSessionRequest request; 721 request.set_request_id(0); 722 request.set_session_id(sessionId2); 723 request.set_keep_alive_time(KEEP_SESSION_TIMEOUT_MS); 724 grpc::ClientContext context; 725 KeepSessionResponse ksResponse; 726 grpc::Status status = profilerStub_->KeepSession(&context, request, &ksResponse); 727 if (sendHeart2) { 728 EXPECT_TRUE(status.ok()); 729 } 730 usleep(KEEP_SESSION_SLEEP_US); 731 } 732 }); 733 734 EXPECT_TRUE(StartPluginSession(sessionId2)); 735 736 // FetchData 737 std::vector<CpuData> cpuDataVec2; 738 std::unique_ptr<grpc::ClientReader<FetchDataResponse>> fetchResponse2 = nullptr; 739 grpc::ClientContext fetchContext2; 740 FetchDataRequest fetchRequest2 = FetchDataRequest(); 741 fetchRequest2.set_request_id(0); 742 fetchRequest2.set_session_id(sessionId2); 743 fetchResponse2 = profilerStub_->FetchData(&fetchContext2, fetchRequest2); 744 ASSERT_TRUE(fetchResponse2 != nullptr); __anonaf86cf320502() 745 std::thread fetchDataThread2([&]() { 746 int ict = 0; 747 while (1) { 748 FetchDataResponse rp; 749 fetchResponse2.get()->Read(&rp); 750 for (int i = 0; i < rp.plugin_data().size(); i++) { 751 ProfilerPluginData pd = rp.plugin_data(i); 752 if (ict == ROUND_COUNT) { 753 return; 754 } 755 756 ASSERT_STREQ(pd.name().c_str(), "memory-plugin"); 757 758 MemoryData md; 759 auto& data = pd.data(); 760 md.ParseFromArray(data.data(), data.size()); 761 EXPECT_FALSE(md.processesinfo(0).has_memsummary()); 762 763 ict += 1; 764 } 765 } 766 }); 767 fetchDataThread2.join(); 768 EXPECT_TRUE(StopPluginSession(sessionId2)); 769 EXPECT_TRUE(DestroyPluginSession(sessionId2)); 770 // 销毁会话之后停止心跳发送 771 sendHeart2 = false; 772 keepSessionThread2.join(); 773 774 StopProcessStub(hiprofilerPluginsPid_); 775 StopProcessStub(hiprofilerdPid_); 776 StopProcessStub(idlePid_); 777 StopProcessStub(requestMemoryPid); 778 779 pluginVec_.clear(); 780 } 781 782 /** 783 * @tc.name: server 784 * @tc.desc: RPC interface performance for cpu_plugin normal test. 785 * @tc.type: FUNC 786 */ 787 HWTEST_F(ProfilerServicePerformanceTest, DFX_DFR_Hiprofiler_0020, Function | MediumTest | Level1) 788 { 789 StopDependServerStub(); 790 ASSERT_TRUE(profilerStub_ != nullptr); 791 StartIdleProcess(); 792 793 StartDependServerStub(); 794 Timer timer = {}; 795 GetCapabilitiesResponse capResponse; 796 EXPECT_TRUE(GetPluginCapabilities(ROUND_COUNT, capResponse)); 797 auto timeCost = timer.ElapsedUs(); 798 printf("GetCapabilities %d time cost %ldus.\n", ROUND_COUNT, timeCost); 799 EXPECT_LE(timeCost, TIMEOUT_US); 800 801 ASSERT_TRUE(isExistSpecifyPlugin(capResponse, "cpu-plugin")); 802 803 timer.Reset(); 804 uint32_t sessionId1; 805 EXPECT_TRUE(CreatePluginSession(sessionId1, idlePid_)); 806 timeCost = timer.ElapsedUs(); 807 printf("CreateSession cost %ldus.\n", timeCost); 808 EXPECT_LE(timeCost, TIMEOUT_US); 809 810 // KeepSession 811 timer.Reset(); 812 EXPECT_TRUE(KeepPluginSession(ROUND_COUNT, sessionId1)); 813 timeCost = timer.ElapsedUs(); 814 printf("KeepSession %d time cost %ldus.\n", ROUND_COUNT, timeCost); 815 EXPECT_LE(timeCost, TIMEOUT_US); 816 817 // 开启每隔4s发一次心跳的线程,确保seesionid不会失效 818 bool sendHeart = true; 819 int loopCount = 0; __anonaf86cf320602() 820 std::thread keepSessionThread1([&]() { 821 while (sendHeart && loopCount < LOOP_COUNT) { 822 ++loopCount; 823 KeepSessionRequest request; 824 request.set_request_id(0); 825 request.set_session_id(sessionId1); 826 request.set_keep_alive_time(KEEP_SESSION_TIMEOUT_MS); 827 grpc::ClientContext context; 828 KeepSessionResponse ksResponse; 829 grpc::Status status = profilerStub_->KeepSession(&context, request, &ksResponse); 830 if (sendHeart) { 831 EXPECT_TRUE(status.ok()); 832 } 833 usleep(KEEP_SESSION_SLEEP_US); 834 } 835 }); 836 837 // StartSession 838 timer.Reset(); 839 EXPECT_TRUE(StartPluginSession(sessionId1)); 840 timeCost = timer.ElapsedUs(); 841 printf("StartSession cost %ldus.\n", timeCost); 842 EXPECT_LE(timeCost, TIMEOUT_US); 843 844 // FetchData 845 timer.Reset(); 846 std::vector<CpuData> cpuDataVec1; 847 std::unique_ptr<grpc::ClientReader<FetchDataResponse>> fetchResponse = nullptr; 848 grpc::ClientContext fetchContext; 849 FetchDataRequest fetchRequest = FetchDataRequest(); 850 fetchRequest.set_request_id(0); 851 fetchRequest.set_session_id(sessionId1); 852 fetchResponse = profilerStub_->FetchData(&fetchContext, fetchRequest); 853 ASSERT_TRUE(fetchResponse != nullptr); __anonaf86cf320702() 854 std::thread fetchDataThread1([&]() { 855 int ict = 0; 856 while (1) { 857 FetchDataResponse rp; 858 fetchResponse.get()->Read(&rp); 859 for (int i = 0; i < rp.plugin_data().size(); i++) { 860 ProfilerPluginData pd = rp.plugin_data(i); 861 if (ict == ROUND_COUNT) { 862 return; 863 } 864 865 ASSERT_STREQ(pd.name().c_str(), "cpu-plugin"); 866 867 CpuData cd; 868 auto& data = pd.data(); 869 cd.ParseFromArray(data.data(), data.size()); 870 cpuDataVec1.push_back(cd); 871 872 ict += 1; 873 } 874 } 875 }); 876 fetchDataThread1.join(); 877 timeCost = timer.ElapsedUs(); 878 printf("FetchData cost %ldus.\n", timeCost); 879 EXPECT_LE(timeCost, FETCHDATA_TIMEOUT_US); 880 881 // StopSession 882 timer.Reset(); 883 EXPECT_TRUE(StopPluginSession(sessionId1)); 884 timeCost = timer.ElapsedUs(); 885 printf("StopSession cost %ldus.\n", timeCost); 886 EXPECT_LE(timeCost, TIMEOUT_US); 887 888 // DestroySession 889 timer.Reset(); 890 EXPECT_TRUE(DestroyPluginSession(sessionId1)); 891 timeCost = timer.ElapsedUs(); 892 printf("DestroySession cost %ldus.\n", timeCost); 893 EXPECT_LE(timeCost, TIMEOUT_US); 894 895 // 销毁会话之后停止心跳发送 896 sendHeart = false; 897 keepSessionThread1.join(); 898 899 // 开启活跃进程获取数据 900 int cpuActivePid = -1; 901 StartActiveProcess(cpuActivePid); 902 //睡眠一秒确保子进程开始工作 903 sleep(1); 904 uint32_t sessionId2; 905 EXPECT_TRUE(CreatePluginSession(sessionId2, cpuActivePid)); 906 EXPECT_TRUE(KeepPluginSession(ROUND_COUNT, sessionId2)); 907 908 // 开启每隔4s发一次心跳的线程,确保seesionid不会失效 909 bool sendHeart2 = true; 910 loopCount = 0; __anonaf86cf320802() 911 std::thread keepSessionThread2([&]() { 912 while (sendHeart2 && loopCount < LOOP_COUNT) { 913 ++loopCount; 914 KeepSessionRequest request; 915 request.set_request_id(0); 916 request.set_session_id(sessionId2); 917 request.set_keep_alive_time(KEEP_SESSION_TIMEOUT_MS); 918 grpc::ClientContext context; 919 KeepSessionResponse ksResponse; 920 grpc::Status status = profilerStub_->KeepSession(&context, request, &ksResponse); 921 if (sendHeart2) { 922 EXPECT_TRUE(status.ok()); 923 } 924 usleep(KEEP_SESSION_SLEEP_US); 925 } 926 }); 927 928 EXPECT_TRUE(StartPluginSession(sessionId2)); 929 930 // FetchData 931 std::vector<CpuData> cpuDataVec2; 932 std::unique_ptr<grpc::ClientReader<FetchDataResponse>> fetchResponse2 = nullptr; 933 grpc::ClientContext fetchContext2; 934 FetchDataRequest fetchRequest2 = FetchDataRequest(); 935 fetchRequest2.set_request_id(0); 936 fetchRequest2.set_session_id(sessionId2); 937 fetchResponse2 = profilerStub_->FetchData(&fetchContext2, fetchRequest2); 938 ASSERT_TRUE(fetchResponse2 != nullptr); __anonaf86cf320902() 939 std::thread fetchDataThread2([&]() { 940 int ict = 0; 941 while (1) { 942 FetchDataResponse rp; 943 fetchResponse2.get()->Read(&rp); 944 for (int i = 0; i < rp.plugin_data().size(); i++) { 945 ProfilerPluginData pd = rp.plugin_data(i); 946 if (ict == ROUND_COUNT) { 947 return; 948 } 949 950 ASSERT_STREQ(pd.name().c_str(), "cpu-plugin"); 951 952 CpuData cd; 953 auto& data = pd.data(); 954 cd.ParseFromArray(data.data(), data.size()); 955 cpuDataVec2.push_back(cd); 956 957 ict += 1; 958 } 959 } 960 }); 961 fetchDataThread2.join(); 962 EXPECT_TRUE(StopPluginSession(sessionId2)); 963 EXPECT_TRUE(DestroyPluginSession(sessionId2)); 964 // 销毁会话之后停止心跳发送 965 sendHeart2 = false; 966 keepSessionThread2.join(); 967 968 // 校验进程的cpu占用时间 969 #ifdef __aarch64__ 970 for (int i = 0; i < cpuDataVec1.size() && i < cpuDataVec2.size(); i++) { 971 EXPECT_LT(cpuDataVec1[i].cpu_usage_info().process_cpu_time_ms(), 972 cpuDataVec2[i].cpu_usage_info().process_cpu_time_ms()); 973 974 EXPECT_GE(cpuDataVec1[i].cpu_usage_info().process_cpu_time_ms(), 0); 975 EXPECT_GE(cpuDataVec2[i].cpu_usage_info().process_cpu_time_ms(), 0); 976 EXPECT_GT(cpuDataVec1[i].cpu_usage_info().system_cpu_time_ms(), 0); 977 EXPECT_GT(cpuDataVec2[i].cpu_usage_info().system_cpu_time_ms(), 0); 978 EXPECT_GT(cpuDataVec1[i].cpu_usage_info().system_boot_time_ms(), 0); 979 EXPECT_GT(cpuDataVec2[i].cpu_usage_info().system_boot_time_ms(), 0); 980 EXPECT_GE(cpuDataVec1[i].cpu_usage_info().cores().size(), 1); 981 EXPECT_GE(cpuDataVec2[i].cpu_usage_info().cores().size(), 1); 982 EXPECT_GE(cpuDataVec1[i].thread_info().size(), 1); 983 EXPECT_GE(cpuDataVec2[i].thread_info().size(), 1); 984 EXPECT_GT(cpuDataVec1[i].thread_info(0).tid(), 0); 985 EXPECT_GT(cpuDataVec2[i].thread_info(0).tid(), 0); 986 EXPECT_STRNE(cpuDataVec1[i].thread_info(0).thread_name().c_str(), ""); 987 EXPECT_STRNE(cpuDataVec2[i].thread_info(0).thread_name().c_str(), ""); 988 EXPECT_LE(cpuDataVec1[i].thread_info(0).thread_state(), ThreadState::THREAD_WAITING); 989 EXPECT_LE(cpuDataVec2[i].thread_info(0).thread_state(), ThreadState::THREAD_WAITING); 990 EXPECT_GE(cpuDataVec1[i].thread_info(0).thread_cpu_time_ms(), 0); 991 EXPECT_GE(cpuDataVec2[i].thread_info(0).thread_cpu_time_ms(), 0); 992 993 if (i > 0) { 994 EXPECT_EQ(cpuDataVec1[i].cpu_usage_info().prev_process_cpu_time_ms(), 995 cpuDataVec1[i-1].cpu_usage_info().process_cpu_time_ms()); 996 EXPECT_EQ(cpuDataVec2[i].cpu_usage_info().prev_process_cpu_time_ms(), 997 cpuDataVec2[i-1].cpu_usage_info().process_cpu_time_ms()); 998 EXPECT_EQ(cpuDataVec1[i].cpu_usage_info().prev_system_cpu_time_ms(), 999 cpuDataVec1[i-1].cpu_usage_info().system_cpu_time_ms()); 1000 EXPECT_EQ(cpuDataVec2[i].cpu_usage_info().prev_system_cpu_time_ms(), 1001 cpuDataVec2[i-1].cpu_usage_info().system_cpu_time_ms()); 1002 EXPECT_EQ(cpuDataVec1[i].cpu_usage_info().prev_system_boot_time_ms(), 1003 cpuDataVec1[i-1].cpu_usage_info().system_boot_time_ms()); 1004 EXPECT_EQ(cpuDataVec2[i].cpu_usage_info().prev_system_boot_time_ms(), 1005 cpuDataVec2[i-1].cpu_usage_info().system_boot_time_ms()); 1006 EXPECT_EQ(cpuDataVec1[i].thread_info(0).prev_thread_cpu_time_ms(), 1007 cpuDataVec1[i-1].thread_info(0).thread_cpu_time_ms()); 1008 EXPECT_EQ(cpuDataVec2[i].thread_info(0).prev_thread_cpu_time_ms(), 1009 cpuDataVec2[i-1].thread_info(0).thread_cpu_time_ms()); 1010 } 1011 } 1012 #endif 1013 1014 StopProcessStub(hiprofilerPluginsPid_); 1015 StopProcessStub(hiprofilerdPid_); 1016 StopProcessStub(cpuActivePid); 1017 StopProcessStub(idlePid_); 1018 1019 pluginVec_.clear(); 1020 } 1021 1022 /** 1023 * @tc.name: server 1024 * @tc.desc: RPC interface performance for diskio_plugin normal test. 1025 * @tc.type: FUNC 1026 */ 1027 HWTEST_F(ProfilerServicePerformanceTest, DFX_DFR_Hiprofiler_0030, Function | MediumTest | Level1) 1028 { 1029 StopDependServerStub(); 1030 ASSERT_TRUE(profilerStub_ != nullptr); 1031 StartDependServerStub(); 1032 Timer timer = {}; 1033 GetCapabilitiesResponse capResponse; 1034 EXPECT_TRUE(GetPluginCapabilities(ROUND_COUNT, capResponse)); 1035 auto timeCost = timer.ElapsedUs(); 1036 printf("GetCapabilities %d time cost %ldus.\n", ROUND_COUNT, timeCost); 1037 EXPECT_LE(timeCost, TIMEOUT_US); 1038 1039 ASSERT_TRUE(isExistSpecifyPlugin(capResponse, "diskio-plugin")); 1040 1041 timer.Reset(); 1042 uint32_t sessionId1; 1043 EXPECT_TRUE(CreatePluginSession(sessionId1, 0)); 1044 timeCost = timer.ElapsedUs(); 1045 printf("CreateSession cost %ldus.\n", timeCost); 1046 EXPECT_LE(timeCost, TIMEOUT_US); 1047 1048 // KeepSession 1049 timer.Reset(); 1050 EXPECT_TRUE(KeepPluginSession(ROUND_COUNT, sessionId1)); 1051 timeCost = timer.ElapsedUs(); 1052 printf("KeepSession %d time cost %ldus.\n", ROUND_COUNT, timeCost); 1053 EXPECT_LE(timeCost, TIMEOUT_US); 1054 1055 // 开启每隔4s发一次心跳的线程,确保seesionid不会失效 1056 bool sendHeart = true; 1057 int loopCount = 0; __anonaf86cf320a02() 1058 std::thread keepSessionThread([&]() { 1059 while (sendHeart && loopCount < LOOP_COUNT) { 1060 ++loopCount; 1061 KeepSessionRequest request; 1062 request.set_request_id(0); 1063 request.set_session_id(sessionId1); 1064 request.set_keep_alive_time(KEEP_SESSION_TIMEOUT_MS); 1065 grpc::ClientContext context; 1066 KeepSessionResponse ksResponse; 1067 grpc::Status status = profilerStub_->KeepSession(&context, request, &ksResponse); 1068 if (sendHeart) { 1069 EXPECT_TRUE(status.ok()); 1070 } 1071 usleep(KEEP_SESSION_SLEEP_US); 1072 } 1073 }); 1074 1075 // StartSession 1076 timer.Reset(); 1077 EXPECT_TRUE(StartPluginSession(sessionId1)); 1078 timeCost = timer.ElapsedUs(); 1079 printf("StartSession cost %ldus.\n", timeCost); 1080 EXPECT_LE(timeCost, TIMEOUT_US); 1081 1082 // FetchData 1083 timer.Reset(); 1084 std::vector<DiskioData> diskioDataVec1; 1085 std::vector<DiskioData> diskioDataVec2; 1086 std::unique_ptr<grpc::ClientReader<FetchDataResponse>> fetchResponse = nullptr; 1087 grpc::ClientContext fetchContext; 1088 FetchDataRequest fetchRequest = FetchDataRequest(); 1089 fetchRequest.set_request_id(0); 1090 fetchRequest.set_session_id(sessionId1); 1091 fetchResponse = profilerStub_->FetchData(&fetchContext, fetchRequest); 1092 ASSERT_TRUE(fetchResponse != nullptr); 1093 int count = ROUND_COUNT + ROUND_COUNT + 1; 1094 int diskioPid = -1; __anonaf86cf320b02() 1095 std::thread fetchDataThread([&]() { 1096 int ict = 0; 1097 while (1) { 1098 FetchDataResponse rp; 1099 fetchResponse.get()->Read(&rp); 1100 for (int i = 0; i < rp.plugin_data().size(); i++) { 1101 ProfilerPluginData pd = rp.plugin_data(i); 1102 if (ict == count) { 1103 return; 1104 } 1105 1106 if (ict == ROUND_COUNT) { // 向磁盘写入100k的数据 1107 StartTestDiskioProcess(diskioPid); 1108 sleep(1); 1109 ict += 1; 1110 break; 1111 } 1112 1113 ASSERT_STREQ(pd.name().c_str(), "diskio-plugin"); 1114 1115 DiskioData dd; 1116 auto& data = pd.data(); 1117 dd.ParseFromArray(data.data(), data.size()); 1118 if (ict < ROUND_COUNT) { 1119 diskioDataVec1.push_back(dd); 1120 } else { 1121 diskioDataVec2.push_back(dd); 1122 } 1123 1124 ict += 1; 1125 } 1126 } 1127 }); 1128 fetchDataThread.join(); 1129 timeCost = timer.ElapsedUs(); 1130 printf("FetchData cost %ldus.\n", timeCost); 1131 EXPECT_LE(timeCost, FETCHDATA_TIMEOUT_US); 1132 1133 // StopSession 1134 timer.Reset(); 1135 EXPECT_TRUE(StopPluginSession(sessionId1)); 1136 timeCost = timer.ElapsedUs(); 1137 printf("StopSession cost %ldus.\n", timeCost); 1138 EXPECT_LE(timeCost, TIMEOUT_US); 1139 1140 // DestroySession 1141 timer.Reset(); 1142 EXPECT_TRUE(DestroyPluginSession(sessionId1)); 1143 timeCost = timer.ElapsedUs(); 1144 printf("DestroySession cost %ldus.\n", timeCost); 1145 EXPECT_LE(timeCost, TIMEOUT_US); 1146 // 销毁会话之后停止心跳发送 1147 sendHeart = false; 1148 keepSessionThread.join(); 1149 1150 // 验证diskio上报的数据指标 1151 for (int i = 0; i < diskioDataVec1.size() && i < diskioDataVec2.size(); i++) { 1152 EXPECT_LE(diskioDataVec1[i].wr_sectors_kb() + WRITE_KB, diskioDataVec2[i].wr_sectors_kb()); 1153 EXPECT_GT(diskioDataVec1[i].wr_sectors_kb(), 0); 1154 EXPECT_GT(diskioDataVec2[i].wr_sectors_kb(), 0); 1155 EXPECT_GT(diskioDataVec1[i].rd_sectors_kb(), 0); 1156 EXPECT_GT(diskioDataVec2[i].rd_sectors_kb(), 0); 1157 } 1158 1159 StopProcessStub(hiprofilerPluginsPid_); 1160 StopProcessStub(hiprofilerdPid_); 1161 StopProcessStub(diskioPid); 1162 1163 pluginVec_.clear(); 1164 } 1165 1166 /** 1167 * @tc.name: server 1168 * @tc.desc: RPC interface performance for process_plugin normal test. 1169 * @tc.type: FUNC 1170 */ 1171 HWTEST_F(ProfilerServicePerformanceTest, DFX_DFR_Hiprofiler_0040, Function | MediumTest | Level1) 1172 { 1173 StopDependServerStub(); 1174 ASSERT_TRUE(profilerStub_ != nullptr); 1175 1176 StartDependServerStub(); 1177 Timer timer = {}; 1178 GetCapabilitiesResponse capResponse; 1179 EXPECT_TRUE(GetPluginCapabilities(ROUND_COUNT, capResponse)); 1180 auto timeCost = timer.ElapsedUs(); 1181 printf("GetCapabilities %d time cost %ldus.\n", ROUND_COUNT, timeCost); 1182 EXPECT_LE(timeCost, TIMEOUT_US); 1183 1184 ASSERT_TRUE(isExistSpecifyPlugin(capResponse, "process-plugin")); 1185 1186 timer.Reset(); 1187 uint32_t sessionId1; 1188 EXPECT_TRUE(CreatePluginSession(sessionId1, 0)); 1189 timeCost = timer.ElapsedUs(); 1190 printf("CreateSession cost %ldus.\n", timeCost); 1191 EXPECT_LE(timeCost, TIMEOUT_US); 1192 1193 // KeepSession 1194 timer.Reset(); 1195 EXPECT_TRUE(KeepPluginSession(ROUND_COUNT, sessionId1)); 1196 timeCost = timer.ElapsedUs(); 1197 printf("KeepSession %d time cost %ldus.\n", ROUND_COUNT, timeCost); 1198 EXPECT_LE(timeCost, TIMEOUT_US); 1199 1200 // 开启每隔4s发一次心跳的线程,确保seesionid不会失效 1201 bool sendHeart = true; 1202 int loopCount = 0; __anonaf86cf320c02() 1203 std::thread keepSessionThread([&]() { 1204 while (sendHeart && loopCount < LOOP_COUNT) { 1205 ++loopCount; 1206 KeepSessionRequest request; 1207 request.set_request_id(0); 1208 request.set_session_id(sessionId1); 1209 request.set_keep_alive_time(KEEP_SESSION_TIMEOUT_MS); 1210 grpc::ClientContext context; 1211 KeepSessionResponse ksResponse; 1212 grpc::Status status = profilerStub_->KeepSession(&context, request, &ksResponse); 1213 if (sendHeart) { 1214 EXPECT_TRUE(status.ok()); 1215 } 1216 usleep(KEEP_SESSION_SLEEP_US); 1217 } 1218 }); 1219 1220 // StartSession 1221 timer.Reset(); 1222 EXPECT_TRUE(StartPluginSession(sessionId1)); 1223 timeCost = timer.ElapsedUs(); 1224 printf("StartSession cost %ldus.\n", timeCost); 1225 EXPECT_LE(timeCost, TIMEOUT_US); 1226 1227 // FetchData 1228 timer.Reset(); 1229 std::unique_ptr<grpc::ClientReader<FetchDataResponse>> fetchResponse = nullptr; 1230 grpc::ClientContext fetchContext; 1231 FetchDataRequest fetchRequest = FetchDataRequest(); 1232 fetchRequest.set_request_id(0); 1233 fetchRequest.set_session_id(sessionId1); 1234 fetchResponse = profilerStub_->FetchData(&fetchContext, fetchRequest); 1235 ASSERT_TRUE(fetchResponse != nullptr); 1236 int hiprofilerdPidCount = 0; 1237 int hiprofilerPluginsPidCount = 0; __anonaf86cf320d02() 1238 std::thread fetchDataThread([&]() { 1239 int ict = 0; 1240 while (1) { 1241 FetchDataResponse rp; 1242 fetchResponse.get()->Read(&rp); 1243 for (int i = 0; i < rp.plugin_data().size(); i++) { 1244 ProfilerPluginData pd = rp.plugin_data(i); 1245 if (ict == ROUND_COUNT) { 1246 return; 1247 } 1248 1249 ASSERT_STREQ(pd.name().c_str(), "process-plugin"); 1250 1251 ProcessData processData; 1252 auto& data = pd.data(); 1253 processData.ParseFromArray(data.data(), data.size()); 1254 EXPECT_GT(static_cast<int>(processData.processesinfo().size()), 0); 1255 for (int j = 0; j < processData.processesinfo().size(); j++) { 1256 ProcessInfo info = processData.processesinfo(j); 1257 if (info.pid() == hiprofilerdPid_) { 1258 hiprofilerdPidCount++; 1259 } else if (info.pid() == hiprofilerPluginsPid_) { 1260 hiprofilerPluginsPidCount++; 1261 } 1262 } 1263 1264 ict += 1; 1265 } 1266 } 1267 }); 1268 fetchDataThread.join(); 1269 timeCost = timer.ElapsedUs(); 1270 printf("FetchData cost %ldus.\n", timeCost); 1271 EXPECT_LE(timeCost, FETCHDATA_TIMEOUT_US); 1272 EXPECT_EQ(hiprofilerdPidCount, ROUND_COUNT); 1273 EXPECT_EQ(hiprofilerPluginsPidCount, ROUND_COUNT); 1274 1275 // StopSession 1276 timer.Reset(); 1277 EXPECT_TRUE(StopPluginSession(sessionId1)); 1278 timeCost = timer.ElapsedUs(); 1279 printf("StopSession cost %ldus.\n", timeCost); 1280 EXPECT_LE(timeCost, TIMEOUT_US); 1281 1282 // DestroySession 1283 timer.Reset(); 1284 EXPECT_TRUE(DestroyPluginSession(sessionId1)); 1285 timeCost = timer.ElapsedUs(); 1286 printf("DestroySession cost %ldus.\n", timeCost); 1287 EXPECT_LE(timeCost, TIMEOUT_US); 1288 // 销毁会话之后停止心跳发送 1289 sendHeart = false; 1290 keepSessionThread.join(); 1291 1292 StopProcessStub(hiprofilerPluginsPid_); 1293 StopProcessStub(hiprofilerdPid_); 1294 1295 pluginVec_.clear(); 1296 } 1297 1298 /** 1299 * @tc.name: server 1300 * @tc.desc: RPC interface performance for hilog_plugin normal test. 1301 * @tc.type: FUNC 1302 */ 1303 HWTEST_F(ProfilerServicePerformanceTest, DFX_DFR_Hiprofiler_0050, Function | MediumTest | Level1) 1304 { 1305 StopDependServerStub(); 1306 ASSERT_TRUE(profilerStub_ != nullptr); 1307 StartDependServerStub(); 1308 Timer timer = {}; 1309 GetCapabilitiesResponse capResponse; 1310 EXPECT_TRUE(GetPluginCapabilities(ROUND_COUNT, capResponse)); 1311 auto timeCost = timer.ElapsedUs(); 1312 printf("GetCapabilities %d time cost %ldus.\n", ROUND_COUNT, timeCost); 1313 EXPECT_LE(timeCost, TIMEOUT_US); 1314 1315 ASSERT_TRUE(isExistSpecifyPlugin(capResponse, "hilog-plugin")); 1316 1317 timer.Reset(); 1318 uint32_t sessionId1; 1319 EXPECT_TRUE(CreatePluginSession(sessionId1, 0, true)); 1320 timeCost = timer.ElapsedUs(); 1321 printf("CreateSession cost %ldus.\n", timeCost); 1322 EXPECT_LE(timeCost, TIMEOUT_US); 1323 1324 // KeepSession 1325 timer.Reset(); 1326 EXPECT_TRUE(KeepPluginSession(ROUND_COUNT, sessionId1)); 1327 timeCost = timer.ElapsedUs(); 1328 printf("KeepSession %d time cost %ldus.\n", ROUND_COUNT, timeCost); 1329 EXPECT_LE(timeCost, TIMEOUT_US); 1330 1331 // 开启每隔4s发一次心跳的线程,确保seesionid不会失效 1332 bool sendHeart = true; 1333 int loopCount = 0; __anonaf86cf320e02() 1334 std::thread keepSessionThread([&]() { 1335 while (sendHeart && loopCount < LOOP_COUNT) { 1336 ++loopCount; 1337 KeepSessionRequest request; 1338 request.set_request_id(0); 1339 request.set_session_id(sessionId1); 1340 request.set_keep_alive_time(KEEP_SESSION_TIMEOUT_MS); 1341 grpc::ClientContext context; 1342 KeepSessionResponse ksResponse; 1343 grpc::Status status = profilerStub_->KeepSession(&context, request, &ksResponse); 1344 if (sendHeart) { 1345 EXPECT_TRUE(status.ok()); 1346 } 1347 usleep(KEEP_SESSION_SLEEP_US); 1348 } 1349 }); 1350 1351 // StartSession 1352 timer.Reset(); 1353 EXPECT_TRUE(StartPluginSession(sessionId1)); 1354 timeCost = timer.ElapsedUs(); 1355 printf("StartSession cost %ldus.\n", timeCost); 1356 EXPECT_LE(timeCost, TIMEOUT_US); 1357 1358 // FetchData 1359 timer.Reset(); 1360 std::unique_ptr<grpc::ClientReader<FetchDataResponse>> fetchResponse = nullptr; 1361 grpc::ClientContext fetchContext; 1362 FetchDataRequest fetchRequest = FetchDataRequest(); 1363 fetchRequest.set_request_id(0); 1364 fetchRequest.set_session_id(sessionId1); 1365 fetchResponse = profilerStub_->FetchData(&fetchContext, fetchRequest); 1366 ASSERT_TRUE(fetchResponse != nullptr); 1367 FetchDataResponse rp; 1368 fetchResponse.get()->Read(&rp); 1369 for (int i = 0; i < rp.plugin_data().size() && i < ROUND_COUNT; i++) { 1370 ProfilerPluginData pd = rp.plugin_data(i); 1371 HilogInfo hilogInfo; 1372 auto& data = pd.data(); 1373 hilogInfo.ParseFromArray(data.data(), data.size()); 1374 EXPECT_GT(static_cast<int>(hilogInfo.info().size()), 0); 1375 EXPECT_TRUE(hilogInfo.info(0).has_detail()); 1376 if (hilogInfo.info(0).detail().pid() > 0) { 1377 EXPECT_STRNE(hilogInfo.info(0).context().c_str(), ""); 1378 } 1379 } 1380 timeCost = timer.ElapsedUs(); 1381 printf("FetchData cost %ldus.\n", timeCost); 1382 1383 // StopSession 1384 timer.Reset(); 1385 EXPECT_TRUE(StopPluginSession(sessionId1)); 1386 timeCost = timer.ElapsedUs(); 1387 printf("StopSession cost %ldus.\n", timeCost); 1388 EXPECT_LE(timeCost, TIMEOUT_US); 1389 1390 // DestroySession 1391 timer.Reset(); 1392 EXPECT_TRUE(DestroyPluginSession(sessionId1)); 1393 timeCost = timer.ElapsedUs(); 1394 printf("DestroySession cost %ldus.\n", timeCost); 1395 EXPECT_LE(timeCost, TIMEOUT_US); 1396 // 销毁会话之后停止心跳发送 1397 sendHeart = false; 1398 keepSessionThread.join(); 1399 1400 // 验证hilog离线模式是否生成hilogResultFile离线文件 1401 uint32_t sessionId2; 1402 EXPECT_TRUE(CreatePluginSession(sessionId2, 0, false)); 1403 EXPECT_TRUE(KeepPluginSession(ROUND_COUNT, sessionId2)); 1404 1405 // 开启每隔4s发一次心跳的线程,确保seesionid不会失效 1406 bool sendHeart2 = true; 1407 loopCount = 0; __anonaf86cf320f02() 1408 std::thread keepSessionThread2([&]() { 1409 while (sendHeart2 && loopCount < LOOP_COUNT) { 1410 ++loopCount; 1411 KeepSessionRequest request; 1412 request.set_request_id(0); 1413 request.set_session_id(sessionId2); 1414 request.set_keep_alive_time(KEEP_SESSION_TIMEOUT_MS); 1415 grpc::ClientContext context; 1416 KeepSessionResponse ksResponse; 1417 grpc::Status status = profilerStub_->KeepSession(&context, request, &ksResponse); 1418 if (sendHeart2) { 1419 EXPECT_TRUE(status.ok()); 1420 } 1421 usleep(KEEP_SESSION_SLEEP_US); 1422 } 1423 }); 1424 1425 EXPECT_TRUE(StartPluginSession(sessionId2)); 1426 usleep(TIMEOUT_US); 1427 EXPECT_EQ(access(HILOG_RESULT_FILE.c_str(), F_OK), 0); 1428 EXPECT_TRUE(StopPluginSession(sessionId2)); 1429 EXPECT_TRUE(DestroyPluginSession(sessionId2)); 1430 // 销毁会话之后停止心跳发送 1431 sendHeart2 = false; 1432 keepSessionThread2.join(); 1433 1434 StopProcessStub(hiprofilerPluginsPid_); 1435 StopProcessStub(hiprofilerdPid_); 1436 1437 pluginVec_.clear(); 1438 } 1439 1440 /** 1441 * @tc.name: server 1442 * @tc.desc: RPC interface performance for network_plugin normal test. 1443 * @tc.type: FUNC 1444 */ 1445 HWTEST_F(ProfilerServicePerformanceTest, DFX_DFR_Hiprofiler_0060, Function | MediumTest | Level1) 1446 { 1447 StopDependServerStub(); 1448 ASSERT_TRUE(profilerStub_ != nullptr); 1449 StartDependServerStub(); 1450 Timer timer = {}; 1451 GetCapabilitiesResponse capResponse; 1452 EXPECT_TRUE(GetPluginCapabilities(ROUND_COUNT, capResponse)); 1453 auto timeCost = timer.ElapsedUs(); 1454 printf("GetCapabilities %d time cost %ldus.\n", ROUND_COUNT, timeCost); 1455 EXPECT_LE(timeCost, TIMEOUT_US); 1456 1457 ASSERT_TRUE(isExistSpecifyPlugin(capResponse, "network-plugin")); 1458 1459 timer.Reset(); 1460 uint32_t sessionId1; 1461 EXPECT_TRUE(CreatePluginSession(sessionId1, 0)); 1462 timeCost = timer.ElapsedUs(); 1463 printf("CreateSession cost %ldus.\n", timeCost); 1464 EXPECT_LE(timeCost, TIMEOUT_US); 1465 1466 // KeepSession 1467 timer.Reset(); 1468 EXPECT_TRUE(KeepPluginSession(ROUND_COUNT, sessionId1)); 1469 timeCost = timer.ElapsedUs(); 1470 printf("KeepSession %d time cost %ldus.\n", ROUND_COUNT, timeCost); 1471 EXPECT_LE(timeCost, TIMEOUT_US); 1472 1473 // 开启每隔4s发一次心跳的线程,确保seesionid不会失效 1474 bool sendHeart = true; 1475 int loopCount = 0; __anonaf86cf321002() 1476 std::thread keepSessionThread([&]() { 1477 while (sendHeart && loopCount < LOOP_COUNT) { 1478 ++loopCount; 1479 KeepSessionRequest request; 1480 request.set_request_id(0); 1481 request.set_session_id(sessionId1); 1482 request.set_keep_alive_time(KEEP_SESSION_TIMEOUT_MS); 1483 grpc::ClientContext context; 1484 KeepSessionResponse ksResponse; 1485 grpc::Status status = profilerStub_->KeepSession(&context, request, &ksResponse); 1486 if (sendHeart) { 1487 EXPECT_TRUE(status.ok()); 1488 } 1489 usleep(KEEP_SESSION_SLEEP_US); 1490 } 1491 }); 1492 1493 // StartSession 1494 timer.Reset(); 1495 EXPECT_TRUE(StartPluginSession(sessionId1)); 1496 timeCost = timer.ElapsedUs(); 1497 printf("StartSession cost %ldus.\n", timeCost); 1498 EXPECT_LE(timeCost, TIMEOUT_US); 1499 1500 // FetchData network_plugin在开发板上没有节点,不上报数据 1501 timer.Reset(); 1502 std::unique_ptr<grpc::ClientReader<FetchDataResponse>> fetchResponse = nullptr; 1503 grpc::ClientContext fetchContext; 1504 FetchDataRequest fetchRequest = FetchDataRequest(); 1505 fetchRequest.set_request_id(0); 1506 fetchRequest.set_session_id(sessionId1); 1507 fetchResponse = profilerStub_->FetchData(&fetchContext, fetchRequest); 1508 ASSERT_TRUE(fetchResponse != nullptr); 1509 bool recvData = true; __anonaf86cf321102() 1510 std::thread fetchDataThread([&]() { 1511 while (recvData) { 1512 FetchDataResponse rp; 1513 fetchResponse.get()->Read(&rp); 1514 EXPECT_EQ(rp.plugin_data().size(), 0); 1515 } 1516 }); 1517 usleep(TIMEOUT_US); 1518 1519 // StopSession 1520 timer.Reset(); 1521 EXPECT_TRUE(StopPluginSession(sessionId1)); 1522 timeCost = timer.ElapsedUs(); 1523 printf("StopSession cost %ldus.\n", timeCost); 1524 EXPECT_LE(timeCost, TIMEOUT_US); 1525 1526 recvData = false; 1527 fetchDataThread.join(); 1528 1529 // DestroySession 1530 timer.Reset(); 1531 EXPECT_TRUE(DestroyPluginSession(sessionId1)); 1532 timeCost = timer.ElapsedUs(); 1533 printf("DestroySession cost %ldus.\n", timeCost); 1534 EXPECT_LE(timeCost, TIMEOUT_US); 1535 // 销毁会话之后停止心跳发送 1536 sendHeart = false; 1537 keepSessionThread.join(); 1538 1539 StopProcessStub(hiprofilerPluginsPid_); 1540 StopProcessStub(hiprofilerdPid_); 1541 1542 pluginVec_.clear(); 1543 } 1544 1545 /** 1546 * @tc.name: server 1547 * @tc.desc: RPC interface performance unusual test. 1548 * @tc.type: FUNC 1549 */ 1550 HWTEST_F(ProfilerServicePerformanceTest, DFX_DFR_Hiprofiler_0070, Function | MediumTest | Level1) 1551 { 1552 StopDependServerStub(); 1553 ASSERT_TRUE(profilerStub_ != nullptr); 1554 uint32_t sessionId; 1555 GetCapabilitiesResponse capResponse1; 1556 EXPECT_FALSE(GetPluginCapabilities(1, capResponse1)); 1557 EXPECT_FALSE(CreatePluginSession(sessionId, 1)); 1558 EXPECT_FALSE(KeepPluginSession(1, sessionId)); 1559 EXPECT_FALSE(StartPluginSession(sessionId)); 1560 EXPECT_FALSE(StartPluginSession(sessionId)); 1561 EXPECT_FALSE(DestroyPluginSession(sessionId)); 1562 StartDependServerStub(); 1563 Timer timer = {}; 1564 GetCapabilitiesResponse capResponse; 1565 EXPECT_TRUE(GetPluginCapabilities(1, capResponse)); 1566 auto timeCost = timer.ElapsedUs(); 1567 printf("UnusualTest: GetCapabilities 1 time cost %ldus.\n", timeCost); 1568 EXPECT_LE(timeCost, TIMEOUT_US); 1569 1570 ASSERT_TRUE(isExistSpecifyPlugin(capResponse, "cpu-plugin")); 1571 1572 timer.Reset(); 1573 EXPECT_TRUE(CreatePluginSession(sessionId, 1)); 1574 timeCost = timer.ElapsedUs(); 1575 printf("UnusualTest: CreateSession cost %ldus.\n", timeCost); 1576 EXPECT_LE(timeCost, TIMEOUT_US); 1577 1578 // KeepSession,5s超时心跳 1579 timer.Reset(); 1580 EXPECT_TRUE(KeepPluginSession(1, sessionId)); 1581 timeCost = timer.ElapsedUs(); 1582 printf("UnusualTest: KeepSession 1 time cost %ldus.\n", timeCost); 1583 EXPECT_LE(timeCost, TIMEOUT_US); 1584 1585 // 睡眠5s,确保seesionId已失效 1586 usleep(KEEP_SESSION_TIME_US); 1587 1588 // StartSession 1589 timer.Reset(); 1590 EXPECT_FALSE(StartPluginSession(sessionId)); 1591 timeCost = timer.ElapsedUs(); 1592 printf("UnusualTest: StartSession cost %ldus.\n", timeCost); 1593 EXPECT_LE(timeCost, TIMEOUT_US); 1594 1595 // FetchData 1596 std::unique_ptr<grpc::ClientReader<FetchDataResponse>> fetchResponse = nullptr; 1597 grpc::ClientContext fetchContext; 1598 FetchDataRequest fetchRequest = FetchDataRequest(); 1599 fetchRequest.set_request_id(0); 1600 fetchRequest.set_session_id(sessionId); 1601 fetchResponse = profilerStub_->FetchData(&fetchContext, fetchRequest); 1602 ASSERT_TRUE(fetchResponse != nullptr); 1603 FetchDataResponse rp; 1604 fetchResponse.get()->Read(&rp); 1605 EXPECT_EQ(rp.plugin_data().size(), 0); 1606 1607 // StopSession 1608 timer.Reset(); 1609 EXPECT_FALSE(StopPluginSession(sessionId)); 1610 timeCost = timer.ElapsedUs(); 1611 printf("UnusualTest: StopSession cost %ldus.\n", timeCost); 1612 EXPECT_LE(timeCost, TIMEOUT_US); 1613 1614 // DestroySession 1615 timer.Reset(); 1616 EXPECT_FALSE(DestroyPluginSession(sessionId)); 1617 timeCost = timer.ElapsedUs(); 1618 printf("UnusualTest: DestroySession cost %ldus.\n", timeCost); 1619 EXPECT_LE(timeCost, TIMEOUT_US); 1620 1621 // 验证心跳正常,pid为1的cpu插件数据上报 1622 GetCapabilitiesResponse capResponse2; 1623 EXPECT_TRUE(GetPluginCapabilities(ROUND_COUNT, capResponse2)); 1624 1625 uint32_t sessionId1; 1626 EXPECT_TRUE(CreatePluginSession(sessionId1, 1)); 1627 1628 // KeepSession 1629 EXPECT_TRUE(KeepPluginSession(ROUND_COUNT, sessionId1)); 1630 1631 // 开启每隔4s发一次心跳的线程,确保seesionid不会失效 1632 bool sendHeart = true; 1633 int loopCount = 0; __anonaf86cf321202() 1634 std::thread keepSessionThread1([&]() { 1635 while (sendHeart && loopCount < LOOP_COUNT) { 1636 ++loopCount; 1637 KeepSessionRequest request; 1638 request.set_request_id(0); 1639 request.set_session_id(sessionId1); 1640 request.set_keep_alive_time(KEEP_SESSION_TIMEOUT_MS); 1641 grpc::ClientContext context; 1642 KeepSessionResponse ksResponse; 1643 grpc::Status status = profilerStub_->KeepSession(&context, request, &ksResponse); 1644 if (sendHeart) { 1645 EXPECT_TRUE(status.ok()); 1646 } 1647 usleep(KEEP_SESSION_SLEEP_US); 1648 } 1649 }); 1650 1651 // StartSession 1652 EXPECT_TRUE(StartPluginSession(sessionId1)); 1653 1654 // FetchData 1655 std::vector<CpuData> cpuDataVec1; 1656 std::unique_ptr<grpc::ClientReader<FetchDataResponse>> fetchResponse1 = nullptr; 1657 grpc::ClientContext fetchContext2; 1658 FetchDataRequest fetchRequest1 = FetchDataRequest(); 1659 fetchRequest1.set_request_id(0); 1660 fetchRequest1.set_session_id(sessionId1); 1661 fetchResponse1 = profilerStub_->FetchData(&fetchContext2, fetchRequest1); 1662 ASSERT_TRUE(fetchResponse1 != nullptr); __anonaf86cf321302() 1663 std::thread fetchDataThread1([&]() { 1664 int ict = 0; 1665 while (1) { 1666 FetchDataResponse rp; 1667 fetchResponse1.get()->Read(&rp); 1668 for (int i = 0; i < rp.plugin_data().size(); i++) { 1669 ProfilerPluginData pd = rp.plugin_data(i); 1670 if (ict == ROUND_COUNT) { 1671 return; 1672 } 1673 1674 ASSERT_STREQ(pd.name().c_str(), "cpu-plugin"); 1675 ict += 1; 1676 } 1677 } 1678 }); 1679 fetchDataThread1.join(); 1680 1681 // StopSession 1682 EXPECT_TRUE(StopPluginSession(sessionId1)); 1683 1684 // DestroySession 1685 EXPECT_TRUE(DestroyPluginSession(sessionId1)); 1686 1687 // 销毁会话之后停止心跳发送 1688 sendHeart = false; 1689 keepSessionThread1.join(); 1690 1691 StopProcessStub(hiprofilerPluginsPid_); 1692 StopProcessStub(hiprofilerdPid_); 1693 1694 pluginVec_.clear(); 1695 } 1696 } 1697 1698 #pragma clang optimize on