ab6d1c2658413945c40d6e6abb936ace975080fb
[beam_stats.git] / src / beam_stats_process.erl
1 -module(beam_stats_process).
2
3 -include("include/beam_stats_process_ancestry.hrl").
4 -include("include/beam_stats_process.hrl").
5
6 -export_type(
7 [ t/0
8 , status/0
9 , ancestry/0
10 , best_known_origin/0
11 ]).
12
13 -export(
14 [ of_pid/1
15 , get_best_known_origin/1
16 , print/1
17 ]).
18
19 -type status() ::
20 exiting
21 | garbage_collecting
22 | runnable
23 | running
24 | suspended
25 | waiting
26 .
27
28 -type ancestry() ::
29 #beam_stats_process_ancestry{}.
30
31 -type best_known_origin() ::
32 {registered_name , atom()}
33 | {ancestry , ancestry()}
34 .
35
36 -define(T, #?MODULE).
37
38 -type t() ::
39 ?T{}.
40
41 %% ============================================================================
42 %% Public API
43 %% ============================================================================
44
45 -spec of_pid(pid()) ->
46 t().
47 of_pid(Pid) ->
48 Dict = pid_info_exn(Pid, dictionary),
49 Ancestry =
50 #beam_stats_process_ancestry
51 { raw_initial_call = pid_info_exn(Pid, initial_call)
52 , otp_initial_call = hope_kv_list:get(Dict, '$initial_call')
53 , otp_ancestors = hope_kv_list:get(Dict, '$ancestors')
54 },
55 ?T
56 { pid = Pid
57 , registered_name = pid_info_opt(Pid, registered_name)
58 , ancestry = Ancestry
59 , status = pid_info_exn(Pid, status)
60 , memory = pid_info_exn(Pid, memory)
61 , total_heap_size = pid_info_exn(Pid, total_heap_size)
62 , stack_size = pid_info_exn(Pid, stack_size)
63 , message_queue_len = pid_info_exn(Pid, message_queue_len)
64 }.
65
66 -spec print(t()) ->
67 ok.
68 print(
69 ?T
70 { pid = Pid
71 , registered_name = RegisteredNameOpt
72 , ancestry = #beam_stats_process_ancestry
73 { raw_initial_call = InitialCallRaw
74 , otp_initial_call = InitialCallOTPOpt
75 , otp_ancestors = AncestorsOpt
76 }
77 , status = Status
78 , memory = Memory
79 , total_heap_size = TotalHeapSize
80 , stack_size = StackSize
81 , message_queue_len = MsgQueueLen
82 }=T
83 ) ->
84 BestKnownOrigin = get_best_known_origin(T),
85 io:format("--------------------------------------------------~n"),
86 io:format(
87 "Pid : ~p~n"
88 "BestKnownOrigin : ~p~n"
89 "RegisteredNameOpt : ~p~n"
90 "InitialCallRaw : ~p~n"
91 "InitialCallOTPOpt : ~p~n"
92 "AncestorsOpt : ~p~n"
93 "Status : ~p~n"
94 "Memory : ~p~n"
95 "TotalHeapSize : ~p~n"
96 "StackSize : ~p~n"
97 "MsgQueueLen : ~p~n"
98 "~n",
99 [ Pid
100 , BestKnownOrigin
101 , RegisteredNameOpt
102 , InitialCallRaw
103 , InitialCallOTPOpt
104 , AncestorsOpt
105 , Status
106 , Memory
107 , TotalHeapSize
108 , StackSize
109 , MsgQueueLen
110 ]
111 ).
112
113 %% ============================================================================
114 %% Private helpers
115 %% ============================================================================
116
117 pid_info_exn(Pid, Key) ->
118 {some, Value} = pid_info_opt(Pid, Key),
119 Value.
120
121 pid_info_opt(Pid, Key) ->
122 case {Key, erlang:process_info(Pid, Key)}
123 of {registered_name, []} -> none
124 ; {_ , {Key, Value}} -> {some, Value}
125 end.
126
127 %% ============================================================================
128 %% Process code origin approximation or naming the anonymous processes.
129 %%
130 %% At runtime, given a PID, how precicely can we identify the origin of the
131 %% code it is running?
132 %%
133 %% We have these data points:
134 %%
135 %% - Sometimes | registered name (if so, we're done)
136 %% - Sometimes | ancestor PIDs or registered names
137 %% - Always | initial_call (can be too generic, such as erlang:apply)
138 %% - Always | current_function (can be too far down the stack)
139 %% - Always | current_location (can be too far down the stack)
140 %% - Potentially | application tree, but maybe expensive to compute, need to check
141 %% ============================================================================
142
143 -define(TAG(Tag), fun (X) -> {Tag, X} end).
144
145 -spec get_best_known_origin(t()) ->
146 best_known_origin().
147 get_best_known_origin(?T{registered_name={some, RegisteredName}}) ->
148 {registered_name, RegisteredName};
149 get_best_known_origin(?T{registered_name=none, ancestry=Ancestry}) ->
150 {ancestry, Ancestry}.
This page took 0.057469 seconds and 3 git commands to generate.