=pod LuaJIT =head1 Profiler =over =item * LuaJIT =over =item * Download E =item * Installation =item * Running =back =item * Extensions =over =item * FFI Library =over =item * FFI Tutorial =item * ffi.* API =item * FFI Semantics =back =item * jit.* Library =item * Lua/C API =item * Profiler =back =item * Status =over =item * Changes =back =item * FAQ =item * Performance E =item * Wiki E =item * Mailing List E =back LuaJIT has an integrated statistical profiler with very low overhead. It allows sampling the currently executing stack and other parameters in regular intervals. The integrated profiler can be accessed from three levels: =over =item * The bundled high-level profiler, invoked by the C<-jp> command line option. =item * A low-level Lua API to control the profiler. =item * A low-level C API to control the profiler. =back =head2 High-Level Profiler The bundled high-level profiler offers basic profiling functionality. It generates simple textual summaries or source code annotations. It can be accessed with the C<-jp> command line option or from Lua code by loading the underlying C module. To cut to the chase E run this to get a CPU usage profile by function name: luajit -jp myapp.lua It's I a stated goal of the bundled profiler to add every possible option or to cater for special profiling needs. The low-level profiler APIs are documented below. They may be used by third-party authors to implement advanced functionality, e.g. IDE integration or graphical profilers. Note: Sampling works for both interpreted and JIT-compiled code. The results for JIT-compiled code may sometimes be surprising. LuaJIT heavily optimizes and inlines Lua code E there's no simple one-to-one correspondence between source code lines and the sampled machine code. =head2 C<-jp=[options[,output]]> The C<-jp> command line option starts the high-level profiler. When the application run by the command line terminates, the profiler stops and writes the results to C or to the specified C file. The C argument specifies how the profiling is to be performed: =over =item * C E Stack dump: function name, otherwise module:line. This is the default mode. =item * C E Stack dump: ditto, but dump module:name. =item * C E Stack dump: module:line. =item * CnumberE> E stack dump depth (callee E caller). Default: 1. =item * C<-EnumberE> E Inverse stack dump depth (caller E callee). =item * C E Split stack dump after first stack level. Implies depth E 2 or depth E -2. =item * C

E Show full path for module names. =item * C E Show VM states. =item * C E Show zones. =item * C E Show raw sample counts. Default: show percentages. =item * C E Annotate excerpts from source code files. =item * C E Annotate complete source code files. =item * C E Produce raw output suitable for graphical tools. =item * CnumberE> E Minimum sample percentage to be shown. Default: 3%. =item * CnumberE> E Sampling interval in milliseconds. Default: 10ms. Note: The actual sampling precision is OS-dependent. =back The default output for C<-jp> is a list of the most CPU consuming spots in the application. Increasing the stack dump depth with (say) C<-jp=2> may help to point out the main callers or callees of hotspots. But sample aggregation is still flat per unique stack dump. To get a two-level view (split view) of callers/callees, use C<-jp=s> or C<-jp=-s>. The percentages shown for the second level are relative to the first level. To see how much time is spent in each line relative to a function, use C<-jp=fl>. To see how much time is spent in different VM states or zones, use C<-jp=v> or C<-jp=z>. Combinations of C with C produce two-level views, e.g. C<-jp=vf> or C<-jp=fv>. This shows the time spent in a VM state or zone vs. hotspots. This can be used to answer questions like "Which time consuming functions are only interpreted?" or "What's the garbage collector overhead for a specific function?". Multiple options can be combined E but not all combinations make sense, see above. E.g. C<-jp=3si4m1> samples three stack levels deep in 4ms intervals and shows a split view of the CPU consuming functions and their callers with a 1% threshold. Source code annotations produced by C<-jp=a> or C<-jp=A> are always flat and at the line level. Obviously, the source code files need to be readable by the profiler script. The high-level profiler can also be started and stopped from Lua code with: require("jit.p").start(options, output) ... require("jit.p").stop() =head2 C E Zones Zones can be used to provide information about different parts of an application to the high-level profiler. E.g. a game could make use of an C<"AI"> zone, a C<"PHYS"> zone, etc. Zones are hierarchical, organized as a stack. The C module needs to be loaded explicitly: local zone = require("jit.zone") =over =item * C pushes a named zone to the zone stack. =item * C pops the current zone from the zone stack and returns its name. =item * C returns the current zone name or C. =item * C flushes the zone stack. =back To show the time spent in each zone use C<-jp=z>. To show the time spent relative to hotspots use e.g. C<-jp=zf> or C<-jp=fz>. =head2 Low-level Lua API The C module gives access to the low-level API of the profiler from Lua code. This module needs to be loaded explicitly: local profile = require("jit.profile") This module can be used to implement your own higher-level profiler. A typical profiling run starts the profiler, captures stack dumps in the profiler callback, adds them to a hash table to aggregate the number of samples, stops the profiler and then analyzes all of the captured stack dumps. Other parameters can be sampled in the profiler callback, too. But it's important not to spend too much time in the callback, since this may skew the statistics. =head2 C E Start profiler This function starts the profiler. The C argument is a string holding options: =over =item * C E Profile with precision down to the function level. =item * C E Profile with precision down to the line level. =item * CnumberE> E Sampling interval in milliseconds (default 10ms). Note: The actual sampling precision is OS-dependent. =back The C argument is a callback function which is called with three arguments: C<(thread, samples, vmstate)>. The callback is called on a separate coroutine, the C argument is the state that holds the stack to sample for profiling. Note: do I modify the stack of that state or call functions on it. C gives the number of accumulated samples since the last callback (usually 1). C holds the VM state at the time the profiling timer triggered. This may or may not correspond to the state of the VM when the profiling callback is called. The state is either C<'N'> native (compiled) code, C<'I'> interpreted code, C<'C'> C code, C<'G'> the garbage collector, or C<'J'> the JIT compiler. =head2 C E Stop profiler This function stops the profiler. =head2 C E Dump stack This function allows taking stack dumps in an efficient manner. It returns a string with a stack dump for the C (coroutine), formatted according to the C argument: =over =item * C

E Preserve the full path for module names. Otherwise only the file name is used. =item * C E Dump the function name if it can be derived. Otherwise use module:line. =item * C E Ditto, but dump module:name. =item * C E Dump module:line. =item * C E Zap the following characters for the last dumped frame. =item * All other characters are added verbatim to the output string. =back The C argument gives the number of frames to dump, starting at the topmost frame of the thread. A negative number dumps the frames in inverse order. The first example prints a list of the current module names and line numbers of up to 10 frames in separate lines. The second example prints semicolon-separated function names for all frames (up to 100) in inverse order: print(profile.dumpstack(thread, "l\n", 10)) print(profile.dumpstack(thread, "lZ;", -100)) =head2 Low-level C API The profiler can be controlled directly from C code, e.g. for use by IDEs. The declarations are in C<"luajit.h"> (see Lua/C API extensions). =head2 C E Start profiler This function starts the profiler. See above for a description of the C argument. The C argument is a callback function with the following declaration: typedef void (*luaJIT_profile_callback)(void *data, lua_State *L, int samples, int vmstate); C is available for use by the callback. C is the state that holds the stack to sample for profiling. Note: do I modify this stack or call functions on this stack E use a separate coroutine for this purpose. See above for a description of C and C. =head2 C E Stop profiler This function stops the profiler. =head2 C

E Dump stack This function allows taking stack dumps in an efficient manner. See above for a description of C and C. This function returns a C pointing to a private string buffer of the profiler. The C argument returns the length of the output string. The buffer is overwritten on the next call and deallocated when the profiler stops. You either need to consume the content immediately or copy it for later use. ---- Copyright E 2005-2017 Mike Pall E Contact =cut #Pod::HTML2Pod conversion notes: #From file ext_profiler.html # 13135 bytes of input #Mon May 14 13:19:16 2018 agentzh # No a_name switch not specified, so will not try to render # No a_href switch not specified, so will not try to render