// File: /workspace/github_cpp_makefile_star2_O2/chahk0129[P]hash_bench/./bench/benchmark.hpp
// Function: benchmark_t<unsigned long>::ycsb_exec(int, int, Pair<unsigned long>*, int, Pair<unsigned long>*, int, int, int*, bool)
inline void benchmark_t<Key_t>::ycsb_exec(int workload_type, int index_type, Pair<Key_t>* init_kv, int init_num, Pair<Key_t>* run_kv, int run_num, int num_threads, int* ops, bool pcm_enabled){
    /*
    if(memory_bandwidth){
	if(geteuid() != 0){
	    fprintf(stderr, "Must be root user to measure memory bandwidth\n");
	    exit(0);
	}
	PCM_memory::InitMemoryMonitor();
    }

    if(numa){
	if(geteuid() != 0){
	    fprintf(stderr, "Must be root user to measure NUMA operations\n");
	    exit(0);
	}
	PCM_NUMA::InitNumaMonitor();
    }*/

    Hash<Key_t>* hashtable = getInstance<Key_t>(index_type);
    //init_num = 100;
    //run_num = 100;

    auto load_func = [&hashtable, &init_kv, init_num, num_threads](uint64_t thread_id, bool){
	size_t total_num = init_num;
	size_t chunk_size = total_num / num_threads;
	size_t from = chunk_size * thread_id;
	size_t to = chunk_size * (thread_id+1);

	for(size_t i=from; i<to; i++){
	    hashtable->Insert(init_kv[i].key, init_kv[i].value);
	}
	return;
    };

    /*
    if(memory_bandwidth)
	PCM_memory::StartMemoryMonitor();

    if(numa)
	PCM_NUMA::StartNUMAMonitor();
	*/
    std::unique_ptr<SystemCounterState> before;
    if(pcm_enabled){
	before = std::make_unique<SystemCounterState>();
	*before = getSystemCounterState();
    }
    clear_cache();
    double start_time = get_now();
    start_threads(hashtable, num_threads, load_func, false);
    double end_time = get_now();

    std::unique_ptr<SystemCounterState> after;
    if(pcm_enabled){
	after = std::make_unique<SystemCounterState>();
	*after = getSystemCounterState();
    }
    /*
    if(memory_bandwidth)
	PCM_memory::EndMemoryMonitor();

    if(numa)
	PCM_NUMA::EndNUMAMonitor();
	*/
    double throughput = init_num / (end_time - start_time) / 1000000; // MOps/sec
    std::cout << "\033[1;32m";
    std::cout << "Insert " << throughput << "\033[0m" << std::endl;

    if(pcm_enabled){
	std::cout << "PCM Metrics:\n"
	    	  << "\tL3 misses: " << getL3CacheMisses(*before, *after) << "\n"
		  << "\tReads(bytes): " << getBytesReadFromMC(*before, *after) << "\n"
		  << "\tWrites(byes): " << getBytesWrittenToMC(*before, *after) << std::endl;
    }

    auto exec_func = [&hashtable, &run_kv, run_num, &ops, num_threads](uint64_t thread_id, bool){
	size_t total_num = run_num;
	size_t chunk_size = total_num / num_threads;
	size_t from = chunk_size * thread_id;
	size_t to = chunk_size * (thread_id+1);

	for(size_t i=from; i<to; i++){
	    if(ops[i] == OP_INSERT){
		hashtable->Insert(run_kv[i].key, run_kv[i].value);
	    }
	    else if(ops[i] == OP_READ){
		auto ret = hashtable->Get(run_kv[i].key);
		if((uint64_t)ret == 0)
		    std::cout << "not found" << std::endl;
	    }
	    else if(ops[i] == OP_UPDATE){
		auto ret = hashtable->Update(run_kv[i].key, run_kv[i].value);
	    }
	}
	return;
    };

    /*
    if(memory_bandwidth)
	PCM_memory::StartMemoryMonitor();

    if(numa)
	PCM_NUMA::StartNUMAMonitor();
    */
    if(pcm_enabled){
	*before = getSystemCounterState();
    }

    clear_cache();
    start_time = get_now();
    start_threads(hashtable, num_threads, exec_func, false);
    end_time = get_now();

    if(pcm_enabled){
	*after = getSystemCounterState();
    }

    /*
    if(memory_bandwidth)
	PCM_memory::EndMemoryMonitor();

    if(numa)
	PCM_NUMA::EndNUMAMonitor();
    */
    throughput = run_num / (end_time - start_time) / 1000000; // MOps/sec
    std::cout << "\033[1;31m";
    if(workload_type == WORKLOAD_A)
	std::cout << "Read/Update " << throughput << "\033[0m" << std::endl;
    else if(workload_type == WORKLOAD_B)
	std::cout << "Read/Update " << throughput << "\033[0m" << std::endl;
    else if(workload_type == WORKLOAD_C)
	std::cout << "Read " << throughput << "\033[0m" << std::endl;
    else if(workload_type == WORKLOAD_D)
	std::cout << "Read/Update " << throughput << "\033[0m" << std::endl;

    if(pcm_enabled){
	std::cout << "PCM Metrics:\n"
	    	  << "\tL3 misses: " << getL3CacheMisses(*before, *after) << "\n"
		  << "\tReads(bytes): " << getBytesReadFromMC(*before, *after) << "\n"
		  << "\tWrites(byes): " << getBytesWrittenToMC(*before, *after) << std::endl;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movl %r8d, %r12d
movl %edx, %edi
movl %esi, %r14d
movl 0x268(%rsp), %ebp
leaq 0x70(%rsp), %rbx
movq %rcx, (%rbx)
movq %r9, 0x28(%rsp)
callq 0xacf1
leaq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movq %rcx, 0x58(%rsp)
movq %rbx, 0x60(%rsp)
movl %r12d, 0x68(%rsp)
movl %ebp, 0x6c(%rsp)
cmpb $0x0, 0x278(%rsp)
movl %r14d, 0x24(%rsp)
je 0xa41a
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
callq 0xada3
movq (%rbx), %rbx
leaq 0x78(%rsp), %rdi
callq 0x20a30
leaq 0x78(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0xade6
movq %r14, %rdi
callq 0xae64
jmp 0xa41c
xorl %ebx, %ebx
callq 0xaea8
movsd %xmm0, 0x18(%rsp)
movq 0x8(%rsp), %rdi
movslq %ebp, %r15
leaq 0x78(%rsp), %rcx
movb $0x0, (%rcx)
leaq 0x58(%rsp), %rdx
movq %r15, %rsi
callq 0xaed7
callq 0xaea8
movsd %xmm0, 0x10(%rsp)
cmpb $0x0, 0x278(%rsp)
je 0xa48a
leaq 0x78(%rsp), %rdi
callq 0xada3
leaq 0x78(%rsp), %rdi
movq (%rdi), %r14
callq 0x20a30
leaq 0x78(%rsp), %r13
movq %r14, %rdi
movq %r13, %rsi
callq 0xade6
movq %r13, %rdi
callq 0xae64
jmp 0xa48d
xorl %r14d, %r14d
movq 0x2fb24(%rip), %rdi     # 0x39fb8
leaq 0x20307(%rip), %rsi     # 0x2a7a2
callq 0x53f0
movq 0x2fb11(%rip), %rdi     # 0x39fb8
leaq 0x202fc(%rip), %rsi     # 0x2a7aa
callq 0x53f0
cvtsi2sd %r12d, %xmm0
movsd 0x10(%rsp), %xmm1
subsd 0x18(%rsp), %xmm1
divsd %xmm1, %xmm0
divsd 0x1fd30(%rip), %xmm0    # 0x2a200
movq %rax, %rdi
callq 0x5680
leaq 0x202d3(%rip), %rsi     # 0x2a7b2
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
callq 0x51e0
movl 0x260(%rsp), %r13d
cmpb $0x0, 0x278(%rsp)
je 0xa606
movq 0x2faac(%rip), %rdi     # 0x39fb8
leaq 0x202a4(%rip), %rsi     # 0x2a7b7
callq 0x53f0
leaq 0x202a6(%rip), %rsi     # 0x2a7c5
movq %rax, %rdi
callq 0x53f0
movq %rax, %r12
movq %rbx, %rdi
movq %r14, %rsi
callq 0xafb4
movq %r12, %rdi
movq %rax, %rsi
callq 0x52b0
leaq 0x2027c(%rip), %rsi     # 0x2a7c3
movq %rax, %rdi
callq 0x53f0
leaq 0x2027c(%rip), %rsi     # 0x2a7d2
movq %rax, %rdi
callq 0x53f0
movq 0xd8(%r14), %rsi
subq 0xd8(%rbx), %rsi
shlq $0x6, %rsi
movq %rax, %rdi
callq 0x52b0
leaq 0x20244(%rip), %rsi     # 0x2a7c3
movq %rax, %rdi
callq 0x53f0
leaq 0x20254(%rip), %rsi     # 0x2a7e2
movq %rax, %rdi
callq 0x53f0
movq 0xd0(%r14), %rsi
subq 0xd0(%rbx), %rsi
shlq $0x6, %rsi
movq %rax, %rdi
callq 0x52b0
movq %rax, %rdi
callq 0x51e0
leaq 0x8(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x28(%rsp), %rax
movq %rax, 0x38(%rsp)
movl %r13d, 0x40(%rsp)
leaq 0x270(%rsp), %rax
movq %rax, 0x48(%rsp)
movl %ebp, 0x50(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x20a30
leaq 0x78(%rsp), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0xade6
movq %r12, %rdi
callq 0xae64
jmp 0xa630
leaq 0x8(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x28(%rsp), %rax
movq %rax, 0x38(%rsp)
movl %r13d, 0x40(%rsp)
leaq 0x270(%rsp), %rax
movq %rax, 0x48(%rsp)
movl %ebp, 0x50(%rsp)
callq 0xaea8
movsd %xmm0, 0x18(%rsp)
movq 0x8(%rsp), %rdi
leaq 0x78(%rsp), %rcx
movb $0x0, (%rcx)
leaq 0x30(%rsp), %rdx
movq %r15, %rsi
callq 0xafe2
callq 0xaea8
movsd %xmm0, 0x10(%rsp)
cmpb $0x0, 0x278(%rsp)
movl 0x24(%rsp), %r15d
je 0xa697
movl %r15d, %r12d
leaq 0x78(%rsp), %rdi
callq 0x20a30
leaq 0x78(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0xade6
movq %r15, %rdi
callq 0xae64
movl %r12d, %r15d
movq 0x2f91a(%rip), %rdi     # 0x39fb8
leaq 0x2014d(%rip), %rsi     # 0x2a7f2
callq 0x53f0
cmpl $0x3, %r15d
ja 0xa709
movl %r15d, %eax
leaq 0x1fb26(%rip), %rcx     # 0x2a1e0
movslq (%rcx,%rax,4), %rsi
addq %rcx, %rsi
movq 0x2f8f0(%rip), %rdi     # 0x39fb8
callq 0x53f0
cvtsi2sd %r13d, %xmm0
movsd 0x10(%rsp), %xmm1
subsd 0x18(%rsp), %xmm1
divsd %xmm1, %xmm0
divsd 0x1fb16(%rip), %xmm0    # 0x2a200
movq %rax, %rdi
callq 0x5680
leaq 0x200b9(%rip), %rsi     # 0x2a7b2
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
callq 0x51e0
cmpb $0x0, 0x278(%rsp)
je 0xa7cc
movq 0x2f89a(%rip), %rdi     # 0x39fb8
leaq 0x20092(%rip), %rsi     # 0x2a7b7
callq 0x53f0
leaq 0x20094(%rip), %rsi     # 0x2a7c5
movq %rax, %rdi
callq 0x53f0
movq %rax, %r15
movq %rbx, %rdi
movq %r14, %rsi
callq 0xafb4
movq %r15, %rdi
movq %rax, %rsi
callq 0x52b0
leaq 0x2006a(%rip), %rsi     # 0x2a7c3
movq %rax, %rdi
callq 0x53f0
leaq 0x2006a(%rip), %rsi     # 0x2a7d2
movq %rax, %rdi
callq 0x53f0
movq 0xd8(%r14), %rsi
subq 0xd8(%rbx), %rsi
shlq $0x6, %rsi
movq %rax, %rdi
callq 0x52b0
leaq 0x20032(%rip), %rsi     # 0x2a7c3
movq %rax, %rdi
callq 0x53f0
leaq 0x20042(%rip), %rsi     # 0x2a7e2
movq %rax, %rdi
callq 0x53f0
movq 0xd0(%r14), %rsi
subq 0xd0(%rbx), %rsi
shlq $0x6, %rsi
movq %rax, %rdi
callq 0x52b0
movq %rax, %rdi
callq 0x51e0
jmp 0xa7d1
testq %r14, %r14
je 0xa7da
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
testq %rbx, %rbx
je 0xa7e8
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xa80d
jmp 0xa80d
jmp 0xa80d
jmp 0xa806
jmp 0xa806
jmp 0xa80d
movq %rax, %r15
jmp 0xa81e
jmp 0xa80d
movq %rax, %r15
testq %r14, %r14
je 0xa81e
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
testq %rbx, %rbx
je 0xa82c
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
movq %r15, %rdi
callq 0x5740