提交 ab6bc60d 编写于 作者: M mindspore-ci-bot 提交者: Gitee

!1314 Using context variable instead of environment variable in profiling

Merge pull request !1314 from caifubi/using-context-instead-of-env
...@@ -114,9 +114,6 @@ bool ProfilingManager::StartupProfiling(uint32_t device_id) { ...@@ -114,9 +114,6 @@ bool ProfilingManager::StartupProfiling(uint32_t device_id) {
return true; return true;
} }
device_id_ = device_id; device_id_ = device_id;
// exp: export PROFILING_MODE=true
// export PROFILING_OPTIONS=training_trace
const char *prof_options_str = std::getenv("PROFILING_OPTIONS");
// register Framework to profiling // register Framework to profiling
int result = Msprof::Engine::RegisterEngine("Framework", engine_0_.get()); int result = Msprof::Engine::RegisterEngine("Framework", engine_0_.get());
if (result != 0) { if (result != 0) {
...@@ -124,53 +121,51 @@ bool ProfilingManager::StartupProfiling(uint32_t device_id) { ...@@ -124,53 +121,51 @@ bool ProfilingManager::StartupProfiling(uint32_t device_id) {
return false; return false;
} }
if (prof_options_str != nullptr) { auto context = MsContext::GetInstance();
const string prof_options_str_tmp = prof_options_str; MS_EXCEPTION_IF_NULL(context);
vector<string> opts = Split(prof_options_str_tmp, ':'); const string prof_options_str = context->profiling_options();
if (!opts.empty()) { vector<string> opts = Split(prof_options_str, ':');
// current one docker only use one device` if (opts.empty()) {
Json p_device; MS_LOG(WARNING) << "Profiling is enabled, but profiling option is not set!";
return true;
// JOBID
auto job_id = GetJobId();
p_device["jobID"] = std::to_string(job_id);
// device_id
p_device["deviceID"] = std::to_string(device_id);
// features:'training_trace', 'task_trace' etc
Json features;
for (vector<string>::size_type i = 0; i < opts.size(); i++) {
Json f;
f["name"] = opts[i];
features[i] = f;
}
p_device["features"] = features;
// only one device, but sProfMgrStartUp API require for device list
Json devices;
devices[0] = p_device;
Json startCfg;
startCfg["startCfg"] = devices;
// convert json to string
std::stringstream ss;
ss << startCfg;
std::string cfg = ss.str();
MS_LOG(INFO) << "profiling config " << cfg;
// call profiling startup API
ProfMgrCfg prof_cfg = {cfg};
prof_handle_ = ProfMgrStartUp(&prof_cfg);
if (prof_handle_ == nullptr) {
MS_LOG(ERROR) << "Startup profiling failed.";
return false;
}
}
} }
// current one docker only use one device`
Json p_device;
// JOBID
auto job_id = GetJobId();
p_device["jobID"] = std::to_string(job_id);
// device_id
p_device["deviceID"] = std::to_string(device_id);
// features:'training_trace', 'task_trace' etc
Json features;
for (vector<string>::size_type i = 0; i < opts.size(); i++) {
Json f;
f["name"] = opts[i];
features[i] = f;
}
p_device["features"] = features;
// only one device, but sProfMgrStartUp API require for device list
Json devices;
devices[0] = p_device;
Json startCfg;
startCfg["startCfg"] = devices;
// convert json to string
std::stringstream ss;
ss << startCfg;
std::string cfg = ss.str();
MS_LOG(INFO) << "profiling config " << cfg;
// call profiling startup API
ProfMgrCfg prof_cfg = {cfg};
prof_handle_ = ProfMgrStartUp(&prof_cfg);
if (prof_handle_ == nullptr) {
MS_LOG(ERROR) << "Startup profiling failed.";
return false;
}
return true; return true;
} }
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include <cstring> #include <cstring>
#include <string> #include <string>
#include <memory> #include <memory>
#include "utils/context/ms_context.h"
using std::map; using std::map;
using std::string; using std::string;
...@@ -41,8 +42,9 @@ class ProfilingManager { ...@@ -41,8 +42,9 @@ class ProfilingManager {
bool StopProfiling() const; bool StopProfiling() const;
inline bool IsProfiling() const { inline bool IsProfiling() const {
const char *is_profiling = std::getenv("PROFILING_MODE"); auto context = MsContext::GetInstance();
return (is_profiling != nullptr && (strcmp("true", is_profiling) == 0)); MS_EXCEPTION_IF_NULL(context);
return context->enable_profiling();
} }
protected: protected:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册