Author: mturk Date: Sat Oct 22 05:32:32 2005 New Revision: 327657 URL: http://svn.apache.org/viewcvs?rev=327657&view=rev Log: Add optimized function for send and recv using presetting of internal ByteBuffer. This way the number of JNI calls is lowered that gives much higher speed, because each send or recv call now have one function param and one JNI call less then before.
Modified: tomcat/connectors/trunk/http11/src/java/org/apache/coyote/http11/InternalAprInputBuffer.java tomcat/connectors/trunk/http11/src/java/org/apache/coyote/http11/InternalAprOutputBuffer.java tomcat/connectors/trunk/jni/java/org/apache/tomcat/jni/Socket.java tomcat/connectors/trunk/jni/native/include/tcn.h tomcat/connectors/trunk/jni/native/src/network.c tomcat/connectors/trunk/util/java/org/apache/tomcat/util/net/AprEndpoint.java Modified: tomcat/connectors/trunk/http11/src/java/org/apache/coyote/http11/InternalAprInputBuffer.java URL: http://svn.apache.org/viewcvs/tomcat/connectors/trunk/http11/src/java/org/apache/coyote/http11/InternalAprInputBuffer.java?rev=327657&r1=327656&r2=327657&view=diff ============================================================================== --- tomcat/connectors/trunk/http11/src/java/org/apache/coyote/http11/InternalAprInputBuffer.java (original) +++ tomcat/connectors/trunk/http11/src/java/org/apache/coyote/http11/InternalAprInputBuffer.java Sat Oct 22 05:32:32 2005 @@ -215,6 +215,7 @@ */ public void setSocket(long socket) { this.socket = socket; + Socket.setrbb(this.socket, bbuf); } @@ -405,8 +406,8 @@ } // Do a simple read with a short timeout bbuf.clear(); - int nRead = Socket.recvbt - (socket, bbuf, 0, buf.length - lastValid, readTimeout); + int nRead = Socket.recvbbt + (socket, 0, buf.length - lastValid, readTimeout); if (nRead > 0) { bbuf.limit(nRead); bbuf.get(buf, pos, nRead); @@ -435,8 +436,8 @@ } // Do a simple read with a short timeout bbuf.clear(); - int nRead = Socket.recvbt - (socket, bbuf, 0, buf.length - lastValid, readTimeout); + int nRead = Socket.recvbbt + (socket, 0, buf.length - lastValid, readTimeout); if (nRead > 0) { bbuf.limit(nRead); bbuf.get(buf, pos, nRead); @@ -779,8 +780,8 @@ } bbuf.clear(); - nRead = Socket.recvb - (socket, bbuf, 0, buf.length - lastValid); + nRead = Socket.recvbb + (socket, 0, buf.length - lastValid); if (nRead > 0) { bbuf.limit(nRead); bbuf.get(buf, pos, nRead); @@ -799,8 +800,8 @@ pos = 0; lastValid = 0; bbuf.clear(); - nRead = Socket.recvb - (socket, bbuf, 0, buf.length); + nRead = Socket.recvbb + (socket, 0, buf.length); if (nRead > 0) { bbuf.limit(nRead); bbuf.get(buf, 0, nRead); Modified: tomcat/connectors/trunk/http11/src/java/org/apache/coyote/http11/InternalAprOutputBuffer.java URL: http://svn.apache.org/viewcvs/tomcat/connectors/trunk/http11/src/java/org/apache/coyote/http11/InternalAprOutputBuffer.java?rev=327657&r1=327656&r2=327657&view=diff ============================================================================== --- tomcat/connectors/trunk/http11/src/java/org/apache/coyote/http11/InternalAprOutputBuffer.java (original) +++ tomcat/connectors/trunk/http11/src/java/org/apache/coyote/http11/InternalAprOutputBuffer.java Sat Oct 22 05:32:32 2005 @@ -182,6 +182,7 @@ */ public void setSocket(long socket) { this.socket = socket; + Socket.setsbb(this.socket, bbuf); } @@ -694,7 +695,7 @@ protected void flushBuffer() throws IOException { if (bbuf.position() > 0) { - if (Socket.sendb(socket, bbuf, 0, bbuf.position()) < 0) { + if (Socket.sendbb(socket, 0, bbuf.position()) < 0) { throw new IOException(sm.getString("iib.failedwrite")); } bbuf.clear(); Modified: tomcat/connectors/trunk/jni/java/org/apache/tomcat/jni/Socket.java URL: http://svn.apache.org/viewcvs/tomcat/connectors/trunk/jni/java/org/apache/tomcat/jni/Socket.java?rev=327657&r1=327656&r2=327657&view=diff ============================================================================== --- tomcat/connectors/trunk/jni/java/org/apache/tomcat/jni/Socket.java (original) +++ tomcat/connectors/trunk/jni/java/org/apache/tomcat/jni/Socket.java Sat Oct 22 05:32:32 2005 @@ -240,6 +240,11 @@ */ public static native int sendb(long sock, ByteBuffer buf, int offset, int len); + /** + * Send data over a network using internally set ByteBuffer + */ + public static native int sendbb(long sock, + int offset, int len); /** * Send multiple packets of data over a network. @@ -338,7 +343,11 @@ */ public static native int recvb(long sock, ByteBuffer buf, int offset, int nbytes); - + /** + * Read data from a network using internally set ByteBuffer + */ + public static native int recvbb(long sock, + int offset, int nbytes); /** * Read data from a network with timeout. * @@ -362,6 +371,11 @@ */ public static native int recvbt(long sock, ByteBuffer buf, int offset, int nbytes, long timeout); + /** + * Read data from a network with timeout using internally set ByteBuffer + */ + public static native int recvbbt(long sock, + int offset, int nbytes, long timeout); /** * @param from The apr_sockaddr_t to fill in the recipient info @@ -467,6 +481,11 @@ byte[][] trailers, long offset, long len, int flags); + /** + * Send a file without header and trailer arrays. + */ + public static native long sendfilen(long sock, long file, long offset, + long len, int flags); /** * Create a child pool from associated socket pool. @@ -489,4 +508,21 @@ */ private static native long get(long socket, int what); + /** + * Set internal send ByteBuffer. + * This function will preset internal Java ByteBuffer for + * consecutive sendbb calls. + * @param thesocket The socket to use + * @param buf The ByteBuffer + */ + public static native void setsbb(long sock, ByteBuffer buf); + + /** + * Set internal receive ByteBuffer. + * This function will preset internal Java ByteBuffer for + * consecutive revcvbb/recvbbt calls. + * @param thesocket The socket to use + * @param buf The ByteBuffer + */ + public static native void setrbb(long sock, ByteBuffer buf); } Modified: tomcat/connectors/trunk/jni/native/include/tcn.h URL: http://svn.apache.org/viewcvs/tomcat/connectors/trunk/jni/native/include/tcn.h?rev=327657&r1=327656&r2=327657&view=diff ============================================================================== --- tomcat/connectors/trunk/jni/native/include/tcn.h (original) +++ tomcat/connectors/trunk/jni/native/include/tcn.h Sat Oct 22 05:32:32 2005 @@ -146,6 +146,8 @@ apr_pool_t *pool; apr_socket_t *sock; void *opaque; + char *jsbbuff; + char *jrbbuff; tcn_nlayer_t *net; } tcn_socket_t; Modified: tomcat/connectors/trunk/jni/native/src/network.c URL: http://svn.apache.org/viewcvs/tomcat/connectors/trunk/jni/native/src/network.c?rev=327657&r1=327656&r2=327657&view=diff ============================================================================== --- tomcat/connectors/trunk/jni/native/src/network.c (original) +++ tomcat/connectors/trunk/jni/native/src/network.c Sat Oct 22 05:32:32 2005 @@ -531,6 +531,32 @@ } } +TCN_IMPLEMENT_CALL(void, Socket, setsbb)(TCN_STDARGS, jlong sock, + jobject buf) +{ + tcn_socket_t *s = J2P(sock, tcn_socket_t *); + UNREFERENCED(o); + TCN_ASSERT(sock != 0); + TCN_ASSERT(s->opaque != NULL); + if (buf) + s->jsbbuff = (char *)(*e)->GetDirectBufferAddress(e, buf); + else + s->jsbbuff = NULL; +} + +TCN_IMPLEMENT_CALL(void, Socket, setrbb)(TCN_STDARGS, jlong sock, + jobject buf) +{ + tcn_socket_t *s = J2P(sock, tcn_socket_t *); + UNREFERENCED(o); + TCN_ASSERT(sock != 0); + TCN_ASSERT(s->opaque != NULL); + if (buf) + s->jrbbuff = (char *)(*e)->GetDirectBufferAddress(e, buf); + else + s->jrbbuff = NULL; +} + TCN_IMPLEMENT_CALL(jint, Socket, sendb)(TCN_STDARGS, jlong sock, jobject buf, jint offset, jint len) { @@ -561,6 +587,34 @@ } } +TCN_IMPLEMENT_CALL(jint, Socket, sendbb)(TCN_STDARGS, jlong sock, + jint offset, jint len) +{ + tcn_socket_t *s = J2P(sock, tcn_socket_t *); + apr_size_t nbytes = (apr_size_t)len; + apr_status_t ss; + + UNREFERENCED(o); + TCN_ASSERT(sock != 0); + TCN_ASSERT(s->opaque != NULL); + TCN_ASSERT(s->jsbbuff != NULL); +#ifdef TCN_DO_STATISTICS + sp_max_send = TCN_MAX(sp_max_send, nbytes); + sp_min_send = TCN_MIN(sp_min_send, nbytes); + sp_tot_send += nbytes; + sp_num_send++; +#endif + + ss = (*s->net->send)(s->opaque, s->jsbbuff + offset, &nbytes); + + if (ss == APR_SUCCESS) + return (jint)nbytes; + else { + TCN_ERROR_WRAP(ss); + return -(jint)ss; + } +} + TCN_IMPLEMENT_CALL(jint, Socket, sendv)(TCN_STDARGS, jlong sock, jobjectArray bufs) { @@ -793,6 +847,48 @@ } } +TCN_IMPLEMENT_CALL(jint, Socket, recvbb)(TCN_STDARGS, jlong sock, + jint offset, jint len) +{ + tcn_socket_t *s = J2P(sock, tcn_socket_t *); + apr_status_t ss; + apr_size_t nbytes = (apr_size_t)len; + + UNREFERENCED(o); + TCN_ASSERT(sock != 0); + TCN_ASSERT(s->opaque != NULL); + TCN_ASSERT(s->jrbbuff != NULL); + + ss = (*s->net->recv)(s->opaque, s->jrbbuff + offset, &nbytes); +#ifdef TCN_DO_STATISTICS + if (ss == APR_SUCCESS) { + sp_max_recv = TCN_MAX(sp_max_recv, nbytes); + sp_min_recv = TCN_MIN(sp_min_recv, nbytes); + sp_tot_recv += nbytes; + sp_num_recv++; + } + else { + if (APR_STATUS_IS_ETIMEDOUT(ss) || + APR_STATUS_IS_TIMEUP(ss)) + sp_tmo_recv++; + else if (APR_STATUS_IS_ECONNABORTED(ss) || + APR_STATUS_IS_ECONNRESET(ss) || + APR_STATUS_IS_EOF(ss)) + sp_rst_recv++; + else { + sp_err_recv++; + sp_erl_recv = ss; + } + } +#endif + if (ss == APR_SUCCESS) + return (jint)nbytes; + else { + TCN_ERROR_WRAP(ss); + return -(jint)ss; + } +} + TCN_IMPLEMENT_CALL(jint, Socket, recvbt)(TCN_STDARGS, jlong sock, jobject buf, jint offset, jint len, jlong timeout) @@ -847,6 +943,57 @@ } } +TCN_IMPLEMENT_CALL(jint, Socket, recvbbt)(TCN_STDARGS, jlong sock, + jint offset, + jint len, jlong timeout) +{ + tcn_socket_t *s = J2P(sock, tcn_socket_t *); + apr_status_t ss; + apr_size_t nbytes = (apr_size_t)len; + apr_interval_time_t t; + + UNREFERENCED(o); + TCN_ASSERT(sock != 0); + TCN_ASSERT(s->jrbbuff != NULL); + TCN_ASSERT(s->opaque != NULL); + + + if ((ss = (*s->net->timeout_get)(s->opaque, &t)) != APR_SUCCESS) + return -(jint)ss; + if ((ss = (*s->net->timeout_set)(s->opaque, J2T(timeout))) != APR_SUCCESS) + return -(jint)ss; + ss = (*s->net->recv)(s->opaque, s->jrbbuff + offset, &nbytes); + /* Resore the original timeout */ + (*s->net->timeout_set)(s->opaque, t); +#ifdef TCN_DO_STATISTICS + if (ss == APR_SUCCESS) { + sp_max_recv = TCN_MAX(sp_max_recv, nbytes); + sp_min_recv = TCN_MIN(sp_min_recv, nbytes); + sp_tot_recv += nbytes; + sp_num_recv++; + } + else { + if (APR_STATUS_IS_ETIMEDOUT(ss) || + APR_STATUS_IS_TIMEUP(ss)) + sp_tmo_recv++; + else if (APR_STATUS_IS_ECONNABORTED(ss) || + APR_STATUS_IS_ECONNRESET(ss) || + APR_STATUS_IS_EOF(ss)) + sp_rst_recv++; + else { + sp_err_recv++; + sp_erl_recv = ss; + } + } +#endif + if (ss == APR_SUCCESS) + return (jint)nbytes; + else { + TCN_ERROR_WRAP(ss); + return -(jint)ss; + } +} + TCN_IMPLEMENT_CALL(jint, Socket, recvfrom)(TCN_STDARGS, jlong from, jlong sock, jint flags, jbyteArray buf, jint offset, jint toread) @@ -1010,6 +1157,51 @@ for (i = 0; i < nt; i++) { (*e)->ReleaseByteArrayElements(e, tba[i], tvec[i].iov_base, JNI_ABORT); } + /* Return Number of bytes actually sent, + * including headers, file, and trailers + */ + if (ss == APR_SUCCESS) + return (jlong)written; + else { + TCN_ERROR_WRAP(ss); + return -(jlong)ss; + } +} + +TCN_IMPLEMENT_CALL(jlong, Socket, sendfilen)(TCN_STDARGS, jlong sock, + jlong file, + jlong offset, jlong len, + jint flags) +{ + tcn_socket_t *s = J2P(sock, tcn_socket_t *); + apr_file_t *f = J2P(file, apr_file_t *); + apr_off_t off = (apr_off_t)offset; + apr_size_t written = (apr_size_t)len; + apr_hdtr_t hdrs; + apr_status_t ss; + + UNREFERENCED(o); + TCN_ASSERT(sock != 0); + TCN_ASSERT(file != 0); + + if (s->net->type != TCN_SOCKET_APR) + return (jint)(-APR_ENOTIMPL); + + hdrs.headers = NULL; + hdrs.numheaders = 0; + hdrs.trailers = NULL; + hdrs.numtrailers = 0; + + + ss = apr_socket_sendfile(s->sock, f, &hdrs, &off, &written, (apr_int32_t)flags); + +#ifdef TCN_DO_STATISTICS + sf_max_send = TCN_MAX(sf_max_send, written); + sf_min_send = TCN_MIN(sf_min_send, written); + sf_tot_send += written; + sf_num_send++; +#endif + /* Return Number of bytes actually sent, * including headers, file, and trailers */ Modified: tomcat/connectors/trunk/util/java/org/apache/tomcat/util/net/AprEndpoint.java URL: http://svn.apache.org/viewcvs/tomcat/connectors/trunk/util/java/org/apache/tomcat/util/net/AprEndpoint.java?rev=327657&r1=327656&r2=327657&view=diff ============================================================================== --- tomcat/connectors/trunk/util/java/org/apache/tomcat/util/net/AprEndpoint.java (original) +++ tomcat/connectors/trunk/util/java/org/apache/tomcat/util/net/AprEndpoint.java Sat Oct 22 05:32:32 2005 @@ -1340,8 +1340,8 @@ // Set the socket to nonblocking mode Socket.timeoutSet(data.socket, 0); while (true) { - long nw = Socket.sendfile(data.socket, data.fd, null, null, - data.pos, data.end - data.pos, 0); + long nw = Socket.sendfilen(data.socket, data.fd, + data.pos, data.end - data.pos, 0); if (nw < 0) { if (!(-nw == Status.EAGAIN)) { Socket.destroy(data.socket); @@ -1453,9 +1453,9 @@ continue; } // Write some data using sendfile - long nw = Socket.sendfile(state.socket, state.fd, - null, null, state.pos, - state.end - state.pos, 0); + long nw = Socket.sendfilen(state.socket, state.fd, + state.pos, + state.end - state.pos, 0); if (nw < 0) { // Close socket and clear pool remove(state); --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]