1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/tests/lp_utils.c
38 * Author: You Feng <youfeng@clusterfs.com>
46 #include <sys/types.h>
47 #include <asm/types.h>
50 #include <sys/ioctl.h>
53 #include <libcfs/libcfs.h>
54 #include "lustre/lustre_user.h"
57 #define MAX_PROCESSES 8
64 struct timeval t1, t2;
67 static char datestring[80];
71 timestamp = time(NULL);
72 strftime(datestring, 80, "%T", localtime(×tamp));
77 inline void begin(char *str) {
78 if (verbose > 0 && rank == 0) {
79 gettimeofday(&t1, NULL);
80 printf("%s:\tBeginning %s\n", timestamp(), str);
85 inline void end(char *str) {
88 MPI_Barrier(MPI_COMM_WORLD);
89 if (verbose > 0 && rank == 0) {
90 gettimeofday(&t2, NULL);
92 elapsed = t2.tv_sec - t1.tv_sec +
93 (float)(t2.tv_usec-t1.tv_usec)/1000000;
95 printf("%s:\tFinished %-15s(%.2f min)\n",
96 timestamp(), str, elapsed / 60);
98 printf("%s:\tFinished %-15s(%.3f sec)\n",
99 timestamp(), str, elapsed);
106 void dump_diff(char *orig_buf, char *buf, int size, long _off)
111 printf("commpared buf size %d, at offset %lu\n\n", size, _off);
114 printf("original buf:\n");
116 end = orig_buf + size;
119 printf(" %8lx", *(long *)p);
124 if (i%8) printf("\n\n");
129 printf("different data: diff_data(orig_data)\n");
136 if (memcmp(p, orig_buf + off, sizeof(long)) != 0) {
137 printf("\toff: %5d,\tdata: %8lx (%8lx)\n", off,
139 *(unsigned long *)(orig_buf + off));
145 printf("\n %d total differents found\n\n", diff);
149 void lp_gethostname(void)
151 if (gethostname(hostname, 1024) == -1) {
152 fprintf(stderr, "gethostname: (%d)%s", errno, strerror(errno));
153 MPI_Abort(MPI_COMM_WORLD, 2);
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
161 void remove_file_or_dir(char *name)
164 char errmsg[MAX_FILENAME_LEN + 20];
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);
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);
184 void create_file(char *name, long filesize, int fill)
186 static char filename[MAX_FILENAME_LEN];
187 char errmsg[MAX_FILENAME_LEN + 20];
192 long left = filesize;
194 /* Process 0 creates the test file(s) */
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);
203 if (lseek(fd, filesize - 1, SEEK_SET) == -1) {
205 sprintf(errmsg, "lseek of file %s", filename);
208 if (write(fd, &zero, 1) == -1) {
210 sprintf(errmsg, "write of file %s", filename);
214 if (filesize > 0 && fill) {
215 if (lseek(fd, 0, SEEK_SET) == -1) {
217 sprintf(errmsg, "lseek of file %s", filename);
220 memset(buf, c, 1024);
222 if ((rc = write(fd, buf,
223 left > (1024 * 8) ? (1024 * 8) : left))
226 sprintf(errmsg, "write of file %s", filename);
232 if (close(fd) == -1) {
233 sprintf(errmsg, "close of file %s", filename);
239 void check_stat(char *filename, struct stat *state, struct stat *old_state)
241 char errmsg[MAX_FILENAME_LEN+20];
243 if (stat(filename, state) == -1) {
244 sprintf(errmsg, "stat of file %s", filename);
248 if (memcmp(state, old_state, sizeof(struct stat)) != 0) {
250 sprintf(errmsg, LP_STAT_FMT, LP_STAT_ARGS);
255 void remove_file(char *name)
257 char filename[MAX_FILENAME_LEN];
258 char errmsg[MAX_FILENAME_LEN + 20];
260 /* Process 0 remove the file(s) */
262 sprintf(filename, "%s/%s", testdir, name);
263 if (unlink(filename) == -1) {
264 sprintf(errmsg, "unlink of file %s", filename);
270 void fill_stride(char *buf, int buf_size, long long rank, long long _off)
273 long long off, data[2];
274 int cp, left = buf_size;
280 cp = left > sizeof(data) ? sizeof(data) : left;