// File: dramninjasUMD[P]BOBSim/BOBWrapper.cpp
// Function: BOBSim::BOBWrapper::UpdateLatencyStats(BOBSim::Transaction*)
void BOBWrapper::UpdateLatencyStats(Transaction *returnedRead)
{
	if(returnedRead->transactionType==LOGIC_RESPONSE)
	{
		DEBUG("  == Got Logic Response : "<<*returnedRead);
		DEBUG("  == Logic All Done!");
		DEBUG("  == Took : "<<CPU_CLK_PERIOD*(currentClockCycle - returnedRead->fullStartTime)<<"ns");
		return;
	}
	else if(returnedRead->transactionType==RETURN_DATA)
	{
		returnedRead->fullTimeTotal = currentClockCycle - returnedRead->fullStartTime;
		returnedRead->cyclesRspPort = currentClockCycle - returnedRead->cyclesRspPort;

		unsigned chanID = returnedRead->mappedChannel;

		//add latencies to lists
		perChanFullLatencies[chanID].push_back(returnedRead->fullTimeTotal);
		perChanReqPort[chanID].push_back(returnedRead->cyclesReqPort);
		perChanRspPort[chanID].push_back(returnedRead->cyclesRspPort);
		perChanReqLink[chanID].push_back(returnedRead->cyclesReqLink);
		perChanRspLink[chanID].push_back(returnedRead->cyclesRspLink);
		perChanAccess[chanID].push_back(returnedRead->dramTimeTotal);
		perChanRRQ[chanID].push_back(returnedRead->cyclesInReadReturnQ);
		perChanWorkQTimes[chanID].push_back(returnedRead->cyclesInWorkQueue);
		/*
			DEBUGN("== Read Returned");
			DEBUGN(" full: "<< returnedRead->fullTimeTotal * CPU_CLK_PERIOD<<"ns");
			DEBUGN(" dram: "<< returnedRead->dramTimeTotal * CPU_CLK_PERIOD<<"ns");
			DEBUG(" chan: "<< returnedRead->channelTimeTotal * CPU_CLK_PERIOD<<"ns");
		*/
		fullLatencies.push_back(returnedRead->fullTimeTotal);
		fullSum+=returnedRead->fullTimeTotal;

		dramLatencies.push_back(returnedRead->dramTimeTotal);
		dramSum+=returnedRead->dramTimeTotal;

		chanLatencies.push_back(returnedRead->channelTimeTotal);
		chanSum+=returnedRead->channelTimeTotal;

		//latencies[((returnedRead->fullTimeTotal/10)*10)*CPU_CLK_PERIOD]++;

		returnedReads++;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x4, (%rax)
jne 0x14f56
leaq 0x23538(%rip), %rax     # 0x38374
cmpl $0x0, (%rax)
je 0x14e6f
movq 0x23180(%rip), %rdi     # 0x37fc8
leaq 0x1621a(%rip), %rsi     # 0x2b069
callq 0x31c0
movq %rax, %rdi
movq -0x10(%rbp), %rsi
callq 0x294b0
movq %rax, %rdi
movq 0x2313e(%rip), %rsi     # 0x37fa8
callq 0x3200
leaq 0x234fe(%rip), %rax     # 0x38374
cmpl $0x0, (%rax)
je 0x14e9d
movq 0x23146(%rip), %rdi     # 0x37fc8
leaq 0x161fb(%rip), %rsi     # 0x2b084
callq 0x31c0
movq %rax, %rdi
movq 0x23110(%rip), %rsi     # 0x37fa8
callq 0x3200
leaq 0x234d0(%rip), %rax     # 0x38374
cmpl $0x0, (%rax)
je 0x14f51
movq 0x23114(%rip), %rdi     # 0x37fc8
leaq 0x161de(%rip), %rsi     # 0x2b099
callq 0x31c0
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, -0x58(%rbp)
movss 0x23481(%rip), %xmm0    # 0x38354
movss %xmm0, -0x4c(%rbp)
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x60(%rcx), %rcx
subq %rcx, %rax
movq %rax, %rdx
shrq %rdx
movl %eax, %ecx
andl $0x1, %ecx
orq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
cvtsi2ss %rax, %xmm1
movss %xmm1, -0x48(%rbp)
testq %rax, %rax
movss %xmm0, -0x44(%rbp)
js 0x14f1c
movss -0x48(%rbp), %xmm0
movss %xmm0, -0x44(%rbp)
movq -0x58(%rbp), %rdi
movss -0x4c(%rbp), %xmm0
movss -0x44(%rbp), %xmm1
mulss %xmm1, %xmm0
callq 0x3140
movq %rax, %rdi
leaq 0x16282(%rip), %rsi     # 0x2b1bf
callq 0x31c0
movq %rax, %rdi
movq 0x2305c(%rip), %rsi     # 0x37fa8
callq 0x3200
jmp 0x151a4
movq -0x10(%rbp), %rax
cmpl $0x2, (%rax)
jne 0x151a2
movq -0x40(%rbp), %rdi
movq 0x8(%rdi), %rcx
movq -0x10(%rbp), %rax
subq 0x60(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x68(%rax)
movq 0x8(%rdi), %rcx
movq -0x10(%rbp), %rax
subq 0x40(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x14(%rbp)
addq $0x170, %rdi            # imm = 0x170
movl -0x14(%rbp), %eax
movl %eax, %esi
callq 0x17040
movq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
movl %eax, -0x18(%rbp)
leaq -0x18(%rbp), %rsi
callq 0x17060
movq -0x40(%rbp), %rdi
addq $0x188, %rdi            # imm = 0x188
movl -0x14(%rbp), %eax
movl %eax, %esi
callq 0x17040
movq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movl %eax, -0x1c(%rbp)
leaq -0x1c(%rbp), %rsi
callq 0x17060
movq -0x40(%rbp), %rdi
addq $0x1a0, %rdi            # imm = 0x1A0
movl -0x14(%rbp), %eax
movl %eax, %esi
callq 0x17040
movq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movl %eax, -0x20(%rbp)
leaq -0x20(%rbp), %rsi
callq 0x17060
movq -0x40(%rbp), %rdi
addq $0x1b8, %rdi            # imm = 0x1B8
movl -0x14(%rbp), %eax
movl %eax, %esi
callq 0x17040
movq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movl %eax, -0x24(%rbp)
leaq -0x24(%rbp), %rsi
callq 0x17060
movq -0x40(%rbp), %rdi
addq $0x1d0, %rdi            # imm = 0x1D0
movl -0x14(%rbp), %eax
movl %eax, %esi
callq 0x17040
movq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movl %eax, -0x28(%rbp)
leaq -0x28(%rbp), %rsi
callq 0x17060
movq -0x40(%rbp), %rdi
addq $0x1e8, %rdi            # imm = 0x1E8
movl -0x14(%rbp), %eax
movl %eax, %esi
callq 0x17040
movq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movl %eax, -0x2c(%rbp)
leaq -0x2c(%rbp), %rsi
callq 0x17060
movq -0x40(%rbp), %rdi
addq $0x200, %rdi            # imm = 0x200
movl -0x14(%rbp), %eax
movl %eax, %esi
callq 0x17040
movq %rax, %rdi
movq -0x10(%rbp), %rsi
addq $0x58, %rsi
callq 0x17090
movq -0x40(%rbp), %rdi
addq $0x218, %rdi            # imm = 0x218
movl -0x14(%rbp), %eax
movl %eax, %esi
callq 0x17040
movq %rax, %rdi
movq -0x10(%rbp), %rsi
addq $0x5c, %rsi
callq 0x17090
movq -0x40(%rbp), %rdi
addq $0x230, %rdi            # imm = 0x230
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
movl %eax, -0x30(%rbp)
leaq -0x30(%rbp), %rsi
callq 0x17060
movq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rcx
movl 0x248(%rdi), %eax
addq %rcx, %rax
movl %eax, 0x248(%rdi)
addq $0x250, %rdi            # imm = 0x250
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movl %eax, -0x34(%rbp)
leaq -0x34(%rbp), %rsi
callq 0x17060
movq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rcx
movl 0x268(%rdi), %eax
addq %rcx, %rax
movl %eax, 0x268(%rdi)
addq $0x270, %rdi            # imm = 0x270
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rax
movl %eax, -0x38(%rbp)
leaq -0x38(%rbp), %rsi
callq 0x17060
movq -0x40(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x88(%rcx), %rdx
movl 0x288(%rax), %ecx
addq %rdx, %rcx
movl %ecx, 0x288(%rax)
movl 0x298(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x298(%rax)
jmp 0x151a4
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)