profiler.cpp 2.6 KB
Newer Older
1
/**
M
Megvii Engine Team 已提交
2 3
 * \file imperative/src/impl/profiler.cpp
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
4
 *
5
 * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
6
 *
M
Megvii Engine Team 已提交
7 8 9
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 11 12 13
 */

#include "megbrain/imperative/profiler.h"

14 15
#include <chrono>

16 17 18
#include "megbrain/imperative/ops/opr_attr.h"
#include "megbrain/imperative/physical_tensor.h"

19 20
#include "megbrain/plugin/opr_footprint.h"

21
#include "./function_hook.h"
22
#include "./event_pool.h"
23 24
#include "./op_trait.h"

25 26
#include "./profiler/formats.h"

27 28 29
namespace mgb {
namespace imperative {

30 31 32 33 34
uint64_t Timer::get_nsecs() {
    using namespace std::chrono;
    auto finish = steady_clock::now();
    auto duration = duration_cast<nanoseconds>(finish - m_start);
    return duration.count();
35 36
}

37 38
uint64_t Timer::get_started_at() {
    return m_started_at;
39 40
}

41 42 43 44 45
void Timer::reset() {
    using namespace std::chrono;
    m_start = steady_clock::now();
    auto now_ns = duration_cast<nanoseconds>(std::chrono::system_clock::now().time_since_epoch());
    m_started_at = now_ns.count();
46 47
}

48 49 50 51
std::shared_ptr<CompNode::Event> Timer::record_event(CompNode device) {
    auto event = EventPool::with_timer().alloc_shared(device);
    event->record();
    return event;
52 53
}

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
Profiler::options_t Profiler::sm_profile_options;
std::mutex Profiler::sm_mutex;
std::unordered_map<std::thread::id, Profiler*> Profiler::sm_profilers;
Timer Profiler::sm_timer;
std::atomic_uint64_t Profiler::sm_last_id = 0;
bool Profiler::sm_profiling = false;
thread_local std::unique_ptr<Profiler> Profiler::tm_profiler = std::make_unique<Profiler>();
std::atomic_size_t Profiler::sm_preferred_capacity;

auto Profiler::get_thread_dict() -> thread_dict_t {
    MGB_LOCK_GUARD(sm_mutex);
    thread_dict_t thread_dict;
    for (auto&& [tid, profiler]: sm_profilers) {
        thread_dict[tid] = profiler->m_thread_name;
    }
    return thread_dict;
70 71
}

72 73
void Profiler::dump_profile(std::string basename, std::string format, results_t results, options_t options) {
    auto thread_dict = get_thread_dict();
74 75
    if (format == "chrome_timeline.json") {
        profiler::dump_chrome_timeline(basename, options, thread_dict, results);
76 77
    } else if (format == "memory_flow.svg") {
        profiler::dump_memory_flow(basename, options, thread_dict, results);
78
    } else {
79 80
        mgb_log_error("unsupported profiling format %s", format.c_str());
    }
81 82
}

83 84 85
}  // namespace imperative

}  // namespace mgb