Whamcloud - gitweb
Revert "b=21951 2.6.32-fc13 patchless client support for HEAD"
[fs/lustre-release.git] / lnet / utils / routerstat.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include <stdio.h>
38 #include <errno.h>
39 #include <string.h>
40 #include <fcntl.h>
41 #include <unistd.h>
42 #include <stdlib.h>
43 #include <sys/types.h>
44 #include <sys/time.h>
45
46 double
47 timenow ()
48 {
49    struct timeval tv;
50    
51    gettimeofday (&tv, NULL);
52    return (tv.tv_sec + tv.tv_usec / 1000000.0);
53 }
54
55 typedef struct {
56         unsigned long        msgs_alloc;
57         unsigned long        msgs_max;
58         unsigned long        errors;
59         unsigned long        send_count;
60         unsigned long        recv_count;
61         unsigned long        route_count;
62         unsigned long        drop_count;
63         unsigned long long   send_length;
64         unsigned long long   recv_length;
65         unsigned long long   route_length;
66         unsigned long long   drop_length;
67 } counters_t;
68
69 unsigned long long subull(unsigned long long a, unsigned long long b)
70 {
71         if (a < b)
72                 return -1ULL - b + a + 1;
73         
74         return a - b;
75 }
76
77 unsigned long long subul(unsigned long a, unsigned long b)
78 {
79         if (a < b)
80                 return -1UL - b + a + 1;
81         
82         return a - b;
83 }
84
85 double rul(unsigned long a, double secs)
86 {
87         return (double)a/secs;
88 }
89
90 double rull(unsigned long long a, double secs)
91 {
92         return (double)a/secs;
93 }
94
95 void
96 do_stat (int fd)
97 {
98    static char  buffer[1024];
99    static double last = 0.0;
100    static counters_t old_counter;
101    double now;
102    double t;
103    counters_t new_counter;
104    counters_t counter;
105    int    n;
106    
107    lseek (fd, 0, SEEK_SET);
108    now = timenow();
109    n = read (fd, buffer, sizeof (buffer));
110    if (n < 0)
111    {
112       fprintf (stderr, "Can't read statfile\n");
113       exit (1);
114    }     
115    buffer[n] = 0;
116    
117    n = sscanf (buffer, "%lu %lu %lu %lu %lu %lu %lu %Lu %Lu %Lu %Lu",
118                &new_counter.msgs_alloc, &new_counter.msgs_max,
119                &new_counter.errors, 
120                &new_counter.send_count, &new_counter.recv_count,
121                &new_counter.route_count, &new_counter.drop_count,
122                &new_counter.send_length, &new_counter.recv_length,
123                &new_counter.route_length, &new_counter.drop_length);
124    if (n < 11)
125    {
126       fprintf (stderr, "Can't parse statfile\n");
127       exit (1);
128    }
129    
130    if (last == 0.0) {
131            printf ("M %lu(%lu) E %lu S %llu/%lu R %llu/%lu F %llu/%lu D %llu/%lu\n",
132                    new_counter.msgs_alloc, new_counter.msgs_max,
133                    new_counter.errors,
134                    new_counter.send_length, new_counter.send_count,
135                    new_counter.recv_length, new_counter.recv_count,
136                    new_counter.route_length, new_counter.route_count,
137                    new_counter.drop_length, new_counter.drop_count);
138    } else {
139            t = now - last;
140
141            counter.msgs_alloc = new_counter.msgs_alloc;
142            counter.msgs_max = new_counter.msgs_max;
143            
144            counter.errors = subul(new_counter.errors, old_counter.errors);
145            counter.send_count = subul(new_counter.send_count, old_counter.send_count);
146            counter.recv_count = subul(new_counter.recv_count, old_counter.recv_count);
147            counter.route_count = subul(new_counter.route_count, old_counter.route_count);
148            counter.drop_count = subul(new_counter.drop_count, old_counter.drop_count);
149            counter.send_length = subull(new_counter.send_length, old_counter.send_length);
150            counter.recv_length = subull(new_counter.recv_length, old_counter.recv_length);
151            counter.route_length = subull(new_counter.route_length, old_counter.route_length);
152            counter.drop_length = subull(new_counter.drop_length, old_counter.drop_length);
153
154            printf ("M %3lu(%3lu) E %0.0f S %7.2f/%6.0f R %7.2f/%6.0f F %7.2f/%6.0f D %4.2f/%0.0f\n",
155                    counter.msgs_alloc, counter.msgs_max,
156                    rul(counter.errors,t),
157                    rull(counter.send_length,t*1024.0*1024.0), rul(counter.send_count, t),
158                    rull(counter.recv_length,t*1024.0*1024.0), rul(counter.recv_count, t),
159                    rull(counter.route_length,t*1024.0*1024.0), rul(counter.route_count, t),
160                    rull(counter.drop_length,t*1024.0*1024.0), rul(counter.drop_count, t));
161    }
162
163    old_counter = new_counter;
164    fflush (stdout);
165    
166    lseek (fd, 0, SEEK_SET);
167    last = timenow();
168 }
169
170 int main (int argc, char **argv)
171 {
172    int  interval = 0;
173    int  fd;
174    
175    if (argc > 1)
176       interval = atoi (argv[1]);
177
178    fd = open ("/proc/sys/lnet/stats", O_RDONLY);
179    if (fd < 0)
180    {
181       fprintf (stderr, "Can't open stat: %s\n", strerror (errno));
182       return (1);
183    }
184    
185    do_stat (fd);
186    if (interval == 0)
187       return (0);
188    
189    for (;;)
190    {
191       sleep (interval);
192       do_stat (fd);
193    }
194 }