|
| 1 | +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. |
| 2 | +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 3 | +// All rights not expressly granted are reserved. |
| 4 | +// |
| 5 | +// This software is distributed under the terms of the GNU General Public |
| 6 | +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 7 | +// |
| 8 | +// In applying this license CERN does not waive the privileges and immunities |
| 9 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 10 | +// or submit itself to any jurisdiction. |
| 11 | + |
| 12 | +/// \file GPUReconstructionDebug.cxx |
| 13 | +/// \author David Rohr |
| 14 | + |
| 15 | +#include "GPUReconstruction.h" |
| 16 | +#include "GPULogging.h" |
| 17 | +#include "GPUSettings.h" |
| 18 | + |
| 19 | +#include <signal.h> |
| 20 | +#include <functional> |
| 21 | +#include <unordered_map> |
| 22 | +#include <mutex> |
| 23 | + |
| 24 | +using namespace o2::gpu; |
| 25 | + |
| 26 | +struct GPUReconstruction::debugInternal { |
| 27 | + std::function<void(int32_t, siginfo_t*, void*)> signalCallback; |
| 28 | + std::function<void()> debugCallback = nullptr; |
| 29 | + std::unordered_map<int32_t, struct sigaction> oldActions; |
| 30 | + static void globalCallback(int32_t signal, siginfo_t* info, void* ucontext) |
| 31 | + { |
| 32 | + GPUReconstruction::mDebugData->signalCallback(signal, info, ucontext); |
| 33 | + } |
| 34 | +}; |
| 35 | + |
| 36 | +std::unique_ptr<GPUReconstruction::debugInternal> GPUReconstruction::mDebugData; |
| 37 | + |
| 38 | +void GPUReconstruction::debugInit() |
| 39 | +{ |
| 40 | + if (GetProcessingSettings().dumpOnFailure) { |
| 41 | + static std::mutex initMutex; |
| 42 | + { |
| 43 | + std::lock_guard<std::mutex> guard(initMutex); |
| 44 | + if (mDebugData) { |
| 45 | + GPUFatal("Error handlers for debug dumps already set, cannot set them again"); |
| 46 | + } |
| 47 | + mDebugData = std::make_unique<debugInternal>(); |
| 48 | + } |
| 49 | + mDebugEnabled = true; |
| 50 | + struct sigaction sa, oldsa; |
| 51 | + memset(&sa, 0, sizeof(sa)); |
| 52 | + sa.sa_sigaction = GPUReconstruction::debugInternal::globalCallback; |
| 53 | + sa.sa_flags = SA_SIGINFO; |
| 54 | + int32_t sigs[] = {SIGINT, SIGABRT, SIGBUS, SIGTERM, SIGSEGV}; |
| 55 | + for (uint32_t i = 0; i < sizeof(sigs) / sizeof(sigs[0]); i++) { |
| 56 | + if (sigaction(sigs[i], &sa, &oldsa)) { |
| 57 | + GPUFatal("Error installing signal handler for error dump on signal %d", sigs[i]); |
| 58 | + } |
| 59 | + mDebugData->oldActions.emplace(sigs[i], oldsa); |
| 60 | + } |
| 61 | + |
| 62 | + mDebugData->signalCallback = [&oldActions = mDebugData->oldActions, myAction = std::move(sa)](int32_t signal, siginfo_t* info, void* ucontext) { |
| 63 | + static std::mutex callbackMutex; |
| 64 | + { |
| 65 | + std::lock_guard<std::mutex> guard(callbackMutex); |
| 66 | + GPUInfo("Received signal %d", signal); |
| 67 | + if (mDebugData->debugCallback) { |
| 68 | + GPUInfo("Running debug callback"); |
| 69 | + mDebugData->debugCallback(); |
| 70 | + } |
| 71 | + mDebugData->debugCallback = nullptr; |
| 72 | + sigaction(signal, &oldActions[signal], nullptr); |
| 73 | + printf("DEF SIGNAL\n"); |
| 74 | + raise(signal); |
| 75 | + } |
| 76 | + sigaction(signal, &myAction, nullptr); |
| 77 | + }; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +void GPUReconstruction::debugExit() |
| 82 | +{ |
| 83 | + if (!mDebugEnabled) { |
| 84 | + return; |
| 85 | + } |
| 86 | + if (mDebugData) { |
| 87 | + for (auto it = mDebugData->oldActions.begin(); it != mDebugData->oldActions.end(); it++) { |
| 88 | + if (sigaction(it->first, &it->second, nullptr)) { |
| 89 | + GPUFatal("Error restoring signal handler for signal %d", it->first); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + mDebugEnabled = false; |
| 94 | +} |
| 95 | + |
| 96 | +void GPUReconstruction::setDebugDumpCallback(std::function<void()>&& callback) |
| 97 | +{ |
| 98 | + if (mMaster) { |
| 99 | + mMaster->setDebugDumpCallback(std::move(callback)); |
| 100 | + } else if (mDebugEnabled && mDebugData) { |
| 101 | + mDebugData->debugCallback = callback; |
| 102 | + } |
| 103 | +} |
0 commit comments