Recent

Author Topic: [SOLVED]Queryperformance for linux: check for correctness please.  (Read 761 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 15646
  • Censorship about opinions does not belong here.
I have written two timer functions to mimic the Windows QueryPerformanceCounter and QueryPerformanceFrequency on Linux.
Can somebody verify they work as advertised, correctness?
It works on my machines.....type of code....(Raspberries and WIN64/LIN64
Code: Pascal  [Select][+][-]
  1. {$ifdef unix}
  2. function QueryPerformanceFrequency(out value:int64):boolean;inline;
  3. begin
  4.   Result := true;
  5.   Value := 1000000000;
  6. end;
  7.  
  8. function QueryPerformanceCounter(out value:int64):int64;inline;
  9. var
  10.   t:Timespec;
  11. begin  
  12.   result:=clock_gettime(CLOCK_THREAD_CPUTIME_ID{or CLOCK_MONOTONIC?},@t);
  13.   Value :=t.tv_nsec;
  14. end;
  15. {$endif}
Also, what kind of clock should I use? See code.
I needed it to make am_profiler.pas from my previous post cross-platform.
I realize not all cpu's have a clock, so I need to add an exception(or three).
« Last Edit: August 10, 2024, 09:43:06 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Thaddy

  • Hero Member
  • *****
  • Posts: 15646
  • Censorship about opinions does not belong here.
Re: [SOLVED]Queryperformance for linux: check for correctness please.
« Reply #1 on: August 10, 2024, 09:52:29 am »
I settled for:
Code: Pascal  [Select][+][-]
  1. {$ifdef unix}
  2. function QueryPerformanceFrequency(out value:int64):boolean;inline;
  3. begin
  4.   { On Linux always succeeds, just pass nanoseconds as resolution }
  5.   Result := true;
  6.   Value := 1000000000;
  7. end;
  8.  
  9. function QueryPerformanceCounter(out value:int64):int64;inline;
  10. var
  11.   t:Timespec;
  12. begin
  13.   { CLOCK_MONOTONIC is available on more systems than CLOCK_THREAD_CPUTIME_ID}
  14.   result:=clock_gettime(CLOCK_MONOTONIC{or CLOCK_THREAD_CPUTIME_ID},@t);
  15.   { assume call succeeds }
  16.   value := t.tv_nsec;
  17.   if result = -1 then
  18.   begin
  19.     { mimic windows error behavior, 0=error }
  20.     result :=0;
  21.     Value := 0
  22.   end;
  23. end;
  24. {$endif}
Saves me a whole lot of ifdefs.
« Last Edit: August 10, 2024, 09:57:18 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

 

TinyPortal © 2005-2018