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