2024-03-15 12:29:39 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* Copyright the Collabora Online contributors.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <thread>
|
|
|
|
#include <chrono>
|
|
|
|
#include <mutex>
|
2024-03-28 13:11:48 -05:00
|
|
|
#include <csignal>
|
2024-03-15 12:29:39 -05:00
|
|
|
#include <condition_variable>
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
2024-03-28 13:11:48 -05:00
|
|
|
void handleUserProfileSignal(const int /* signal */);
|
2024-03-15 12:29:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A class to watch to see when threads are not making progress.
|
|
|
|
*/
|
|
|
|
class Watchdog : private std::thread
|
|
|
|
{
|
|
|
|
std::atomic<bool> _exit;
|
|
|
|
std::unique_ptr<std::thread> _thread;
|
|
|
|
std::mutex _lock;
|
|
|
|
std::condition_variable _condition;
|
2024-03-28 13:11:48 -05:00
|
|
|
typedef std::pair<std::atomic<uint64_t>*,int *> WatchDetail;
|
|
|
|
std::vector<WatchDetail> _times;
|
2024-03-15 12:29:39 -05:00
|
|
|
|
|
|
|
static const uint64_t MsToTrigger = 100;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Watchdog()
|
|
|
|
{
|
|
|
|
startThread();
|
2024-03-28 13:11:48 -05:00
|
|
|
|
|
|
|
// Steal SIGUSR2 from the backtrace handler for profiling
|
|
|
|
struct sigaction action;
|
|
|
|
|
|
|
|
sigemptyset(&action.sa_mask);
|
|
|
|
action.sa_flags = 0;
|
|
|
|
action.sa_handler = handleUserProfileSignal;
|
|
|
|
sigaction(SIGUSR2, &action, nullptr);
|
2024-03-15 12:29:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
~Watchdog()
|
|
|
|
{
|
2024-04-04 15:59:37 -05:00
|
|
|
if (_thread)
|
|
|
|
joinThread();
|
2024-03-15 12:29:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t getDisableStamp() { return 0; }
|
|
|
|
|
|
|
|
static uint64_t getTimestamp()
|
|
|
|
{
|
|
|
|
return std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
std::chrono::steady_clock::now().time_since_epoch())
|
|
|
|
.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
void startThread()
|
|
|
|
{
|
|
|
|
assert(!_thread);
|
|
|
|
_exit = false;
|
|
|
|
_thread.reset(new std::thread(&Watchdog::checkTime, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void joinThread()
|
|
|
|
{
|
2024-03-29 05:45:54 -05:00
|
|
|
if (_thread)
|
|
|
|
{
|
|
|
|
_exit = true;
|
|
|
|
_condition.notify_all();
|
|
|
|
_thread->join();
|
|
|
|
_thread.reset();
|
|
|
|
}
|
2024-03-15 12:29:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void checkTime()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> guard(_lock);
|
|
|
|
while (!_exit)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
uint64_t msSinceEpoc = getTimestamp();
|
2024-03-28 13:11:48 -05:00
|
|
|
for (auto d : _times)
|
2024-03-15 12:29:39 -05:00
|
|
|
{
|
2024-03-28 13:11:48 -05:00
|
|
|
uint64_t snapshot = *d.first; // one atomic read
|
2024-03-15 12:29:39 -05:00
|
|
|
if (snapshot == 0) // sleeping / polling
|
|
|
|
continue;
|
|
|
|
// out of the poll for longer than threshold:
|
|
|
|
if (msSinceEpoc - snapshot > MsToTrigger)
|
|
|
|
{
|
2024-03-28 13:11:48 -05:00
|
|
|
// Signal the poorly behaved thread to profile it
|
|
|
|
Util::killThreadById(*d.second, SIGUSR2);
|
2024-03-15 12:29:39 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-30 04:20:20 -05:00
|
|
|
_condition.wait_for(guard, std::chrono::milliseconds(50));
|
2024-03-15 12:29:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-28 13:11:48 -05:00
|
|
|
void addTime(std::atomic<uint64_t> *timeRef, int *threadIdRef)
|
2024-03-15 12:29:39 -05:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> guard(_lock);
|
2024-03-28 13:11:48 -05:00
|
|
|
_times.push_back(WatchDetail(timeRef, threadIdRef));
|
2024-03-15 12:29:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void removeTime(std::atomic<uint64_t> *timeRef)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> guard(_lock);
|
2024-03-28 13:11:48 -05:00
|
|
|
for (auto it = _times.begin(); it != _times.end(); it++)
|
|
|
|
{
|
|
|
|
if (it->first == timeRef)
|
|
|
|
{
|
|
|
|
it = _times.erase(it);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert("missing time to remove" && false);
|
2024-03-15 12:29:39 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|