| 1 | -module(beam_stats_process). |
| 2 | |
| 3 | -include("include/beam_stats_process.hrl"). |
| 4 | |
| 5 | -export_type( |
| 6 | [ t/0 |
| 7 | , status/0 |
| 8 | ]). |
| 9 | |
| 10 | -export( |
| 11 | [ of_pid/1 |
| 12 | , print/1 |
| 13 | ]). |
| 14 | |
| 15 | -type status() :: |
| 16 | exiting |
| 17 | | garbage_collecting |
| 18 | | runnable |
| 19 | | running |
| 20 | | suspended |
| 21 | | waiting |
| 22 | . |
| 23 | |
| 24 | -define(T, #?MODULE). |
| 25 | |
| 26 | -type t() :: |
| 27 | ?T{}. |
| 28 | |
| 29 | %% ============================================================================ |
| 30 | %% Public API |
| 31 | %% ============================================================================ |
| 32 | |
| 33 | -spec of_pid(pid()) -> |
| 34 | t(). |
| 35 | of_pid(Pid) -> |
| 36 | Dict = pid_info_exn(Pid, dictionary), |
| 37 | ?T |
| 38 | { pid = Pid |
| 39 | , registered_name = pid_info_opt(Pid, registered_name) |
| 40 | , raw_initial_call = pid_info_exn(Pid, initial_call) |
| 41 | , otp_initial_call = hope_kv_list:get(Dict, '$initial_call') |
| 42 | , otp_ancestors = hope_kv_list:get(Dict, '$ancestors') |
| 43 | , status = pid_info_exn(Pid, status) |
| 44 | , memory = pid_info_exn(Pid, memory) |
| 45 | , total_heap_size = pid_info_exn(Pid, total_heap_size) |
| 46 | , stack_size = pid_info_exn(Pid, stack_size) |
| 47 | , message_queue_len = pid_info_exn(Pid, message_queue_len) |
| 48 | }. |
| 49 | |
| 50 | -spec print(t()) -> |
| 51 | ok. |
| 52 | print( |
| 53 | ?T |
| 54 | { pid = Pid |
| 55 | , registered_name = RegisteredNameOpt |
| 56 | , raw_initial_call = InitialCallRaw |
| 57 | , otp_initial_call = InitialCallOTPOpt |
| 58 | , otp_ancestors = AncestorsOpt |
| 59 | , status = Status |
| 60 | , memory = Memory |
| 61 | , total_heap_size = TotalHeapSize |
| 62 | , stack_size = StackSize |
| 63 | , message_queue_len = MsgQueueLen |
| 64 | } |
| 65 | ) -> |
| 66 | io:format("--------------------------------------------------~n"), |
| 67 | io:format( |
| 68 | "Pid : ~p~n" |
| 69 | "RegisteredNameOpt : ~p~n" |
| 70 | "InitialCallRaw : ~p~n" |
| 71 | "InitialCallOTPOpt : ~p~n" |
| 72 | "AncestorsOpt : ~p~n" |
| 73 | "Status : ~p~n" |
| 74 | "Memory : ~p~n" |
| 75 | "TotalHeapSize : ~p~n" |
| 76 | "StackSize : ~p~n" |
| 77 | "MsgQueueLen : ~p~n" |
| 78 | "~n", |
| 79 | [ Pid |
| 80 | , RegisteredNameOpt |
| 81 | , InitialCallRaw |
| 82 | , InitialCallOTPOpt |
| 83 | , AncestorsOpt |
| 84 | , Status |
| 85 | , Memory |
| 86 | , TotalHeapSize |
| 87 | , StackSize |
| 88 | , MsgQueueLen |
| 89 | ] |
| 90 | ). |
| 91 | |
| 92 | %% ============================================================================ |
| 93 | %% Private helpers |
| 94 | %% ============================================================================ |
| 95 | |
| 96 | pid_info_exn(Pid, Key) -> |
| 97 | {some, Value} = pid_info_opt(Pid, Key), |
| 98 | Value. |
| 99 | |
| 100 | pid_info_opt(Pid, Key) -> |
| 101 | case {Key, erlang:process_info(Pid, Key)} |
| 102 | of {registered_name, []} -> none |
| 103 | ; {_ , {Key, Value}} -> {some, Value} |
| 104 | end. |