DoEvents Regression from 2014r2 to 2018r1

Hello,

we wrote our own server software for a client.
It runs and provides a JSON based API via Serversocket from a console app to query database and have some logic in the server app.

Basically it runs a loop with DoEvents as the main loop:

do DoEvents 10 loop until ende

This loop takes 0.2% CPU time in Xojo 2014r2 and 99% in 2018r1. Mac and Linux, 32 or 64bit doesn’t matter.

Works fine in 2018r1, but as soon as we initialize the thread pool code used (From RBLibrary years ago), you can see CPU time going up.
Anyone has an idea what that could be?

A thread is running, it calls Sleep() a lot, but still he whole app doesn’t find sleep.

It has to be something in your threads. A simple test here with only the Run event loop executing from a console app built using 2012r2.1, 2016r4, 2017r3, and 2018r1 result in the same 0.3% CPU on a single core here.

I’m not seeing it either Christian. Maybe once it’s in production…

Feedback case: <https://xojo.com/issue/52167>

It may depend on the specific code in that thread pool.

Well, you may need to run a thread, which calls sleep(1,true) in the loop?

Our middleware server has at least two threads that are started before the DoEvents loop that keep running through the life of the server. Again, though, I checked on my Mac while our production platform is Linux.

(I just realized that I last compiled with something, er, newer than 2018r1. Have you tried that?)

Wont the:

Me.Sleep(1, True)

in the lower portion of ThreadPoolManager.Run always be ignored/skipped as other threads are running? This will cause the loop its in to run at full speed which causes 100% CPU usage on the logical processor. I have no idea why this is different to the old version, maybe they fixed something? If you set that True to False it runs as expected with minimal CPU usage (windows 10).

As per your ticket, commenting out:

ThreadPoolManager.SetHints Thread.NormalPriority, 5

removes an extra thread which means that the code above runs as expected with the delay being utilised which stops the CPU from being pegged at 100%.

There’s a noticeable difference in performance between 2014r2 and 2018r1 in Windows, I couldn’t call it a 2% to 99% difference though, more like 50% to 90%. The difference might just be optimisations in the compiler causing a faster loop and thus more CPU usage.

We need the thread pool to process incoming requests!
So I need ThreadPoolManager.SetHints to stay there.

Just that the sleep should let the app sleep if there is nothing to do.

[quote=386927:@Christian Schmitz]We need the thread pool to process incoming requests!
So I need ThreadPoolManager.SetHints to stay there.

Just that the sleep should let the app sleep if there is nothing to do.[/quote]

What if you make a property in the app, SleepTime As Integer.

Do
DoEvents SleepTime
Loop Untill Ende

Change Sleeptime to a high value if there is nothing todo, otherwise set it to 5-20 (ms)

^^^ That’s essentially what we do.

Well, we switch between delay 0 and 1 there. 0 when busy, 1 when not.

But still I think there is a bug in the thread handling.
If a thread called sleep(1, true) and nothing else is to be done, Xojo doesn’t do the 1ms pause.

Try higher numbers, like 10 and 50.

[quote=386952:@Christian Schmitz]Well, we switch between delay 0 and 1 there. 0 when busy, 1 when not.

But still I think there is a bug in the thread handling.
If a thread called sleep(1, true) and nothing else is to be done, Xojo doesn’t do the 1ms pause.[/quote]

i notice if i use 5ms instead of 1ms then it’s all back to normal. (tested macos xojo 2018r1)
sleep 1ms = 99.6% cpu on 32-bit
sleep 2ms = 33.7% cpu on 32-bit
sleep 5ms = 3.2%-5.6% cpu on 32-bit

sleep 1ms = 99.7% cpu on 64-bit
sleep 2ms = 35.9%-36.9% cpu on 64-bit
sleep 5ms = 3.29% cpu on 64-bit

so it’s about the same as it ever was, as i don’t see negative change if we say App.DoEvents( [ms] ) is the same as Sleep( [ms] ).
Perhaps 1ms is just too much overhead (fast switching)? I’ve just never used it as it proved the processing power it uses is having overhead due to some context switching being too fast, at least i must have read that somewhere in the past.

usually if i use any kind of sleep i never go lower than 2ms on a mac an no lower than 5ms on windows.