Matt S Trout > Rakudo-Star-2012.08_001 > tools/dev/


Annotate this POD





Convert the output of Parrot's profiling runcore to a Callgrind-compatible format.


perl tools/dev/ parrot.pprof.1234


Generate a profile by passing -Rprofiling to parrot, for example ./parrot -Rprofiling perl6.pbc hello.p6. Once execution completes, parrot will print a message specifying the location of the parrot profile (pprof). The profile will be named parrot.pprof.XXXX, where XXXX is the PID of the parrot process unless another name is specified by the PARROT_PROFILING_OUTPUT environment variable.

To generate a Callgrind-compatible profile, run this script with the pprof filename as the first argument. The output file usable by kcachegrind will be in parrot.out.XXXX, where XXXX again is the PID of the original parrot process.



If the environment variable PARROT_PROFILING_OUTPUT is set, the profiling runcore will attempt to use its value as the profile filename. Note that it does not check whether the file already exists and will happily overwrite existing files.



Parrot's execution model is built on continuation-passing style and does not precisely fit the straightforward function-based format that Callgrind-compatible tools expect. For this reason, the profiling runcore captures information about context switches (CS lines in the pprof file) and maintains a context stack that functions similarly to a typical call stack. then maps these context switches as if they were function calls and returns. See $call_stack for more information.


Variables which are named $call_stack hold a reference to an array of hashes which contain information about the currently active contexts. When collecting timing information about an op, it is necessary to add that information to all function calls on the stack because Callgrind-compatible tools expect the cost of a function call to include the cost of all calls made by that function, etc.

When a context switch is detected, process_line looks at the context stack to determine if the context switch looks like a function call (if the context hasn't been seen before) or a return (if the context is somewhere on the stack). There are some other cases that the code handles, but these can be ignored for now in the interest of simplicity. If the context has been seen, process_line shifts contexts off the stack until it finds the context that has been switched to. When process_line detects a new context, it adds a fake op representing a function call to $stats and unshifts a new context onto the stack.

Each element of @$call_stack contains the information needed to uniquely identify the site of the original context switch.


Variables which are named $stats contain a reference to a deeply nested HoHoH.. which contains all information gathered about a profiled PIR program. The nested hashes and arrays refer to the file, namespace, line of source code and op number, respectively. The op number is used to allow multiple instructions per line because PIR instructions often represent multiple low-level instructions. This also makes it easy to inject pseudo-ops to represent function calls.

Each op always has a time value representing the total amount of time spent in that op. Ops may also have an op_name value that gives the name of the op. When control flow similar to a function call is detected, a pseudo-op representing a function call is injected. These pseudo-ops have zero cost when initialized and are used to determine the total time spent between when the context becomes active and when control flow returns to or past the context. Although they're not exactly like functions calls, they're close enough that it may help to think of them as such.

Uncomment the print_stats line in main to see a representation of the data contained in $stats.



This function is minimal driver for the other functions in this file, taking the name of a Parrot profile and writing a Callgrind-compatible profile to a similarly-named file.


This function takes a file handle open to a Parrot profile and a reference to a hash of fine-grained statistics about the current PIR program. It modifies the statistics according to the information from the Parrot profile.


This function prints a complete, human-readable representation of the statistical data that have been collected into the $stats argument to stdout. It is primarily intended to ease debugging and is not necessary to create a Callgrind-compatible profile.


This function adds statistical data to the $stats hash reference. The $loc argument specifies information such as the namespace, file, line and op number where the data should go. $time is an integer representing the amount of time spent at the specified location. $extra contains any ancillary data that should be stored in the hash. This includes data on (faked) subroutine calls and op names.


This function takes a reference to a hash of statistical information about a PIR program and returns a string containing a Callgrind-compatible profile. Although some information in the profile may not be accurate (namely PID and creator), tools such as kcachegrind are able to consume files generated by this function.

syntax highlighting: