1// Copyright 2019 The SwiftShader Authors. All Rights Reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15// regres is a tool that detects test regressions with SwiftShader changes. 16// 17// Regres monitors changes that have been put up for review with Gerrit. 18// Once a new patchset has been found, regres will checkout, build and test the 19// change against the parent changelist. Any differences in results are reported 20// as a review comment on the change. 21// 22// Once a day regres will also test another, larger set of tests, and post the 23// full test results as a Gerrit changelist. The CI test lists can be based from 24// this daily test list, so testing can be limited to tests that were known to 25// pass. 26package main 27 28import ( 29 "bytes" 30 "crypto/sha1" 31 "encoding/hex" 32 "encoding/json" 33 "errors" 34 "flag" 35 "fmt" 36 "io/ioutil" 37 "log" 38 "math" 39 "os" 40 "os/exec" 41 "path" 42 "path/filepath" 43 "regexp" 44 "runtime" 45 "sort" 46 "strings" 47 "time" 48 49 "../../cause" 50 "../../consts" 51 "../../cov" 52 "../../deqp" 53 "../../git" 54 "../../llvm" 55 "../../shell" 56 "../../testlist" 57 "../../util" 58 59 gerrit "github.com/andygrunwald/go-gerrit" 60) 61 62const ( 63 gitURL = "https://swiftshader.googlesource.com/SwiftShader" 64 gerritURL = "https://swiftshader-review.googlesource.com/" 65 coverageURL = "https://$USERNAME:$PASSWORD@github.com/swiftshader-regres/swiftshader-coverage.git" 66 coverageBranch = "gh-pages" 67 coveragePath = "coverage/coverage.zip" 68 reportHeader = "Regres report:" 69 changeUpdateFrequency = time.Minute * 5 70 changeQueryFrequency = time.Minute * 5 71 testTimeout = time.Minute * 2 // timeout for a single test 72 buildTimeout = time.Minute * 10 // timeout for a build 73 fullTestListRelPath = "tests/regres/full-tests.json" 74 ciTestListRelPath = "tests/regres/ci-tests.json" 75 deqpConfigRelPath = "tests/regres/deqp.json" 76 swsTestLists = "tests/regres/testlists" 77 deqpTestLists = "external/vulkancts/mustpass/master" 78) 79 80var ( 81 numParallelTests = runtime.NumCPU() 82 llvmVersion = llvm.Version{Major: 10} 83 84 cacheDir = flag.String("cache", "cache", "path to the output cache directory") 85 gerritEmail = flag.String("email", "$SS_REGRES_EMAIL", "gerrit email address for posting regres results") 86 gerritUser = flag.String("user", "$SS_REGRES_USER", "gerrit username for posting regres results") 87 gerritPass = flag.String("pass", "$SS_REGRES_PASS", "gerrit password for posting regres results") 88 githubUser = flag.String("gh-user", "$SS_GITHUB_USER", "github user for posting coverage results") 89 githubPass = flag.String("gh-pass", "$SS_GITHUB_PASS", "github password for posting coverage results") 90 keepCheckouts = flag.Bool("keep", false, "don't delete checkout directories after use") 91 dryRun = flag.Bool("dry", false, "don't post regres reports to gerrit") 92 maxProcMemory = flag.Uint64("max-proc-mem", shell.MaxProcMemory, "maximum virtual memory per child process") 93 dailyNow = flag.Bool("dailynow", false, "Start by running the daily pass") 94 dailyOnly = flag.Bool("dailyonly", false, "Run only the daily pass") 95 dailyChange = flag.String("dailychange", "", "Change hash to use for daily pass, HEAD if not provided") 96 priority = flag.String("priority", "", "Prioritize a single change with the given id") 97 limit = flag.Int("limit", 0, "only run a maximum of this number of tests") 98) 99 100func main() { 101 flag.ErrHelp = errors.New("regres is a tool to detect regressions between versions of SwiftShader") 102 flag.Parse() 103 104 shell.MaxProcMemory = *maxProcMemory 105 106 r := regres{ 107 cacheRoot: *cacheDir, 108 gerritEmail: os.ExpandEnv(*gerritEmail), 109 gerritUser: os.ExpandEnv(*gerritUser), 110 gerritPass: os.ExpandEnv(*gerritPass), 111 githubUser: os.ExpandEnv(*githubUser), 112 githubPass: os.ExpandEnv(*githubPass), 113 keepCheckouts: *keepCheckouts, 114 dryRun: *dryRun, 115 dailyNow: *dailyNow, 116 dailyOnly: *dailyOnly, 117 dailyChange: *dailyChange, 118 priority: *priority, 119 } 120 121 if err := r.run(); err != nil { 122 fmt.Fprintln(os.Stderr, err) 123 os.Exit(-1) 124 } 125} 126 127type regres struct { 128 cmake string // path to cmake executable 129 make string // path to make executable 130 python string // path to python executable 131 tar string // path to tar executable 132 cacheRoot string // path to the regres cache directory 133 toolchain *llvm.Toolchain // the LLVM toolchain used to build SwiftShader 134 gerritEmail string // gerrit email address used for posting results 135 gerritUser string // gerrit username used for posting results 136 gerritPass string // gerrit password used for posting results 137 githubUser string // github username used for posting results 138 githubPass string // github password used for posting results 139 keepCheckouts bool // don't delete source & build checkouts after testing 140 dryRun bool // don't post any reviews 141 maxProcMemory uint64 // max virtual memory for child processes 142 dailyNow bool // start with a daily run 143 dailyOnly bool // run only the daily run 144 dailyChange string // Change hash to use for daily pass, HEAD if not provided 145 priority string // Prioritize a single change with the given id 146} 147 148// getToolchain returns the LLVM toolchain, possibly downloading and 149// decompressing it if it wasn't found in the cache directory. 150func getToolchain(tarExe, cacheRoot string) (*llvm.Toolchain, error) { 151 path := filepath.Join(cacheRoot, "llvm") 152 153 if toolchain := llvm.Search(path).Find(llvmVersion); toolchain != nil { 154 return toolchain, nil 155 } 156 157 // LLVM toolchain may have been updated, remove the directory if it exists. 158 os.RemoveAll(path) 159 160 log.Printf("Downloading LLVM %v toolchain...\n", llvmVersion) 161 tar, err := llvmVersion.Download() 162 if err != nil { 163 return nil, fmt.Errorf("Couldn't download LLVM %v: %v", llvmVersion, err) 164 } 165 166 tarFile := filepath.Join(cacheRoot, "llvm.tar.xz") 167 if err := ioutil.WriteFile(tarFile, tar, 0666); err != nil { 168 return nil, fmt.Errorf("Couldn't write '%v': %v", tarFile, err) 169 } 170 defer os.Remove(tarFile) 171 172 log.Printf("Decompressing LLVM %v toolchain...\n", llvmVersion) 173 target := filepath.Join(cacheRoot, "llvm-tmp") 174 os.MkdirAll(target, 0755) 175 defer os.RemoveAll(target) 176 if err := exec.Command(tarExe, "-xf", tarFile, "-C", target).Run(); err != nil { 177 return nil, fmt.Errorf("Couldn't decompress LLVM tar download: %v", err) 178 } 179 180 // The tar, once decompressed, holds a single root directory with a name 181 // starting with 'clang+llvm'. Move this to path. 182 files, err := filepath.Glob(filepath.Join(target, "*")) 183 if err != nil { 184 return nil, fmt.Errorf("Couldn't glob decompressed files: %v", err) 185 } 186 if len(files) != 1 || !util.IsDir(files[0]) { 187 return nil, fmt.Errorf("Unexpected decompressed files: %+v", files) 188 } 189 if err := os.Rename(files[0], path); err != nil { 190 return nil, fmt.Errorf("Couldn't move %v to %v", files[0], path) 191 } 192 193 // We should now have everything in the right place. 194 toolchain := llvm.Search(path).Find(llvmVersion) 195 if toolchain == nil { 196 return nil, fmt.Errorf("Couldn't find LLVM toolchain after downloading") 197 } 198 199 return toolchain, nil 200} 201 202// toolchainEnv() returns the environment variables for executing CMake commands. 203func (r *regres) toolchainEnv() []string { 204 return append([]string{ 205 "CC=" + r.toolchain.Clang(), 206 "CXX=" + r.toolchain.ClangXX(), 207 }, os.Environ()...) 208} 209 210// resolveDirs ensures that the necessary directories used can be found, and 211// expands them to absolute paths. 212func (r *regres) resolveDirs() error { 213 allDirs := []*string{ 214 &r.cacheRoot, 215 } 216 217 for _, path := range allDirs { 218 abs, err := filepath.Abs(*path) 219 if err != nil { 220 return cause.Wrap(err, "Couldn't find path '%v'", *path) 221 } 222 *path = abs 223 } 224 225 if err := os.MkdirAll(r.cacheRoot, 0777); err != nil { 226 return cause.Wrap(err, "Couldn't create cache root directory") 227 } 228 229 for _, path := range allDirs { 230 if !util.IsDir(*path) { 231 return fmt.Errorf("Couldn't find path '%v'", *path) 232 } 233 } 234 235 return nil 236} 237 238// resolveExes resolves all external executables used by regres. 239func (r *regres) resolveExes() error { 240 type exe struct { 241 name string 242 path *string 243 } 244 for _, e := range []exe{ 245 {"cmake", &r.cmake}, 246 {"make", &r.make}, 247 {"python", &r.python}, 248 {"tar", &r.tar}, 249 } { 250 path, err := exec.LookPath(e.name) 251 if err != nil { 252 return cause.Wrap(err, "Couldn't find path to %s", e.name) 253 } 254 *e.path = path 255 } 256 return nil 257} 258 259// run performs the main processing loop for the regress tool. It: 260// * Scans for open and recently updated changes in gerrit using queryChanges() 261// and changeInfo.update(). 262// * Builds the most recent patchset and the commit's parent CL using 263// r.newTest(<hash>).lazyRun(). 264// * Compares the results of the tests using compare(). 265// * Posts the results of the compare to gerrit as a review. 266// * Repeats the above steps until the process is interrupted. 267func (r *regres) run() error { 268 if err := r.resolveExes(); err != nil { 269 return cause.Wrap(err, "Couldn't resolve all exes") 270 } 271 272 if err := r.resolveDirs(); err != nil { 273 return cause.Wrap(err, "Couldn't resolve all directories") 274 } 275 276 toolchain, err := getToolchain(r.tar, r.cacheRoot) 277 if err != nil { 278 return cause.Wrap(err, "Couldn't download LLVM toolchain") 279 } 280 r.toolchain = toolchain 281 282 client, err := gerrit.NewClient(gerritURL, nil) 283 if err != nil { 284 return cause.Wrap(err, "Couldn't create gerrit client") 285 } 286 if r.gerritUser != "" { 287 client.Authentication.SetBasicAuth(r.gerritUser, r.gerritPass) 288 } 289 290 changes := map[string]*changeInfo{} // Change ID -> changeInfo 291 lastUpdatedTestLists := toDate(time.Now()) 292 lastQueriedChanges := time.Time{} 293 294 if r.dailyNow || r.dailyOnly { 295 lastUpdatedTestLists = date{} 296 } 297 298 for { 299 if now := time.Now(); toDate(now) != lastUpdatedTestLists { 300 lastUpdatedTestLists = toDate(now) 301 if err := r.runDaily(client, backendLLVM, false); err != nil { 302 log.Println(err.Error()) 303 } 304 if err := r.runDaily(client, backendSubzero, true); err != nil { 305 log.Println(err.Error()) 306 } 307 } 308 309 if r.dailyOnly { 310 log.Println("Daily finished with --dailyonly. Stopping") 311 return nil 312 } 313 314 // Update list of tracked changes. 315 if time.Since(lastQueriedChanges) > changeQueryFrequency { 316 lastQueriedChanges = time.Now() 317 if err := queryChanges(client, changes); err != nil { 318 log.Println(err.Error()) 319 } 320 } 321 322 // Update change info. 323 for _, change := range changes { 324 if time.Since(change.lastUpdated) > changeUpdateFrequency { 325 change.lastUpdated = time.Now() 326 err := change.update(client) 327 if err != nil { 328 log.Println(cause.Wrap(err, "Couldn't update info for change '%s'", change.id)) 329 } 330 } 331 } 332 333 for _, c := range changes { 334 if c.pending && r.priority == c.id { 335 log.Printf("Prioritizing change '%s'\n", c.id) 336 c.priority = 1e6 337 } 338 } 339 340 // Find the change with the highest priority. 341 var change *changeInfo 342 numPending := 0 343 for _, c := range changes { 344 if c.pending { 345 numPending++ 346 if change == nil || c.priority > change.priority { 347 change = c 348 } 349 } 350 } 351 352 if change == nil { 353 // Everything up to date. Take a break. 354 log.Println("Nothing to do. Sleeping") 355 time.Sleep(time.Minute) 356 continue 357 } 358 359 log.Printf("%d changes queued for testing\n", numPending) 360 361 log.Printf("Testing change '%s'\n", change.id) 362 363 // Test the latest patchset in the change, diff against parent change. 364 msg, alert, err := r.test(change) 365 if err != nil { 366 log.Println(cause.Wrap(err, "Failed to test changelist '%s'", change.latest)) 367 time.Sleep(time.Minute) 368 change.pending = false 369 continue 370 } 371 372 // Always include the reportHeader in the message. 373 // changeInfo.update() uses this header to detect whether a patchset has 374 // already got a test result. 375 msg = reportHeader + "\n\n" + msg 376 377 // Limit the message length to prevent '400 Bad Request' response. 378 maxMsgLength := 16000 379 if len(msg) > maxMsgLength { 380 trunc := " [truncated]\n" 381 msg = msg[0:maxMsgLength-len(trunc)] + trunc 382 } 383 384 if r.dryRun { 385 log.Printf("DRY RUN: add review to change '%v':\n%v\n", change.id, msg) 386 } else { 387 log.Printf("Posting review to '%s'\n", change.id) 388 notify := "OWNER" 389 if alert { 390 notify = "OWNER_REVIEWERS" 391 } 392 _, _, err = client.Changes.SetReview(change.id, change.latest.String(), &gerrit.ReviewInput{ 393 Message: msg, 394 Tag: "autogenerated:regress", 395 Notify: notify, 396 }) 397 if err != nil { 398 return cause.Wrap(err, "Failed to post comments on change '%s'", change.id) 399 } 400 } 401 change.pending = false 402 } 403} 404 405func (r *regres) test(change *changeInfo) (string, bool, error) { 406 latest := r.newTest(change.latest) 407 defer latest.cleanup() 408 409 if err := latest.checkout(); err != nil { 410 return "", true, cause.Wrap(err, "Failed to checkout '%s'", change.latest) 411 } 412 413 deqpBuild, err := r.getOrBuildDEQP(latest) 414 if err != nil { 415 return "", true, cause.Wrap(err, "Failed to build dEQP '%v' for change", change.id) 416 } 417 418 log.Printf("Testing latest patchset for change '%s'\n", change.id) 419 latestResults, testlists, err := r.testLatest(change, latest, deqpBuild) 420 if err != nil { 421 return "", true, cause.Wrap(err, "Failed to test latest change of '%v'", change.id) 422 } 423 424 log.Printf("Testing parent of change '%s'\n", change.id) 425 parentResults, err := r.testParent(change, testlists, deqpBuild) 426 if err != nil { 427 return "", true, cause.Wrap(err, "Failed to test parent change of '%v'", change.id) 428 } 429 430 log.Println("Comparing latest patchset's results with parent") 431 msg, alert := compare(parentResults, latestResults) 432 433 return msg, alert, nil 434} 435 436type deqpBuild struct { 437 path string // path to deqp directory 438 hash string // hash of the deqp config 439} 440 441func (r *regres) getOrBuildDEQP(test *test) (deqpBuild, error) { 442 checkoutDir := test.checkoutDir 443 if p := path.Join(checkoutDir, deqpConfigRelPath); !util.IsFile(p) { 444 checkoutDir, _ = os.Getwd() 445 log.Printf("Couldn't open dEQP config file from change (%v), falling back to internal version\n", p) 446 } else { 447 log.Println("Using dEQP config file from change") 448 } 449 file, err := os.Open(path.Join(checkoutDir, deqpConfigRelPath)) 450 if err != nil { 451 return deqpBuild{}, cause.Wrap(err, "Couldn't open dEQP config file") 452 } 453 defer file.Close() 454 455 cfg := struct { 456 Remote string `json:"remote"` 457 Branch string `json:"branch"` 458 SHA string `json:"sha"` 459 Patches []string `json:"patches"` 460 }{} 461 if err := json.NewDecoder(file).Decode(&cfg); err != nil { 462 return deqpBuild{}, cause.Wrap(err, "Couldn't parse %s", deqpConfigRelPath) 463 } 464 465 hasher := sha1.New() 466 if err := json.NewEncoder(hasher).Encode(&cfg); err != nil { 467 return deqpBuild{}, cause.Wrap(err, "Couldn't re-encode %s", deqpConfigRelPath) 468 } 469 hash := hex.EncodeToString(hasher.Sum(nil)) 470 cacheDir := path.Join(r.cacheRoot, "deqp", hash) 471 buildDir := path.Join(cacheDir, "build") 472 if !util.IsDir(cacheDir) { 473 if err := os.MkdirAll(cacheDir, 0777); err != nil { 474 return deqpBuild{}, cause.Wrap(err, "Couldn't make deqp cache directory '%s'", cacheDir) 475 } 476 477 success := false 478 defer func() { 479 if !success { 480 os.RemoveAll(cacheDir) 481 } 482 }() 483 484 if cfg.Branch != "" { 485 // If a branch is specified, then fetch the branch then checkout the 486 // commit by SHA. This is a workaround for git repos that error when 487 // attempting to directly checkout a remote commit. 488 log.Printf("Checking out deqp %v branch %v into %v\n", cfg.Remote, cfg.Branch, cacheDir) 489 if err := git.CheckoutRemoteBranch(cacheDir, cfg.Remote, cfg.Branch); err != nil { 490 return deqpBuild{}, cause.Wrap(err, "Couldn't checkout deqp branch %v @ %v", cfg.Remote, cfg.Branch) 491 } 492 log.Printf("Checking out deqp %v commit %v \n", cfg.Remote, cfg.SHA) 493 if err := git.CheckoutCommit(cacheDir, git.ParseHash(cfg.SHA)); err != nil { 494 return deqpBuild{}, cause.Wrap(err, "Couldn't checkout deqp commit %v @ %v", cfg.Remote, cfg.SHA) 495 } 496 } else { 497 log.Printf("Checking out deqp %v @ %v into %v\n", cfg.Remote, cfg.SHA, cacheDir) 498 if err := git.CheckoutRemoteCommit(cacheDir, cfg.Remote, git.ParseHash(cfg.SHA)); err != nil { 499 return deqpBuild{}, cause.Wrap(err, "Couldn't checkout deqp commit %v @ %v", cfg.Remote, cfg.SHA) 500 } 501 } 502 503 log.Println("Fetching deqp dependencies") 504 if err := shell.Shell(buildTimeout, r.python, cacheDir, "external/fetch_sources.py"); err != nil { 505 return deqpBuild{}, cause.Wrap(err, "Couldn't fetch deqp sources %v @ %v", cfg.Remote, cfg.SHA) 506 } 507 508 log.Println("Applying deqp patches") 509 for _, patch := range cfg.Patches { 510 fullPath := path.Join(checkoutDir, patch) 511 if err := git.Apply(cacheDir, fullPath); err != nil { 512 return deqpBuild{}, cause.Wrap(err, "Couldn't apply deqp patch %v for %v @ %v", patch, cfg.Remote, cfg.SHA) 513 } 514 } 515 516 log.Printf("Building deqp into %v\n", buildDir) 517 if err := os.MkdirAll(buildDir, 0777); err != nil { 518 return deqpBuild{}, cause.Wrap(err, "Couldn't make deqp build directory '%v'", buildDir) 519 } 520 521 if err := shell.Shell(buildTimeout, r.cmake, buildDir, 522 "-DDEQP_TARGET=default", 523 "-DCMAKE_BUILD_TYPE=Release", 524 ".."); err != nil { 525 return deqpBuild{}, cause.Wrap(err, "Couldn't generate build rules for deqp %v @ %v", cfg.Remote, cfg.SHA) 526 } 527 528 if err := shell.Shell(buildTimeout, r.make, buildDir, 529 fmt.Sprintf("-j%d", runtime.NumCPU()), 530 "deqp-vk"); err != nil { 531 return deqpBuild{}, cause.Wrap(err, "Couldn't build deqp %v @ %v", cfg.Remote, cfg.SHA) 532 } 533 534 success = true 535 } 536 537 return deqpBuild{ 538 path: cacheDir, 539 hash: hash, 540 }, nil 541} 542 543var additionalTestsRE = regexp.MustCompile(`\n\s*Test[s]?:\s*([^\s]+)[^\n]*`) 544 545func (r *regres) testLatest(change *changeInfo, test *test, d deqpBuild) (*deqp.Results, testlist.Lists, error) { 546 // Get the test results for the latest patchset in the change. 547 testlists, err := test.loadTestLists(ciTestListRelPath) 548 if err != nil { 549 return nil, nil, cause.Wrap(err, "Failed to load '%s'", change.latest) 550 } 551 552 if matches := additionalTestsRE.FindAllStringSubmatch(change.commitMessage, -1); len(matches) > 0 { 553 log.Println("Change description contains additional test patterns") 554 555 // Change specifies additional tests to try. Load the full test list. 556 fullTestLists, err := test.loadTestLists(fullTestListRelPath) 557 if err != nil { 558 return nil, nil, cause.Wrap(err, "Failed to load '%s'", change.latest) 559 } 560 561 // Add any tests in the full list that match the pattern to the list to test. 562 for _, match := range matches { 563 if len(match) > 1 { 564 pattern := match[1] 565 log.Printf("Adding custom tests with pattern '%s'\n", pattern) 566 filtered := fullTestLists.Filter(func(name string) bool { 567 ok, _ := filepath.Match(pattern, name) 568 return ok 569 }) 570 testlists = append(testlists, filtered...) 571 } 572 } 573 } 574 575 cachePath := test.resultsCachePath(testlists, d) 576 577 if results, err := deqp.LoadResults(cachePath); err == nil { 578 return results, testlists, nil // Use cached results 579 } 580 581 // Build the change and test it. 582 results := test.buildAndRun(testlists, d) 583 584 // Cache the results for future tests 585 if err := results.Save(cachePath); err != nil { 586 log.Printf("Warning: Couldn't save results of test to '%v'\n", cachePath) 587 } 588 589 return results, testlists, nil 590} 591 592func (r *regres) testParent(change *changeInfo, testlists testlist.Lists, d deqpBuild) (*deqp.Results, error) { 593 // Get the test results for the changes's parent changelist. 594 test := r.newTest(change.parent) 595 defer test.cleanup() 596 597 cachePath := test.resultsCachePath(testlists, d) 598 599 if results, err := deqp.LoadResults(cachePath); err == nil { 600 return results, nil // Use cached results 601 } 602 603 // Couldn't load cached results. Have to build them. 604 if err := test.checkout(); err != nil { 605 return nil, cause.Wrap(err, "Failed to checkout '%s'", change.parent) 606 } 607 608 // Build the parent change and test it. 609 results := test.buildAndRun(testlists, d) 610 611 // Store the results of the parent change to the cache. 612 if err := results.Save(cachePath); err != nil { 613 log.Printf("Warning: Couldn't save results of test to '%v'\n", cachePath) 614 } 615 616 return results, nil 617} 618 619// runDaily runs a full deqp run on the HEAD change, posting the results to a 620// new or existing gerrit change. If genCov is true, then coverage 621// information will be generated for the run, and commiteed to the 622// coverageBranch. 623func (r *regres) runDaily(client *gerrit.Client, reactorBackend reactorBackend, genCov bool) error { 624 // TODO(b/152192800): Generating coverage data is currently broken. 625 genCov = false 626 627 log.Printf("Updating test lists (Backend: %v)\n", reactorBackend) 628 629 if genCov { 630 if r.githubUser == "" { 631 log.Println("--gh-user not specified and SS_GITHUB_USER not set. Disabling code coverage generation") 632 genCov = false 633 } else if r.githubPass == "" { 634 log.Println("--gh-pass not specified and SS_GITHUB_PASS not set. Disabling code coverage generation") 635 genCov = false 636 } 637 } 638 639 dailyHash := git.Hash{} 640 if r.dailyChange == "" { 641 headHash, err := git.FetchRefHash("HEAD", gitURL) 642 if err != nil { 643 return cause.Wrap(err, "Could not get hash of master HEAD") 644 } 645 dailyHash = headHash 646 } else { 647 dailyHash = git.ParseHash(r.dailyChange) 648 } 649 650 return r.runDailyTest(dailyHash, reactorBackend, genCov, 651 func(test *test, testLists testlist.Lists, results *deqp.Results) error { 652 errs := []error{} 653 654 if err := r.postDailyResults(client, test, testLists, results, reactorBackend, dailyHash); err != nil { 655 errs = append(errs, err) 656 } 657 658 if genCov { 659 if err := r.postCoverageResults(results.Coverage, dailyHash); err != nil { 660 errs = append(errs, err) 661 } 662 } 663 664 return cause.Merge(errs...) 665 }) 666} 667 668// runDailyTest performs the full deqp run on the HEAD change, calling 669// withResults with the test results. 670func (r *regres) runDailyTest(dailyHash git.Hash, reactorBackend reactorBackend, genCov bool, withResults func(*test, testlist.Lists, *deqp.Results) error) error { 671 // Get the full test results. 672 test := r.newTest(dailyHash).setReactorBackend(reactorBackend) 673 defer test.cleanup() 674 675 // Always need to checkout the change. 676 if err := test.checkout(); err != nil { 677 return cause.Wrap(err, "Failed to checkout '%s'", dailyHash) 678 } 679 680 d, err := r.getOrBuildDEQP(test) 681 if err != nil { 682 return cause.Wrap(err, "Failed to build deqp for '%s'", dailyHash) 683 } 684 685 // Load the test lists. 686 testLists, err := test.loadTestLists(fullTestListRelPath) 687 if err != nil { 688 return cause.Wrap(err, "Failed to load full test lists for '%s'", dailyHash) 689 } 690 691 if genCov { 692 test.coverageEnv = &cov.Env{ 693 LLVM: *r.toolchain, 694 RootDir: test.checkoutDir, 695 ExePath: filepath.Join(test.buildDir, "libvk_swiftshader.so"), 696 TurboCov: filepath.Join(test.buildDir, "turbo-cov"), 697 } 698 } 699 700 // Build the change. 701 if err := test.build(); err != nil { 702 return cause.Wrap(err, "Failed to build '%s'", dailyHash) 703 } 704 705 // Run the tests on the change. 706 results, err := test.run(testLists, d) 707 if err != nil { 708 return cause.Wrap(err, "Failed to test '%s'", dailyHash) 709 } 710 711 return withResults(test, testLists, results) 712} 713 714// copyFileIfDifferent copies src to dst if src doesn't exist or if there are differences 715// between the files. 716func copyFileIfDifferent(dst, src string) error { 717 srcFileInfo, err := os.Stat(src) 718 if err != nil { 719 return err 720 } 721 srcContents, err := os.ReadFile(src) 722 if err != nil { 723 return err 724 } 725 726 dstContents, err := os.ReadFile(dst) 727 if err != nil && !errors.Is(err, os.ErrNotExist) { 728 return err 729 } 730 731 if !bytes.Equal(srcContents, dstContents) { 732 if err := os.WriteFile(dst, srcContents, srcFileInfo.Mode()); err != nil { 733 return err 734 } 735 } 736 return nil 737} 738 739// updatedEQPFiles copies the lists of tests in dEQP to the same lists in 740// SwiftShader, and sets the SHA in deqp.json to the latest dEQP revision 741func (r *regres) updateLocalDeqpFiles(test *test) ([]string, error) { 742 out := []string{} 743 // Use getOrBuildDEQP as it'll prevent us from copying data from a revision of dEQP that has build errors. 744 deqpBuild, err := r.getOrBuildDEQP(test) 745 746 if err != nil { 747 return nil, cause.Wrap(err, "Failed to retrieve dEQP build information") 748 } 749 750 log.Println("Copying deqp's vulkan testlist to checkout %s", test.commit) 751 deqpTestlistDir := path.Join(deqpBuild.path, deqpTestLists) 752 swsTestlistDir := path.Join(test.checkoutDir, swsTestLists) 753 754 deqpDefault := path.Join(deqpTestlistDir, "vk-default.txt") 755 swsDefault := path.Join(swsTestlistDir, "vk-master.txt") 756 757 if err := copyFileIfDifferent(swsDefault, deqpDefault); err != nil { 758 return nil, cause.Wrap(err, "Failed to copy '%s' to '%s'", deqpDefault, swsDefault) 759 } 760 761 out = append(out, swsDefault) 762 763 files, err := ioutil.ReadDir(path.Join(deqpTestlistDir, "vk-default")) 764 if err != nil { 765 return nil, cause.Wrap(err, "Could not read files from %s/vk-default/", deqpTestlistDir) 766 } 767 768 for _, f := range files { 769 if f.IsDir() { 770 continue 771 } 772 773 swsFile := path.Join(swsTestlistDir, "vk-default", f.Name()) 774 deqpFile := path.Join(deqpTestlistDir, "vk-default", f.Name()) 775 776 if err := copyFileIfDifferent(swsFile, deqpFile); err != nil { 777 return nil, cause.Wrap(err, "Failed to copy '%s' to '%s'", deqpFile, swsFile) 778 } 779 out = append(out, swsFile) 780 } 781 782 // Update deqp.json 783 p := path.Join(test.checkoutDir, deqpConfigRelPath) 784 if !util.IsFile(p) { 785 return nil, fmt.Errorf("Failed to locate %s while trying to update the dEQP SHA", deqpConfigRelPath) 786 } 787 file, err := os.Open(p) 788 if err != nil { 789 return nil, cause.Wrap(err, "Couldn't open dEQP config file") 790 } 791 defer file.Close() 792 793 cfg := struct { 794 Remote string `json:"remote"` 795 Branch string `json:"branch"` 796 SHA string `json:"sha"` 797 Patches []string `json:"patches"` 798 }{} 799 if err := json.NewDecoder(file).Decode(&cfg); err != nil { 800 return nil, cause.Wrap(err, "Couldn't parse %s", deqpConfigRelPath) 801 } 802 803 hash, err := git.FetchRefHash("HEAD", cfg.Remote) 804 if err != nil { 805 return nil, cause.Wrap(err, "Failed to fetch dEQP ref") 806 } 807 cfg.SHA = hash.String() 808 log.Println("New dEQP revision: %s", cfg.SHA) 809 810 newFile, err := os.Create(p) 811 if err != nil { 812 return nil, cause.Wrap(err, "Failed to open %s for encoding", deqpConfigRelPath) 813 } 814 defer newFile.Close() 815 816 encoder := json.NewEncoder(newFile) 817 // Make the encoder create a new-line and space-based indents for each field 818 encoder.SetIndent("", " ") 819 if err := encoder.Encode(&cfg); err != nil { 820 return nil, cause.Wrap(err, "Failed to re-encode %s", deqpConfigRelPath) 821 } 822 out = append(out, p) 823 824 return out, nil 825} 826 827// postDailyResults posts the results of the daily full deqp run to gerrit as 828// a new change, or reusing an old, unsubmitted change. 829// This change contains the updated test lists, an updated deqp.json that 830// points to the latest dEQP commit, and updated dEQP test files, along with a 831// summary of the test results. 832func (r *regres) postDailyResults( 833 client *gerrit.Client, 834 test *test, 835 testLists testlist.Lists, 836 results *deqp.Results, 837 reactorBackend reactorBackend, 838 dailyHash git.Hash) error { 839 840 // Write out the test list status files. 841 filePaths, err := test.writeTestListsByStatus(testLists, results) 842 if err != nil { 843 return cause.Wrap(err, "Failed to write test lists by status") 844 } 845 846 newPaths, err := r.updateLocalDeqpFiles(test) 847 if err != nil { 848 return cause.Wrap(err, "Failed to update test lists from dEQP") 849 } 850 851 filePaths = append(filePaths, newPaths...) 852 853 // Stage all the updated test files. 854 for _, path := range filePaths { 855 log.Println("Staging", path) 856 if err := git.Add(test.checkoutDir, path); err != nil { 857 return err 858 } 859 } 860 861 log.Println("Checking for existing test list") 862 existingChange, err := r.findTestListChange(client) 863 if err != nil { 864 return err 865 } 866 867 commitMsg := strings.Builder{} 868 commitMsg.WriteString(consts.TestListUpdateCommitSubjectPrefix + dailyHash.String()[:8]) 869 commitMsg.WriteString("\n\nReactor backend: " + string(reactorBackend)) 870 if existingChange != nil { 871 // Reuse gerrit change ID if there's already a change up for review. 872 commitMsg.WriteString("\n\n") 873 commitMsg.WriteString("Change-Id: " + existingChange.ChangeID + "\n") 874 } 875 876 if err := git.Commit(test.checkoutDir, commitMsg.String(), git.CommitFlags{ 877 Name: "SwiftShader Regression Bot", 878 Email: r.gerritEmail, 879 }); err != nil { 880 return cause.Wrap(err, "Failed to commit test results") 881 } 882 883 if r.dryRun { 884 log.Printf("DRY RUN: post results for review") 885 } else { 886 log.Println("Pushing test results for review") 887 if err := git.Push(test.checkoutDir, gitURL, "HEAD", "refs/for/master", git.PushFlags{ 888 Username: r.gerritUser, 889 Password: r.gerritPass, 890 }); err != nil { 891 return cause.Wrap(err, "Failed to push test results for review") 892 } 893 log.Println("Test results posted for review") 894 } 895 896 // We've just pushed a new commit. Let's reset back to the parent commit 897 // (dailyHash), so that we can run runDaily again for another backend, 898 // and have it update the commit with the same change-id. 899 if err := git.CheckoutCommit(test.checkoutDir, dailyHash); err != nil { 900 return cause.Wrap(err, "Failed to checkout parent commit") 901 } 902 log.Println("Checked out parent commit") 903 904 change, err := r.findTestListChange(client) 905 if err != nil { 906 return err 907 } 908 909 if err := r.postMostCommonFailures(client, change, results); err != nil { 910 return err 911 } 912 913 return nil 914} 915 916func (r *regres) postCoverageResults(cov *cov.Tree, revision git.Hash) error { 917 log.Printf("Committing coverage for %v\n", revision.String()) 918 919 url := coverageURL 920 url = strings.ReplaceAll(url, "$USERNAME", r.githubUser) 921 url = strings.ReplaceAll(url, "$PASSWORD", r.githubPass) 922 923 dir := filepath.Join(r.cacheRoot, "coverage") 924 defer os.RemoveAll(dir) 925 if err := git.CheckoutRemoteBranch(dir, url, coverageBranch); err != nil { 926 return cause.Wrap(err, "Failed to checkout gh-pages branch") 927 } 928 929 filePath := filepath.Join(dir, "coverage.dat") 930 file, err := os.Create(filePath) 931 if err != nil { 932 return cause.Wrap(err, "Failed to create file '%s'", filePath) 933 } 934 defer file.Close() 935 936 if err := cov.Encode(revision.String(), file); err != nil { 937 return cause.Wrap(err, "Failed to encode coverage") 938 } 939 file.Close() 940 941 if err := git.Add(dir, filePath); err != nil { 942 return cause.Wrap(err, "Failed to git add '%s'", filePath) 943 } 944 945 shortHash := revision.String()[:8] 946 947 err = git.Commit(dir, "Update coverage data @ "+shortHash, git.CommitFlags{ 948 Name: "SwiftShader Regression Bot", 949 Email: r.gerritEmail, 950 }) 951 if err != nil { 952 return cause.Wrap(err, "Failed to git commit") 953 } 954 955 if !r.dryRun { 956 err = git.Push(dir, url, coverageBranch, coverageBranch, git.PushFlags{}) 957 if err != nil { 958 return cause.Wrap(err, "Failed to 'git push'") 959 } 960 log.Printf("Coverage for %v pushed to Github\n", shortHash) 961 } 962 963 return nil 964} 965 966// postMostCommonFailures posts the most common failure cases as a review 967// comment on the given change. 968func (r *regres) postMostCommonFailures(client *gerrit.Client, change *gerrit.ChangeInfo, results *deqp.Results) error { 969 const limit = 25 970 971 failures := commonFailures(results) 972 if len(failures) > limit { 973 failures = failures[:limit] 974 } 975 sb := strings.Builder{} 976 sb.WriteString(fmt.Sprintf("Top %v most common failures:\n", len(failures))) 977 for _, f := range failures { 978 lines := strings.Split(f.error, "\n") 979 if len(lines) == 1 { 980 line := lines[0] 981 if line != "" { 982 sb.WriteString(fmt.Sprintf(" • %d occurrences: %v: %v\n", f.count, f.status, line)) 983 } else { 984 sb.WriteString(fmt.Sprintf(" • %d occurrences: %v\n", f.count, f.status)) 985 } 986 } else { 987 sb.WriteString(fmt.Sprintf(" • %d occurrences: %v:\n", f.count, f.status)) 988 for _, l := range lines { 989 sb.WriteString(" > ") 990 sb.WriteString(l) 991 sb.WriteString("\n") 992 } 993 } 994 sb.WriteString(fmt.Sprintf(" Example test: %v\n", f.exampleTest)) 995 996 } 997 msg := sb.String() 998 999 if r.dryRun { 1000 log.Printf("DRY RUN: add most common failures to '%v':\n%v\n", change.ChangeID, msg) 1001 } else { 1002 log.Printf("Posting most common failures to '%s'\n", change.ChangeID) 1003 _, _, err := client.Changes.SetReview(change.ChangeID, change.CurrentRevision, &gerrit.ReviewInput{ 1004 Message: msg, 1005 Tag: "autogenerated:regress", 1006 }) 1007 if err != nil { 1008 return cause.Wrap(err, "Failed to post comments on change '%s'", change.ChangeID) 1009 } 1010 } 1011 return nil 1012} 1013 1014func (r *regres) findTestListChange(client *gerrit.Client) (*gerrit.ChangeInfo, error) { 1015 log.Println("Checking for existing test list change") 1016 changes, _, err := client.Changes.QueryChanges(&gerrit.QueryChangeOptions{ 1017 QueryOptions: gerrit.QueryOptions{ 1018 Query: []string{fmt.Sprintf(`status:open+owner:"%v"`, r.gerritEmail)}, 1019 Limit: 1, 1020 }, 1021 ChangeOptions: gerrit.ChangeOptions{ 1022 AdditionalFields: []string{"CURRENT_REVISION"}, 1023 }, 1024 }) 1025 if err != nil { 1026 return nil, cause.Wrap(err, "Failed to checking for existing test list") 1027 } 1028 if len(*changes) > 0 { 1029 // TODO: This currently assumes that only change changes from 1030 // gerritEmail are test lists updates. This may not always be true. 1031 return &(*changes)[0], nil 1032 } 1033 return nil, nil 1034} 1035 1036// changeInfo holds the important information about a single, open change in 1037// gerrit. 1038type changeInfo struct { 1039 id string // Gerrit change ID. 1040 pending bool // Is this change waiting a test for the latest patchset? 1041 priority int // Calculated priority based on Gerrit labels. 1042 latest git.Hash // Git hash of the latest patchset in the change. 1043 parent git.Hash // Git hash of the changelist this change is based on. 1044 lastUpdated time.Time // Time the change was last fetched. 1045 commitMessage string 1046} 1047 1048// queryChanges updates the changes map by querying gerrit for the latest open 1049// changes. 1050func queryChanges(client *gerrit.Client, changes map[string]*changeInfo) error { 1051 log.Println("Checking for latest changes") 1052 results, _, err := client.Changes.QueryChanges(&gerrit.QueryChangeOptions{ 1053 QueryOptions: gerrit.QueryOptions{ 1054 Query: []string{"status:open+-age:3d"}, 1055 Limit: 100, 1056 }, 1057 }) 1058 if err != nil { 1059 return cause.Wrap(err, "Failed to get list of changes") 1060 } 1061 1062 ids := map[string]bool{} 1063 for _, r := range *results { 1064 ids[r.ChangeID] = true 1065 } 1066 1067 // Add new changes 1068 for id := range ids { 1069 if _, found := changes[id]; !found { 1070 log.Printf("Tracking new change '%v'\n", id) 1071 changes[id] = &changeInfo{id: id} 1072 } 1073 } 1074 1075 // Remove old changes 1076 for id := range changes { 1077 if found := ids[id]; !found { 1078 log.Printf("Untracking change '%v'\n", id) 1079 delete(changes, id) 1080 } 1081 } 1082 1083 return nil 1084} 1085 1086// update queries gerrit for information about the given change. 1087func (c *changeInfo) update(client *gerrit.Client) error { 1088 change, _, err := client.Changes.GetChange(c.id, &gerrit.ChangeOptions{ 1089 AdditionalFields: []string{"CURRENT_REVISION", "CURRENT_COMMIT", "MESSAGES", "LABELS", "DETAILED_ACCOUNTS"}, 1090 }) 1091 if err != nil { 1092 return cause.Wrap(err, "Getting info for change '%s'", c.id) 1093 } 1094 1095 current, ok := change.Revisions[change.CurrentRevision] 1096 if !ok { 1097 return fmt.Errorf("Couldn't find current revision for change '%s'", c.id) 1098 } 1099 1100 if len(current.Commit.Parents) == 0 { 1101 return fmt.Errorf("Couldn't find current commit for change '%s' has no parents(?)", c.id) 1102 } 1103 1104 kokoroPresubmit := change.Labels["Kokoro-Presubmit"].Approved.AccountID != 0 1105 codeReviewScore := change.Labels["Code-Review"].Value 1106 codeReviewApproved := change.Labels["Code-Review"].Approved.AccountID != 0 1107 presubmitReady := change.Labels["Presubmit-Ready"].Approved.AccountID != 0 1108 verifiedScore := change.Labels["Verified"].Value 1109 1110 c.priority = 0 1111 if presubmitReady { 1112 c.priority += 10 1113 } 1114 c.priority += codeReviewScore 1115 if codeReviewApproved { 1116 c.priority += 2 1117 } 1118 if kokoroPresubmit { 1119 c.priority++ 1120 } 1121 1122 // Is the change from a Googler or reviewed by a Googler? 1123 canTest := strings.HasSuffix(current.Commit.Committer.Email, "@google.com") || 1124 strings.HasSuffix(change.Labels["Code-Review"].Approved.Email, "@google.com") || 1125 strings.HasSuffix(change.Labels["Code-Review"].Recommended.Email, "@google.com") || 1126 strings.HasSuffix(change.Labels["Presubmit-Ready"].Approved.Email, "@google.com") 1127 1128 // Don't test if the change has negative scores. 1129 if canTest { 1130 if codeReviewScore < 0 || verifiedScore < 0 { 1131 canTest = false 1132 } 1133 } 1134 1135 // Has the latest patchset already been tested? 1136 if canTest { 1137 for _, msg := range change.Messages { 1138 if msg.RevisionNumber == current.Number && 1139 strings.Contains(msg.Message, reportHeader) { 1140 canTest = false 1141 break 1142 } 1143 } 1144 } 1145 1146 c.pending = canTest 1147 c.latest = git.ParseHash(change.CurrentRevision) 1148 c.parent = git.ParseHash(current.Commit.Parents[0].Commit) 1149 c.commitMessage = current.Commit.Message 1150 1151 return nil 1152} 1153 1154func (r *regres) newTest(commit git.Hash) *test { 1155 checkoutDir := filepath.Join(r.cacheRoot, "checkout", commit.String()) 1156 resDir := filepath.Join(r.cacheRoot, "res", commit.String()) 1157 return &test{ 1158 r: r, 1159 commit: commit, 1160 checkoutDir: checkoutDir, 1161 resDir: resDir, 1162 buildDir: filepath.Join(checkoutDir, "build"), 1163 reactorBackend: backendSubzero, 1164 } 1165} 1166 1167func (t *test) setReactorBackend(reactorBackend reactorBackend) *test { 1168 t.reactorBackend = reactorBackend 1169 return t 1170} 1171 1172type reactorBackend string 1173 1174const ( 1175 backendLLVM reactorBackend = "LLVM" 1176 backendSubzero reactorBackend = "Subzero" 1177) 1178 1179type test struct { 1180 r *regres 1181 commit git.Hash // hash of the commit to test 1182 checkoutDir string // directory for the SwiftShader checkout 1183 resDir string // directory for the test results 1184 buildDir string // directory for SwiftShader build 1185 toolchain llvm.Toolchain // the toolchain used for building 1186 reactorBackend reactorBackend // backend for SwiftShader build 1187 coverageEnv *cov.Env // coverage generation environment (optional). 1188} 1189 1190// cleanup removes any temporary files used by the test. 1191func (t *test) cleanup() { 1192 if t.checkoutDir != "" && !t.r.keepCheckouts { 1193 os.RemoveAll(t.checkoutDir) 1194 } 1195} 1196 1197// checkout clones the test's source commit into t.src. 1198func (t *test) checkout() error { 1199 if util.IsDir(t.checkoutDir) && t.r.keepCheckouts { 1200 log.Printf("Reusing source cache for commit '%s'\n", t.commit) 1201 return nil 1202 } 1203 log.Printf("Checking out '%s'\n", t.commit) 1204 os.RemoveAll(t.checkoutDir) 1205 if err := git.CheckoutRemoteCommit(t.checkoutDir, gitURL, t.commit); err != nil { 1206 return cause.Wrap(err, "Checking out commit '%s'", t.commit) 1207 } 1208 log.Printf("Checked out commit '%s'\n", t.commit) 1209 return nil 1210} 1211 1212// buildAndRun calls t.build() followed by t.run(). Errors are logged and 1213// reported in the returned deqprun.Results.Error field. 1214func (t *test) buildAndRun(testLists testlist.Lists, d deqpBuild) *deqp.Results { 1215 // Build the parent change. 1216 if err := t.build(); err != nil { 1217 msg := fmt.Sprintf("Failed to build '%s'", t.commit) 1218 log.Println(cause.Wrap(err, msg)) 1219 return &deqp.Results{Error: msg} 1220 } 1221 1222 // Run the tests on the parent change. 1223 results, err := t.run(testLists, d) 1224 if err != nil { 1225 msg := fmt.Sprintf("Failed to test change '%s'", t.commit) 1226 log.Println(cause.Wrap(err, msg)) 1227 return &deqp.Results{Error: msg} 1228 } 1229 1230 return results 1231} 1232 1233// build builds the SwiftShader source into t.buildDir. 1234func (t *test) build() error { 1235 log.Printf("Building '%s'\n", t.commit) 1236 1237 if err := os.MkdirAll(t.buildDir, 0777); err != nil { 1238 return cause.Wrap(err, "Failed to create build directory") 1239 } 1240 1241 args := []string{ 1242 `..`, 1243 `-DCMAKE_BUILD_TYPE=Release`, 1244 `-DSWIFTSHADER_DCHECK_ALWAYS_ON=1`, 1245 `-DREACTOR_VERIFY_LLVM_IR=1`, 1246 `-DREACTOR_BACKEND=` + string(t.reactorBackend), 1247 `-DSWIFTSHADER_LLVM_VERSION=10.0`, 1248 `-DSWIFTSHADER_WARNINGS_AS_ERRORS=0`, 1249 } 1250 1251 if t.coverageEnv != nil { 1252 args = append(args, "-DSWIFTSHADER_EMIT_COVERAGE=1") 1253 } 1254 1255 if err := shell.Env(buildTimeout, t.r.cmake, t.buildDir, t.r.toolchainEnv(), args...); err != nil { 1256 return err 1257 } 1258 1259 if err := shell.Shell(buildTimeout, t.r.make, t.buildDir, fmt.Sprintf("-j%d", runtime.NumCPU())); err != nil { 1260 return err 1261 } 1262 1263 return nil 1264} 1265 1266func (t *test) run(testLists testlist.Lists, d deqpBuild) (*deqp.Results, error) { 1267 log.Printf("Running tests for '%s'\n", t.commit) 1268 1269 swiftshaderICDSo := filepath.Join(t.buildDir, "libvk_swiftshader.so") 1270 if !util.IsFile(swiftshaderICDSo) { 1271 return nil, fmt.Errorf("Couldn't find '%s'", swiftshaderICDSo) 1272 } 1273 1274 swiftshaderICDJSON := filepath.Join(t.buildDir, "Linux", "vk_swiftshader_icd.json") 1275 if !util.IsFile(swiftshaderICDJSON) { 1276 return nil, fmt.Errorf("Couldn't find '%s'", swiftshaderICDJSON) 1277 } 1278 1279 if *limit != 0 { 1280 log.Printf("Limiting tests to %d\n", *limit) 1281 testLists = append(testlist.Lists{}, testLists...) 1282 for i := range testLists { 1283 testLists[i] = testLists[i].Limit(*limit) 1284 } 1285 } 1286 1287 // Directory for per-test small transient files, such as log files, 1288 // coverage output, etc. 1289 // TODO(bclayton): consider using tmpfs here. 1290 tempDir := filepath.Join(t.buildDir, "temp") 1291 os.MkdirAll(tempDir, 0777) 1292 1293 // Path to SwiftShader's libvulkan.so.1, which can be loaded directly by 1294 // dEQP without use of the Vulkan Loader. 1295 swiftshaderLibvulkanPath := filepath.Join(t.buildDir, "Linux") 1296 1297 config := deqp.Config{ 1298 ExeEgl: filepath.Join(d.path, "build", "modules", "egl", "deqp-egl"), 1299 ExeGles2: filepath.Join(d.path, "build", "modules", "gles2", "deqp-gles2"), 1300 ExeGles3: filepath.Join(d.path, "build", "modules", "gles3", "deqp-gles3"), 1301 ExeVulkan: filepath.Join(d.path, "build", "external", "vulkancts", "modules", "vulkan", "deqp-vk"), 1302 TempDir: tempDir, 1303 TestLists: testLists, 1304 Env: []string{ 1305 "LD_LIBRARY_PATH=" + os.Getenv("LD_LIBRARY_PATH") + ":" + swiftshaderLibvulkanPath, 1306 "VK_ICD_FILENAMES=" + swiftshaderICDJSON, 1307 "DISPLAY=" + os.Getenv("DISPLAY"), 1308 "LIBC_FATAL_STDERR_=1", // Put libc explosions into logs. 1309 }, 1310 LogReplacements: map[string]string{ 1311 t.checkoutDir: "<SwiftShader>", 1312 }, 1313 NumParallelTests: numParallelTests, 1314 TestTimeout: testTimeout, 1315 CoverageEnv: t.coverageEnv, 1316 } 1317 1318 return config.Run() 1319} 1320 1321func (t *test) writeTestListsByStatus(testLists testlist.Lists, results *deqp.Results) ([]string, error) { 1322 out := []string{} 1323 1324 for _, list := range testLists { 1325 files := map[testlist.Status]*os.File{} 1326 for _, status := range testlist.Statuses { 1327 path := testlist.FilePathWithStatus(filepath.Join(t.checkoutDir, list.File), status) 1328 dir := filepath.Dir(path) 1329 os.MkdirAll(dir, 0777) 1330 f, err := os.Create(path) 1331 if err != nil { 1332 return nil, cause.Wrap(err, "Couldn't create file '%v'", path) 1333 } 1334 defer f.Close() 1335 files[status] = f 1336 1337 out = append(out, path) 1338 } 1339 1340 for _, testName := range list.Tests { 1341 if r, found := results.Tests[testName]; found { 1342 fmt.Fprintln(files[r.Status], testName) 1343 } 1344 } 1345 } 1346 1347 return out, nil 1348} 1349 1350// resultsCachePath returns the path to the cache results file for the given 1351// test, testlists and deqpBuild. 1352func (t *test) resultsCachePath(testLists testlist.Lists, d deqpBuild) string { 1353 return filepath.Join(t.resDir, testLists.Hash(), d.hash) 1354} 1355 1356type testStatusAndError struct { 1357 status testlist.Status 1358 error string 1359} 1360 1361type commonFailure struct { 1362 count int 1363 testStatusAndError 1364 exampleTest string 1365} 1366 1367func commonFailures(results *deqp.Results) []commonFailure { 1368 failures := map[testStatusAndError]int{} 1369 examples := map[testStatusAndError]string{} 1370 for name, test := range results.Tests { 1371 if !test.Status.Failing() { 1372 continue 1373 } 1374 key := testStatusAndError{test.Status, test.Err} 1375 if count, ok := failures[key]; ok { 1376 failures[key] = count + 1 1377 } else { 1378 failures[key] = 1 1379 examples[key] = name 1380 } 1381 } 1382 out := make([]commonFailure, 0, len(failures)) 1383 for failure, count := range failures { 1384 out = append(out, commonFailure{count, failure, examples[failure]}) 1385 } 1386 sort.Slice(out, func(i, j int) bool { return out[i].count > out[j].count }) 1387 return out 1388} 1389 1390// compare returns a string describing all differences between two 1391// deqprun.Results, and a boolean indicating that this there are differences 1392// that are considered important. 1393// This string is used as the report message posted to the gerrit code review. 1394func compare(old, new *deqp.Results) (msg string, alert bool) { 1395 if old.Error != "" { 1396 return old.Error, false 1397 } 1398 if new.Error != "" { 1399 return new.Error, true 1400 } 1401 1402 oldStatusCounts, newStatusCounts := map[testlist.Status]int{}, map[testlist.Status]int{} 1403 totalTests := 0 1404 1405 broken, fixed, failing, removed, changed := []string{}, []string{}, []string{}, []string{}, []string{} 1406 1407 for test, new := range new.Tests { 1408 old, found := old.Tests[test] 1409 if !found { 1410 log.Printf("Test result for '%s' not found on old change\n", test) 1411 continue 1412 } 1413 switch { 1414 case !old.Status.Failing() && new.Status.Failing(): 1415 broken = append(broken, test) 1416 alert = true 1417 case !old.Status.Passing() && new.Status.Passing(): 1418 fixed = append(fixed, test) 1419 case old.Status != new.Status: 1420 changed = append(changed, test) 1421 alert = true 1422 case old.Status.Failing() && new.Status.Failing(): 1423 failing = append(failing, test) // Still broken 1424 alert = true 1425 } 1426 totalTests++ 1427 if found { 1428 oldStatusCounts[old.Status] = oldStatusCounts[old.Status] + 1 1429 } 1430 newStatusCounts[new.Status] = newStatusCounts[new.Status] + 1 1431 } 1432 1433 for test := range old.Tests { 1434 if _, found := new.Tests[test]; !found { 1435 removed = append(removed, test) 1436 } 1437 } 1438 1439 sb := strings.Builder{} 1440 1441 // list prints the list l to sb, truncating after a limit. 1442 list := func(l []string) { 1443 const max = 10 1444 for i, s := range l { 1445 sb.WriteString(" ") 1446 if i == max { 1447 sb.WriteString(fmt.Sprintf("> %d more\n", len(l)-i)) 1448 break 1449 } 1450 sb.WriteString(fmt.Sprintf("> %s", s)) 1451 if n, ok := new.Tests[s]; ok { 1452 if o, ok := old.Tests[s]; ok && n != o { 1453 sb.WriteString(fmt.Sprintf(" - [%s -> %s]", o.Status, n.Status)) 1454 } else { 1455 sb.WriteString(fmt.Sprintf(" - [%s]", n.Status)) 1456 } 1457 sb.WriteString("\n") 1458 for _, line := range strings.Split(n.Err, "\n") { 1459 if line != "" { 1460 sb.WriteString(fmt.Sprintf(" %v\n", line)) 1461 } 1462 } 1463 } else { 1464 sb.WriteString("\n") 1465 } 1466 } 1467 } 1468 1469 if n := len(broken); n > 0 { 1470 sort.Strings(broken) 1471 sb.WriteString(fmt.Sprintf("\n--- This change breaks %d tests: ---\n", n)) 1472 list(broken) 1473 } 1474 if n := len(fixed); n > 0 { 1475 sort.Strings(fixed) 1476 sb.WriteString(fmt.Sprintf("\n--- This change fixes %d tests: ---\n", n)) 1477 list(fixed) 1478 } 1479 if n := len(removed); n > 0 { 1480 sort.Strings(removed) 1481 sb.WriteString(fmt.Sprintf("\n--- This change removes %d tests: ---\n", n)) 1482 list(removed) 1483 } 1484 if n := len(changed); n > 0 { 1485 sort.Strings(changed) 1486 sb.WriteString(fmt.Sprintf("\n--- This change alters %d tests: ---\n", n)) 1487 list(changed) 1488 } 1489 1490 if len(broken) == 0 && len(fixed) == 0 && len(removed) == 0 && len(changed) == 0 { 1491 sb.WriteString(fmt.Sprintf("\n--- No change in test results ---\n")) 1492 } 1493 1494 sb.WriteString(fmt.Sprintf(" Total tests: %d\n", totalTests)) 1495 for _, s := range []struct { 1496 label string 1497 status testlist.Status 1498 }{ 1499 {" Pass", testlist.Pass}, 1500 {" Fail", testlist.Fail}, 1501 {" Timeout", testlist.Timeout}, 1502 {" UNIMPLEMENTED()", testlist.Unimplemented}, 1503 {" UNSUPPORTED()", testlist.Unsupported}, 1504 {" UNREACHABLE()", testlist.Unreachable}, 1505 {" ASSERT()", testlist.Assert}, 1506 {" ABORT()", testlist.Abort}, 1507 {" Crash", testlist.Crash}, 1508 {" Not Supported", testlist.NotSupported}, 1509 {"Compatibility Warning", testlist.CompatibilityWarning}, 1510 {" Quality Warning", testlist.QualityWarning}, 1511 } { 1512 old, new := oldStatusCounts[s.status], newStatusCounts[s.status] 1513 if old == 0 && new == 0 { 1514 continue 1515 } 1516 change := util.Percent64(int64(new-old), int64(old)) 1517 switch { 1518 case old == new: 1519 sb.WriteString(fmt.Sprintf("%s: %v\n", s.label, new)) 1520 case change == 0: 1521 sb.WriteString(fmt.Sprintf("%s: %v -> %v (%+d)\n", s.label, old, new, new-old)) 1522 default: 1523 sb.WriteString(fmt.Sprintf("%s: %v -> %v (%+d %+d%%)\n", s.label, old, new, new-old, change)) 1524 } 1525 } 1526 1527 if old, new := old.Duration, new.Duration; old != 0 && new != 0 { 1528 label := " Time taken" 1529 change := util.Percent64(int64(new-old), int64(old)) 1530 switch { 1531 case old == new: 1532 sb.WriteString(fmt.Sprintf("%s: %v\n", label, new)) 1533 case change == 0: 1534 sb.WriteString(fmt.Sprintf("%s: %v -> %v\n", label, old, new)) 1535 default: 1536 sb.WriteString(fmt.Sprintf("%s: %v -> %v (%+d%%)\n", label, old, new, change)) 1537 } 1538 } 1539 1540 type timingDiff struct { 1541 old time.Duration 1542 new time.Duration 1543 relDelta float64 1544 name string 1545 } 1546 1547 timingDiffs := []timingDiff{} 1548 for name, new := range new.Tests { 1549 if old, ok := old.Tests[name]; ok { 1550 old, new := old.TimeTaken, new.TimeTaken 1551 delta := new.Seconds() - old.Seconds() 1552 absDelta := math.Abs(delta) 1553 relDelta := delta / old.Seconds() 1554 if absDelta > 2.0 && math.Abs(relDelta) > 0.05 { // If change > ±2s and > than ±5% old time... 1555 timingDiffs = append(timingDiffs, timingDiff{ 1556 old: old, 1557 new: new, 1558 name: name, 1559 relDelta: relDelta, 1560 }) 1561 } 1562 } 1563 } 1564 if len(timingDiffs) > 0 { 1565 sb.WriteString(fmt.Sprintf("\n--- Test duration changes ---\n")) 1566 const limit = 10 1567 if len(timingDiffs) > limit { 1568 sort.Slice(timingDiffs, func(i, j int) bool { return math.Abs(timingDiffs[i].relDelta) > math.Abs(timingDiffs[j].relDelta) }) 1569 timingDiffs = timingDiffs[:limit] 1570 } 1571 sort.Slice(timingDiffs, func(i, j int) bool { return timingDiffs[i].relDelta < timingDiffs[j].relDelta }) 1572 for _, d := range timingDiffs { 1573 percent := util.Percent64(int64(d.new-d.old), int64(d.old)) 1574 sb.WriteString(fmt.Sprintf(" > %v: %v -> %v (%+d%%)\n", d.name, d.old, d.new, percent)) 1575 } 1576 } 1577 1578 return sb.String(), alert 1579} 1580 1581// loadTestLists loads the full test lists from the json file. 1582// The file is first searched at {t.srcDir}/{relPath} 1583// If this cannot be found, then the file is searched at the fallback path 1584// {CWD}/{relPath} 1585// This allows CLs to alter the list of tests to be run, as well as providing 1586// a default set. 1587func (t *test) loadTestLists(relPath string) (testlist.Lists, error) { 1588 // Seach for the test.json file in the checked out source directory. 1589 if path := filepath.Join(t.checkoutDir, relPath); util.IsFile(path) { 1590 log.Printf("Loading test list '%v' from commit\n", relPath) 1591 return testlist.Load(t.checkoutDir, path) 1592 } 1593 1594 // Not found there. Search locally. 1595 wd, err := os.Getwd() 1596 if err != nil { 1597 return testlist.Lists{}, cause.Wrap(err, "Couldn't get current working directory") 1598 } 1599 if path := filepath.Join(wd, relPath); util.IsFile(path) { 1600 log.Printf("Loading test list '%v' from regres\n", relPath) 1601 return testlist.Load(wd, relPath) 1602 } 1603 1604 return nil, errors.New("Couldn't find a test list file") 1605} 1606 1607type date struct { 1608 year int 1609 month time.Month 1610 day int 1611} 1612 1613func toDate(t time.Time) date { 1614 d := date{} 1615 d.year, d.month, d.day = t.Date() 1616 return d 1617} 1618