Recent

Author Topic: xPlatform System Info  (Read 5026 times)

Tony Stone

  • Full Member
  • ***
  • Posts: 219
xPlatform System Info
« on: July 21, 2021, 01:40:28 am »
I am trying to get system info for my program.  Mainly I would like to know when the system is near idle so my program can do some behind the scenes work and not effect the user.  I also would like to have my programs memory usage and CPU usage recorded.  I found https://www.mitec.cz/msics.html which seems to be for Windows only.  I would really like to be able to get this information easily from all platforms my program may run on.... Apple, Windows, Linux etc.

So before I get crazy and try to write my own components, do you guys know of any exsisting cross platform components?  I think for linux i could parse information from htop... windows seems to have a pretty extensive API for this info... Apple, well I have no idea.

I AM VERY inexperienced with Pascal and Lazarus... so for my first question...

1. Is writing a component that can be reused to gather all this information something that is gonna be so complex that a beginner should not waste time with it?  Windows seems semi easy...

2.  Before i go too far, is this something that is already available?

3.  I have noticed in code samples what I believe are called compiler directives?  Where it looks like {ifdef win32 ... }
I did some searching on those comments and I am not finding any great simple info for beginners.  Are they called compiler directives?
Just found my answer to this one... i am reading through https://wiki.freepascal.org/Multiplatform_Programming_Guide
not sure how i missed it before

I guess those are my questions for now... I may have more depending on those answers.

« Last Edit: July 21, 2021, 01:44:11 am by Tony Stone »

dseligo

  • Hero Member
  • *****
  • Posts: 1221
Re: xPlatform System Info
« Reply #1 on: July 21, 2021, 09:38:28 am »
I can't help you with first two questions.

3.  I have noticed in code samples what I believe are called compiler directives?  Where it looks like {ifdef win32 ... }
I did some searching on those comments and I am not finding any great simple info for beginners.  Are they called compiler directives?

This are compiles directives: https://www.freepascal.org/docs-html/prog/progch1.html

You are looking at conditional compilation (https://www.freepascal.org/docs-html/prog/progsu31.html#x38-370001.2.31).
This are 'defines' that you could use: https://www.freepascal.org/docs-html/prog/progap7.html#x340-356000G

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: xPlatform System Info
« Reply #2 on: July 21, 2021, 11:21:08 am »
I am trying to get system info for my program.  Mainly I would like to know when the system is near idle so my program can do some behind the scenes work and not effect the user.  I also would like to have my programs memory usage and CPU usage recorded.  I found https://www.mitec.cz/msics.html which seems to be for Windows only.  I would really like to be able to get this information easily from all platforms my program may run on.... Apple, Windows, Linux etc.

You are likely to find that this is very much platform-specific, i.e. it will be very different for Windows, Mac, Linux and the BSD family (not to mention e.g. SunOS/Solaris).

You might find that putting it into a component for GUI-driven programming is more trouble than it's worth, and that (in the case of Linux) consulting various things in /proc and /sys and returning the interpreted result using a function is more appropriate. For example, the load average returned by the top and uptime commands is derived from /proc/loadavg.

In the context of Lazarus/LCL, you will find the idletimer worth looking at since it's a good way of grabbing a bit of CPU time when your program isn't doing anything substantial at the GUI level. However if you're hanging e.g. a complicated bit of iterative maths onto that you'll need to make sure that you return on a regular basis since otherwise the program's GUI will freeze... I'm not the best person to advise on that.

You might find that a derivative of an idletimer which only triggers once the system load is less than some defined value is useful. But that would obviously push the system load (which on Linux is a rolling average) up, so the next time you checked it would obviously have been affected by what you've just done.

Quote
3.  I have noticed in code samples what I believe are called compiler directives?

I suggest saving these two links which are good documentation roots:

https://www.freepascal.org/docs.html

https://lazarus-ccr.sourceforge.io/docs/

The compiler directives are described in depth in the Programmer's Guide (via the first of those), and that's the definitive info as distinct from the wiki which is "best effort" and possibly outdated.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Kays

  • Hero Member
  • *****
  • Posts: 575
  • Whasup!?
    • KaiBurghardt.de
Re: xPlatform System Info
« Reply #3 on: July 21, 2021, 04:50:11 pm »
[…] Mainly I would like to know when the system is near idle so my program can do some behind the scenes work and not effect the user. […]
This is rather a task of an operating system, of the scheduler, to balance all demands evenly so the system remains operational/responsive enough. You can outsource your housekeeping tasks to a separate process and then renice/prioritize it lowest. Or, maybe write it as a separate program and install a cron job for 4:00 in the morning. If things can’t be postponed, use cgroups and restrict resource utilization. There are many possibilities. The best is of course to find a better, more efficient algorithm.

But maybe you’re simply caring too much: In general a 100 % load is good. You do want to use your available resources as much as possible. Less than 100 % means something had to wait for something else (e. g. disk spinning up, receiving network packages) and waiting is bad.
Yours Sincerely
Kai Burghardt

Tony Stone

  • Full Member
  • ***
  • Posts: 219
Re: xPlatform System Info
« Reply #4 on: July 21, 2021, 10:41:29 pm »
I did find this old and seemingly abandoned project last night. https://github.com/hyperic/sigar  It is written in C.  It just seems to me like a cross platform library or component that wraps up all these bits of information to to a helper type component would be a worthy project for Lazarus and FPC...  I suppose at this point I will focus on some specific items of interest to me for Linux, Windows and Mac... Maybe I can just start it all in a seperate .pas file and have it full of various functions rather than trying to start a component.  I kind of need some of this functionality for a stupid program i am putting together but more than anything I am hoping to put something of actual quality together with actual usefulness to possibly contribute to Lazarus and FPC.  Which is why I thought a component would be a cool way to go.  Then try and get it included in the Online Package Manager.  Either way... my skill is probably not at a level yet where other people should attempt to rely on my code. lol!  I will play with this idea over the next few months and see what other feedback I get here.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: xPlatform System Info
« Reply #5 on: July 21, 2021, 10:52:57 pm »
Maybe I can just start it all in a seperate .pas file and have it full of various functions rather than trying to start a component.

Word of warning there: "component" generally means something that can be dropped onto a Lazarus GUI project. If in doubt you /definitely/ want to focus on plain code (and demo/test cases) first.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Tony Stone

  • Full Member
  • ***
  • Posts: 219
Re: xPlatform System Info
« Reply #6 on: July 22, 2021, 02:07:14 am »
Maybe I can just start it all in a seperate .pas file and have it full of various functions rather than trying to start a component.

Word of warning there: "component" generally means something that can be dropped onto a Lazarus GUI project. If in doubt you /definitely/ want to focus on plain code (and demo/test cases) first.

MarkMLl

Noted.  Thanks... I am probably using some terms quite loosely.  But I am imagining a non-visual component...  but i suppose that would be useless with a non-GUI program....  A basic .pas file is how I will start.  Actually already started...

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: xPlatform System Info
« Reply #7 on: July 22, 2021, 09:59:22 am »
Noted.  Thanks... I am probably using some terms quite loosely.  But I am imagining a non-visual component...  but i suppose that would be useless with a non-GUI program....  A basic .pas file is how I will start.  Actually already started...

That's right. So you've got functions and procedures in a unit, with the declarations in both the implementation and interface parts so that other units can use them.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018