Add proper per-file copyright notices/licenses and top-level license.
[bluesky.git] / TBBT / trace_init / latency.pl
1 #\r
2 # Copyright (c) 2002-2003\r
3 #      The President and Fellows of Harvard College.\r
4 #\r
5 # Redistribution and use in source and binary forms, with or without\r
6 # modification, are permitted provided that the following conditions\r
7 # are met:\r
8 # 1. Redistributions of source code must retain the above copyright\r
9 #    notice, this list of conditions and the following disclaimer.\r
10 # 2. Redistributions in binary form must reproduce the above copyright\r
11 #    notice, this list of conditions and the following disclaimer in the\r
12 #    documentation and/or other materials provided with the distribution.\r
13 # 3. Neither the name of the University nor the names of its contributors\r
14 #    may be used to endorse or promote products derived from this software\r
15 #    without specific prior written permission.\r
16 #\r
17 # THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND\r
18 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
20 # ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE\r
21 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
22 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
23 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
24 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
26 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
27 # SUCH DAMAGE.\r
28 #\r
29 # $Id: latency.pl,v 1.8 2003/07/28 14:27:16 ellard Exp $\r
30 #\r
31 # latency.pl -\r
32 \r
33 package latency;\r
34 \r
35 %PendingOps     = ();\r
36 %OpCount        = ();\r
37 %OpTime         = ();\r
38 %KeysSeen       = ();\r
39 \r
40 @OpList         = ();\r
41 \r
42 sub init {\r
43         my (@oplist) = @_;\r
44 \r
45         @OpList = @oplist;\r
46 }\r
47 \r
48 # Bugs:  might not recognize the actual response packets.  It's an\r
49 # approximation.\r
50 \r
51 sub update {\r
52         my ($key, $proto, $op, $xid, $client, $now) = @_;\r
53 \r
54         my $uxid = "$client-$xid";\r
55 \r
56         if ($proto eq 'C3' || $proto eq 'C2') {\r
57                 $PendingOps{$uxid} = $now;\r
58         }\r
59         elsif (exists $PendingOps{$uxid}) {\r
60                 my $elapsed = $now - $PendingOps{$uxid};\r
61 \r
62                 $KeysSeen{$key} = 1;\r
63 \r
64                 $OpTime{"$key,$op"} += $elapsed;\r
65                 $OpCount{"$key,$op"}++;\r
66 \r
67                 $OpTime{"$key,TOTAL"} += $elapsed;\r
68                 $OpCount{"$key,TOTAL"}++;\r
69 \r
70                 $OpTime{"$key,INTERESTING"} += $elapsed;\r
71                 $OpCount{"$key,INTERESTING"}++;\r
72 \r
73                 delete $PendingOps{$uxid};\r
74         }\r
75 }\r
76 \r
77 sub resetOpCounts {\r
78 \r
79         my $k;\r
80 \r
81         foreach $k ( keys %OpTime ) {\r
82                 $OpTime{$k} = 0.0;\r
83         }\r
84         foreach $k ( keys %OpCount ) {\r
85                 $OpCount{$k} = 0;\r
86         }\r
87 \r
88         return ;\r
89 }\r
90 \r
91 sub printTitle {\r
92         my $str = "#L time client euid egid fh";\r
93 \r
94         foreach my $op ( @OpList ) {\r
95                 $str .= " $op-cnt $op-lat";\r
96         }\r
97         $str .= "\n";\r
98 \r
99         print $str;\r
100\r
101 \r
102 sub printOps {\r
103         my ($start_time, $out) = @_;\r
104         my ($k, $str, $op, $nk, $latms, $cnt);\r
105 \r
106         my @allkeys = sort keys %KeysSeen;\r
107 \r
108         foreach $k ( @allkeys ) {\r
109                 my $tot = "$k,TOTAL";\r
110 \r
111                 if ($main::OMIT_ZEROS &&\r
112                         (! exists $OpCounts{$tot} || $OpCounts{$tot} == 0)) {\r
113                         next;\r
114                 }\r
115 \r
116                 $str = sprintf ("L %s %s", $start_time, &key::key2str ($k));\r
117 \r
118                 foreach $op ( @OpList ) {\r
119                         $nk = "$k,$op";\r
120 \r
121                         if (exists $OpCount{$nk}) {\r
122                                 $cnt = $OpCount{"$k,$op"};\r
123                         }\r
124                         else {\r
125                                 $cnt = 0;\r
126                         }\r
127 \r
128                         if ($cnt > 0) {\r
129                                 $latms = 1000 * $OpTime{$nk} / $cnt;\r
130                         }\r
131                         else {\r
132                                 $latms = -1;\r
133                         }\r
134 \r
135                         $str .= sprintf (" %d %.4f", $cnt, $latms);\r
136                 }\r
137 \r
138                 print $out "$str\n";\r
139         }\r
140 }\r
141 \r
142 # Purge all the pending XID records dated earlier than $when (which is\r
143 # typically at least $PRUNE_INTERVAL seconds ago).  This is important\r
144 # because otherwise missing XID records can pile up, eating a lot of\r
145 # memory. \r
146   \r
147 sub prunePending {\r
148         my ($when) = @_;\r
149 \r
150         foreach my $uxid ( keys %PendingOps ) {\r
151                 if ($PendingOps{$uxid} < $when) {\r
152                         delete $PendingOps{$uxid};\r
153                 }\r
154         }\r
155 \r
156         return ;\r
157 }\r
158 \r
159 1;\r