-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request NixOS#2782 from grahamc/flames
Track function start and end
- Loading branch information
Showing
7 changed files
with
189 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/env nix-shell | ||
#!nix-shell -i python3 -p python3 --pure | ||
|
||
# To be used with `--trace-function-calls` and `-vvvv` and | ||
# `flamegraph.pl`. | ||
# | ||
# For example: | ||
# | ||
# nix-instantiate --trace-function-calls -vvvv '<nixpkgs>' -A hello 2> nix-function-calls.trace | ||
# ./contrib/stack-collapse.py nix-function-calls.trace > nix-function-calls.folded | ||
# nix-shell -p flamegraph --run "flamegraph.pl nix-function-calls.folded > nix-function-calls.svg" | ||
|
||
import sys | ||
from pprint import pprint | ||
import fileinput | ||
|
||
stack = [] | ||
timestack = [] | ||
|
||
for line in fileinput.input(): | ||
components = line.strip().split(" ", 2) | ||
if components[0] != "function-trace": | ||
continue | ||
|
||
direction = components[1] | ||
components = components[2].rsplit(" ", 2) | ||
|
||
loc = components[0] | ||
_at = components[1] | ||
time = int(components[2]) | ||
|
||
if direction == "entered": | ||
stack.append(loc) | ||
timestack.append(time) | ||
elif direction == "exited": | ||
dur = time - timestack.pop() | ||
vst = ";".join(stack) | ||
print(f"{vst} {dur}") | ||
stack.pop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
|
||
#include "eval.hh" | ||
#include <sys/time.h> | ||
|
||
namespace nix { | ||
|
||
struct FunctionCallTrace | ||
{ | ||
const Pos & pos; | ||
|
||
FunctionCallTrace(const Pos & pos) : pos(pos) { | ||
auto duration = std::chrono::high_resolution_clock::now().time_since_epoch(); | ||
auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(duration); | ||
vomit("function-trace entered %1% at %2%", pos, ns.count()); | ||
} | ||
|
||
~FunctionCallTrace() { | ||
auto duration = std::chrono::high_resolution_clock::now().time_since_epoch(); | ||
auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(duration); | ||
vomit("function-trace exited %1% at %2%", pos, ns.count()); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
source common.sh | ||
|
||
set +x | ||
|
||
expect_trace() { | ||
expr="$1" | ||
expect="$2" | ||
actual=$( | ||
nix-instantiate \ | ||
--trace-function-calls \ | ||
-vvvv \ | ||
--expr "$expr" 2>&1 \ | ||
| grep "function-trace" \ | ||
| sed -e 's/ [0-9]*$//' | ||
); | ||
|
||
echo -n "Tracing expression '$expr'" | ||
set +e | ||
msg=$(diff -swB \ | ||
<(echo "$expect") \ | ||
<(echo "$actual") | ||
); | ||
result=$? | ||
set -e | ||
if [ $result -eq 0 ]; then | ||
echo " ok." | ||
else | ||
echo " failed. difference:" | ||
echo "$msg" | ||
return $result | ||
fi | ||
} | ||
|
||
# failure inside a tryEval | ||
expect_trace 'builtins.tryEval (throw "example")' " | ||
function-trace entered undefined position at | ||
function-trace exited undefined position at | ||
function-trace entered (string):1:1 at | ||
function-trace entered (string):1:19 at | ||
function-trace exited (string):1:19 at | ||
function-trace exited (string):1:1 at | ||
" | ||
|
||
# Missing argument to a formal function | ||
expect_trace '({ x }: x) { }' " | ||
function-trace entered undefined position at | ||
function-trace exited undefined position at | ||
function-trace entered (string):1:1 at | ||
function-trace exited (string):1:1 at | ||
" | ||
|
||
# Too many arguments to a formal function | ||
expect_trace '({ x }: x) { x = "x"; y = "y"; }' " | ||
function-trace entered undefined position at | ||
function-trace exited undefined position at | ||
function-trace entered (string):1:1 at | ||
function-trace exited (string):1:1 at | ||
" | ||
|
||
# Not enough arguments to a lambda | ||
expect_trace '(x: y: x + y) 1' " | ||
function-trace entered undefined position at | ||
function-trace exited undefined position at | ||
function-trace entered (string):1:1 at | ||
function-trace exited (string):1:1 at | ||
" | ||
|
||
# Too many arguments to a lambda | ||
expect_trace '(x: x) 1 2' " | ||
function-trace entered undefined position at | ||
function-trace exited undefined position at | ||
function-trace entered (string):1:1 at | ||
function-trace exited (string):1:1 at | ||
function-trace entered (string):1:1 at | ||
function-trace exited (string):1:1 at | ||
" | ||
|
||
# Not a function | ||
expect_trace '1 2' " | ||
function-trace entered undefined position at | ||
function-trace exited undefined position at | ||
function-trace entered (string):1:1 at | ||
function-trace exited (string):1:1 at | ||
" | ||
|
||
set -e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters