Recent

Author Topic: Hi, Any win32/win10 knowledge(e) that can maybe assist me with how to get the me  (Read 1832 times)

d-_-b

  • New Member
  • *
  • Posts: 24
I already have a working example where I use

Code: Pascal  [Select]
  1. Snapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
(gitlab code)

to get a list of handles of parent processes on my system and that correlates with the task manager process count.

And now I loop over that list and use 

Code: Pascal  [Select]
  1. GetProcessWorkingSetSize(HProcess, minsize, maxsize)
(gitlab code)

With the handle to retrieve `maxsize` and the size it returns is correct… but for one process. And applications like Brave has a process/handle/thread(???) per tab.

**And I’m not sure how to inspect those processes memory usage.**

My ultimate goal is to reply to this month old /r/ post

ASK /r/cmd: command line application to view memory usage like windirstat?


with fptuitreemap but without actually getting the children memory usage the app is useless on windows.

It works ok on macos and linux with `ps` and now even added google charts support with the sweet hack from /u/anomalous_cowherd.

Because the tui treemap pascal draw function also needs attention...

If you want to push pascal on a more social platform I also asked my question on /r/pascal.

Later,
Code: Pascal  [Select]
  1. mov     ax,0013h
  2. int     10h
Denthor thanks for the vga programming tutorials | Download all tutorials

440bx

  • Hero Member
  • *****
  • Posts: 1132
**And I’m not sure how to inspect those processes memory usage.**
It sounds like what you want is GetProcessMemoryInfo https://docs.microsoft.com/en-us/windows/desktop/api/psapi/nf-psapi-getprocessmemoryinfo
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

d-_-b

  • New Member
  • *
  • Posts: 24
Thanks for the info @440bx.

I do believe it is the call I'm trying to make here but it is failing for me:

Code: Pascal  [Select]
  1. if GetProcessMemoryInfo(PID, pmc, SizeOf(pmc)) then
gitlab code

and as mentioned I do get PID set size:

Code: Pascal  [Select]
  1. if GetProcessWorkingSetSize(HProcess, minsize, maxsize) then
gitlab code

Maybe I'm just calling the c++ function incorrectly?

I added the two diff calls behind a --rss flag:

Quote
C:\prj\pascal\tui>fptuitreemap.exe --help
C:\prj\pascal\tui\fptuitreemap.exe version 190519

-h, --help: This help.
-v, --version: Version.
-f, --file <file>: File to use to read ps listing from. default to ps.tmp if avail.
-n, --interval <ms>: Redraw every <ms> milliseconds.
-o, --html <filename>: Creates a Google Chart Treemap HTML file.
--rss <0,1>: [Windows only] We can read rss info of PID from either 0: MemoryInfo or 1: SetSize. Default to 1.
--readln. Wait after draw for <enter>.

EDIT: I found my problem I forgot to open the PID for my MemoryInfo call!

Code: Pascal  [Select]
  1. HProcess := OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, False, PID);
gitlab code

« Last Edit: May 26, 2019, 12:36:54 pm by d-_-b »
Code: Pascal  [Select]
  1. mov     ax,0013h
  2. int     10h
Denthor thanks for the vga programming tutorials | Download all tutorials

440bx

  • Hero Member
  • *****
  • Posts: 1132
I'm pleased the function I mentioned turned out to be what you were looking for.  Now in what you said....

Code: Pascal  [Select]
  1. if GetProcessWorkingSetSize(HProcess, minsize, maxsize) then
EDIT: I found my problem I forgot to open the PID for my MemoryInfo call!

Code: Pascal  [Select]
  1. HProcess := OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, False, PID);
gitlab code
...obviously I haven't looked at all of your code but, if you had added the call to GetProcessMemoryInfo immediately after the call to GetProcessWorkingSetSize then the HPROCESS handle you used for that call should be usable for the additional call.

Basically, if you're calling OpenProcess twice, you should probably restructure the code so it is called only once.

HTH.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

d-_-b

  • New Member
  • *
  • Posts: 24
I haven't looked at all of your code but, if you had added the call to GetProcessMemoryInfo immediately after the call to GetProcessWorkingSetSize then the HPROCESS handle you used for that call should be usable for the additional call.

Yea my code was a bit foobar'ed. But the only function I want to use is `GetProcessMemoryInfo`. `GetProcessWorkingSetSize` was just a function I found on a forum and tried to make it work.

Atm because im only interested in `GetProcessMemoryInfo` im not going to restructure my code. But as soon as I'm going to query more information of the HPROCESS handle I will make sure I open handle, query with diff func(s), close handle.

Do you maybe have any idea how "expensive" it is to open a handle on windows?

Because I'm wondering at what resolution I must take snapshot of my memory usage to see it change over time.(query memory usage every how many seconds?)

Thanks for your input, it really helped.
Code: Pascal  [Select]
  1. mov     ax,0013h
  2. int     10h
Denthor thanks for the vga programming tutorials | Download all tutorials

440bx

  • Hero Member
  • *****
  • Posts: 1132
Do you maybe have any idea how "expensive" it is to open a handle on windows?

Because I'm wondering at what resolution I must take snapshot of my memory usage to see it change over time.(query memory usage every how many seconds?)

Thanks for your input, it really helped.
You're welcome, I'm please you found it helpful.

As far as how "expensive" it is to open a handle, I haven't tested the performance of OpenProcess and OpenThread but, going by what some utilities such as ProcessHacker and ProcessExplorer do - which is refresh process information at specific intervals of time - I am led to believe it isn't a very expensive operation.

A simple way to test it would be to use toolhelp to get a list of process ids and, open a handle for every id in a loop about 1000 times, that should give an idea.  (remember to close the process handles... <chuckle>)

The default in ProcessHacker is to refresh every second. That seems to work reasonably well, it doesn't consume an inordinate amount of CPU.  ProcessExplorer defaults to the same value and doesn't use much CPU either.

HTH.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

trev

  • Full Member
  • ***
  • Posts: 214
  • Former Delphi 7 and Delphi 10.2 User
Talking about periodical refresh, I'd make it a user configurable period to take account of machine and/or OS speed.
o Lazarus v2.1.0 r61775, FPC v3.3.1 r42640, macOS 10.14.6 (with sup update), Xcode 10.3
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.0 (Parallels VM)
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 18.04 (Parallels VM)