From 960640f5bc3baa38f3e1a81761b619827155648c Mon Sep 17 00:00:00 2001 From: Evgenii Alekseev Date: Sun, 31 Mar 2024 16:30:33 +0300 Subject: [PATCH] feat: restore multithreaded processing --- .../package/contents/ui/dataengine.qml | 4 -- .../plugin/awdataengineaggregator.cpp | 8 ++-- .../plugin/awdataengineaggregator.h | 2 +- sources/awesome-widget/plugin/awkeys.cpp | 47 ++++++------------- sources/awesome-widget/plugin/awkeys.h | 8 +--- sources/extsysmonsources/extsysmonsensor.cpp | 5 +- sources/libraries.cmake | 6 +-- sources/test/testawkeys.cpp | 6 --- sources/test/testawkeys.h | 1 - 9 files changed, 25 insertions(+), 62 deletions(-) diff --git a/sources/awesome-widget/package/contents/ui/dataengine.qml b/sources/awesome-widget/package/contents/ui/dataengine.qml index d059407..23e49a4 100644 --- a/sources/awesome-widget/package/contents/ui/dataengine.qml +++ b/sources/awesome-widget/package/contents/ui/dataengine.qml @@ -191,10 +191,6 @@ KCM.SimpleKCM { Component.onCompleted: { // init submodule awKeys.updateCache() - - // update hdd model - hdd.model = awKeys.getHddDevices() - hdd.onCompleted } Component.onDestruction: { diff --git a/sources/awesome-widget/plugin/awdataengineaggregator.cpp b/sources/awesome-widget/plugin/awdataengineaggregator.cpp index 36be7bd..29dfe0a 100644 --- a/sources/awesome-widget/plugin/awdataengineaggregator.cpp +++ b/sources/awesome-widget/plugin/awdataengineaggregator.cpp @@ -139,7 +139,7 @@ void AWDataEngineAggregator::sensorRemoved(const QString &_sensor) } -void AWDataEngineAggregator::updateData(KSysGuard::SensorDataList _data) +void AWDataEngineAggregator::updateData(const KSysGuard::SensorDataList &_data) { emit(dataUpdated(m_sensors, _data)); } @@ -147,9 +147,9 @@ void AWDataEngineAggregator::updateData(KSysGuard::SensorDataList _data) void AWDataEngineAggregator::updateSensors(const QHash &_sensors) { - for (auto sensor = _sensors.cbegin(); sensor != _sensors.cend(); ++sensor) { - if (!isValidSensor(sensor.value())) + for (auto [source, sensor] : _sensors.asKeyValueRange()) { + if (!isValidSensor(sensor)) continue; - m_sensors.insert(sensor.key(), sensor.value()); + m_sensors.insert(source, sensor); } } diff --git a/sources/awesome-widget/plugin/awdataengineaggregator.h b/sources/awesome-widget/plugin/awdataengineaggregator.h index 244811a..a528302 100644 --- a/sources/awesome-widget/plugin/awdataengineaggregator.h +++ b/sources/awesome-widget/plugin/awdataengineaggregator.h @@ -49,7 +49,7 @@ public slots: void dropSource(const QString &_source); void sensorAdded(const QString &_sensor); void sensorRemoved(const QString &_sensor); - void updateData(KSysGuard::SensorDataList _data); + void updateData(const KSysGuard::SensorDataList &_data); void updateSensors(const QHash &_sensors); private: diff --git a/sources/awesome-widget/plugin/awkeys.cpp b/sources/awesome-widget/plugin/awkeys.cpp index 8009849..94b4c43 100644 --- a/sources/awesome-widget/plugin/awkeys.cpp +++ b/sources/awesome-widget/plugin/awkeys.cpp @@ -17,11 +17,11 @@ #include "awkeys.h" +#include #include #include #include #include -#include #include "awdataaggregator.h" #include "awdataengineaggregator.h" @@ -153,26 +153,6 @@ QStringList AWKeys::dictKeys(const bool _sorted, const QString &_regexp) const } -QVariantList AWKeys::getHddDevices() const -{ - QStringList hddDevices = m_keyOperator->devices("hdd"); - // required by selector in the UI - hddDevices.insert(0, "disable"); - hddDevices.insert(0, "auto"); - - // build model - QVariantList devices; - for (auto &device : hddDevices) { - QVariantMap model; - model["label"] = device; - model["name"] = device; - devices.append(model); - } - - return devices; -} - - QString AWKeys::infoByKey(const QString &_key) const { qCDebug(LOG_AW) << "Requested info for key" << _key; @@ -202,12 +182,14 @@ void AWKeys::editItem(const QString &_type) void AWKeys::dataUpdated(const QHash &_sensors, const KSysGuard::SensorDataList &_data) { - for (auto &single : _data) { - if (_sensors.contains(single.sensorProperty)) { - setDataBySource(single.sensorProperty, _sensors.value(single.sensorProperty), single); - } - // TODO use QtConcurrent::map or something like that - // QtConcurrent::run(m_threadPool, this, &AWKeys::setDataBySource, "ss", sensor); + qCDebug(LOG_AW) << "Update data for" << _data.count() << "items"; + + // though it is better to use QtConcurrent::map here, but it might cause stack corruption + for (auto &data : _data) { + if (!_sensors.contains(data.sensorProperty)) + continue; + auto sensor = _sensors[data.sensorProperty]; + std::ignore = QtConcurrent::run(m_threadPool, &AWKeys::setDataBySource, this, data.sensorProperty, sensor, data.payload); } } @@ -272,7 +254,7 @@ void AWKeys::calculateValues() m_values["memtotmb"] = m_values["memusedmb"].toLongLong() + m_values["memfreemb"].toLongLong(); m_values["memtotgb"] = m_values["memusedgb"].toDouble() + m_values["memfreegb"].toDouble(); // mem - m_values["mem"] = 100.0f * m_values["memmb"].toDouble() / m_values["memtotmb"].toDouble(); + m_values["mem"] = 100.0 * m_values["memmb"].toDouble() / m_values["memtotmb"].toDouble(); // up, down, upkb, downkb, upunits, downunits int netIndex = m_keyOperator->devices("net").indexOf(m_values["netdev"].toString()); @@ -291,7 +273,7 @@ void AWKeys::calculateValues() m_values["swaptotmb"] = m_values["swapmb"].toLongLong() + m_values["swapfreemb"].toLongLong(); m_values["swaptotgb"] = m_values["swapgb"].toDouble() + m_values["swapfreegb"].toDouble(); // swap - m_values["swap"] = 100.0f * m_values["swapmb"].toDouble() / m_values["swaptotmb"].toDouble(); + m_values["swap"] = 100.0 * m_values["swapmb"].toDouble() / m_values["swaptotmb"].toDouble(); // user defined keys for (auto &key : m_keyOperator->userKeys()) @@ -357,10 +339,9 @@ QString AWKeys::parsePattern(QString _pattern) const } -void AWKeys::setDataBySource(const QString &_source, const KSysGuard::SensorInfo &_sensor, - const KSysGuard::SensorData &_data) +void AWKeys::setDataBySource(const QString &_source, const KSysGuard::SensorInfo &_sensor, const QVariant &_value) { - qCDebug(LOG_AW) << "Source" << _source << _sensor.name << "with data" << _data.payload; + qCDebug(LOG_AW) << "Source" << _source << _sensor.name << "with data" << _value; // first list init auto tags = m_aggregator->keysFromSource(_source); @@ -374,6 +355,6 @@ void AWKeys::setDataBySource(const QString &_source, const KSysGuard::SensorInfo } m_mutex.lock(); - std::for_each(tags.cbegin(), tags.cend(), [this, &_data](const QString &tag) { m_values[tag] = _data.payload; }); + std::for_each(tags.cbegin(), tags.cend(), [this, _value](const QString &tag) { m_values[tag] = _value; }); m_mutex.unlock(); } diff --git a/sources/awesome-widget/plugin/awkeys.h b/sources/awesome-widget/plugin/awkeys.h index 98ce5ca..c5dbefa 100644 --- a/sources/awesome-widget/plugin/awkeys.h +++ b/sources/awesome-widget/plugin/awkeys.h @@ -46,22 +46,19 @@ public: Q_INVOKABLE void updateCache(); // keys Q_INVOKABLE [[nodiscard]] QStringList dictKeys(bool _sorted = false, const QString &_regexp = "") const; - Q_INVOKABLE [[nodiscard]] QVariantList getHddDevices() const; // values Q_INVOKABLE [[nodiscard]] QString infoByKey(const QString &_key) const; Q_INVOKABLE [[nodiscard]] QString valueByKey(const QString &_key) const; // configuration Q_INVOKABLE void editItem(const QString &_type); -public slots: - void dataUpdated(const QHash &_sensors, const KSysGuard::SensorDataList &_data); - signals: void dropSourceFromDataengine(const QString &_source); void needTextToBeUpdated(const QString &_newText) const; void needToolTipToBeUpdated(const QString &_newText) const; private slots: + void dataUpdated(const QHash &_sensors, const KSysGuard::SensorDataList &_data); void reinitKeys(const QStringList &_currentKeys); void updateTextData(); @@ -70,8 +67,7 @@ private: void calculateValues(); void createDBusInterface(); [[nodiscard]] QString parsePattern(QString _pattern) const; - void setDataBySource(const QString &_source, const KSysGuard::SensorInfo &_sensor, - const KSysGuard::SensorData &_data); + void setDataBySource(const QString &_source, const KSysGuard::SensorInfo &_sensor, const QVariant &_value); // objects AWDataAggregator *m_dataAggregator = nullptr; AWDataEngineAggregator *m_dataEngineAggregator = nullptr; diff --git a/sources/extsysmonsources/extsysmonsensor.cpp b/sources/extsysmonsources/extsysmonsensor.cpp index b910aec..7d38eb7 100644 --- a/sources/extsysmonsources/extsysmonsensor.cpp +++ b/sources/extsysmonsources/extsysmonsensor.cpp @@ -73,10 +73,7 @@ void ExtSysMonSensor::update() void ExtSysMonSensor::loadProperties() { - auto sensors = m_source->sources(); - for (auto sensor = sensors.cbegin(); sensor != sensors.cend(); ++sensor) { - auto source = sensor.key(); - auto info = sensor.value(); + for (auto [source, info] : m_source->sources().asKeyValueRange()) { auto property = new KSysGuard::SensorProperty(source, info->name, this); property->setUnit(info->unit); diff --git a/sources/libraries.cmake b/sources/libraries.cmake index 2d9aaa9..49889e6 100644 --- a/sources/libraries.cmake +++ b/sources/libraries.cmake @@ -2,17 +2,17 @@ find_package(Gettext REQUIRED) # main qt libraries -find_package(Qt6 6.6.0 REQUIRED COMPONENTS Core DBus Network Qml Test Widgets) +find_package(Qt6 6.6.0 REQUIRED COMPONENTS Core Concurrent DBus Network Qml Test Widgets) add_definitions( ${Qt6Core_DEFINITIONS} ${Qt6DBus_DEFINITIONS} ${Qt6Network_DEFINITIONS} ${Qt6Qml_DEFINITIONS} ${Qt6Widgets_DEFINITIONS} ) set(Qt_INCLUDE - ${Qt6Core_INCLUDE_DIRS} ${Qt6DBus_INCLUDE_DIRS} ${Qt6Network_INCLUDE_DIRS} + ${Qt6Core_INCLUDE_DIRS} ${Qt6Concurrent_INCLUDE_DIRS} ${Qt6DBus_INCLUDE_DIRS} ${Qt6Network_INCLUDE_DIRS} ${Qt6Qml_INCLUDE_DIRS} ${Qt6Widgets_INCLUDE_DIRS} ) set(Qt_LIBRARIES - ${Qt6Core_LIBRARIES} ${Qt6DBus_LIBRARIES} ${Qt6Network_LIBRARIES} + ${Qt6Core_LIBRARIES} ${Qt6Concurrent_LIBRARIES} ${Qt6DBus_LIBRARIES} ${Qt6Network_LIBRARIES} ${Qt6Qml_LIBRARIES} ${Qt6Widgets_LIBRARIES} ) diff --git a/sources/test/testawkeys.cpp b/sources/test/testawkeys.cpp index 4cea17f..fae42cf 100644 --- a/sources/test/testawkeys.cpp +++ b/sources/test/testawkeys.cpp @@ -73,12 +73,6 @@ void TestAWKeys::cleanupTestCase() } -void TestAWKeys::test_hddDevices() -{ - QVERIFY(plugin->getHddDevices().count() >= 2); -} - - void TestAWKeys::test_dictKeys() { auto keys = plugin->dictKeys(); diff --git a/sources/test/testawkeys.h b/sources/test/testawkeys.h index 6ba97e8..2546448 100644 --- a/sources/test/testawkeys.h +++ b/sources/test/testawkeys.h @@ -31,7 +31,6 @@ private slots: void initTestCase(); void cleanupTestCase(); // test - void test_hddDevices(); void test_dictKeys(); void test_pattern(); void test_tooltip();