* * *

Author Topic: Another profiler for FreePascal?  (Read 1481 times)

Eugene Loza

  • Hero Member
  • *****
  • Posts: 561
    • My "almost daily" development blog
Another profiler for FreePascal?
« on: December 04, 2017, 01:34:14 pm »
Hi all!
Recently I've written a "very-simple" macro-based profiler for my game: https://github.com/eugeneloza/decoherence/blob/master/src/profiler.pas (FPC 3.1.1 required)
Usage example: https://github.com/eugeneloza/decoherence/blob/master/tests/profiler/ProfilerTest.pas
Example output: https://github.com/eugeneloza/decoherence/files/1502992/171125_profiler.txt
I wonder, is there any need in that sort of stuff?
I mean, at this point the profiler is heavily-dependent on other game files and Castle Game Engine and requires manual operation, but I could do some improvements to make it much more "universal" and create an automatic source parser and analysis modules to release it as a usable tool if anything like that might be useful to others...

P.S. And yes, don't blame me too much, I know my programming skills are far below average  :-[
« Last Edit: December 04, 2017, 01:36:20 pm by Eugene Loza »
Lazarus 1.9 + FPC 3.1.1 Debian Jessie 64 bit.

My Free and Open Source games in Lazarus/FreePascal/CastleGameEngine:
https://decoherence.itch.io/
(and some ancient games in Turbo Pascal too)
Sources are here: https://github.com/eugeneloza?tab=repositories

Graeme

  • Hero Member
  • *****
  • Posts: 1422
    • Graeme on the web
Re: Another profiler for FreePascal?
« Reply #1 on: December 10, 2017, 10:39:23 am »
Why not simply use FPProfiler - which seem to work in a similar way, but automaticalyl injects the profiling code into your units and automatically removes them afterwards. At least, that is how I remember FPProfiler working some years back.

   http://wiki.lazarus.freepascal.org/FPProfiler

The problem with such profiles though, is that they modify the source code to enable profiling, and thus also modify line number information. But indeed, they are much more lightweight than "real" profilers that monitor the debug information in your executables.
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

Eugene Loza

  • Hero Member
  • *****
  • Posts: 561
    • My "almost daily" development blog
Re: Another profiler for FreePascal?
« Reply #2 on: December 10, 2017, 10:49:21 am »
I've tried LazProfiler and it couldn't instrument my code correctly (actually destroyed it, but luckily I had a backup) :) That's why I had to make my own implementation. Which can be made independent of EpicTimer and other thrid-party units (yes, now it depends on CGE and my external units, e.g. for timer implementation, but this is a weak dependency and can be easily avoided) and be more "fail-safe".
The largest problem is parser for such profilers, e.g. even JEDI and CodeTools fail at some valid FPC syntax (especially when it comes to compiler directives and, especially, macro). Basically, that's the problem actually nearly unsolvable, so finally we'll have just another "poor" profiler that has poor instrumentation support.
Lazarus 1.9 + FPC 3.1.1 Debian Jessie 64 bit.

My Free and Open Source games in Lazarus/FreePascal/CastleGameEngine:
https://decoherence.itch.io/
(and some ancient games in Turbo Pascal too)
Sources are here: https://github.com/eugeneloza?tab=repositories

Graeme

  • Hero Member
  • *****
  • Posts: 1422
    • Graeme on the web
Re: Another profiler for FreePascal?
« Reply #3 on: December 10, 2017, 11:08:54 am »
I've tried LazProfiler and it couldn't instrument my code correctly (actually destroyed it, but luckily I had a backup) :)
That is worrying - don't you use SubVersion or Git to version control your software?? I highly suggest you start.

Quote
yes, now it depends on CGE and my external units, e.g. for timer implementation,
So the point is moot - both have dependencies.


Quote
The largest problem is parser for such profilers, e.g. even JEDI and CodeTools fail at some valid FPC syntax
You don't need a parser for that. Granted I don't know LazProfiler (is that a renamed FPProfiler?). Also I haven't followed FPProfiler in recent years. The last time I worked on FPProfile was some 7 years ago - my code is still available [https://github.com/graemeg/fpprofiler].

If you look at my code, you will see, no parser is required. You simply use a tokenizer to find begin..end pairs of functions or procedures and inject your profiling code after/before those tokens. It doesn't need to know anything about the syntax or logic of the language.

Like I said, maybe the later "official" FPProfiler now uses a Object Pascal parser, but the FPProfiler in my code repository (from 7 years ago) did not need that.
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

Thaddy

  • Hero Member
  • *****
  • Posts: 6130
Re: Another profiler for FreePascal?
« Reply #4 on: December 10, 2017, 11:20:59 am »
Has there been done any work on Eric's Sampling profiler? I happen to like that a lot for my Delphi code and it does not rely on instrumentation.
https://www.delphitools.info/samplingprofiler/
I might not give the answer that you want me to.. Peter Green 1969

Eugene Loza

  • Hero Member
  • *****
  • Posts: 561
    • My "almost daily" development blog
Re: Another profiler for FreePascal?
« Reply #5 on: December 10, 2017, 11:32:43 am »
As I've said before, I'm a relatively newbie to FPC and programming :) So while having an "extensive experience" I still don't know what do you mean by "to version control your software" :) I work on offline copy (not always Internet is available) and sync it by using Git, creating manual backups for every major change.
Quote
both have dependencies
That code depends only on timer and output (log) implementation. That's easily removable, just copy-paste a procedure.
As I've already said, it was not created for "release". I made it for my own purposes, but I asked about whether there is any need in that sort of thing (and I believe there is none, but just in case).
Quote
a tokenizer to find begin..end pairs of functions or procedures
Yes, that seems exactly what I've meant by parsing the code.
E.g. it will almost surely fail for these lines https://github.com/eugeneloza/decoherence/blob/master/src/decodungeongenerator.pas#L1621 (use of "include files") or here https://github.com/eugeneloza/decoherence/blob/014509f0119407165fd48ae28b0d6c3c6fb30db2/profiler/project1.lpr#L15 (use of "macro").
I can also "think of" some conditionals that will fail (in this example they look stupid, but sometimes, situations like that pop up in a more complex way, e.g. in case of cross-platform implementation):
Code: Pascal  [Select]
  1. procedure MyProcedure;
  2. {$IFDEF Repeat}
  3. var i: integer;
  4. begin
  5.   for i := 0 to 10000 do begin
  6. {$ELSE}
  7. begin
  8. {$END}
  9.      //do something
  10. {$IFDEF Repeat}
  11.   end;
  12. {$END}
  13. end;
Lazarus 1.9 + FPC 3.1.1 Debian Jessie 64 bit.

My Free and Open Source games in Lazarus/FreePascal/CastleGameEngine:
https://decoherence.itch.io/
(and some ancient games in Turbo Pascal too)
Sources are here: https://github.com/eugeneloza?tab=repositories

Pascal

  • Hero Member
  • *****
  • Posts: 664
Re: Another profiler for FreePascal?
« Reply #6 on: May 08, 2018, 02:08:41 pm »
I've tried LazProfiler and it couldn't instrument my code correctly (actually destroyed it, but luckily I had a backup) :)

I've only tested LazProfiler on Windows. So there is a big possibility that i've overlocked something with backup and renaming source files.
If you can give me any hints i can have a look at it. BTW i use TPascalScanner of fcl-passrc/src/pscanner.pp.
« Last Edit: May 08, 2018, 02:14:25 pm by Pascal »
laz trunk - fpc trunk 32bit - Windows 10 Pro x64 (1803)

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus