c – ReadProcessMemory比SharedMemory上的memcpy更快

我正在尝试通过使用共享内存进行通信来改进我的多进程应用程序.我正在做一些简单测试的分析,出现了一些奇怪的东西.当我试图复制存储在SharedMemory中的数据时,使用ReadProcessMemory比使用Memcopy更快.

我知道我不应该这样使用SharedMemory(最好直接在共享内存中读取),但我仍然想知道为什么会发生这种情况.通过进一步调查,另一件事出现了:如果我在相同的共享内存区域(实际上是同一区域)上连续执行2次memcpy,则第二个副本比第一个快两倍.

以下是显示问题的示例代码.在这个例子中,只有一个进程,但问题在于此处.从共享内存区域执行memcpy比在我自己的进程上执行相同区域的ReadProcessMemory要慢!

#include <tchar.h>
#include <basetsd.h>
#include <iostream>

#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/windows_shared_memory.hpp>
#include <time.h>
namespace bip = boost::interprocess;
#include <boost/asio.hpp>

 bip::windows_shared_memory* AllocateSharedMemory(UINT32 a_UI32_Size)
{
    bip::windows_shared_memory* l_pShm = new bip::windows_shared_memory (bip::create_only, "Global\\testSharedMemory", bip::read_write, a_UI32_Size);
    bip::mapped_region l_region(*l_pShm, bip::read_write);
    std::memset(l_region.get_address(), 1, l_region.get_size());
    return l_pShm;
}

//Copy the shared memory with memcpy
void CopySharedMemory(UINT32 a_UI32_Size)
{
    bip::windows_shared_memory m_shm(bip::open_only, "Global\\testSharedMemory", bip::read_only);
    bip::mapped_region l_region(m_shm, bip::read_only);
    void* l_pData = malloc(a_UI32_Size);
    memcpy(l_pData, l_region.get_address(), a_UI32_Size);
    free(l_pData);
}

//Copy the shared memory with ReadProcessMemory
void ProcessCopySharedMemory(UINT32 a_UI32_Size)
{
    bip::windows_shared_memory m_shm(bip::open_only, "Global\\testSharedMemory", bip::read_only);
    bip::mapped_region l_region(m_shm, bip::read_only);
    void* l_pData = malloc(a_UI32_Size);
    HANDLE hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE,(DWORD) GetCurrentProcessId());
    size_t l_szt_CurRemote_Readsize;
    ReadProcessMemory(hProcess,
                      (LPCVOID)((void*)l_region.get_address()),
                      l_pData,
                      a_UI32_Size,
                      (SIZE_T*)&l_szt_CurRemote_Readsize);
    free(l_pData);
}

// do 2 memcpy on the same shared memory
void CopySharedMemory2(UINT32 a_UI32_Size)
{
    bip::windows_shared_memory m_shm(bip::open_only, "Global\\testSharedMemory", bip::read_only);
    bip::mapped_region l_region(m_shm, bip::read_only);
    clock_t begin = clock();
    void* l_pData = malloc(a_UI32_Size);
    memcpy(l_pData, l_region.get_address(), a_UI32_Size);
    clock_t end = clock();
    std::cout << "FirstCopy: " << (end - begin) * 1000 / CLOCKS_PER_SEC << " ms" << std::endl; 
    free(l_pData);

    begin = clock();
    l_pData = malloc(a_UI32_Size);
    memcpy(l_pData, l_region.get_address(), a_UI32_Size);
    end = clock();
    std::cout << "SecondCopy: " << (end - begin) * 1000 / CLOCKS_PER_SEC << " ms" << std::endl; 
    free(l_pData);
}

int _tmain(int argc, _TCHAR* argv[])
{
    UINT32 l_UI32_Size = 1048576000;
    bip::windows_shared_memory* l_pShm = AllocateSharedMemory(l_UI32_Size);
    clock_t begin = clock();
    for (int i=0; i<10 ; i++)
        CopySharedMemory(l_UI32_Size);
    clock_t end = clock();
    std::cout << "MemCopy: " << (end - begin) * 1000 / CLOCKS_PER_SEC << " ms" << std::endl; 
    begin = clock();
    for (int i=0; i<10 ; i++)
        ProcessCopySharedMemory(l_UI32_Size);
    end = clock();
    std::cout << "ReadProcessMemory: " << (end - begin) * 1000 / CLOCKS_PER_SEC << " ms" << std::endl; 

    for (int i=0; i<10 ; i++)
        CopySharedMemory2(l_UI32_Size);

    delete l_pShm;
    return 0;
}

这是输出:

MemCopy: 8891 ms
ReadProcessMemory: 6068 ms

FirstCopy: 796 ms
SecondCopy: 327 ms
FirstCopy: 795 ms
SecondCopy: 328 ms
FirstCopy: 780 ms
SecondCopy: 344 ms
FirstCopy: 780 ms
SecondCopy: 343 ms
FirstCopy: 780 ms
SecondCopy: 327 ms
FirstCopy: 795 ms
SecondCopy: 343 ms
FirstCopy: 780 ms
SecondCopy: 344 ms
FirstCopy: 796 ms
SecondCopy: 343 ms
FirstCopy: 796 ms
SecondCopy: 327 ms
FirstCopy: 780 ms
SecondCopy: 328 ms

如果有人知道为什么memcpy如此缓慢以及是否有解决这个问题的解决方案,我全都听见了.

谢谢.

最佳答案 我的评论作为答案供参考.

在大块内存中使用“memcpy”需要操作系统为每个复制的新页面筛选其进程/内存表.反过来,使用’ReadProcessMemory’告诉操作系统哪些页面应从哪个进程复制到哪个进程.

当您使用单页进行基准测试时,这种差异消失了,确认了其中的一部分.

我可以猜测’memcpy’在’小’场景中更快的原因可能是’ReadProcessMemory’有一个从用户到内核模式的额外切换.另一方面,Memcpy将任务卸载到底层内存管理系统,该系统始终与您的进程并行运行,并且在某种程度上由硬件原生支持.

点赞