98_apptime.pm 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. ################################################################
  2. # 98_apptime:application timing
  3. # $Id: 98_apptime.pm 14087 2017-04-23 13:45:38Z martinp876 $
  4. ################################################################
  5. #####################################################
  6. #
  7. package main;
  8. use strict;
  9. use warnings;
  10. use vars qw(%defs); # FHEM device/button definitions
  11. use vars qw(%intAt);
  12. use vars qw($nextat);
  13. sub apptime_Initialize($);
  14. my $apptimeStatus;
  15. sub apptime_Initialize($){
  16. $apptimeStatus = 1;#set active by default
  17. $cmds{"apptime"}{Fn} = "apptime_CommandDispTiming";
  18. $cmds{"apptime"}{Hlp} = "[clear|<field>] [top|all] [<filter>],application function calls and duration";
  19. }
  20. sub HandleTimeout() {
  21. return undef if(!$nextat);
  22. my $now = gettimeofday();
  23. return ($nextat-$now) if($now < $nextat);
  24. $now += 0.01;# need to cover min delay at least
  25. $nextat = 0;
  26. #############
  27. # Check the internal list.
  28. foreach my $i (sort { $intAt{$a}{TRIGGERTIME} <=>
  29. $intAt{$b}{TRIGGERTIME} } keys %intAt) {
  30. my $tim = $intAt{$i}{TRIGGERTIME};
  31. my $fn = $intAt{$i}{FN};
  32. if(!defined($tim) || !defined($fn)) {
  33. delete($intAt{$i});
  34. next;
  35. } elsif($tim <= $now) {
  36. my $arg = $intAt{$i}{ARG};
  37. $arg = "" if (!$arg);
  38. apptime_getTiming("global","tmr-".$fn.";".$arg,$fn,$tim,$arg);
  39. delete($intAt{$i});
  40. } else {
  41. $nextat = $tim if(!$nextat || $nextat > $tim);
  42. }
  43. }
  44. return undef if(!$nextat);
  45. $now = gettimeofday(); # possibly some tasks did timeout in the meantime
  46. # we will cover them
  47. return ($now+ 0.01 < $nextat) ? ($nextat-$now) : 0.01;
  48. }
  49. sub CallFn(@) {
  50. my $d = shift;
  51. my $n = shift;
  52. if(!$d || !$defs{$d}) {
  53. $d = "<undefined>" if(!defined($d));
  54. Log 0, "Strange call for nonexistent $d: $n";
  55. return undef;
  56. }
  57. if(!$defs{$d}{TYPE}) {
  58. Log 0, "Strange call for typeless $d: $n";
  59. return undef;
  60. }
  61. my $fn = $modules{$defs{$d}{TYPE}}{$n};
  62. return "" if(!$fn);
  63. my @ret = apptime_getTiming($d,$fn,$fn,0,@_);
  64. if(wantarray){return @ret;}
  65. else {return $ret[0];}
  66. }
  67. sub apptime_getTiming($$$@) {
  68. my ($e,$fnName,$fn,$tim,@arg) = @_;
  69. my $h;
  70. my $ts1;
  71. if ($apptimeStatus){
  72. if (!$defs{$e}{helper} ||
  73. !$defs{$e}{helper}{bm} ||
  74. !$defs{$e}{helper}{bm}{$fnName} ){
  75. %{$defs{$e}{helper}{bm}{$fnName}} =(max => 0, mAr => "",
  76. cnt => 1, tot => 0,
  77. dmx => 0, mTS => "");
  78. $h = $defs{$e}{helper}{bm}{$fnName};
  79. }
  80. else{
  81. $h = $defs{$e}{helper}{bm}{$fnName};
  82. $h->{cnt}++;
  83. }
  84. $ts1 = gettimeofday();
  85. if ($tim){
  86. my $td = int(($ts1-$tim)*1000);
  87. $h->{dmx} = $td if ($h->{dmx} < $td);
  88. }
  89. }
  90. no strict "refs";
  91. my @ret = &{$fn}(@arg);
  92. use strict "refs";
  93. if ($apptimeStatus){
  94. $ts1 = int((gettimeofday()-$ts1)*1000);
  95. if ($ts1 && $h->{max} < $ts1){
  96. $h->{max} = $ts1;
  97. $h->{mAr} = \@arg;
  98. $h->{mTS}= strftime("%d.%m. %H:%M:%S", localtime());
  99. }
  100. $h->{tot} += $ts1;
  101. }
  102. return @ret;
  103. }
  104. #####################################
  105. sub apptime_CommandDispTiming($$@) {
  106. my ($cl,$param) = @_;
  107. my ($sFld,$top,$filter) = split" ",$param;
  108. $sFld = "max" if (!$sFld);
  109. $top = "top" if (!$top);
  110. my %fld = (name=>0,funktion=>1,max=>2,count=>3,total=>4,average=>5,maxDly=>6,cont=>98,pause=>98,clear=>99);
  111. return "$sFld undefined field, use one of ".join(",",keys %fld)
  112. if(!defined $fld{$sFld});
  113. my @bmArr;
  114. my @a = map{"$defs{$_}:$_"} keys (%defs); # prepare mapping hash 2 name
  115. $_ =~ s/[HASH\(\)]//g foreach(@a);
  116. if ($sFld eq "pause"){# no further collection of data, clear also
  117. $apptimeStatus = 0;#stop collecting data
  118. }
  119. elsif ($sFld eq "cont"){# no further collection of data, clear also
  120. $apptimeStatus = 1;#continue collecting data
  121. }
  122. foreach my $d (sort keys %defs) {
  123. next if(!$defs{$d}{helper}||!$defs{$d}{helper}{bm});
  124. if ($sFld eq "clear"){
  125. delete $defs{$d}{helper}{bm};
  126. }
  127. elsif ($sFld =~ m/(pause|cont)/){
  128. }
  129. else{
  130. foreach my $f (sort keys %{$defs{$d}{helper}{bm}}) {
  131. next if(!defined $defs{$d}{helper}{bm}{$f}{cnt});
  132. next if($filter && $d !~ m/$filter/ && $f !~ m/$filter/);
  133. my ($n,$t) = ($d,$f);
  134. ($n,$t) = split(";",$f,2) if ($d eq "global");
  135. $t = "" if (!defined $t);
  136. my $h = $defs{$d}{helper}{bm}{$f};
  137. my $arg = "";
  138. if ($h->{mAr} && scalar(@{$h->{mAr}})){
  139. foreach my $i (0..scalar(@{$h->{mAr}})){
  140. if(ref(${$h->{mAr}}[$i]) eq 'HASH' and exists(${$h->{mAr}}[$i]->{NAME})){
  141. ${$h->{mAr}}[$i] = "HASH(".${$h->{mAr}}[$i]->{NAME}.")";
  142. }
  143. }
  144. $arg = join ("; ", map { $_ // "(undef)" } @{$h->{mAr}});
  145. }
  146. push @bmArr,[($n,$t
  147. ,$h->{max}
  148. ,$h->{cnt}
  149. ,$h->{tot}
  150. ,$h->{tot} /$h->{cnt}
  151. ,$h->{dmx}
  152. ,$h->{mTS}
  153. ,$arg
  154. )];
  155. }
  156. }
  157. }
  158. my $field = $fld{$sFld};
  159. if ($field>1){@bmArr = sort { $b->[$field] <=> $a->[$field] } @bmArr;}
  160. else {@bmArr = sort { $b->[$field] cmp $a->[$field] } @bmArr;}
  161. my $ret = ($apptimeStatus ? "" : "------ apptime PAUSED data collection ----------\n")
  162. .sprintf("\n %-40s %-35s %6s %6s %8s %8s %6s %-15s %s",
  163. "name","function","max","count","total","average","maxDly","TS Max call","param Max call");
  164. my $end = ($top && $top eq "top")?20:@bmArr-1;
  165. $end = @bmArr-1 if ($end>@bmArr-1);
  166. $ret .= sprintf("\n %-40s %-35s %6d %6d %8d %8.2f %6d %-15s %s",@{$bmArr[$_]})for (0..$end);
  167. return $ret;
  168. }
  169. 1;
  170. =pod
  171. =item command
  172. =item summary support to analyse function performance
  173. =item summary_DE Unterst&uuml;tzung bei der Performanceanalyse von Funktionen
  174. =begin html
  175. <a name="apptime"></a>
  176. <h3>apptime</h3>
  177. <div style="padding-left: 2ex;">
  178. <h4><code>apptime</code></h4>
  179. <p>
  180. apptime provides information about application procedure execution time.
  181. It is designed to identify long running jobs causing latency as well as
  182. general high <abbr>CPU</abbr> usage jobs.
  183. </p>
  184. <p>
  185. No information about <abbr>FHEM</abbr> kernel times and delays will be provided.
  186. </p>
  187. <p>
  188. Once started, apptime monitors tasks. User may reset counter during operation.
  189. apptime adds about 1% <abbr>CPU</abbr> load in average to <abbr>FHEM</abbr>.
  190. </p>
  191. <p>
  192. In order to remove apptime, <kbd>shutdown restart</kbd> is necessary.
  193. </p>
  194. <p>
  195. <strong>Features</strong>
  196. </P>
  197. <dl>
  198. <dt><code><kbd>apptime</kbd></code></dt>
  199. <dd>
  200. <p>
  201. <kbd>apptime</kbd> is started with the its first call and continously monitor operations.<br>
  202. To unload apptime, <kbd>shutdown restart</kbd> is necessary.<br> </li>
  203. </p>
  204. </dd>
  205. <dt><code><kbd>apptime clear</code></dt>
  206. <dd>
  207. <p>
  208. Reset all counter and start from zero.
  209. </p>
  210. </dd>
  211. <dt><code><kbd>apptime pause</code></dt>
  212. <dd>
  213. <p>
  214. Suspend accumulation of data. Data is not cleared.
  215. </p>
  216. </dd>
  217. <dt><code><kbd>apptime cont</code></dt>
  218. <dd>
  219. <p>
  220. Continue data collection after pause.
  221. </p>
  222. </dd>
  223. <dt><code><kbd>apptime [count|funktion|average|clear|max|name|total] [all]</kbd></code></dt>
  224. <dd>
  225. <p>
  226. Display a table sorted by the field selected.
  227. </p>
  228. <p>
  229. <strong><kbd>all</kbd></strong> will display the complete table while by default only the top lines are printed.<
  230. </p>
  231. </dd>
  232. </dl>
  233. <p>
  234. <strong>Columns:</strong>
  235. </p>
  236. <dl>
  237. <dt><strong>name</strong></dt>
  238. <dd>
  239. <p>
  240. Name of the entity executing the procedure.
  241. </p>
  242. <p>
  243. If it is a function called by InternalTimer the name starts with <var>tmr-</var>.
  244. By then it gives the name of the function to be called.
  245. </p>
  246. </dd>
  247. <dt><strong>funktion</strong><dt>
  248. <dd>
  249. <p>
  250. Procedure name which was executed.
  251. </p>
  252. <p>
  253. If it is an <var>InternalTimer</var> call it gives its calling parameter.
  254. </p>
  255. </dd>
  256. <dt><strong>max</strong></dt>
  257. <dd>
  258. <p>
  259. Longest duration measured for this procedure in <abbr>ms</abbr>.
  260. </p>
  261. </dd>
  262. <dt><strong>count</strong></dt>
  263. <dd>
  264. <p>
  265. Number of calls for this procedure.
  266. </p>
  267. </dt>
  268. <dt><strong>total</strong></dt>
  269. <dd>
  270. <p>
  271. Accumulated duration of this procedure over all calls monitored.
  272. </p>
  273. </dd>
  274. <dt><strong>average</strong></dt>
  275. <dd>
  276. <p>
  277. Average time a call of this procedure takes.
  278. </p>
  279. </dd>
  280. <dt><strong>maxDly</strong></dt>
  281. <dd>
  282. <p>
  283. Maximum delay of a timer call to its schedules time.
  284. This column is not relevant for non-timer calls.
  285. </p>
  286. </dd>
  287. <dt><strong>param Max call</strong></dt>
  288. <dd>
  289. <p>
  290. Gives the parameter of the call with the longest duration.
  291. </p>
  292. </dd>
  293. </dl>
  294. </div>
  295. =end html
  296. =cut