public void WhenManagingMultipleProcesses() { const long oneProcessPrivateMemory = 1024; TimeSpan expectedTotalKernelTime = TimeSpan.FromSeconds(2); TimeSpan expectedTotalUserTime = TimeSpan.FromSeconds(2); var expectedCpuStats = new CpuStatistics { TotalKernelTime = expectedTotalKernelTime, TotalUserTime = expectedTotalUserTime, }; var firstProcess = Substitute.For <IProcess>(); firstProcess.Id.Returns(1); firstProcess.PrivateMemoryBytes.Returns(oneProcessPrivateMemory); var secondProcess = Substitute.For <IProcess>(); secondProcess.Id.Returns(2); secondProcess.PrivateMemoryBytes.Returns(oneProcessPrivateMemory); JobObject.GetCpuStatistics().Returns(expectedCpuStats); JobObject.GetProcessIds().Returns(new int[] { 1, 2 }); ProcessHelper.GetProcesses(null).ReturnsForAnyArgs(new[] { firstProcess, secondProcess }); var metrics = Container.GetMetrics(); Assert.Equal(expectedTotalKernelTime + expectedTotalUserTime, metrics.CpuStat.TotalProcessorTime); Assert.Equal((ulong)firstProcess.PrivateMemoryBytes + (ulong)secondProcess.PrivateMemoryBytes, metrics.MemoryStat.PrivateBytes); }
private ContainerMemoryStat GetMemoryStat() { lock (_ioLock) { var processIds = jobObject.GetProcessIds(); var processes = processHelper.GetProcesses(processIds).ToList(); ulong privateMemory = 0; foreach (var process in processes) { try { privateMemory += (ulong)process.PrivateMemoryBytes; } catch (InvalidOperationException) { // skip the error, the process has exited } } return(new ContainerMemoryStat { PrivateBytes = privateMemory, }); } }
public IReadOnlyList <IProcess> GetAllChildProcesses() { var ids = jobObject.GetProcessIds() .Where(id => hostProcess.Id != id) .ToList(); return(processHelper.GetProcesses(ids).ToList()); }
public ContainerTests() { ConstrainedProcessRunner = Substitute.For <IProcessRunner>(); ContainerEnvironment = new Dictionary <string, string>() { { "Handle", "handle" } }; Directory = Substitute.For <IContainerDirectory>(); JobObject = Substitute.For <JobObject>(); JobObject.GetCpuStatistics().Returns(new CpuStatistics { TotalKernelTime = TimeSpan.Zero, TotalUserTime = TimeSpan.Zero, }); JobObject.GetProcessIds().Returns(new int[0]); ProcessHelper = Substitute.For <ProcessHelper>(); ProcessRunner = Substitute.For <IProcessRunner>(); ContainerPropertiesService = Substitute.For <IContainerPropertyService>(); TcpPortManager = Substitute.For <ILocalTcpPortManager>(); User = Substitute.For <IContainerUser>(); _containerUsername = string.Concat("container-username-", Guid.NewGuid().ToString()); User.UserName.Returns(_containerUsername); ContainerDiskQuota = Substitute.For <IContainerDiskQuota>(); DependencyHelper = Substitute.For <ContainerHostDependencyHelper>(); bindMounts = new[] { new BindMount() }; Container = new Container( string.Concat("id-", Guid.NewGuid()), string.Concat("handle-", Guid.NewGuid()), User, Directory, ContainerPropertiesService, TcpPortManager, JobObject, ContainerDiskQuota, ProcessRunner, ConstrainedProcessRunner, ProcessHelper, ContainerEnvironment, DependencyHelper, bindMounts ); }
public void WhenContainerStopped_Runs() { JobObject.GetCpuStatistics().Returns(new CpuStatistics { TotalKernelTime = TimeSpan.Zero, TotalUserTime = TimeSpan.Zero, }); JobObject.GetProcessIds().Returns(new int[0]); Container.Stop(false); var info = Container.GetInfo(); Assert.NotNull(info); }
public void WhenProcessHasExited() { var firstProcess = Substitute.For <IProcess>(); firstProcess.Id.Returns(1); firstProcess.PrivateMemoryBytes.Throws(new InvalidOperationException()); JobObject.GetProcessIds().Returns(new int[] { 1 }); ProcessHelper.GetProcesses(null).ReturnsForAnyArgs(new[] { firstProcess }); var metrics = Container.GetMetrics(); Assert.Equal(0ul, metrics.MemoryStat.PrivateBytes); }
public void WhenManagingNoProcess() { JobObject.GetCpuStatistics().Returns(new CpuStatistics { TotalKernelTime = TimeSpan.Zero, TotalUserTime = TimeSpan.Zero, }); JobObject.GetProcessIds().Returns(new int[0]); var metrics = Container.GetMetrics(); Assert.Equal(TimeSpan.Zero, metrics.CpuStat.TotalProcessorTime); Assert.Equal(0ul, metrics.MemoryStat.PrivateBytes); }
public GetAllChildProcesses() : base() { HostProcessId = 100; ExpectedProcessIds = new [] { 1, 2, 3, 4, 5, HostProcessId }; HostProcess.Id.Returns(HostProcessId); JobObject.GetProcessIds().Returns(ExpectedProcessIds); ProcessHelper.GetProcesses(null) .ReturnsForAnyArgs(call => { var ids = call.Arg <IEnumerable <int> >(); return(ids.Select(id => { var p = Substitute.For <IProcess>(); p.Id.Returns(id); return p; })); }); }