相关疑难解决方法(0)

记忆障碍和TLB

内存障碍保证数据缓存一致.但是,它是否保证TLB一致?

我看到一个问题,当在线程之间传递MappedByteBuffer时,JVM(java 7更新1)有时会因内存错误(SIGBUS,SIGSEG)而崩溃.

例如

final AtomicReference<MappedByteBuffer> mbbQueue = new AtomicReference<>();

// in a background thread.
MappedByteBuffer map = raf.map(MapMode.READ_WRITE, offset, allocationSize);
Thread.yield();
while (!inQueue.compareAndSet(null, map));


// the main thread. (more than 10x faster than using map() in the same thread)
MappedByteBuffer mbb = inQueue.getAndSet(null);
Run Code Online (Sandbox Code Playgroud)

没有Thread.yield()我偶尔会在force(),put()和C的memcpy()中崩溃,这些都表示我试图非法访问内存.使用Thread.yield()我没有遇到任何问题,但这听起来不是一个可靠的解决方案.

有人遇到过这个问题吗?有关TLB和内存障碍的保证吗?


编辑:操作系统是Centos 5.7,我已经看到了i7和双Xeon机器上的行为.

为什么我这样做?因为写入消息的平均时间是35-100 ns,具体取决于长度,使用普通的write()并不是那么快.如果我在当前线程中进行内存映射和清理,则需要50-130微秒,使用后台线程执行此操作需要大约3-5微秒的主线程交换缓冲区.为什么我需要交换缓冲区呢?因为我写的是很多GB数据而且ByteBuffer的大小不能超过2 GB.

java centos memory-mapped-files tlb memory-barriers

27
推荐指数
1
解决办法
1450
查看次数

如何打破 FileChannel#transferFrom 循环?

我正在为FileChannel编写一个实用程序类。

下面的方法看起来可能有效。

// tries to transfer as many bytes as specified
public static long transferTo(final FileChannel src, long position,
                              long count, final WritableByteChannel dst)
    throws IOException {

    long accumulated = 0L;

    while (position < src.size()) {
        final long transferred = src.transferTo(position, count, dst);
        position += transferred;
        count -= transferred;
        accumulated += transferred;
    }

    return accumulated;
}
Run Code Online (Sandbox Code Playgroud)

但是版本transferFrom有问题。

// tries to transfer as many bytes as specified
public static long transferFrom(final FileChannel dst,
                                final ReadableByteChannel src,
                                long …
Run Code Online (Sandbox Code Playgroud)

java nio filechannel

3
推荐指数
1
解决办法
386
查看次数