Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 1 | // Copyright 2020 Google Inc. 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 | package build |
| 16 | |
| 17 | // This file contains the functionality to upload data from one location to |
| 18 | // another. |
| 19 | |
| 20 | import ( |
| 21 | "io/ioutil" |
| 22 | "os" |
| 23 | "path/filepath" |
| 24 | "time" |
| 25 | |
Patrice Arruda | 7cc2074 | 2020-06-10 18:48:01 +0000 | [diff] [blame] | 26 | "android/soong/ui/metrics" |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 27 | "github.com/golang/protobuf/proto" |
| 28 | |
| 29 | upload_proto "android/soong/ui/metrics/upload_proto" |
| 30 | ) |
| 31 | |
| 32 | const ( |
Patrice Arruda | 92dc64f | 2020-11-16 16:29:16 -0800 | [diff] [blame^] | 33 | // Used to generate a raw protobuf file that contains information |
| 34 | // of the list of metrics files from host to destination storage. |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 35 | uploadPbFilename = ".uploader.pb" |
| 36 | ) |
| 37 | |
Patrice Arruda | 7cc2074 | 2020-06-10 18:48:01 +0000 | [diff] [blame] | 38 | var ( |
Patrice Arruda | 92dc64f | 2020-11-16 16:29:16 -0800 | [diff] [blame^] | 39 | // For testing purpose. |
| 40 | tmpDir = ioutil.TempDir |
Patrice Arruda | 7cc2074 | 2020-06-10 18:48:01 +0000 | [diff] [blame] | 41 | ) |
| 42 | |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 43 | // UploadMetrics uploads a set of metrics files to a server for analysis. An |
Patrice Arruda | 92dc64f | 2020-11-16 16:29:16 -0800 | [diff] [blame^] | 44 | // uploader full path is specified in ANDROID_ENABLE_METRICS_UPLOAD environment |
| 45 | // variable in order to upload the set of metrics files. The metrics files are |
| 46 | // first copied to a temporary directory and the uploader is then executed in |
| 47 | // the background to allow the user/system to continue working. Soong communicates |
| 48 | // to the uploader through the upload_proto raw protobuf file. |
Patrice Arruda | f445ba1 | 2020-07-28 17:49:01 +0000 | [diff] [blame] | 49 | func UploadMetrics(ctx Context, config Config, simpleOutput bool, buildStarted time.Time, files ...string) { |
Patrice Arruda | 7cc2074 | 2020-06-10 18:48:01 +0000 | [diff] [blame] | 50 | ctx.BeginTrace(metrics.RunSetupTool, "upload_metrics") |
| 51 | defer ctx.EndTrace() |
| 52 | |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 53 | uploader := config.MetricsUploaderApp() |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 54 | if uploader == "" { |
Patrice Arruda | 92dc64f | 2020-11-16 16:29:16 -0800 | [diff] [blame^] | 55 | // If the uploader path was not specified, no metrics shall be uploaded. |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 56 | return |
| 57 | } |
| 58 | |
Patrice Arruda | 92dc64f | 2020-11-16 16:29:16 -0800 | [diff] [blame^] | 59 | // Some files passed in to this function may not exist. For example, |
| 60 | // build errors protobuf file may not exist since the build was successful. |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 61 | var metricsFiles []string |
| 62 | for _, f := range files { |
| 63 | if _, err := os.Stat(f); err == nil { |
| 64 | metricsFiles = append(metricsFiles, f) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if len(metricsFiles) == 0 { |
| 69 | return |
| 70 | } |
| 71 | |
Patrice Arruda | 7cc2074 | 2020-06-10 18:48:01 +0000 | [diff] [blame] | 72 | // The temporary directory cannot be deleted as the metrics uploader is started |
| 73 | // in the background and requires to exist until the operation is done. The |
| 74 | // uploader can delete the directory as it is specified in the upload proto. |
Patrice Arruda | 92dc64f | 2020-11-16 16:29:16 -0800 | [diff] [blame^] | 75 | tmpDir, err := tmpDir("", "upload_metrics") |
Patrice Arruda | 7cc2074 | 2020-06-10 18:48:01 +0000 | [diff] [blame] | 76 | if err != nil { |
| 77 | ctx.Fatalf("failed to create a temporary directory to store the list of metrics files: %v\n", err) |
| 78 | } |
| 79 | |
| 80 | for i, src := range metricsFiles { |
| 81 | dst := filepath.Join(tmpDir, filepath.Base(src)) |
| 82 | if _, err := copyFile(src, dst); err != nil { |
| 83 | ctx.Fatalf("failed to copy %q to %q: %v\n", src, dst, err) |
| 84 | } |
| 85 | metricsFiles[i] = dst |
| 86 | } |
| 87 | |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 88 | // For platform builds, the branch and target name is hardcoded to specific |
| 89 | // values for later extraction of the metrics in the data metrics pipeline. |
| 90 | data, err := proto.Marshal(&upload_proto.Upload{ |
Patrice Arruda | 73c790f | 2020-07-13 23:01:18 +0000 | [diff] [blame] | 91 | CreationTimestampMs: proto.Uint64(uint64(buildStarted.UnixNano() / int64(time.Millisecond))), |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 92 | CompletionTimestampMs: proto.Uint64(uint64(time.Now().UnixNano() / int64(time.Millisecond))), |
| 93 | BranchName: proto.String("developer-metrics"), |
| 94 | TargetName: proto.String("platform-build-systems-metrics"), |
| 95 | MetricsFiles: metricsFiles, |
Patrice Arruda | 7cc2074 | 2020-06-10 18:48:01 +0000 | [diff] [blame] | 96 | DirectoriesToDelete: []string{tmpDir}, |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 97 | }) |
| 98 | if err != nil { |
| 99 | ctx.Fatalf("failed to marshal metrics upload proto buffer message: %v\n", err) |
| 100 | } |
| 101 | |
Patrice Arruda | 7cc2074 | 2020-06-10 18:48:01 +0000 | [diff] [blame] | 102 | pbFile := filepath.Join(tmpDir, uploadPbFilename) |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 103 | if err := ioutil.WriteFile(pbFile, data, 0644); err != nil { |
| 104 | ctx.Fatalf("failed to write the marshaled metrics upload protobuf to %q: %v\n", pbFile, err) |
| 105 | } |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 106 | |
Patrice Arruda | 7cc2074 | 2020-06-10 18:48:01 +0000 | [diff] [blame] | 107 | // Start the uploader in the background as it takes several milliseconds to start the uploader |
Patrice Arruda | 92dc64f | 2020-11-16 16:29:16 -0800 | [diff] [blame^] | 108 | // and prepare the metrics for upload. This affects small shell commands like "lunch". |
Patrice Arruda | 7cc2074 | 2020-06-10 18:48:01 +0000 | [diff] [blame] | 109 | cmd := Command(ctx, config, "upload metrics", uploader, "--upload-metrics", pbFile) |
Patrice Arruda | f445ba1 | 2020-07-28 17:49:01 +0000 | [diff] [blame] | 110 | if simpleOutput { |
Patrice Arruda | 7cc2074 | 2020-06-10 18:48:01 +0000 | [diff] [blame] | 111 | cmd.RunOrFatal() |
| 112 | } else { |
| 113 | cmd.RunAndStreamOrFatal() |
| 114 | } |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 115 | } |