Whamcloud - gitweb
Stop routerstat from resetting the router's statistics after every read, which
[fs/lustre-release.git] / lnet / utils / routerstat.c
1 #include <stdio.h>
2 #include <errno.h>
3 #include <string.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <sys/types.h>
8 #include <sys/time.h>
9
10 double
11 timenow ()
12 {
13    struct timeval tv;
14    
15    gettimeofday (&tv, NULL);
16    return (tv.tv_sec + tv.tv_usec / 1000000.0);
17 }
18
19 void
20 do_stat (int fd)
21 {
22    static char  buffer[1024];
23    static double last = 0.0;
24    static unsigned long long old_bytes;
25    static unsigned long      old_packets;
26    static unsigned long      old_errors;
27    double now;
28    double t;
29    unsigned long long new_bytes, bytes;
30    unsigned long      new_packets, packets;
31    unsigned long      new_errors, errors;
32    unsigned long      depth;
33    int    n;
34    
35    lseek (fd, 0, SEEK_SET);
36    now = timenow();
37    n = read (fd, buffer, sizeof (buffer));
38    if (n < 0)
39    {
40       fprintf (stderr, "Can't read statfile\n");
41       exit (1);
42    }     
43    buffer[n] = 0;
44    
45    n = sscanf (buffer, "%Lu %lu %lu %lu",
46                &new_bytes, &new_packets, &new_errors, &depth);
47    
48    if (n < 3)
49    {
50       fprintf (stderr, "Can't parse statfile\n");
51       exit (1);
52    }
53    
54    if (last == 0.0)
55       printf ("%llu bytes, %lu packets (sz %lld), %lu errors", 
56               new_bytes, new_packets,
57               (long long)((new_packets == 0) ? 0LL : new_bytes/new_packets),
58               new_errors);
59    else
60    {
61       t = now - last;
62
63       if (new_bytes < old_bytes)
64           bytes = -1ULL - old_bytes + new_bytes + 1;
65       else
66           bytes = new_bytes - old_bytes;
67       if (new_packets < old_packets)
68           packets = -1UL - old_packets + new_packets + 1;
69       else
70           packets = new_packets - old_packets;
71       if (new_errors < old_errors)
72           errors = -1UL - old_errors + new_errors + 1;
73       else
74           errors = new_errors - old_errors;
75       
76       printf ("%9llu bytes (%7.2fMb/s), %7lu packets (sz %5lld, %5ld/s), %lu errors (%ld/s)", 
77               bytes, ((double)bytes)/((1<<20) * t),
78               packets, (long long)((packets == 0) ? 0LL : bytes/packets), (long)(packets/t),
79               errors, (long)(errors/t));
80    }
81    old_bytes = new_bytes;
82    old_packets = new_packets;
83    old_errors = new_errors;
84
85    if (n == 4)
86       printf (", depth (%ld)\n", depth);
87    else
88       printf ("\n");
89
90    fflush (stdout);
91    
92    lseek (fd, 0, SEEK_SET);
93    last = timenow();
94 }
95
96 int main (int argc, char **argv)
97 {
98    int  interval = 0;
99    int  fd;
100    
101    if (argc > 1)
102       interval = atoi (argv[1]);
103
104    fd = open ("/proc/sys/portals/router", O_RDONLY);
105    if (fd < 0)
106    {
107       fprintf (stderr, "Can't open stat: %s\n", strerror (errno));
108       return (1);
109    }
110    
111    do_stat (fd);
112    if (interval == 0)
113       return (0);
114    
115    for (;;)
116    {
117       sleep (interval);
118       do_stat (fd);
119    }
120 }