@@ -1434,7 +1434,7 @@ struct PyProgram final {
14341434
14351435 std::unique_ptr<PyMethod> load_method (const std::string& method_name) {
14361436 Result<Method> res = state_->program_ ->load_method (
1437- method_name.c_str (), memory_->mem_manager ());
1437+ method_name.c_str (), memory_->mem_manager (), event_tracer_. get () );
14381438 THROW_IF_ERROR (
14391439 res.error (),
14401440 " Failed to load method %s, error: 0x:%" PRIx32,
@@ -1459,6 +1459,39 @@ struct PyProgram final {
14591459 return std::make_unique<PyMethodMeta>(state_, std::move (res.get ()));
14601460 }
14611461
1462+ bool has_etdump () {
1463+ return static_cast <bool >(event_tracer_);
1464+ }
1465+
1466+ void write_etdump_result_to_file (
1467+ const std::string& path,
1468+ const py::object& debug_buffer_path) {
1469+ if (!has_etdump ()) {
1470+ throw std::runtime_error (" No etdump found" );
1471+ }
1472+ auto & etdump = *event_tracer_;
1473+ etdump_result result = etdump.get_etdump_data ();
1474+ if (result.buf != nullptr && result.size > 0 ) {
1475+ write_data_to_file (path, result.buf , result.size );
1476+ free (result.buf );
1477+ if (debug_buffer_size_ > 0 &&
1478+ py::isinstance<py::str>(debug_buffer_path)) {
1479+ // Also write out the debug buffer to a separate file if requested.
1480+ std::string debug_buffer_path_str =
1481+ py::cast<std::string>(debug_buffer_path);
1482+ const auto debug_buffer = get_etdump_debug_buffer ();
1483+ write_data_to_file (
1484+ debug_buffer_path_str, debug_buffer.data (), debug_buffer.size ());
1485+ }
1486+ } else {
1487+ ET_LOG (
1488+ Info,
1489+ " No etdump data found, try rebuilding with "
1490+ " the CMake option EXECUTORCH_ENABLE_EVENT_TRACER or with "
1491+ " buck run --config executorch.event_tracer_enabled=true" );
1492+ }
1493+ }
1494+
14621495 private:
14631496 std::shared_ptr<ProgramMemory> memory_;
14641497 std::shared_ptr<ProgramState> state_;
@@ -1685,6 +1718,13 @@ PYBIND11_MODULE(EXECUTORCH_PYTHON_MODULE_NAME, m) {
16851718 " method_meta" ,
16861719 &PyProgram::method_meta,
16871720 py::arg (" method_name" ),
1721+ call_guard)
1722+ .def (" has_etdump" , &PyProgram::has_etdump, call_guard)
1723+ .def (
1724+ " write_etdump_result_to_file" ,
1725+ &PyProgram::write_etdump_result_to_file,
1726+ py::arg (" path" ),
1727+ py::arg (" debug_buffer_path" ) = py::none (),
16881728 call_guard);
16891729 py::class_<PyMethod>(m, " ExecuTorchMethod" )
16901730 .def (" set_inputs" , &PyMethod::set_inputs, py::arg (" inputs" ), call_guard)
0 commit comments