1 %%%-------------------------------------------------------------------
3 %%% Author : Mats Cronqvist <etxmacr@cbe2077>
6 %%% Created : 14 Jan 2003 by Mats Cronqvist <etxmacr@cbe2077>
7 %%%-------------------------------------------------------------------
9 -export([go
/0,go
/1,go
/2,go
/3]).
11 -define(CALLBACK
, cb_foolprof
).
14 go(Proc
) -> go(Proc
, []).
15 go(Proc
, Prefix
) -> go(Proc
, Prefix
, -1).
16 go(Proc
, Prefix
, Count
) ->
18 Tot
= ets_lup(?CALLBACK
, {total
, time}),
19 Ptot
= ets_lup(?CALLBACK
, {{pid, time}, Pid
}),
20 stax(Count
, 0, Tot
, Pid
, Prefix
, ets:next(?CALLBACK
, {{stack
,time},Pid
,0})).
22 stax(M
, M
, _
, Pid
, _
, {_
,Pid
,_
}) -> {did
, M
, stacks
};
23 stax(M
, N
, Tot
, Pid
, Prefix
, {_
,Pid
,_
} = Key
) ->
24 {_
, Stak
, Time
} = ets_lup(?CALLBACK
, Key
),
25 case prefix(lists:reverse(Stak
), Prefix
) of
26 {yes
, Tail
} -> io:fwrite("~6w ~w~n", [N
, Tail
]);
29 stax(M
, N
+1, Tot
, Pid
, Prefix
, ets:next(?CALLBACK
, Key
));
30 stax(_
, _
, _
, Pid
, _
, _
) -> {Pid
, exhausted
}.
32 prefix(Rest
, '') -> prefix(Rest
, []);
33 prefix(Rest
, []) -> {yes
, Rest
};
34 prefix([MFA
|Rest
], [MFA
|Tail
]) -> prefix(Rest
, Tail
);
38 Tot
= ets_lup(?CALLBACK
, {total
, time}),
39 TPs
= ets:match(?CALLBACK
, {{{pid,time},'$2'},'$1'}),
40 RTs
= [{reg(P
), T
} || [T
, P
] <- TPs
],
41 F
= fun({R
, T
}, [{R
, N
, TT
}|O
]) ->
42 [{R
, N
+1, percent(T
, Tot
)+TT
}|O
];
44 [{R
, 1, percent(T
, Tot
)}|O
]
46 RNTs
= lists:foldl(F
, [], lists:sort(RTs
)),
47 [[length(TPs
), Tot
]|rsort(3, RNTs
)].
49 reg(P
) -> ets_lup(panScan
, {P
,registered_name
}).
50 rsort(Pos
, L
) -> lists:reverse(lists:keysort(Pos
, L
)).
52 percent(A
, B
) -> A
/B
*100.
55 key(Reg
) when atom(Reg
) ->
57 key(MFA
) when tuple(MFA
) ->
60 Pids
= ets:match(panScan
, {{'$1',registered_name
},PID
}),
63 exit({{?MODULE
, ?LINE
}, no_such_process
, PID
});
65 io:fwrite("only ~w procs with this tag~n", [length(Pids
)]),
68 [Pid
] = lists:nth(N
, Pids
),
72 case catch ets:lookup(Tab
, Tag
) of