diff --git a/mindspore/ccsrc/runtime/device/ascend/ascend_kernel_runtime.cc b/mindspore/ccsrc/runtime/device/ascend/ascend_kernel_runtime.cc index c7f79f849e..89838d8f3f 100644 --- a/mindspore/ccsrc/runtime/device/ascend/ascend_kernel_runtime.cc +++ b/mindspore/ccsrc/runtime/device/ascend/ascend_kernel_runtime.cc @@ -22,6 +22,7 @@ #include #include #include +#include "runtime/device/ascend/signal_util.h" #include "debug/data_dump/e2e_dump_util.h" #include "runtime/device/ascend/ascend_device_address.h" #include "runtime/device/cpu/mpi/mpi_interface.h" @@ -595,6 +596,7 @@ void AscendKernelRuntime::DumpTaskExceptionInfo(const session::KernelGraph *grap } bool AscendKernelRuntime::Run(session::KernelGraph *graph, bool is_task_sink) { + SignalGuard sg; MS_EXCEPTION_IF_NULL(graph); bool ret = false; #if defined(_WIN32) || defined(_WIN64) diff --git a/mindspore/ccsrc/runtime/device/ascend/signal_util.cc b/mindspore/ccsrc/runtime/device/ascend/signal_util.cc new file mode 100644 index 0000000000..3619b3858b --- /dev/null +++ b/mindspore/ccsrc/runtime/device/ascend/signal_util.cc @@ -0,0 +1,48 @@ +/** + * Copyright 2020 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 "runtime/device/ascend/signal_util.h" +#include +#include +#include "utils/log_adapter.h" + +namespace mindspore { +SignalGuard::SignalGuard() { RegisterHandlers(); } + +SignalGuard::~SignalGuard() { + if (old_handler != nullptr) { + int_action.sa_sigaction = old_handler; + (void)sigemptyset(&int_action.sa_mask); + int_action.sa_flags = SA_RESTART | SA_SIGINFO; + (void)sigaction(SIGINT, &int_action, nullptr); + } +} + +void SignalGuard::RegisterHandlers() { + struct sigaction old_int_action; + (void)sigaction(SIGINT, nullptr, &old_int_action); + if (old_int_action.sa_sigaction != nullptr) { + MS_LOG(INFO) << "The signal has been registered"; + old_handler = old_int_action.sa_sigaction; + } + int_action.sa_sigaction = &IntHandler; + (void)sigemptyset(&int_action.sa_mask); + int_action.sa_flags = SA_RESTART | SA_SIGINFO; + (void)sigaction(SIGINT, &int_action, nullptr); +} + +void SignalGuard::IntHandler(int, siginfo_t *, void *) { MS_LOG_EXCEPTION << "Exit"; } +} // namespace mindspore diff --git a/mindspore/ccsrc/runtime/device/ascend/signal_util.h b/mindspore/ccsrc/runtime/device/ascend/signal_util.h new file mode 100644 index 0000000000..b1bbe66b3f --- /dev/null +++ b/mindspore/ccsrc/runtime/device/ascend/signal_util.h @@ -0,0 +1,35 @@ +/** + * Copyright 2020 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_CCSRC_RUNTIME_DEVICE_ASCEND_SIGNAL_UTIL_H_ +#define MINDSPORE_CCSRC_RUNTIME_DEVICE_ASCEND_SIGNAL_UTIL_H_ + +#include + +namespace mindspore { +class SignalGuard { + public: + SignalGuard(); + ~SignalGuard(); + + private: + void RegisterHandlers(); + static void IntHandler(int sig_num, siginfo_t *sig_info, void *context); + + void (*old_handler)(int, siginfo_t *, void *) = nullptr; + struct sigaction int_action; +}; +} // namespace mindspore +#endif // MINDSPORE_CCSRC_RUNTIME_DEVICE_ASCEND_SIGNAL_UTIL_H_ diff --git a/tests/ut/cpp/CMakeLists.txt b/tests/ut/cpp/CMakeLists.txt index a020330c16..506b11c67b 100644 --- a/tests/ut/cpp/CMakeLists.txt +++ b/tests/ut/cpp/CMakeLists.txt @@ -95,6 +95,7 @@ file(GLOB_RECURSE MINDSPORE_SRC_LIST RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "../../../mindspore/ccsrc/runtime/device/convert_tensor_utils.cc" "../../../mindspore/ccsrc/runtime/device/ascend/kernel_build_ascend.cc" "../../../mindspore/ccsrc/runtime/device/ascend/ascend_kernel_runtime.cc" + "../../../mindspore/ccsrc/runtime/device/ascend/signal_util.cc" "../../../mindspore/ccsrc/runtime/device/ascend/ascend_memory_manager.cc" "../../../mindspore/ccsrc/runtime/device/ascend/ascend_device_address.cc" "../../../mindspore/ccsrc/runtime/device/ascend/ascend_memory_pool.cc"