代码拉取完成,页面将自动刷新
同步操作将从 openKylin/kylin-app-cgroupd 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#include "confmanager.h"
#include <QFile>
#include <QJsonDocument>
#include <QJsonArray>
#include <QFileSystemWatcher>
#include <QJsonObject>
#include <QMetaEnum>
#include <QDebug>
ConfManager::ConfManager(QObject *parent)
: QObject{parent}
{
confFileWatcher = new QFileSystemWatcher(QStringList() << kConfFilename, this);
connect(confFileWatcher, &QFileSystemWatcher::fileChanged, this, &ConfManager::readConf);
readConf();
}
QStringList ConfManager::features() const
{
return m_features;
}
QList<ConfManager::Resource> ConfManager::pressureResouces() const
{
QList<ConfManager::Resource> resouces = m_pressureTriggers.keys();
for (auto &resource : qAsConst(resouces)) {
switch (resource)
{
case CPU:
if (!m_features.contains("cpu")) {
resouces.removeOne(resource);
}
break;
case IO:
if (!m_features.contains("io")) {
resouces.removeOne(resource);
}
break;
case Memory:
if (!m_features.contains("memory")) {
resouces.removeOne(resource);
}
break;
}
}
return resouces;
}
QMap<ConfManager::ResourceUrgency, QString> ConfManager::pressureTriggers(Resource resource) const
{
if (!m_pressureTriggers.contains(resource)) {
return QMap<ConfManager::ResourceUrgency, QString>();
}
QMetaEnum meta = QMetaEnum::fromType<ResourceUrgency>();
ResourceUrgencyThrehold threholds = m_pressureTriggers.value(resource);
if (threholds.size() != meta.keyCount()) {
return QMap<ConfManager::ResourceUrgency, QString>();
}
QMap<ConfManager::ResourceUrgency, QString> result;
for (int i=0; i<meta.keyCount(); ++i) {
int value = threholds.value(ResourceUrgency(meta.value(i)));
qDebug() << value << meta.value(i) << threholds.contains(ConfManager::Low) << ResourceUrgency(meta.value(i)) << threholds;
QString trigger = QString("some %1 1000000").arg(value / (double)100 * 1000000);
result[ResourceUrgency(meta.value(i))] = trigger;
}
qDebug() << "result" << result;
return result;
}
ConfManager::ResourceUrgencyThrehold ConfManager::resourceThreshold(Resource resource) const
{
if (!m_resourceThresholds.contains(resource)) {
return ResourceUrgencyThrehold();
}
return m_resourceThresholds[resource];
}
int ConfManager::detectEffectiveNumber(Resource resource) const
{
if (!m_detectEffectiveNumbers.contains(resource)) {
return 0;
}
return m_detectEffectiveNumbers.value(resource);
}
int ConfManager::resourceUrgencyIndex(ResourceUrgency urgency) const
{
QMetaEnum urgencyMeta = QMetaEnum::fromType<ResourceUrgency>();
for (int i=0; i<urgencyMeta.keyCount(); ++i) {
if (urgencyMeta.value(i) == urgency) {
return i;
}
}
return -1;
}
void ConfManager::readConf()
{
QFile file(kConfFilename);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "open json file failed: " << kConfFilename << file.errorString();
return;
}
QJsonDocument jdc(QJsonDocument::fromJson(file.readAll()));
auto jsonObj = jdc.object();
if (jsonObj.isEmpty() || !jsonObj.contains("rulesets")) {
qWarning() << "Config file error, rulesets is empty.";
return;
}
auto features = jsonObj["features"];
updateFeatures(features.toObject());
auto rulesets = jsonObj["rulesets"].toArray();
if (rulesets.isEmpty()) {
qWarning() << "Config file error, rulesets is empty.";
return;
}
auto detectors = rulesets.first().toObject().value("detectors").toArray();
if (detectors.isEmpty()) {
qWarning() << "Config file error, no detector defined.";
return;
}
for (const auto &detector : qAsConst(detectors)) {
auto detectorObj = detector.toObject();
if (detector.toObject().isEmpty()) {
qWarning() << "Config file error, detectors is empty.";
continue;
}
bool ok;
QString resource = detectorObj.value("resource").toString();
auto thresholds = detectorObj.value("threshold").toArray();
int effectiveNumber = detectorObj.value("effectiveNumber").toInt();
QString api = detectorObj.value("api").toString();
qDebug() << "thresholds" << thresholds;
updateResourceConfig(resource, api, thresholds, effectiveNumber);
}
qDebug() << "read conf" << m_pressureTriggers << m_resourceThresholds;
}
void ConfManager::updateFeatures(const QJsonObject &jsonObj)
{
m_features.clear();
if (jsonObj.isEmpty()) {
qWarning() << "Features config is empty.";
return;
}
auto it = jsonObj.constBegin();
while (it != jsonObj.constEnd()) {
if (it.value().toBool()) {
m_features.push_back(it.key());
}
++ it;
}
qDebug() << "features " << m_features;
}
void ConfManager::updateResourceConfig(const QString &resource,
const QString &api,
const QJsonArray &thresholds,
const int &effectiveNumber)
{
QMetaEnum urgencyMeta = QMetaEnum::fromType<ResourceUrgency>();
QMetaEnum resouceMeta = QMetaEnum::fromType<Resource>();
if (thresholds.size() != urgencyMeta.keyCount()) {
qWarning() << "Config file thresholds error, " << thresholds << " " << urgencyMeta.keyCount();
return;
}
int enumValue = resouceMeta.keyToValue(resource.toLocal8Bit().data());
bool isResourcePressure = api.startsWith("/proc/pressure") ? true : false;
QMap<ResourceUrgency, int> resources;
for (int i=0; i<urgencyMeta.keyCount(); ++i) {
resources[ResourceUrgency(urgencyMeta.value(i))] = thresholds.at(i).toInt();
}
m_detectEffectiveNumbers[Resource(enumValue)] = effectiveNumber;
if (isResourcePressure) {
m_pressureTriggers[Resource(enumValue)] = resources;
// return;
}
m_resourceThresholds[Resource(enumValue)] = resources;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。