summary refs log tree commit diff stats
path: root/compiler/vmprofiler.nim
blob: f586c8ffe3b5a98cef5b47511706f709cda5b05d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highl
import
  options, vmdef, times, lineinfos, strutils, tables,
  msgs

proc enter*(prof: var Profiler, c: PCtx, tos: PStackFrame) {.inline.} =
  if optProfileVM in c.config.globalOptions:
    prof.tEnter = cpuTime()
    prof.tos = tos

proc leaveImpl(prof: var Profiler, c: PCtx) {.noinline.} =
  let tLeave = cpuTime()
  var tos = prof.tos
  var data = c.config.vmProfileData.data
  while tos != nil:
    if tos.prc != nil:
      let li = tos.prc.info
      if li notin data:
        data[li] = ProfileInfo()
      data[li].time += tLeave - prof.tEnter
      if tos == prof.tos:
        inc data[li].count
    tos = tos.next

proc leave*(prof: var Profiler, c: PCtx) {.inline.} =
  if optProfileVM in c.config.globalOptions:
    leaveImpl(prof, c)

proc dump*(conf: ConfigRef, pd: ProfileData): string =
  var data = pd.data
  echo "\nprof:     µs    #instr  location"
  for i in 0..<32:
    var tMax: float
    var infoMax: ProfileInfo
    var flMax: TLineInfo
    for fl, info in data:
      if info.time > infoMax.time:
        infoMax = info
        flMax = fl
    if infoMax.count == 0:
      break
    result.add  "  " & align($int(infoMax.time * 1e6), 10) &
                       align($int(infoMax.count), 10) & "  " &
                       conf.toFileLineCol(flMax) & "\n"
    data.del flMax