|
28 | 28 | #include "src/output_membuf.h" |
29 | 29 |
|
30 | 30 | #include "src/Logger.h" |
| 31 | +#include "test/MockActivitySubProfiler.h" |
31 | 32 |
|
32 | 33 | using namespace std::chrono; |
33 | 34 | using namespace KINETO_NAMESPACE; |
34 | 35 |
|
35 | 36 | #define CUDA_LAUNCH_KERNEL CUPTI_RUNTIME_TRACE_CBID_cudaLaunchKernel_v7000 |
36 | 37 | #define CUDA_MEMCPY CUPTI_RUNTIME_TRACE_CBID_cudaMemcpy_v3020 |
37 | 38 |
|
| 39 | +namespace { |
| 40 | +const TraceSpan& defaultTraceSpan() { |
| 41 | + static TraceSpan span(0, 0, "Unknown", ""); |
| 42 | + return span; |
| 43 | +} |
| 44 | +} |
| 45 | + |
38 | 46 | // Provides ability to easily create a few test CPU-side ops |
39 | 47 | struct MockCpuActivityBuffer : public CpuTraceBuffer { |
40 | 48 | MockCpuActivityBuffer(int64_t startTime, int64_t endTime) { |
@@ -375,6 +383,78 @@ TEST_F(ActivityProfilerTest, CorrelatedTimestampTest) { |
375 | 383 | EXPECT_EQ(counts["launchKernel"], 1); |
376 | 384 | } |
377 | 385 |
|
| 386 | +TEST_F(ActivityProfilerTest, SubActivityProfilers) { |
| 387 | + using ::testing::Return; |
| 388 | + using ::testing::ByMove; |
| 389 | + |
| 390 | + // Verbose logging is useful for debugging |
| 391 | + std::vector<std::string> log_modules( |
| 392 | + {"ActivityProfiler.cpp"}); |
| 393 | + SET_LOG_VERBOSITY_LEVEL(2, log_modules); |
| 394 | + |
| 395 | + // Setup example events to test |
| 396 | + GenericTraceActivity ev{defaultTraceSpan(), ActivityType::GLOW_RUNTIME, ""}; |
| 397 | + ev.device = 1; |
| 398 | + ev.resource = 0; |
| 399 | + |
| 400 | + int64_t start_time_us = 100; |
| 401 | + int64_t duration_us = 1000; |
| 402 | + auto start_time = time_point<system_clock>(microseconds(start_time_us)); |
| 403 | + |
| 404 | + std::vector<GenericTraceActivity> test_activities{3, ev}; |
| 405 | + test_activities[0].startTime = start_time_us; |
| 406 | + test_activities[0].endTime = start_time_us + 5000; |
| 407 | + test_activities[0].activityName = "SubGraph A execution"; |
| 408 | + test_activities[1].startTime = start_time_us; |
| 409 | + test_activities[1].endTime = start_time_us + 2000; |
| 410 | + test_activities[1].activityName = "Operator foo"; |
| 411 | + test_activities[2].startTime = start_time_us + 2500; |
| 412 | + test_activities[2].endTime = start_time_us + 2900; |
| 413 | + test_activities[2].activityName = "Operator bar"; |
| 414 | + |
| 415 | + auto mock_activity_profiler = |
| 416 | + std::make_shared<MockActivityProfiler>(test_activities); |
| 417 | + |
| 418 | + MockCuptiActivities activities; |
| 419 | + ActivityProfiler profiler(activities, /*cpu only*/ true); |
| 420 | + profiler.addActivityProfiler(mock_activity_profiler); |
| 421 | + |
| 422 | + profiler.configure(*cfg_, start_time); |
| 423 | + profiler.startTrace(start_time); |
| 424 | + EXPECT_TRUE(profiler.isActive()); |
| 425 | + |
| 426 | + profiler.stopTrace(start_time + microseconds(duration_us)); |
| 427 | + EXPECT_TRUE(profiler.isActive()); |
| 428 | + |
| 429 | + char filename[] = "/tmp/libkineto_testXXXXXX.json"; |
| 430 | + mkstemps(filename, 5); |
| 431 | + LOG(INFO) << "Logging to tmp file " << filename; |
| 432 | + |
| 433 | + // process trace |
| 434 | + auto logger = std::make_unique<MemoryTraceLogger>(*cfg_); |
| 435 | + profiler.processTrace(*logger); |
| 436 | + profiler.setLogger(logger.get()); |
| 437 | + |
| 438 | + ActivityTrace trace(std::move(logger), loggerFactory); |
| 439 | + trace.save(filename); |
| 440 | + const auto& traced_activites = trace.activities(); |
| 441 | + |
| 442 | + // Test we have all the events |
| 443 | + EXPECT_EQ(traced_activites->size(), test_activities.size()); |
| 444 | + |
| 445 | + // Check that the expected file was written and that it has some content |
| 446 | + int fd = open(filename, O_RDONLY); |
| 447 | + if (!fd) { |
| 448 | + perror(filename); |
| 449 | + } |
| 450 | + EXPECT_TRUE(fd); |
| 451 | + |
| 452 | + // Should expect at least 100 bytes |
| 453 | + struct stat buf{}; |
| 454 | + fstat(fd, &buf); |
| 455 | + EXPECT_GT(buf.st_size, 100); |
| 456 | +} |
| 457 | + |
378 | 458 | TEST_F(ActivityProfilerTest, BufferSizeLimitTestWarmup) { |
379 | 459 | ActivityProfiler profiler(cuptiActivities_, /*cpu only*/ false); |
380 | 460 |
|
|
0 commit comments