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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
/*
* Copyright 2023 KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "pressurewatcher.h"
#include <QMetaEnum>
#include <QDebug>
#include <sys/epoll.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "common.h"
PressureWatcher::PressureWatcher(QObject *parent)
: ResourceWatcher{parent}
, m_stop(false)
{
initTriggers();
}
void PressureWatcher::start()
{
if (m_triggers.isEmpty()) {
qWarning() << "PressureWatcher: pressure triggers is empty.";
return;
}
struct epoll_event events[1024];
int epfd = epoll_create(1);
if (epfd < 0) {
qWarning() << "epoll_create failed, " << strerror(errno);
return;
}
qDebug() << "m_triggers" << m_triggers.first().resource;
for (auto &trigger : m_triggers) {
int fd = open(trigger.fileInterface.toLocal8Bit().data(), O_RDWR | O_NONBLOCK);
if (fd < 0) {
qWarning() << "open file " << trigger.fileInterface << " failed, " << strerror(errno);
continue;
}
QByteArray ba = trigger.trigger.toLocal8Bit();
const char *trigger2write = ba.data();
if (write(fd, trigger2write, strlen(trigger2write) + 1) < 0) {
qWarning() << "write failed, " << strerror(errno);
continue;
}
epoll_event ev;
ev.data.fd = fd;
ev.events = EPOLLPRI;
ev.events |= EPOLLET;
int result = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);
if (result < 0) {
qWarning() << "epoll_ctl failed, " << strerror(errno);
continue;
}
trigger.fd = fd;
}
while (!m_stop) {
qDebug() << "epoll wait ";
int n = epoll_wait(epfd, events, 1024, -1);
qDebug() << "epoll wait " << n;
QMap<ConfManager::Resource, ConfManager::ResourceUrgency> triggersUrgency;
for (int i=0; i<n; ++i) {
if (events[i].events & EPOLLPRI) {
qDebug() << "fd " << events[i].data.fd;
PressureTrigger trig = trigger(events[i].data.fd);
if (triggersUrgency.contains(trig.resource)) {
if (triggersUrgency.value(trig.resource) < trig.urgency) {
triggersUrgency[trig.resource] = trig.urgency;
}
continue;
}
triggersUrgency[trig.resource] = trig.urgency;
}
}
handleWarningMessage(triggersUrgency);
sleep(1);
}
for (auto const &trigger : qAsConst(m_triggers)) {
close(trigger.fd);
}
}
void PressureWatcher::stop()
{
m_stop = true;
}
void PressureWatcher::initTriggers()
{
ConfManager &confMgr = common::Singleton<ConfManager>::GetInstance();
auto initTrigger = [this, &confMgr](ConfManager::Resource resouce) {
auto trigger = confMgr.pressureTriggers(resouce);
auto triIt = trigger.constBegin();
QMetaEnum meta = QMetaEnum::fromType<ConfManager::Resource>();
QString strResouce(meta.valueToKey(resouce));
QString fileInterface = QString("/proc/pressure/%1").arg(strResouce.toLower());
while (triIt != trigger.constEnd()) {
m_triggers.push_back({ resouce, triIt.key(), triIt.value(), fileInterface, -1 });
++ triIt;
}
};
auto pressureResources = confMgr.pressureResouces();
for (const auto &resource : pressureResources) {
initTrigger(resource);
}
}
PressureWatcher::PressureTrigger PressureWatcher::trigger(int fd)
{
for (const auto &trigger : qAsConst(m_triggers)) {
if (trigger.fd == fd) {
return trigger;
}
}
return PressureWatcher::PressureTrigger {};
}
void PressureWatcher::handleWarningMessage(QMap<ConfManager::Resource, ConfManager::ResourceUrgency> triggerUrgency)
{
ConfManager &confMgr = common::Singleton<ConfManager>::GetInstance();
QMetaEnum meta = QMetaEnum::fromType<ConfManager::ResourceUrgency>();
auto it = triggerUrgency.constBegin();
while (it != triggerUrgency.constEnd()) {
int level = confMgr.resourceUrgencyIndex(it.value());
if (level == -1) {
qWarning() << "Get urgency index error, urgency is" << it.value();
++ it;
continue;
}
++ level;
qDebug() << "level " << level << it.key();
switch (it.key())
{
case ConfManager::CPU:
Q_EMIT ResourceThresholdWarning("CPU", level);
break;
case ConfManager::IO:
Q_EMIT ResourceThresholdWarning("IO", level);
break;
case ConfManager::Memory:
Q_EMIT ResourceThresholdWarning("Memory", level);
break;
}
++ it;
}
}
|