Whamcloud - gitweb
LU-1146 build: batch update copyright messages
[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  * Copyright (c) 2011, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  */
38
39 #include <stdio.h>
40 #include <errno.h>
41 #include <string.h>
42 #include <fcntl.h>
43 #include <unistd.h>
44 #include <stdlib.h>
45 #include <sys/types.h>
46 #include <sys/time.h>
47
48 double
49 timenow ()
50 {
51    struct timeval tv;
52
53    gettimeofday (&tv, NULL);
54    return (tv.tv_sec + tv.tv_usec / 1000000.0);
55 }
56
57 typedef struct {
58         unsigned long        msgs_alloc;
59         unsigned long        msgs_max;
60         unsigned long        errors;
61         unsigned long        send_count;
62         unsigned long        recv_count;
63         unsigned long        route_count;
64         unsigned long        drop_count;
65         unsigned long long   send_length;
66         unsigned long long   recv_length;
67         unsigned long long   route_length;
68         unsigned long long   drop_length;
69 } counters_t;
70
71 unsigned long long subull(unsigned long long a, unsigned long long b)
72 {
73         if (a < b)
74                 return -1ULL - b + a + 1;
75
76         return a - b;
77 }
78
79 unsigned long long subul(unsigned long a, unsigned long b)
80 {
81         if (a < b)
82                 return -1UL - b + a + 1;
83
84         return a - b;
85 }
86
87 double rul(unsigned long a, double secs)
88 {
89         return (double)a/secs;
90 }
91
92 double rull(unsigned long long a, double secs)
93 {
94         return (double)a/secs;
95 }
96
97 void
98 do_stat (int fd)
99 {
100    static char  buffer[1024];
101    static double last = 0.0;
102    static counters_t old_counter;
103    double now;
104    double t;
105    counters_t new_counter;
106    counters_t counter;
107    int    n;
108
109    lseek (fd, 0, SEEK_SET);
110    now = timenow();
111    n = read (fd, buffer, sizeof (buffer));
112    if (n < 0)
113    {
114       fprintf (stderr, "Can't read statfile\n");
115       exit (1);
116    }
117    buffer[n] = 0;
118
119    n = sscanf(buffer, "%lu %lu %lu %lu %lu %lu %lu %llu %llu %llu %llu",
120                &new_counter.msgs_alloc, &new_counter.msgs_max,
121                &new_counter.errors, 
122                &new_counter.send_count, &new_counter.recv_count,
123                &new_counter.route_count, &new_counter.drop_count,
124                &new_counter.send_length, &new_counter.recv_length,
125                &new_counter.route_length, &new_counter.drop_length);
126    if (n < 11)
127    {
128       fprintf (stderr, "Can't parse statfile\n");
129       exit (1);
130    }
131
132    if (last == 0.0) {
133                 printf("M %lu(%lu) E %lu S %llu/%lu R %llu/%lu F %llu/%lu "
134                        "D %llu/%lu\n",
135                    new_counter.msgs_alloc, new_counter.msgs_max,
136                    new_counter.errors,
137                    new_counter.send_length, new_counter.send_count,
138                    new_counter.recv_length, new_counter.recv_count,
139                    new_counter.route_length, new_counter.route_count,
140                    new_counter.drop_length, new_counter.drop_count);
141    } else {
142            t = now - last;
143
144            counter.msgs_alloc = new_counter.msgs_alloc;
145            counter.msgs_max = new_counter.msgs_max;
146
147            counter.errors = subul(new_counter.errors, old_counter.errors);
148            counter.send_count = subul(new_counter.send_count, old_counter.send_count);
149            counter.recv_count = subul(new_counter.recv_count, old_counter.recv_count);
150            counter.route_count = subul(new_counter.route_count, old_counter.route_count);
151            counter.drop_count = subul(new_counter.drop_count, old_counter.drop_count);
152            counter.send_length = subull(new_counter.send_length, old_counter.send_length);
153            counter.recv_length = subull(new_counter.recv_length, old_counter.recv_length);
154            counter.route_length = subull(new_counter.route_length, old_counter.route_length);
155            counter.drop_length = subull(new_counter.drop_length, old_counter.drop_length);
156
157            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",
158                    counter.msgs_alloc, counter.msgs_max,
159                    rul(counter.errors,t),
160                    rull(counter.send_length,t*1024.0*1024.0), rul(counter.send_count, t),
161                    rull(counter.recv_length,t*1024.0*1024.0), rul(counter.recv_count, t),
162                    rull(counter.route_length,t*1024.0*1024.0), rul(counter.route_count, t),
163                    rull(counter.drop_length,t*1024.0*1024.0), rul(counter.drop_count, t));
164    }
165
166    old_counter = new_counter;
167    fflush (stdout);
168
169    lseek (fd, 0, SEEK_SET);
170    last = timenow();
171 }
172
173 int main (int argc, char **argv)
174 {
175    int  interval = 0;
176    int  fd;
177
178    if (argc > 1)
179       interval = atoi (argv[1]);
180
181    fd = open ("/proc/sys/lnet/stats", O_RDONLY);
182    if (fd < 0)
183    {
184       fprintf (stderr, "Can't open stat: %s\n", strerror (errno));
185       return (1);
186    }
187
188    do_stat (fd);
189    if (interval == 0)
190       return (0);
191
192    for (;;)
193    {
194       sleep (interval);
195       do_stat (fd);
196    }
197 }