rx: Make the rx_call structure private
[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 #include <errno.h>
132 #ifdef AFS_NT40_ENV
133 #include <afs/afsutil.h>
134 #endif
135
136 int
137 main(int argc, char **argv)
138 {
139     struct rx_trace ip;
140     int err = 0;
141
142     setlinebuf(stdout);
143     argv++;
144     argc--;
145     while (argc && **argv == '-') {
146         if (strcmp(*argv, "-trace") == 0) {
147             strcpy(rxi_tracename, *(++argv));
148             argc--;
149         } else {
150             err++;
151             break;
152         }
153         argv++, argc--;
154     }
155     if (err || argc != 0) {
156         printf("usage: dumptrace [-trace pathname]");
157         exit(1);
158     }
159
160     rxi_logfd = open(rxi_tracename, O_RDONLY);
161     if (rxi_logfd < 0) {
162         perror("");
163         exit(errno);
164     }
165
166     while (read(rxi_logfd, &ip, sizeof(struct rx_trace))) {
167         printf("%9u ", ip.now);
168         switch (ip.event) {
169         case RX_CALL_END:
170             putchar('E');
171             break;
172         case RX_CALL_START:
173             putchar('S');
174             break;
175         case RX_CALL_ARRIVAL:
176             putchar('A');
177             break;
178         case RX_TRACE_DROP:
179             putchar('D');
180             break;
181         default:
182             putchar('U');
183             break;
184         }
185         printf(" %3u %7u %7u      %x.%x\n", ip.qlen, ip.servicetime,
186                ip.waittime, ip.cid, ip.call);
187     }
188     return 0;
189 }
190
191 #endif /* DUMPTRACE */
192 #endif /* RXDEBUG */