profiler.cpp 2.5 KB
Newer Older
1 2
#include "megbrain/imperative/profiler.h"

3
#include <chrono>
4
#include <unordered_map>
5

6
#include "megbrain/imperative/cpp_cupti.h"
7 8 9
#include "megbrain/imperative/ops/opr_attr.h"
#include "megbrain/imperative/physical_tensor.h"

10 11
#include "megbrain/plugin/opr_footprint.h"

12
#include "./event_pool.h"
M
Megvii Engine Team 已提交
13
#include "./function_hook.h"
14 15
#include "./op_trait.h"

16
#include "./profiler/formats.h"
17
#include "./profiler/states.h"
18

19 20 21
namespace mgb {
namespace imperative {

22
profiler::Time Timer::record_host() {
23
    return std::chrono::system_clock::now();
24 25
}

26
std::shared_ptr<CompNode::Event> Timer::record_device(CompNode device) {
27 28 29
    auto event = EventPool::with_timer().alloc_shared(device);
    event->record();
    return event;
30 31
}

32
std::vector<Profiler::entry_t> Profiler::sm_records;
33 34
Profiler::options_t Profiler::sm_profile_options;
std::mutex Profiler::sm_mutex;
35
std::unordered_map<std::thread::id, std::unique_ptr<Profiler>> Profiler::sm_profilers;
36
Timer Profiler::sm_timer;
37
profiler::HostTime Profiler::sm_start_at = profiler::HostTime::min();
38 39
std::atomic_uint64_t Profiler::sm_last_id = 0;
bool Profiler::sm_profiling = false;
40
thread_local Profiler* Profiler::tm_profiler = nullptr;
41 42
std::atomic_size_t Profiler::sm_preferred_capacity;

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
void Profiler::start_profile() {
    mgb_assert(!sm_profiling);
    sm_start_at = Timer::record_host();
    sm_profiling = true;
    if (cupti::enabled()) {
        MGB_RECORD_EVENT(profiler::CUPTITimestampEvent, cupti::clock::now());
    }
}

void Profiler::stop_profile() {
    mgb_assert(sm_profiling);
    cupti::flush();
    sm_profiling = false;
}

58 59 60 61 62
void Profiler::stop_step() {
    mgb_assert(sm_profiling);
    MGB_RECORD_EVENT(profiler::StopStepEvent);
}

63 64
auto Profiler::get_thread_dict() -> thread_dict_t {
    thread_dict_t thread_dict;
M
Megvii Engine Team 已提交
65
    for (auto&& [tid, profiler] : sm_profilers) {
66
        thread_dict[tid] = sys::get_thread_name(tid);
67 68
    }
    return thread_dict;
69 70
}

71
void Profiler::dump_profile(std::string basename, std::string format, bundle_t result) {
M
Megvii Engine Team 已提交
72 73 74 75 76
    static std::unordered_map<std::string, void (*)(std::string, bundle_t)>
            format_table = {
                    {"chrome_timeline.json", profiler::dump_chrome_timeline},
                    {"memory_flow.svg", profiler::dump_memory_flow},
            };
77 78
    auto iter = format_table.find(format);
    if (iter == format_table.end()) {
79 80
        mgb_log_error("unsupported profiling format %s", format.c_str());
    }
81
    return (iter->second)(basename, std::move(result));
82 83
}

84 85 86
}  // namespace imperative

}  // namespace mgb