Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 1 | // Copyright 2018 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 status |
| 16 | |
| 17 | import ( |
| 18 | "bufio" |
| 19 | "fmt" |
| 20 | "io" |
| 21 | "os" |
Spandan Das | 0506361 | 2021-06-25 01:39:04 +0000 | [diff] [blame] | 22 | "regexp" |
Jeongik Cha | 3622b34 | 2023-11-27 11:00:52 +0900 | [diff] [blame] | 23 | "runtime" |
Spandan Das | 0506361 | 2021-06-25 01:39:04 +0000 | [diff] [blame] | 24 | "strings" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 25 | "syscall" |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 26 | "time" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 27 | |
Dan Willemsen | 4591b64 | 2021-05-24 14:24:12 -0700 | [diff] [blame] | 28 | "google.golang.org/protobuf/proto" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 29 | |
| 30 | "android/soong/ui/logger" |
| 31 | "android/soong/ui/status/ninja_frontend" |
| 32 | ) |
| 33 | |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 34 | // NewNinjaReader reads the protobuf frontend format from ninja and translates it |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 35 | // into calls on the ToolStatus API. |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 36 | func NewNinjaReader(ctx logger.Logger, status ToolStatus, fifo string) *NinjaReader { |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 37 | os.Remove(fifo) |
| 38 | |
usta | fb67fd1 | 2022-08-19 19:26:00 -0400 | [diff] [blame] | 39 | if err := syscall.Mkfifo(fifo, 0666); err != nil { |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 40 | ctx.Fatalf("Failed to mkfifo(%q): %v", fifo, err) |
| 41 | } |
| 42 | |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 43 | n := &NinjaReader{ |
Colin Cross | b0b369c | 2023-08-30 17:32:17 -0700 | [diff] [blame] | 44 | status: status, |
| 45 | fifo: fifo, |
| 46 | forceClose: make(chan bool), |
| 47 | done: make(chan bool), |
| 48 | cancelOpen: make(chan bool), |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | go n.run() |
| 52 | |
| 53 | return n |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 56 | type NinjaReader struct { |
Colin Cross | b0b369c | 2023-08-30 17:32:17 -0700 | [diff] [blame] | 57 | status ToolStatus |
| 58 | fifo string |
| 59 | forceClose chan bool |
| 60 | done chan bool |
| 61 | cancelOpen chan bool |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | const NINJA_READER_CLOSE_TIMEOUT = 5 * time.Second |
| 65 | |
| 66 | // Close waits for NinjaReader to finish reading from the fifo, or 5 seconds. |
| 67 | func (n *NinjaReader) Close() { |
| 68 | // Signal the goroutine to stop if it is blocking opening the fifo. |
Colin Cross | b0b369c | 2023-08-30 17:32:17 -0700 | [diff] [blame] | 69 | close(n.cancelOpen) |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 70 | |
Colin Cross | b0b369c | 2023-08-30 17:32:17 -0700 | [diff] [blame] | 71 | // Ninja should already have exited or been killed, wait 5 seconds for the FIFO to be closed and any |
| 72 | // remaining messages to be processed through the NinjaReader.run goroutine. |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 73 | timeoutCh := time.After(NINJA_READER_CLOSE_TIMEOUT) |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 74 | select { |
| 75 | case <-n.done: |
Colin Cross | b0b369c | 2023-08-30 17:32:17 -0700 | [diff] [blame] | 76 | return |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 77 | case <-timeoutCh: |
Colin Cross | b0b369c | 2023-08-30 17:32:17 -0700 | [diff] [blame] | 78 | // Channel is not closed yet |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 79 | } |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 80 | |
Colin Cross | b0b369c | 2023-08-30 17:32:17 -0700 | [diff] [blame] | 81 | n.status.Error(fmt.Sprintf("ninja fifo didn't finish after %s", NINJA_READER_CLOSE_TIMEOUT.String())) |
| 82 | |
| 83 | // Force close the reader even if the FIFO didn't close. |
| 84 | close(n.forceClose) |
| 85 | |
| 86 | // Wait again for the reader thread to acknowledge the close before giving up and assuming it isn't going |
| 87 | // to send anything else. |
| 88 | timeoutCh = time.After(NINJA_READER_CLOSE_TIMEOUT) |
| 89 | select { |
| 90 | case <-n.done: |
| 91 | return |
| 92 | case <-timeoutCh: |
| 93 | // Channel is not closed yet |
| 94 | } |
| 95 | |
| 96 | n.status.Verbose(fmt.Sprintf("ninja fifo didn't finish even after force closing after %s", NINJA_READER_CLOSE_TIMEOUT.String())) |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | func (n *NinjaReader) run() { |
| 100 | defer close(n.done) |
| 101 | |
| 102 | // Opening the fifo can block forever if ninja never opens the write end, do it in a goroutine so this |
| 103 | // method can exit on cancel. |
| 104 | fileCh := make(chan *os.File) |
| 105 | go func() { |
| 106 | f, err := os.Open(n.fifo) |
| 107 | if err != nil { |
| 108 | n.status.Error(fmt.Sprintf("Failed to open fifo: %v", err)) |
| 109 | close(fileCh) |
| 110 | return |
| 111 | } |
| 112 | fileCh <- f |
| 113 | }() |
| 114 | |
| 115 | var f *os.File |
| 116 | |
| 117 | select { |
| 118 | case f = <-fileCh: |
| 119 | // Nothing |
Colin Cross | b0b369c | 2023-08-30 17:32:17 -0700 | [diff] [blame] | 120 | case <-n.cancelOpen: |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 121 | return |
| 122 | } |
| 123 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 124 | defer f.Close() |
| 125 | |
| 126 | r := bufio.NewReader(f) |
| 127 | |
| 128 | running := map[uint32]*Action{} |
| 129 | |
Colin Cross | b0b369c | 2023-08-30 17:32:17 -0700 | [diff] [blame] | 130 | msgChan := make(chan *ninja_frontend.Status) |
| 131 | |
| 132 | // Read from the ninja fifo and decode the protobuf in a goroutine so the main NinjaReader.run goroutine |
| 133 | // can listen |
| 134 | go func() { |
| 135 | defer close(msgChan) |
| 136 | for { |
| 137 | size, err := readVarInt(r) |
| 138 | if err != nil { |
| 139 | if err != io.EOF { |
| 140 | n.status.Error(fmt.Sprintf("Got error reading from ninja: %s", err)) |
| 141 | } |
| 142 | return |
| 143 | } |
| 144 | |
| 145 | buf := make([]byte, size) |
| 146 | _, err = io.ReadFull(r, buf) |
| 147 | if err != nil { |
| 148 | if err == io.EOF { |
| 149 | n.status.Print(fmt.Sprintf("Missing message of size %d from ninja\n", size)) |
| 150 | } else { |
| 151 | n.status.Error(fmt.Sprintf("Got error reading from ninja: %s", err)) |
| 152 | } |
| 153 | return |
| 154 | } |
| 155 | |
| 156 | msg := &ninja_frontend.Status{} |
| 157 | err = proto.Unmarshal(buf, msg) |
| 158 | if err != nil { |
| 159 | n.status.Print(fmt.Sprintf("Error reading message from ninja: %v", err)) |
| 160 | continue |
| 161 | } |
| 162 | |
| 163 | msgChan <- msg |
| 164 | } |
| 165 | }() |
| 166 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 167 | for { |
Colin Cross | b0b369c | 2023-08-30 17:32:17 -0700 | [diff] [blame] | 168 | var msg *ninja_frontend.Status |
| 169 | var msgOk bool |
| 170 | select { |
| 171 | case <-n.forceClose: |
| 172 | // Close() has been called, but the reader goroutine didn't get EOF after 5 seconds |
| 173 | break |
| 174 | case msg, msgOk = <-msgChan: |
| 175 | // msg is ready or closed |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Colin Cross | b0b369c | 2023-08-30 17:32:17 -0700 | [diff] [blame] | 178 | if !msgOk { |
| 179 | // msgChan is closed |
| 180 | break |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 181 | } |
Jeongik Cha | 3622b34 | 2023-11-27 11:00:52 +0900 | [diff] [blame] | 182 | |
| 183 | if msg.BuildStarted != nil { |
| 184 | parallelism := uint32(runtime.NumCPU()) |
| 185 | if msg.BuildStarted.GetParallelism() > 0 { |
| 186 | parallelism = msg.BuildStarted.GetParallelism() |
| 187 | } |
| 188 | // It is estimated from total time / parallelism assumming the build is packing enough. |
| 189 | estimatedDurationFromTotal := time.Duration(msg.BuildStarted.GetEstimatedTotalTime()/parallelism) * time.Millisecond |
| 190 | // It is estimated from critical path time which is useful for small size build. |
| 191 | estimatedDurationFromCriticalPath := time.Duration(msg.BuildStarted.GetCriticalPathTime()) * time.Millisecond |
| 192 | // Select the longer one. |
| 193 | estimatedDuration := max(estimatedDurationFromTotal, estimatedDurationFromCriticalPath) |
| 194 | |
| 195 | if estimatedDuration > 0 { |
| 196 | n.status.SetEstimatedTime(time.Now().Add(estimatedDuration)) |
Jeongik Cha | d74c914 | 2023-12-11 11:40:51 +0000 | [diff] [blame] | 197 | n.status.Verbose(fmt.Sprintf("parallelism: %d, estimated from total time: %s, critical path time: %s", |
Jeongik Cha | 3622b34 | 2023-11-27 11:00:52 +0900 | [diff] [blame] | 198 | parallelism, |
| 199 | estimatedDurationFromTotal, |
| 200 | estimatedDurationFromCriticalPath)) |
| 201 | |
| 202 | } |
| 203 | } |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 204 | if msg.TotalEdges != nil { |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 205 | n.status.SetTotalActions(int(msg.TotalEdges.GetTotalEdges())) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 206 | } |
| 207 | if msg.EdgeStarted != nil { |
| 208 | action := &Action{ |
Yu Liu | a58467a | 2024-03-05 01:12:19 +0000 | [diff] [blame] | 209 | Description: msg.EdgeStarted.GetDesc(), |
| 210 | Outputs: msg.EdgeStarted.Outputs, |
| 211 | Inputs: msg.EdgeStarted.Inputs, |
| 212 | Command: msg.EdgeStarted.GetCommand(), |
| 213 | ChangedInputs: msg.EdgeStarted.ChangedInputs, |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 214 | } |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 215 | n.status.StartAction(action) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 216 | running[msg.EdgeStarted.GetId()] = action |
| 217 | } |
| 218 | if msg.EdgeFinished != nil { |
| 219 | if started, ok := running[msg.EdgeFinished.GetId()]; ok { |
| 220 | delete(running, msg.EdgeFinished.GetId()) |
| 221 | |
| 222 | var err error |
| 223 | exitCode := int(msg.EdgeFinished.GetStatus()) |
| 224 | if exitCode != 0 { |
| 225 | err = fmt.Errorf("exited with code: %d", exitCode) |
| 226 | } |
| 227 | |
Spandan Das | 0506361 | 2021-06-25 01:39:04 +0000 | [diff] [blame] | 228 | outputWithErrorHint := errorHintGenerator.GetOutputWithErrorHint(msg.EdgeFinished.GetOutput(), exitCode) |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 229 | n.status.FinishAction(ActionResult{ |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 230 | Action: started, |
Spandan Das | 0506361 | 2021-06-25 01:39:04 +0000 | [diff] [blame] | 231 | Output: outputWithErrorHint, |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 232 | Error: err, |
Colin Cross | d888b6b | 2020-10-15 13:46:32 -0700 | [diff] [blame] | 233 | Stats: ActionResultStats{ |
| 234 | UserTime: msg.EdgeFinished.GetUserTime(), |
| 235 | SystemTime: msg.EdgeFinished.GetSystemTime(), |
| 236 | MaxRssKB: msg.EdgeFinished.GetMaxRssKb(), |
| 237 | MinorPageFaults: msg.EdgeFinished.GetMinorPageFaults(), |
| 238 | MajorPageFaults: msg.EdgeFinished.GetMajorPageFaults(), |
| 239 | IOInputKB: msg.EdgeFinished.GetIoInputKb(), |
| 240 | IOOutputKB: msg.EdgeFinished.GetIoOutputKb(), |
| 241 | VoluntaryContextSwitches: msg.EdgeFinished.GetVoluntaryContextSwitches(), |
| 242 | InvoluntaryContextSwitches: msg.EdgeFinished.GetInvoluntaryContextSwitches(), |
Dan Albert | e82234e | 2023-06-01 23:09:38 +0000 | [diff] [blame] | 243 | Tags: msg.EdgeFinished.GetTags(), |
Colin Cross | d888b6b | 2020-10-15 13:46:32 -0700 | [diff] [blame] | 244 | }, |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 245 | }) |
| 246 | } |
| 247 | } |
| 248 | if msg.Message != nil { |
| 249 | message := "ninja: " + msg.Message.GetMessage() |
| 250 | switch msg.Message.GetLevel() { |
| 251 | case ninja_frontend.Status_Message_INFO: |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 252 | n.status.Status(message) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 253 | case ninja_frontend.Status_Message_WARNING: |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 254 | n.status.Print("warning: " + message) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 255 | case ninja_frontend.Status_Message_ERROR: |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 256 | n.status.Error(message) |
Dan Willemsen | 0821822 | 2020-05-18 14:02:02 -0700 | [diff] [blame] | 257 | case ninja_frontend.Status_Message_DEBUG: |
| 258 | n.status.Verbose(message) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 259 | default: |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 260 | n.status.Print(message) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | if msg.BuildFinished != nil { |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 264 | n.status.Finish() |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | func readVarInt(r *bufio.Reader) (int, error) { |
| 270 | ret := 0 |
| 271 | shift := uint(0) |
| 272 | |
| 273 | for { |
| 274 | b, err := r.ReadByte() |
| 275 | if err != nil { |
| 276 | return 0, err |
| 277 | } |
| 278 | |
| 279 | ret += int(b&0x7f) << (shift * 7) |
| 280 | if b&0x80 == 0 { |
| 281 | break |
| 282 | } |
| 283 | shift += 1 |
| 284 | if shift > 4 { |
| 285 | return 0, fmt.Errorf("Expected varint32 length-delimited message") |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | return ret, nil |
| 290 | } |
Spandan Das | 0506361 | 2021-06-25 01:39:04 +0000 | [diff] [blame] | 291 | |
| 292 | // key is pattern in stdout/stderr |
| 293 | // value is error hint |
| 294 | var allErrorHints = map[string]string{ |
| 295 | "Read-only file system": `\nWrite to a read-only file system detected. Possible fixes include |
| 296 | 1. Generate file directly to out/ which is ReadWrite, #recommend solution |
| 297 | 2. BUILD_BROKEN_SRC_DIR_RW_ALLOWLIST := <my/path/1> <my/path/2> #discouraged, subset of source tree will be RW |
| 298 | 3. BUILD_BROKEN_SRC_DIR_IS_WRITABLE := true #highly discouraged, entire source tree will be RW |
| 299 | `, |
| 300 | } |
| 301 | var errorHintGenerator = *newErrorHintGenerator(allErrorHints) |
| 302 | |
| 303 | type ErrorHintGenerator struct { |
| 304 | allErrorHints map[string]string |
| 305 | allErrorHintPatternsCompiled *regexp.Regexp |
| 306 | } |
| 307 | |
| 308 | func newErrorHintGenerator(allErrorHints map[string]string) *ErrorHintGenerator { |
| 309 | var allErrorHintPatterns []string |
| 310 | for errorHintPattern, _ := range allErrorHints { |
| 311 | allErrorHintPatterns = append(allErrorHintPatterns, errorHintPattern) |
| 312 | } |
| 313 | allErrorHintPatternsRegex := strings.Join(allErrorHintPatterns[:], "|") |
| 314 | re := regexp.MustCompile(allErrorHintPatternsRegex) |
| 315 | return &ErrorHintGenerator{ |
| 316 | allErrorHints: allErrorHints, |
| 317 | allErrorHintPatternsCompiled: re, |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | func (errorHintGenerator *ErrorHintGenerator) GetOutputWithErrorHint(rawOutput string, buildExitCode int) string { |
| 322 | if buildExitCode == 0 { |
| 323 | return rawOutput |
| 324 | } |
| 325 | errorHint := errorHintGenerator.getErrorHint(rawOutput) |
| 326 | if errorHint == nil { |
| 327 | return rawOutput |
| 328 | } |
| 329 | return rawOutput + *errorHint |
| 330 | } |
| 331 | |
| 332 | // Returns the error hint corresponding to the FIRST match in raw output |
| 333 | func (errorHintGenerator *ErrorHintGenerator) getErrorHint(rawOutput string) *string { |
| 334 | firstMatch := errorHintGenerator.allErrorHintPatternsCompiled.FindString(rawOutput) |
| 335 | if _, found := errorHintGenerator.allErrorHints[firstMatch]; found { |
| 336 | errorHint := errorHintGenerator.allErrorHints[firstMatch] |
| 337 | return &errorHint |
| 338 | } |
| 339 | return nil |
| 340 | } |