Whamcloud - gitweb
911ab5febd174e3e8b0239695e56719c250de220
[fs/lustre-release.git] / lustre / utils / obdbarrier.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *   Author: Eric Barton <eeb@clusterfs.com>
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <string.h>
28
29 #include "obdiolib.h"
30
31 int
32 parse_kmg (uint64_t *valp, char *str)
33 {
34         uint64_t        val;
35         char            mod[32];
36
37         switch (sscanf (str, LPU64"%1[gGmMkK]", &val, mod))
38         {
39         default:
40                 return (-1);
41
42         case 1:
43                 *valp = val;
44                 return (0);
45
46         case 2:
47                 switch (*mod)
48                 {
49                 case 'g':
50                 case 'G':
51                         *valp = val << 30;
52                         return (0);
53
54                 case 'm':
55                 case 'M':
56                         *valp = val << 20;
57                         return (0);
58
59                 case 'k':
60                 case 'K':
61                         *valp = val << 10;
62                         return (0);
63
64                 default:
65                         *valp = val;
66                         return (0);
67                 }
68         }
69 }
70
71 void
72 usage (char *cmdname, int help) 
73 {
74         char *name = strrchr (cmdname, '/');
75         
76         if (name == NULL)
77                 name = cmdname;
78         
79         fprintf (help ? stdout : stderr,
80                  "usage: %s -d device -s size -o offset [-i id][-n reps][-l] oid\n",
81                  name);
82 }
83
84 int
85 exponential_modulus (int i, int base)
86 {
87         int   top = base;
88         int   mod = 1;
89         
90         for (;;) {
91                 if (i < top)
92                         return (i%mod == 0);
93                 
94                 mod = top;
95                 top *= base;
96         }
97 }
98
99 int
100 main (int argc, char **argv) 
101 {
102         uint64_t              bid = (((uint64_t)gethostid()) << 32) | getpid ();
103         int                   set_bid = 0;
104         uint64_t              oid;
105         int                   setup = 0;
106         int                   device = -1;
107         int                   npeers = 0;
108         int                   reps = 1;
109         char                  hostname[128];
110         struct obdio_conn    *conn;
111         struct obdio_barrier *b;
112         char                 *end;
113         uint64_t              val;
114         int                   rc;
115         int                   c;
116
117         setvbuf (stdout, NULL, _IOLBF, 0);
118         memset (hostname, 0, sizeof (hostname));
119         gethostname (hostname, sizeof (hostname));
120         hostname[sizeof(hostname) - 1] = 0;
121         
122         while ((c = getopt (argc, argv, "hsi:d:n:p:")) != -1)
123                 switch (c) {
124                 case 'h':
125                         usage (argv[0], 1);
126                         return (0);
127                         
128                 case 'i':
129                         bid = strtoll (optarg, &end, 0);
130                         if (end == optarg || *end != 0) {
131                                 fprintf (stderr, "Can't parse id %s\n",
132                                          optarg);
133                                 return (1);
134                         }
135                         set_bid = 1;
136                         break;
137                         
138                 case 's':
139                         setup = 1;
140                         break;
141                         
142                 case 'd':
143                         device = strtol (optarg, &end, 0);
144                         if (end == optarg || *end != 0 || device < 0) {
145                                 fprintf (stderr, "Can't parse device %s\n",
146                                          optarg);
147                                 return (1);
148                         }
149                         break;
150
151                 case 'n':
152                         if (parse_kmg (&val, optarg) != 0) {
153                                 fprintf (stderr, "Can't parse reps %s\n",
154                                          optarg);
155                                 return (1);
156                         }
157                         reps = (int)val;
158                         break;
159
160                 case 'p':
161                         npeers = strtol (optarg, &end, 0);
162                         if (end == optarg || *end != 0 || npeers <= 0) {
163                                 fprintf (stderr, "Can't parse npeers %s\n",
164                                          optarg);
165                                 return (1);
166                         }
167                         break;
168
169                 default:
170                         usage (argv[0], 0);
171                         return (1);
172         }
173
174         if ((!setup && !set_bid) ||
175             npeers <= 0 ||
176             device < 0 ||
177             optind == argc) {
178                 fprintf (stderr, "%s not specified\n",
179                          (!setup && !set_bid) ? "id" :
180                          npeers <= 0 ? "npeers" :
181                          device < 0 ? "device" : "object id");
182                 return (1);
183         }
184         
185         oid = strtoull (argv[optind], &end, 0);
186         if (end == argv[optind] || *end != 0) {
187                 fprintf (stderr, "Can't parse object id %s\n",
188                          argv[optind]);
189                 return (1);
190         }
191         
192         conn = obdio_connect (device);
193         if (conn == NULL)
194                 return (1);
195
196         b = obdio_new_barrier (oid, bid, npeers);
197         if (b == NULL)
198                 return (1);
199
200         rc = 0;
201         if (setup) {
202                 rc = obdio_setup_barrier (conn, b);
203                 if (rc == 0)
204                         printf ("Setup barrier: -d %d -i "LPX64" -p %d -n1 "LPX64"\n",
205                                 device, bid, npeers, oid);
206         } else {
207                 for (c = 0; c < reps; c++) {
208                         rc = obdio_barrier (conn, b);
209                         if (rc != 0)
210                                 break;
211                         if (exponential_modulus (c, 10))
212                                 printf ("%s: Barrier %d\n", hostname, c);
213                 }
214         }
215
216         free (b);
217         
218         obdio_disconnect (conn);
219
220         return (rc == 0 ? 0 : 1);
221 }
222
223