[tools] Perfmon tweaks

* Clean the local dawn repo before checkout. Fixes some git errors when going backwards in time.
* Profile backwards in time instead of forwards. We care more about the now than the past.
* Add additional logging.

Change-Id: I82495ab59909905d76bd7fb831d8571ecebb088e
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/163303
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
diff --git a/tools/src/cmd/perfmon/main.go b/tools/src/cmd/perfmon/main.go
index 84b24fd..4350bf0 100644
--- a/tools/src/cmd/perfmon/main.go
+++ b/tools/src/cmd/perfmon/main.go
@@ -409,21 +409,19 @@
 	if err != nil {
 		return nil, fmt.Errorf("failed to obtain dawn log:\n  %w", err)
 	}
+	log.Println(len(allChanges), "changes between", e.cfg.RootChange.String(), "and", latest.String())
 	changesWithBenchmarks, err := e.changesWithBenchmarks()
 	if err != nil {
 		return nil, fmt.Errorf("failed to gather changes with existing benchmarks:\n  %w", err)
 	}
+	log.Println(len(changesWithBenchmarks), "changes with existing benchmarks")
+
 	changesToBenchmark := make([]HashAndDesc, 0, len(allChanges))
 	for _, c := range allChanges {
 		if _, exists := changesWithBenchmarks[c.Hash]; !exists {
 			changesToBenchmark = append(changesToBenchmark, HashAndDesc{c.Hash, c.Subject})
 		}
 	}
-	// Reverse the order of changesToBenchmark, so that the oldest comes first.
-	for i := len(changesToBenchmark)/2 - 1; i >= 0; i-- {
-		j := len(changesToBenchmark) - 1 - i
-		changesToBenchmark[i], changesToBenchmark[j] = changesToBenchmark[j], changesToBenchmark[i]
-	}
 
 	return changesToBenchmark, nil
 }
@@ -1136,6 +1134,9 @@
 // Note: call fetch() to ensure that this is the latest change on the
 // branch.
 func checkout(hash git.Hash, repo *git.Repository) error {
+	if err := repo.Clean(nil); err != nil {
+		return fmt.Errorf("failed to clean repo '%v':\n  %w", hash, err)
+	}
 	if err := repo.Checkout(hash.String(), nil); err != nil {
 		return fmt.Errorf("failed to checkout '%v':\n  %w", hash, err)
 	}
diff --git a/tools/src/git/git.go b/tools/src/git/git.go
index 958f782..9de5e09 100644
--- a/tools/src/git/git.go
+++ b/tools/src/git/git.go
@@ -293,6 +293,23 @@
 	return ParseHash(out)
 }
 
+// Optional settings for Repository.Clean
+type CleanOptions struct {
+	// Timeout for the operation
+	Timeout time.Duration
+}
+
+// Clean deletes all the untracked files in the repo
+func (r Repository) Clean(opt *CleanOptions) error {
+	if opt == nil {
+		opt = &CleanOptions{}
+	}
+	if _, err := r.run(nil, opt.Timeout, "clean", "-fd"); err != nil {
+		return err
+	}
+	return nil
+}
+
 // Optional settings for Repository.Checkout
 type CheckoutOptions struct {
 	// Timeout for the operation