Commit a2a00e21 by Ben Clayton Committed by Ben Clayton

Add regres tool.

Regress is a tool that detects test regressions with SwiftShader changes. Regres monitors changes that have been put up for review with Gerrit. Once a new patchset has been found, regres will checkout, build and test the change against the parent changelist. Any differences in results are reported as a review comment on the change. Change-Id: I0a848a56d99f020d6ffd48699a2277fa71526788 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/24929Tested-by: 's avatarBen Clayton <bclayton@google.com> Reviewed-by: 's avatarNicolas Capens <nicolascapens@google.com>
parent 34a76b50
# Ignored folders #
/cache/
/lib/
/obj/
/bin/
......
// Copyright 2019 The SwiftShader Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package cause provides functions for building wrapped errors.
package cause
import (
"fmt"
)
// errWithCause is an error that wrap an inner error with additional info.
type errWithCause struct {
cause error
msg string
}
func (e errWithCause) Error() string { return fmt.Sprintf("%s. Cause: %v", e.msg, e.cause) }
// Wrap returns a new error wrapping cause with the additional message.
func Wrap(cause error, msg string, args ...interface{}) error {
return errWithCause{cause, fmt.Sprintf(msg, args...)}
}
// Copyright 2019 The SwiftShader Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package git provides functions for interacting with Git.
package git
import (
"encoding/hex"
"fmt"
"io/ioutil"
"net/url"
"os"
"os/exec"
"time"
"../cause"
"../shell"
)
const (
gitTimeout = time.Minute * 5 // timeout for a git operation
)
var exe string
func init() {
path, err := exec.LookPath("git")
if err != nil {
panic(cause.Wrap(err, "Couldn't find path to git executable"))
}
exe = path
}
// Hash is a 20 byte, git object hash.
type Hash [20]byte
func (h Hash) String() string { return hex.EncodeToString(h[:]) }
// ParseHash returns a Hash from a hexadecimal string.
func ParseHash(s string) Hash {
b, _ := hex.DecodeString(s)
h := Hash{}
copy(h[:], b)
return h
}
// Add calls 'git add <file>'.
func Add(project, file string) error {
if err := shell.Shell(gitTimeout, exe, project, "add", file); err != nil {
return err
}
return nil
}
// CommitFlags advanced flags for Commit
type CommitFlags struct {
Name string // Used for author and committer
Email string // Used for author and committer
}
// Commit calls 'git commit -m <msg> --author <author>'.
func Commit(project, msg string, flags CommitFlags) error {
args := []string{}
if flags.Name != "" {
args = append(args, "-c", "user.name="+flags.Name)
}
if flags.Email != "" {
args = append(args, "-c", "user.email="+flags.Email)
}
args = append(args, "commit", "-m", msg)
return shell.Shell(gitTimeout, exe, project, args...)
}
// PushFlags advanced flags for Commit
type PushFlags struct {
Username string // Used for authentication when uploading
Password string // Used for authentication when uploading
}
// Push pushes the local branch to remote.
func Push(project, remote, localBranch, remoteBranch string, flags PushFlags) error {
args := []string{}
if flags.Username != "" {
f, err := ioutil.TempFile("", "regres-cookies.txt")
if err != nil {
return cause.Wrap(err, "Couldn't create cookie file")
}
defer f.Close()
defer os.Remove(f.Name())
u, err := url.Parse(remote)
if err != nil {
return cause.Wrap(err, "Couldn't parse url '%v'", remote)
}
f.WriteString(fmt.Sprintf("%v FALSE / TRUE 2147483647 o %v=%v\n", u.Host, flags.Username, flags.Password))
f.Close()
args = append(args, "-c", "http.cookiefile="+f.Name())
}
args = append(args, "push", remote, localBranch+":"+remoteBranch)
return shell.Shell(gitTimeout, exe, project, args...)
}
// Checkout performs a git checkout of the given commit into path.
func Checkout(path, url string, commit Hash) error {
if err := os.MkdirAll(path, 0777); err != nil {
return cause.Wrap(err, "mkdir '"+path+"' failed")
}
for _, cmds := range [][]string{
{"init"},
{"remote", "add", "origin", url},
{"fetch", "origin", commit.String()},
{"checkout", commit.String()},
} {
if err := shell.Shell(gitTimeout, exe, path, cmds...); err != nil {
os.RemoveAll(path)
return err
}
}
return nil
}
// FetchRefHash returns the git hash of the given ref.
func FetchRefHash(ref, url string) (Hash, error) {
out, err := shell.Exec(gitTimeout, exe, "", nil, "ls-remote", url, ref)
if err != nil {
return Hash{}, err
}
return ParseHash(string(out)), nil
}
// Copyright 2019 The SwiftShader Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package shell provides functions for running sub-processes.
package shell
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"strconv"
"syscall"
"time"
"../cause"
)
// MaxProcMemory is the maximum virtual memory per child process
var MaxProcMemory uint64 = 2 * 1024 * 1024 * 1024 // 2GB
func init() {
// As we are going to be running a number of tests concurrently, we need to
// limit the amount of virtual memory each test uses, otherwise memory
// hungry tests can bring the whole system down into a swapping apocalypse.
//
// Linux has the setrlimit() function to limit a process (and child's)
// virtual memory usage - but we cannot call this from the regres process
// as this process may need more memory than the limit allows.
//
// Unfortunately golang has no native support for setting rlimits for child
// processes (https://github.com/golang/go/issues/6603), so we instead wrap
// the exec to the test executable with another child regres process using a
// special --exec mode:
//
// [regres] -> [regres --exec <test-exe N args...>] -> [test-exe]
// ^^^^
// (calls rlimit() with memory limit of N bytes)
if len(os.Args) > 3 && os.Args[1] == "--exec" {
exe := os.Args[2]
limit, err := strconv.ParseUint(os.Args[3], 10, 64)
if err != nil {
log.Fatalf("Expected memory limit as 3rd argument. %v\n", err)
}
if limit > 0 {
if err := syscall.Setrlimit(syscall.RLIMIT_AS, &syscall.Rlimit{Cur: limit, Max: limit}); err != nil {
log.Fatalln(cause.Wrap(err, "Setrlimit").Error())
}
}
c := exec.Command(exe, os.Args[4:]...)
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
c.Run()
os.Exit(c.ProcessState.ExitCode())
}
}
// Shell runs the executable exe with the given arguments, in the working
// directory wd.
// If the process does not finish within timeout a errTimeout will be returned.
func Shell(timeout time.Duration, exe, wd string, args ...string) error {
if out, err := Exec(timeout, exe, wd, nil, args...); err != nil {
return cause.Wrap(err, "%s", out)
}
return nil
}
// Exec runs the executable exe with the given arguments, in the working
// directory wd, with the custom environment flags.
// If the process does not finish within timeout a errTimeout will be returned.
func Exec(timeout time.Duration, exe, wd string, env []string, args ...string) ([]byte, error) {
// Shell via regres: --exec N <exe> <args...>
// See main() for details.
args = append([]string{"--exec", exe, fmt.Sprintf("%v", MaxProcMemory)}, args...)
b := bytes.Buffer{}
c := exec.Command(os.Args[0], args...)
c.Dir = wd
c.Env = env
c.Stdout = &b
c.Stderr = &b
if err := c.Start(); err != nil {
return nil, err
}
res := make(chan error)
go func() { res <- c.Wait() }()
select {
case <-time.NewTimer(timeout).C:
c.Process.Signal(syscall.SIGINT)
time.Sleep(time.Second * 5)
if !c.ProcessState.Exited() {
c.Process.Kill()
}
return b.Bytes(), ErrTimeout{exe, timeout}
case err := <-res:
return b.Bytes(), err
}
}
// ErrTimeout is the error returned when a process does not finish with its
// permitted time.
type ErrTimeout struct {
process string
timeout time.Duration
}
func (e ErrTimeout) Error() string {
return fmt.Sprintf("'%v' did not return after %v", e.process, e.timeout)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment