profiler_test.cc 3.0 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
C
chenjian 已提交
26
#include "paddle/fluid/platform/profiler/event_python.h"
27 28 29 30 31
#include "paddle/fluid/platform/profiler/event_tracing.h"
#include "paddle/fluid/platform/profiler/profiler.h"

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

TEST(ProfilerTest, TestCudaTracer) {
  using paddle::platform::Profiler;
63
  using paddle::platform::ProfilerOptions;
C
chenjian 已提交
64
  using paddle::platform::ProfilerResult;
L
liutiexing 已提交
65 66
  ProfilerOptions options;
  options.trace_level = 0;
C
chenjian 已提交
67
  options.trace_switch = 3;
L
liutiexing 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81
  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 已提交
82
  auto profiler_result = profiler->Stop();
L
liutiexing 已提交
83
  auto nodetree = profiler_result->GetNodeTrees();
L
liutiexing 已提交
84 85 86 87 88 89 90 91 92 93 94 95
  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
}