WiscSort / pmem_benchmark / memcpy_tests / testM.cc
testM.cc
Raw
#include "record.h"
#include "config.h"
#include "timer.h"
#include "utils.h"

#include "unistd.h"
#include "fcntl.h"
#include <sys/stat.h>
#include <assert.h>
#include <sys/mman.h>
#include <sys/types.h>

#ifdef pmdk
#include <libpmem.h>
#endif

#ifdef avx512
#ifdef __cplusplus
#include <stdint.h>
extern "C"
{
void *__memmove_chk_avx512_no_vzeroupper(void *dest, void *src, size_t s);
}
#endif
#endif


#define GIB(x) (x * 1024 * 1024 * 1024ull)
#define GB(x) (x * 1000 * 1000 * 1000ull)

int main(int argc, char** argv) {

    //char* dummy = (char*)malloc();
    char* read_buffer = (char *)malloc(GB(10));
    memset(read_buffer, '\0', GB(10));
//    char* read_buffer = NULL;
//    posix_memalign((void**)&read_buffer, 2*1024*1024, GIB(10));
    // first read

    char *input_mapped_buffer_ = NULL;
    int fd = open(argv[1], O_RDWR | O_DIRECT);
    if (fd < 0)
    {
        printf("Couldn't open file %s: %s\n", argv[1], strerror(errno));
        exit(1);
    }
    struct stat st;
    stat(argv[1], &st);
    off_t file_size = st.st_size;
    input_mapped_buffer_ = (char *)mmap(NULL, file_size, PROT_READ | PROT_WRITE,
                                        MAP_SHARED, fd, 0);
    if (input_mapped_buffer_ == MAP_FAILED)
    {
        printf("Failed to mmap read file of size %lu: %s\n",
               file_size, strerror(errno));
        return -1;
    }

    size_t read_buff_count = (16*1024*1024)/BLOCK_SIZE;
    size_t buff_ptr = 0;
    size_t read_blks = 0;

    Timer timer;
    timer.start("read");
    for(int i =0; i < 625; i++) {
        while (read_blks < read_buff_count)
        {

#ifdef avx512
            __memmove_chk_avx512_no_vzeroupper((void *)(read_buffer + buff_ptr),
                                               &input_mapped_buffer_[buff_ptr],
                                               BLOCK_SIZE);
#elif pmdk
//         pmem_memcpy_nodrain((void *)(read_buffer + buff_ptr),
//                                           &input_mapped_buffer_[buff_ptr],
//                                           BLOCK_SIZE)ndwdith('5');
    sleep(5);
            pmem_memcpy_nodrain(
                &input_mapped_buffer_[buff_ptr],(void *)(read_buffer + buff_ptr),
                BLOCK_SIZE);
#else
        memcpy((void *)(read_buffer + buff_ptr),
                                           &input_mapped_buffer_[buff_ptr],BLOCK_SIZE);

#endif

#ifdef clflush
            // flush and fence to ensure write is complete
            flush_clflushopt(&input_mapped_buffer_[buff_ptr], BLOCK_SIZE);
//            _mm_sfence();
#endif
            buff_ptr += BLOCK_SIZE;
            read_blks++;
//	printf("read blks %lu buff_ptr %lu\n", read_blks, buff_ptr);
        }
        buff_ptr = 0;
        read_blks = 0;
    }
    timer.end("read");

    // forcing optimizer to do the job
    for(size_t i =0; i < 500; i++) {
        size_t v1 = rand() % 10000000000;
        read_buffer[v1] += 'k';
        //read_buffer[i] = 0xFF;
        printf("%c",read_buffer[i]);
    }
 
    sleep(5);
    buff_ptr = 0;
    read_blks = 0;
    timer.start("write");
    for(int i =0; i < 625; i++) {
        while (read_blks < read_buff_count)
        {

#ifdef avx512
            __memmove_chk_avx512_no_vzeroupper((void *)(read_buffer + buff_ptr),
                                               &input_mapped_buffer_[buff_ptr],
                                               BLOCK_SIZE);
#elif pmdk
//         pmem_memcpy_nodrain((void *)(read_buffer + buff_ptr),
//                                           &input_mapped_buffer_[buff_ptr],
//                                           BLOCK_SIZE);
            pmem_memcpy_nodrain(
                &input_mapped_buffer_[buff_ptr],(void *)(read_buffer + buff_ptr),
                BLOCK_SIZE);
#else
// NOTE: only memcpy is writing, the above are still reading here. 
            memcpy(&input_mapped_buffer_[buff_ptr],(void *) (read_buffer + buff_ptr),
                   BLOCK_SIZE);

#endif

#ifdef clflush
            // flush and fence to ensure write is complete
            flush_clflushopt(&input_mapped_buffer_[buff_ptr], BLOCK_SIZE);
            //_mm_sfence();
#endif
            buff_ptr += BLOCK_SIZE;
            read_blks++;
//	printf("read blks %lu buff_ptr %lu\n", read_blks, buff_ptr);
        }
        buff_ptr = 0;
        read_blks = 0;
    }
    timer.end("write");

    printf("Read time: %f\n", timer.get_time("read"));
    printf("Write time: %f\n", timer.get_time("write"));
}