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