src/jit/v.lua - luajit-2.0-src

Functions defined

Source code

  1. ----------------------------------------------------------------------------
  2. -- Verbose mode of the LuaJIT compiler.
  3. --
  4. -- Copyright (C) 2005-2015 Mike Pall. All rights reserved.
  5. -- Released under the MIT license. See Copyright Notice in luajit.h
  6. ----------------------------------------------------------------------------
  7. --
  8. -- This module shows verbose information about the progress of the
  9. -- JIT compiler. It prints one line for each generated trace. This module
  10. -- is useful to see which code has been compiled or where the compiler
  11. -- punts and falls back to the interpreter.
  12. --
  13. -- Example usage:
  14. --
  15. --   luajit -jv -e "for i=1,1000 do for j=1,1000 do end end"
  16. --   luajit -jv=myapp.out myapp.lua
  17. --
  18. -- Default output is to stderr. To redirect the output to a file, pass a
  19. -- filename as an argument (use '-' for stdout) or set the environment
  20. -- variable LUAJIT_VERBOSEFILE. The file is overwritten every time the
  21. -- module is started.
  22. --
  23. -- The output from the first example should look like this:
  24. --
  25. -- [TRACE   1 (command line):1 loop]
  26. -- [TRACE   2 (1/3) (command line):1 -> 1]
  27. --
  28. -- The first number in each line is the internal trace number. Next are
  29. -- the file name ('(command line)') and the line number (':1') where the
  30. -- trace has started. Side traces also show the parent trace number and
  31. -- the exit number where they are attached to in parentheses ('(1/3)').
  32. -- An arrow at the end shows where the trace links to ('-> 1'), unless
  33. -- it loops to itself.
  34. --
  35. -- In this case the inner loop gets hot and is traced first, generating
  36. -- a root trace. Then the last exit from the 1st trace gets hot, too,
  37. -- and triggers generation of the 2nd trace. The side trace follows the
  38. -- path along the outer loop and *around* the inner loop, back to its
  39. -- start, and then links to the 1st trace. Yes, this may seem unusual,
  40. -- if you know how traditional compilers work. Trace compilers are full
  41. -- of surprises like this -- have fun! :-)
  42. --
  43. -- Aborted traces are shown like this:
  44. --
  45. -- [TRACE --- foo.lua:44 -- leaving loop in root trace at foo:lua:50]
  46. --
  47. -- Don't worry -- trace aborts are quite common, even in programs which
  48. -- can be fully compiled. The compiler may retry several times until it
  49. -- finds a suitable trace.
  50. --
  51. -- Of course this doesn't work with features that are not-yet-implemented
  52. -- (NYI error messages). The VM simply falls back to the interpreter. This
  53. -- may not matter at all if the particular trace is not very high up in
  54. -- the CPU usage profile. Oh, and the interpreter is quite fast, too.
  55. --
  56. -- Also check out the -jdump module, which prints all the gory details.
  57. --
  58. ------------------------------------------------------------------------------

  59. -- Cache some library functions and objects.
  60. local jit = require("jit")
  61. assert(jit.version_num == 20100, "LuaJIT core/library version mismatch")
  62. local jutil = require("jit.util")
  63. local vmdef = require("jit.vmdef")
  64. local funcinfo, traceinfo = jutil.funcinfo, jutil.traceinfo
  65. local type, format = type, string.format
  66. local stdout, stderr = io.stdout, io.stderr

  67. -- Active flag and output file handle.
  68. local active, out

  69. ------------------------------------------------------------------------------

  70. local startloc, startex

  71. local function fmtfunc(func, pc)
  72.   local fi = funcinfo(func, pc)
  73.   if fi.loc then
  74.     return fi.loc
  75.   elseif fi.ffid then
  76.     return vmdef.ffnames[fi.ffid]
  77.   elseif fi.addr then
  78.     return format("C:%x", fi.addr)
  79.   else
  80.     return "(?)"
  81.   end
  82. end

  83. -- Format trace error message.
  84. local function fmterr(err, info)
  85.   if type(err) == "number" then
  86.     if type(info) == "function" then info = fmtfunc(info) end
  87.     err = format(vmdef.traceerr[err], info)
  88.   end
  89.   return err
  90. end

  91. -- Dump trace states.
  92. local function dump_trace(what, tr, func, pc, otr, oex)
  93.   if what == "start" then
  94.     startloc = fmtfunc(func, pc)
  95.     startex = otr and "("..otr.."/"..oex..") " or ""
  96.   else
  97.     if what == "abort" then
  98.       local loc = fmtfunc(func, pc)
  99.       if loc ~= startloc then
  100.         out:write(format("[TRACE --- %s%s -- %s at %s]\n",
  101.           startex, startloc, fmterr(otr, oex), loc))
  102.       else
  103.         out:write(format("[TRACE --- %s%s -- %s]\n",
  104.           startex, startloc, fmterr(otr, oex)))
  105.       end
  106.     elseif what == "stop" then
  107.       local info = traceinfo(tr)
  108.       local link, ltype = info.link, info.linktype
  109.       if ltype == "interpreter" then
  110.         out:write(format("[TRACE %3s %s%s -- fallback to interpreter]\n",
  111.           tr, startex, startloc))
  112.       elseif ltype == "stitch" then
  113.         out:write(format("[TRACE %3s %s%s %s %s]\n",
  114.           tr, startex, startloc, ltype, fmtfunc(func, pc)))
  115.       elseif link == tr or link == 0 then
  116.         out:write(format("[TRACE %3s %s%s %s]\n",
  117.           tr, startex, startloc, ltype))
  118.       elseif ltype == "root" then
  119.         out:write(format("[TRACE %3s %s%s -> %d]\n",
  120.           tr, startex, startloc, link))
  121.       else
  122.         out:write(format("[TRACE %3s %s%s -> %d %s]\n",
  123.           tr, startex, startloc, link, ltype))
  124.       end
  125.     else
  126.       out:write(format("[TRACE %s]\n", what))
  127.     end
  128.     out:flush()
  129.   end
  130. end

  131. ------------------------------------------------------------------------------

  132. -- Detach dump handlers.
  133. local function dumpoff()
  134.   if active then
  135.     active = false
  136.     jit.attach(dump_trace)
  137.     if out and out ~= stdout and out ~= stderr then out:close() end
  138.     out = nil
  139.   end
  140. end

  141. -- Open the output file and attach dump handlers.
  142. local function dumpon(outfile)
  143.   if active then dumpoff() end
  144.   if not outfile then outfile = os.getenv("LUAJIT_VERBOSEFILE") end
  145.   if outfile then
  146.     out = outfile == "-" and stdout or assert(io.open(outfile, "w"))
  147.   else
  148.     out = stderr
  149.   end
  150.   jit.attach(dump_trace, "trace")
  151.   active = true
  152. end

  153. -- Public module functions.
  154. return {
  155.   on = dumpon,
  156.   off = dumpoff,
  157.   start = dumpon -- For -j command line option.
  158. }