From 2674d9bd8a1d578bbf1da91766e4aa31b4241063 Mon Sep 17 00:00:00 2001 From: Mika Fischer Date: Fri, 5 Apr 2019 01:23:18 +0200 Subject: [PATCH] Fix profiling with C API (#710) Currently, when using OrtEnableProfiling to enable profiling using the C API, the profile output file is created but is always empty. The reason is that InferenceSession::EndProfiling() needs to be called to write the profiling data to the output file. However there's currently no way to call this function via the C API. This adds a call to EndProfiling() to the descructor of the session if profiling is enabled in the session options. --- onnxruntime/core/session/inference_session.cc | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/onnxruntime/core/session/inference_session.cc b/onnxruntime/core/session/inference_session.cc index 072e4a3965..db33083558 100644 --- a/onnxruntime/core/session/inference_session.cc +++ b/onnxruntime/core/session/inference_session.cc @@ -191,7 +191,22 @@ InferenceSession::InferenceSession(const SessionOptions& session_options, loggin } } -InferenceSession::~InferenceSession() = default; +InferenceSession::~InferenceSession() { + if (session_options_.enable_profiling) { + try { + EndProfiling(); + } catch (std::exception& e) { + // TODO: Currently we have no way to transport this error to the API user + // Maybe this should be refactored, so that profiling must be explicitly + // started and stopped via C-API functions. + // And not like now a session option and therefore profiling must be started + // and stopped implicitly. + LOGS(*session_logger_, ERROR) << "Error during EndProfiling(): " << e.what(); + } catch (...) { + LOGS(*session_logger_, ERROR) << "Unknown error during EndProfiling()"; + } + } +} common::Status InferenceSession::RegisterExecutionProvider(std::unique_ptr p_exec_provider) { if (p_exec_provider == nullptr) {