Skip to content

Commit

Permalink
Moved CalculateCabbingThreadCount() to BindDatabaseCommand because we…
Browse files Browse the repository at this point in the history
… need the capped value in both CreateCabinetsCommand and UpdateFileFacadesCommand.

Fixed bug in capping the thread count between 1 and processor count times 2. The "-ct 1000000" value was wrongly passed thru in the test CabinetFilesSequencedCorrectly

Added ThreadCount to UpdateFileFacadesCommand
  • Loading branch information
MBroholmA authored and barnson committed Sep 3, 2024
1 parent 4357d18 commit fb76913
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,29 @@ public BindDatabaseCommand(IBindContext context, IEnumerable<IWindowsInstallerBa

private CancellationToken CancellationToken { get; }

private int CalculateCabbingThreadCount()
{
var processorCount = Environment.ProcessorCount;

// If the number of processors is invalid, default to a single processor.
if (processorCount == 0)
{
processorCount = 1;

this.Messaging.Write(WarningMessages.InvalidEnvironmentVariable("NUMBER_OF_PROCESSORS", Environment.ProcessorCount.ToString(), processorCount.ToString()));
}

// If the cabbing thread count was provided, and it isn't more than double the number of processors, use it.
if (0 < this.CabbingThreadCount && this.CabbingThreadCount < processorCount * 2)
{
processorCount = this.CabbingThreadCount;
}

this.Messaging.Write(VerboseMessages.SetCabbingThreadCount(processorCount.ToString()));

return processorCount;
}

public IBindResult Execute()
{
if (!this.Intermediate.HasLevel(Data.IntermediateLevels.Linked) || !this.Intermediate.HasLevel(Data.IntermediateLevels.Resolved))
Expand All @@ -123,6 +146,8 @@ public IBindResult Execute()

var containsMergeModules = false;

int calculatedCabbingThreadCount = this.CalculateCabbingThreadCount();

// Load standard tables, authored custom tables, and extension custom tables.
TableDefinitionCollection tableDefinitions;
{
Expand Down Expand Up @@ -280,7 +305,7 @@ public IBindResult Execute()

// Gather information about files that do not come from merge modules.
{
var command = new UpdateFileFacadesCommand(this.Messaging, this.FileSystem, section, allFileFacades, fileFacadesFromIntermediate, variableCache, overwriteHash: true, this.CancellationToken);
var command = new UpdateFileFacadesCommand(this.Messaging, this.FileSystem, section, allFileFacades, fileFacadesFromIntermediate, variableCache, overwriteHash: true, this.CancellationToken, calculatedCabbingThreadCount);
command.Execute();
}

Expand Down Expand Up @@ -324,7 +349,7 @@ public IBindResult Execute()
{
var updatedFacades = reresolvedFiles.Select(f => allFileFacades.First(ff => ff.Id == f.Id?.Id));

var command = new UpdateFileFacadesCommand(this.Messaging, this.FileSystem, section, allFileFacades, updatedFacades, variableCache, overwriteHash: false, this.CancellationToken);
var command = new UpdateFileFacadesCommand(this.Messaging, this.FileSystem, section, allFileFacades, updatedFacades, variableCache, overwriteHash: false, this.CancellationToken, calculatedCabbingThreadCount);
command.Execute();
}
}
Expand Down Expand Up @@ -442,7 +467,7 @@ public IBindResult Execute()
{
this.Messaging.Write(VerboseMessages.CreatingCabinetFiles());

var command = new CreateCabinetsCommand(this.ServiceProvider, this.Messaging, this.WindowsInstallerBackendHelper, this.BackendExtensions, section, this.CabCachePath, this.CabbingThreadCount, this.OutputPath, this.IntermediateFolder, this.DefaultCompressionLevel, compressed, modularizationSuffix, filesByCabinetMedia, data, tableDefinitions, this.ResolveMedia);
var command = new CreateCabinetsCommand(this.ServiceProvider, this.Messaging, this.WindowsInstallerBackendHelper, this.BackendExtensions, section, this.CabCachePath, calculatedCabbingThreadCount, this.OutputPath, this.IntermediateFolder, this.DefaultCompressionLevel, compressed, modularizationSuffix, filesByCabinetMedia, data, tableDefinitions, this.ResolveMedia);
command.Execute();

fileTransfers.AddRange(command.FileTransfers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,9 @@ public CreateCabinetsCommand(IServiceProvider serviceProvider, IMessaging messag

public void Execute()
{
var calculatedCabbingThreadCount = this.CalculateCabbingThreadCount();

this.GetMediaTemplateAttributes(out var maximumCabinetSizeForLargeFileSplitting, out var maximumUncompressedMediaSize);

var cabinetBuilder = new CabinetBuilder(this.Messaging, calculatedCabbingThreadCount, maximumCabinetSizeForLargeFileSplitting, maximumUncompressedMediaSize);
var cabinetBuilder = new CabinetBuilder(this.Messaging, this.CabbingThreadCount, maximumCabinetSizeForLargeFileSplitting, maximumUncompressedMediaSize);

var hashesByFileId = this.Section.Symbols.OfType<MsiFileHashSymbol>().ToDictionary(s => s.Id.Id);

Expand Down Expand Up @@ -122,29 +120,6 @@ public void Execute()
this.UpdateMediaWithSpannedCabinets(cabinetBuilder.CompletedCabinets);
}

private int CalculateCabbingThreadCount()
{
var processorCount = Environment.ProcessorCount;

// If the number of processors is invalid, default to a single processor.
if (processorCount == 0)
{
processorCount = 1;

this.Messaging.Write(WarningMessages.InvalidEnvironmentVariable("NUMBER_OF_PROCESSORS", Environment.ProcessorCount.ToString(), processorCount.ToString()));
}

// If the cabbing thread count was provided, and it isn't more than double the number of processors, use it.
if (this.CabbingThreadCount > 0 && processorCount < this.CabbingThreadCount * 2)
{
processorCount = this.CabbingThreadCount;
}

this.Messaging.Write(VerboseMessages.SetCabbingThreadCount(processorCount.ToString()));

return processorCount;
}

private CabinetWorkItem CreateCabinetWorkItem(WindowsInstallerData data, string cabinetDir, MediaSymbol mediaSymbol, CompressionLevel compressionLevel, IEnumerable<IFileFacade> fileFacades, Dictionary<string, MsiFileHashSymbol> hashesByFileId)
{
CabinetWorkItem cabinetWorkItem = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace WixToolset.Core.WindowsInstaller.Bind
/// </summary>
internal class UpdateFileFacadesCommand
{
public UpdateFileFacadesCommand(IMessaging messaging, IFileSystem fileSystem, IntermediateSection section, IEnumerable<IFileFacade> allFileFacades, IEnumerable<IFileFacade> updateFileFacades, IDictionary<string, string> variableCache, bool overwriteHash, CancellationToken cancellationToken)
public UpdateFileFacadesCommand(IMessaging messaging, IFileSystem fileSystem, IntermediateSection section, IEnumerable<IFileFacade> allFileFacades, IEnumerable<IFileFacade> updateFileFacades, IDictionary<string, string> variableCache, bool overwriteHash, CancellationToken cancellationToken, int threadCount)
{
this.Messaging = messaging;
this.FileSystem = fileSystem;
Expand All @@ -32,6 +32,7 @@ public UpdateFileFacadesCommand(IMessaging messaging, IFileSystem fileSystem, In
this.VariableCache = variableCache;
this.OverwriteHash = overwriteHash;
this.CancellationToken = cancellationToken;
this.ThreadCount = threadCount;
}

private IMessaging Messaging { get; }
Expand All @@ -50,6 +51,8 @@ public UpdateFileFacadesCommand(IMessaging messaging, IFileSystem fileSystem, In

private CancellationToken CancellationToken { get; }

private int ThreadCount { get; }

public void Execute()
{
try
Expand All @@ -74,7 +77,8 @@ private void UpdateFileFacadesInParallel(IEnumerable<IFileFacade> facades)

Parallel.ForEach(facades,
new ParallelOptions{
CancellationToken = this.CancellationToken
CancellationToken = this.CancellationToken,
MaxDegreeOfParallelism = this.ThreadCount
},
() =>
{
Expand Down

0 comments on commit fb76913

Please sign in to comment.