Shinichiro Hamaji | b69bf8a | 2015-06-10 14:52:06 +0900 | [diff] [blame] | 1 | // Copyright 2015 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 | |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 15 | package kati |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 16 | |
| 17 | import ( |
| 18 | "fmt" |
Fumitoshi Ukai | 432a242 | 2015-06-11 15:16:29 +0900 | [diff] [blame] | 19 | "io" |
| 20 | "os" |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 21 | "sort" |
Fumitoshi Ukai | f543f4d | 2015-06-15 15:21:47 +0900 | [diff] [blame] | 22 | "sync" |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 23 | "time" |
| 24 | ) |
| 25 | |
Fumitoshi Ukai | 432a242 | 2015-06-11 15:16:29 +0900 | [diff] [blame] | 26 | type traceEventT struct { |
Fumitoshi Ukai | f543f4d | 2015-06-15 15:21:47 +0900 | [diff] [blame] | 27 | mu sync.Mutex |
Fumitoshi Ukai | 432a242 | 2015-06-11 15:16:29 +0900 | [diff] [blame] | 28 | f io.WriteCloser |
| 29 | t0 time.Time |
| 30 | pid int |
| 31 | } |
| 32 | |
Fumitoshi Ukai | f543f4d | 2015-06-15 15:21:47 +0900 | [diff] [blame] | 33 | const ( |
| 34 | traceEventMain = iota + 1 |
Fumitoshi Ukai | 0547db6 | 2015-07-29 16:20:59 +0900 | [diff] [blame] | 35 | // add new ones to use new goroutine. |
Fumitoshi Ukai | f543f4d | 2015-06-15 15:21:47 +0900 | [diff] [blame] | 36 | ) |
| 37 | |
Fumitoshi Ukai | 432a242 | 2015-06-11 15:16:29 +0900 | [diff] [blame] | 38 | var traceEvent traceEventT |
| 39 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 40 | // TraceEventStart starts trace event. |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 41 | func TraceEventStart(f io.WriteCloser) { |
| 42 | traceEvent.start(f) |
| 43 | } |
| 44 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 45 | // TraceEventStop stops trace event. |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 46 | func TraceEventStop() { |
| 47 | traceEvent.stop() |
| 48 | } |
| 49 | |
Fumitoshi Ukai | 432a242 | 2015-06-11 15:16:29 +0900 | [diff] [blame] | 50 | func (t *traceEventT) start(f io.WriteCloser) { |
| 51 | t.f = f |
| 52 | t.t0 = time.Now() |
| 53 | fmt.Fprint(t.f, "[ ") |
| 54 | } |
| 55 | |
| 56 | func (t *traceEventT) enabled() bool { |
| 57 | return t.f != nil |
| 58 | } |
| 59 | |
| 60 | func (t *traceEventT) stop() { |
| 61 | fmt.Fprint(t.f, "\n]\n") |
| 62 | t.f.Close() |
| 63 | } |
| 64 | |
| 65 | type event struct { |
| 66 | name, v string |
Fumitoshi Ukai | f543f4d | 2015-06-15 15:21:47 +0900 | [diff] [blame] | 67 | tid int |
Fumitoshi Ukai | 432a242 | 2015-06-11 15:16:29 +0900 | [diff] [blame] | 68 | t time.Time |
Fumitoshi Ukai | cb393c5 | 2015-06-11 15:34:10 +0900 | [diff] [blame] | 69 | emit bool |
Fumitoshi Ukai | 432a242 | 2015-06-11 15:16:29 +0900 | [diff] [blame] | 70 | } |
| 71 | |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 72 | func (t *traceEventT) begin(name string, v Value, tid int) event { |
Fumitoshi Ukai | 432a242 | 2015-06-11 15:16:29 +0900 | [diff] [blame] | 73 | var e event |
Fumitoshi Ukai | f543f4d | 2015-06-15 15:21:47 +0900 | [diff] [blame] | 74 | e.tid = tid |
Fumitoshi Ukai | 432a242 | 2015-06-11 15:16:29 +0900 | [diff] [blame] | 75 | e.t = time.Now() |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 76 | if t.f != nil || EvalStatsFlag { |
Fumitoshi Ukai | 6ab72dd | 2015-06-11 15:45:52 +0900 | [diff] [blame] | 77 | e.name = name |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 78 | e.v = v.String() |
Fumitoshi Ukai | 6ab72dd | 2015-06-11 15:45:52 +0900 | [diff] [blame] | 79 | } |
Fumitoshi Ukai | 432a242 | 2015-06-11 15:16:29 +0900 | [diff] [blame] | 80 | if t.f != nil { |
Fumitoshi Ukai | 0547db6 | 2015-07-29 16:20:59 +0900 | [diff] [blame] | 81 | e.emit = name == "include" || name == "shell" |
Fumitoshi Ukai | cb393c5 | 2015-06-11 15:34:10 +0900 | [diff] [blame] | 82 | if e.emit { |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 83 | t.emit("B", e, e.t.Sub(t.t0)) |
Fumitoshi Ukai | 432a242 | 2015-06-11 15:16:29 +0900 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | return e |
| 87 | } |
| 88 | |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 89 | func (t *traceEventT) emit(ph string, e event, ts time.Duration) { |
Fumitoshi Ukai | f543f4d | 2015-06-15 15:21:47 +0900 | [diff] [blame] | 90 | t.mu.Lock() |
| 91 | defer t.mu.Unlock() |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 92 | |
| 93 | if t.pid == 0 { |
| 94 | t.pid = os.Getpid() |
| 95 | } else { |
| 96 | fmt.Fprintf(t.f, ",\n") |
| 97 | } |
| 98 | fmt.Fprintf(t.f, `{"pid":%d,"tid":%d,"ts":%d,"ph":%q,"cat":%q,"name":%q,"args":{}}`, |
| 99 | t.pid, |
| 100 | e.tid, |
| 101 | ts.Nanoseconds()/1e3, |
| 102 | ph, |
| 103 | e.name, |
| 104 | e.v, |
| 105 | ) |
| 106 | } |
| 107 | |
| 108 | func (t *traceEventT) end(e event) { |
Fumitoshi Ukai | 432a242 | 2015-06-11 15:16:29 +0900 | [diff] [blame] | 109 | if t.f != nil { |
Fumitoshi Ukai | cb393c5 | 2015-06-11 15:34:10 +0900 | [diff] [blame] | 110 | if e.emit { |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 111 | t.emit("E", e, time.Since(t.t0)) |
Fumitoshi Ukai | 432a242 | 2015-06-11 15:16:29 +0900 | [diff] [blame] | 112 | } |
| 113 | } |
Fumitoshi Ukai | 9042b99 | 2015-06-23 16:10:27 +0900 | [diff] [blame] | 114 | stats.add(e.name, e.v, e.t) |
Fumitoshi Ukai | 432a242 | 2015-06-11 15:16:29 +0900 | [diff] [blame] | 115 | } |
| 116 | |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 117 | type statsData struct { |
| 118 | Name string |
| 119 | Count int |
| 120 | Longest time.Duration |
| 121 | Total time.Duration |
| 122 | } |
| 123 | |
Fumitoshi Ukai | 9042b99 | 2015-06-23 16:10:27 +0900 | [diff] [blame] | 124 | type statsT struct { |
| 125 | mu sync.Mutex |
| 126 | data map[string]statsData |
| 127 | } |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 128 | |
Fumitoshi Ukai | 9042b99 | 2015-06-23 16:10:27 +0900 | [diff] [blame] | 129 | var stats = &statsT{ |
| 130 | data: make(map[string]statsData), |
| 131 | } |
| 132 | |
| 133 | func (s *statsT) add(name, v string, t time.Time) { |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 134 | if !EvalStatsFlag { |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 135 | return |
| 136 | } |
Fumitoshi Ukai | 358c68a | 2015-06-08 13:12:55 +0900 | [diff] [blame] | 137 | d := time.Since(t) |
Fumitoshi Ukai | 432a242 | 2015-06-11 15:16:29 +0900 | [diff] [blame] | 138 | key := fmt.Sprintf("%s:%s", name, v) |
Fumitoshi Ukai | 9042b99 | 2015-06-23 16:10:27 +0900 | [diff] [blame] | 139 | s.mu.Lock() |
| 140 | sd := s.data[key] |
| 141 | if d > sd.Longest { |
| 142 | sd.Longest = d |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 143 | } |
Fumitoshi Ukai | 9042b99 | 2015-06-23 16:10:27 +0900 | [diff] [blame] | 144 | sd.Total += d |
| 145 | sd.Count++ |
| 146 | s.data[key] = sd |
| 147 | s.mu.Unlock() |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 148 | } |
| 149 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 150 | // DumpStats dumps statistics collected if EvalStatsFlag is set. |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 151 | func DumpStats() { |
| 152 | if !EvalStatsFlag { |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 153 | return |
| 154 | } |
| 155 | var sv byTotalTime |
Fumitoshi Ukai | 9042b99 | 2015-06-23 16:10:27 +0900 | [diff] [blame] | 156 | for k, v := range stats.data { |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 157 | v.Name = k |
| 158 | sv = append(sv, v) |
| 159 | } |
| 160 | sort.Sort(sv) |
Fumitoshi Ukai | 47f401f | 2015-04-30 17:29:50 +0900 | [diff] [blame] | 161 | fmt.Println("count,longest(ns),total(ns),longest,total,name") |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 162 | for _, s := range sv { |
Fumitoshi Ukai | 47f401f | 2015-04-30 17:29:50 +0900 | [diff] [blame] | 163 | fmt.Printf("%d,%d,%d,%v,%v,%s\n", s.Count, s.Longest, s.Total, s.Longest, s.Total, s.Name) |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | type byTotalTime []statsData |
| 168 | |
| 169 | func (b byTotalTime) Len() int { return len(b) } |
| 170 | func (b byTotalTime) Swap(i, j int) { b[i], b[j] = b[j], b[i] } |
| 171 | func (b byTotalTime) Less(i, j int) bool { |
| 172 | return b[i].Total > b[j].Total |
| 173 | } |
Fumitoshi Ukai | 9042b99 | 2015-06-23 16:10:27 +0900 | [diff] [blame] | 174 | |
| 175 | type shellStatsT struct { |
| 176 | mu sync.Mutex |
| 177 | duration time.Duration |
| 178 | count int |
| 179 | } |
| 180 | |
| 181 | var shellStats = &shellStatsT{} |
| 182 | |
| 183 | func (s *shellStatsT) add(d time.Duration) { |
| 184 | s.mu.Lock() |
| 185 | s.duration += d |
| 186 | s.count++ |
| 187 | s.mu.Unlock() |
| 188 | } |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 189 | |
| 190 | func (s *shellStatsT) Duration() time.Duration { |
| 191 | s.mu.Lock() |
| 192 | defer s.mu.Unlock() |
| 193 | return s.duration |
| 194 | } |
| 195 | |
| 196 | func (s *shellStatsT) Count() int { |
| 197 | s.mu.Lock() |
| 198 | defer s.mu.Unlock() |
| 199 | return s.count |
| 200 | } |