diff --git a/README.md b/README.md index 5f58a83..0ed61d4 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,11 @@ This application allows you to set constraints on Windows processes. It uses [a - [Installation](#installation) -- [Setting procgov limits on a process](#setting-procgov-limits-on-a-process) -- [Setting procgov limits on multiple processes](#setting-procgov-limits-on-multiple-processes) -- [Update already applied limits](#update-already-applied-limits) +- [Understanding procgov run modes](#understanding-procgov-run-modes) +- [Applying limits on processes](#applying-limits-on-processes) + - [Setting limits on a single process](#setting-limits-on-a-single-process) + - [Setting limits on multiple processes](#setting-limits-on-multiple-processes) + - [Updating already applied limits](#updating-already-applied-limits) - [Available process constraints](#available-process-constraints) - [Limit memory of a process](#limit-memory-of-a-process) - [Limit CPU usage of a process \(CPU affinity\)](#limit-cpu-usage-of-a-process-cpu-affinity) @@ -38,9 +40,15 @@ choco install procgov winget install procgov ``` -## Setting procgov limits on a process +## Understanding procgov run modes -You may set limits on a newly created process or on an already running one. To **attach to a process** use the **-p|--pid** switch, eg. +FIXME: cmd app, service, monitor + +## Applying limits on processes + +### Setting limits on a single process + +You may set limits on a newly created process or on an already running one. To **constraint a running process** use the **-p|--pid** switch, eg. ```shell procgov.exe --maxmem 40M --pid 1234 @@ -54,9 +62,9 @@ procgov.exe -m 100M -- test.exe -arg1 -arg2=val2 arg3 Finally, you may **run procgov always when a given process starts**. When you use the **--install** switch Process Governor will add a special key to the **Image File Execution Options** in the registry, so that it will always start before your chosen process. To install Process Governor for a test.exe process, use the following command: `procgov64 --install --maxmem 40M test.exe`. You may later remove this installation by using the **--uninstall** switch, eg. `procgov64 --uninstall test.exe`. Be careful with this option as it may break some applications, especially those that spawn child processes with the same executable as the parent, for example, chrome.exe or msedge.exe. -## Setting procgov limits on multiple processes +### Setting limits on multiple processes -Starting from version 2.12 it is possible to assign multiple processes to the same job object. When you provide more than one process ID to the **-p** parameter, procgov will apply the same limits for all the processes, for example: +You may assign multiple processes to the same job object. When you provide more than one process ID to the **-p** parameter, procgov will apply the same limits for all the processes, for example: ```shell procgov.exe --maxmem 100M --pid 1234,1235,1236 @@ -64,9 +72,9 @@ procgov.exe --maxmem 100M --pid 1234,1235,1236 If any of the processes was already assigned to a procgov job object, others will be assigned to it as well. -## Update already applied limits +### Updating already applied limits -Starting from version 2.8, it is possible to **update once set limits**. Simply run procgov providing new limits and the target process ID(s). Procgov will update only the specified limits. Let's have a look at an example to understand this behavior better: +It is also possible to **update once set limits**. However, there is one requirement: the processes can't be assigned to different procgov jobs (so they must be either in the same job or unassigned). To update the limits, simply run procgov providing new limits and the target process ID(s). Procgov will update only the specified limits. Let's have a look at an example to understand this behavior better: ```shell We set a CPU limit on a process 1234 diff --git a/procgov-tests/Code/ProgramTests_ParseArgs.cs b/procgov-tests/Code/ProgramTests_ParseArgs.cs index a96c47c..40dba8c 100644 --- a/procgov-tests/Code/ProgramTests_ParseArgs.cs +++ b/procgov-tests/Code/ProgramTests_ParseArgs.cs @@ -222,7 +222,6 @@ public static void ParseArgsExecutionMode() switch (Program.ParseArgs(RealSystemInfo, ["--uninstall-all"])) { case RemoveAllProcessGovernance: - Assert.Pass(); break; default: Assert.Fail(); @@ -262,7 +261,6 @@ public static void ParseArgsAffinityMaskFromCpuCount() switch (Program.ParseArgs(TestSystemInfo2Numas4Groups, ["test.exe"])) { case RunAsCmdApp { JobSettings.CpuAffinity: null }: - Assert.Pass(); break; default: Assert.Fail(); diff --git a/procgov/Program.cs b/procgov/Program.cs index 85ac946..34956c3 100644 --- a/procgov/Program.cs +++ b/procgov/Program.cs @@ -448,22 +448,19 @@ GroupAffinity[] CalculateAffinityMaskFromCpuCount(int cpuCount) } List jobCpuAffinity = []; - for (int nidx = 0; nidx < systemInfo.NumaNodes.Length && cpuCount > 0; nidx++) + for (int i = 0; i < systemInfo.ProcessorGroups.Length && cpuCount > 0; i++) { - for (int gidx = 0; gidx < systemInfo.NumaNodes[nidx].ProcessorGroups.Length && cpuCount > 0; gidx++) + var group = systemInfo.ProcessorGroups[i]; + int coresNumber = BitOperations.PopCount(group.AffinityMask); + if (cpuCount > coresNumber) { - var group = systemInfo.NumaNodes[nidx].ProcessorGroups[gidx]; - int coresNumber = BitOperations.PopCount(group.AffinityMask); - if (cpuCount > coresNumber) - { - jobCpuAffinity.Add(new(group.Number, group.AffinityMask)); - cpuCount -= coresNumber; - } - else - { - jobCpuAffinity.Add(new(group.Number, group.AffinityMask >> (coresNumber - cpuCount))); - cpuCount = 0; - } + jobCpuAffinity.Add(new(group.Number, group.AffinityMask)); + cpuCount -= coresNumber; + } + else + { + jobCpuAffinity.Add(new(group.Number, group.AffinityMask >> (coresNumber - cpuCount))); + cpuCount = 0; } } return [.. jobCpuAffinity];