Skip to content

Commit

Permalink
Some refactoring, base end to end tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lilla28 committed Mar 31, 2023
1 parent 6dbf4a3 commit e5695dd
Show file tree
Hide file tree
Showing 23 changed files with 418 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProcessExplorer.Abstraction
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProcessExplorer.Core.Tests", "test\ProcessExplorer.Core.Tests\ProcessExplorer.Core.Tests.csproj", "{3C2B4F9E-CA84-4FB1-AE2B-CC61886A3CE7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProcessExplorer.Server.IntegrationTests", "test\ProcessExplorer.Server.EndToEndTests\ProcessExplorer.Server.IntegrationTests.csproj", "{18B9042E-0D77-4917-97FF-14BC34085385}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -45,6 +47,10 @@ Global
{3C2B4F9E-CA84-4FB1-AE2B-CC61886A3CE7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C2B4F9E-CA84-4FB1-AE2B-CC61886A3CE7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C2B4F9E-CA84-4FB1-AE2B-CC61886A3CE7}.Release|Any CPU.Build.0 = Release|Any CPU
{18B9042E-0D77-4917-97FF-14BC34085385}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{18B9042E-0D77-4917-97FF-14BC34085385}.Debug|Any CPU.Build.0 = Debug|Any CPU
{18B9042E-0D77-4917-97FF-14BC34085385}.Release|Any CPU.ActiveCfg = Release|Any CPU
{18B9042E-0D77-4917-97FF-14BC34085385}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -55,6 +61,7 @@ Global
{AF40EAD5-8A98-465E-BF26-2EB06274A384} = {3413C687-1DD1-4751-8C4F-0CD308248CB2}
{51110571-1082-4790-AA00-3DDDEE80641A} = {3413C687-1DD1-4751-8C4F-0CD308248CB2}
{3C2B4F9E-CA84-4FB1-AE2B-CC61886A3CE7} = {55657FBD-CD23-40A2-B8E9-2BBE734924CC}
{18B9042E-0D77-4917-97FF-14BC34085385} = {55657FBD-CD23-40A2-B8E9-2BBE734924CC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {819F883C-DDD0-4F2B-82C5-820D4D6AB446}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ namespace ProcessExplorer.Abstractions;

public interface IProcessInfoAggregator : IDisposable
{
/// <summary>
/// Main process id to watch.
/// </summary>
public int MainProcessId { get; }

/// <summary>
/// Adds a runtime information to the collection.
/// </summary>
Expand All @@ -37,8 +42,8 @@ public interface IProcessInfoAggregator : IDisposable
/// <summary>
/// Sets Compose PID.
/// </summary>
/// <param name="pid"></param>
void SetComposePid(int pid);
/// <param name="processId"></param>
void SetMainProcessId(int processId);

/// <summary>
/// Sets the SubsystemController.
Expand Down Expand Up @@ -115,8 +120,8 @@ public interface IProcessInfoAggregator : IDisposable
/// <summary>
/// Sets the processes, which is gotten from the ModuleLoader.
/// </summary>
/// <param name="processInfo"></param>
void InitProcesses(ReadOnlySpan<int> pids);
/// <param name="processIds"></param>
void InitProcesses(ReadOnlySpan<int> processIds);

/// <summary>
/// Initializes the subsystems taken from the user defined manifest.
Expand Down Expand Up @@ -165,9 +170,9 @@ public interface IProcessInfoAggregator : IDisposable
/// <summary>
/// Adds processes to watch to the existing watchable process ids list.
/// </summary>
/// <param name="processes"></param>
/// <param name="processIds"></param>
/// <returns></returns>
ValueTask AddProcesses(ReadOnlySpan<int> processes);
ValueTask AddProcesses(ReadOnlySpan<int> processIds);

/// <summary>
/// Puts the given subsystem into the queue to send subsystem state changed information to the UI's.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
syntax = "proto3";

option csharp_namespace = "ProcessExplorer.Server.Server.Infrastructure.Protos";
option csharp_namespace = "ProcessExplorer.Abstractions.Infrastructure.Protos";

import "google/protobuf/duration.proto";
import "google/protobuf/empty.proto";
Expand All @@ -12,7 +12,9 @@ service ProcessExplorerMessageHandler{
}

message Message{
ActionType action = 1;
oneof action1 {
ActionType action = 1;
};
optional string assemblyId = 2;
repeated Process processes = 3;
repeated Registration registrations = 4;
Expand All @@ -25,6 +27,7 @@ message Message{
map<string, ProcessInfoCollectorData> multipleRuntimeInfo = 11;
int32 periodOfDelay = 12;
map<int32, string> processStatusChanges = 13;
optional string description = 14;
}

enum ActionType{
Expand All @@ -50,12 +53,10 @@ enum ActionType{
TerminateSubsystemsAction = 19;
LaunchSubsystemsAction = 20;
LaunchSubsystemsWithDelayAction = 21;
SubscriptionAliveAction = 22;
}

message Process{
optional string instanceId = 1;
optional string uiType = 2;
optional string uiHint = 3;
optional string startTime = 4;
optional google.protobuf.Duration processorUsageTime = 5;
optional int64 physicalMemoryUsageBit = 6;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,20 @@
</PropertyGroup>

<ItemGroup>
<None Remove="Infrastructure\Protos\ProcessExplorerMessages.proto" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.22.1" />
<PackageReference Include="Grpc" Version="2.46.6" />
<PackageReference Include="Grpc.Tools" Version="2.53.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="JetBrains.Annotations" Version="10.3.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="6.0.0" />
<PackageReference Include="protobuf-net.Core" Version="3.2.16" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="6.0.0" />
<PackageReference Include="System.Management" Version="6.0.0" />
<PackageReference Include="System.ServiceModel.Primitives" Version="4.10.0" />
Expand All @@ -19,4 +30,10 @@
<ProjectReference Include="..\LocalCollector\LocalCollector.csproj" />
</ItemGroup>

<ItemGroup>
<Protobuf Include="Infrastructure\Protos\ProcessExplorerMessages.proto">
<GrpcServices>Both</GrpcServices>
</Protobuf>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,13 @@

namespace ProcessExplorer.Abstractions.Processes;

public struct ProcessInfoData
public class ProcessInfoData
{
public ProcessInfoData(
string? startTime,
TimeSpan? processorUsageTime,
long? physicalMemoryUsageBit,
string? processName,
int pID,
int? priorityLevel,
string? processPriorityClass,
IEnumerable<ProcessThread> threads,
long? virtualMemorySize,
int? parentId,
long? privateMemoryUsage,
string? processStatus,
float? memoryUsage,
float? processorUsage)
{
StartTime = startTime;
ProcessorUsageTime = processorUsageTime;
PhysicalMemoryUsageBit = physicalMemoryUsageBit;
ProcessName = processName;
PID = pID;
PriorityLevel = priorityLevel;
ProcessPriorityClass = processPriorityClass;
Threads = threads;
VirtualMemorySize = virtualMemorySize;
ParentId = parentId;
PrivateMemoryUsage = privateMemoryUsage;
ProcessStatus = processStatus;
MemoryUsage = memoryUsage;
ProcessorUsage = processorUsage;
}

public string? StartTime { get; set; }
public TimeSpan? ProcessorUsageTime { get; set; }
public long? PhysicalMemoryUsageBit { get; set; }
public string? ProcessName { get; set; }
public int PID { get; set; }
public int ProcessId { get; set; }
public int? PriorityLevel { get; set; }
public string? ProcessPriorityClass { get; set; }
public IEnumerable<ProcessThread> Threads { get; set; } = Enumerable.Empty<ProcessThread>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace ProcessExplorer.Abstractions.Processes;

public abstract class ProcessInfoManager : IDisposable
public abstract class ProcessInfoManager : IDisposable //Create an interface
{
private ProcessCreatedHandler? _processCreatedHandler;
private ProcessModifiedHandler? _processModifiedHandler;
Expand Down Expand Up @@ -78,42 +78,42 @@ public void SetHandlers(
_processesModifiedHandler = processesModifiedHandler;
}

public void SetComposePid(int pid)
public void SetComposePid(int processId)
{
_composePid = pid;
_composePid = processId;
}

public bool ContainsId(int pid)
public bool ContainsId(int processId)
{
lock (_locker)
{
return _processIds.Contains(pid);
return _processIds.Contains(processId);
}
}

public void AddProcess(int pid)
public void AddProcess(int processId)
{
lock (_locker)
{
if (pid == 0) return;
if (processId == 0) return;

_processIds.Add(pid);
_processIds.Add(processId);
}
}

public void RemoveProcessId(int pid)
public void RemoveProcessId(int processId)
{
lock (_locker)
{
_processIds.Remove(pid);
_processIds.Remove(processId);
}
}

public void SetProcessIds(ReadOnlySpan<int> ids)
public void SetProcessIds(ReadOnlySpan<int> processIds)
{
lock (_locker)
{
foreach (var id in ids)
foreach (var id in processIds)
{
if (_processIds.Contains(id)) continue;
_processIds.Add(id);
Expand Down Expand Up @@ -150,16 +150,14 @@ public void ClearProcessIds()
/// <summary>
/// Returns the memory usage (%) of the given process.
/// </summary>
/// <param name="process"></param>
/// <returns></returns>
public abstract float GetMemoryUsage(int id, string processName);
public abstract float GetMemoryUsage(int processId, string processName);

/// <summary>
/// Returns the CPU usage (%) of the given process.
/// </summary>
/// <param name="process"></param>
/// <returns></returns>
public abstract float GetCpuUsage(int id, string processName);
public abstract float GetCpuUsage(int processId, string processName);

/// <summary>
/// Continuously watching created processes.
Expand All @@ -183,52 +181,52 @@ private bool IsComposeProcess(int processId)

if (processId == _composePid || ContainsId(processId)) return true;

var ppid = GetParentId(processId, Process.GetProcessById(processId).ProcessName);
var parentProcessId = GetParentId(processId, Process.GetProcessById(processId).ProcessName);

if (ppid == null || ppid == 0) return false;
if (parentProcessId == null || parentProcessId == 0) return false;

if (ContainsId((int)ppid)) return true;
if (ContainsId((int)parentProcessId)) return true;

return IsComposeProcess(Convert.ToInt32(ppid));
return IsComposeProcess(Convert.ToInt32(parentProcessId));
}

/// <summary>
/// Searches for child processes to watch.
/// </summary>
/// <param name="processInfo"></param>
/// <returns></returns>
public abstract ReadOnlySpan<int> AddChildProcesses(int pid, string? processName);
public abstract ReadOnlySpan<int> AddChildProcesses(int processId, string? processName);

/// <summary>
/// Checks if a process belongs to the Compose.
/// </summary>
/// <param name="pid"></param>
public bool CheckIfIsComposeProcess(int pid)
/// <param name="processId"></param>
public bool CheckIfIsComposeProcess(int processId)
{
return IsComposeProcess(pid);
return IsComposeProcess(processId);
}

public void SendNewProcessUpdate(int pid)
public void SendNewProcessUpdate(int processId)
{
_processCreatedHandler?.Invoke(pid);
_processCreatedHandler?.Invoke(processId);
}

/// <summary>
/// Sends a terminated process information to publish
/// </summary>
/// <param name="pid"></param>
public void SendTerminatedProcessUpdate(int pid)
/// <param name="processId"></param>
public void SendTerminatedProcessUpdate(int processId)
{
ProcessTerminated(pid);
ProcessTerminated(processId);
}

/// <summary>
/// Sends a modified process information to publish
/// </summary>
/// <param name="pid"></param>
public void SendProcessModifiedUpdate(int pid)
/// <param name="processId"></param>
public void SendProcessModifiedUpdate(int processId)
{
_processModifiedHandler?.Invoke(pid);
_processModifiedHandler?.Invoke(processId);
}

public void SetDeadProcessRemovalDelay(int delay)
Expand All @@ -255,31 +253,31 @@ private void ProcessTerminated(int pid)
if (!TryDeleteProcess(pid)) _logger.CannotTerminateProcessError(pid);
}

private bool TryDeleteProcess(int pid)
private bool TryDeleteProcess(int processId)
{
try
{
_logger.ProcessTerminatedInformation(pid);
_processStatusChangedHandler?.Invoke(new(pid, Status.Terminated));
RemoveProcessAfterTimeout(pid);
_logger.ProcessTerminatedInformation(processId);
_processStatusChangedHandler?.Invoke(new(processId, Status.Terminated));
RemoveProcessAfterTimeout(processId);
return true;
}
catch (Exception exception)
{
_logger.PpidExpected(pid, exception);
_logger.PpidExpected(processId, exception);
}

return false;
}

private void RemoveProcessAfterTimeout(int item)
private async void RemoveProcessAfterTimeout(int processId)
{
Task.Run(() =>
await Task.Run(() =>
{
Task.Delay(_delayTime);
var ids = GetProcessIds();
_processesModifiedHandler?.Invoke(ids);
_processTerminatedHandler?.Invoke(item);
_processTerminatedHandler?.Invoke(processId);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace ProcessExplorer.Abstractions.Subsystems;

//TODO(Lilla): define ProtoContract to derive it implicitly without protoconverthelper
public class SubsystemInfo
{
public string Name { get; set; } = string.Empty;
Expand Down
Loading

0 comments on commit e5695dd

Please sign in to comment.