Skip to content

Commit

Permalink
Merge pull request #74 from DougGregor/parallel
Browse files Browse the repository at this point in the history
Respect numParallelJobs and also install INT handler
  • Loading branch information
aciidgh authored Oct 7, 2019
2 parents 26b72bf + 908dbe8 commit dc19ff3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
7 changes: 5 additions & 2 deletions Sources/SwiftDriver/Driver/Driver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,8 @@ extension Driver {
/// Run the driver.
public mutating func run(
resolver: ArgsResolver,
executorDelegate: JobExecutorDelegate? = nil
executorDelegate: JobExecutorDelegate? = nil,
processSet: ProcessSet? = nil
) throws {
// We just need to invoke the corresponding tool if the kind isn't Swift compiler.
guard driverKind.isSwiftCompiler else {
Expand Down Expand Up @@ -402,7 +403,9 @@ extension Driver {
// Start up an executor and perform the build.
let jobExecutor = JobExecutor(
jobs: jobs, resolver: resolver,
executorDelegate: executorDelegate
executorDelegate: executorDelegate,
numParallelJobs: numParallelJobs,
processSet: processSet
)
try jobExecutor.execute()
}
Expand Down
44 changes: 41 additions & 3 deletions Sources/SwiftDriver/Execution/JobExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,24 @@ public final class JobExecutor {
/// Queue for executor delegate.
let delegateQueue: DispatchQueue = DispatchQueue(label: "org.swift.driver.job-executor-delegate")

/// Operation queue for executing tasks in parallel.
let jobQueue: OperationQueue

/// The process set to use when launching new processes.
let processSet: ProcessSet?

init(
argsResolver: ArgsResolver,
producerMap: [VirtualPath: Job],
executorDelegate: JobExecutorDelegate
executorDelegate: JobExecutorDelegate,
jobQueue: OperationQueue,
processSet: ProcessSet?
) {
self.producerMap = producerMap
self.argsResolver = argsResolver
self.executorDelegate = executorDelegate
self.jobQueue = jobQueue
self.processSet = processSet
}
}

Expand All @@ -106,14 +116,24 @@ public final class JobExecutor {
/// The job executor delegate.
let executorDelegate: JobExecutorDelegate

/// The number of jobs to run in parallel.
let numParallelJobs: Int

/// The process set to use when launching new processes.
let processSet: ProcessSet?

public init(
jobs: [Job],
resolver: ArgsResolver,
executorDelegate: JobExecutorDelegate
executorDelegate: JobExecutorDelegate,
numParallelJobs: Int? = nil,
processSet: ProcessSet? = nil
) {
self.jobs = jobs
self.argsResolver = resolver
self.executorDelegate = executorDelegate
self.numParallelJobs = numParallelJobs ?? 1
self.processSet = processSet
}

/// Execute all jobs.
Expand Down Expand Up @@ -141,10 +161,16 @@ public final class JobExecutor {
}
}

let jobQueue = OperationQueue()
jobQueue.name = "org.swift.driver.job-execution"
jobQueue.maxConcurrentOperationCount = numParallelJobs

return Context(
argsResolver: argsResolver,
producerMap: producerMap,
executorDelegate: executorDelegate
executorDelegate: executorDelegate,
jobQueue: jobQueue,
processSet: processSet
)
}
}
Expand Down Expand Up @@ -281,6 +307,13 @@ class ExecuteJobRule: LLBuildRule {
return engine.taskIsComplete(DriverBuildValue.jobExecution(success: false))
}

let context = engine.jobExecutorContext
context.jobQueue.addOperation {
self.executeJob(engine)
}
}

private func executeJob(_ engine: LLTaskBuildEngine) {
let context = engine.jobExecutorContext
let resolver = context.argsResolver
let job = key.job
Expand All @@ -295,6 +328,11 @@ class ExecuteJobRule: LLBuildRule {
let process = try context.executorDelegate.launchProcess(for: job, arguments: arguments)
pid = Int(process.processID)

// Add it to the process set if it's a real process.
if case let realProcess as TSCBasic.Process = process {
try context.processSet?.add(realProcess)
}

// Inform the delegate.
context.delegateQueue.async {
context.executorDelegate.jobStarted(job: job, arguments: arguments, pid: pid)
Expand Down
13 changes: 11 additions & 2 deletions Sources/swift-driver/main.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import SwiftDriver

import TSCLibc
import enum TSCUtility.Diagnostics
import TSCBasic
import TSCUtility

var intHandler: InterruptHandler?

do {
let processSet = ProcessSet()
intHandler = try InterruptHandler {
processSet.terminate()
}

var driver = try Driver(args: CommandLine.arguments)
let resolver = try ArgsResolver()
try driver.run(resolver: resolver)
try driver.run(resolver: resolver, processSet: processSet)

if driver.diagnosticEngine.hasErrors {
exit(EXIT_FAILURE)
Expand Down

0 comments on commit dc19ff3

Please sign in to comment.