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