parent
21670a5777
commit
2617f6c620
@ -1,14 +1,23 @@
|
||||
if(ENABLE_GPU)
|
||||
file(GLOB_RECURSE PROFILER_SRC_LIST RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "device/gpu/*.cc")
|
||||
file(GLOB_RECURSE PROFILER_SRC_LIST RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
|
||||
"device/gpu/*.cc" "device/cpu/*.cc")
|
||||
set_property(SOURCE ${PROFILER_SRC_LIST} PROPERTY COMPILE_DEFINITIONS
|
||||
SUBMODULE_ID=mindspore::SubModuleId::SM_PROFILER)
|
||||
add_library(_mindspore_profiler_obj OBJECT ${PROFILER_SRC_LIST})
|
||||
endif()
|
||||
|
||||
if(ENABLE_D)
|
||||
file(GLOB_RECURSE PROFILER_SRC_LIST RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "device/ascend/*.cc" "device/common/*.cc")
|
||||
file(GLOB_RECURSE PROFILER_SRC_LIST RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
|
||||
"device/common/*.cc" "device/ascend/*.cc" "device/cpu/*.cc")
|
||||
set_property(SOURCE ${PROFILER_SRC_LIST} PROPERTY COMPILE_DEFINITIONS
|
||||
SUBMODULE_ID=mindspore::SubModuleId::SM_PROFILER)
|
||||
add_library(_mindspore_profiler_obj OBJECT ${PROFILER_SRC_LIST})
|
||||
add_dependencies(_mindspore_profiler_obj mindspore::protobuf)
|
||||
endif()
|
||||
|
||||
if(ENABLE_CPU AND NOT (ENABLE_D OR ENABLE_GPU))
|
||||
file(GLOB_RECURSE PROFILER_SRC_LIST RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "device/cpu/*.cc")
|
||||
set_property(SOURCE ${PROFILER_SRC_LIST} PROPERTY COMPILE_DEFINITIONS
|
||||
SUBMODULE_ID=mindspore::SubModuleId::SM_PROFILER)
|
||||
add_library(_mindspore_profiler_obj OBJECT ${PROFILER_SRC_LIST})
|
||||
endif()
|
@ -0,0 +1,174 @@
|
||||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#include "profiler/device/cpu/cpu_data_saver.h"
|
||||
#include <fstream>
|
||||
#include <numeric>
|
||||
#include "sys/stat.h"
|
||||
#include "utils/log_adapter.h"
|
||||
#include "utils/ms_utils.h"
|
||||
#include "utils/ms_context.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace profiler {
|
||||
namespace cpu {
|
||||
OpDetailInfo::OpDetailInfo(std::shared_ptr<OpInfo> op_info, float proportion)
|
||||
: op_info_(op_info), proportion_(proportion) {
|
||||
// op_full_name is like 'xxx/xxx/{op_type}-op{node_id}'
|
||||
op_full_name_ = op_info->op_name;
|
||||
auto op_type_begin_iter = op_full_name_.rfind('/') + 1;
|
||||
auto op_type_end_iter = op_full_name_.rfind('-');
|
||||
op_type_ = op_full_name_.substr(op_type_begin_iter, op_type_end_iter - op_type_begin_iter);
|
||||
op_name_ = op_full_name_.substr(op_type_begin_iter);
|
||||
op_avg_time_ = op_info->op_cost_time / op_info->op_count;
|
||||
}
|
||||
|
||||
void DataSaver::ParseOpInfo(const OpInfoMap &op_info_maps) {
|
||||
const float factor_percent = 100;
|
||||
op_detail_infos_.reserve(op_info_maps.size());
|
||||
float total_time_sum = GetTotalOpTime(op_info_maps);
|
||||
for (auto item : op_info_maps) {
|
||||
op_timestamps_map_[item.first] = item.second.start_duration;
|
||||
float proportion = item.second.op_cost_time / total_time_sum * factor_percent;
|
||||
auto op_info = std::make_shared<OpInfo>(item.second);
|
||||
OpDetailInfo op_detail_info = OpDetailInfo(op_info, proportion);
|
||||
op_detail_infos_.emplace_back(op_detail_info);
|
||||
AddOpDetailInfoForType(op_detail_info);
|
||||
}
|
||||
// update average time of op type
|
||||
for (auto &op_type : op_type_infos_) {
|
||||
// device_infos: <type_name, op_type_info>
|
||||
op_type.second.avg_time_ = op_type.second.total_time_ / op_type.second.count_;
|
||||
}
|
||||
MS_LOG(DEBUG) << "Get " << op_detail_infos_.size() << " operation items.";
|
||||
MS_LOG(DEBUG) << "Get " << op_type_infos_.size() << " operation type items.";
|
||||
}
|
||||
|
||||
void DataSaver::AddOpDetailInfoForType(const OpDetailInfo &op_detail_info) {
|
||||
// Construct OpType object according to op detail info
|
||||
OpType op_type = OpType{op_detail_info.op_type_,
|
||||
op_detail_info.op_info_->op_count,
|
||||
op_detail_info.op_info_->op_count,
|
||||
op_detail_info.op_info_->op_cost_time,
|
||||
0,
|
||||
op_detail_info.proportion_};
|
||||
// Set the OpType into op_type_infos_ map
|
||||
std::string type_name = op_detail_info.op_type_;
|
||||
auto iter = op_type_infos_.find(type_name);
|
||||
if (iter == op_type_infos_.end()) {
|
||||
op_type_infos_.emplace(type_name, op_type);
|
||||
} else {
|
||||
iter->second += op_type;
|
||||
}
|
||||
}
|
||||
|
||||
float DataSaver::GetTotalOpTime(const OpInfoMap &op_info_maps) {
|
||||
float sum = 0;
|
||||
sum = std::accumulate(op_info_maps.begin(), op_info_maps.end(), sum,
|
||||
[](float i, auto iter) { return i + iter.second.op_cost_time; });
|
||||
MS_LOG(DEBUG) << "The total op time is " << sum;
|
||||
return sum;
|
||||
}
|
||||
|
||||
void DataSaver::WriteFile(std::string out_path_dir) {
|
||||
if (op_detail_infos_.empty() || op_type_infos_.empty()) {
|
||||
MS_LOG(INFO) << "No cpu operation detail infos to write.";
|
||||
return;
|
||||
}
|
||||
auto context_ptr = MsContext::GetInstance();
|
||||
MS_EXCEPTION_IF_NULL(context_ptr);
|
||||
auto device_id = context_ptr->get_param<uint32_t>(MS_CTX_DEVICE_ID);
|
||||
device_id_ = std::to_string(device_id);
|
||||
WriteOpDetail(out_path_dir);
|
||||
WriteOpType(out_path_dir);
|
||||
WriteOpTimestamp(out_path_dir);
|
||||
}
|
||||
|
||||
void DataSaver::WriteOpType(const std::string &saver_base_dir) {
|
||||
std::string file_path = saver_base_dir + "/cpu_op_type_info_" + device_id_ + ".csv";
|
||||
std::ofstream ofs(file_path);
|
||||
// check if the file is writable
|
||||
if (!ofs.is_open()) {
|
||||
MS_LOG(WARNING) << "Open file '" << file_path << "' failed!";
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// write op type info into file
|
||||
ofs << OpType().GetHeader() << std::endl;
|
||||
for (auto op_type_info : op_type_infos_) {
|
||||
ofs << op_type_info.second << std::endl;
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
MS_LOG(ERROR) << "Write " << file_path << "failed: " << e.what();
|
||||
}
|
||||
ofs.close();
|
||||
ChangeFileMode(file_path);
|
||||
MS_LOG(INFO) << "Write " << op_type_infos_.size() << " op type infos into file: " << file_path;
|
||||
}
|
||||
|
||||
void DataSaver::WriteOpDetail(const std::string &saver_base_dir) {
|
||||
std::string file_path = saver_base_dir + "/cpu_op_detail_info_" + device_id_ + ".csv";
|
||||
std::ofstream ofs(file_path);
|
||||
if (!ofs.is_open()) {
|
||||
MS_LOG(WARNING) << "Open file '" << file_path << "' failed!";
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// write op detail info into file
|
||||
ofs << OpDetailInfo().GetHeader() << std::endl;
|
||||
for (auto op_detail : op_detail_infos_) {
|
||||
ofs << op_detail << std::endl;
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
MS_LOG(ERROR) << "Write " << file_path << "failed: " << e.what();
|
||||
}
|
||||
ofs.close();
|
||||
ChangeFileMode(file_path);
|
||||
MS_LOG(INFO) << "Write " << op_detail_infos_.size() << " op detail infos into file: " << file_path;
|
||||
}
|
||||
|
||||
void DataSaver::WriteOpTimestamp(const std::string &saver_base_dir) {
|
||||
std::string file_path = saver_base_dir + "/cpu_op_execute_timestamp_" + device_id_ + ".txt";
|
||||
std::ofstream ofs(file_path);
|
||||
// check if the file is writable
|
||||
if (!ofs.is_open()) {
|
||||
MS_LOG(WARNING) << "Open file '" << file_path << "' failed!";
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// write op timestamp info into file
|
||||
for (const auto &op_timestamp_info : op_timestamps_map_) {
|
||||
ofs << op_timestamp_info.first << ";host_cpu_ops;";
|
||||
for (auto start_end : op_timestamp_info.second) {
|
||||
ofs << start_end.start_timestamp << "," << start_end.duration << " ";
|
||||
}
|
||||
ofs << std::endl;
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
MS_LOG(ERROR) << "Write " << file_path << "failed: " << e.what();
|
||||
}
|
||||
ofs.close();
|
||||
ChangeFileMode(file_path);
|
||||
}
|
||||
|
||||
void DataSaver::ChangeFileMode(const std::string &file_path) {
|
||||
if (chmod(common::SafeCStr(file_path), S_IRUSR) == -1) {
|
||||
MS_LOG(WARNING) << "Modify file: " << file_path << " to rw fail.";
|
||||
return;
|
||||
}
|
||||
}
|
||||
} // namespace cpu
|
||||
} // namespace profiler
|
||||
} // namespace mindspore
|
@ -0,0 +1,123 @@
|
||||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_CPU_DATA_SAVER_H
|
||||
#define MINDSPORE_CPU_DATA_SAVER_H
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include "profiler/device/cpu/cpu_profiling.h"
|
||||
namespace mindspore {
|
||||
namespace profiler {
|
||||
namespace cpu {
|
||||
struct OpDetailInfo {
|
||||
std::string op_type_;
|
||||
std::string op_name_;
|
||||
std::string op_full_name_;
|
||||
std::shared_ptr<OpInfo> op_info_{nullptr};
|
||||
float op_avg_time_{0};
|
||||
float proportion_{0};
|
||||
|
||||
OpDetailInfo() = default;
|
||||
|
||||
OpDetailInfo(std::shared_ptr<OpInfo> op_info, float proportion);
|
||||
|
||||
std::string GetHeader() const {
|
||||
return "op_side,op_type,op_name,full_op_name,op_occurrences,compute_time(ms),"
|
||||
"avg_execution_time(ms),total_proportion,subgraph,pid";
|
||||
}
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os, const OpDetailInfo &event) {
|
||||
os << "Host," << event.op_type_ << ',' << event.op_name_ << ',' << event.op_full_name_ << ','
|
||||
<< event.op_info_->op_count << ',' << event.op_info_->op_cost_time << ',' << event.op_avg_time_ << ','
|
||||
<< event.proportion_ << ",Default," << event.op_info_->pid;
|
||||
return os;
|
||||
}
|
||||
};
|
||||
|
||||
struct OpType {
|
||||
std::string op_type_;
|
||||
int count_{0};
|
||||
int step_{0};
|
||||
float total_time_{0};
|
||||
float avg_time_{0};
|
||||
float proportion_{0};
|
||||
|
||||
std::string GetHeader() const {
|
||||
return "op_type,total_called_times,called_times(per-step),"
|
||||
"total_compute_time,compute_time(ms per-step),percent";
|
||||
}
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os, const OpType &event) {
|
||||
os << event.op_type_ << ',' << event.count_ << ',' << event.count_ / event.step_ << ',' << event.total_time_ << ','
|
||||
<< event.total_time_ / event.step_ << ',' << event.proportion_;
|
||||
return os;
|
||||
}
|
||||
|
||||
OpType &operator+=(const OpType &other) {
|
||||
this->count_ += other.count_;
|
||||
this->total_time_ += other.total_time_;
|
||||
this->proportion_ += other.proportion_;
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
using OpInfoMap = std::unordered_map<std::string, OpInfo>;
|
||||
using OpTypeInfos = std::unordered_map<std::string, OpType>; // <op_full_name, Optype>
|
||||
using OpDetailInfos = std::vector<OpDetailInfo>;
|
||||
// <op_full_name, StartDuration>
|
||||
using OpTimestampInfo = std::unordered_map<std::string, std::vector<StartDuration>>;
|
||||
|
||||
class DataSaver {
|
||||
public:
|
||||
DataSaver() = default;
|
||||
|
||||
~DataSaver() = default;
|
||||
|
||||
DataSaver(const DataSaver &) = delete;
|
||||
|
||||
DataSaver &operator=(const DataSaver &) = delete;
|
||||
|
||||
void ParseOpInfo(const OpInfoMap &op_info_maps);
|
||||
|
||||
void WriteFile(std::string out_path);
|
||||
|
||||
private:
|
||||
void AddOpDetailInfoForType(const OpDetailInfo &op_detail_info);
|
||||
|
||||
float GetTotalOpTime(const OpInfoMap &op_info_maps);
|
||||
|
||||
void WriteOpType(const std::string &saver_base_dir);
|
||||
|
||||
void WriteOpDetail(const std::string &saver_base_dir);
|
||||
|
||||
void WriteOpTimestamp(const std::string &saver_base_dir);
|
||||
|
||||
void ChangeFileMode(const std::string &file_path);
|
||||
|
||||
std::string device_id_;
|
||||
OpTypeInfos op_type_infos_;
|
||||
OpDetailInfos op_detail_infos_;
|
||||
OpTimestampInfo op_timestamps_map_;
|
||||
};
|
||||
} // namespace cpu
|
||||
} // namespace profiler
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // MINDSPORE_CPU_DATA_SAVER_H
|
@ -0,0 +1,136 @@
|
||||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "profiler/device/cpu/cpu_profiling.h"
|
||||
|
||||
#include <time.h>
|
||||
#include <cxxabi.h>
|
||||
#include <cmath>
|
||||
#include "profiler/device/cpu/cpu_data_saver.h"
|
||||
#include "pybind_api/api_register.h"
|
||||
#include "utils/log_adapter.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace profiler {
|
||||
namespace cpu {
|
||||
std::shared_ptr<CPUProfiler> CPUProfiler::profiler_inst_ = nullptr;
|
||||
|
||||
uint64_t GetMonoTimeStamp() {
|
||||
struct timespec ts;
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
#else
|
||||
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
|
||||
#endif
|
||||
constexpr uint64_t kNSecondInSecond = 1000000000;
|
||||
uint64_t cur_time_stamp = ts.tv_sec * kNSecondInSecond + ts.tv_nsec;
|
||||
return cur_time_stamp;
|
||||
}
|
||||
|
||||
std::shared_ptr<CPUProfiler> CPUProfiler::GetInstance() {
|
||||
if (profiler_inst_ == nullptr) {
|
||||
profiler_inst_ = std::shared_ptr<CPUProfiler>(new (std::nothrow) CPUProfiler());
|
||||
}
|
||||
return profiler_inst_;
|
||||
}
|
||||
|
||||
void CPUProfiler::Init(const std::string &profileDataPath = "") {
|
||||
MS_LOG(INFO) << "Initialize CPU Profiling";
|
||||
base_time_ = GetMonoTimeStamp();
|
||||
profile_data_path_ = profileDataPath;
|
||||
MS_LOG(INFO) << " Host start time(ns): " << base_time_ << " profile data path: " << profile_data_path_;
|
||||
}
|
||||
|
||||
void CPUProfiler::StepProfilingEnable(const bool enable_flag) {
|
||||
MS_LOG(INFO) << "CPU Profiler enable flag: " << enable_flag;
|
||||
enable_flag_ = enable_flag;
|
||||
}
|
||||
|
||||
void CPUProfiler::SetRunTimeData(const std::string &op_name, const uint32_t pid) {
|
||||
auto iter = op_info_map_.find(op_name);
|
||||
if (iter != op_info_map_.end()) {
|
||||
iter->second.op_count += 1;
|
||||
} else {
|
||||
OpInfo op_info;
|
||||
op_info.op_name = op_name;
|
||||
op_info.pid = pid;
|
||||
op_info.op_count = 1;
|
||||
op_info_map_[op_name] = op_info;
|
||||
}
|
||||
op_name_ = op_name;
|
||||
pid_ = pid;
|
||||
}
|
||||
|
||||
void CPUProfiler::SetRunTimeData(const std::string &op_name, const float time_elapsed) {
|
||||
auto iter = op_info_map_.find(op_name);
|
||||
if (iter != op_info_map_.end()) {
|
||||
// The time unit is ms, convert to us
|
||||
iter->second.op_cost_time += time_elapsed;
|
||||
}
|
||||
}
|
||||
|
||||
void CPUProfiler::SetRunTimeData(const std::string &op_name, const uint64_t start, const float duration) {
|
||||
auto iter = op_info_map_.find(op_name);
|
||||
if (iter != op_info_map_.end()) {
|
||||
iter->second.start_duration.emplace_back(StartDuration({start, duration}));
|
||||
}
|
||||
}
|
||||
|
||||
void CPUProfiler::OpDataProducerBegin(const std::string op_name, const uint32_t pid) {
|
||||
op_time_start_ = GetMonoTimeStamp();
|
||||
op_time_mono_start_ = GetMonoTimeStamp();
|
||||
SetRunTimeData(op_name, pid);
|
||||
}
|
||||
|
||||
void CPUProfiler::OpDataProducerEnd() {
|
||||
float op_time_elapsed = 0;
|
||||
op_time_stop_ = GetMonoTimeStamp();
|
||||
op_time_elapsed = (op_time_stop_ - op_time_start_) / kTimeUnit;
|
||||
MS_LOG(DEBUG) << "Host Time Elapsed(us)," << op_name_ << "," << op_time_elapsed;
|
||||
SetRunTimeData(op_name_, op_time_elapsed);
|
||||
SetRunTimeData(op_name_, op_time_mono_start_, op_time_elapsed);
|
||||
}
|
||||
|
||||
void CPUProfiler::Stop() {
|
||||
MS_LOG(INFO) << "Stop CPU Profiling";
|
||||
SaveProfileData();
|
||||
ClearInst();
|
||||
}
|
||||
|
||||
void CPUProfiler::SaveProfileData() {
|
||||
if (profile_data_path_.empty()) {
|
||||
MS_LOG(WARNING) << "Profile data path is empty, skip save profile data.";
|
||||
} else {
|
||||
DataSaver dataSaver;
|
||||
dataSaver.ParseOpInfo(op_info_map_);
|
||||
dataSaver.WriteFile(profile_data_path_);
|
||||
}
|
||||
}
|
||||
|
||||
void CPUProfiler::ClearInst() { op_info_map_.clear(); }
|
||||
|
||||
REGISTER_PYBIND_DEFINE(CPUProfiler_, ([](const py::module *m) {
|
||||
(void)py::class_<CPUProfiler, std::shared_ptr<CPUProfiler>>(*m, "CPUProfiler")
|
||||
.def_static("get_instance", &CPUProfiler::GetInstance, "CPUProfiler get_instance.")
|
||||
.def("init", &CPUProfiler::Init, py::arg("profile_data_path"), "init")
|
||||
.def("stop", &CPUProfiler::Stop, "stop")
|
||||
.def("step_profiling_enable", &CPUProfiler::StepProfilingEnable, py::arg("enable_flag"),
|
||||
"enable or disable step profiling");
|
||||
}));
|
||||
} // namespace cpu
|
||||
} // namespace profiler
|
||||
} // namespace mindspore
|
@ -0,0 +1,86 @@
|
||||
/**
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_CPU_PROFILING_H
|
||||
#define MINDSPORE_CPU_PROFILING_H
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace mindspore {
|
||||
namespace profiler {
|
||||
namespace cpu {
|
||||
struct StartDuration {
|
||||
uint64_t start_timestamp = 0l;
|
||||
float duration = 0l;
|
||||
};
|
||||
|
||||
struct OpInfo {
|
||||
std::string op_name;
|
||||
float op_cost_time = 0;
|
||||
int op_count = 0;
|
||||
std::vector<StartDuration> start_duration;
|
||||
uint32_t pid;
|
||||
};
|
||||
|
||||
const float kTimeUnit = 1000;
|
||||
|
||||
class CPUProfiler {
|
||||
public:
|
||||
static std::shared_ptr<CPUProfiler> GetInstance();
|
||||
~CPUProfiler() = default;
|
||||
CPUProfiler(const CPUProfiler &) = delete;
|
||||
CPUProfiler &operator=(const CPUProfiler &) = delete;
|
||||
|
||||
void Init(const std::string &profileDataPath);
|
||||
void Stop();
|
||||
void StepProfilingEnable(const bool enable_flag);
|
||||
bool GetEnableFlag() const { return enable_flag_; }
|
||||
void OpDataProducerBegin(const std::string op_name, const uint32_t pid);
|
||||
void OpDataProducerEnd();
|
||||
std::string ProfileDataPath() const { return profile_data_path_; }
|
||||
|
||||
private:
|
||||
CPUProfiler() = default;
|
||||
void ClearInst();
|
||||
void SetRunTimeData(const std::string &op_name, const uint32_t pid);
|
||||
void SetRunTimeData(const std::string &op_name, const float time_elapsed);
|
||||
void SetRunTimeData(const std::string &op_name, const uint64_t start, const float duration);
|
||||
|
||||
static std::shared_ptr<CPUProfiler> profiler_inst_;
|
||||
bool enable_flag_ = false;
|
||||
std::unordered_map<std::string, OpInfo> op_info_map_;
|
||||
uint64_t base_time_;
|
||||
std::string op_name_;
|
||||
uint32_t pid_;
|
||||
void SaveProfileData();
|
||||
|
||||
uint64_t op_time_start_;
|
||||
uint64_t op_time_mono_start_;
|
||||
uint64_t op_time_stop_;
|
||||
std::string profile_data_path_;
|
||||
};
|
||||
} // namespace cpu
|
||||
} // namespace profiler
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // MINDSPORE_CPU_PROFILING_H
|
Loading…
Reference in new issue