X-Git-Url: http://git.alex.org.uk diff --git a/nbd-tester-client.c b/nbd-tester-client.c index 6824b48..78d4a1f 100644 --- a/nbd-tester-client.c +++ b/nbd-tester-client.c @@ -23,11 +23,10 @@ */ #include #include +#include #include -#include #include #include -#include #include #include #include @@ -36,9 +35,14 @@ #define MY_NAME "nbd-tester-client" #include "cliserv.h" +#include +#include + static gchar errstr[1024]; const static int errstr_len=1024; +static uint64_t size; + typedef enum { CONNECTION_TYPE_NONE, CONNECTION_TYPE_CONNECT, @@ -52,7 +56,36 @@ typedef enum { CONNECTION_CLOSE_FAST, } CLOSE_TYPE; -inline int read_all(int f, void *buf, size_t len) { +#define TEST_WRITE (1<<0) +#define TEST_FLUSH (1<<1) + +int timeval_subtract (struct timeval *result, struct timeval *x, + struct timeval *y) { + if (x->tv_usec < y->tv_usec) { + int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; + y->tv_usec -= 1000000 * nsec; + y->tv_sec += nsec; + } + + if (x->tv_usec - y->tv_usec > 1000000) { + int nsec = (x->tv_usec - y->tv_usec) / 1000000; + y->tv_usec += 1000000 * nsec; + y->tv_sec -= nsec; + } + + result->tv_sec = x->tv_sec - y->tv_sec; + result->tv_usec = x->tv_usec - y->tv_usec; + + return x->tv_sec < y->tv_sec; +} + +double timeval_diff_to_double (struct timeval * x, struct timeval * y) { + struct timeval r; + timeval_subtract(&r, x, y); + return r.tv_sec * 1.0 + r.tv_usec/1000000.0; +} + +static inline int read_all(int f, void *buf, size_t len) { ssize_t res; size_t retval=0; @@ -68,12 +101,36 @@ inline int read_all(int f, void *buf, size_t len) { return retval; } -int setup_connection(gchar *hostname, int port, CONNECTION_TYPE ctype) { +static inline int write_all(int f, void *buf, size_t len) { + ssize_t res; + size_t retval=0; + + while(len>0) { + if((res=write(f, buf, len)) <=0) { + snprintf(errstr, errstr_len, "Write failed: %s", strerror(errno)); + return -1; + } + len-=res; + buf+=res; + retval+=res; + } + return retval; +} + +#define READ_ALL_ERRCHK(f, buf, len, whereto, errmsg...) if((read_all(f, buf, len))<=0) { snprintf(errstr, errstr_len, ##errmsg); goto whereto; } +#define READ_ALL_ERR_RT(f, buf, len, whereto, rval, errmsg...) if((read_all(f, buf, len))<=0) { snprintf(errstr, errstr_len, ##errmsg); retval = rval; goto whereto; } + +#define WRITE_ALL_ERRCHK(f, buf, len, whereto, errmsg...) if((write_all(f, buf, len))<=0) { snprintf(errstr, errstr_len, ##errmsg); goto whereto; } +#define WRITE_ALL_ERR_RT(f, buf, len, whereto, rval, errmsg...) if((write_all(f, buf, len))<=0) { snprintf(errstr, errstr_len, ##errmsg); retval = rval; goto whereto; } + +int setup_connection(gchar *hostname, int port, gchar* name, CONNECTION_TYPE ctype, int* serverflags) { int sock; struct hostent *host; struct sockaddr_in addr; char buf[256]; + uint64_t mymagic = (name ? opts_magic : cliserv_magic); u64 tmp64; + uint32_t tmp32 = 0; sock=0; if(ctype>10) & 15) == 3); + int sendflush = (testflags & TEST_FLUSH) && (((i>>10) & 15) == 11); + req.type=htonl((testflags & TEST_WRITE)?NBD_CMD_WRITE:NBD_CMD_READ); + if (sendfua) + req.type = htonl(NBD_CMD_WRITE | NBD_CMD_FLAG_FUA); + memcpy(&(req.handle),&i,sizeof(i)); req.from=htonll(i); - write(sock, &req, sizeof(req)); - printf("Requests(+): %d\n", ++requests); + if (write_all(sock, &req, sizeof(req)) <0) { + retval=-1; + goto err_open; + } + if (testflags & TEST_WRITE) { + if (write_all(sock, writebuf, 1024) <0) { + retval=-1; + goto err_open; + } + } + printf("%d: Requests(+): %d\n", (int)mypid, ++requests); + if (sendflush) { + long long int j = i ^ (1LL<<63); + req.type = htonl(NBD_CMD_FLUSH); + memcpy(&(req.handle),&j,sizeof(j)); + req.from=0; + if (write_all(sock, &req, sizeof(req)) <0) { + retval=-1; + goto err_open; + } + printf("%d: Requests(+): %d\n", (int)mypid, ++requests); + } } do { FD_ZERO(&set); @@ -258,11 +419,11 @@ int throughput_test(gchar* hostname, int port, int sock, char sock_is_open, char if(FD_ISSET(sock, &set)) { /* Okay, there's something ready for * reading here */ - if(read_packet_check_header(sock, 1024, i)<0) { + if(read_packet_check_header(sock, (testflags & TEST_WRITE)?0:1024, i)<0) { retval=-1; goto err_open; } - printf("Requests(-): %d\n", --requests); + printf("%d: Requests(-): %d\n", (int)mypid, --requests); } } while FD_ISSET(sock, &set); /* Now wait until we can write again or until a second have @@ -289,8 +450,8 @@ int throughput_test(gchar* hostname, int port, int sock, char sock_is_open, char if(FD_ISSET(sock, &set)) { /* Okay, there's something ready for * reading here */ - read_packet_check_header(sock, 1024, i); - printf("Requests(-): %d\n", --requests); + read_packet_check_header(sock, (testflags & TEST_WRITE)?0:1024, i); + printf("%d: Requests(-): %d\n", (int)mypid, --requests); } } while (requests); if(gettimeofday(&stop, NULL)<0) { @@ -298,21 +459,21 @@ int throughput_test(gchar* hostname, int port, int sock, char sock_is_open, char snprintf(errstr, errstr_len, "Could not measure end time: %s", strerror(errno)); goto err_open; } - timespan=stop.tv_sec-start.tv_sec+(stop.tv_usec-start.tv_usec)/1000000; - speed=(int)(size/timespan); + timespan=timeval_diff_to_double(&stop, &start); + speed=size/timespan; if(speed>1024) { - speed>>=10; + speed=speed/1024.0; speedchar[0]='K'; } if(speed>1024) { - speed>>=10; + speed=speed/1024.0; speedchar[0]='M'; } if(speed>1024) { - speed>>=10; + speed=speed/1024.0; speedchar[0]='G'; } - g_message("Throughput test complete. Took %.3f seconds to complete, %d%sB/s",timespan,speed,speedchar); + g_message("%d: Throughput %s test (%s flushes) complete. Took %.3f seconds to complete, %.3f%sib/s", (int)getpid(), (testflags & TEST_WRITE)?"write":"read", (testflags & TEST_FLUSH)?"with":"without", timespan, speed, speedchar); err_open: if(close_sock) { @@ -322,28 +483,63 @@ err: return retval; } +typedef int (*testfunc)(gchar*, int, char*, int, char, char, int); + int main(int argc, char**argv) { gchar *hostname; - long int p; - int port; + long int p = 0; + char* name = NULL; int sock=0; + int c; + bool want_port = TRUE; + int nonopt=0; + int testflags=0; + testfunc test = throughput_test; if(argc<3) { - g_message("Not enough arguments"); - g_message("Usage: %s ", argv[0]); + g_message("%d: Not enough arguments", (int)getpid()); + g_message("%d: Usage: %s ", (int)getpid(), argv[0]); + g_message("%d: Or: %s -N ", (int)getpid(), argv[0]); exit(EXIT_FAILURE); } logging(); - hostname=g_strdup(argv[1]); - p=(strtol(argv[2], NULL, 0)); - if(p==LONG_MIN||p==LONG_MAX) { - g_critical("Could not parse port number: %s", strerror(errno)); - exit(EXIT_FAILURE); + while((c=getopt(argc, argv, "-N:owf"))>=0) { + switch(c) { + case 1: + switch(nonopt) { + case 0: + hostname=g_strdup(optarg); + nonopt++; + break; + case 1: + if(want_port) + p=(strtol(argv[2], NULL, 0)); + if(p==LONG_MIN||p==LONG_MAX) { + g_critical("Could not parse port number: %s", strerror(errno)); + exit(EXIT_FAILURE); + } + break; + } + break; + case 'N': + name=g_strdup(optarg); + p = 10809; + want_port = false; + break; + case 'o': + test=oversize_test; + break; + case 'w': + testflags|=TEST_WRITE; + break; + case 'f': + testflags|=TEST_FLUSH; + break; + } } - port=(int)p; - if(throughput_test(hostname, port, sock, FALSE, TRUE)<0) { - g_warning("Could not run throughput test: %s", errstr); + if(test(hostname, (int)p, name, sock, FALSE, TRUE, testflags)<0) { + g_warning("Could not run test: %s", errstr); exit(EXIT_FAILURE); }