Witness: enum witness_notifyResponse_type
[wireshark-wip.git] / ui / cli / tap-scsistat.c
blob4d8e8033a26f60f2c2936c70658818763d17e313
1 /* tap-scsistat.c 2010 Chris Costa and Cal Turney
3 * $Id$
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "config.h"
26 #include <stdio.h>
28 #include <string.h>
29 #include <epan/packet_info.h>
30 #include <epan/epan.h>
31 #include <epan/stat_cmd_args.h>
32 #include <epan/tap.h>
33 #include <epan/conversation.h>
34 #include <epan/dissectors/packet-scsi.h>
35 #include <epan/dissectors/packet-fc.h>
36 #include <epan/dissectors/packet-scsi-sbc.h>
37 #include <epan/dissectors/packet-scsi-ssc.h>
38 #include <epan/dissectors/packet-scsi-smc.h>
39 #include <epan/dissectors/packet-scsi-osd.h>
41 void register_tap_listener_scsistat(void);
43 static guint8 scsi_program=0;
45 /* used to keep track of statistics for a specific procedure */
46 typedef struct _scsi_procedure_t {
47 const char *proc;
48 int num;
49 nstime_t min;
50 nstime_t max;
51 nstime_t tot;
52 } scsi_procedure_t;
54 /* used to keep track of the statistics for an entire program interface */
55 typedef struct _scsistat_t {
56 guint8 cmdset;
57 char *filter;
58 const value_string *cdbnames;
59 const char *prog;
60 #define MAX_PROCEDURES 256
61 scsi_procedure_t *procedures;
62 } scsistat_t;
64 #define NANOSECS_PER_SEC 1000000000
66 static void
67 scsistat_reset(void *prs)
69 scsistat_t *rs=(scsistat_t *)prs;
70 guint32 i;
72 for(i=0; i < MAX_PROCEDURES; i++) {
73 rs->procedures[i].num=0;
74 rs->procedures[i].min.secs=0;
75 rs->procedures[i].min.nsecs=0;
76 rs->procedures[i].max.secs=0;
77 rs->procedures[i].max.nsecs=0;
78 rs->procedures[i].tot.secs=0;
79 rs->procedures[i].tot.nsecs=0;
83 static int
84 scsistat_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pri)
86 scsistat_t *rs = (scsistat_t *)prs;
87 const scsi_task_data_t *ri = (const scsi_task_data_t *)pri;
88 nstime_t delta;
89 scsi_procedure_t *rp;
91 /* we are only interested in response packets */
92 if(ri->type!=SCSI_PDU_TYPE_RSP) {
93 return 0;
95 /* we are only interested in a specific commandset */
96 if( (!ri->itl) || ((ri->itl->cmdset&SCSI_CMDSET_MASK)!=rs->cmdset) ) {
97 return 0;
99 /* check that the opcode looks sane */
100 if( (!ri->itlq) || (ri->itlq->scsi_opcode > 255) ) {
101 return 0;
104 rp=&(rs->procedures[ri->itlq->scsi_opcode]);
106 /* calculate time delta between request and reply */
107 nstime_delta(&delta, &pinfo->fd->abs_ts, &ri->itlq->fc_time);
109 if(rp->num==0) {
110 rp->max.secs=delta.secs;
111 rp->max.nsecs=delta.nsecs;
113 if(rp->num==0) {
114 rp->min.secs= delta.secs;
115 rp->min.nsecs=delta.nsecs;
117 if( (delta.secs < rp->min.secs)
118 ||( (delta.secs == rp->min.secs)
119 &&(delta.nsecs < rp->min.nsecs) ) ) {
120 rp->min.secs = delta.secs;
121 rp->min.nsecs= delta.nsecs;
123 if( (delta.secs > rp->max.secs)
124 ||( (delta.secs == rp->max.secs)
125 &&(delta.nsecs > rp->max.nsecs) ) ) {
126 rp->max.secs = delta.secs;
127 rp->max.nsecs= delta.nsecs;
129 rp->tot.secs += delta.secs;
130 rp->tot.nsecs += delta.nsecs;
131 if(rp->tot.nsecs > NANOSECS_PER_SEC) {
132 rp->tot.nsecs -= NANOSECS_PER_SEC;
133 rp->tot.secs++;
135 rp->num++;
136 return 1;
139 static void
140 scsistat_draw(void *prs)
142 scsistat_t *rs=(scsistat_t *)prs;
143 guint32 i;
144 guint64 td;
146 printf("\n");
147 printf("===========================================================\n");
148 printf("SCSI %s SRT Statistics:\n", rs->prog);
149 printf("Filter: %s\n", rs->filter?rs->filter:"");
150 printf("Procedure Calls Min SRT Max SRT Avg SRT\n");
151 for(i=0; i < MAX_PROCEDURES; i++) {
152 if(rs->procedures[i].num==0) {
153 continue;
155 /* scale it to units of 1us.*/
156 td = ((guint64)(rs->procedures[i].tot.secs)) * NANOSECS_PER_SEC + rs->procedures[i].tot.nsecs;
157 td = ((td / rs->procedures[i].num) + 500) / 1000;
159 printf("%-19s %6d %3d.%06u %3d.%06u %3d.%06u \n",
160 rs->procedures[i].proc,
161 rs->procedures[i].num,
162 (int)(rs->procedures[i].min.secs),
163 (rs->procedures[i].min.nsecs+500)/1000,
164 (int)(rs->procedures[i].max.secs),
165 (rs->procedures[i].max.nsecs+500)/1000,
166 (int)(td/1000000), (int)(td%1000000)
169 printf("===========================================================\n");
172 static void
173 scsistat_init(const char *opt_arg, void* userdata _U_)
175 scsistat_t *rs;
176 guint32 i;
177 int program, pos;
178 const char *filter=NULL;
179 GString *error_string;
181 pos=0;
182 if(sscanf(opt_arg, "scsi,srt,%d,%n", &program, &pos)==1) {
183 if(pos) {
184 filter=opt_arg+pos;
185 } else {
186 filter=NULL;
188 } else {
189 fprintf(stderr, "tshark: invalid \"-z scsi,srt,<cmdset>[,<filter>]\" argument\n");
190 exit(1);
193 scsi_program=program;
194 rs=g_new(scsistat_t,1);
195 if(filter) {
196 rs->filter=g_strdup(filter);
197 } else {
198 rs->filter=NULL;
200 rs->cmdset=program;
202 switch(program) {
203 case SCSI_DEV_SBC:
204 rs->prog="SBC (disk)";
205 rs->cdbnames=scsi_sbc_vals;
206 break;
207 case SCSI_DEV_SSC:
208 rs->prog="SSC (tape)";
209 rs->cdbnames=scsi_ssc_vals;
210 break;
211 case SCSI_DEV_CDROM:
212 rs->prog="MMC (cd/dvd)";
213 rs->cdbnames=scsi_mmc_vals;
214 break;
215 case SCSI_DEV_SMC:
216 rs->prog="SMC (tape robot)";
217 rs->cdbnames=scsi_smc_vals;
218 break;
219 case SCSI_DEV_OSD:
220 rs->prog="OSD (object based)";
221 rs->cdbnames=scsi_osd_vals;
222 break;
223 default:
224 /* Default to the SBC (disk), since this is what EMC SCSI seem to always be */
225 rs->cmdset=0;
226 rs->prog="SBC (disk)";
227 rs->cdbnames=scsi_sbc_vals;
228 break;
230 rs->procedures=g_new(scsi_procedure_t,MAX_PROCEDURES);
231 for(i=0; i < MAX_PROCEDURES; i++) {
232 rs->procedures[i].proc=val_to_str(i, rs->cdbnames, "Unknown-0x%02x");
233 rs->procedures[i].num=0;
234 rs->procedures[i].min.secs=0;
235 rs->procedures[i].min.nsecs=0;
236 rs->procedures[i].max.secs=0;
237 rs->procedures[i].max.nsecs=0;
238 rs->procedures[i].tot.secs=0;
239 rs->procedures[i].tot.nsecs=0;
241 error_string=register_tap_listener("scsi", rs, filter, 0, scsistat_reset, scsistat_packet, scsistat_draw);
242 if(error_string) {
243 /* error, we failed to attach to the tap. clean up */
244 g_free(rs->procedures);
245 g_free(rs->filter);
246 g_free(rs);
248 fprintf(stderr, "tshark: Couldn't register scsi,srt tap: %s\n",
249 error_string->str);
250 g_string_free(error_string, TRUE);
251 exit(1);
255 void
256 register_tap_listener_scsistat(void)
258 register_stat_cmd_arg("scsi,srt,", scsistat_init, NULL);