From 8c8f46566c41c2cfd2e3950c0ecb42157df8984a Mon Sep 17 00:00:00 2001 From: Shaun Tancheff Date: Sun, 22 Sep 2019 03:28:41 -0500 Subject: [PATCH] LU-12795 tests: Introduce FAILF and start using it Unify the sprintf(msg, ...); FAIL(msg); pattern into a single FAILF() macro and avoid the needless use of a temporary buffer. Remove the duplicate lp_utils.c and lp_utils.h files. Signed-off-by: Shaun Tancheff Change-Id: Ia0ed463e305ec671909a049d847eb8518291f72f Reviewed-on: https://review.whamcloud.com/36255 Tested-by: jenkins Tested-by: Maloo Reviewed-by: Arshad Hussain Reviewed-by: Petros Koutoupis Reviewed-by: Oleg Drokin --- lustre/tests/lp_utils.c | 279 -------------------------------------------- lustre/tests/lp_utils.h | 115 ------------------ lustre/tests/mpi/lp_utils.c | 82 +++++-------- lustre/tests/mpi/lp_utils.h | 30 +++-- 4 files changed, 49 insertions(+), 457 deletions(-) delete mode 100644 lustre/tests/lp_utils.c delete mode 100644 lustre/tests/lp_utils.h diff --git a/lustre/tests/lp_utils.c b/lustre/tests/lp_utils.c deleted file mode 100644 index ea0170b..0000000 --- a/lustre/tests/lp_utils.c +++ /dev/null @@ -1,279 +0,0 @@ -/* - * GPL HEADER START - * - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 only, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License version 2 for more details (a copy is included - * in the LICENSE file that accompanied this code). - * - * You should have received a copy of the GNU General Public License - * version 2 along with this program; If not, see - * http://www.gnu.org/licenses/gpl-2.0.html - * - * GPL HEADER END - */ -/* - * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved. - * Use is subject to license terms. - * - * Copyright (c) 2014, Intel Corporation. - */ -/* - * This file is part of Lustre, http://www.lustre.org/ - * Lustre is a trademark of Sun Microsystems, Inc. - * - * lustre/tests/lp_utils.c - * - * Author: You Feng - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "lustre/lustre_user.h" -#include "lustre/tests/lp_utils.h" - -#define MAX_PROCESSES 8 - -int verbose = 0; -int debug = 0; - -char hostname[1024]; - -struct timeval t1, t2; - -char *timestamp() { - static char datestring[80]; - time_t timestamp; - - fflush(stdout); - timestamp = time(NULL); - strftime(datestring, 80, "%T", localtime(×tamp)); - - return datestring; -} - -void begin(char *str) { - if (verbose > 0 && rank == 0) { - gettimeofday(&t1, NULL); - printf("%s:\tBeginning %s\n", timestamp(), str); - fflush(stdout); - } -} - -void end(char *str) { - float elapsed; - - MPI_Barrier(MPI_COMM_WORLD); - if (verbose > 0 && rank == 0) { - gettimeofday(&t2, NULL); - elapsed = (t2.tv_sec + ((float)t2.tv_usec/1000000)) - - (t1.tv_sec + ((float)t1.tv_usec/1000000)); - if (elapsed >= 60) { - printf("%s:\tFinished %-15s(%.2f min)\n", - timestamp(), str, elapsed / 60); - } else { - printf("%s:\tFinished %-15s(%.3f sec)\n", - timestamp(), str, elapsed); - - } - fflush(stdout); - } -} - -void dump_diff(char *orig_buf, char *buf, int size, long _off) -{ - int i, diff, off; - char *p, *end; - - printf("commpared buf size %d, at offset %lu\n\n", size, _off); - - if (orig_buf) { - printf("original buf:\n"); - p = orig_buf; - end = orig_buf + size; - i = 1; - while (p < end) { - printf(" %8lx", *(long *)p); - p += sizeof(long); - if (i++%8 == 0) - printf("\n"); - } - if (i%8) printf("\n\n"); - else printf("\n"); - } - - if (buf) { - printf("different data: diff_data(orig_data)\n"); - diff = 0; - off = 0; - i = 1; - p = buf; - end = buf + size; - while (p < end) { - if (memcmp(p, orig_buf + off, sizeof(long)) != 0) { - printf("\toff: %5d,\tdata: %8lx (%8lx)\n", off, - *(unsigned long *)p, - *(unsigned long *)(orig_buf + off)); - diff++; - } - off += sizeof(long); - p += sizeof(long); - } - printf("\n %d total differents found\n\n", diff); - } -} - -void lp_gethostname(void) -{ - if (gethostname(hostname, 1024) == -1) { - fprintf(stderr, "gethostname: (%d)%s", errno, strerror(errno)); - MPI_Abort(MPI_COMM_WORLD, 2); - } -} - -/* This function does not FAIL if the requested "name" does not exit. - * This is just to clean up any files or directories left over from - * previous runs - */ -void remove_file_or_dir(char *name) -{ - struct stat statbuf; - char errmsg[MAX_FILENAME_LEN + 20]; - - if (stat(name, &statbuf) != -1) { - if (S_ISREG(statbuf.st_mode)) { - printf("stale file found\n"); - if (unlink(name) == -1) { - sprintf(errmsg, "unlink of %s", name); - FAIL(errmsg); - } - } - if (S_ISDIR(statbuf.st_mode)) { - printf("stale directory found\n"); - if (rmdir(name) == -1) { - sprintf(errmsg, "rmdir of %s", name); - FAIL(errmsg); - } - } - } -} - -void create_file(char *name, long filesize, int fill) -{ - static char filename[MAX_FILENAME_LEN]; - char errmsg[MAX_FILENAME_LEN + 20]; - char buf[1024 * 8]; - char c = 'A' + size; - int fd, rc; - short zero = 0; - long left = filesize; - - /* Process 0 creates the test file(s) */ - if (rank == 0) { - sprintf(filename, "%s/%s", testdir, name); - remove_file_or_dir(filename); - if ((fd = creat(filename, FILEMODE)) == -1) { - sprintf(errmsg, "create of file %s", filename); - FAIL(errmsg); - } - if (filesize > 0) { - if (lseek(fd, filesize - 1, SEEK_SET) == -1) { - close(fd); - sprintf(errmsg, "lseek of file %s", filename); - FAIL(errmsg); - } - if (write(fd, &zero, 1) == -1) { - close(fd); - sprintf(errmsg, "write of file %s", filename); - FAIL(errmsg); - } - } - if (filesize > 0 && fill) { - if (lseek(fd, 0, SEEK_SET) == -1) { - close(fd); - sprintf(errmsg, "lseek of file %s", filename); - FAIL(errmsg); - } - memset(buf, c, 1024); - while (left > 0) { - if ((rc = write(fd, buf, - left > (1024 * 8) ? (1024 * 8) : left)) - == -1) { - close(fd); - sprintf(errmsg, "write of file %s", filename); - FAIL(errmsg); - } - left -= rc; - } - } - if (close(fd) == -1) { - sprintf(errmsg, "close of file %s", filename); - FAIL(errmsg); - } - } -} - -void check_stat(char *filename, struct stat *state, struct stat *old_state) -{ - char errmsg[MAX_FILENAME_LEN+20]; - - if (stat(filename, state) == -1) { - sprintf(errmsg, "stat of file %s", filename); - FAIL(errmsg); - } - - if (memcmp(state, old_state, sizeof(struct stat)) != 0) { - errno = 0; - sprintf(errmsg, LP_STAT_FMT, LP_STAT_ARGS); - FAIL(errmsg); - } -} - -void remove_file(char *name) -{ - char filename[MAX_FILENAME_LEN]; - char errmsg[MAX_FILENAME_LEN + 20]; - - /* Process 0 remove the file(s) */ - if (rank == 0) { - sprintf(filename, "%s/%s", testdir, name); - if (unlink(filename) == -1) { - sprintf(errmsg, "unlink of file %s", filename); - FAIL(errmsg); - } - } -} - -void fill_stride(char *buf, int buf_size, long long rank, long long _off) -{ - char *p = buf; - long long off, data[2]; - int cp, left = buf_size; - - data[0] = rank; - off = _off; - while (left > 0) { - data[1] = off; - cp = left > sizeof(data) ? sizeof(data) : left; - memcpy(p, data, cp); - off += cp; - p += cp; - left -= cp; - } -} diff --git a/lustre/tests/lp_utils.h b/lustre/tests/lp_utils.h deleted file mode 100644 index b312534..0000000 --- a/lustre/tests/lp_utils.h +++ /dev/null @@ -1,115 +0,0 @@ -/* - * GPL HEADER START - * - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 only, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License version 2 for more details (a copy is included - * in the LICENSE file that accompanied this code). - * - * You should have received a copy of the GNU General Public License - * version 2 along with this program; If not, see - * http://www.gnu.org/licenses/gpl-2.0.html - * - * GPL HEADER END - */ -/* - * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved. - * Use is subject to license terms. - */ -/* - * This file is part of Lustre, http://www.lustre.org/ - * Lustre is a trademark of Sun Microsystems, Inc. - * - * lustre/tests/lp_utils.h - * - * Author: You Feng - */ - -#ifndef __LP_UTILS_H__ -#define __LP_UTILS_H__ - -#include - -#define FAIL(msg) \ - \ -do { \ - printf("%s: Process %d (%s)\n", timestamp(), rank, hostname); \ - if (debug) \ - printf("\tFAILED in %s:%d:%s()\n", __FILE__, __LINE__, __func__); \ - else \ - printf("\tFAILED in %s()\n", __func__); \ - printf("%s", msg); \ - fflush(stdout); \ - MPI_Abort(MPI_COMM_WORLD, 1); \ -} while(0) - -#define FILEMODE S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH -#define MAX_FILENAME_LEN 512 - -extern int verbose; -extern int debug; - -extern int rank; -extern int size; - -extern char hostname[]; -extern char *timestamp(); -extern char *testdir; - -extern void begin(char *str); -extern void end(char *str); - -extern void dump_diff(char *orig_buf, char *buf, int len, long off); -extern void lp_gethostname(void); - -extern void create_file(char *name, long filesize, int fill); -extern void fill_file(char *name, long filesize); - -#define LP_STAT_FMT \ - \ -"Stat error:\n \ -\tfields\t\tvalue\told value\n \ -\tst_dev\t\t%d\t%d\n \ -\tst_ino\t\t%d\t%d\n \ -\tst_mode\t\t%o\t%o\n \ -\tst_nlink\t%d\t%d\n \ -\tst_uid\t\t%d\t%d\n \ -\tst_gid\t\t%d\t%d\n \ -\tst_rdev\t\t%x.%x\t%x.%x\n \ -\tst_size\t\t%lu\t%lu\n \ -\tst_blksize\t%d\t%d\n \ -\tst_blocks\t%u\t%u\n \ -\tst_atime\t%d\t%d\n \ -\tst_mtime\t%d\t%d\n \ -\tst_ctime\t%d\t%d\n" - -#define LP_STAT_ARGS \ - \ -(int)state->st_dev, (int)old_state->st_dev, \ -(int)state->st_ino, (int)old_state->st_ino, \ -state->st_mode & 07777, old_state->st_mode & 07777, \ -(int)state->st_nlink, (int)old_state->st_nlink, \ -state->st_uid, old_state->st_uid, \ -state->st_gid, old_state->st_gid, \ -(int)((state->st_rdev >> 8) & 0xff), (int)(state->st_rdev & 0xff), \ -(int)((old_state->st_rdev >> 8) & 0xff), (int)(old_state->st_rdev & 0xff), \ -(unsigned long)state->st_size, (unsigned long)old_state->st_size, \ -(int)state->st_blksize, (int)old_state->st_blksize, \ -(unsigned int)state->st_blocks, (unsigned int)old_state->st_blocks, \ -(int)state->st_atime, (int)old_state->st_atime, \ -(int)state->st_mtime, (int)old_state->st_mtime, \ -(int)state->st_ctime, (int)old_state->st_ctime - -extern void check_stat(char *filename, struct stat *state, struct stat *old_state); -extern void remove_file(char *name); -extern void remove_file_or_dir(char *name); -extern void fill_stride(char *buf, int buf_size, long long rank, long long _off); - -#endif /* __LP_UTILS_H__ */ diff --git a/lustre/tests/mpi/lp_utils.c b/lustre/tests/mpi/lp_utils.c index b64d207..8bcb555 100644 --- a/lustre/tests/mpi/lp_utils.c +++ b/lustre/tests/mpi/lp_utils.c @@ -20,8 +20,10 @@ * GPL HEADER END */ /* - * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved. * Use is subject to license terms. + * + * Copyright (c) 2014, Intel Corporation. */ /* * This file is part of Lustre, http://www.lustre.org/ @@ -31,14 +33,13 @@ * * Author: You Feng */ -#include + #include #include #include #include #include #include -#include #include #include #include @@ -83,12 +84,11 @@ void end(char *str) MPI_Barrier(MPI_COMM_WORLD); if (verbose > 0 && rank == 0) { gettimeofday(&t2, NULL); - - elapsed = t2.tv_sec - t1.tv_sec + - (float)(t2.tv_usec - t1.tv_usec) / 1000000; + elapsed = (t2.tv_sec + ((float)t2.tv_usec / 1000000)) - + (t1.tv_sec + ((float)t1.tv_usec / 1000000)); if (elapsed >= 60) { - printf("%s:\tFinished %-15s(%.2f min)\n", - timestamp(), str, elapsed / 60); + printf("%s:\tFinished %-15s(%.2f min)\n", timestamp(), + str, elapsed / 60); } else { printf("%s:\tFinished %-15s(%.3f sec)\n", timestamp(), str, elapsed); @@ -150,29 +150,25 @@ void lp_gethostname(void) } } -/* This function does not FAIL if the requested "name" does not exit. +/* + * This function does not FAIL if the requested "name" does not exit. * This is just to clean up any files or directories left over from * previous runs */ void remove_file_or_dir(char *name) { struct stat statbuf; - char errmsg[MAX_FILENAME_LEN + 20]; if (stat(name, &statbuf) != -1) { if (S_ISREG(statbuf.st_mode)) { printf("stale file found\n"); - if (unlink(name) == -1) { - sprintf(errmsg, "unlink of %s", name); - FAIL(errmsg); - } + if (unlink(name) == -1) + FAILF("unlink of %s", name); } if (S_ISDIR(statbuf.st_mode)) { printf("stale directory found\n"); - if (rmdir(name) == -1) { - sprintf(errmsg, "rmdir of %s", name); - FAIL(errmsg); - } + if (rmdir(name) == -1) + FAILF("rmdir of %s", name); } } } @@ -180,7 +176,6 @@ void remove_file_or_dir(char *name) void create_file(char *name, long filesize, int fill) { static char filename[MAX_FILENAME_LEN]; - char errmsg[MAX_FILENAME_LEN + 20]; char buf[1024 * 8]; char c = 'A' + size; int fd, rc; @@ -191,75 +186,60 @@ void create_file(char *name, long filesize, int fill) if (rank == 0) { sprintf(filename, "%s/%s", testdir, name); remove_file_or_dir(filename); - if ((fd = creat(filename, FILEMODE)) == -1) { - sprintf(errmsg, "create of file %s", filename); - FAIL(errmsg); - } + fd = creat(filename, FILEMODE); + if (fd < 0) + FAILF("create of file %s", filename); + if (filesize > 0) { if (lseek(fd, filesize - 1, SEEK_SET) == -1) { close(fd); - sprintf(errmsg, "lseek of file %s", filename); - FAIL(errmsg); + FAILF("lseek of file %s", filename); } if (write(fd, &zero, 1) == -1) { close(fd); - sprintf(errmsg, "write of file %s", filename); - FAIL(errmsg); + FAILF("write of file %s", filename); } } if (filesize > 0 && fill) { if (lseek(fd, 0, SEEK_SET) == -1) { close(fd); - sprintf(errmsg, "lseek of file %s", filename); - FAIL(errmsg); + FAILF("lseek of file %s", filename); } memset(buf, c, 1024); while (left > 0) { - if ((rc = write(fd, buf, - left > (1024 * 8) ? (1024 * 8) : left)) == -1) { + rc = write(fd, buf, MAX(left, 8192)); + if (rc < 0) { close(fd); - sprintf(errmsg, "write of file %s", - filename); - FAIL(errmsg); + FAILF("write of file %s", filename); } left -= rc; } } - if (close(fd) == -1) { - sprintf(errmsg, "close of file %s", filename); - FAIL(errmsg); - } + if (close(fd) == -1) + FAILF("close of file %s", filename); } } void check_stat(char *filename, struct stat *state, struct stat *old_state) { - char errmsg[MAX_FILENAME_LEN + 20]; - - if (stat(filename, state) == -1) { - sprintf(errmsg, "stat of file %s", filename); - FAIL(errmsg); - } + if (stat(filename, state) == -1) + FAILF("stat of file %s", filename); if (memcmp(state, old_state, sizeof(struct stat)) != 0) { errno = 0; - sprintf(errmsg, LP_STAT_FMT, LP_STAT_ARGS); - FAIL(errmsg); + FAILF(LP_STAT_FMT, LP_STAT_ARGS); } } void remove_file(char *name) { char filename[MAX_FILENAME_LEN]; - char errmsg[MAX_FILENAME_LEN + 20]; /* Process 0 remove the file(s) */ if (rank == 0) { sprintf(filename, "%s/%s", testdir, name); - if (unlink(filename) == -1) { - sprintf(errmsg, "unlink of file %s", filename); - FAIL(errmsg); - } + if (unlink(filename) == -1) + FAILF("unlink of file %s", filename); } } diff --git a/lustre/tests/mpi/lp_utils.h b/lustre/tests/mpi/lp_utils.h index e30b60f..fc67ba5b 100644 --- a/lustre/tests/mpi/lp_utils.h +++ b/lustre/tests/mpi/lp_utils.h @@ -20,7 +20,7 @@ * GPL HEADER END */ /* - * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved. * Use is subject to license terms. */ /* @@ -37,18 +37,24 @@ #include -#define FAIL(msg) \ - \ +#define FAILF(fmt, ...) \ do { \ - printf("%s: Process %d (%s)\n", timestamp(), rank, hostname); \ - if (debug) \ - printf("\tFAILED in %s:%d:%s()\n", __FILE__, __LINE__, __func__); \ - else \ - printf("\tFAILED in %s()\n", __func__); \ - printf("%s", msg); \ - fflush(stdout); \ - MPI_Abort(MPI_COMM_WORLD, 1); \ -} while(0) + printf("%s: Process %d (%s)\n", timestamp(), rank, hostname); \ + if (debug) \ + printf("\tFAILED in %s:%d:%s()\n", \ + __FILE__, __LINE__, __func__); \ + else \ + printf("\tFAILED in %s()\n", __func__); \ + printf(fmt, ##__VA_ARGS__); \ + fflush(stdout); \ + MPI_Abort(MPI_COMM_WORLD, 1); \ +} while (0) + +#define FAIL(msg) FAILF("%s", (msg)) + +#ifndef MAX +#define MAX(a, b) ((a) > (b) ? (b) : (a)) +#endif #define FILEMODE S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH #define MAX_FILENAME_LEN 512 -- 1.8.3.1