Jump to content
pyscripter

Revisiting TThreadedQueue and TMonitor

Recommended Posts

Guest

Just ran that stress test with Darian's default settings (thread count =30000, timeout=20) on Hyper-V guest with Server 2016, the test was for 10 minutes on 1 dedicated core guest and with another 6 cores out of 8 cores XEON CPU, both worked fine and the latter was faster almost twice (if not more) than my device.

 

Share this post


Link to post
10 hours ago, Kas Ob. said:

@Darian Miller Thank you, 

 

I am trying to make sense of my observation, now it is morning and i just turned on my PC less an hour ago, it does worked fine for about 10 minutes with 256 thread with timeout at 2ms but failed at 1ms immediately, yesterday before shutting down and after the PC was stressed all day, the 16ms was failing and 20 looked stable, while yesterday in the morning 1ms worked for more than 10 minutes without fail.

 

Windows 10 Build 1803 ( 17134.706), i7-2600K .

 

A new Quality Portal issue could be raised to ensure that the timeout values of the queue are >= 16ms.  Anything less is apparently not achievable based on earlier comments in this thread.

 

 

Share this post


Link to post
5 hours ago, Primož Gabrijelčič said:

Did some testing on that recently and most of the time wait functions work as expected while on some virtual machines they return early. The test cases were running on few Windows boxes and on two VMWare Fusion/Mac VMs. Worked OK on all but one. One Fusion/Mac combo consistently returned early from wait.

 

I've seen multiple cases of a timeout of 200ms returning in 160ms when running within VMWare.  My first stress test included a fudge factor for returning too early just for that reason.

 

Share this post


Link to post

Thanks a lot !
The proposed here changes will be in 10.4 Update 1 with some modifications. Mostly due to new cross-platform AtomicCmpExchange128 for 64bit platforms.

  • Like 6

Share this post


Link to post
1 hour ago, Darian Miller said:

A new Quality Portal issue could be raised to ensure that the timeout values of the queue are >= 16ms.  Anything less is apparently not achievable based on earlier comments in this thread.

I disagree.

TMonitor is cross platform and the ~16ms timeout granularity is a Windows limitation, which can (but very seldom should) be modified with timeBeginPeriod.

It would be better to document the current behavior on Windows - and maybe also why it's there.

Share this post


Link to post
Just now, Anders Melander said:

I disagree.

TMonitor is cross platform and the ~16ms timeout granularity is a Windows limitation, which can (but very seldom should) be modified with timeBeginPeriod.

It would be better to document the current behavior on Windows - and maybe also why it's there.

I agree!  I had switched to QP to type up a new issue and thought the same thing.  When I closed that window, this window beeped with your reply - must be good karma!

 

  • Like 1

Share this post


Link to post
Guest
2 hours ago, Darian Miller said:

I've seen multiple cases of a timeout of 200ms returning in 160ms when running within VMWare.  My first stress test included a fudge factor for returning too early just for that reason.

The following code might give hint with what is going when running code compiled with Delphi on VM.

program SimpleTiming;
{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  Windows;

procedure ASMPad4One;
asm
end;

procedure ASMPad4Two;
asm
end;

procedure ASMPad4Three;
asm
end;

procedure ASMPad4Four;
asm
end;

procedure ASMProc1;
var
  Temp1, Temp2, Temp3, Temp4: Cardinal;
asm
        mov     eax, 10000000

@Loop:
        mov     Temp1, ebx
        mov     Temp2, esi
        mov     edx, eax
        AND     edx, 3
        cmp     edx, 0
        jnz     @Skip
        XOR     Temp3, eax
        XOR     Temp4, edx

@Skip:
        dec     eax
        cmp     eax, 0
        jnz     @Loop
end;

procedure ASMProc2;
var
  Temp1, Temp2, Temp3, Temp4: Cardinal;
asm
        mov     eax, 10000000

@Loop:
        mov     Temp1, ebx
        mov     Temp2, esi
        mov     edx, eax
        AND     edx, 3
        cmp     edx, 0
        jnz     @Skip
        XOR     Temp3, eax
        XOR     Temp4, edx

@Skip:
        dec     eax
        cmp     eax, 0
        jnz     @Loop
end;

var
  Count1, Count2: Int64;

procedure Benchmark2;
var
  QS1, QF1: Int64;
  QS2, QF2: Int64;
begin
  QueryPerformanceCounter(QS1);
  ASMProc1;
  QueryPerformanceCounter(QF1);

  QueryPerformanceCounter(QS2);
  ASMProc2;
  QueryPerformanceCounter(QF2);

  Inc(Count1, QF1 - QS1);
  Inc(Count2, QF2 - QS2);
  Writeln('AsmProc1: ' + IntToStr(QF1 - QS1) + #9 + 'AsmProc2: ' + IntToStr(QF2 - QS2));
end;

const
  LOOP_COUNT = 10;

var
  i: Integer;

begin
  // force linking
  ASMPad4One;
  ASMPad4Two;
  //ASMPad4Three;
  //ASMPad4Four;

  ASMProc1;
  ASMProc2;


  Writeln('ASMProc1 address : '+IntToHex(NativeUInt(Addr(ASMProc1))));
  Writeln('ASMProc2 address : '+IntToHex(NativeUInt(Addr(ASMProc2))));
  Count1 := 0;
  Count2 := 0;
  for I := 1 to LOOP_COUNT do
    Benchmark2;
  Writeln('Average 1 : ' + IntToStr(Count1 div LOOP_COUNT) + '  Average 2 : ' +
    IntToStr(Count2 div LOOP_COUNT));
  Readln;
end.

 

Now notes on that code:

1) The code seems only 32bit but it is perfect safe to be running on 64bit, and the behaviour is somehow similar.

2) to tweak the result uncomment or comment those procedures called ASMPad4(X)

3) the result with the code above on my device is "Average 1 : 36195  Average 2 : 42693", (comment/ uncomment to make them same or reversed ) losing/gaining around 1/5 of the speed out of 12 assembly code is not a joke!, it be any more serious, specially on MM level or system.move , or managed types intrinsic functions.

4) yes it is the same assembly and the lose is huge due only 2 loops where the jump address are not aligned !!

5) you may need to add similar padding function between ASMProc1 and ASMProc2 to induce even stranger behaviour.

6) not long ago i was trying to write this demo for Arnaud and Pierre, as i think they might be interested in it more than anyone else, but reached only this result about aligned conditional jumps, and got bored before demonstrate the effect from using cmp and jmpX sequentially on VM machine ( or emulated CPU) , the mentioned behaviour needs different and little complex assembly near them like few mov with 3 registers instead of those above with one register and stack, any way this is not the point here, while it is still very important as how much compiled application can lose performance on VM, we are in cloud era after all.

7) trying to use $CODEALIGN, which i just recently heard about might fix it , didn't try, i still trust FPC for my performance critical code.

 

Now after all of that lets go to the point;

 

59 minutes ago, Dmitry Arefiev said:

The proposed here changes will be in 10.4 Update 1 with some modifications. Mostly due to new cross-platform AtomicCmpExchange128 for 64bit platforms.

Please if you are going to walk, then walk the walk till finish line.

AtomicCmpExchange128 like other AtomicCmpExchangeXX depends on that THE loop, there is always a loop and branch, pretty please align the bit of it ! this is for every CPU out there (including ARM and CPU on VM), so do your research or hire who knows.

Embarcadero gave up on the compiler to generate more efficient, faster and shorter native code, That is OK, then used assembly as workaround, so that asm part covering many parts of RTL, should be redone the right way, aligned and optimized, extra tested, definitely it should be done by professional not hobbyist.

The point of having RTL is ....., let say is not beautiful code, no one care how the code does look in RTL, as it fast and reliable, and that is the finish line.

 

It is 2020, intrinsic functions is essential for any high performance application, i can't list all what i can think of what is desperately Delphi needed, but consider those most popular from AtomicXX to bit manipulation on low level, like any other compiler, also this is a finish line.

 

My post is already very long, i am stopping here.

Share this post


Link to post
2 hours ago, Kas Ob. said:

definitely it should be done by professional not hobbyist

....i think you should have a little faith in this "hobbyist".

Share this post


Link to post
Guest
54 minutes ago, ConstantGardener said:

....i think you should have a little faith in this "hobbyist".

Hobbyist might do perfect job, professionals just do it, we are talking about specific part that will save trillions of CPU cycles in countless apps, so i prefer this part to be done right and one time.

Share this post


Link to post
1 minute ago, Kas Ob. said:

Hobbyist might do perfect job, professionals just do it

I'm afraid your experience of "professionals" differ from mine. Supposedly the original implementation was written by "professionals".

 

Quote

https://www.merriam-webster.com/dictionary/professional

professional adjective
pro·fes·sion·al | \ prə-ˈfesh-nəl
a : participating for gain or livelihood in an activity or field of endeavor often engaged in by amateurs
b : having a particular profession as a permanent career
c : engaged in by persons receiving financial return

 

  • Like 1

Share this post


Link to post
11 minutes ago, Kas Ob. said:

Hobbyist might do perfect job, professionals just do it, we are talking about specific part that will save trillions of CPU cycles in countless apps, so i prefer this part to be done right and one time.

... that was not my point. i think dmitry can handle something. ...some infos

 

  • Like 1

Share this post


Link to post
5 hours ago, Dmitry Arefiev said:

Thanks a lot !
The proposed here changes will be in 10.4 Update 1 with some modifications. Mostly due to new cross-platform AtomicCmpExchange128 for 64bit platforms.

This is worth a certain degree of celebration.   I cannot remember Embarcadero/Inprice/Borland etc. being so responsive ever. It also demonstrates the power of the collective efforts of the community to make Delphi better.

  • Like 1

Share this post


Link to post
Guest

@Anders MelanderThis is the last time i will address you, you like trolling and i am not finding it funny, if you are failed to get what i wrote there, then first i am not native English speaker, second i don't care if you get the idea or not.

 

1 minute ago, ConstantGardener said:

... that was not my point. i think dmitry can handle something. ...some infos

OK i am lost now, i wasn't talking about specific person or group, i pointing the fact that 10-15 years ago Delphi adopted FastMM4 and some of FastCode, since then what had been evolved ?, nothing on RTL side , and before start listing me the enhancement, please get the point i am talking about, compiler that is still the same, while RTL still has inefficient assembly!

That should be addressed years ago, now there is a chance for Delphi to become 2020 application generator, but definitely it should be done by experts who did right, this should be guess work.

 

As for criticism i wrote posts here, and some of you felt offended by my words, offending something they do like, be assured i like it and love it more than you, my words are out of fear to lose this love.

 

Here a blog that i follow, and i suggest everyone to read this post: ( even if you feel like a god knowing it all)

http://www.cs.uni.edu/~wallingf/blog/archives/monthly/2020-05.html#e2020-05-18T16_10_46.htm

Share this post


Link to post

 

7 minutes ago, pyscripter said:

I cannot remember Embarcadero/Inprice/Borland etc. being so responsive ever.

 

I can remember. It's every time Dmitry.

 

Share this post


Link to post
5 hours ago, Dmitry Arefiev said:

Thanks a lot !
The proposed here changes will be in 10.4 Update 1 with some modifications. Mostly due to new cross-platform AtomicCmpExchange128 for 64bit platforms.

 

This is fantastic!  Thank you.

Share this post


Link to post
Guest

Now i get it!, Dimitry is the god father of AnyDac, one of the two most valued jewels in Delphi collection, two without doubt were the right decisions made by the guys steering the wheel.

 

On other hand about two years ago (i might be wrong about the dates), Danijel Tkalcec in his RTC licensed forum (private) offered his RTC SDK in full for sell, he made full disclosure about RTC financial situation and licenses sold for some year(s) period (also can't remember for sure), he preferred one of his users to buy, while as i remember Embarcadero was Happy acquiring a navigator with bookmarks, bookmarks !! CnWizards had them for years, i didn't even know that Delphi comes without bookmarks.

 

Can Nexus Quality suit be included with Delphi/Rad Studio, sure, is it the best tool out there, no, but the developers at Nexus are capable to make it better, as long as there is interest and their time been paid, wouldn't this be better than marketing Deleaker ?

 

Share this post


Link to post
8 hours ago, Kas Ob. said:

This is the last time i will address you, you like trolling and i am not finding it funny, if you are failed to get what i wrote there, then first i am not native English speaker, second i don't care if you get the idea or not.

Me disagreeing with you or not understanding what your point is, and saying so, is hardly trolling but feel free to ignore me if that's more convenient to you.

I don't mind constructive criticism of Delphi, Embarcadero, me or my work at all. I can deal it as good as any, when I get in the mood. But please do try to focus your critique a bit. When you start listing all the things you're upset about about it just become off topic ramblings.

 

8 hours ago, Kas Ob. said:

i pointing the fact that 10-15 years ago Delphi adopted FastMM4 and some of FastCode, since then what had been evolved ?, nothing on RTL side , and before start listing me the enhancement, please get the point i am talking about, compiler that is still the same, while RTL still has inefficient assembly!

I too would like a better compiler and a more optimized RTL, but I do think that the compiler is the more important. The RTL is a general purpose library and as such I expect it to be generally usable but I don't expect it to be optimal. For that would always roll my own. It would be nice if I didn't have to, but I've never used a tool where this wasn't the case.

 

8 hours ago, Kas Ob. said:

On other hand about two years ago (i might be wrong about the dates), Danijel Tkalcec in his RTC licensed forum (private) offered his RTC SDK in full for sell

I'm guessing your point here is that they should've acquired RTC instead of Parnassus?

Apart from the fact that RTC is a special purpose library (with limited relevance to the majority of Delphi users) while Parnassus is a general use utility, you do realize that acquiring the ownership of a third party product is probably the easiest and cheapest part of the process. The hard part is integrating it into your own product, documenting it and finding the resources to maintain and enhance it.

It's sooo easy to sit on the side line and say why didn't they do this and why didn't they do that, when you're not the one responsible for the product.

 

8 hours ago, Kas Ob. said:

Can Nexus Quality suit be included with Delphi/Rad Studio, sure, is it the best tool out there, no, but the developers at Nexus are capable to make it better, as long as there is interest and their time been paid, wouldn't this be better than marketing Deleaker ?

If they bundle Nexus Quality Suite with Delphi (which I wouldn't mind) then it becomes their responsibility to document and support it and to make sure that it works. Hardly comparable to hosting a CodeRage youtube video about Deleaker if that's what you're referring to.

Share this post


Link to post
Guest

Will answer this time to make sure the point(s) are delivered, may be one last time!

16 minutes ago, Anders Melander said:

But please do try to focus your critique a bit. When you start listing all the things you're upset about about it just become off topic ramblings.

The loudest voice in the room is mostly the wrong/stupid one, yea i see the irony here, but this all what i can do before let it go, sometimes you need to shout as loud as you can to make the other person get wake up and get your attention for one last time.

 

19 minutes ago, Anders Melander said:

The RTL is a general purpose library and as such I expect it to be generally usable but I don't expect it to be optimal. For that would always roll my own. It would be nice if I didn't have to, but I've never used a tool where this wasn't the case.

I mentioned the compiler few times, as i said if they gave up on it, then i suggest to take the shortest path, work on the RTL, and remember no matter what RTL you do own, with the compiler it will return to those low level calls in the RTL.System , you know that and refuse to acknowledge it, that is your opinion, and i respect every opinion.

 

22 minutes ago, Anders Melander said:

I'm guessing your point here is that they should've acquired RTC instead of Parnassus?

No that wasn't the point, that was an example of not-best-decision and missed opportunity, like i said where would Delphi be without FireDac ?, what Dimitry is doing with them? ( no answer needed here) 

Delphi in 2020 doesn't have a communication suit ( sockets, HTTP, secure transport.....), doesn't have Security suit,... (too long to list)

Hell without Pierre it wouldn't have decent Memory Manager, and now with Gunther and Pierre we have the chance to have better one, 

Andres , can you see the pattern ? 

 

When i mentioned RTC, i was comparing the right choice that could affect all of us and make it more attractive to be used in new projects, this is in their interest at first place, Will anybody having a bookmark navigator in the IDE affect his decision about buying the IDE ?!

 

30 minutes ago, Anders Melander said:

If they bundle Nexus Quality Suite with Delphi (which I wouldn't mind) then it becomes their responsibility to document and support it and to make sure that it works. Hardly comparable to hosting a CodeRage youtube video about Deleaker if that's what you're referring to.

Again you did not get full picture of my point, simply put advertise what so ever you want as long you are taking care of your people, and your people are those 3rd-party along with your clients (us), they made Delphi what it is now.

 

Anders, do i sound like ramblings, may be, believe me i am not enjoying this and i don't like wasting my time or anyones.

Now again to make sure @ConstantGardener get me right because i feel we missed understand each other.

 

I have no doubt Dimitry is a top expert in DB developing, that not a thing to discuss but will it be a good decision to ask Dimity to work on the compiler, with all my respect, that will be wrong because Dimitry mastership came from hundred thousands of hours with many many years working on the DB, while the compiler need same mastership with same expert level and time spent on compiler, and this is a contradiction, plain and simple.

And this lead us to Embarcadero doesn't not have the qualified key people to get those things covered, yes they covered in DB with Dimitry, what about... !!!

 

Now to be blunt: telling a friend or a relative Attaboy when he is wrong is wrong, no good coming from this, burying your head in the sand and pretend everything is OK, also wrong, is Embarcadero doing its best ?, nope, they are not even trying.

 

Making long posts in a forum does have a chance to be heard and the points to delivered and received, so please Anders refrain form answering me as i said, you are going after me in person, while i am after correcting a process that affect me and my life like of all you here.

Share this post


Link to post
9 minutes ago, Kas Ob. said:

you are going after me in person

No. I don't know you anything about you. I'm arguing the points you make as I understand them. Nothing personal in that.

 

I agree that things could be better (to put it mildly) with regards to the quality of the compiler, the RTL, the VCL and FMX. Is that because Embarcadero is not doing their best? I have no idea. I don't know enough about their financial situation, their business domain, their strategy or most of the factors that must affect their decisions. And I bet you don't either.

 

We all make mistakes and we all write code that could be better. Those that have worked with me know that I have no problem with ripping someone a new a*hole, in public, if they make mistakes because they're lazy or if they don't own up to their mistakes. Like I said I don't know enough about the internals of Embarcadero to judge why they make mistakes, but they certainly could be better at acknowledging them and taking responsibility for them.

Share this post


Link to post
Guest
26 minutes ago, Anders Melander said:

No. I don't know you anything about you. I'm arguing the points you make as I understand them. Nothing personal in that.

Yet, you tried to stone me in public, and please i don't to steer this far away, lets stop this (this conversation between you and me) here, it is already far from the subject of this thread, but there is not other Delphi platform to sound logic and hope get logical discussion about it.

Last Wednesday i spent around an hour trying to solve a bug, found it and solved it, it was a corruption in the Manifest file, and yesterday when i brought RTC (still fresh in my brain after i answered a question here) as an example i was choosing between it and a decent manifest editor ?!!!

Do you know one can be accompanied with Delphi/RAD ? I believe so why a bookmark navigator would be more important from having a decent full resource editor, from the box ? let that sink, you mentioned maintaining and documentation.... is it so hard for you to do this extra tool, you are already doing nice job, right ? (no answer please)

 

 

I started this after seeing Dimitry here, and it is painful for me to meet him like this with such subject instead having nice words, but i really want to be sure that the point had been received and it is up to each and every client, sounds you concern, but not in this thread.

 

 

Share this post


Link to post

Using TThreadedQueue<T> in Delphi 10.4.1, I have a couple of computers with windows 10 running an application of mine that have an Access violation error in line 7917 of system.generics.collections as reported by Eurekalog.

it is the line: "TMonitor.Enter(FQueueLock);" It is not random. It happens all the time the first time it is used.

Can you think of a reason?

function TThreadedQueue<T>.PushItem(const AItem: T; var AQueueSize: Integer): TWaitResult;
begin
  TMonitor.Enter(FQueueLock);
  try
    Result := wrSignaled;
    while (Result = wrSignaled) and (FQueueSize = Length(FQueue)) and not FShutDown do
      if not TMonitor.Wait(FQueueNotFull, FQueueLock, FPushTimeout) then
        Result := wrTimeout;

    if FShutDown or (Result <> wrSignaled) then
      Exit;

    FQueue[(FQueueOffset + FQueueSize) mod Length(FQueue)] := AItem;
    Inc(FQueueSize);
    Inc(FTotalItemsPushed);

  finally
    AQueueSize := FQueueSize;
    TMonitor.Exit(FQueueLock);
  end;

  TMonitor.Pulse(FQueueNotEmpty);
end;

 

Share this post


Link to post
5 hours ago, dkounal said:

Using TThreadedQueue<T> in Delphi 10.4.1, I have a couple of computers with windows 10 running an application of mine that have an Access violation error in line 7917 of system.generics.collections as reported by Eurekalog.

it is the line: "TMonitor.Enter(FQueueLock);" It is not random. It happens all the time the first time it is used.

Can you think of a reason?

 

Can you provide a small sample project that reproduces the error?  My limited testing shows that it's working much better in 10.4.1.  If you could reproduce the problem in a simple example project, it has a much higher chance at getting fixed.

Share this post


Link to post
On 1/29/2021 at 6:13 PM, Darian Miller said:

 

Can you provide a small sample project that reproduces the error?  My limited testing shows that it's working much better in 10.4.1.  If you could reproduce the problem in a simple example project, it has a much higher chance at getting fixed.

I can not reproduce it in a separate project and it does not happen to all computers in the original Delphi project. I am still looking for a solution.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×