Skip to content

Add concurrent jobs control and fix flawed logic #356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions commands/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W

builderCtx.CoreBuildCachePath = paths.TempDir().Join("arduino-core-cache")

builderCtx.Jobs = int(req.GetJobs())

builderCtx.USBVidPid = req.GetVidPid()
builderCtx.WarningsLevel = req.GetWarnings()

Expand Down
81 changes: 48 additions & 33 deletions legacy/builder/builder_utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -169,47 +170,61 @@ func compileFilesWithRecipe(ctx *types.Context, sourcePath *paths.Path, sources
if len(sources) == 0 {
return objectFiles, nil
}
objectFilesChan := make(chan *paths.Path)
errorsChan := make(chan error)
doneChan := make(chan struct{})
var objectFilesMux sync.Mutex
var errors []error
var errorsMux sync.Mutex

ctx.Progress.Steps = ctx.Progress.Steps / float64(len(sources))
var wg sync.WaitGroup
wg.Add(len(sources))

for _, source := range sources {
go func(source *paths.Path) {
defer wg.Done()
PrintProgressIfProgressEnabledAndMachineLogger(ctx)
objectFile, err := compileFileWithRecipe(ctx, sourcePath, source, buildPath, buildProperties, includes, recipe)
if err != nil {
errorsChan <- err
} else {
objectFilesChan <- objectFile
}
}(source)
queue := make(chan *paths.Path)
job := func(source *paths.Path) {
PrintProgressIfProgressEnabledAndMachineLogger(ctx)
objectFile, err := compileFileWithRecipe(ctx, sourcePath, source, buildPath, buildProperties, includes, recipe)
if err != nil {
errorsMux.Lock()
errors = append(errors, err)
errorsMux.Unlock()
} else {
objectFilesMux.Lock()
objectFiles.Add(objectFile)
objectFilesMux.Unlock()
}
}

go func() {
wg.Wait()
doneChan <- struct{}{}
}()

for {
select {
case objectFile := <-objectFilesChan:
objectFiles.Add(objectFile)
case err := <-errorsChan:
return nil, i18n.WrapError(err)
case <-doneChan:
close(objectFilesChan)
for objectFile := range objectFilesChan {
objectFiles.Add(objectFile)
// Spawn jobs runners
var wg sync.WaitGroup
jobs := ctx.Jobs
if jobs == 0 {
jobs = runtime.NumCPU()
}
for i := 0; i < jobs; i++ {
wg.Add(1)
go func() {
for source := range queue {
job(source)
}
objectFiles.Sort()
return objectFiles, nil
wg.Done()
}()
}

// Feed jobs until error or done
for _, source := range sources {
errorsMux.Lock()
gotError := len(errors) > 0
errorsMux.Unlock()
if gotError {
break
}
queue <- source
}
close(queue)
wg.Wait()
if len(errors) > 0 {
// output the first error
return nil, i18n.WrapError(errors[0])
}
objectFiles.Sort()
return objectFiles, nil
}

func compileFileWithRecipe(ctx *types.Context, sourcePath *paths.Path, source *paths.Path, buildPath *paths.Path, buildProperties *properties.Map, includes []string, recipe string) (*paths.Path, error) {
Expand Down
3 changes: 3 additions & 0 deletions legacy/builder/types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ type Context struct {
// Experimental: use arduino-preprocessor to create prototypes
UseArduinoPreprocessor bool

// Parallel processes
Jobs int

// Out and Err stream to redirect all Exec commands
ExecStdout io.Writer
ExecStderr io.Writer
Expand Down
59 changes: 34 additions & 25 deletions rpc/commands/compile.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rpc/commands/compile.proto
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ message CompileReq {
bool quiet = 11; // Suppresses almost every output.
string vidPid = 12; // VID/PID specific build properties.
string exportFile = 13; // The compiled binary is written to this file
int32 jobs = 14; // The max number of concurrent compiler instances to run (as make -jx)
}

message CompileResp {
Expand Down