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