File: signal_utils.cpp

package info (click to toggle)
intel-compute-runtime-legacy 24.35.30872.40-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 73,292 kB
  • sloc: cpp: 826,355; lisp: 3,686; sh: 677; makefile: 148; python: 21
file content (131 lines) | stat: -rw-r--r-- 4,166 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * Copyright (C) 2022-2024 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "shared/test/common/libult/signal_utils.h"

#include "shared/source/os_interface/windows/windows_wrapper.h"

#include "gtest/gtest.h"

#include <condition_variable>
#include <io.h>
#include <signal.h>
#include <thread>

std::string lastTest("");
static int newStdOut = -1;

namespace NEO {
extern const char *executionName;
extern unsigned int ultIterationMaxTimeInS;
} // namespace NEO

std::unique_ptr<std::thread> alarmThread;

LONG WINAPI ultExceptionFilter(
    _In_ struct _EXCEPTION_POINTERS *exceptionInfo) {
    std::cout << "UnhandledException: 0x" << std::hex << exceptionInfo->ExceptionRecord->ExceptionCode << std::dec
              << " on test: " << lastTest << std::endl;
    return EXCEPTION_CONTINUE_SEARCH;
}

void (*oldSigAbrt)(int) = nullptr;
void handleSIGABRT(int sigNo) {
    if (newStdOut != -1) {
        _dup2(newStdOut, 1);
    }
    std::cout << "SIGABRT on: " << lastTest << std::endl;
    signal(SIGABRT, oldSigAbrt);
    raise(sigNo);
}

int setAbrt(bool enableAbrt) {
    std::cout << "enable SIGABRT handler: " << enableAbrt << std::endl;

    if (newStdOut == -1) {
        newStdOut = _dup(1);
    }

    SetUnhandledExceptionFilter(&ultExceptionFilter);
    if (enableAbrt) {
        oldSigAbrt = signal(SIGABRT, handleSIGABRT);
    }
    return 0;
}

std::atomic<bool> abortOnTimeout = false;

int setAlarm(bool enableAlarm) {
    std::cout << "enable SIGALRM handler: " << enableAlarm << std::endl;

    if (enableAlarm) {
        std::string envVar = std::string("NEO_") + NEO::executionName + "_DISABLE_TEST_ALARM";
        char *envValue = getenv(envVar.c_str());
        if (envValue != nullptr) {
            enableAlarm = false;
            std::cout << "WARNING: SIGALRM handler disabled by environment variable: " << envVar << std::endl;
        }
    }

    if (enableAlarm) {
        abortOnTimeout = true;
        std::atomic<bool> threadStarted{false};
        alarmThread = std::make_unique<std::thread>([&]() {
            auto currentUltIterationMaxTimeInS = NEO::ultIterationMaxTimeInS;

            std::string envVar = std::string("NEO_") + NEO::executionName + "_ITERATION_MAX_TIME";
            auto ultIterationMaxTimeInSEnv = getenv(envVar.c_str());
            if (ultIterationMaxTimeInSEnv != nullptr) {
                currentUltIterationMaxTimeInS = atoi(ultIterationMaxTimeInSEnv);
            } else {
                ultIterationMaxTimeInSEnv = getenv("NEO_ULT_ITERATION_MAX_TIME");
                if (ultIterationMaxTimeInSEnv != nullptr) {
                    currentUltIterationMaxTimeInS = atoi(ultIterationMaxTimeInSEnv);
                }
            }
            unsigned int alarmTimeInS = currentUltIterationMaxTimeInS * ::testing::GTEST_FLAG(repeat);
            std::cout << "set timeout to: " << alarmTimeInS << " seconds" << std::endl;
            threadStarted = true;
            std::chrono::high_resolution_clock::time_point startTime, endTime;
            std::chrono::milliseconds elapsedTime{};
            startTime = std::chrono::high_resolution_clock::now();
            do {
                std::this_thread::yield();
                std::this_thread::sleep_for(std::chrono::milliseconds(1));
                endTime = std::chrono::high_resolution_clock::now();
                elapsedTime = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime);
                if (!abortOnTimeout) {
                    return;
                }
            } while (abortOnTimeout && elapsedTime.count() < alarmTimeInS * 1000);

            if (abortOnTimeout) {
                printf("timeout on: %s\n", lastTest.c_str());
                abort();
            }
        });
        SetThreadPriority(alarmThread->native_handle(), THREAD_PRIORITY_LOWEST);

        while (!threadStarted.load()) {
            std::this_thread::yield();
        }
    }

    return 0;
}

int setSegv(bool enableSegv) {
    return 0;
}

void cleanupSignals() {
    if (alarmThread) {
        abortOnTimeout = false;
        alarmThread->join();
        alarmThread.reset();
    }
}