windows-development-sandbox 1.9.0 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/config.example.json +2 -1
- package/dist/cli/commands/launch/launch.js +2 -0
- package/dist/cli/commands/vpn.js +1 -0
- package/dist/cli/commands/web.d.ts +5 -0
- package/dist/cli/commands/web.js +38 -0
- package/dist/core/config/defaults.js +1 -1
- package/dist/core/schema/config.d.ts +1 -0
- package/dist/core/schema/vpn.js +1 -1
- package/dist/core/schema/web.d.ts +1 -0
- package/dist/core/schema/web.js +4 -1
- package/dist/guest/boot/boot.js +3 -1
- package/dist/guest/governor/decide.d.ts +11 -1
- package/dist/guest/governor/decide.js +30 -1
- package/dist/guest/governor/governor.d.ts +3 -0
- package/dist/guest/governor/governor.js +18 -2
- package/dist/guest/network/firewall.d.ts +1 -0
- package/dist/guest/network/firewall.js +65 -23
- package/dist/guest/runtime/process.js +53 -5
- package/dist/guest/setup/claude-mem.js +12 -4
- package/dist/guest/toolchains/languages/node.js +29 -3
- package/dist/host/life/host-memory.d.ts +1 -0
- package/dist/host/life/host-memory.js +4 -1
- package/dist/host/life/watchdog.d.ts +2 -0
- package/dist/host/life/watchdog.js +19 -3
- package/dist/runtime/agent.js +14 -10
- package/dist/runtime/daemon.js +861 -711
- package/dist/runtime/db.js +14 -10
- package/dist/runtime/main.js +159 -41
- package/dist/runtime/netproxy.js +4 -3
- package/dist/runtime/view.js +4 -3
- package/dist/selftest/guest-boot.test.js +47 -1
- package/dist/selftest/heavy-load.test.js +27 -1
- package/dist/selftest/network.test.js +20 -1
- package/dist/selftest/support/fixtures.d.ts +1 -0
- package/dist/shared/io/copy-tree.d.ts +7 -0
- package/dist/shared/io/copy-tree.js +45 -0
- package/dist/shared/io/index.d.ts +2 -0
- package/dist/shared/io/index.js +2 -0
- package/dist/shared/io/robocopy-result.d.ts +6 -0
- package/dist/shared/io/robocopy-result.js +23 -0
- package/dist/shared/memory/policy.d.ts +5 -0
- package/dist/shared/memory/policy.js +8 -0
- package/dist/shared/network/firewall-rules.d.ts +8 -0
- package/dist/shared/network/firewall-rules.js +28 -0
- package/native/csharp/HcsHost/AssemblyInfo.cs +3 -3
- package/native/csharp/HostTools/AssemblyInfo.cs +3 -3
- package/native/csharp/HostTools/MemoryStatus.cs +128 -107
- package/native/scripts/memory-status.ps1 +10 -2
- package/package.json +1 -1
- package/sandbox/csharp/MemGov.cs +466 -398
- package/sandbox/scripts/new-shortcuts.ps1 +3 -1
- package/schemas/config.schema.json +6 -2
package/sandbox/csharp/MemGov.cs
CHANGED
|
@@ -1,398 +1,466 @@
|
|
|
1
|
-
// memgov.exe: the Windows side of the sandbox's memory governor (src/guest/governor). One long-running process the
|
|
2
|
-
// daemon talks to over stdin/stdout, so the decisions stay in TypeScript and only the Win32 calls live here. C# 5.
|
|
3
|
-
//
|
|
4
|
-
// stdout, a JSON line every --interval seconds (default 2):
|
|
5
|
-
// {"ramTotalMb":..,"ramAvailMb":..,"commitLimitMb":..,"commitAvailMb":..,"procs":[{"pid":..,"name":"rustc","mb":..,"prio":"normal"}]}
|
|
6
|
-
// procs = the processes in the agents' job (the job ptyhost makes for everything the agents start), nothing else.
|
|
7
|
-
// stdin, one command per line:
|
|
8
|
-
// priority <pid> <idle|below|normal> lowers (or restores) the priority of a process in that job
|
|
9
|
-
// suspend <pid> <ms> pauses a process in that job, resumes it after ms (at most 30000)
|
|
10
|
-
// exit
|
|
11
|
-
//
|
|
12
|
-
// It only ever touches processes that are in the agents' job, never itself, and resumes everything it paused when it
|
|
13
|
-
// ends or when the daemon goes away (stdin closes).
|
|
14
|
-
using System;
|
|
15
|
-
using System.Collections.Generic;
|
|
16
|
-
using System.ComponentModel;
|
|
17
|
-
using System.Diagnostics;
|
|
18
|
-
using System.Globalization;
|
|
19
|
-
using System.IO;
|
|
20
|
-
using System.Runtime.InteropServices;
|
|
21
|
-
using System.Text;
|
|
22
|
-
using System.Threading;
|
|
23
|
-
|
|
24
|
-
static class MemGov
|
|
25
|
-
{
|
|
26
|
-
private const string AgentJobName = "Local\\AgentSandboxAgentsJob";
|
|
27
|
-
private const double BytesPerMb = 1024.0 * 1024.0;
|
|
28
|
-
private const int MaxProcesses = 1024;
|
|
29
|
-
private const int MaxSuspendMs = 30000;
|
|
30
|
-
private const uint JobObjectQuery = 0x0004;
|
|
31
|
-
private const int JobObjectBasicProcessIdList = 3;
|
|
32
|
-
private const uint ProcessQueryLimited = 0x1000;
|
|
33
|
-
private const uint ProcessSetInformation = 0x0200;
|
|
34
|
-
private const uint ProcessSuspendResume = 0x0800;
|
|
35
|
-
private const uint IdleClass = 0x00000040;
|
|
36
|
-
private const uint BelowNormalClass = 0x00004000;
|
|
37
|
-
private const uint NormalClass = 0x00000020;
|
|
38
|
-
|
|
39
|
-
// MEMORYSTATUSEX.
|
|
40
|
-
[StructLayout(LayoutKind.Sequential)]
|
|
41
|
-
private struct MemoryStatus
|
|
42
|
-
{
|
|
43
|
-
public uint Length;
|
|
44
|
-
public uint MemoryLoad;
|
|
45
|
-
public ulong TotalPhys;
|
|
46
|
-
public ulong AvailPhys;
|
|
47
|
-
public ulong TotalPageFile;
|
|
48
|
-
public ulong AvailPageFile;
|
|
49
|
-
public ulong TotalVirtual;
|
|
50
|
-
public ulong AvailVirtual;
|
|
51
|
-
public ulong AvailExtendedVirtual;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
private static class NativeMethods
|
|
55
|
-
{
|
|
56
|
-
[DllImport("kernel32.dll", SetLastError = true)]
|
|
57
|
-
public static extern bool GlobalMemoryStatusEx(ref MemoryStatus status);
|
|
58
|
-
|
|
59
|
-
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
|
60
|
-
public static extern IntPtr OpenJobObjectW(uint access, bool inherit, string name);
|
|
61
|
-
|
|
62
|
-
[DllImport("kernel32.dll", SetLastError = true)]
|
|
63
|
-
public static extern bool QueryInformationJobObject(IntPtr job, int infoClass, IntPtr info, int length, IntPtr returnLength);
|
|
64
|
-
|
|
65
|
-
[DllImport("kernel32.dll", SetLastError = true)]
|
|
66
|
-
public static extern bool IsProcessInJob(IntPtr process, IntPtr job, out bool result);
|
|
67
|
-
|
|
68
|
-
[DllImport("kernel32.dll", SetLastError = true)]
|
|
69
|
-
public static extern IntPtr OpenProcess(uint access, bool inherit, int pid);
|
|
70
|
-
|
|
71
|
-
[DllImport("kernel32.dll", SetLastError = true)]
|
|
72
|
-
public static extern bool SetPriorityClass(IntPtr process, uint priorityClass);
|
|
73
|
-
|
|
74
|
-
[DllImport("kernel32.dll", SetLastError = true)]
|
|
75
|
-
public static extern uint GetPriorityClass(IntPtr process);
|
|
76
|
-
|
|
77
|
-
[DllImport("kernel32.dll", SetLastError = true)]
|
|
78
|
-
public static extern bool CloseHandle(IntPtr handle);
|
|
79
|
-
|
|
80
|
-
[DllImport("ntdll.dll")]
|
|
81
|
-
public static extern int NtSuspendProcess(IntPtr process);
|
|
82
|
-
|
|
83
|
-
[DllImport("ntdll.dll")]
|
|
84
|
-
public static extern int NtResumeProcess(IntPtr process);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
private static readonly object Gate = new object();
|
|
88
|
-
private static readonly Dictionary<int, DateTime> Paused = new Dictionary<int, DateTime>();
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
static
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
{
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
sb
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
{
|
|
212
|
-
ids
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
return
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
{
|
|
236
|
-
NativeMethods.
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
private static
|
|
247
|
-
{
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
return;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
if (
|
|
311
|
-
{
|
|
312
|
-
return;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
{
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
foreach (int
|
|
353
|
-
{
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
1
|
+
// memgov.exe: the Windows side of the sandbox's memory governor (src/guest/governor). One long-running process the
|
|
2
|
+
// daemon talks to over stdin/stdout, so the decisions stay in TypeScript and only the Win32 calls live here. C# 5.
|
|
3
|
+
//
|
|
4
|
+
// stdout, a JSON line every --interval seconds (default 2):
|
|
5
|
+
// {"ramTotalMb":..,"ramAvailMb":..,"commitLimitMb":..,"commitAvailMb":..,"procs":[{"pid":..,"name":"rustc","mb":..,"prio":"normal"}]}
|
|
6
|
+
// procs = the processes in the agents' job (the job ptyhost makes for everything the agents start), nothing else.
|
|
7
|
+
// stdin, one command per line:
|
|
8
|
+
// priority <pid> <idle|below|normal> lowers (or restores) the priority of a process in that job
|
|
9
|
+
// suspend <pid> <ms> pauses a process in that job, resumes it after ms (at most 30000)
|
|
10
|
+
// exit
|
|
11
|
+
//
|
|
12
|
+
// It only ever touches processes that are in the agents' job, never itself, and resumes everything it paused when it
|
|
13
|
+
// ends or when the daemon goes away (stdin closes).
|
|
14
|
+
using System;
|
|
15
|
+
using System.Collections.Generic;
|
|
16
|
+
using System.ComponentModel;
|
|
17
|
+
using System.Diagnostics;
|
|
18
|
+
using System.Globalization;
|
|
19
|
+
using System.IO;
|
|
20
|
+
using System.Runtime.InteropServices;
|
|
21
|
+
using System.Text;
|
|
22
|
+
using System.Threading;
|
|
23
|
+
|
|
24
|
+
static class MemGov
|
|
25
|
+
{
|
|
26
|
+
private const string AgentJobName = "Local\\AgentSandboxAgentsJob";
|
|
27
|
+
private const double BytesPerMb = 1024.0 * 1024.0;
|
|
28
|
+
private const int MaxProcesses = 1024;
|
|
29
|
+
private const int MaxSuspendMs = 30000;
|
|
30
|
+
private const uint JobObjectQuery = 0x0004;
|
|
31
|
+
private const int JobObjectBasicProcessIdList = 3;
|
|
32
|
+
private const uint ProcessQueryLimited = 0x1000;
|
|
33
|
+
private const uint ProcessSetInformation = 0x0200;
|
|
34
|
+
private const uint ProcessSuspendResume = 0x0800;
|
|
35
|
+
private const uint IdleClass = 0x00000040;
|
|
36
|
+
private const uint BelowNormalClass = 0x00004000;
|
|
37
|
+
private const uint NormalClass = 0x00000020;
|
|
38
|
+
|
|
39
|
+
// MEMORYSTATUSEX.
|
|
40
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
41
|
+
private struct MemoryStatus
|
|
42
|
+
{
|
|
43
|
+
public uint Length;
|
|
44
|
+
public uint MemoryLoad;
|
|
45
|
+
public ulong TotalPhys;
|
|
46
|
+
public ulong AvailPhys;
|
|
47
|
+
public ulong TotalPageFile;
|
|
48
|
+
public ulong AvailPageFile;
|
|
49
|
+
public ulong TotalVirtual;
|
|
50
|
+
public ulong AvailVirtual;
|
|
51
|
+
public ulong AvailExtendedVirtual;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private static class NativeMethods
|
|
55
|
+
{
|
|
56
|
+
[DllImport("kernel32.dll", SetLastError = true)]
|
|
57
|
+
public static extern bool GlobalMemoryStatusEx(ref MemoryStatus status);
|
|
58
|
+
|
|
59
|
+
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
|
60
|
+
public static extern IntPtr OpenJobObjectW(uint access, bool inherit, string name);
|
|
61
|
+
|
|
62
|
+
[DllImport("kernel32.dll", SetLastError = true)]
|
|
63
|
+
public static extern bool QueryInformationJobObject(IntPtr job, int infoClass, IntPtr info, int length, IntPtr returnLength);
|
|
64
|
+
|
|
65
|
+
[DllImport("kernel32.dll", SetLastError = true)]
|
|
66
|
+
public static extern bool IsProcessInJob(IntPtr process, IntPtr job, out bool result);
|
|
67
|
+
|
|
68
|
+
[DllImport("kernel32.dll", SetLastError = true)]
|
|
69
|
+
public static extern IntPtr OpenProcess(uint access, bool inherit, int pid);
|
|
70
|
+
|
|
71
|
+
[DllImport("kernel32.dll", SetLastError = true)]
|
|
72
|
+
public static extern bool SetPriorityClass(IntPtr process, uint priorityClass);
|
|
73
|
+
|
|
74
|
+
[DllImport("kernel32.dll", SetLastError = true)]
|
|
75
|
+
public static extern uint GetPriorityClass(IntPtr process);
|
|
76
|
+
|
|
77
|
+
[DllImport("kernel32.dll", SetLastError = true)]
|
|
78
|
+
public static extern bool CloseHandle(IntPtr handle);
|
|
79
|
+
|
|
80
|
+
[DllImport("ntdll.dll")]
|
|
81
|
+
public static extern int NtSuspendProcess(IntPtr process);
|
|
82
|
+
|
|
83
|
+
[DllImport("ntdll.dll")]
|
|
84
|
+
public static extern int NtResumeProcess(IntPtr process);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private static readonly object Gate = new object();
|
|
88
|
+
private static readonly Dictionary<int, DateTime> Paused = new Dictionary<int, DateTime>();
|
|
89
|
+
|
|
90
|
+
/// <summary>The paused process ids on disk, so a memgov that is killed mid-pause is undone by the next one.</summary>
|
|
91
|
+
private static readonly string HeldFile = Path.Combine(Path.GetTempPath(), "agent-sandbox-memgov-held.txt");
|
|
92
|
+
private static IntPtr job = IntPtr.Zero;
|
|
93
|
+
private static volatile bool stop;
|
|
94
|
+
|
|
95
|
+
static int Main(string[] args)
|
|
96
|
+
{
|
|
97
|
+
if (args.Length == 0 || args[0] != "serve")
|
|
98
|
+
{
|
|
99
|
+
Console.Error.WriteLine("usage: memgov serve [--interval seconds]");
|
|
100
|
+
return 2;
|
|
101
|
+
}
|
|
102
|
+
int interval = 2;
|
|
103
|
+
for (int i = 1; i + 1 < args.Length; i++)
|
|
104
|
+
{
|
|
105
|
+
if (args[i] == "--interval")
|
|
106
|
+
{
|
|
107
|
+
int.TryParse(args[i + 1], NumberStyles.Integer, CultureInfo.InvariantCulture, out interval);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (interval < 1)
|
|
111
|
+
{
|
|
112
|
+
interval = 2;
|
|
113
|
+
}
|
|
114
|
+
// A memgov that ended without resuming (killed, crashed) left its pauses in the file: undo them first.
|
|
115
|
+
RecoverHeld();
|
|
116
|
+
Thread commands = new Thread(ReadCommands);
|
|
117
|
+
commands.IsBackground = true;
|
|
118
|
+
commands.Start();
|
|
119
|
+
Thread resumer = new Thread(ResumeWhenDue);
|
|
120
|
+
resumer.IsBackground = true;
|
|
121
|
+
resumer.Start();
|
|
122
|
+
while (!stop)
|
|
123
|
+
{
|
|
124
|
+
try
|
|
125
|
+
{
|
|
126
|
+
Console.Out.WriteLine(Sample());
|
|
127
|
+
Console.Out.Flush();
|
|
128
|
+
}
|
|
129
|
+
catch (Exception)
|
|
130
|
+
{
|
|
131
|
+
// A sample that fails (a process ended while it was read) is skipped; the next one tries again.
|
|
132
|
+
}
|
|
133
|
+
for (int waited = 0; waited < interval * 10 && !stop; waited++)
|
|
134
|
+
{
|
|
135
|
+
Thread.Sleep(100);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
ResumeAll();
|
|
139
|
+
return 0;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/// <summary>The job, opened once it exists (ptyhost makes it when the first agent starts).</summary>
|
|
143
|
+
private static IntPtr Job()
|
|
144
|
+
{
|
|
145
|
+
if (job == IntPtr.Zero)
|
|
146
|
+
{
|
|
147
|
+
job = NativeMethods.OpenJobObjectW(JobObjectQuery, false, AgentJobName);
|
|
148
|
+
}
|
|
149
|
+
return job;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
private static string Sample()
|
|
153
|
+
{
|
|
154
|
+
MemoryStatus status = new MemoryStatus();
|
|
155
|
+
status.Length = (uint)Marshal.SizeOf(typeof(MemoryStatus));
|
|
156
|
+
if (!NativeMethods.GlobalMemoryStatusEx(ref status))
|
|
157
|
+
{
|
|
158
|
+
throw new Win32Exception(Marshal.GetLastWin32Error());
|
|
159
|
+
}
|
|
160
|
+
StringBuilder sb = new StringBuilder();
|
|
161
|
+
sb.Append("{\"ramTotalMb\":").Append(Mb(status.TotalPhys));
|
|
162
|
+
sb.Append(",\"ramAvailMb\":").Append(Mb(status.AvailPhys));
|
|
163
|
+
sb.Append(",\"commitLimitMb\":").Append(Mb(status.TotalPageFile));
|
|
164
|
+
sb.Append(",\"commitAvailMb\":").Append(Mb(status.AvailPageFile));
|
|
165
|
+
sb.Append(",\"procs\":[");
|
|
166
|
+
bool first = true;
|
|
167
|
+
int self = Process.GetCurrentProcess().Id;
|
|
168
|
+
foreach (int pid in JobProcessIds())
|
|
169
|
+
{
|
|
170
|
+
if (pid == self)
|
|
171
|
+
{
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
try
|
|
175
|
+
{
|
|
176
|
+
using (Process p = Process.GetProcessById(pid))
|
|
177
|
+
{
|
|
178
|
+
if (!first)
|
|
179
|
+
{
|
|
180
|
+
sb.Append(',');
|
|
181
|
+
}
|
|
182
|
+
first = false;
|
|
183
|
+
sb.Append("{\"pid\":").Append(pid.ToString(CultureInfo.InvariantCulture));
|
|
184
|
+
sb.Append(",\"name\":").Append(Json(p.ProcessName));
|
|
185
|
+
sb.Append(",\"mb\":").Append(Mb((ulong)p.PrivateMemorySize64));
|
|
186
|
+
sb.Append(",\"prio\":\"").Append(PriorityName(pid)).Append("\"}");
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
catch (Exception)
|
|
190
|
+
{
|
|
191
|
+
// The process ended while it was read.
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return sb.Append("]}").ToString();
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private static List<int> JobProcessIds()
|
|
198
|
+
{
|
|
199
|
+
List<int> ids = new List<int>();
|
|
200
|
+
IntPtr handle = Job();
|
|
201
|
+
if (handle == IntPtr.Zero)
|
|
202
|
+
{
|
|
203
|
+
return ids;
|
|
204
|
+
}
|
|
205
|
+
// JOBOBJECT_BASIC_PROCESS_ID_LIST: two counts, then the ids (pointer-sized).
|
|
206
|
+
int length = 8 + IntPtr.Size * MaxProcesses;
|
|
207
|
+
IntPtr buffer = Marshal.AllocHGlobal(length);
|
|
208
|
+
try
|
|
209
|
+
{
|
|
210
|
+
if (!NativeMethods.QueryInformationJobObject(handle, JobObjectBasicProcessIdList, buffer, length, IntPtr.Zero))
|
|
211
|
+
{
|
|
212
|
+
return ids;
|
|
213
|
+
}
|
|
214
|
+
int count = Marshal.ReadInt32(buffer, 4);
|
|
215
|
+
for (int i = 0; i < count && i < MaxProcesses; i++)
|
|
216
|
+
{
|
|
217
|
+
ids.Add((int)Marshal.ReadIntPtr(buffer, 8 + i * IntPtr.Size).ToInt64());
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
finally
|
|
221
|
+
{
|
|
222
|
+
Marshal.FreeHGlobal(buffer);
|
|
223
|
+
}
|
|
224
|
+
return ids;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private static string PriorityName(int pid)
|
|
228
|
+
{
|
|
229
|
+
IntPtr process = NativeMethods.OpenProcess(ProcessQueryLimited, false, pid);
|
|
230
|
+
if (process == IntPtr.Zero)
|
|
231
|
+
{
|
|
232
|
+
return "normal";
|
|
233
|
+
}
|
|
234
|
+
try
|
|
235
|
+
{
|
|
236
|
+
uint c = NativeMethods.GetPriorityClass(process);
|
|
237
|
+
return c == IdleClass ? "idle" : c == BelowNormalClass ? "below" : "normal";
|
|
238
|
+
}
|
|
239
|
+
finally
|
|
240
|
+
{
|
|
241
|
+
NativeMethods.CloseHandle(process);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/// <summary>Whether pid is a process of the agents' job and not this one: the only ones this program touches.</summary>
|
|
246
|
+
private static bool Allowed(int pid)
|
|
247
|
+
{
|
|
248
|
+
return pid != Process.GetCurrentProcess().Id && JobProcessIds().Contains(pid);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
private static void ReadCommands()
|
|
252
|
+
{
|
|
253
|
+
string line;
|
|
254
|
+
while ((line = Console.In.ReadLine()) != null)
|
|
255
|
+
{
|
|
256
|
+
try
|
|
257
|
+
{
|
|
258
|
+
Run(line.Trim().Split(' '));
|
|
259
|
+
}
|
|
260
|
+
catch (Exception)
|
|
261
|
+
{
|
|
262
|
+
// A bad command is ignored.
|
|
263
|
+
}
|
|
264
|
+
if (stop)
|
|
265
|
+
{
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
// The daemon is gone: put everything back and end.
|
|
270
|
+
stop = true;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
private static void Run(string[] parts)
|
|
274
|
+
{
|
|
275
|
+
if (parts.Length == 1 && parts[0] == "exit")
|
|
276
|
+
{
|
|
277
|
+
stop = true;
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
int pid;
|
|
281
|
+
if (parts.Length < 3 || !int.TryParse(parts[1], NumberStyles.Integer, CultureInfo.InvariantCulture, out pid) || !Allowed(pid))
|
|
282
|
+
{
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
if (parts[0] == "priority")
|
|
286
|
+
{
|
|
287
|
+
uint cls = parts[2] == "idle" ? IdleClass : parts[2] == "below" ? BelowNormalClass : NormalClass;
|
|
288
|
+
IntPtr process = NativeMethods.OpenProcess(ProcessSetInformation, false, pid);
|
|
289
|
+
if (process != IntPtr.Zero)
|
|
290
|
+
{
|
|
291
|
+
NativeMethods.SetPriorityClass(process, cls);
|
|
292
|
+
NativeMethods.CloseHandle(process);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
else if (parts[0] == "suspend")
|
|
296
|
+
{
|
|
297
|
+
int ms;
|
|
298
|
+
if (!int.TryParse(parts[2], NumberStyles.Integer, CultureInfo.InvariantCulture, out ms) || ms < 1)
|
|
299
|
+
{
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
Suspend(pid, Math.Min(ms, MaxSuspendMs));
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
private static void Suspend(int pid, int ms)
|
|
307
|
+
{
|
|
308
|
+
lock (Gate)
|
|
309
|
+
{
|
|
310
|
+
if (Paused.ContainsKey(pid))
|
|
311
|
+
{
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
IntPtr process = NativeMethods.OpenProcess(ProcessSuspendResume, false, pid);
|
|
315
|
+
if (process == IntPtr.Zero)
|
|
316
|
+
{
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
try
|
|
320
|
+
{
|
|
321
|
+
// Recorded before the pause, so a memgov that dies right after still lets the next one resume it.
|
|
322
|
+
Paused[pid] = DateTime.UtcNow.AddMilliseconds(ms);
|
|
323
|
+
SaveHeld();
|
|
324
|
+
NativeMethods.NtSuspendProcess(process);
|
|
325
|
+
}
|
|
326
|
+
finally
|
|
327
|
+
{
|
|
328
|
+
NativeMethods.CloseHandle(process);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
private static void Resume(int pid)
|
|
334
|
+
{
|
|
335
|
+
IntPtr process = NativeMethods.OpenProcess(ProcessSuspendResume, false, pid);
|
|
336
|
+
if (process != IntPtr.Zero)
|
|
337
|
+
{
|
|
338
|
+
NativeMethods.NtResumeProcess(process);
|
|
339
|
+
NativeMethods.CloseHandle(process);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/// <summary>A paused process always comes back: when its time is up (checked every 100 ms).</summary>
|
|
344
|
+
private static void ResumeWhenDue()
|
|
345
|
+
{
|
|
346
|
+
while (true)
|
|
347
|
+
{
|
|
348
|
+
Thread.Sleep(100);
|
|
349
|
+
lock (Gate)
|
|
350
|
+
{
|
|
351
|
+
List<int> due = new List<int>();
|
|
352
|
+
foreach (KeyValuePair<int, DateTime> p in Paused)
|
|
353
|
+
{
|
|
354
|
+
if (p.Value <= DateTime.UtcNow)
|
|
355
|
+
{
|
|
356
|
+
due.Add(p.Key);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
foreach (int pid in due)
|
|
360
|
+
{
|
|
361
|
+
Resume(pid);
|
|
362
|
+
Paused.Remove(pid);
|
|
363
|
+
}
|
|
364
|
+
if (due.Count > 0)
|
|
365
|
+
{
|
|
366
|
+
SaveHeld();
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
private static void ResumeAll()
|
|
373
|
+
{
|
|
374
|
+
lock (Gate)
|
|
375
|
+
{
|
|
376
|
+
foreach (int pid in new List<int>(Paused.Keys))
|
|
377
|
+
{
|
|
378
|
+
Resume(pid);
|
|
379
|
+
}
|
|
380
|
+
Paused.Clear();
|
|
381
|
+
SaveHeld();
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/// <summary>The processes paused right now, one id per line (empty: the file goes). Called with Gate held.</summary>
|
|
386
|
+
private static void SaveHeld()
|
|
387
|
+
{
|
|
388
|
+
try
|
|
389
|
+
{
|
|
390
|
+
if (Paused.Count == 0)
|
|
391
|
+
{
|
|
392
|
+
File.Delete(HeldFile);
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
List<string> lines = new List<string>();
|
|
396
|
+
foreach (int pid in Paused.Keys)
|
|
397
|
+
{
|
|
398
|
+
lines.Add(pid.ToString(CultureInfo.InvariantCulture));
|
|
399
|
+
}
|
|
400
|
+
File.WriteAllLines(HeldFile, lines.ToArray());
|
|
401
|
+
}
|
|
402
|
+
catch (IOException)
|
|
403
|
+
{
|
|
404
|
+
// Best effort: the in-memory list still resumes every pause while this memgov runs.
|
|
405
|
+
}
|
|
406
|
+
catch (UnauthorizedAccessException)
|
|
407
|
+
{
|
|
408
|
+
// Same as above.
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/// <summary>Resumes what an earlier memgov paused and never resumed. Resuming a running process changes nothing.</summary>
|
|
413
|
+
private static void RecoverHeld()
|
|
414
|
+
{
|
|
415
|
+
try
|
|
416
|
+
{
|
|
417
|
+
if (!File.Exists(HeldFile))
|
|
418
|
+
{
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
foreach (string line in File.ReadAllLines(HeldFile))
|
|
422
|
+
{
|
|
423
|
+
int pid;
|
|
424
|
+
if (int.TryParse(line.Trim(), NumberStyles.Integer, CultureInfo.InvariantCulture, out pid) && pid > 0)
|
|
425
|
+
{
|
|
426
|
+
Resume(pid);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
File.Delete(HeldFile);
|
|
430
|
+
}
|
|
431
|
+
catch (IOException)
|
|
432
|
+
{
|
|
433
|
+
// Nothing more to do: the file stays and the next start tries again.
|
|
434
|
+
}
|
|
435
|
+
catch (UnauthorizedAccessException)
|
|
436
|
+
{
|
|
437
|
+
// Same as above.
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
private static string Mb(ulong bytes)
|
|
442
|
+
{
|
|
443
|
+
return Math.Floor(bytes / BytesPerMb).ToString("0", CultureInfo.InvariantCulture);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
private static string Json(string value)
|
|
447
|
+
{
|
|
448
|
+
StringBuilder sb = new StringBuilder("\"");
|
|
449
|
+
foreach (char c in value)
|
|
450
|
+
{
|
|
451
|
+
if (c == '"' || c == '\\')
|
|
452
|
+
{
|
|
453
|
+
sb.Append('\\').Append(c);
|
|
454
|
+
}
|
|
455
|
+
else if (c < 0x20)
|
|
456
|
+
{
|
|
457
|
+
sb.Append("\\u").Append(((int)c).ToString("x4", CultureInfo.InvariantCulture));
|
|
458
|
+
}
|
|
459
|
+
else
|
|
460
|
+
{
|
|
461
|
+
sb.Append(c);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
return sb.Append('"').ToString();
|
|
465
|
+
}
|
|
466
|
+
}
|