rx: Constify rx_opaque_populate
[openafs.git] / src / rx / rx_trace.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 #include <roken.h>
14
15 #ifndef RXDEBUG
16 char rxi_tracename[80] = "\0Tracing not compiled in";
17 #ifdef DUMPTRACE
18 int
19 main(int argc, char **argv)
20 {
21     return 0;
22 }
23 #endif
24 #else
25
26 #include "rx.h"
27 #include "rx_atomic.h"
28 #include "rx_globals.h"
29 #include "rx_internal.h"
30 #include "rx_trace.h"
31
32 #include "rx_conn.h"
33 #include "rx_call.h"
34
35 #ifdef RXTRACEON
36 char rxi_tracename[80] = "/tmp/rxcalltrace";
37 #else
38 char rxi_tracename[80] =
39     "\0Change This pathname (and preceding NUL) to initiate tracing";
40 #endif
41 int rxi_logfd = -1;
42 char rxi_tracebuf[4096];
43 afs_uint32 rxi_tracepos = 0;
44
45 struct rx_trace {
46     afs_uint32 cid;
47     unsigned short call;
48     unsigned short qlen;
49     afs_uint32 now;
50     afs_uint32 waittime;
51     afs_uint32 servicetime;
52     afs_uint32 event;
53 };
54
55 void
56 rxi_flushtrace(void)
57 {
58     if (rxi_logfd >= 0)
59         write(rxi_logfd, rxi_tracebuf, rxi_tracepos);
60     rxi_tracepos = 0;
61 }
62
63 void
64 rxi_calltrace(unsigned int event, struct rx_call *call)
65 {
66     struct clock now;
67     struct rx_trace rxtinfo;
68
69     if (!rxi_tracename[0])
70         return;
71
72     if (rxi_logfd < 0) {
73         rxi_logfd = open(rxi_tracename, O_WRONLY | O_CREAT | O_TRUNC, 0777);
74         if (rxi_logfd < 0)
75             rxi_tracename[0] = '\0';
76     }
77     clock_GetTime(&now);
78
79     rxtinfo.event = event;
80     rxtinfo.now = now.sec * 1000 + now.usec / 1000;
81     rxtinfo.cid = call->conn->cid;
82     rxtinfo.call = *(call->callNumber);
83     rxtinfo.qlen = rx_atomic_read(&rx_nWaiting);
84     rxtinfo.servicetime = 0;
85     rxtinfo.waittime = 0;
86
87     switch (event) {
88     case RX_CALL_END:
89         clock_Sub(&now, &(call->traceStart));
90         rxtinfo.servicetime = now.sec * 10000 + now.usec / 100;
91         if (call->traceWait.sec) {
92             now = call->traceStart;
93             clock_Sub(&now, &(call->traceWait));
94             rxtinfo.waittime = now.sec * 10000 + now.usec / 100;
95         } else
96             rxtinfo.waittime = 0;
97         call->traceWait.sec = call->traceWait.usec = call->traceStart.sec =
98             call->traceStart.usec = 0;
99         break;
100
101     case RX_CALL_START:
102         call->traceStart = now;
103         if (call->traceWait.sec) {
104             clock_Sub(&now, &(call->traceWait));
105             rxtinfo.waittime = now.sec * 10000 + now.usec / 100;
106         } else
107             rxtinfo.waittime = 0;
108         break;
109
110     case RX_TRACE_DROP:
111         if (call->traceWait.sec) {
112             clock_Sub(&now, &(call->traceWait));
113             rxtinfo.waittime = now.sec * 10000 + now.usec / 100;
114         } else
115             rxtinfo.waittime = 0;
116         break;
117
118     case RX_CALL_ARRIVAL:
119         call->traceWait = now;
120     default:
121         break;
122     }
123
124     memcpy(rxi_tracebuf + rxi_tracepos, &rxtinfo, sizeof(struct rx_trace));
125     rxi_tracepos += sizeof(struct rx_trace);
126     if (rxi_tracepos >= (4096 - sizeof(struct rx_trace)))
127         rxi_flushtrace();
128 }
129
130 #ifdef DUMPTRACE
131 #ifdef AFS_NT40_ENV
132 #include <afs/afsutil.h>
133 #endif
134
135 int
136 main(int argc, char **argv)
137 {
138     struct rx_trace ip;
139     int err = 0;
140
141     setlinebuf(stdout);
142     argv++;
143     argc--;
144     while (argc && **argv == '-') {
145         if (strcmp(*argv, "-trace") == 0) {
146             strcpy(rxi_tracename, *(++argv));
147             argc--;
148         } else {
149             err++;
150             break;
151         }
152         argv++, argc--;
153     }
154     if (err || argc != 0) {
155         printf("usage: dumptrace [-trace pathname]");
156         exit(1);
157     }
158
159     rxi_logfd = open(rxi_tracename, O_RDONLY);
160     if (rxi_logfd < 0) {
161         perror("");
162         exit(errno);
163     }
164
165     while (read(rxi_logfd, &ip, sizeof(struct rx_trace))) {
166         printf("%9u ", ip.now);
167         switch (ip.event) {
168         case RX_CALL_END:
169             putchar('E');
170             break;
171         case RX_CALL_START:
172             putchar('S');
173             break;
174         case RX_CALL_ARRIVAL:
175             putchar('A');
176             break;
177         case RX_TRACE_DROP:
178             putchar('D');
179             break;
180         default:
181             putchar('U');
182             break;
183         }
184         printf(" %3u %7u %7u      %x.%x\n", ip.qlen, ip.servicetime,
185                ip.waittime, ip.cid, ip.call);
186     }
187     return 0;
188 }
189
190 #endif /* DUMPTRACE */
191 #endif /* RXDEBUG */