Whamcloud - gitweb
Mass conversion of all copyright messages to Oracle.
[fs/lustre-release.git] / lustre / tests / mpi / lp_utils.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) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/tests/lp_utils.c
37  *
38  * Author: You Feng <youfeng@clusterfs.com>
39  */
40
41 #include <mpi.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <time.h>
45 #include <sys/time.h>
46 #include <sys/types.h>
47 #include <asm/types.h>
48 #include <sys/stat.h>
49 #include <unistd.h>
50 #include <sys/ioctl.h>
51 #include <fcntl.h>
52 #include <errno.h>
53 #include <libcfs/libcfs.h>
54 #include "lustre/lustre_user.h"
55 #include "lp_utils.h"
56
57 #define MAX_PROCESSES 8
58
59 int verbose = 0;
60 int debug = 0;
61
62 char hostname[1024];
63
64 struct timeval t1, t2;
65
66 char *timestamp() {
67         static char datestring[80];
68         time_t timestamp;
69
70         fflush(stdout);
71         timestamp = time(NULL);
72         strftime(datestring, 80, "%T", localtime(&timestamp));
73
74         return datestring;
75 }
76
77 inline void begin(char *str) {
78         if (verbose > 0 && rank == 0) {
79                 gettimeofday(&t1, NULL);
80                 printf("%s:\tBeginning %s\n", timestamp(), str);
81                 fflush(stdout);
82         }
83 }
84
85 inline void end(char *str) {
86         float elapsed;
87
88         MPI_Barrier(MPI_COMM_WORLD);
89         if (verbose > 0 && rank == 0) {
90                 gettimeofday(&t2, NULL);
91
92                 elapsed = t2.tv_sec - t1.tv_sec +
93                           (float)(t2.tv_usec-t1.tv_usec)/1000000;
94                 if (elapsed >= 60) {
95                         printf("%s:\tFinished %-15s(%.2f min)\n",
96                                timestamp(), str, elapsed / 60);
97                 } else {
98                         printf("%s:\tFinished %-15s(%.3f sec)\n",
99                                timestamp(), str, elapsed);
100
101                 }
102                 fflush(stdout);
103         }
104 }
105
106 void dump_diff(char *orig_buf, char *buf, int size, long _off)
107 {
108         int i, diff, off;
109         char *p, *end;
110
111         printf("commpared buf size %d, at offset %lu\n\n", size, _off);
112
113         if (orig_buf) {
114                 printf("original buf:\n");
115                 p = orig_buf;
116                 end = orig_buf + size;
117                 i = 1;
118                 while (p < end) {
119                         printf(" %8lx", *(long *)p);
120                         p += sizeof(long);
121                         if (i++%8 == 0)
122                                 printf("\n");
123                 }
124                 if (i%8) printf("\n\n");
125                 else printf("\n");
126         }
127
128         if (buf) {
129                 printf("different data: diff_data(orig_data)\n");
130                 diff = 0;
131                 off = 0;
132                 i = 1;
133                 p = buf;
134                 end = buf + size;
135                 while (p < end) {
136                         if (memcmp(p, orig_buf + off, sizeof(long)) != 0) {
137                                 printf("\toff: %5d,\tdata: %8lx (%8lx)\n", off,
138                                        *(unsigned long *)p,
139                                        *(unsigned long *)(orig_buf + off));
140                                 diff++;
141                         }
142                         off += sizeof(long);
143                         p += sizeof(long);
144                 }
145                 printf("\n %d total differents found\n\n", diff);
146         }
147 }
148
149 void lp_gethostname(void)
150 {
151         if (gethostname(hostname, 1024) == -1) {
152                 fprintf(stderr, "gethostname: (%d)%s", errno, strerror(errno));
153                 MPI_Abort(MPI_COMM_WORLD, 2);
154         }
155 }
156
157 /* This function does not FAIL if the requested "name" does not exit.
158  * This is just to clean up any files or directories left over from
159  * previous runs
160  */
161 void remove_file_or_dir(char *name)
162 {
163         struct stat statbuf;
164         char errmsg[MAX_FILENAME_LEN + 20];
165
166         if (stat(name, &statbuf) != -1) {
167                 if (S_ISREG(statbuf.st_mode)) {
168                         printf("stale file found\n");
169                         if (unlink(name) == -1) {
170                                 sprintf(errmsg, "unlink of %s", name);
171                                 FAIL(errmsg);
172                         }
173                 }
174                 if (S_ISDIR(statbuf.st_mode)) {
175                         printf("stale directory found\n");
176                         if (rmdir(name) == -1) {
177                                 sprintf(errmsg, "rmdir of %s", name);
178                                 FAIL(errmsg);
179                         }
180                 }
181         }
182 }
183
184 void create_file(char *name, long filesize, int fill)
185 {
186         static char filename[MAX_FILENAME_LEN];
187         char errmsg[MAX_FILENAME_LEN + 20];
188         char buf[1024 * 8];
189         char c = 'A' + size;
190         int fd, rc;
191         short zero = 0;
192         long left = filesize;
193
194         /* Process 0 creates the test file(s) */
195         if (rank == 0) {
196                 sprintf(filename, "%s/%s", testdir, name);
197                 remove_file_or_dir(filename);
198                 if ((fd = creat(filename, FILEMODE)) == -1) {
199                         sprintf(errmsg, "create of file %s", filename);
200                         FAIL(errmsg);
201                 }
202                 if (filesize > 0) {
203                         if (lseek(fd, filesize - 1, SEEK_SET) == -1) {
204                                 close(fd);
205                                 sprintf(errmsg, "lseek of file %s", filename);
206                                 FAIL(errmsg);
207                         }
208                         if (write(fd, &zero, 1) == -1) {
209                                 close(fd);
210                                 sprintf(errmsg, "write of file %s", filename);
211                                 FAIL(errmsg);
212                         }
213                 }
214                 if (filesize > 0 && fill) {
215                         if (lseek(fd, 0, SEEK_SET) == -1) {
216                                 close(fd);
217                                 sprintf(errmsg, "lseek of file %s", filename);
218                                 FAIL(errmsg);
219                         }
220                         memset(buf, c, 1024);
221                         while (left > 0) {
222                                 if ((rc = write(fd, buf,
223                                                 left > (1024 * 8) ? (1024 * 8) : left))
224                                     == -1) {
225                                         close(fd);
226                                         sprintf(errmsg, "write of file %s", filename);
227                                         FAIL(errmsg);
228                                 }
229                                 left -= rc;
230                         }
231                 }
232                 if (close(fd) == -1) {
233                         sprintf(errmsg, "close of file %s", filename);
234                         FAIL(errmsg);
235                 }
236         }
237 }
238
239 void check_stat(char *filename, struct stat *state, struct stat *old_state)
240 {
241         char errmsg[MAX_FILENAME_LEN+20];
242
243         if (stat(filename, state) == -1) {
244                 sprintf(errmsg, "stat of file %s", filename);
245                 FAIL(errmsg);
246         }
247
248         if (memcmp(state, old_state, sizeof(struct stat)) != 0) {
249                 errno = 0;
250                 sprintf(errmsg, LP_STAT_FMT, LP_STAT_ARGS);
251                 FAIL(errmsg);
252         }
253 }
254
255 void remove_file(char *name)
256 {
257         char filename[MAX_FILENAME_LEN];
258         char errmsg[MAX_FILENAME_LEN + 20];
259
260         /* Process 0 remove the file(s) */
261         if (rank == 0) {
262                 sprintf(filename, "%s/%s", testdir, name);
263                 if (unlink(filename) == -1) {
264                         sprintf(errmsg, "unlink of file %s", filename);
265                         FAIL(errmsg);
266                 }
267         }
268 }
269
270 void fill_stride(char *buf, int buf_size, long long rank, long long _off)
271 {
272         char *p = buf;
273         long long off, data[2];
274         int cp, left = buf_size;
275
276         data[0] = rank;
277         off = _off;
278         while (left > 0) {
279                 data[1] = off;
280                 cp = left > sizeof(data) ? sizeof(data) : left;
281                 memcpy(p, data, cp);
282                 off += cp;
283                 p += cp;
284                 left -= cp;
285         }
286 }