1%% 2%% %CopyrightBegin% 3%% 4%% Copyright Ericsson AB 2002-2017. All Rights Reserved. 5%% 6%% Licensed under the Apache License, Version 2.0 (the "License"); 7%% you may not use this file except in compliance with the License. 8%% You may obtain a copy of the License at 9%% 10%% http://www.apache.org/licenses/LICENSE-2.0 11%% 12%% Unless required by applicable law or agreed to in writing, software 13%% distributed under the License is distributed on an "AS IS" BASIS, 14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15%% See the License for the specific language governing permissions and 16%% limitations under the License. 17%% 18%% %CopyrightEnd% 19%% 20-module(etop_txt). 21-author('siri@erix.ericsson.se'). 22 23%%-compile(export_all). 24-export([init/1,stop/1]). 25-export([do_update/4]). 26 27-include("etop.hrl"). 28-include("etop_defs.hrl"). 29 30-import(etop,[loadinfo/2,meminfo/2]). 31 32stop(Pid) -> Pid ! stop. 33 34init(Config) -> 35 loop(#etop_info{},Config). 36 37loop(Prev,Config) -> 38 Info = do_update(Prev,Config), 39 receive 40 stop -> stopped; 41 {dump,Fd} -> do_update(Fd,Info,Prev,Config), loop(Info,Config); 42 {config,_,Config1} -> loop(Info,Config1) 43 after Config#opts.intv -> loop(Info,Config) 44 end. 45 46do_update(Prev,Config) -> 47 Info = etop:update(Config), 48 do_update(standard_io,Info,Prev,Config). 49 50do_update(Fd,Info,Prev,Config) -> 51 {Cpu,NProcs,RQ,Clock} = loadinfo(Info,Prev), 52 io:nl(Fd), 53 writedoubleline(Fd), 54 case Info#etop_info.memi of 55 undefined -> 56 io:fwrite(Fd, " ~-72w~10s~n" 57 " Load: cpu ~8w~n" 58 " procs~8w~n" 59 " runq ~8w~n", 60 [Config#opts.node,Clock, 61 Cpu,NProcs,RQ]); 62 Memi -> 63 [Tot,Procs,Atom,Bin,Code,Ets] = 64 meminfo(Memi, [total,processes,atom,binary,code,ets]), 65 io:fwrite(Fd, ?SYSFORM, 66 [Config#opts.node,Clock, 67 Cpu,Tot,Bin, 68 NProcs,Procs,Code, 69 RQ,Atom,Ets]) 70 end, 71 io:nl(Fd), 72 writepinfo_header(Fd), 73 writesingleline(Fd), 74 writepinfo(Fd,Info#etop_info.procinfo,modifier(Fd)), 75 writedoubleline(Fd), 76 io:nl(Fd), 77 Info. 78 79writepinfo_header(Fd) -> 80 io:fwrite(Fd,"Pid Name or Initial Func Time Reds Memory MsgQ Current Function~n",[]). 81 82writesingleline(Fd) -> 83 io:fwrite(Fd,"----------------------------------------------------------------------------------------~n",[]). 84writedoubleline(Fd) -> 85 io:fwrite(Fd,"========================================================================================~n",[]). 86 87writepinfo(Fd,[#etop_proc_info{pid=Pid, 88 mem=Mem, 89 reds=Reds, 90 name=Name, 91 runtime=Time, 92 cf=MFA, 93 mq=MQ} 94 |T], 95 Modifier) -> 96 io:fwrite(Fd,proc_format(Modifier), 97 [Pid,to_string(Name,Modifier),Time,Reds,Mem,MQ, 98 to_string(MFA,Modifier)]), 99 writepinfo(Fd,T,Modifier); 100writepinfo(_Fd,[],_) -> 101 ok. 102 103proc_format(Modifier) -> 104 "~-15w~-20"++Modifier++"s~8w~8w~8w~8w ~-20"++Modifier++"s~n". 105 106to_string({M,F,A},Modifier) -> 107 io_lib:format("~w:~"++Modifier++"w/~w",[M,F,A]); 108to_string(Other,Modifier) -> 109 io_lib:format("~"++Modifier++"w",[Other]). 110 111modifier(Device) -> 112 case encoding(Device) of 113 latin1 -> ""; 114 _ -> "t" 115 end. 116 117encoding(Device) -> 118 case io:getopts(Device) of 119 List when is_list(List) -> 120 proplists:get_value(encoding,List,latin1); 121 _ -> 122 latin1 123 end. 124 125