| 1 |
/* |
| 2 |
SMB tree connection rate test |
| 3 |
|
| 4 |
Copyright (C) 2006-2007 James Peach |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 3 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
This program is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "includes.h" |
| 21 |
#include "libcli/libcli.h" |
| 22 |
#include "libcli/resolve/resolve.h" |
| 23 |
#include "torture/smbtorture.h" |
| 24 |
#include "lib/cmdline/cmdline.h" |
| 25 |
#include "param/param.h" |
| 26 |
|
| 27 |
#include "system/filesys.h" |
| 28 |
#include "system/shmem.h" |
| 29 |
#include "torture/raw/proto.h" |
| 30 |
|
| 31 |
#define TIME_LIMIT_SECS 30 |
| 32 |
#define usec_to_sec(s) ((s) / 1000000) |
| 33 |
|
| 34 |
/* Map a shared memory buffer of at least nelem counters. */ |
| 35 |
static void * map_count_buffer(unsigned nelem, size_t elemsz) |
| 36 |
{ |
| 37 |
void * buf; |
| 38 |
size_t bufsz; |
| 39 |
size_t pagesz = getpagesize(); |
| 40 |
|
| 41 |
bufsz = nelem * elemsz; |
| 42 |
bufsz = (bufsz + pagesz) % pagesz; /* round up to pagesz */ |
| 43 |
|
| 44 |
#ifdef MAP_ANON |
| 45 |
/* BSD */ |
| 46 |
buf = mmap(NULL, bufsz, PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, |
| 47 |
-1 /* fd */, 0 /* offset */); |
| 48 |
#else |
| 49 |
buf = mmap(NULL, bufsz, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, |
| 50 |
open("/dev/zero", O_RDWR), 0 /* offset */); |
| 51 |
#endif |
| 52 |
|
| 53 |
if (buf == MAP_FAILED) { |
| 54 |
printf("failed to map count buffer: %s\n", |
| 55 |
strerror(errno)); |
| 56 |
return NULL; |
| 57 |
} |
| 58 |
|
| 59 |
return buf; |
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
static int fork_tcon_client(struct torture_context *tctx, |
| 64 |
int *tcon_count, unsigned tcon_timelimit, |
| 65 |
const char *host, const char *share) |
| 66 |
{ |
| 67 |
pid_t child; |
| 68 |
struct smbcli_state *cli; |
| 69 |
struct timeval end; |
| 70 |
struct timeval now; |
| 71 |
struct smbcli_options options; |
| 72 |
struct smbcli_session_options session_options; |
| 73 |
|
| 74 |
lpcfg_smbcli_options(tctx->lp_ctx, &options); |
| 75 |
lpcfg_smbcli_session_options(tctx->lp_ctx, &session_options); |
| 76 |
|
| 77 |
child = fork(); |
| 78 |
if (child == -1) { |
| 79 |
printf("failed to fork child: %s\n,", strerror(errno)); |
| 80 |
return -1; |
| 81 |
} else if (child != 0) { |
| 82 |
/* Parent, just return. */ |
| 83 |
return 0; |
| 84 |
} |
| 85 |
|
| 86 |
/* Child. Just make as many connections as possible within the |
| 87 |
* time limit. Don't bother synchronising the child start times |
| 88 |
* because it's probably not work the effort, and a bit of startup |
| 89 |
* jitter is probably a more realistic test. |
| 90 |
*/ |
| 91 |
|
| 92 |
|
| 93 |
end = timeval_current(); |
| 94 |
now = timeval_current(); |
| 95 |
end.tv_sec += tcon_timelimit; |
| 96 |
*tcon_count = 0; |
| 97 |
|
| 98 |
while (timeval_compare(&now, &end) == -1) { |
| 99 |
NTSTATUS status; |
| 100 |
|
| 101 |
status = smbcli_full_connection(NULL, &cli, |
| 102 |
host, share, |
| 103 |
NULL, lpcfg_socket_options(tctx->lp_ctx), |
| 104 |
samba_cmdline_get_creds(), |
| 105 |
tctx->lp_ctx, |
| 106 |
lpcfg_resolve_context(tctx->lp_ctx), |
| 107 |
tctx->ev, &options, &session_options, |
| 108 |
lpcfg_gensec_settings(tctx, tctx->lp_ctx)); |
| 109 |
|
| 110 |
if (!NT_STATUS_IS_OK(status)) { |
| 111 |
printf("failed to connect to //%s/%s: %s\n", |
| 112 |
host, share, nt_errstr(status)); |
| 113 |
goto done; |
| 114 |
} |
| 115 |
|
| 116 |
smbcli_tdis(cli); |
| 117 |
talloc_free(cli); |
| 118 |
|
| 119 |
*tcon_count = *tcon_count + 1; |
| 120 |
now = timeval_current(); |
| 121 |
} |
| 122 |
|
| 123 |
done: |
| 124 |
exit(0); |
| 125 |
} |
| 126 |
|
| 127 |
static bool children_remain(void) |
| 128 |
{ |
| 129 |
bool res; |
| 130 |
|
| 131 |
/* Reap as many children as possible. */ |
| 132 |
for (;;) { |
| 133 |
pid_t ret = waitpid(-1, NULL, WNOHANG); |
| 134 |
if (ret == 0) { |
| 135 |
/* no children ready */ |
| 136 |
res = true; |
| 137 |
break; |
| 138 |
} |
| 139 |
if (ret == -1) { |
| 140 |
/* no children left. maybe */ |
| 141 |
res = errno != ECHILD; |
| 142 |
break; |
| 143 |
} |
| 144 |
} |
| 145 |
return res; |
| 146 |
} |
| 147 |
|
| 148 |
static double rate_convert_secs(unsigned count, |
| 149 |
const struct timeval *start, const struct timeval *end) |
| 150 |
{ |
| 151 |
return (double)count / |
| 152 |
usec_to_sec((double)usec_time_diff(end, start)); |
| 153 |
} |
| 154 |
|
| 155 |
/* Test the rate at which the server will accept connections. */ |
| 156 |
bool torture_bench_treeconnect(struct torture_context *tctx) |
| 157 |
{ |
| 158 |
const char *host = torture_setting_string(tctx, "host", NULL); |
| 159 |
const char *share = torture_setting_string(tctx, "share", NULL); |
| 160 |
|
| 161 |
int timelimit = torture_setting_int(tctx, "timelimit", |
| 162 |
TIME_LIMIT_SECS); |
| 163 |
int nprocs = torture_setting_int(tctx, "nprocs", 4); |
| 164 |
|
| 165 |
int *curr_counts = map_count_buffer(nprocs, sizeof(int)); |
| 166 |
int *last_counts = talloc_zero_array(tctx, int, nprocs); |
| 167 |
|
| 168 |
struct timeval now, last, start; |
| 169 |
int i, delta; |
| 170 |
|
| 171 |
torture_assert(tctx, nprocs > 0, "bad proc count"); |
| 172 |
torture_assert(tctx, timelimit > 0, "bad timelimit"); |
| 173 |
torture_assert(tctx, curr_counts, "allocation failure"); |
| 174 |
torture_assert(tctx, last_counts, "allocation failure"); |
| 175 |
|
| 176 |
start = last = timeval_current(); |
| 177 |
for (i = 0; i < nprocs; ++i) { |
| 178 |
fork_tcon_client(tctx, &curr_counts[i], timelimit, host, share); |
| 179 |
} |
| 180 |
|
| 181 |
while (children_remain()) { |
| 182 |
|
| 183 |
sleep(1); |
| 184 |
now = timeval_current(); |
| 185 |
|
| 186 |
for (i = 0, delta = 0; i < nprocs; ++i) { |
| 187 |
delta += curr_counts[i] - last_counts[i]; |
| 188 |
} |
| 189 |
|
| 190 |
printf("%u connections/sec\n", |
| 191 |
(unsigned)rate_convert_secs(delta, &last, &now)); |
| 192 |
|
| 193 |
memcpy(last_counts, curr_counts, nprocs * sizeof(int)); |
| 194 |
last = timeval_current(); |
| 195 |
} |
| 196 |
|
| 197 |
now = timeval_current(); |
| 198 |
|
| 199 |
for (i = 0, delta = 0; i < nprocs; ++i) { |
| 200 |
delta += curr_counts[i]; |
| 201 |
} |
| 202 |
|
| 203 |
printf("TOTAL: %u connections/sec over %u secs\n", |
| 204 |
(unsigned)rate_convert_secs(delta, &start, &now), |
| 205 |
timelimit); |
| 206 |
return true; |
| 207 |
} |
| 208 |
|
| 209 |
/* vim: set sts=8 sw=8 : */ |