events.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package trace
  5. import (
  6. "bytes"
  7. "fmt"
  8. "html/template"
  9. "io"
  10. "log"
  11. "net/http"
  12. "runtime"
  13. "sort"
  14. "strconv"
  15. "strings"
  16. "sync"
  17. "sync/atomic"
  18. "text/tabwriter"
  19. "time"
  20. )
  21. var eventsTmpl = template.Must(template.New("events").Funcs(template.FuncMap{
  22. "elapsed": elapsed,
  23. "trimSpace": strings.TrimSpace,
  24. }).Parse(eventsHTML))
  25. const maxEventsPerLog = 100
  26. type bucket struct {
  27. MaxErrAge time.Duration
  28. String string
  29. }
  30. var buckets = []bucket{
  31. {0, "total"},
  32. {10 * time.Second, "errs<10s"},
  33. {1 * time.Minute, "errs<1m"},
  34. {10 * time.Minute, "errs<10m"},
  35. {1 * time.Hour, "errs<1h"},
  36. {10 * time.Hour, "errs<10h"},
  37. {24000 * time.Hour, "errors"},
  38. }
  39. // RenderEvents renders the HTML page typically served at /debug/events.
  40. // It does not do any auth checking; see AuthRequest for the default auth check
  41. // used by the handler registered on http.DefaultServeMux.
  42. // req may be nil.
  43. func RenderEvents(w http.ResponseWriter, req *http.Request, sensitive bool) {
  44. now := time.Now()
  45. data := &struct {
  46. Families []string // family names
  47. Buckets []bucket
  48. Counts [][]int // eventLog count per family/bucket
  49. // Set when a bucket has been selected.
  50. Family string
  51. Bucket int
  52. EventLogs eventLogs
  53. Expanded bool
  54. }{
  55. Buckets: buckets,
  56. }
  57. data.Families = make([]string, 0, len(families))
  58. famMu.RLock()
  59. for name := range families {
  60. data.Families = append(data.Families, name)
  61. }
  62. famMu.RUnlock()
  63. sort.Strings(data.Families)
  64. // Count the number of eventLogs in each family for each error age.
  65. data.Counts = make([][]int, len(data.Families))
  66. for i, name := range data.Families {
  67. // TODO(sameer): move this loop under the family lock.
  68. f := getEventFamily(name)
  69. data.Counts[i] = make([]int, len(data.Buckets))
  70. for j, b := range data.Buckets {
  71. data.Counts[i][j] = f.Count(now, b.MaxErrAge)
  72. }
  73. }
  74. if req != nil {
  75. var ok bool
  76. data.Family, data.Bucket, ok = parseEventsArgs(req)
  77. if !ok {
  78. // No-op
  79. } else {
  80. data.EventLogs = getEventFamily(data.Family).Copy(now, buckets[data.Bucket].MaxErrAge)
  81. }
  82. if data.EventLogs != nil {
  83. defer data.EventLogs.Free()
  84. sort.Sort(data.EventLogs)
  85. }
  86. if exp, err := strconv.ParseBool(req.FormValue("exp")); err == nil {
  87. data.Expanded = exp
  88. }
  89. }
  90. famMu.RLock()
  91. defer famMu.RUnlock()
  92. if err := eventsTmpl.Execute(w, data); err != nil {
  93. log.Printf("net/trace: Failed executing template: %v", err)
  94. }
  95. }
  96. func parseEventsArgs(req *http.Request) (fam string, b int, ok bool) {
  97. fam, bStr := req.FormValue("fam"), req.FormValue("b")
  98. if fam == "" || bStr == "" {
  99. return "", 0, false
  100. }
  101. b, err := strconv.Atoi(bStr)
  102. if err != nil || b < 0 || b >= len(buckets) {
  103. return "", 0, false
  104. }
  105. return fam, b, true
  106. }
  107. // An EventLog provides a log of events associated with a specific object.
  108. type EventLog interface {
  109. // Printf formats its arguments with fmt.Sprintf and adds the
  110. // result to the event log.
  111. Printf(format string, a ...interface{})
  112. // Errorf is like Printf, but it marks this event as an error.
  113. Errorf(format string, a ...interface{})
  114. // Finish declares that this event log is complete.
  115. // The event log should not be used after calling this method.
  116. Finish()
  117. }
  118. // NewEventLog returns a new EventLog with the specified family name
  119. // and title.
  120. func NewEventLog(family, title string) EventLog {
  121. el := newEventLog()
  122. el.ref()
  123. el.Family, el.Title = family, title
  124. el.Start = time.Now()
  125. el.events = make([]logEntry, 0, maxEventsPerLog)
  126. el.stack = make([]uintptr, 32)
  127. n := runtime.Callers(2, el.stack)
  128. el.stack = el.stack[:n]
  129. getEventFamily(family).add(el)
  130. return el
  131. }
  132. func (el *eventLog) Finish() {
  133. getEventFamily(el.Family).remove(el)
  134. el.unref() // matches ref in New
  135. }
  136. var (
  137. famMu sync.RWMutex
  138. families = make(map[string]*eventFamily) // family name => family
  139. )
  140. func getEventFamily(fam string) *eventFamily {
  141. famMu.Lock()
  142. defer famMu.Unlock()
  143. f := families[fam]
  144. if f == nil {
  145. f = &eventFamily{}
  146. families[fam] = f
  147. }
  148. return f
  149. }
  150. type eventFamily struct {
  151. mu sync.RWMutex
  152. eventLogs eventLogs
  153. }
  154. func (f *eventFamily) add(el *eventLog) {
  155. f.mu.Lock()
  156. f.eventLogs = append(f.eventLogs, el)
  157. f.mu.Unlock()
  158. }
  159. func (f *eventFamily) remove(el *eventLog) {
  160. f.mu.Lock()
  161. defer f.mu.Unlock()
  162. for i, el0 := range f.eventLogs {
  163. if el == el0 {
  164. copy(f.eventLogs[i:], f.eventLogs[i+1:])
  165. f.eventLogs = f.eventLogs[:len(f.eventLogs)-1]
  166. return
  167. }
  168. }
  169. }
  170. func (f *eventFamily) Count(now time.Time, maxErrAge time.Duration) (n int) {
  171. f.mu.RLock()
  172. defer f.mu.RUnlock()
  173. for _, el := range f.eventLogs {
  174. if el.hasRecentError(now, maxErrAge) {
  175. n++
  176. }
  177. }
  178. return
  179. }
  180. func (f *eventFamily) Copy(now time.Time, maxErrAge time.Duration) (els eventLogs) {
  181. f.mu.RLock()
  182. defer f.mu.RUnlock()
  183. els = make(eventLogs, 0, len(f.eventLogs))
  184. for _, el := range f.eventLogs {
  185. if el.hasRecentError(now, maxErrAge) {
  186. el.ref()
  187. els = append(els, el)
  188. }
  189. }
  190. return
  191. }
  192. type eventLogs []*eventLog
  193. // Free calls unref on each element of the list.
  194. func (els eventLogs) Free() {
  195. for _, el := range els {
  196. el.unref()
  197. }
  198. }
  199. // eventLogs may be sorted in reverse chronological order.
  200. func (els eventLogs) Len() int { return len(els) }
  201. func (els eventLogs) Less(i, j int) bool { return els[i].Start.After(els[j].Start) }
  202. func (els eventLogs) Swap(i, j int) { els[i], els[j] = els[j], els[i] }
  203. // A logEntry is a timestamped log entry in an event log.
  204. type logEntry struct {
  205. When time.Time
  206. Elapsed time.Duration // since previous event in log
  207. NewDay bool // whether this event is on a different day to the previous event
  208. What string
  209. IsErr bool
  210. }
  211. // WhenString returns a string representation of the elapsed time of the event.
  212. // It will include the date if midnight was crossed.
  213. func (e logEntry) WhenString() string {
  214. if e.NewDay {
  215. return e.When.Format("2006/01/02 15:04:05.000000")
  216. }
  217. return e.When.Format("15:04:05.000000")
  218. }
  219. // An eventLog represents an active event log.
  220. type eventLog struct {
  221. // Family is the top-level grouping of event logs to which this belongs.
  222. Family string
  223. // Title is the title of this event log.
  224. Title string
  225. // Timing information.
  226. Start time.Time
  227. // Call stack where this event log was created.
  228. stack []uintptr
  229. // Append-only sequence of events.
  230. //
  231. // TODO(sameer): change this to a ring buffer to avoid the array copy
  232. // when we hit maxEventsPerLog.
  233. mu sync.RWMutex
  234. events []logEntry
  235. LastErrorTime time.Time
  236. discarded int
  237. refs int32 // how many buckets this is in
  238. }
  239. func (el *eventLog) reset() {
  240. // Clear all but the mutex. Mutexes may not be copied, even when unlocked.
  241. el.Family = ""
  242. el.Title = ""
  243. el.Start = time.Time{}
  244. el.stack = nil
  245. el.events = nil
  246. el.LastErrorTime = time.Time{}
  247. el.discarded = 0
  248. el.refs = 0
  249. }
  250. func (el *eventLog) hasRecentError(now time.Time, maxErrAge time.Duration) bool {
  251. if maxErrAge == 0 {
  252. return true
  253. }
  254. el.mu.RLock()
  255. defer el.mu.RUnlock()
  256. return now.Sub(el.LastErrorTime) < maxErrAge
  257. }
  258. // delta returns the elapsed time since the last event or the log start,
  259. // and whether it spans midnight.
  260. // L >= el.mu
  261. func (el *eventLog) delta(t time.Time) (time.Duration, bool) {
  262. if len(el.events) == 0 {
  263. return t.Sub(el.Start), false
  264. }
  265. prev := el.events[len(el.events)-1].When
  266. return t.Sub(prev), prev.Day() != t.Day()
  267. }
  268. func (el *eventLog) Printf(format string, a ...interface{}) {
  269. el.printf(false, format, a...)
  270. }
  271. func (el *eventLog) Errorf(format string, a ...interface{}) {
  272. el.printf(true, format, a...)
  273. }
  274. func (el *eventLog) printf(isErr bool, format string, a ...interface{}) {
  275. e := logEntry{When: time.Now(), IsErr: isErr, What: fmt.Sprintf(format, a...)}
  276. el.mu.Lock()
  277. e.Elapsed, e.NewDay = el.delta(e.When)
  278. if len(el.events) < maxEventsPerLog {
  279. el.events = append(el.events, e)
  280. } else {
  281. // Discard the oldest event.
  282. if el.discarded == 0 {
  283. // el.discarded starts at two to count for the event it
  284. // is replacing, plus the next one that we are about to
  285. // drop.
  286. el.discarded = 2
  287. } else {
  288. el.discarded++
  289. }
  290. // TODO(sameer): if this causes allocations on a critical path,
  291. // change eventLog.What to be a fmt.Stringer, as in trace.go.
  292. el.events[0].What = fmt.Sprintf("(%d events discarded)", el.discarded)
  293. // The timestamp of the discarded meta-event should be
  294. // the time of the last event it is representing.
  295. el.events[0].When = el.events[1].When
  296. copy(el.events[1:], el.events[2:])
  297. el.events[maxEventsPerLog-1] = e
  298. }
  299. if e.IsErr {
  300. el.LastErrorTime = e.When
  301. }
  302. el.mu.Unlock()
  303. }
  304. func (el *eventLog) ref() {
  305. atomic.AddInt32(&el.refs, 1)
  306. }
  307. func (el *eventLog) unref() {
  308. if atomic.AddInt32(&el.refs, -1) == 0 {
  309. freeEventLog(el)
  310. }
  311. }
  312. func (el *eventLog) When() string {
  313. return el.Start.Format("2006/01/02 15:04:05.000000")
  314. }
  315. func (el *eventLog) ElapsedTime() string {
  316. elapsed := time.Since(el.Start)
  317. return fmt.Sprintf("%.6f", elapsed.Seconds())
  318. }
  319. func (el *eventLog) Stack() string {
  320. buf := new(bytes.Buffer)
  321. tw := tabwriter.NewWriter(buf, 1, 8, 1, '\t', 0)
  322. printStackRecord(tw, el.stack)
  323. tw.Flush()
  324. return buf.String()
  325. }
  326. // printStackRecord prints the function + source line information
  327. // for a single stack trace.
  328. // Adapted from runtime/pprof/pprof.go.
  329. func printStackRecord(w io.Writer, stk []uintptr) {
  330. for _, pc := range stk {
  331. f := runtime.FuncForPC(pc)
  332. if f == nil {
  333. continue
  334. }
  335. file, line := f.FileLine(pc)
  336. name := f.Name()
  337. // Hide runtime.goexit and any runtime functions at the beginning.
  338. if strings.HasPrefix(name, "runtime.") {
  339. continue
  340. }
  341. fmt.Fprintf(w, "# %s\t%s:%d\n", name, file, line)
  342. }
  343. }
  344. func (el *eventLog) Events() []logEntry {
  345. el.mu.RLock()
  346. defer el.mu.RUnlock()
  347. return el.events
  348. }
  349. // freeEventLogs is a freelist of *eventLog
  350. var freeEventLogs = make(chan *eventLog, 1000)
  351. // newEventLog returns a event log ready to use.
  352. func newEventLog() *eventLog {
  353. select {
  354. case el := <-freeEventLogs:
  355. return el
  356. default:
  357. return new(eventLog)
  358. }
  359. }
  360. // freeEventLog adds el to freeEventLogs if there's room.
  361. // This is non-blocking.
  362. func freeEventLog(el *eventLog) {
  363. el.reset()
  364. select {
  365. case freeEventLogs <- el:
  366. default:
  367. }
  368. }
  369. const eventsHTML = `
  370. <html>
  371. <head>
  372. <title>events</title>
  373. </head>
  374. <style type="text/css">
  375. body {
  376. font-family: sans-serif;
  377. }
  378. table#req-status td.family {
  379. padding-right: 2em;
  380. }
  381. table#req-status td.active {
  382. padding-right: 1em;
  383. }
  384. table#req-status td.empty {
  385. color: #aaa;
  386. }
  387. table#reqs {
  388. margin-top: 1em;
  389. }
  390. table#reqs tr.first {
  391. {{if $.Expanded}}font-weight: bold;{{end}}
  392. }
  393. table#reqs td {
  394. font-family: monospace;
  395. }
  396. table#reqs td.when {
  397. text-align: right;
  398. white-space: nowrap;
  399. }
  400. table#reqs td.elapsed {
  401. padding: 0 0.5em;
  402. text-align: right;
  403. white-space: pre;
  404. width: 10em;
  405. }
  406. address {
  407. font-size: smaller;
  408. margin-top: 5em;
  409. }
  410. </style>
  411. <body>
  412. <h1>/debug/events</h1>
  413. <table id="req-status">
  414. {{range $i, $fam := .Families}}
  415. <tr>
  416. <td class="family">{{$fam}}</td>
  417. {{range $j, $bucket := $.Buckets}}
  418. {{$n := index $.Counts $i $j}}
  419. <td class="{{if not $bucket.MaxErrAge}}active{{end}}{{if not $n}}empty{{end}}">
  420. {{if $n}}<a href="?fam={{$fam}}&b={{$j}}{{if $.Expanded}}&exp=1{{end}}">{{end}}
  421. [{{$n}} {{$bucket.String}}]
  422. {{if $n}}</a>{{end}}
  423. </td>
  424. {{end}}
  425. </tr>{{end}}
  426. </table>
  427. {{if $.EventLogs}}
  428. <hr />
  429. <h3>Family: {{$.Family}}</h3>
  430. {{if $.Expanded}}<a href="?fam={{$.Family}}&b={{$.Bucket}}">{{end}}
  431. [Summary]{{if $.Expanded}}</a>{{end}}
  432. {{if not $.Expanded}}<a href="?fam={{$.Family}}&b={{$.Bucket}}&exp=1">{{end}}
  433. [Expanded]{{if not $.Expanded}}</a>{{end}}
  434. <table id="reqs">
  435. <tr><th>When</th><th>Elapsed</th></tr>
  436. {{range $el := $.EventLogs}}
  437. <tr class="first">
  438. <td class="when">{{$el.When}}</td>
  439. <td class="elapsed">{{$el.ElapsedTime}}</td>
  440. <td>{{$el.Title}}
  441. </tr>
  442. {{if $.Expanded}}
  443. <tr>
  444. <td class="when"></td>
  445. <td class="elapsed"></td>
  446. <td><pre>{{$el.Stack|trimSpace}}</pre></td>
  447. </tr>
  448. {{range $el.Events}}
  449. <tr>
  450. <td class="when">{{.WhenString}}</td>
  451. <td class="elapsed">{{elapsed .Elapsed}}</td>
  452. <td>.{{if .IsErr}}E{{else}}.{{end}}. {{.What}}</td>
  453. </tr>
  454. {{end}}
  455. {{end}}
  456. {{end}}
  457. </table>
  458. {{end}}
  459. </body>
  460. </html>
  461. `