profiler_test.cc 4.6 KB
Newer Older
L
liutiexing 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
14 15 16

#include <set>
#include <string>
17

18 19 20 21 22 23 24 25
#include "glog/logging.h"
#include "gtest/gtest.h"
#ifdef PADDLE_WITH_CUDA
#include <cuda.h>
#endif
#ifdef PADDLE_WITH_HIP
#include <hip/hip_runtime.h>
#endif
26 27
#include "paddle/fluid/platform/place.h"
#include "paddle/fluid/platform/profiler.h"
C
chenjian 已提交
28
#include "paddle/fluid/platform/profiler/event_python.h"
29 30 31 32 33
#include "paddle/fluid/platform/profiler/event_tracing.h"
#include "paddle/fluid/platform/profiler/profiler.h"

TEST(ProfilerTest, TestHostTracer) {
  using paddle::platform::Profiler;
34 35
  using paddle::platform::ProfilerOptions;
  using paddle::platform::ProfilerResult;
36 37 38 39
  using paddle::platform::RecordInstantEvent;
  using paddle::platform::TracerEventType;
  ProfilerOptions options;
  options.trace_level = 2;
C
chenjian 已提交
40
  options.trace_switch = 3;
41 42 43 44 45
  auto profiler = Profiler::Create(options);
  EXPECT_TRUE(profiler);
  profiler->Prepare();
  profiler->Start();
  {
46 47 48 49
    RecordInstantEvent(
        "TestTraceLevel_record1", TracerEventType::UserDefined, 2);
    RecordInstantEvent(
        "TestTraceLevel_record2", TracerEventType::UserDefined, 3);
50
  }
C
chenjian 已提交
51
  auto profiler_result = profiler->Stop();
L
liutiexing 已提交
52
  auto nodetree = profiler_result->GetNodeTrees();
53
  std::set<std::string> host_events;
L
liutiexing 已提交
54
  for (const auto pair : nodetree->Traverse(true)) {
55 56 57
    for (const auto evt : pair.second) {
      host_events.insert(evt->Name());
    }
L
liutiexing 已提交
58
  }
59 60 61
  EXPECT_EQ(host_events.count("TestTraceLevel_record1"), 1u);
  EXPECT_EQ(host_events.count("TestTraceLevel_record2"), 0u);
}
L
liutiexing 已提交
62 63 64

TEST(ProfilerTest, TestCudaTracer) {
  using paddle::platform::Profiler;
65
  using paddle::platform::ProfilerOptions;
C
chenjian 已提交
66
  using paddle::platform::ProfilerResult;
L
liutiexing 已提交
67 68
  ProfilerOptions options;
  options.trace_level = 0;
C
chenjian 已提交
69
  options.trace_switch = 3;
L
liutiexing 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83
  auto profiler = Profiler::Create(options);
  EXPECT_TRUE(profiler);
  profiler->Prepare();
  profiler->Start();
#ifdef PADDLE_WITH_CUDA
  cudaStream_t stream;
  cudaStreamCreate(&stream);
  cudaStreamSynchronize(stream);
#endif
#ifdef PADDLE_WITH_HIP
  hipStream_t stream;
  hipStreamCreate(&stream);
  hipStreamSynchronize(stream);
#endif
C
chenjian 已提交
84
  auto profiler_result = profiler->Stop();
L
liutiexing 已提交
85
  auto nodetree = profiler_result->GetNodeTrees();
L
liutiexing 已提交
86 87 88 89 90 91 92 93 94 95 96 97
  std::vector<std::string> runtime_events;
  for (const auto pair : nodetree->Traverse(true)) {
    for (const auto host_node : pair.second) {
      for (auto runtime_node : host_node->GetRuntimeTraceEventNodes()) {
        runtime_events.push_back(runtime_node->Name());
      }
    }
  }
#ifdef PADDLE_WITH_CUPTI
  EXPECT_GT(runtime_events.size(), 0u);
#endif
}
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

TEST(ProfilerTest, TestHostTracerForMem) {
  using paddle::platform::CPUPlace;
  using paddle::platform::EnableHostEventRecorder;
  using paddle::platform::MemTraceEventNode;
  using paddle::platform::Profiler;
  using paddle::platform::ProfilerOptions;
  using paddle::platform::ProfilerResult;
  using paddle::platform::RecordEvent;
  using paddle::platform::RecordInstantEvent;
  using paddle::platform::RecordMemEvent;
  using paddle::platform::TracerEventType;
  using paddle::platform::TracerMemEventType;
  ProfilerOptions options;
  options.trace_level = 1;
  options.trace_switch = 3;
  auto profiler = Profiler::Create(options);
  EXPECT_TRUE(profiler);
  EnableHostEventRecorder();
  profiler->Prepare();
  profiler->Start();
  {
    RecordEvent event1(
        "TestTracerForMem_phase1", TracerEventType::UserDefined, 1);
    RecordMemEvent(reinterpret_cast<void*>(0),
                   CPUPlace(),
                   1024,
                   TracerMemEventType::Allocate);
    RecordMemEvent(
        reinterpret_cast<void*>(0), CPUPlace(), 1024, TracerMemEventType::Free);
  }
  {
    RecordEvent event2(
        "TestTracerForMem_phase2", TracerEventType::UserDefined, 1);
    RecordMemEvent(reinterpret_cast<void*>(1024),
                   CPUPlace(),
                   1024,
                   TracerMemEventType::Allocate);
    RecordMemEvent(reinterpret_cast<void*>(1024),
                   CPUPlace(),
                   1024,
                   TracerMemEventType::Free);
  }
  auto profiler_result = profiler->Stop();
  auto nodetree = profiler_result->GetNodeTrees();
}