Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame^] | 1 | // Copyright 2016 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 tracer |
| 16 | |
| 17 | import ( |
| 18 | "bufio" |
| 19 | "os" |
| 20 | "sort" |
| 21 | "strconv" |
| 22 | "strings" |
| 23 | "time" |
| 24 | ) |
| 25 | |
| 26 | type ninjaLogEntry struct { |
| 27 | Name string |
| 28 | Begin int |
| 29 | End int |
| 30 | } |
| 31 | type ninjaLogEntries []*ninjaLogEntry |
| 32 | |
| 33 | func (n ninjaLogEntries) Len() int { return len(n) } |
| 34 | func (n ninjaLogEntries) Less(i, j int) bool { return n[i].Begin < n[j].Begin } |
| 35 | func (n ninjaLogEntries) Swap(i, j int) { n[i], n[j] = n[j], n[i] } |
| 36 | |
| 37 | // ImportNinjaLog reads a .ninja_log file from ninja and writes the events out |
| 38 | // to the trace. |
| 39 | // |
| 40 | // startOffset is when the ninja process started, and is used to position the |
| 41 | // relative times from the ninja log into the trace. It's also used to skip |
| 42 | // reading the ninja log if nothing was run. |
| 43 | func (t *tracerImpl) ImportNinjaLog(thread Thread, filename string, startOffset time.Time) { |
| 44 | t.Begin("ninja log import", thread) |
| 45 | defer t.End(thread) |
| 46 | |
| 47 | if stat, err := os.Stat(filename); err != nil { |
| 48 | t.log.Println("Missing ninja log:", err) |
| 49 | return |
| 50 | } else if stat.ModTime().Before(startOffset) { |
| 51 | t.log.Verboseln("Ninja log not modified, not importing any entries.") |
| 52 | return |
| 53 | } |
| 54 | |
| 55 | f, err := os.Open(filename) |
| 56 | if err != nil { |
| 57 | t.log.Println("Error opening ninja log:", err) |
| 58 | return |
| 59 | } |
| 60 | defer f.Close() |
| 61 | |
| 62 | s := bufio.NewScanner(f) |
| 63 | header := true |
| 64 | entries := ninjaLogEntries{} |
| 65 | prevEnd := 0 |
| 66 | for s.Scan() { |
| 67 | if header { |
| 68 | hdr := s.Text() |
| 69 | if hdr != "# ninja log v5" { |
| 70 | t.log.Printf("Unknown ninja log header: %q", hdr) |
| 71 | return |
| 72 | } |
| 73 | header = false |
| 74 | continue |
| 75 | } |
| 76 | |
| 77 | fields := strings.Split(s.Text(), "\t") |
| 78 | begin, err := strconv.Atoi(fields[0]) |
| 79 | if err != nil { |
| 80 | t.log.Printf("Unable to parse ninja entry %q: %v", s.Text(), err) |
| 81 | return |
| 82 | } |
| 83 | end, err := strconv.Atoi(fields[1]) |
| 84 | if err != nil { |
| 85 | t.log.Printf("Unable to parse ninja entry %q: %v", s.Text(), err) |
| 86 | return |
| 87 | } |
| 88 | if end < prevEnd { |
| 89 | entries = nil |
| 90 | } |
| 91 | prevEnd = end |
| 92 | entries = append(entries, &ninjaLogEntry{ |
| 93 | Name: fields[3], |
| 94 | Begin: begin, |
| 95 | End: end, |
| 96 | }) |
| 97 | } |
| 98 | if err := s.Err(); err != nil { |
| 99 | t.log.Println("Unable to parse ninja log:", err) |
| 100 | return |
| 101 | } |
| 102 | |
| 103 | sort.Sort(entries) |
| 104 | |
| 105 | cpus := []int{} |
| 106 | offset := uint64(startOffset.UnixNano()) / 1000 |
| 107 | for _, entry := range entries { |
| 108 | tid := -1 |
| 109 | for cpu, endTime := range cpus { |
| 110 | if endTime <= entry.Begin { |
| 111 | tid = cpu |
| 112 | cpus[cpu] = entry.End |
| 113 | break |
| 114 | } |
| 115 | } |
| 116 | if tid == -1 { |
| 117 | tid = len(cpus) |
| 118 | cpus = append(cpus, entry.End) |
| 119 | } |
| 120 | |
| 121 | t.writeEvent(&viewerEvent{ |
| 122 | Name: entry.Name, |
| 123 | Phase: "X", |
| 124 | Time: offset + uint64(entry.Begin)*1000, |
| 125 | Dur: uint64(entry.End-entry.Begin) * 1000, |
| 126 | Pid: 1, |
| 127 | Tid: uint64(tid), |
| 128 | }) |
| 129 | } |
| 130 | } |