public static JobObject Enable(this JobObject job, IsolationLimits limits, Action <string, object> onSet = null) { if (job == null) { throw new ArgumentNullException(nameof(job)); } if (limits == null) { throw new ArgumentNullException(nameof(limits)); } job.Update(o => { if (limits.MaxMemory > 0) { o.ProcessMemoryLimit = limits.MaxMemory; onSet?.Invoke(nameof(IsolationLimits.MaxMemory), limits.MaxMemory); } if (limits.MaxCpuUsage > 0) { o.CpuRateLimit = limits.MaxCpuUsage; onSet?.Invoke(nameof(IsolationLimits.MaxCpuUsage), limits.MaxCpuUsage); } if (limits.AffinityMask != IntPtr.Zero) { o.ProcessorAffinity = limits.AffinityMask; onSet?.Invoke(nameof(IsolationLimits.AffinityMask), limits.AffinityMask); } }); return(job); }
private async Task <int> Run(HostCommandContext context, string pipeName, long maxMemory, int affinityMask, int maxCpu) { var currentProcess = SDProcess.GetCurrentProcess(); var logger = context.ServiceProvider.GetService <ILoggerFactory>().CreateLogger <HostProcessImpl>(); logger.HostProcessStart(currentProcess.Id); string name = "pihost." + currentProcess.Id; var limits = new IsolationLimits(maxMemory, maxCpu, affinityMask); if (limits.IsAnyEnabled) { s_jobObject = new JobObject(name); s_jobObject.AddProcess(currentProcess); s_jobObject.Enable(limits, (l, v) => logger.ProcessLimitSet(l, v)); } var host = new IpcServiceHostBuilder(context.ServiceProvider) .AddNamedPipeEndpoint <IHostProcess>(name, pipeName, includeFailureDetailsInResponse: true) .Build(); logger.IpcServiceHostStarting(pipeName); context.SetStartComplete(); await host.RunAsync(context.Token).ConfigureAwait(false); return(0); }