Whamcloud - gitweb
- merge 0.7rc1 from b_devel to HEAD (20030612 merge point)
[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    double now;
25    double t;
26    long long bytes;
27    long      packets;
28    long      errors;
29    long      depth;
30    int    n;
31    
32    lseek (fd, 0, SEEK_SET);
33    now = timenow();
34    n = read (fd, buffer, sizeof (buffer));
35    if (n < 0)
36    {
37       fprintf (stderr, "Can't read statfile\n");
38       exit (1);
39    }     
40    buffer[n] = 0;
41    
42    n = sscanf (buffer, "%Ld %ld %ld %ld", &bytes, &packets, &errors, &depth);
43    
44    if (n < 3)
45    {
46       fprintf (stderr, "Can't parse statfile\n");
47       exit (1);
48    }
49    
50    if (last == 0.0)
51       printf ("%Ld bytes, %ld packets (sz %Ld) %ld errors", 
52               bytes, packets, (long long)((packets == 0) ? 0LL : bytes/packets), errors);
53    else
54    {
55       t = now - last;
56
57       printf ("%9Ld (%7.2fMb/s), %7ld packets (sz %5Ld, %5ld/s) %ld errors (%ld/s)", 
58               bytes, ((double)bytes)/((1<<20) * t),
59               packets, (long long)((packets == 0) ? 0LL : bytes/packets), (long)(packets/t),
60               errors, (long)(errors/t));
61    }
62
63    if (n == 4)
64       printf (" (%ld)\n", depth);
65    else
66       printf ("\n");
67
68    fflush (stdout);
69    
70    lseek (fd, 0, SEEK_SET);
71    write (fd, "\n", 1);
72    last = timenow();
73 }
74
75 int main (int argc, char **argv)
76 {
77    int  interval = 0;
78    int  fd;
79    
80    if (argc > 1)
81       interval = atoi (argv[1]);
82
83    fd = open ("/proc/sys/portals/router", O_RDWR);
84    if (fd < 0)
85    {
86       fprintf (stderr, "Can't open stat: %s\n", strerror (errno));
87       return (1);
88    }
89    
90    do_stat (fd);
91    if (interval == 0)
92       return (0);
93    
94    for (;;)
95    {
96       sleep (interval);
97       do_stat (fd);
98    }
99 }