提交 fba82c7c 编写于 作者: C chegar

8035897: Better memory allocation for file descriptors greater than 1024 on macosx

Reviewed-by: michaelm, alanb
上级 e13ecc4c
......@@ -509,7 +509,8 @@ Java_java_net_PlainDatagramSocketImpl_peek(JNIEnv *env, jobject this,
JNU_ThrowNullPointerException(env, "Null address in peek()");
}
if (timeout) {
int ret = NET_Timeout(fd, timeout);
int ret = NET_Timeout(env, fd, timeout);
JNU_CHECK_EXCEPTION_RETURN(env, -1);
if (ret == 0) {
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
"Peek timed out");
......@@ -611,7 +612,8 @@ Java_java_net_PlainDatagramSocketImpl_peekData(JNIEnv *env, jobject this,
packetBufferOffset = (*env)->GetIntField(env, packet, dp_offsetID);
packetBufferLen = (*env)->GetIntField(env, packet, dp_bufLengthID);
if (timeout) {
int ret = NET_Timeout(fd, timeout);
int ret = NET_Timeout(env, fd, timeout);
JNU_CHECK_EXCEPTION_RETURN(env, -1);
if (ret == 0) {
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
"Receive timed out");
......@@ -829,9 +831,11 @@ Java_java_net_PlainDatagramSocketImpl_receive0(JNIEnv *env, jobject this,
retry = JNI_FALSE;
if (timeout) {
int ret = NET_Timeout(fd, timeout);
int ret = NET_Timeout(env, fd, timeout);
if (ret <= 0) {
if (ret == 0) {
if ((*env)->ExceptionCheck(env)) {
// fall-through, to potentially free, then return
} else if (ret == 0) {
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
"Receive timed out");
} else if (ret == JVM_IO_ERR) {
......
......@@ -704,11 +704,11 @@ Java_java_net_PlainSocketImpl_socketAccept(JNIEnv *env, jobject this,
/* passing a timeout of 0 to poll will return immediately,
but in the case of ServerSocket 0 means infinite. */
if (timeout <= 0) {
ret = NET_Timeout(fd, -1);
ret = NET_Timeout(env, fd, -1);
} else {
ret = NET_Timeout(fd, timeout);
ret = NET_Timeout(env, fd, timeout);
}
JNU_CHECK_EXCEPTION(env);
if (ret == 0) {
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
"Accept timed out");
......
......@@ -100,9 +100,11 @@ Java_java_net_SocketInputStream_socketRead0(JNIEnv *env, jobject this,
}
if (timeout) {
nread = NET_Timeout(fd, timeout);
nread = NET_Timeout(env, fd, timeout);
if (nread <= 0) {
if (nread == 0) {
if ((*env)->ExceptionCheck(env)) {
// fall-through, to potentially free, then return
} else if (nread == 0) {
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
"Read timed out");
} else if (nread == JVM_IO_ERR) {
......
......@@ -25,6 +25,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/param.h>
#include <signal.h>
#include <pthread.h>
#include <sys/types.h>
......@@ -35,9 +36,10 @@
#include <sys/uio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/poll.h>
#include "jni_util.h"
/*
* Stack allocated by thread when doing blocking operation
*/
......@@ -344,9 +346,13 @@ int NET_Select(int s, fd_set *readfds, fd_set *writefds,
* Auto restarts with adjusted timeout if interrupted by
* signal other than our wakeup signal.
*/
int NET_Timeout(int s, long timeout) {
int NET_Timeout(JNIEnv *env, int s, long timeout) {
long prevtime = 0, newtime;
struct timeval t, *tp = &t;
fd_set fds;
fd_set* fdsp = NULL;
int allocated = 0;
threadEntry_t self;
fdEntry_t *fdEntry = getFdEntry(s);
/*
......@@ -376,20 +382,30 @@ int NET_Timeout(int s, long timeout) {
t.tv_usec = 0;
}
if (s < FD_SETSIZE) {
fdsp = &fds;
FD_ZERO(fdsp);
} else {
int length = (howmany(s+1, NFDBITS)) * sizeof(int);
fdsp = (fd_set *) calloc(1, length);
if (fdsp == NULL) {
JNU_ThrowOutOfMemoryError(env, "NET_Select native heap allocation failed");
return 0;
}
allocated = 1;
}
FD_SET(s, fdsp);
for(;;) {
fd_set rfds;
int rv;
threadEntry_t self;
/*
* call select on the fd. If interrupted by our wakeup signal
* errno will be set to EBADF.
*/
FD_ZERO(&rfds);
FD_SET(s, &rfds);
startOp(fdEntry, &self);
rv = select(s+1, &rfds, 0, 0, tp);
rv = select(s+1, fdsp, 0, 0, tp);
endOp(fdEntry, &self);
/*
......@@ -403,6 +419,8 @@ int NET_Timeout(int s, long timeout) {
newtime = now.tv_sec * 1000 + now.tv_usec / 1000;
timeout -= newtime - prevtime;
if (timeout <= 0) {
if (allocated != 0)
free(fdsp);
return 0;
}
prevtime = newtime;
......@@ -410,6 +428,8 @@ int NET_Timeout(int s, long timeout) {
t.tv_usec = (timeout % 1000) * 1000;
}
} else {
if (allocated != 0)
free(fdsp);
return rv;
}
......
......@@ -34,9 +34,10 @@
#include <sys/uio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/poll.h>
#include "jni.h"
/*
* Stack allocated by thread when doing blocking operation
*/
......@@ -325,7 +326,7 @@ int NET_Select(int s, fd_set *readfds, fd_set *writefds,
* Auto restarts with adjusted timeout if interrupted by
* signal other than our wakeup signal.
*/
int NET_Timeout(int s, long timeout) {
int NET_Timeout(JNIEnv *unused, int s, long timeout) {
long prevtime = 0, newtime;
struct timeval t;
fdEntry_t *fdEntry = getFdEntry(s);
......
/*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8035897
* @summary FD_SETSIZE should be set on macosx
* @run main/othervm AnotherSelectFdsLimit 1023
* @run main/othervm AnotherSelectFdsLimit 1024
* @run main/othervm AnotherSelectFdsLimit 1025
* @run main/othervm AnotherSelectFdsLimit 1600
*/
import java.io.IOException;
import java.net.ServerSocket;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
public class AnotherSelectFdsLimit {
static final int DEFAULT_FDS_TO_USE = 1600;
public static void main(String [] args) throws Exception {
if (!System.getProperty("os.name").contains("OS X")) {
System.out.println("Test only run on MAC. Exiting.");
return;
}
int fdsToUse = DEFAULT_FDS_TO_USE;
if (args.length == 1)
fdsToUse = Integer.parseInt(args[0]);
System.out.println("Using " + fdsToUse + " fds.");
List<Thread> threads = new ArrayList<>();
for (int i=0; i<fdsToUse; i++)
threads.add(new WorkerThread());
for (Thread t : threads)
t.start();
for (Thread t : threads)
t.join();
}
static class WorkerThread extends Thread {
public void run() {
try (ServerSocket ss = new ServerSocket(0)) {
ss.setSoTimeout(2000);
ss.accept();
} catch (SocketTimeoutException x) {
// expected
} catch (IOException x) {
throw new java.io.UncheckedIOException(x);
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册