| 1 | use strict; | ||
| 2 | package NetServer::Portal::Top; | ||
| 3 | use NetServer::Portal qw(term $Host); | ||
| 4 | use Event qw(all_watchers QUEUES time); | ||
| 5 | use Event::Stats 0.7 qw(round_seconds idle_time total_time); | ||
| 6 | |||
| 7 | NetServer::Portal->register(cmd => "top", | ||
| 8 | title => "Process Top", | ||
| 9 | package => __PACKAGE__); | ||
| 10 | |||
| 11 | use vars qw($NextID %ID2W %W2ID %OldW2ID); | ||
| 12 | |||
| 13 | # keep state in a ref that can be saved with Storable... | ||
| 14 | |||
| 15 | sub new { | ||
| 16 | my ($class, $client) = @_; | ||
| 17 | my $o = $client->conf(__PACKAGE__); | ||
| 18 | $o->{seconds} ||= round_seconds(60); | ||
| 19 | $o->{filter} ||= ''; | ||
| 20 | $o->{by} ||= 't'; | ||
| 21 | $o->{page} ||= 1; | ||
| 22 | $NextID ||= 1; | ||
| 23 | $o; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub enter { | ||
| 27 | my ($o, $c) = @_; | ||
| 28 | Event::Stats::collect(1); | ||
| 29 | $c->{io}->timeout(4); | ||
| 30 | } | ||
| 31 | |||
| 32 | sub leave { | ||
| 33 | # there's probably a better way to do this...? XXX | ||
| 34 | Event->timer(desc => 'NetServer::Portal::Top->leave', | ||
| 35 | after => 15 * 60, cb => sub { | ||
| 36 | my $e = shift; | ||
| 37 | $e->w->cancel; | ||
| 38 | if (!Event::Stats::collect(-1)) { | ||
| 39 | %ID2W = (); | ||
| 40 | } | ||
| 41 | }); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub reset_idmap { | ||
| 45 | %OldW2ID = %W2ID; | ||
| 46 | if ($NextID > 2*keys %OldW2ID) { | ||
| 47 | %OldW2ID=(); # too much fragmentation, start over | ||
| 48 | } | ||
| 49 | $NextID = 1; | ||
| 50 | %W2ID=(); | ||
| 51 | } | ||
| 52 | sub assign_id { # assign unique ids without reassignments | ||
| 53 | my ($w) = @_; | ||
| 54 | my $id; | ||
| 55 | if (exists $OldW2ID{ 0+$w } and !exists $ID2W{ $OldW2ID{ 0+$w } }) { | ||
| 56 | $id = $OldW2ID{ 0+$w }; | ||
| 57 | $NextID = $id + 1 | ||
| 58 | if $NextID < $id + 1; | ||
| 59 | } else { | ||
| 60 | $id = $NextID++; | ||
| 61 | } | ||
| 62 | $W2ID{ 0+$w } = $id; | ||
| 63 | $ID2W{ $id } = $w; | ||
| 64 | $id; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub update { | ||
| 68 | my ($o, $c) = @_; | ||
| 69 | |||
| 70 | reset_idmap(); | ||
| 71 | |||
| 72 | my $uconf = $c->conf; | ||
| 73 | my $s = term->Tgoto('cm', 0, 0, $c->{io}->fd); | ||
| 74 | # my $s = term->Tputs('cl',1,$c->{io}->fd); | ||
| 75 | my $ln = $c->format_line; | ||
| 76 | my $name = $0; | ||
| 77 | $name =~ s,^.*/,,; | ||
| 78 | $s .= $ln->("$name PID=$$ \@ $Host"); | ||
| 79 | |||
| 80 | my ($sec,$min,$hr) = localtime(time); | ||
| 81 | my $tm = sprintf("| %02d:%02d:%02d [%4ds]", $hr,$min,$sec,$o->{seconds}); | ||
| 82 | $s .= term->Tgoto('cm', $uconf->{cols} - (1+length $tm), 0, $c->{io}->fd); | ||
| 83 | $s .= $tm."\n"; | ||
| 84 | |||
| 85 | my @load; | ||
| 86 | my @events = all_watchers(); | ||
| 87 | for my $sec (15,60,60*15) { | ||
| 88 | my $busy = 0; | ||
| 89 | for (@events) { $busy += ($_->stats($sec))[2] } | ||
| 90 | my $idle = (idle_time($sec))[2]; | ||
| 91 | my $tm = $idle + $busy; | ||
| 92 | push @load, $tm? $busy / $tm : 0; | ||
| 93 | } | ||
| 94 | |||
| 95 | my @all = map { | ||
| 96 | [{ obj => $_, | ||
| 97 | id => assign_id($_), | ||
| 98 | desc => $_->desc, | ||
| 99 | prio => $_->prio }, | ||
| 100 | $_->stats($o->{seconds})] } @events; | ||
| 101 | push @all, [{ id => 0, desc => 'idle', prio => QUEUES }, | ||
| 102 | idle_time($o->{seconds})]; | ||
| 103 | my $total = 0; | ||
| 104 | for (@all) { $total += $_->[3] } | ||
| 105 | my $other_tm = total_time($o->{seconds}) - $total; | ||
| 106 | $other_tm = 0 if $other_tm < 0; | ||
| 107 | push @all, [{ id => 0, desc => 'other processes', prio => -1 }, | ||
| 108 | 0, 0, $other_tm]; | ||
| 109 | |||
| 110 | # $lag should not be affected by other processes | ||
| 111 | my $lag = $total - $o->{seconds}; | ||
| 112 | $lag = 0 if $lag < 0; | ||
| 113 | |||
| 114 | $s .= $ln->("%d events; load averages: %.2f, %.2f, %.2f; lag %2d%%", | ||
| 115 | scalar @events, @load, $total? 100*$lag/$total : 0); | ||
| 116 | $s .= "\n"; | ||
| 117 | |||
| 118 | $total += $other_tm; # add in other processes for %time [XXX optional?] | ||
| 119 | |||
| 120 | my $filter = $o->{filter}; | ||
| 121 | @all = grep { $_->[0]{desc} =~ /$filter/ } @all | ||
| 122 | if length $filter; | ||
| 123 | |||
| 124 | $o->{page} = 1 if $o->{page} < 1; | ||
| 125 | my $rows_per_page = $uconf->{rows} - 8; | ||
| 126 | my $maxpage = int((@all + $rows_per_page - 1)/$rows_per_page); | ||
| 127 | $o->{page} = $maxpage if $o->{page} > $maxpage; | ||
| 128 | |||
| 129 | my $page = " P$o->{page}"; | ||
| 130 | $s .= $ln->(" EID PRI STATE RAN TIME CPU TYPE DESCRIPTION"); | ||
| 131 | my $start_row = 4; | ||
| 132 | $s .= term->Tgoto('cm', $uconf->{cols} - (1+length $page), $start_row-1, | ||
| 133 | $c->{io}->fd); | ||
| 134 | $s .= $page."\n"; | ||
| 135 | |||
| 136 | if ($o->{by} eq 't') { | ||
| 137 | @all = sort { $b->[3] <=> $a->[3] } @all; | ||
| 138 | } elsif ($o->{by} eq 'i') { | ||
| 139 | @all = sort { $a->[0]{id} <=> $b->[0]{id} } @all; | ||
| 140 | } elsif ($o->{by} eq 'r') { | ||
| 141 | @all = sort { $b->[1] <=> $a->[1] } @all; | ||
| 142 | } elsif ($o->{by} eq 'd') { | ||
| 143 | @all = sort { $a->[0]{desc} cmp $b->[0]{desc} } @all; | ||
| 144 | } elsif ($o->{by} eq 'p') { | ||
| 145 | @all = sort { $a->[0]{prio} cmp $b->[0]{prio} } @all; | ||
| 146 | } else { | ||
| 147 | warn "unknown sort by '$o->{by}'"; | ||
| 148 | } | ||
| 149 | splice @all, 0, $rows_per_page * ($o->{page} - 1) | ||
| 150 | if $o->{page} > 1; | ||
| 151 | |||
| 152 | for (my $r = 0; $r < $rows_per_page; $r++) { | ||
| 153 | my $st = shift @all; | ||
| 154 | if ($st) { | ||
| 155 | my $e = $st->[0]{obj}; | ||
| 156 | my ($type, $fstr); | ||
| 157 | if (!$e) { | ||
| 158 | $type = 'sys'; | ||
| 159 | $fstr = ''; | ||
| 160 | } else { | ||
| 161 | $type = ref $e; | ||
| 162 | $type =~ s/^Event:://; | ||
| 163 | $fstr = do { | ||
| 164 | # make look pretty! | ||
| 165 | if ($e->is_suspended) { | ||
| 166 | 'S'.(($e->is_active? 'W':''). | ||
| 167 | ($e->is_running?'R':'')) | ||
| 168 | } elsif ($e->is_running) { | ||
| 169 | 'cpu' | ||
| 170 | } elsif ($e->pending) { | ||
| 171 | 'queue' | ||
| 172 | } elsif ($e->is_active) { | ||
| 173 | $type eq 'idle'? 'wait' : 'sleep' | ||
| 174 | } else { | ||
| 175 | $type eq 'idle'? 'sleep' : 'zomb' | ||
| 176 | } | ||
| 177 | }; | ||
| 178 | } | ||
| 179 | my @prf = ($st->[0]{id}, | ||
| 180 | $st->[0]{prio}, | ||
| 181 | $fstr, | ||
| 182 | $st->[1], | ||
| 183 | int($st->[3]/60), $st->[3] % 60, | ||
| 184 | $total? 100 * $st->[3]/$total : 0, | ||
| 185 | substr($type,0,length($type)>4? 4:length($type)), | ||
| 186 | $st->[0]{desc}); | ||
| 187 | # warn join('x', @prf)."\n"; | ||
| 188 | my $line = sprintf("%5d %2d %-5s %5d %2d:%02d%5.1f%% %4s %s", @prf); | ||
| 189 | $s .= $ln->($line); | ||
| 190 | } else { | ||
| 191 | $s .= $ln->(); | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | $s .= "\n".$ln->($o->{error})."% "; | ||
| 196 | $s; | ||
| 197 | } | ||
| 198 | |||
| 199 | sub cmd { | ||
| 200 | my ($o, $c, $in) = @_; | ||
| 201 | |||
| 202 | if ($in eq '') { | ||
| 203 | $o->{error} = ''; | ||
| 204 | } elsif ($in eq 'h' or $in eq '?') { | ||
| 205 | $c->set_screen('NetServer::Portal::Top::Help'); | ||
| 206 | } elsif ($in =~ m/^d\s*(\d+)$/) { | ||
| 207 | $Event::DebugLevel = int $1; | ||
| 208 | } elsif ($in =~ m/^o\s*(\w+)$/) { | ||
| 209 | my $by = $1; | ||
| 210 | if ($by =~ m/^(t|i|r|d|p)$/) { | ||
| 211 | $o->{by} = $by; | ||
| 212 | } else { | ||
| 213 | $o->{error} = "Sort by '$by'? Type 'h' for help!"; | ||
| 214 | } | ||
| 215 | } elsif ($in =~ m/^p\s*(\d+)$/) { | ||
| 216 | $o->{page} = $1 || 1; | ||
| 217 | } elsif ($in =~ m/^(\,+)$/) { # compatible with Pi key bindings | ||
| 218 | $o->{page} -= length $1; | ||
| 219 | } elsif ($in =~ m/^(\.+)$/) { | ||
| 220 | $o->{page} += length $1; | ||
| 221 | } elsif ($in =~ m/^e\s*(\d+)$/) { | ||
| 222 | my $got = $ID2W{$1}; | ||
| 223 | if ($got) { | ||
| 224 | # XXX | ||
| 225 | $c->set_screen('NetServer::Portal::Top::Edit')->{edit} = $got; | ||
| 226 | } else { | ||
| 227 | $o->{error} = "Can't find event id '$1'"; | ||
| 228 | } | ||
| 229 | } elsif ($in =~ m{ ^/ (.*) $ }x) { | ||
| 230 | $o->{filter} = $1; | ||
| 231 | } elsif ($in =~ m/^t\s*(\d+)$/) { | ||
| 232 | my $s = $1; | ||
| 233 | my $max = &Event::Stats::MAXTIME; | ||
| 234 | if ($s < 0) { | ||
| 235 | $o->{error} = "Sorry, past performance is not an indication of future performance."; | ||
| 236 | } else { | ||
| 237 | $o->{seconds} = round_seconds($1); | ||
| 238 | } | ||
| 239 | } elsif ($in =~ m/^(s|r)\s*(\d+)$/) { | ||
| 240 | my $do = $1; | ||
| 241 | my $id = $2; | ||
| 242 | my $ev = $ID2W{ $2 }; | ||
| 243 | if (!$ev) { | ||
| 244 | $o->{error} = "Can't find event '$id'."; | ||
| 245 | } else { | ||
| 246 | $ev->suspend($do eq 's') | ||
| 247 | } | ||
| 248 | } else { | ||
| 249 | $o->{error} = "'$in'? Type 'h' for help!"; | ||
| 250 | } | ||
| 251 | } | ||
| 252 | |||
| 253 | package NetServer::Portal::Top::Edit; | ||
| 254 | use NetServer::Portal qw(term); | ||
| 255 | |||
| 256 | sub new { bless { error=>'' }, shift } | ||
| 257 | |||
| 258 | sub update { | ||
| 259 | my ($o, $c) = @_; | ||
| 260 | my $ln = $c->format_line; | ||
| 261 | my $s = term->Tputs('cl',1,$c->{io}->fd); | ||
| 262 | $s .= "Event Minieditor"."\n"x3; | ||
| 263 | my $e = $o->{edit}; | ||
| 264 | if (!$e) { | ||
| 265 | delete $o->{edit}; | ||
| 266 | $c->set_screen('NetServer::Portal::Top'); | ||
| 267 | return; | ||
| 268 | } | ||
| 269 | my $f = 'a'; | ||
| 270 | for my $k ($e->attributes) { | ||
| 271 | my $v = $e->$k(); | ||
| 272 | $v = defined $v? $v:'<undef>'; | ||
| 273 | if (length $v > 40) { | ||
| 274 | $v = substr($v,0,40) . ' ...'; | ||
| 275 | } | ||
| 276 | $v =~ s/\0/^0/g; | ||
| 277 | $v =~ s/\n/\\n/g; | ||
| 278 | $s .= $ln->("%s %-16s %-s", $f++, $k, $v); | ||
| 279 | } | ||
| 280 | $s .= $ln->(); | ||
| 281 | $s .= $ln->("(Use Zvalue to assign value to field 'Z'. 'x' when you are done.)"); | ||
| 282 | $s .= $ln->(); | ||
| 283 | $s .= $ln->($o->{error}); | ||
| 284 | $s .= "% "; | ||
| 285 | $s; | ||
| 286 | } | ||
| 287 | |||
| 288 | sub cmd { | ||
| 289 | my ($o, $c, $in) = @_; | ||
| 290 | if ($in eq '') { | ||
| 291 | # ignore | ||
| 292 | } elsif ($in eq 'x') { | ||
| 293 | delete $o->{edit}; | ||
| 294 | $c->set_screen('NetServer::Portal::Top'); | ||
| 295 | } elsif ($in =~ m/^(\w)\s*(.+)$/) { | ||
| 296 | my $f = ord(lc $1) - ord 'a'; | ||
| 297 | my $v = $2; | ||
| 298 | my $ev = $o->{edit}; | ||
| 299 | my @k = $ev->attributes; | ||
| 300 | if ($f < 0 || $f >= @k) { | ||
| 301 | $o->{error} = "Field '$f' is not available"; | ||
| 302 | } else { | ||
| 303 | eval { | ||
| 304 | my $m = $k[$f]; | ||
| 305 | $ev->$m($v) | ||
| 306 | }; #hope safe enough! | ||
| 307 | $o->{error} = $@ if $@; | ||
| 308 | } | ||
| 309 | } else { | ||
| 310 | $o->{error} = "'$in'?"; | ||
| 311 | } | ||
| 312 | } | ||
| 313 | |||
| 314 | package NetServer::Portal::Top::Help; | ||
| 315 | use NetServer::Portal qw(term); | ||
| 316 | |||
| 317 | sub new { bless { }, shift } | ||
| 318 | |||
| 319 | sub update { | ||
| 320 | my (undef, $c) = @_; | ||
| 321 | my $o = $c->conf('NetServer::Portal::Top'); | ||
| 322 | my $s = term->Tputs('cl',1,$c->{io}->fd); | ||
| 323 | $s .= "NetServer::Portal Help | ||
| 324 | |||
| 325 | |||
| 326 | CMD DESCRIPTION | ||
| 327 | -------- ----------------------------------------------------------- | ||
| 328 | d # set Event::DebugLevel [$Event::DebugLevel] | ||
| 329 | e #id edit event | ||
| 330 | h this screen | ||
| 331 | o #how order by t=time, i=id, r=ran, d=desc, p=prio [$o->{by}] | ||
| 332 | p #page switch to page #page [$o->{page}] | ||
| 333 | r #id resume event | ||
| 334 | s #id suspend event | ||
| 335 | t #sec show stats for the last #sec seconds [$o->{seconds}] | ||
| 336 | /regexp show events with matching descriptions [$o->{filter}] | ||
| 337 | |||
| 338 | |||
| 339 | |||
| 340 | |||
| 341 | |||
| 342 | |||
| 343 | (Press return to continue.)"; | ||
| 344 | $s; | ||
| 345 | } | ||
| 346 | |||
| 347 | sub cmd { | ||
| 348 | my ($o, $c, $in) = @_; | ||
| 349 | $c->set_screen('back'); | ||
| 350 | } | ||
| 351 | |||
| 352 | 1; | ||
| 353 | |||
| 354 | __END__ |