Skip to content

Commit

Permalink
Try to use a ram disk
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan committed Jun 15, 2024
1 parent 7c3b6ad commit 63be305
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
46 changes: 37 additions & 9 deletions Runner/JitDiffJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,48 @@ private async Task ChangeWorkingDirectoryToLargestDiskAsync()
return;
}

const string NewWorkDir = "/mnt/runner";
int availableRamGB = await GetTotalSystemMemoryGBAsync(TimeSpan.FromSeconds(5));

try
if (availableRamGB > 60)
{
Directory.CreateDirectory(NewWorkDir);
Environment.CurrentDirectory = NewWorkDir;
if (await TryApplyAsync(async () =>
{
const string NewWorkDir = "/ramdisk";
const string LogPrefix = "Prepare RAM disk";
int ramDiskSize = Math.Min(128, availableRamGB / 4 * 3);
await RunProcessAsync("mkdir", NewWorkDir, logPrefix: LogPrefix);
await RunProcessAsync("mount", $"-t tmpfs -o size={ramDiskSize}G tmpfs {NewWorkDir}", logPrefix: LogPrefix);
return NewWorkDir;
}))
{
return;
}
}
catch (Exception ex)

await TryApplyAsync(() =>
{
await LogAsync($"Failed to apply new working directory ({NewWorkDir}): {ex}");
return;
}
const string NewWorkDir = "/mnt/runner";
Directory.CreateDirectory(NewWorkDir);
return Task.FromResult(NewWorkDir);
});

async Task<bool> TryApplyAsync(Func<Task<string>> action)
{
try
{
string newDirectory = await action();
Environment.CurrentDirectory = newDirectory;

await LogAsync($"Changed working directory to {NewWorkDir}");
await LogAsync($"Changed working directory to {newDirectory}");
return true;
}
catch (Exception ex)
{
await LogAsync($"Failed to apply new working directory: {ex}");
return false;
}
}
}

private async Task CloneRuntimeAndSetupToolsAsync()
Expand Down
18 changes: 18 additions & 0 deletions Runner/JobBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,24 @@ protected int GetTotalSystemMemoryGB()
return (int)(memory.TotalPhysical / 1024 / 1024 / 1024);
}

protected async ValueTask<int> GetTotalSystemMemoryGBAsync(TimeSpan timeout)
{
Stopwatch s = Stopwatch.StartNew();

do
{
if (_hardwareInfo is not null)
{
return GetTotalSystemMemoryGB();
}

await Task.Delay(10);
}
while (s.Elapsed < timeout);

return 1;
}

private async Task StreamSystemHardwareInfoAsync()
{
Stopwatch stopwatch = Stopwatch.StartNew();
Expand Down

0 comments on commit 63be305

Please sign in to comment.