11#include < chrono>
2+ #include < ctime>
3+ #include < fstream>
4+ #include < iomanip>
25#include < iostream>
36#include < numeric>
7+ #include < sstream>
48#include < vector>
59
610#include " tensorflow/cc/ops/array_ops.h"
1418#include " tensorflow/core/platform/init_main.h"
1519#include " tensorflow/core/platform/logging.h"
1620#include " tensorflow/core/platform/types.h"
21+ #include " tensorflow/core/platform/env.h"
22+ #include " tensorflow/core/platform/path.h"
23+ #include " tensorflow/core/platform/host_info.h"
1724#include " tensorflow/core/profiler/lib/profiler_session.h"
1825#include " tensorflow/core/profiler/lib/traceme.h"
19- #include " tensorflow/core/profiler/rpc/client/capture_profile.h"
2026#include " tensorflow/core/public/session.h"
2127#include " tensorflow/core/util/command_line_flags.h"
2228
@@ -35,6 +41,10 @@ using tensorflow::Tensor;
3541 } \
3642 } while (0 )
3743
44+ // Directory required by Tensorboard to load the .xplane.pb properly
45+ const string kProfilePath = " plugins/profile" ;
46+ const string kProfileSuffix = " .xplane.pb" ;
47+
3848// Get the GPU and its default context.
3949Status GetDevice (std::unique_ptr<tensorflow::Session>& session,
4050 tensorflow::Device** device,
@@ -144,6 +154,26 @@ Status SetupCallable(std::unique_ptr<tensorflow::Session>& session,
144154 return session->MakeCallable (opts, handle);
145155}
146156
157+ // Get the current time as a string.
158+ string GetTime () {
159+ std::stringstream ss;
160+ std::time_t now = std::time (nullptr );
161+ ss << std::put_time (std::localtime (&now), " %EY_%m_%d_%H_%M_%S" );
162+ return ss.str ();
163+ }
164+
165+ // Create the directory structure expected by TensorBoard.
166+ Status GetAndCreateProfilePath (const string& out_dir,
167+ string* profile_path) {
168+ // Use date for run ID
169+ string run_id = GetTime ();
170+ string host_id = tensorflow::port::Hostname ();
171+ string run_dir = tensorflow::io::JoinPath (out_dir, kProfilePath , run_id);
172+ *profile_path = tensorflow::io::JoinPath (run_dir, host_id + kProfileSuffix );
173+ TF_RETURN_IF_ERROR (tensorflow::Env::Default ()->RecursivelyCreateDir (run_dir));
174+ return Status::OK ();
175+ }
176+
147177// Start the profiling session.
148178Status StartProfiling (std::unique_ptr<tensorflow::ProfilerSession>& profiler) {
149179 profiler = tensorflow::ProfilerSession::Create (
@@ -157,7 +187,18 @@ Status StopProfiling(std::unique_ptr<tensorflow::ProfilerSession>& profiler,
157187 const string& out_dir) {
158188 tensorflow::profiler::XSpace xspace;
159189 TF_RETURN_IF_ERROR (profiler->CollectData (&xspace));
160- tensorflow::profiler::ExportToTensorBoard (xspace, out_dir);
190+
191+ // Get export path
192+ string profile_path;
193+ TF_RETURN_IF_ERROR (GetAndCreateProfilePath (out_dir, &profile_path));
194+
195+ // Serialize and write to disk
196+ string xspace_serialized;
197+ xspace.SerializeToString (&xspace_serialized);
198+ std::ofstream ofs (profile_path);
199+ ofs << xspace_serialized;
200+ ofs.close ();
201+
161202 profiler.reset ();
162203 return Status::OK ();
163204}
0 commit comments