|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +#include "compiler/compilerThread.hpp" |
| 26 | +#include "compiler/compileTask.hpp" |
| 27 | +#include "compilerThreadTimeout_linux.hpp" |
| 28 | +#include "oops/method.hpp" |
| 29 | +#include "runtime/osThread.hpp" |
| 30 | +#include "signals_posix.hpp" |
| 31 | +#include "utilities/globalDefinitions.hpp" |
| 32 | + |
| 33 | +#include <pthread.h> |
| 34 | + |
| 35 | +#ifdef ASSERT |
| 36 | +void compiler_signal_handler(int signo, siginfo_t* info, void* context) { |
| 37 | + CompilerThread::current()->timeout()->compiler_signal_handler(signo, info, context); |
| 38 | +} |
| 39 | + |
| 40 | +void CompilerThreadTimeoutLinux::compiler_signal_handler(int signo, siginfo_t* info, void* context) { |
| 41 | + switch (signo) { |
| 42 | + case TIMEOUT_SIGNAL: { |
| 43 | + CompileTask* task = CompilerThread::current()->task(); |
| 44 | + const int SIZE = 512; |
| 45 | + char method_name_buf[SIZE]; |
| 46 | + task->method()->name_and_sig_as_C_string(method_name_buf, SIZE); |
| 47 | + assert(false, "compile task %d (%s) timed out after %zd ms", |
| 48 | + task->compile_id(), method_name_buf, CompileTaskTimeout); |
| 49 | + } |
| 50 | + default: { |
| 51 | + assert(false, "unexpected signal %d", signo); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | +#endif // ASSERT |
| 56 | + |
| 57 | +void CompilerThreadTimeoutLinux::arm() { |
| 58 | +#ifdef ASSERT |
| 59 | + if (CompileTaskTimeout == 0) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + const intx sec = (CompileTaskTimeout * NANOSECS_PER_MILLISEC) / NANOSECS_PER_SEC; |
| 64 | + const intx nsec = (CompileTaskTimeout * NANOSECS_PER_MILLISEC) % NANOSECS_PER_SEC; |
| 65 | + const struct timespec ts = {.tv_sec = sec, .tv_nsec = nsec}; |
| 66 | + const struct itimerspec its {.it_interval = ts, .it_value = ts}; |
| 67 | + |
| 68 | + // Start the timer. |
| 69 | + timer_settime(_timer, 0, &its, nullptr); |
| 70 | +#endif // ASSERT |
| 71 | +} |
| 72 | + |
| 73 | +void CompilerThreadTimeoutLinux::disarm() { |
| 74 | +#ifdef ASSERT |
| 75 | + if (CompileTaskTimeout == 0) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + // Reset the timer by setting it to zero. |
| 80 | + const struct itimerspec its { |
| 81 | + .it_interval = {.tv_sec = 0, .tv_nsec=0}, |
| 82 | + .it_value = {.tv_sec = 0, .tv_nsec=0} |
| 83 | + }; |
| 84 | + timer_settime(_timer, 0, &its, nullptr); |
| 85 | +#endif // ASSERT |
| 86 | +} |
| 87 | + |
| 88 | +bool CompilerThreadTimeoutLinux::init_timeout() { |
| 89 | +#ifdef ASSERT |
| 90 | + if (CompileTaskTimeout == 0) { |
| 91 | + return true; |
| 92 | + } |
| 93 | + |
| 94 | + JavaThread* thread = JavaThread::current(); |
| 95 | + |
| 96 | + // Create a POSIX timer sending SIGALRM to this thread only. |
| 97 | + sigevent_t sev; |
| 98 | + sev.sigev_value.sival_ptr = nullptr; |
| 99 | + sev.sigev_signo = TIMEOUT_SIGNAL; |
| 100 | + sev.sigev_notify = SIGEV_THREAD_ID; |
| 101 | + #ifdef MUSL_LIBC |
| 102 | + sev.sigev_notify_thread_id = thread->osthread()->thread_id(); |
| 103 | + #else |
| 104 | + sev._sigev_un._tid = thread->osthread()->thread_id(); |
| 105 | + #endif // MUSL_LIBC |
| 106 | + clockid_t clock; |
| 107 | + int err = pthread_getcpuclockid(thread->osthread()->pthread_id(), &clock); |
| 108 | + if (err != 0) { |
| 109 | + return false; |
| 110 | + } |
| 111 | + err = timer_create(clock, &sev, &_timer); |
| 112 | + if (err != 0) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + // Install the signal handler and check that we do not have a conflicting handler. |
| 117 | + struct sigaction sigact, sigact_old; |
| 118 | + err = PosixSignals::install_sigaction_signal_handler(&sigact, |
| 119 | + &sigact_old, |
| 120 | + TIMEOUT_SIGNAL, |
| 121 | + (sa_sigaction_t)::compiler_signal_handler); |
| 122 | + if (err != 0 || (sigact_old.sa_sigaction != sigact.sa_sigaction && |
| 123 | + sigact_old.sa_handler != SIG_DFL && sigact_old.sa_handler != SIG_IGN)) { |
| 124 | + return false; |
| 125 | + } |
| 126 | +#endif // ASSERT |
| 127 | + return true; |
| 128 | +} |
0 commit comments