profiler.cc 19.0 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
D
dangqingqing 已提交
2 3 4 5

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
6

D
dangqingqing 已提交
7 8 9 10 11 12 13 14
    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. */

15
#include <mutex>  // NOLINT
16
#include <random>
L
liutiexing 已提交
17
#include <sstream>
18
#include <string>
L
liutiexing 已提交
19
#include <type_traits>
Y
Yancey1989 已提交
20

21
#include "paddle/fluid/platform/device_tracer.h"
W
wangchaochaohu 已提交
22 23
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/profiler.h"
24
#include "paddle/fluid/platform/profiler/common_event.h"
L
liutiexing 已提交
25
#include "paddle/fluid/platform/profiler/host_event_recorder.h"
26
#include "paddle/fluid/platform/profiler/host_tracer.h"
W
wangchaochaohu 已提交
27
#include "paddle/fluid/platform/profiler_helper.h"
28 29 30
#ifdef PADDLE_WITH_CUDA
#include "paddle/fluid/platform/dynload/nvtx.h"
#endif
31
#include "paddle/fluid/platform/os_info.h"
D
dangqingqing 已提交
32

Z
Zeng Jinle 已提交
33 34
PADDLE_DEFINE_EXPORTED_bool(enable_rpc_profiler, false,
                            "Enable rpc profiler or not.");
G
gongweibao 已提交
35

36 37 38
DEFINE_bool(enable_host_event_recorder_hook, false,
            "enable HostEventRecorder, hook Profiler");

D
dangqingqing 已提交
39 40 41
namespace paddle {
namespace platform {

W
wangchaochaohu 已提交
42
MemEvenRecorder MemEvenRecorder::recorder;
D
dangqingqing 已提交
43

44
Event::Event(EventType type, std::string name, uint32_t thread_id,
Y
Yuang Liu 已提交
45 46 47 48 49 50
             EventRole role, std::string attr)
    : type_(type),
      name_(name),
      thread_id_(thread_id),
      role_(role),
      attr_(attr) {
D
dangqingqing 已提交
51 52 53
  cpu_ns_ = GetTimeInNsec();
}

C
chengduo 已提交
54
const EventType &Event::type() const { return type_; }
D
dangqingqing 已提交
55

C
chengduo 已提交
56
double Event::CpuElapsedMs(const Event &e) const {
57
  return (e.cpu_ns_ - cpu_ns_) / (1000000.0);
D
dangqingqing 已提交
58 59
}

C
chengduo 已提交
60
double Event::CudaElapsedMs(const Event &e) const {
61 62
#ifdef PADDLE_WITH_CUPTI
  return gpu_ns_ / 1000000.0;
D
Dun Liang 已提交
63
#else
D
Dun Liang 已提交
64 65
  LOG_FIRST_N(WARNING, 1) << "CUDA CUPTI is not enabled";
  return 0;
D
dangqingqing 已提交
66 67 68
#endif
}

C
chenjian 已提交
69 70
RecordEvent::RecordEvent(const char *name, const TracerEventType type,
                         uint32_t level, const EventRole role) {
L
liutiexing 已提交
71 72 73 74 75 76 77 78
#ifndef _WIN32
#ifdef PADDLE_WITH_CUDA
  if (g_enable_nvprof_hook) {
    dynload::nvtxRangePushA(name);
    is_pushed_ = true;
  }
#endif
#endif
79
  if (FLAGS_enable_host_event_recorder_hook == false) {
L
liutiexing 已提交
80 81 82
    if (g_state != ProfilerState::kDisabled) {  // avoid temp string
      OriginalConstruct(name, role, "none");
    }
L
liutiexing 已提交
83 84
    return;
  }
85 86 87 88
  if (UNLIKELY(HostTraceLevel::GetInstance().NeedTrace(level) == false)) {
    return;
  }
  is_enabled_ = true;
L
liutiexing 已提交
89 90
  shallow_copy_name_ = name;
  role_ = role;
C
chenjian 已提交
91
  type_ = type;
L
liutiexing 已提交
92 93 94
  start_ns_ = PosixInNsec();
}

C
chenjian 已提交
95 96
RecordEvent::RecordEvent(const std::string &name, const TracerEventType type,
                         uint32_t level, const EventRole role) {
L
liutiexing 已提交
97 98 99 100 101 102 103 104
#ifndef _WIN32
#ifdef PADDLE_WITH_CUDA
  if (g_enable_nvprof_hook) {
    dynload::nvtxRangePushA(name.c_str());
    is_pushed_ = true;
  }
#endif
#endif
105
  if (FLAGS_enable_host_event_recorder_hook == false) {
L
liutiexing 已提交
106
    OriginalConstruct(name, role, "none");
L
liutiexing 已提交
107 108
    return;
  }
109 110 111 112
  if (UNLIKELY(HostTraceLevel::GetInstance().NeedTrace(level) == false)) {
    return;
  }
  is_enabled_ = true;
L
liutiexing 已提交
113 114
  name_ = new std::string(name);
  role_ = role;
C
chenjian 已提交
115
  type_ = type;
L
liutiexing 已提交
116 117 118
  start_ns_ = PosixInNsec();
}

C
chenjian 已提交
119 120 121
RecordEvent::RecordEvent(const std::string &name, const std::string &attr,
                         const TracerEventType type, uint32_t level,
                         const EventRole role) {
122 123 124 125 126 127 128 129
#ifndef _WIN32
#ifdef PADDLE_WITH_CUDA
  if (g_enable_nvprof_hook) {
    dynload::nvtxRangePushA(name.c_str());
    is_pushed_ = true;
  }
#endif
#endif
130
  if (FLAGS_enable_host_event_recorder_hook == false) {
L
liutiexing 已提交
131
    OriginalConstruct(name, role, attr);
L
liutiexing 已提交
132 133
    return;
  }
134 135 136 137
  if (UNLIKELY(HostTraceLevel::GetInstance().NeedTrace(level) == false)) {
    return;
  }
  is_enabled_ = true;
C
chenjian 已提交
138
  type_ = type;
L
liutiexing 已提交
139 140 141 142
  name_ = new std::string(name);
  start_ns_ = PosixInNsec();
  attr_ = new std::string(attr);
}
L
liutiexing 已提交
143

L
liutiexing 已提交
144 145 146
void RecordEvent::OriginalConstruct(const std::string &name,
                                    const EventRole role,
                                    const std::string &attr) {
147
  if (g_state == ProfilerState::kDisabled || name.empty()) return;
148 149

  // do some initialization
L
liutiexing 已提交
150
  name_ = new std::string(name);
151 152
  start_ns_ = PosixInNsec();
  role_ = role;
L
liutiexing 已提交
153
  attr_ = new std::string(attr);
X
Xin Pan 已提交
154
  is_enabled_ = true;
155
  // lock is not needed, the code below is thread-safe
156
  // Maybe need the same push/pop behavior.
Y
Yuang Liu 已提交
157
  Event *e = PushEvent(name, role, attr);
158
  SetCurAnnotation(e);
L
liutiexing 已提交
159
  *name_ = e->name();
D
dangqingqing 已提交
160 161
}

L
liutiexing 已提交
162
void RecordEvent::End() {
163 164
#ifndef _WIN32
#ifdef PADDLE_WITH_CUDA
165
  if (g_enable_nvprof_hook && is_pushed_) {
166
    dynload::nvtxRangePop();
167
    is_pushed_ = false;
168 169 170
  }
#endif
#endif
171
  if (LIKELY(FLAGS_enable_host_event_recorder_hook && is_enabled_)) {
L
liutiexing 已提交
172
    uint64_t end_ns = PosixInNsec();
L
liutiexing 已提交
173
    if (LIKELY(shallow_copy_name_ != nullptr)) {
C
chenjian 已提交
174 175
      HostEventRecorder::GetInstance().RecordEvent(
          shallow_copy_name_, start_ns_, end_ns, role_, type_);
L
liutiexing 已提交
176 177
    } else if (name_ != nullptr) {
      if (attr_ == nullptr) {
C
chenjian 已提交
178 179
        HostEventRecorder::GetInstance().RecordEvent(*name_, start_ns_, end_ns,
                                                     role_, type_);
L
liutiexing 已提交
180
      } else {
C
chenjian 已提交
181 182
        HostEventRecorder::GetInstance().RecordEvent(*name_, start_ns_, end_ns,
                                                     role_, type_, *attr_);
L
liutiexing 已提交
183
        delete attr_;
L
liutiexing 已提交
184
      }
L
liutiexing 已提交
185
      delete name_;
L
liutiexing 已提交
186
    }
187 188
    // use this flag to avoid double End();
    is_enabled_ = false;
L
liutiexing 已提交
189 190 191
    return;
  }

X
Xin Pan 已提交
192
  if (g_state == ProfilerState::kDisabled || !is_enabled_) return;
193
  // lock is not needed, the code below is thread-safe
C
chengduo 已提交
194
  DeviceTracer *tracer = GetDeviceTracer();
X
Xin Pan 已提交
195
  if (tracer) {
L
liutiexing 已提交
196
    uint64_t end_ns = PosixInNsec();
L
liutiexing 已提交
197 198
    tracer->AddCPURecords(CurAnnotationName(), start_ns_, end_ns, BlockDepth(),
                          g_thread_id);
X
Xin Pan 已提交
199
  }
Y
Yibing Liu 已提交
200
  ClearCurAnnotation();
L
liutiexing 已提交
201 202 203
  PopEvent(*name_, role_);
  delete name_;
  delete attr_;
204 205
  // use this flag to avoid double End();
  is_enabled_ = false;
D
dangqingqing 已提交
206
}
D
dangqingqing 已提交
207

208 209 210
RecordInstantEvent::RecordInstantEvent(const char *name, TracerEventType type,
                                       uint32_t level) {
  if (UNLIKELY(HostTraceLevel::GetInstance().NeedTrace(level) == false)) {
L
liutiexing 已提交
211 212 213 214
    return;
  }
  auto start_end_ns = PosixInNsec();
  HostEventRecorder::GetInstance().RecordEvent(name, start_end_ns, start_end_ns,
215
                                               EventRole::kOrdinary, type);
L
liutiexing 已提交
216 217
}

C
chengduo 已提交
218 219 220 221 222
void MemEvenRecorder::PushMemRecord(const void *ptr, const Place &place,
                                    size_t size) {
  if (g_state == ProfilerState::kDisabled) return;
  std::lock_guard<std::mutex> guard(mtx_);
  auto &events = address_memevent_[place];
G
GaoWei8 已提交
223 224 225
  PADDLE_ENFORCE_EQ(events.count(ptr), 0,
                    platform::errors::InvalidArgument(
                        "The Place can't exist in the stage of PushMemRecord"));
C
chengduo 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
  events.emplace(ptr, std::unique_ptr<RecordMemEvent>(
                          new MemEvenRecorder::RecordMemEvent(place, size)));
}

void MemEvenRecorder::PopMemRecord(const void *ptr, const Place &place) {
  if (g_state == ProfilerState::kDisabled) return;
  std::lock_guard<std::mutex> guard(mtx_);
  auto &events = address_memevent_[place];
  auto iter = events.find(ptr);
  // The ptr maybe not in address_memevent
  if (iter != events.end()) {
    events.erase(iter);
  }
}

void MemEvenRecorder::Flush() {
  std::lock_guard<std::mutex> guard(mtx_);
  address_memevent_.clear();
}

MemEvenRecorder::RecordMemEvent::RecordMemEvent(const Place &place,
                                                size_t bytes)
    : place_(place),
      bytes_(bytes),
      start_ns_(PosixInNsec()),
      alloc_in_(CurAnnotationName()) {
  PushMemEvent(start_ns_, end_ns_, bytes_, place_, alloc_in_);
}

MemEvenRecorder::RecordMemEvent::~RecordMemEvent() {
  DeviceTracer *tracer = GetDeviceTracer();
  end_ns_ = PosixInNsec();

  auto annotation_free = CurAnnotationName();
  if (tracer) {
    tracer->AddMemInfoRecord(start_ns_, end_ns_, bytes_, place_, alloc_in_,
                             annotation_free, g_mem_thread_id);
  }
  PopMemEvent(start_ns_, end_ns_, bytes_, place_, annotation_free);
}

L
liutiexing 已提交
267
/*RecordRPCEvent::RecordRPCEvent(const std::string &name) {
G
gongweibao 已提交
268
  if (FLAGS_enable_rpc_profiler) {
269
    event_.reset(new platform::RecordEvent(name));
G
gongweibao 已提交
270
  }
L
liutiexing 已提交
271
}*/
G
gongweibao 已提交
272

X
Xin Pan 已提交
273 274
RecordBlock::RecordBlock(int block_id)
    : is_enabled_(false), start_ns_(PosixInNsec()) {
275
  // lock is not needed, the code below is thread-safe
X
Xin Pan 已提交
276
  if (g_state == ProfilerState::kDisabled) return;
X
Xin Pan 已提交
277
  is_enabled_ = true;
X
Xin Pan 已提交
278 279 280 281 282
  SetCurBlock(block_id);
  name_ = string::Sprintf("block_%d", block_id);
}

RecordBlock::~RecordBlock() {
283
  // lock is not needed, the code below is thread-safe
X
Xin Pan 已提交
284
  if (g_state == ProfilerState::kDisabled || !is_enabled_) return;
C
chengduo 已提交
285
  DeviceTracer *tracer = GetDeviceTracer();
X
Xin Pan 已提交
286 287 288 289
  if (tracer) {
    // We try to put all blocks at the same nested depth in the
    // same timeline lane. and distinguish the using thread_id.
    tracer->AddCPURecords(name_, start_ns_, PosixInNsec(), BlockDepth(),
290
                          g_thread_id);
X
Xin Pan 已提交
291 292 293 294
  }
  ClearCurBlock();
}

W
wangchaochaohu 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307
void PushMemEvent(uint64_t start_ns, uint64_t end_ns, size_t bytes,
                  const Place &place, const std::string &annotation) {
  GetMemEventList().Record(EventType::kPushRange, start_ns, end_ns, bytes,
                           place, g_mem_thread_id, annotation);
}

void PopMemEvent(uint64_t start_ns, uint64_t end_ns, size_t bytes,
                 const Place &place, const std::string &annotation) {
  GetMemEventList().Record(EventType::kPopRange, start_ns, end_ns, bytes, place,
                           g_mem_thread_id, annotation);
}

void Mark(const std::string &name) {
308
  if (FLAGS_enable_host_event_recorder_hook) {
309
    HostEventRecorder::GetInstance().RecordEvent(
C
chenjian 已提交
310
        name, 0, 0, EventRole::kOrdinary, TracerEventType::UserDefined);
311 312
    return;
  }
W
wangchaochaohu 已提交
313 314 315
  GetEventList().Record(EventType::kMark, name, g_thread_id);
}

Y
Yuang Liu 已提交
316 317 318 319
Event *PushEvent(const std::string &name, const EventRole role,
                 std::string attr) {
  return GetEventList().Record(EventType::kPushRange, name, g_thread_id, role,
                               attr);
320 321
}

Y
Yuang Liu 已提交
322 323
void PopEvent(const std::string &name, const EventRole role, std::string attr) {
  GetEventList().Record(EventType::kPopRange, name, g_thread_id, role, attr);
W
wangchaochaohu 已提交
324
}
D
dangqingqing 已提交
325
void EnableProfiler(ProfilerState state) {
W
wangchaochaohu 已提交
326 327 328 329
  PADDLE_ENFORCE_NE(state, ProfilerState::kDisabled,
                    platform::errors::InvalidArgument(
                        "Can't enable profiling, since the input state is"
                        "ProfilerState::kDisabled"));
330
  SynchronizeAllDevice();
X
Xin Pan 已提交
331
  std::lock_guard<std::mutex> l(profiler_mu);
332 333
  if (state == g_state) {
    return;
334
  }
335
  g_state = state;
X
Xin Pan 已提交
336
  should_send_profile_state = true;
337
  GetDeviceTracer()->Enable();
338
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
339 340
  if (g_state == ProfilerState::kCUDA || g_state == ProfilerState::kAll ||
      g_state == ProfilerState::kCPU) {
341
    // Generate some dummy events first to reduce the startup overhead.
342 343
    DummyKernelAndEvent();
    GetDeviceTracer()->Reset();
D
dangqingqing 已提交
344 345 346
  }
#endif
  // Mark the profiling start.
347
  Mark("_start_profiler_");
D
dangqingqing 已提交
348 349
}

350
void ResetProfiler() {
351 352
  SynchronizeAllDevice();
  GetDeviceTracer()->Reset();
C
chengduo 已提交
353
  MemEvenRecorder::Instance().Flush();
D
dangqingqing 已提交
354
  std::lock_guard<std::mutex> guard(g_all_event_lists_mutex);
355 356 357 358
  for (auto it = g_all_event_lists.begin(); it != g_all_event_lists.end();
       ++it) {
    (*it)->Clear();
  }
C
chengduo 已提交
359 360 361 362
  for (auto it = g_all_mem_event_lists.begin();
       it != g_all_mem_event_lists.end(); ++it) {
    (*it)->Clear();
  }
363 364
}

365 366 367 368
static std::map<uint64_t, ThreadEvents> DockHostEventRecorderHostPart();
static void DockHostEventRecorderDevicePart(
    const std::map<uint64_t, ThreadEvents> &thr_events);

369
void DisableProfiler(EventSortingKey sorted_key,
C
chengduo 已提交
370
                     const std::string &profile_path) {
371
  SynchronizeAllDevice();
372
  auto thr_events = DockHostEventRecorderHostPart();
C
chengduo 已提交
373 374
  MemEvenRecorder::Instance().Flush();

X
Xin Pan 已提交
375
  std::lock_guard<std::mutex> l(profiler_mu);
376
  if (g_state == ProfilerState::kDisabled) return;
377
  // Mark the profiling stop.
378
  Mark("_stop_profiler_");
379
  DealWithShowName();
380

C
chengduo 已提交
381
  DeviceTracer *tracer = GetDeviceTracer();
382
  if (tracer->IsEnabled()) {
383
    tracer->Disable();
384
    DockHostEventRecorderDevicePart(thr_events);
385
    tracer->GenEventKernelCudaElapsedTime();
386
    tracer->GenProfile(profile_path);
387
  }
388 389

  std::vector<std::vector<Event>> all_events = GetAllEvents();
390

391 392
  ParseEvents(all_events, true, sorted_key);
  ParseEvents(all_events, false, sorted_key);
H
Huihuang Zheng 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406

  std::vector<std::vector<MemEvent>> all_mem_events = GetMemEvents();
  ParseMemEvents(all_mem_events);

  ResetProfiler();
  g_state = ProfilerState::kDisabled;
  g_tracer_option = TracerOption::kDefault;
  should_send_profile_state = true;
}

void CompleteProfilerEvents(proto::Profile *tracer_profile,
                            std::vector<std::vector<Event>> *time_events,
                            std::vector<std::vector<MemEvent>> *mem_events) {
  SynchronizeAllDevice();
407
  auto thr_events = DockHostEventRecorderHostPart();
H
Huihuang Zheng 已提交
408 409 410 411 412 413 414 415 416 417 418
  MemEvenRecorder::Instance().Flush();

  std::lock_guard<std::mutex> l(profiler_mu);
  if (g_state == ProfilerState::kDisabled) return;

  // Mark the profiling stop.
  Mark("_stop_profiler_");

  DeviceTracer *tracer = GetDeviceTracer();
  if (tracer->IsEnabled() && tracer_profile != nullptr) {
    tracer->Disable();
419
    DockHostEventRecorderDevicePart(thr_events);
H
Huihuang Zheng 已提交
420 421 422 423 424 425 426 427 428
    tracer->GenEventKernelCudaElapsedTime();
    *tracer_profile = tracer->GetProfile();
  }

  if (time_events != nullptr) {
    *time_events = GetAllEvents();
  }
  if (mem_events != nullptr) {
    *mem_events = GetMemEvents();
C
chengduo 已提交
429 430
  }

431
  ResetProfiler();
432
  g_state = ProfilerState::kDisabled;
433
  g_tracer_option = TracerOption::kDefault;
X
Xin Pan 已提交
434
  should_send_profile_state = true;
435 436
}

W
wangchaochaohu 已提交
437 438 439 440 441 442 443 444 445 446
std::vector<std::vector<Event>> GetAllEvents() {
  std::lock_guard<std::mutex> guard(g_all_event_lists_mutex);
  std::vector<std::vector<Event>> result;
  for (auto it = g_all_event_lists.begin(); it != g_all_event_lists.end();
       ++it) {
    result.emplace_back((*it)->Reduce());
  }
  return result;
}

447 448
bool IsProfileEnabled() { return g_state != ProfilerState::kDisabled; }

W
wangchaochaohu 已提交
449
bool ShouldSendProfileState() { return should_send_profile_state; }
450

451 452
std::string OpName(const framework::VariableNameMap &name_map,
                   const std::string &type_name) {
453 454
  if (platform::GetTracerOption() != platform::TracerOption::kAllOpDetail ||
      !IsProfileEnabled())
455 456 457 458 459
    return "";

  std::string ret = type_name + "%";
  for (auto it = name_map.begin(); it != name_map.end(); it++) {
    auto name_outputs = it->second;
460
    if (!name_outputs.empty()) {
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
      ret = ret + name_outputs[0];
      break;
    }
  }
  ret = ret + "%";

  return ret;
}

void SetTracerOption(TracerOption option) {
  std::lock_guard<std::mutex> l(profiler_mu);
  g_tracer_option = option;
}

platform::TracerOption GetTracerOption() { return g_tracer_option; }
W
wangchaochaohu 已提交
476 477 478 479 480 481 482 483 484 485 486

void SetProfileListener() {
  std::mt19937 rng;
  rng.seed(std::random_device()());
  std::uniform_int_distribution<std::mt19937::result_type> dist6(
      1, std::numeric_limits<int>::max());
  profiler_lister_id = dist6(rng);
}

int64_t ListenerId() { return profiler_lister_id; }

487 488 489 490 491 492 493
void NvprofEnableRecordEvent() {
  SynchronizeAllDevice();
  g_enable_nvprof_hook = true;
}

void NvprofDisableRecordEvent() { g_enable_nvprof_hook = false; }

494
void EnableHostEventRecorder() { FLAGS_enable_host_event_recorder_hook = true; }
L
liutiexing 已提交
495

C
chenjian 已提交
496 497 498 499
void DisableHostEventRecorder() {
  FLAGS_enable_host_event_recorder_hook = false;
}

L
liutiexing 已提交
500 501 502 503 504 505
std::string PrintHostEvents() {
  std::ostringstream oss;
  auto host_evt_sec = HostEventRecorder::GetInstance().GatherEvents();
  for (const auto &thr_evt_sec : host_evt_sec.thr_sections) {
    oss << thr_evt_sec.thread_id << std::endl;
    for (const auto &evt : thr_evt_sec.events) {
L
liutiexing 已提交
506 507 508
      oss << "{ " << evt.name << " | " << evt.start_ns << "ns | " << evt.end_ns
          << "ns | " << (evt.end_ns - evt.start_ns) / 1000.000 << "us }"
          << std::endl;
L
liutiexing 已提交
509 510 511 512 513
    }
  }
  return oss.str();
}

514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
static void EmulateEventPushAndPop(const HostEventSection &host_sec,
                                   std::map<uint64_t, ThreadEvents> *out) {
  for (const auto &thr_sec : host_sec.thr_sections) {
    uint64_t tid = thr_sec.thread_id;
    auto cur_thr_list = std::make_shared<EventList<Event>>();
    g_all_event_lists.emplace_front(cur_thr_list);
    // for nesting events
    std::stack<size_t> evt_stk;
    std::stack<std::string> prefix_stk;
    std::map<uint64_t, size_t> start2evt;
    for (size_t i = 0; i < thr_sec.events.size(); ++i) {
      const auto &evt = thr_sec.events[i];
      start2evt[evt.start_ns] = i;
    }
    auto iter = start2evt.begin();
    // loop events
    for (size_t i = 0; i < thr_sec.events.size(); ++i) {
      const auto &thr_evts = thr_sec.events;
      const auto &evt = thr_evts[i];
      // For nesting events
      while (!evt_stk.empty() && thr_evts[evt_stk.top()].end_ns <= evt.end_ns) {
        evt_stk.pop();
        prefix_stk.pop();
      }
      while (iter != start2evt.end() &&
             thr_evts[iter->second].start_ns < evt.start_ns) {
        if (thr_evts[iter->second].end_ns > evt.start_ns) {
          evt_stk.push(iter->second);
          std::string prefix = thr_evts[iter->second].name;
          if (!prefix_stk.empty()) {
            prefix = prefix_stk.top() + "/" + prefix;
          }
          prefix_stk.push(prefix);
        }
        ++iter;
      }
      // Record orig event pair
      std::string name =
          prefix_stk.empty() ? evt.name : prefix_stk.top() + "/" + evt.name;
      const char *attr = (evt.attr == nullptr ? "none" : evt.attr);
      Event *orig_evt = cur_thr_list->Record(EventType::kPushRange, name, tid,
                                             evt.role, attr);
      (*out)[tid][evt.end_ns] = std::make_pair(orig_evt, evt.start_ns);
      cur_thr_list->Record(EventType::kPopRange, name, tid, evt.role, attr);
    }
  }
}

static void EmulateCPURecordsAdd(const HostEventSection &host_sec) {
  DeviceTracer *tracer = GetDeviceTracer();
  if (tracer == nullptr) {
    return;
  }
  for (const auto &thr_sec : host_sec.thr_sections) {
    uint64_t tid = thr_sec.thread_id;
    for (const auto &evt : thr_sec.events) {
      tracer->AddCPURecords(evt.name, evt.start_ns, evt.end_ns, BlockDepth(),
                            tid);
    }
  }
}

static void EmulateCorrelation(
    const std::map<uint64_t, ThreadEvents> &thr_events) {
  DeviceTracer *tracer = GetDeviceTracer();
  if (tracer == nullptr) {
    return;
  }
  tracer->AddAnnotations(thr_events);
}

static std::map<uint64_t, ThreadEvents> DockHostEventRecorderHostPart() {
  std::map<uint64_t, ThreadEvents> thr_events;
  if (FLAGS_enable_host_event_recorder_hook == false) {
    return thr_events;
  }
  auto host_evt_sec = HostEventRecorder::GetInstance().GatherEvents();
  EmulateEventPushAndPop(host_evt_sec, &thr_events);
  EmulateCPURecordsAdd(host_evt_sec);
  return std::move(thr_events);
}

static void DockHostEventRecorderDevicePart(
    const std::map<uint64_t, ThreadEvents> &thr_events) {
  if (FLAGS_enable_host_event_recorder_hook == false) {
    return;
  }
  EmulateCorrelation(thr_events);
}

D
dangqingqing 已提交
604 605
}  // namespace platform
}  // namespace paddle