| 1 |
/***************************************************************************** |
| 2 |
* Copyright (C) 2010-14 Lawrence Livermore National Security, LLC. |
| 3 |
* Written by Jim Garlick <[email protected]> LLNL-CODE-423279 |
| 4 |
* All Rights Reserved. |
| 5 |
* |
| 6 |
* This file is part of the Distributed I/O Daemon (diod). |
| 7 |
* For details, see http://code.google.com/p/diod. |
| 8 |
* |
| 9 |
* This program is free software; you can redistribute it and/or modify it |
| 10 |
* under the terms of the GNU General Public License as published by the Free |
| 11 |
* Software Foundation; either version 2 of the license, or (at your option) |
| 12 |
* any later version. |
| 13 |
* |
| 14 |
* This program is distributed in the hope that it will be useful, but |
| 15 |
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 16 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 17 |
* for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU General Public License along |
| 20 |
* with this program; if not, write to the Free Software Foundation, |
| 21 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 22 |
* See also: http://www.gnu.org/licenses |
| 23 |
*****************************************************************************/ |
| 24 |
|
| 25 |
/* diodload.c - load generator for diod */ |
| 26 |
|
| 27 |
#if HAVE_CONFIG_H |
| 28 |
#include "config.h" |
| 29 |
#endif |
| 30 |
#include <sys/types.h> |
| 31 |
#include <sys/stat.h> |
| 32 |
#include <fcntl.h> |
| 33 |
#include <unistd.h> |
| 34 |
#include <stdlib.h> |
| 35 |
#include <stdint.h> |
| 36 |
#include <inttypes.h> |
| 37 |
#include <stdarg.h> |
| 38 |
#include <stdio.h> |
| 39 |
#if HAVE_GETOPT_H |
| 40 |
#include <getopt.h> |
| 41 |
#endif |
| 42 |
#include <string.h> |
| 43 |
#include <errno.h> |
| 44 |
#include <ctype.h> |
| 45 |
#include <libgen.h> |
| 46 |
#include <signal.h> |
| 47 |
#include <time.h> |
| 48 |
#include <pthread.h> |
| 49 |
|
| 50 |
#include "9p.h" |
| 51 |
#include "npfs.h" |
| 52 |
#include "npclient.h" |
| 53 |
|
| 54 |
#include "list.h" |
| 55 |
#include "diod_log.h" |
| 56 |
#include "diod_sock.h" |
| 57 |
#include "diod_auth.h" |
| 58 |
|
| 59 |
#define OPTIONS "s:m:n:r:g" |
| 60 |
#if HAVE_GETOPT_LONG |
| 61 |
#define GETOPT(ac,av,opt,lopt) getopt_long (ac,av,opt,lopt,NULL) |
| 62 |
static const struct option longopts[] = { |
| 63 |
{"server", required_argument, 0, 's'}, |
| 64 |
{"msize", required_argument, 0, 'm'}, |
| 65 |
{"numthreads", required_argument, 0, 'n'}, |
| 66 |
{"runtime", required_argument, 0, 'r'}, |
| 67 |
{"getattr", no_argument, 0, 'g'}, |
| 68 |
{0, 0, 0, 0}, |
| 69 |
}; |
| 70 |
#else |
| 71 |
#define GETOPT(ac,av,opt,lopt) getopt (ac,av,opt) |
| 72 |
#endif |
| 73 |
|
| 74 |
typedef enum { LOAD_IO, LOAD_GETATTR } load_t; |
| 75 |
|
| 76 |
typedef struct { |
| 77 |
pthread_t t; |
| 78 |
time_t stoptime; |
| 79 |
char *server; |
| 80 |
int msize; |
| 81 |
int fd; |
| 82 |
uint64_t readbytes; |
| 83 |
uint64_t writebytes; |
| 84 |
uint64_t opcount; |
| 85 |
char *buf; |
| 86 |
Npcfsys *fs; |
| 87 |
Npcfid *afid; |
| 88 |
Npcfid *root; |
| 89 |
Npcfid *infile; |
| 90 |
Npcfid *outfile; |
| 91 |
load_t loadtype; |
| 92 |
} thd_t; |
| 93 |
|
| 94 |
|
| 95 |
static void *loadgen (void *arg); |
| 96 |
|
| 97 |
static void |
| 98 |
usage (void) |
| 99 |
{ |
| 100 |
fprintf (stderr, |
| 101 |
"Usage: diodload [OPTIONS]\n" |
| 102 |
" -s,--server HOST:PORT server (default localhost:564)\n" |
| 103 |
" -m,--msize msize (default 65536)\n" |
| 104 |
" -n,--numthreads specify thread count (default 16)\n" |
| 105 |
" -r,--runtime specify runtime in seconds (default 10)\n" |
| 106 |
" -g,--getattr issue getattrs instead of read/write\n" |
| 107 |
); |
| 108 |
exit (1); |
| 109 |
} |
| 110 |
|
| 111 |
int |
| 112 |
main (int argc, char *argv[]) |
| 113 |
{ |
| 114 |
char *server = NULL; |
| 115 |
int msize = 65536; |
| 116 |
int numthreads = 16; |
| 117 |
int runtime = 10; |
| 118 |
int i, c, err; |
| 119 |
time_t now = time (NULL); |
| 120 |
thd_t *t; |
| 121 |
uint64_t readbytes = 0, writebytes = 0, opcount = 0; |
| 122 |
load_t loadtype = LOAD_IO; |
| 123 |
|
| 124 |
diod_log_init (argv[0]); |
| 125 |
|
| 126 |
opterr = 0; |
| 127 |
while ((c = GETOPT (argc, argv, OPTIONS, longopts)) != -1) { |
| 128 |
switch (c) { |
| 129 |
case 's': /* --server HOST[:PORT] or /path/to/socket */ |
| 130 |
server = optarg; |
| 131 |
break; |
| 132 |
case 'm': /* --msize SIZE */ |
| 133 |
msize = strtoul (optarg, NULL, 10); |
| 134 |
break; |
| 135 |
case 'n': /* --numthreads INT */ |
| 136 |
numthreads = strtoul (optarg, NULL, 10); |
| 137 |
break; |
| 138 |
case 'r': /* --runtime INT */ |
| 139 |
runtime = strtoul (optarg, NULL, 10); |
| 140 |
break; |
| 141 |
case 'g': /* --getattr */ |
| 142 |
loadtype = LOAD_GETATTR; |
| 143 |
break; |
| 144 |
default: |
| 145 |
usage (); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
if (signal (SIGPIPE, SIG_IGN) == SIG_ERR) |
| 150 |
err_exit ("signal"); |
| 151 |
|
| 152 |
if (!(t = malloc (sizeof (thd_t) * numthreads))) |
| 153 |
msg_exit ("out of memory"); |
| 154 |
|
| 155 |
for (i = 0; i < numthreads; i++) { |
| 156 |
t[i].server = server; |
| 157 |
t[i].msize = msize; |
| 158 |
t[i].stoptime = now + runtime; |
| 159 |
t[i].fd = -1; |
| 160 |
t[i].fs = NULL; |
| 161 |
t[i].root = t[i].afid = t[i].infile = t[i].outfile = NULL; |
| 162 |
t[i].readbytes = t[i].writebytes = 0; |
| 163 |
t[i].loadtype = loadtype; |
| 164 |
if (!(t[i].buf = malloc (msize))) |
| 165 |
msg_exit ("out of memory"); |
| 166 |
if ((err = pthread_create (&t[i].t, NULL, loadgen, &t[i]))) |
| 167 |
errn_exit (err, "pthread_create"); |
| 168 |
} |
| 169 |
|
| 170 |
for (i = 0; i < numthreads; i++) { |
| 171 |
if ((err = pthread_join (t[i].t, NULL))) |
| 172 |
errn_exit (err, "pthread_join"); |
| 173 |
readbytes += t[i].readbytes; |
| 174 |
writebytes += t[i].writebytes; |
| 175 |
opcount += t[i].opcount; |
| 176 |
free (t[i].buf); |
| 177 |
} |
| 178 |
free (t); |
| 179 |
|
| 180 |
msg ("%"PRIu64" ops/s, %"PRIu64" rMB/s, %"PRIu64" wMB/s", |
| 181 |
opcount/runtime, |
| 182 |
readbytes/(1024*1024*runtime), writebytes/(1024*1024*runtime)); |
| 183 |
|
| 184 |
diod_log_fini (); |
| 185 |
|
| 186 |
exit (0); |
| 187 |
} |
| 188 |
|
| 189 |
static void * |
| 190 |
loadgen (void *arg) |
| 191 |
{ |
| 192 |
thd_t *t = (thd_t *)arg; |
| 193 |
uid_t uid = geteuid (); |
| 194 |
void *ret = NULL; |
| 195 |
int n, loops = 0; |
| 196 |
|
| 197 |
if ((t->fd = diod_sock_connect (t->server, 0)) < 0) |
| 198 |
goto done; |
| 199 |
if (!(t->fs = npc_start (t->fd, t->fd, t->msize, 0))) { |
| 200 |
errn (np_rerror (), "error negotiating protocol with server"); |
| 201 |
goto done; |
| 202 |
} |
| 203 |
if (!(t->afid = npc_auth (t->fs, "ctl", uid, diod_auth)) |
| 204 |
&& np_rerror () != 0) { |
| 205 |
errn (np_rerror (), "error authenticating to server"); |
| 206 |
goto done; |
| 207 |
} |
| 208 |
if (!(t->root = npc_attach (t->fs, t->afid, "ctl", uid))) { |
| 209 |
errn (np_rerror (), "error attaching to aname=ctl"); |
| 210 |
goto done; |
| 211 |
} |
| 212 |
if (t->loadtype == LOAD_IO) { |
| 213 |
if (!(t->infile = npc_open_bypath (t->root, "zero", O_RDONLY))) { |
| 214 |
errn (np_rerror (), "open zero"); |
| 215 |
goto done; |
| 216 |
} |
| 217 |
if (!(t->outfile = npc_open_bypath (t->root, "null", O_WRONLY))) { |
| 218 |
errn (np_rerror (), "open null"); |
| 219 |
goto done; |
| 220 |
} |
| 221 |
do { |
| 222 |
if ((n = npc_pread (t->infile, t->buf, t->msize, 0)) <= 0) { |
| 223 |
errn (np_rerror (), "read zero"); |
| 224 |
break; |
| 225 |
} |
| 226 |
t->readbytes += n; |
| 227 |
if ((n = npc_pwrite (t->outfile, t->buf, t->msize, 0)) < 0) { |
| 228 |
errn (np_rerror (), "write null"); |
| 229 |
break; |
| 230 |
} |
| 231 |
t->writebytes += n; |
| 232 |
t->opcount++; |
| 233 |
loops++; |
| 234 |
} while ((loops % 100 != 0) || time (NULL) < t->stoptime); |
| 235 |
} else if (t->loadtype == LOAD_GETATTR) { |
| 236 |
if (!(t->infile = npc_walk (t->root, "null"))) { |
| 237 |
errn (np_rerror (), "walk null"); |
| 238 |
goto done; |
| 239 |
} |
| 240 |
do { |
| 241 |
struct stat sb; |
| 242 |
|
| 243 |
if (npc_fstat (t->infile, &sb) < 0) { |
| 244 |
errn (np_rerror (), "walk null"); |
| 245 |
break; |
| 246 |
} |
| 247 |
t->opcount++; |
| 248 |
loops++; |
| 249 |
} while ((loops % 100 != 0) || time (NULL) < t->stoptime); |
| 250 |
} |
| 251 |
done: |
| 252 |
if (t->outfile && npc_clunk (t->outfile) < 0) |
| 253 |
errn (np_rerror (), "error clunking null"); |
| 254 |
if (t->infile && npc_clunk (t->infile) < 0) |
| 255 |
errn (np_rerror (), "error clunking zero"); |
| 256 |
if (t->root && npc_clunk (t->root) < 0) |
| 257 |
errn (np_rerror (), "error clunking ctl"); |
| 258 |
if (t->afid && npc_clunk (t->afid) < 0) |
| 259 |
errn (np_rerror (), "error clunking afid"); |
| 260 |
if (t->fs) { |
| 261 |
npc_finish (t->fs); /* closes fd */ |
| 262 |
t->fd = -1; |
| 263 |
} |
| 264 |
if (t->fd != -1) |
| 265 |
close (t->fd); |
| 266 |
return ret; |
| 267 |
} |
| 268 |
|
| 269 |
/* |
| 270 |
* vi:tabstop=4 shiftwidth=4 expandtab |
| 271 |
*/ |