Whamcloud - gitweb
LU-8191 lnet: convert functions in utils to static
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <glob.h>
35 #include <limits.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/types.h>
40 #include <sys/time.h>
41 #include <unistd.h>
42
43 #include <libcfs/util/param.h>
44
45 static double timenow(void)
46 {
47    struct timeval tv;
48
49    gettimeofday (&tv, NULL);
50    return (tv.tv_sec + tv.tv_usec / 1000000.0);
51 }
52
53 typedef struct {
54         unsigned long        msgs_alloc;
55         unsigned long        msgs_max;
56         unsigned long        errors;
57         unsigned long        send_count;
58         unsigned long        recv_count;
59         unsigned long        route_count;
60         unsigned long        drop_count;
61         unsigned long long   send_length;
62         unsigned long long   recv_length;
63         unsigned long long   route_length;
64         unsigned long long   drop_length;
65 } counters_t;
66
67 static unsigned long long subull(unsigned long long a, unsigned long long b)
68 {
69         if (a < b)
70                 return -1ULL - b + a + 1;
71
72         return a - b;
73 }
74
75 static unsigned long long subul(unsigned long a, unsigned long b)
76 {
77         if (a < b)
78                 return -1UL - b + a + 1;
79
80         return a - b;
81 }
82
83 static double rul(unsigned long a, double secs)
84 {
85         return (double)a/secs;
86 }
87
88 static double rull(unsigned long long a, double secs)
89 {
90         return (double)a/secs;
91 }
92
93 static void do_stat(int fd)
94 {
95    static char  buffer[1024];
96    static double last = 0.0;
97    static counters_t old_counter;
98    double now;
99    double t;
100    counters_t new_counter;
101    counters_t counter;
102    int    n;
103
104    lseek (fd, 0, SEEK_SET);
105    now = timenow();
106    n = read(fd, buffer, sizeof(buffer) - 1);
107    if (n < 0)
108    {
109       fprintf (stderr, "Can't read statfile\n");
110       exit (1);
111    }
112    buffer[n] = 0;
113
114    n = sscanf(buffer, "%lu %lu %lu %lu %lu %lu %lu %llu %llu %llu %llu",
115                &new_counter.msgs_alloc, &new_counter.msgs_max,
116                &new_counter.errors, 
117                &new_counter.send_count, &new_counter.recv_count,
118                &new_counter.route_count, &new_counter.drop_count,
119                &new_counter.send_length, &new_counter.recv_length,
120                &new_counter.route_length, &new_counter.drop_length);
121    if (n < 11)
122    {
123       fprintf (stderr, "Can't parse statfile\n");
124       exit (1);
125    }
126
127    if (last == 0.0) {
128                 printf("M %lu(%lu) E %lu S %llu/%lu R %llu/%lu F %llu/%lu "
129                        "D %llu/%lu\n",
130                    new_counter.msgs_alloc, new_counter.msgs_max,
131                    new_counter.errors,
132                    new_counter.send_length, new_counter.send_count,
133                    new_counter.recv_length, new_counter.recv_count,
134                    new_counter.route_length, new_counter.route_count,
135                    new_counter.drop_length, new_counter.drop_count);
136    } else {
137            t = now - last;
138
139            counter.msgs_alloc = new_counter.msgs_alloc;
140            counter.msgs_max = new_counter.msgs_max;
141
142            counter.errors = subul(new_counter.errors, old_counter.errors);
143            counter.send_count = subul(new_counter.send_count, old_counter.send_count);
144            counter.recv_count = subul(new_counter.recv_count, old_counter.recv_count);
145            counter.route_count = subul(new_counter.route_count, old_counter.route_count);
146            counter.drop_count = subul(new_counter.drop_count, old_counter.drop_count);
147            counter.send_length = subull(new_counter.send_length, old_counter.send_length);
148            counter.recv_length = subull(new_counter.recv_length, old_counter.recv_length);
149            counter.route_length = subull(new_counter.route_length, old_counter.route_length);
150            counter.drop_length = subull(new_counter.drop_length, old_counter.drop_length);
151
152            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",
153                    counter.msgs_alloc, counter.msgs_max,
154                    rul(counter.errors,t),
155                    rull(counter.send_length,t*1024.0*1024.0), rul(counter.send_count, t),
156                    rull(counter.recv_length,t*1024.0*1024.0), rul(counter.recv_count, t),
157                    rull(counter.route_length,t*1024.0*1024.0), rul(counter.route_count, t),
158                    rull(counter.drop_length,t*1024.0*1024.0), rul(counter.drop_count, t));
159    }
160
161    old_counter = new_counter;
162    fflush (stdout);
163
164    lseek (fd, 0, SEEK_SET);
165    last = timenow();
166 }
167
168 int main(int argc, char **argv)
169 {
170         int interval = 0;
171         glob_t path;
172         int fd;
173
174         if (argc > 1)
175                 interval = atoi(argv[1]);
176
177         if (cfs_get_param_paths(&path, "stats") != 0) {
178                 fprintf(stderr, "LNet stats not available\n");
179                 return 1;
180         }
181
182         fd = open(path.gl_pathv[0], O_RDONLY);
183         if (fd < 0) {
184                 fprintf(stderr, "failed to open '%s': %s\n", path.gl_pathv[0],
185                         strerror(errno));
186                 cfs_free_param_data(&path);
187                 return 1;
188         }
189         cfs_free_param_data(&path);
190
191         do_stat(fd);
192         if (interval == 0)
193                 return 0;
194
195         while (1) {
196                 sleep(interval);
197                 do_stat(fd);
198         }
199         /* Never reached */
200 }