[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.programming

extremally important question (on how winapi program works

kenobi

8/29/2015 10:23:00 PM

This is extremally important question to me, though i doubt if many people are able to fully answer this, lets see

assume you got winapi classic WinMain()
app (like small game drawing by gdi, something like that)

you got a loop there

for(;;)
{
GameStateProcessing();
MessagePump();
Sleep(10);
}

you do your game data processing,
then call message pump classic code
(mine looks like that

for(;;)
{
/* EVENTS
*/

static int done = 0;

MSG msg;

while(PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))

if(!GetMessage(&msg, NULL, 0,0)) done = 1;
else
TranslateMessage(&msg),
DispatchMessage(&msg);

if(done) break;

//.........
}

then yet you have a sleep
(may assume for example that game processing takes 10 ms, message pump part probably is quick so, all loop 'turn' takes 20 ms so game works at 50 Hz)

THE TWO IMPORTANT QUESTIONS

1) I got WndProcedure callback with my own code there.. May I assume that this procedure is called by the MessagePump part (though indirectly i call some api call, which calls my callback back) ?

Which one api call executes it? (I mean which api call executes my callback)
Is it Dispatchmessage()?

2) Not only my ap but System needs
to be handled too.. If so i thing something that i may call virtually
SystemDoYourOwnWorkForAWhileThenBack()
must be called regurally and i wonder
when it is called.. (what rules plays here)

I wonder if this something like preempting my app by bruteforce (by freezing by some interrupt in half of the routines) in regular time periods base (I do not notice such behaviour when i measure frame times by timer)
or this is maybe done when i call
Sleep() - (Sleep could be potentially
used here i think, but i dont know how it works), (or maybe some message pump appi takes opportunity that app regullary gives avay cpu for a while then do some system work there?)

what if some app do not call Sleep?
Then all cpu would be consumed and
if no MessagePump would call system task it would have to be preempted by force
(but i am only speculating here)
How it is done?

TNX
6 Answers

Dmitry A. Kazakov

8/30/2015 7:19:00 AM

0

On Sat, 29 Aug 2015 15:23:28 -0700 (PDT), fir wrote:

> Which one api call executes it? (I mean which api call executes my callback)
> Is it Dispatchmessage()?

Yes

> 2) Not only my ap but System needs
> to be handled too.. If so i thing something that i may call virtually
> SystemDoYourOwnWorkForAWhileThenBack()
> must be called regurally and i wonder
> when it is called.. (what rules plays here)

On scheduling events. In time sharing systems one of such event is a timer
interrupt. The frequency of timer interrupts influences response times and
minimum time-sharing quants.

> I wonder if this something like preempting my app by bruteforce (by
> freezing by some interrupt in half of the routines) in regular time
> periods base (I do not notice such behaviour when i measure frame times by
> timer)

Because you did it wrong. Windows time services are unsuitable for fine
time measurements. To measure time at nanosecond accuracy (the CPU, front
bus frequency) you should use so-called performance counters derived from
real-time clock (depends on the processor/main board hardware). See
QueryPerformanceCounter.

> what if some app do not call Sleep?

The thread continues until it enters a non-busy wait (e.g. for I/O
completion) or loses CPU to a thread of higher priority. BTW, it is
customary to call Sleep(0) in order to give up the CPU by the current
thread.

> Then all cpu would be consumed and
> if no MessagePump would call system task it would have to be preempted by force
> (but i am only speculating here)
> How it is done?

Upon a scheduling event, the scheduler checks if the time quant of the
current thread is expired, if yes, the thread's effective priority is
lowered and it loses the processor for a ready-to-execute thread with a
higher priority. There are tons of scheduling strategies and algorithms for
different purposes.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-...

kenobi

8/30/2015 10:48:00 AM

0

W dniu niedziela, 30 sierpnia 2015 09:18:39 UTC+2 uzytkownik Dmitry A. Kazakov napisal:
> On Sat, 29 Aug 2015 15:23:28 -0700 (PDT), fir wrote:
>
> > Which one api call executes it? (I mean which api call executes my callback)
> > Is it Dispatchmessage()?
>
> Yes
>
> > 2) Not only my ap but System needs
> > to be handled too.. If so i thing something that i may call virtually
> > SystemDoYourOwnWorkForAWhileThenBack()
> > must be called regurally and i wonder
> > when it is called.. (what rules plays here)
>
> On scheduling events. In time sharing systems one of such event is a timer
> interrupt. The frequency of timer interrupts influences response times and
> minimum time-sharing quants.
>
> > I wonder if this something like preempting my app by bruteforce (by
> > freezing by some interrupt in half of the routines) in regular time
> > periods base (I do not notice such behaviour when i measure frame times by
> > timer)
>
> Because you did it wrong. Windows time services are unsuitable for fine
> time measurements. To measure time at nanosecond accuracy (the CPU, front
> bus frequency) you should use so-called performance counters derived from
> real-time clock (depends on the processor/main board hardware). See
> QueryPerformanceCounter.
>
> > what if some app do not call Sleep?
>
> The thread continues until it enters a non-busy wait (e.g. for I/O
> completion) or loses CPU to a thread of higher priority. BTW, it is
> customary to call Sleep(0) in order to give up the CPU by the current
> thread.
>
> > Then all cpu would be consumed and
> > if no MessagePump would call system task it would have to be preempted by force
> > (but i am only speculating here)
> > How it is done?
>
> Upon a scheduling event, the scheduler checks if the time quant of the
> current thread is expired, if yes, the thread's effective priority is
> lowered and it loses the processor for a ready-to-execute thread with a
> higher priority. There are tons of scheduling strategies and algorithms for
> different purposes.
>
this is not the answer i seek ("there is a tons of stategies") - i doubt it, (even if there are a couple of possibilities i would like to know what in my example works and roughly how

probably this is one of the two
1) the SystemWork() is dobe 'by the way' of sleep() call [or by the way of mesage pump - i dont know]
2) the system work is been doing by the
hard freezing preemptive switch, tearing of my process in random moments

Im using QPC and the times are like regular ones, if i see some fluctuations
of frame times they are very slight
(though i am not plaing heavy task in the background - but even so i think system must do some work, like with services, maybe some with network, clock etc - maybe he is quick at it end needs only like 0.3 ms per each 50 ms - thus i could not notice it I dont know) - But understanding it seem very important to me

Dmitry A. Kazakov

8/30/2015 1:12:00 PM

0

On Sun, 30 Aug 2015 03:48:02 -0700 (PDT), fir wrote:

> W dniu niedziela, 30 sierpnia 2015 09:18:39 UTC+2 u?ytkownik Dmitry A. Kazakov napisa3:

>> Upon a scheduling event, the scheduler checks if the time quant of the
>> current thread is expired, if yes, the thread's effective priority is
>> lowered and it loses the processor for a ready-to-execute thread with a
>> higher priority. There are tons of scheduling strategies and algorithms for
>> different purposes.
>>
> this is not the answer i seek ("there is a tons of stategies") - i doubt
> it,

Scheduling is an optimization problem. First, there are literally tons of
objectives, goals optimization has to achieve, e.g.

- balanced load
- minimize latency
- guaranteed serviceability
- schedulability
- minimize priority inversion
....

all more or less mutually exclusive and most of them non-achievable. Which
is the second point.

A strategy targets some weighted mix of the goals and the algorithm
implementing it is only an approximation of.

> (even if there are a couple of possibilities i would like to know what
> in my example works and roughly how

Windows scheduler is proprietary.

(In a real-time and mission critical OS, e.g. in VxWorks you can implement
your own scheduler and make the system use it.)

> probably this is one of the two
> 1) the SystemWork() is dobe 'by the way' of sleep() call [or by the way of
> mesage pump - i dont know]
> 2) the system work is been doing by the
> hard freezing preemptive switch, tearing of my process in random moments

That depends on the process priority class. See

https://msdn.microsoft.com/en-us/library/windows/desktop/ms685100%28v=vs....

Normally, a Windows process/thread can be preemted, if that was your
question.

> Im using QPC and the times are like regular ones, if i see some fluctuations
> of frame times they are very slight
> (though i am not plaing heavy task in the background - but even so i think
> system must do some work, like with services, maybe some with network,
> clock etc - maybe he is quick at it end needs only like 0.3 ms per each 50
> ms - thus i could not notice it I dont know) - But understanding it seem
> very important to me

Again, Windows is a time-sharing system. Which means, in particular, that
all tasks within time-sharing priority class get at the processor, some
time, they *share* the processor time. The percentage of the time they get
depends on the priority level and many, many other factors.

Furthermore, regarding latencies and jitter. The hard margin under Windows,
regardless processor capacity is 1ms. Defaulted to 10ms on some versions.
See

https://msdn.microsoft.com/en-us/library/windows/desktop/dd757624%28v=vs....

for further information.

Realistically, you could have about 5ms periodic tasks, reliably.

Regarding frame rates, anything below 20-50ms is meaningless because human
eye cannot notice a difference. If there is a perceived jitter then it is
other factors in play.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-...

kenobi

8/30/2015 8:38:00 PM

0

W dniu niedziela, 30 sierpnia 2015 15:12:31 UTC+2 uzytkownik Dmitry A. Kazakov napisal:
> On Sun, 30 Aug 2015 03:48:02 -0700 (PDT), fir wrote:
>
> > W dniu niedziela, 30 sierpnia 2015 09:18:39 UTC+2 uzytkownik Dmitry A. Kazakov napisal:
>
> >> Upon a scheduling event, the scheduler checks if the time quant of the
> >> current thread is expired, if yes, the thread's effective priority is
> >> lowered and it loses the processor for a ready-to-execute thread with a
> >> higher priority. There are tons of scheduling strategies and algorithms for
> >> different purposes.
> >>
> > this is not the answer i seek ("there is a tons of stategies") - i doubt
> > it,
>
> Scheduling is an optimization problem. First, there are literally tons of
> objectives, goals optimization has to achieve, e.g.
>
> - balanced load
> - minimize latency
> - guaranteed serviceability
> - schedulability
> - minimize priority inversion
> ...
>
> all more or less mutually exclusive and most of them non-achievable. Which
> is the second point.
>
> A strategy targets some weighted mix of the goals and the algorithm
> implementing it is only an approximation of.
>

riding a bike is also "tons of objectives mutaully exclusive" but in reality on some level it is not so hard

at some sustained factor (liek app described and no other heavier tasks) i think it works some easy way (dont think it changes constantly and shadulars draw some chaotic and dynamic trajectory rtc ;/ )

only would like to know how..

According to my thought it could me maybe
reasonable if system would not preempt app if it return controll in regular time slices - only could do that ifapp will not getting back longer time (but i dont know,

wil check those links maybe there s some info there

In general reasonable app imo whould work at the schema as running in some fps/Hz cycles (sorta like 20 - 200 Hz, im not sure if <20 or >200 could be considered reasonable (?) and also should be maybe assumed that do not use 100% of cpu load
(though maybe could be assumed to use up to 95 %) If so system could maybe try to fit its tasks in the the abandoned time
- system could cooperate... :/

Otherwise if this preempting slices qould not be to much long (<1ms) it would be maybe no disaster but if it would be longer it would result in jitter in frame times

(those are all my speculations, though a bit based on observation of my frame times
- in my real apps i do not notice jitter,
sorta like al the system would be executed in abandoned (sleep) time - i was even not noticing it on old one core p4 processor

will see the links in a bit moment of time,
i was already reading something but not found yet strict answer claryfing this view

(should maybe title this thread "extremally important question for understanding winapi apps" 9at it is not maybe extremally important for enybody, but still i find this understanding important)

Dmitry A. Kazakov

8/31/2015 7:50:00 AM

0

On Sun, 30 Aug 2015 13:38:28 -0700 (PDT), fir wrote:

> W dniu niedziela, 30 sierpnia 2015 15:12:31 UTC+2 u?ytkownik Dmitry A. Kazakov napisa3:
>> On Sun, 30 Aug 2015 03:48:02 -0700 (PDT), fir wrote:
>>
>>> W dniu niedziela, 30 sierpnia 2015 09:18:39 UTC+2 u?ytkownik Dmitry A. Kazakov napisa3:
>>
>>>> Upon a scheduling event, the scheduler checks if the time quant of the
>>>> current thread is expired, if yes, the thread's effective priority is
>>>> lowered and it loses the processor for a ready-to-execute thread with a
>>>> higher priority. There are tons of scheduling strategies and algorithms for
>>>> different purposes.
>>>>
>>> this is not the answer i seek ("there is a tons of stategies") - i doubt
>>> it,
>>
>> Scheduling is an optimization problem. First, there are literally tons of
>> objectives, goals optimization has to achieve, e.g.
>>
>> - balanced load
>> - minimize latency
>> - guaranteed serviceability
>> - schedulability
>> - minimize priority inversion
>> ...
>>
>> all more or less mutually exclusive and most of them non-achievable. Which
>> is the second point.
>>
>> A strategy targets some weighted mix of the goals and the algorithm
>> implementing it is only an approximation of.
>
> riding a bike is also "tons of objectives mutaully exclusive" but in
> reality on some level it is not so hard

Whatever. I just stated the fact. There is a whole research field concerned
the issue. If you are interested you can find reference lists on the web.

> According to my thought it could me maybe
> reasonable if system would not preempt app if it return controll in
> regular time slices

If a task returns the CPU prior to the time quant expiration in absence of
tasks with higher priority, the task won't be interrupted, obviously, even
tautologically.

You can easily see that in the Windows task manager behavior. It is not
uncommon to have a heavy loaded system with the task manager showing
miniscule load percentage. One of the reasons is the feature/bug of the
system CPU time measurements. Only fully spent time quants are counted.
Thus if you have many tasks switching prior to their quants expiration
(e.g. doing asynchronous I/O) the time they spent before giving up the last
quant is not counted. So, theoretically, if all tasks leave the processor
before single quant expiration, you can have 100% processor load with the
task manager showing 0%. That was not an issue for 25MHz i386, but it is
now.

> Otherwise if this preempting slices qould not be to much long (<1ms) it
> would be maybe no disaster but if it would be longer it would result in
> jitter in frame times

It is always longer than 1ms in presence of other tasks ready to execute =
under 100% CPU load. See timeBeginPeriod. 1ms is the minimum time. Thus if
a task loses the processor it won't return sooner than in 1ms (if there are
other tasks).

This is also the reason why tasks doing busy waiting (polling) should do
Sleep(0) frequently in order to give breath to other tasks. It does not
change the load, but it reduces response times and latencies.

> (those are all my speculations, though a bit based on observation of my
> frame times - in my real apps i do not notice jitter,

You would not due to task switches. Because 20-50ms jitter is non-existent
for the human eye. You have nothing to worry unless you are doing hard-real
time stuff, like signal generation or frequency measurements. For which
nobody would use a time-sharing OS anyway.

> will see the links in a bit moment of time,
> i was already reading something but not found yet strict answer claryfing this view
> (should maybe title this thread "extremally important question for
> understanding winapi apps" 9at it is not maybe extremally important for
> enybody, but still i find this understanding important)

It is unclear what the question actually is. And even less what the problem
was. It is said that stated problem is a half of the answer...

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-...

Chad

8/31/2015 11:46:00 PM

0

Dmitry,give it up. You're dealing with a low IQ toilet scrubber. Also known as a fucking moron.