lowleveldesign / process-governor

This application allows you to put various limits on Windows processes.
MIT License
626 stars 62 forks source link

New option -p only works for the first time #56

Closed chk-mytoys closed 1 year ago

chk-mytoys commented 1 year ago

For the new -p options, it looks like adding additional processes as an "update" does not work.

What works: start procgov with -p and attach a job object to already running processes.

What not works: When starting procgov again, to a add an additional, newly started process to the job, the newly started process is not added to the job object.

lowleveldesign commented 1 year ago

Could you specify the commands you're running. I just checked and the following worked:

# started winver.exe process (PID: 3164)
# apply limit to it
procgov64 --cpu 1 -p 3164
# started nother winver.exe process (PID: 5448)
# applied the same limit as in 3164
procgov64 --cpu 1 -p 3164,5448

Both processes had the same job ID and the same CPU affinity.

chk-mytoys commented 1 year ago

procgov64.exe --pid (($ServicePID -ne 0) -join ",") --maxjobmem 440G >> c:\tools\progovout.txt ... where $ServicePID contains die list of process IDs. E.g.: 3652,3520,14824

This targets processes running as services, procgov is therefore also started with system privileges / as system service.

lowleveldesign commented 1 year ago

I'm not sure if I understand what you want to achieve. The command you pasted would add all services processes to one job object. Did you check in Process Hacker or Process Explorer that they have the limit applied? If you also want to include child processes to the same job object use the -r option:

procgov64.exe -r --pid (($ServicePID -ne 0) -join ",") --maxjobmem 440G >> c:\tools\progovout.txt
chk-mytoys commented 1 year ago

I want to limit the processes to a combined limit of 440GB memory through one job object. This works then started for the first time with all processes already running.

But then starting an addition process or restarting one of the processes, they are subsequently not added to the job object and thereby not limited.

Expected or desired behaviour would be: the processes that are currently not yet part of the job object, get added to the existing one.

Membership of the job object is verified using ProcessExplorer.

lowleveldesign commented 1 year ago

If this newly started process has no relation to the other monitored process, it won't be added - procgov does not monitor all processes in the system. If this new process is a child process of one of the selected process then the -r option will fix your problem.

chk-mytoys commented 1 year ago

They are not child processes, but procgov is newly started with the list of the existsing and new processes.

So it is not expected that a running procgov instance is automatically picking up the process.

lowleveldesign commented 1 year ago

No, procgov will monitor only the selected processes + their children (if you decide to). If you need to add all newly started processes to the same job object, you will need some other tool to run procgov whenever a new process is created. For this scenario consider using the --nowait option so procgov won't hang.

chk-mytoys commented 1 year ago

No, that part is clear. I am NOT expecting procgov to pickup the newly started process. I am detecting that there are new processes and I restart procgov. But the new processes are not added to the job obhect.

lowleveldesign commented 1 year ago

OK, back to the beginning. Let's assume you have processes with PIDs 1,2,3 running and you launch procgov:

procgov --pid 1,2,3 --maxjobmem 40G

All those processes are now in the same job object. Then new process is created (PID: 4) so you stop procgov and launch it again, adding the new PID:

procgov --pid 1,2,3,4 --maxjobmem 40G

And this new PID 4 is not added to the job object. Is that what happens? Can you show me the output of procgov with -v option set.

chk-mytoys commented 1 year ago

The output is:

[procgov] Verbose: 0 : Procgov job already exists ('procgov-3c0fc8a9-709c-4fd1-bbcb-f04cb751e9db') for process 3652 and we will use it for other processes. [procgov] Verbose: 0 : Process 3520 already assigned to job 'procgov-3c0fc8a9-709c-4fd1-bbcb-f04cb751e9db'. [procgov] Verbose: 0 : Assigning process 14824 to job 'procgov-3c0fc8a9-709c-4fd1-bbcb-f04cb751e9db'

But process 14824 is not indeed added (verified using ProcessExplorer).

image
lowleveldesign commented 1 year ago

Maybe it's something specific about those processes. What are they? I'm trying with winver.exe and it works properly:

[procgov] Verbose: 0 : Procgov job already exists ('procgov-3df740af-5ac4-44a4-bc23-7a182fa33c4a') for process 3592 and we will use it for other processes.
[procgov] Verbose: 0 : Process 4048 already assigned to job 'procgov-3df740af-5ac4-44a4-bc23-7a182fa33c4a'.
[procgov] Verbose: 0 : Assigning process 264 to job 'procgov-3df740af-5ac4-44a4-bc23-7a182fa33c4a'

image

lowleveldesign commented 1 year ago

Maybe you could share a procmon trace of these launches?

chk-mytoys commented 1 year ago

The "unusual" part maybe, that these are running as services using a domain account, and procgov is also started as system service using "Local System" permissions. But during the first run, this is no problem and all processes are added:

[procgov] Verbose: 0 : All processes will be assigned to a newly created job (procgov-be213ae5-c3ae-4486-b905-64ab9de7e24d). [procgov] Verbose: 0 : Assigning process 3676 to job 'procgov-be213ae5-c3ae-4486-b905-64ab9de7e24d' [procgov] Verbose: 0 : Assigning process 3580 to job 'procgov-be213ae5-c3ae-4486-b905-64ab9de7e24d' [procgov] Verbose: 0 : Assigning process 3384 to job 'procgov-be213ae5-c3ae-4486-b905-64ab9de7e24d' [procgov] Verbose: 0 : Assigning process 3652 to job 'procgov-be213ae5-c3ae-4486-b905-64ab9de7e24d'

image
lowleveldesign commented 1 year ago

Unfortunately, I'm not able to reproduce this behavior. Please share the procmon trace file. You may leave only the profiling events as I only need the process details.

chk-mytoys commented 1 year ago

I'll have to check how to proceed with the procmon traces. But by running it in a console, instead of a service, I can see that the error that gets thrown is: ERROR: Access is denied. (0x80004005)

PS C:\Tools\procgov-new> .\procgov64.exe --pid 4288,3368,9676,9556 -v --nowait --maxjobmem 440G Process Governor v2.12.2363.8 - sets limits on processes Copyright (C) 2023 Sebastian Solnica (lowleveldesign.org)

Maximum job committed memory (MB): 450.560

[procgov] Verbose: 0 : Procgov job already exists ('procgov-7c86d04f-af0e-4520-a59c-5e81977bf7e4') for process 4288 and we will use it for other processes. [procgov] Verbose: 0 : Process 3368 already assigned to job 'procgov-7c86d04f-af0e-4520-a59c-5e81977bf7e4'. [procgov] Verbose: 0 : Assigning process 9676 to job 'procgov-7c86d04f-af0e-4520-a59c-5e81977bf7e4' ERROR: Access is denied. (0x80004005)

lowleveldesign commented 1 year ago

Regarding procmon, unselect all except the profiling button: image Reproduce the problem, save the pml file, zip it and upload to the issue (or send it to sebastian at wtrace.net).

lowleveldesign commented 1 year ago

Closing due to inactivity.