通过 socketRead0 进行字符串 TLAB 分配

3

运行环境:

  1. Linux 4.x
  2. async-profiler 1.6 (https://github.com/jvm-profiling-tools/async-profiler)
  3. OpenJDK8

应用程序代码:

通过 SocketInputStream 进行域套接字通信

操作:

使用 async profiler 运行应用程序: -d 60 -e alloc -f /tmp/alloc.svg

问题:

从 SocketInputStream#socketRead0 意外分配了字符串

(青色: TLAB 分配)

enter image description here socketRead 和 socketRead0 的 JDK 代码

private int socketRead(FileDescriptor fd,
                       byte b[], int off, int len,
                       int timeout)
    throws IOException {
    return socketRead0(fd, b, off, len, timeout);
}

private native int socketRead0(FileDescriptor fd,
                               byte b[], int off, int len,
                               int timeout)

本地Socket实现:

  • jdk/src/solaris/native/java/net/SocketInputStream.c

假设:

在以下代码中,字符串可能是通过JNI在java堆中分配的,因为在字符串分配旁边的StackTrace中有一个SocketTimeoutException。

Java_java_net_SocketInputStream_socketRead0(JNIEnv *env, jobject this,
                                            jobject fdObj, jbyteArray data,
                                            jint off, jint len, jint timeout)
{
    [...]
    if (timeout) {
        nread = NET_ReadWithTimeout(env, fd, bufP, len, timeout);
        if ((*env)->ExceptionCheck(env)) {
            if (bufP != BUF) {
                free(bufP);
            }
            return nread;
        }
    } else {
        nread = NET_Read(fd, bufP, len);
    }
    [...]
}

static int NET_ReadWithTimeout(JNIEnv *env, int fd, char *bufP, int len, long timeout) {
    int result = 0;
    long prevtime = NET_GetCurrentTime(), newtime;
    while (timeout > 0) {
        result = NET_TimeoutWithCurrentTime(fd, timeout, prevtime);
        if (result <= 0) {
            if (result == 0) {
                JNU_ThrowByName(env, "java/net/SocketTimeoutException", "Read timed out");
            } else if (result == -1) {
                if (errno == EBADF) {
                    JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
                } else if (errno == ENOMEM) {
                    JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed");
                } else {
                    JNU_ThrowByNameWithMessageAndLastError
                            (env, "java/net/SocketException", "select/poll failed");
                }
            }
            return -1;
        }
        result = NET_NonBlockingRead(fd, bufP, len);
        if (result == -1 && ((errno == EAGAIN) || (errno == EWOULDBLOCK))) {
            newtime = NET_GetCurrentTime();
            timeout -= newtime - prevtime;
            if (timeout > 0) {
                prevtime = newtime;
            }
        } else {
            break;
        }
    }
    return result;
}

我查看了C代码,没有找到任何jString分配,因此我有点困惑。

有人知道字符串分配可能发生在哪里吗?

1个回答

4
你的假设是正确的。在文件中的SocketTimeoutException告诉我们,该方法分配了一个异常对象,并且这个异常对象包含需要分配的String消息。
我刚刚提交了一个更改到异步分析器,增加了--cstack选项,用于在分配分析模式下记录 C 栈和 Java 栈。这证明了java.lang.String确实是从 JNI 的 ThrowNew 函数中分配的。
链接到图片:async-profiler

我从主分支构建了async-profiler,事实上,我确实看到了与你在示例中描述的--cstack相同的行为。感谢您的贡献。 - Sergej Isbrecht

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接