https://bugs.kde.org/show_bug.cgi?id=528666

            Bug ID: 528666
           Summary: Sensors of a chart sometimes load in the wrong order:
                    SensorResolver relies on SensorQuery::finished
                    arriving in start order
    Classification: Applications
           Product: plasma-systemmonitor
      Version First 6.6.6
       Reported In:
          Platform: Ubuntu
                OS: Linux
            Status: REPORTED
          Severity: normal
          Priority: NOR
         Component: general
          Assignee: [email protected]
          Reporter: [email protected]
                CC: [email protected], [email protected]
  Target Milestone: ---

SUMMARY
A chart that shows two or more sensors sometimes comes up with them in a
different order from the one saved in the page, so the legend reads, for
example, GPU then CPU where the page says CPU then GPU. It changes from one
launch to the next with nothing in the configuration changing. The cause is in
libksysguard: SensorResolver assumes the queries it starts finish in the order
it started them, and QtDBus does not keep that order when one call's reply
arrives before its QDBusPendingCallWatcher has been constructed. On my machine,
with every core busy, that happens to about one query in five. The analysis and
measurements are under ADDITIONAL INFORMATION.

STEPS TO REPRODUCE
1. Keep every CPU core busy. My machine had a load average of 13 to 20 on 8
threads throughout; I have not tried an idle machine.
2. Start System Monitor and look at a chart whose sensor list has two or more
entries. The stock History page has two: Memory (memory/physical/used,
memory/swap/used) and Network (network/all/download, network/all/upload). My
own page has line charts with a CPU sensor first and a GPU sensor second.
3. Quit and start it again. Repeat.

OBSERVED RESULT
On some launches the list is resolved in reverse. In 35 launches each of my two
charts was reversed 6 times, and the History page's Memory and Network lists 3
times each. On my charts I checked that the legend follows the reversed list;
for the stock pages I checked the resolved list only. The lines keep their
colours, because colours are stored per sensor id.

EXPECTED RESULT
The sensors are always in the order saved in the page.

SOFTWARE/OS VERSIONS
Operating System: Ubuntu 26.04.1 LTS, kernel 7.0.0-43-generic
KDE Plasma Version: 6.6.6 (plasma-systemmonitor 6.6.6, ksystemstats 6.6.6,
libksysguard 6.6.5, as packaged by Ubuntu)
KDE Frameworks Version: 6.24.0
Qt Version: 6.10.2
Graphics Platform: Wayland

ADDITIONAL INFORMATION

THE CAUSE

1. SensorResolver::execute() in faces/SensorFaceController.cpp (lines 147 to
184 on master at 7dc5af97; the appending is the same in 6.6.5) starts one
SensorQuery per entry of the list, and the handler of each query's finished
signal appends that query's ids to the result:

    query->connect(query, &KSysGuard::SensorQuery::finished, controller,
[this](SensorQuery *query) {
        query->sortByName();
        ...
        std::transform(ids.begin(), ids.end(), std::back_inserter(found), ...);

"found" becomes highPrioritySensorIds, totalSensors or lowPrioritySensorIds, so
each of those is in the order the finished signals were delivered, not the
order of the entries.

2. Every query makes its own allSensors call and only then constructs the
watcher for it (SensorDaemonInterface::allSensors() in
sensors/SensorDaemonInterface.cpp):

    return new QDBusPendingCallWatcher{d->dbusInterface->allSensors()};

3. QtDBus delivers a watcher's finished signal by one of two routes (qtbase
v6.10.2, src/dbus/qdbuspendingcall.cpp and qdbusintegrator.cpp):

    a. The reply arrives after the watcher exists.
QDBusConnectionPrivate::processFinishedCall(), on the D-Bus thread, emits the
helper's finished(), which QDBusPendingCallWatcherHelper::add() connected to
the watcher with Qt::QueuedConnection. One posted event.

    b. The reply arrives before the watcher exists. The watcher's constructor
finds replyMessage already set and calls
QMetaObject::invokeMethod(d->watcherHelper,
&QDBusPendingCallWatcherHelper::finished, Qt::QueuedConnection). When that
event is handled it emits finished(), which is queued to the watcher again. Two
posted events, the second posted only when the first is handled.

So when query A goes by route b and the next query B by route a, and B's reply
is posted before the event loop has handled A's first event, B's watcher fires
before A's and B's ids are appended first.

4. The reply beats the watcher because the main thread is preempted between
making the call and constructing the watcher, presumably at the moment the call
wakes Qt's D-Bus thread. With every core busy it does not run again for about
1.4 ms, by which time ksystemstats has answered.

MEASUREMENTS

I preloaded a small observer into the unmodified plasma-systemmonitor with
LD_PRELOAD. It wraps SensorQuery's constructor, SensorQuery::execute(),
SensorQuery::sortByName() (the first call in the resolver's handler) and
QDBusPendingCallWatcher's constructor. Each wrapper logs a line and calls the
real function, and the watcher's wrapper first reads
QDBusPendingCall::isFinished(), the same condition the constructor tests. 35
launches with QT_QPA_PLATFORM=offscreen:

- 976 queries. In 186 of them (19%) the reply was already there when the
watcher was constructed. The time from entering execute() to the watcher's
constructor had a median of about 1.4 ms for those and 0.03 ms for the rest. In
the 59 of them for which I also recorded getrusage(RUSAGE_THREAD), the main
thread had an involuntary context switch in that interval in all 59, a
voluntary one in none, and used about 0.04 ms of CPU time.

- Seven lists of two or more entries (my two charts; the History page's Memory
and Network; the Overview page's Memory and Swap totalSensors and its Networks
highPrioritySensorIds): 244 loads, 23 out of order. A route b query ahead of a
route a query in the same list occurred in 24 loads, and 23 of those were the
ones out of order. None of the other 220 loads was out of order.

- Holding every watcher's construction back 20 ms in the observer, which puts
every query on route b: 10 launches, no list out of order.

- Holding back only the watcher of the first entry of my two charts: route b
then route a occurred in 17 of 20 loads, and 10 of those were reversed. I take
the other 7 to be the case where the event loop handled the first query's first
event before the second reply arrived, but I did not measure that.

OTHER EFFECTS

- For a chart with no saved colours, ColorGradientSource.generate() in
faces/import/SensorFace.qml assigns colours by index into highPrioritySensorIds
and saves them. A first load that resolves in reverse pairs the colours with
the sensors the other way round, and that pairing is kept. I saw this in a test
with the order forced.

- The System Monitor widgets use the same SensorFaceController. I only tested
the app.

-- 
You are receiving this mail because:
You are watching all bug changes.

Reply via email to