Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nspr.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "plgetopt.h" |
michael@0 | 9 | |
michael@0 | 10 | #include <stdlib.h> |
michael@0 | 11 | #include <string.h> |
michael@0 | 12 | |
michael@0 | 13 | |
michael@0 | 14 | #ifndef IOV_MAX |
michael@0 | 15 | #define IOV_MAX 16 |
michael@0 | 16 | #endif |
michael@0 | 17 | |
michael@0 | 18 | #define BASE_PORT 9867 |
michael@0 | 19 | |
michael@0 | 20 | int PR_CALLBACK Writev(int argc, char **argv) |
michael@0 | 21 | { |
michael@0 | 22 | |
michael@0 | 23 | PRStatus rv; |
michael@0 | 24 | PRNetAddr serverAddr; |
michael@0 | 25 | PRFileDesc *clientSock, *debug = NULL; |
michael@0 | 26 | |
michael@0 | 27 | char *buffer = NULL; |
michael@0 | 28 | PRIOVec *iov = NULL; |
michael@0 | 29 | PRBool passed = PR_TRUE; |
michael@0 | 30 | PRIntervalTime timein, elapsed, timeout; |
michael@0 | 31 | PRIntervalTime tmo_min = 0x7fffffff, tmo_max = 0, tmo_elapsed = 0; |
michael@0 | 32 | PRInt32 tmo_counted = 0, iov_index, loop, bytes, number_fragments; |
michael@0 | 33 | PRInt32 message_length = 100, fragment_length = 100, messages = 100; |
michael@0 | 34 | struct Descriptor { PRInt32 length; PRUint32 checksum; } descriptor; |
michael@0 | 35 | |
michael@0 | 36 | /* |
michael@0 | 37 | * USAGE |
michael@0 | 38 | * -h dns name of host serving the connection (default = self) |
michael@0 | 39 | * -m number of messages to send (default = 100) |
michael@0 | 40 | * -s size of each message (default = 100) |
michael@0 | 41 | * -f size of each message fragment (default = 100) |
michael@0 | 42 | */ |
michael@0 | 43 | |
michael@0 | 44 | PLOptStatus os; |
michael@0 | 45 | PLOptState *opt = PL_CreateOptState(argc, argv, "dh:m:s:f:"); |
michael@0 | 46 | |
michael@0 | 47 | PR_STDIO_INIT(); |
michael@0 | 48 | rv = PR_InitializeNetAddr(PR_IpAddrLoopback, BASE_PORT, &serverAddr); |
michael@0 | 49 | PR_ASSERT(PR_SUCCESS == rv); |
michael@0 | 50 | |
michael@0 | 51 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) |
michael@0 | 52 | { |
michael@0 | 53 | if (PL_OPT_BAD == os) continue; |
michael@0 | 54 | switch (opt->option) |
michael@0 | 55 | { |
michael@0 | 56 | case 'h': /* the remote host */ |
michael@0 | 57 | { |
michael@0 | 58 | PRIntn es = 0; |
michael@0 | 59 | PRHostEnt host; |
michael@0 | 60 | char buffer[1024]; |
michael@0 | 61 | (void)PR_GetHostByName(opt->value, buffer, sizeof(buffer), &host); |
michael@0 | 62 | es = PR_EnumerateHostEnt(es, &host, BASE_PORT, &serverAddr); |
michael@0 | 63 | PR_ASSERT(es > 0); |
michael@0 | 64 | } |
michael@0 | 65 | break; |
michael@0 | 66 | case 'd': /* debug mode */ |
michael@0 | 67 | debug = PR_GetSpecialFD(PR_StandardError); |
michael@0 | 68 | break; |
michael@0 | 69 | case 'm': /* number of messages to send */ |
michael@0 | 70 | messages = atoi(opt->value); |
michael@0 | 71 | break; |
michael@0 | 72 | case 's': /* total size of each message */ |
michael@0 | 73 | message_length = atoi(opt->value); |
michael@0 | 74 | break; |
michael@0 | 75 | case 'f': /* size of each message fragment */ |
michael@0 | 76 | fragment_length = atoi(opt->value); |
michael@0 | 77 | break; |
michael@0 | 78 | default: |
michael@0 | 79 | break; |
michael@0 | 80 | } |
michael@0 | 81 | } |
michael@0 | 82 | PL_DestroyOptState(opt); |
michael@0 | 83 | |
michael@0 | 84 | buffer = (char*)malloc(message_length); |
michael@0 | 85 | |
michael@0 | 86 | number_fragments = (message_length + fragment_length - 1) / fragment_length + 1; |
michael@0 | 87 | while (IOV_MAX < number_fragments) |
michael@0 | 88 | { |
michael@0 | 89 | fragment_length = message_length / (IOV_MAX - 2); |
michael@0 | 90 | number_fragments = (message_length + fragment_length - 1) / |
michael@0 | 91 | fragment_length + 1; |
michael@0 | 92 | if (NULL != debug) PR_fprintf(debug, |
michael@0 | 93 | "Too many fragments - reset fragment length to %ld\n", fragment_length); |
michael@0 | 94 | } |
michael@0 | 95 | iov = (PRIOVec*)malloc(number_fragments * sizeof(PRIOVec)); |
michael@0 | 96 | |
michael@0 | 97 | iov[0].iov_base = (char*)&descriptor; |
michael@0 | 98 | iov[0].iov_len = sizeof(descriptor); |
michael@0 | 99 | for (iov_index = 1; iov_index < number_fragments; ++iov_index) |
michael@0 | 100 | { |
michael@0 | 101 | iov[iov_index].iov_base = buffer + (iov_index - 1) * fragment_length; |
michael@0 | 102 | iov[iov_index].iov_len = fragment_length; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | for (bytes = 0; bytes < message_length; ++bytes) |
michael@0 | 106 | buffer[bytes] = (char)bytes; |
michael@0 | 107 | |
michael@0 | 108 | timeout = PR_SecondsToInterval(1); |
michael@0 | 109 | |
michael@0 | 110 | for (loop = 0; loop < messages; ++loop) |
michael@0 | 111 | { |
michael@0 | 112 | if (NULL != debug) |
michael@0 | 113 | PR_fprintf(debug, "[%d]socket ... ", loop); |
michael@0 | 114 | clientSock = PR_NewTCPSocket(); |
michael@0 | 115 | if (clientSock) |
michael@0 | 116 | { |
michael@0 | 117 | timein = PR_IntervalNow(); |
michael@0 | 118 | if (NULL != debug) |
michael@0 | 119 | PR_fprintf(debug, "connecting ... "); |
michael@0 | 120 | rv = PR_Connect(clientSock, &serverAddr, timeout); |
michael@0 | 121 | if (PR_SUCCESS == rv) |
michael@0 | 122 | { |
michael@0 | 123 | descriptor.checksum = 0; |
michael@0 | 124 | descriptor.length = (loop < (messages - 1)) ? message_length : 0; |
michael@0 | 125 | if (0 == descriptor.length) number_fragments = 1; |
michael@0 | 126 | else |
michael@0 | 127 | for (iov_index = 0; iov_index < descriptor.length; ++iov_index) |
michael@0 | 128 | { |
michael@0 | 129 | PRUint32 overflow = descriptor.checksum & 0x80000000; |
michael@0 | 130 | descriptor.checksum = (descriptor.checksum << 1); |
michael@0 | 131 | if (0x00000000 != overflow) descriptor.checksum += 1; |
michael@0 | 132 | descriptor.checksum += buffer[iov_index]; |
michael@0 | 133 | } |
michael@0 | 134 | if (NULL != debug) PR_fprintf( |
michael@0 | 135 | debug, "sending %d bytes ... ", descriptor.length); |
michael@0 | 136 | |
michael@0 | 137 | /* then, at the last moment ... */ |
michael@0 | 138 | descriptor.length = PR_ntohl(descriptor.length); |
michael@0 | 139 | descriptor.checksum = PR_ntohl(descriptor.checksum); |
michael@0 | 140 | |
michael@0 | 141 | bytes = PR_Writev(clientSock, iov, number_fragments, timeout); |
michael@0 | 142 | if (NULL != debug) |
michael@0 | 143 | PR_fprintf(debug, "closing ... "); |
michael@0 | 144 | rv = PR_Shutdown(clientSock, PR_SHUTDOWN_BOTH); |
michael@0 | 145 | rv = PR_Close(clientSock); |
michael@0 | 146 | if (NULL != debug) PR_fprintf( |
michael@0 | 147 | debug, "%s\n", ((PR_SUCCESS == rv) ? "good" : "bad")); |
michael@0 | 148 | elapsed = PR_IntervalNow() - timein; |
michael@0 | 149 | if (elapsed < tmo_min) tmo_min = elapsed; |
michael@0 | 150 | else if (elapsed > tmo_max) tmo_max = elapsed; |
michael@0 | 151 | tmo_elapsed += elapsed; |
michael@0 | 152 | tmo_counted += 1; |
michael@0 | 153 | } |
michael@0 | 154 | else |
michael@0 | 155 | { |
michael@0 | 156 | if (NULL != debug) PR_fprintf( |
michael@0 | 157 | debug, "failed - retrying (%d, %d)\n", |
michael@0 | 158 | PR_GetError(), PR_GetOSError()); |
michael@0 | 159 | PR_Close(clientSock); |
michael@0 | 160 | } |
michael@0 | 161 | } |
michael@0 | 162 | else if (NULL != debug) |
michael@0 | 163 | { |
michael@0 | 164 | PR_fprintf(debug, "unable to create client socket\n"); |
michael@0 | 165 | passed = PR_FALSE; |
michael@0 | 166 | } |
michael@0 | 167 | } |
michael@0 | 168 | if (NULL != debug) { |
michael@0 | 169 | if (0 == tmo_counted) { |
michael@0 | 170 | PR_fprintf(debug, "No connection made\n"); |
michael@0 | 171 | } else { |
michael@0 | 172 | PR_fprintf( |
michael@0 | 173 | debug, "\nTimings: %d [%d] %d (microseconds)\n", |
michael@0 | 174 | PR_IntervalToMicroseconds(tmo_min), |
michael@0 | 175 | PR_IntervalToMicroseconds(tmo_elapsed / tmo_counted), |
michael@0 | 176 | PR_IntervalToMicroseconds(tmo_max)); |
michael@0 | 177 | } |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | PR_DELETE(buffer); |
michael@0 | 181 | PR_DELETE(iov); |
michael@0 | 182 | |
michael@0 | 183 | PR_fprintf( |
michael@0 | 184 | PR_GetSpecialFD(PR_StandardError), |
michael@0 | 185 | "%s\n", (passed) ? "PASSED" : "FAILED"); |
michael@0 | 186 | return (passed) ? 0 : 1; |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | int main(int argc, char **argv) |
michael@0 | 190 | { |
michael@0 | 191 | return (PR_VersionCheck(PR_VERSION)) ? |
michael@0 | 192 | PR_Initialize(Writev, argc, argv, 4) : -1; |
michael@0 | 193 | } /* main */ |
michael@0 | 194 | |
michael@0 | 195 | /* writev.c */ |
michael@0 | 196 | |
michael@0 | 197 |