// File: CMU-SAFARI[P]MQSim/src/ssd/Stats.cpp
// Function: SSD_Components::Stats::Init_stats(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)
void Stats::Init_stats(unsigned int channel_no, unsigned int chip_no_per_channel, unsigned int die_no_per_chip, unsigned int plane_no_per_die, 
		unsigned int block_no_per_plane, unsigned int page_no_per_block, unsigned int max_allowed_block_erase_count)
	{
		Block_erase_histogram = new unsigned int ****[channel_no];
		for (unsigned int channel_cntr = 0; channel_cntr < channel_no; channel_cntr++) {
			Block_erase_histogram[channel_cntr] = new unsigned int***[chip_no_per_channel];
			for (unsigned int chip_cntr = 0; chip_cntr < chip_no_per_channel; chip_cntr++) {
				Block_erase_histogram[channel_cntr][chip_cntr] = new unsigned int**[die_no_per_chip];
				for (unsigned int die_cntr = 0; die_cntr < die_no_per_chip; die_cntr++) {
					Block_erase_histogram[channel_cntr][chip_cntr][die_cntr] = new unsigned int*[plane_no_per_die];
					for (unsigned int plane_cntr = 0; plane_cntr < plane_no_per_die; plane_cntr++) {
						Block_erase_histogram[channel_cntr][chip_cntr][die_cntr][plane_cntr] = new unsigned int[max_allowed_block_erase_count];
						Block_erase_histogram[channel_cntr][chip_cntr][die_cntr][plane_cntr][0] = block_no_per_plane * page_no_per_block; //At the start of the simulation all pages have zero erase count
						for (unsigned int i = 1; i < max_allowed_block_erase_count; ++i) {
							Block_erase_histogram[channel_cntr][chip_cntr][die_cntr][plane_cntr][i] = 0;
						}
					}
				}
			}
		}

		IssuedReadCMD = 0; IssuedCopybackReadCMD = 0; IssuedInterleaveReadCMD = 0; IssuedMultiplaneReadCMD = 0; IssuedMultiplaneCopybackReadCMD = 0;
		IssuedProgramCMD = 0; IssuedInterleaveProgramCMD = 0; IssuedMultiplaneProgramCMD = 0; IssuedMultiplaneCopybackProgramCMD = 0; IssuedInterleaveMultiplaneProgramCMD = 0; IssuedSuspendProgramCMD = 0; IssuedCopybackProgramCMD = 0;
		IssuedEraseCMD = 0; IssuedInterleaveEraseCMD = 0; IssuedMultiplaneEraseCMD = 0; IssuedInterleaveMultiplaneEraseCMD = 0;
		IssuedSuspendEraseCMD = 0;
		Total_flash_reads_for_mapping = 0; Total_flash_writes_for_mapping = 0; 
		CMT_hits = 0; readTR_CMT_hits = 0; writeTR_CMT_hits = 0;
		CMT_miss = 0; readTR_CMT_miss = 0; writeTR_CMT_miss = 0;
		total_CMT_queries = 0; total_readTR_CMT_queries = 0; total_writeTR_CMT_queries = 0;

		Total_gc_executions = 0;  Total_page_movements_for_gc = 0;
		Total_wl_executions = 0;  Total_page_movements_for_wl = 0;

		for (stream_id_type stream_id = 0; stream_id < MAX_SUPPORT_STREAMS; stream_id++) {
			Total_flash_reads_for_mapping_per_stream[stream_id] = 0;
			Total_flash_writes_for_mapping_per_stream[stream_id] = 0;
			CMT_hits_per_stream[stream_id] = 0; readTR_CMT_hits_per_stream[stream_id] = 0; writeTR_CMT_hits_per_stream[stream_id] = 0;
			CMT_miss_per_stream[stream_id] = 0; readTR_CMT_miss_per_stream[stream_id] = 0;  writeTR_CMT_miss_per_stream[stream_id] = 0;
			total_CMT_queries_per_stream[stream_id] = 0; total_readTR_CMT_queries_per_stream[stream_id] = 0; total_writeTR_CMT_queries_per_stream[stream_id] = 0;
			Total_gc_executions_per_stream[stream_id] = 0;
			Total_gc_page_movements_per_stream[stream_id] = 0;
			Total_wl_executions_per_stream[stream_id] = 0;
			Total_wl_page_movements_per_stream[stream_id] = 0;
		}
	}

	void Stats::Clear_stats(unsigned int channel_no, unsigned int chip_no_per_channel, unsigned int die_no_per_chip, unsigned int plane_no_per_die,
		unsigned int block_no_per_plane, unsigned int page_no_per_block, unsigned int max_allowed_block_erase_count)
	{
		for (unsigned int channel_cntr = 0; channel_cntr < channel_no; channel_cntr++) {
			for (unsigned int chip_cntr = 0; chip_cntr < chip_no_per_channel; chip_cntr++) {
				for (unsigned int die_cntr = 0; die_cntr < die_no_per_chip; die_cntr++) {
					for (unsigned int plane_cntr = 0; plane_cntr < plane_no_per_die; plane_cntr++) {
						delete[] Block_erase_histogram[channel_cntr][chip_cntr][die_cntr][plane_cntr];
					}
					delete[] Block_erase_histogram[channel_cntr][chip_cntr][die_cntr];
				}
				delete[] Block_erase_histogram[channel_cntr][chip_cntr];
			}
			delete[] Block_erase_histogram[channel_cntr];
		}
		delete[] Block_erase_histogram;
	}
}

// Assembly:
subq $0x38, %rsp
movl 0x40(%rsp), %eax
movl %edi, 0x34(%rsp)
movl %esi, 0x30(%rsp)
movl %edx, 0x2c(%rsp)
movl %ecx, 0x28(%rsp)
movl %r8d, 0x24(%rsp)
movl %r9d, 0x20(%rsp)
movl 0x34(%rsp), %eax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x8030
movq %rax, 0x40ff2(%rip)     # 0xdb7f0
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
cmpl 0x34(%rsp), %eax
jae 0x9aa2d
movl 0x30(%rsp), %eax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x8030
movq %rax, %rdx
movq 0x40fad(%rip), %rax     # 0xdb7f0
movl 0x1c(%rsp), %ecx
movq %rdx, (%rax,%rcx,8)
movl $0x0, 0x18(%rsp)
movl 0x18(%rsp), %eax
cmpl 0x30(%rsp), %eax
jae 0x9aa1b
movl 0x2c(%rsp), %eax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x8030
movq %rax, %rdx
movq 0x40f60(%rip), %rax     # 0xdb7f0
movl 0x1c(%rsp), %ecx
movq (%rax,%rcx,8), %rax
movl 0x18(%rsp), %ecx
movq %rdx, (%rax,%rcx,8)
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
cmpl 0x2c(%rsp), %eax
jae 0x9aa09
movl 0x28(%rsp), %eax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x8030
movq %rax, %rdx
movq 0x40f0b(%rip), %rax     # 0xdb7f0
movl 0x1c(%rsp), %ecx
movq (%rax,%rcx,8), %rax
movl 0x18(%rsp), %ecx
movq (%rax,%rcx,8), %rax
movl 0x14(%rsp), %ecx
movq %rdx, (%rax,%rcx,8)
movl $0x0, 0x10(%rsp)
movl 0x10(%rsp), %eax
cmpl 0x28(%rsp), %eax
jae 0x9a9f7
movl 0x40(%rsp), %eax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x8030
movq %rax, %rdx
movq 0x40eae(%rip), %rax     # 0xdb7f0
movl 0x1c(%rsp), %ecx
movq (%rax,%rcx,8), %rax
movl 0x18(%rsp), %ecx
movq (%rax,%rcx,8), %rax
movl 0x14(%rsp), %ecx
movq (%rax,%rcx,8), %rax
movl 0x10(%rsp), %ecx
movq %rdx, (%rax,%rcx,8)
movl 0x24(%rsp), %ecx
imull 0x20(%rsp), %ecx
movq 0x40e7e(%rip), %rax     # 0xdb7f0
movl 0x1c(%rsp), %edx
movq (%rax,%rdx,8), %rax
movl 0x18(%rsp), %edx
movq (%rax,%rdx,8), %rax
movl 0x14(%rsp), %edx
movq (%rax,%rdx,8), %rax
movl 0x10(%rsp), %edx
movq (%rax,%rdx,8), %rax
movl %ecx, (%rax)
movl $0x1, 0xc(%rsp)
movl 0xc(%rsp), %eax
cmpl 0x40(%rsp), %eax
jae 0x9a9e5
movq 0x40e43(%rip), %rax     # 0xdb7f0
movl 0x1c(%rsp), %ecx
movq (%rax,%rcx,8), %rax
movl 0x18(%rsp), %ecx
movq (%rax,%rcx,8), %rax
movl 0x14(%rsp), %ecx
movq (%rax,%rcx,8), %rax
movl 0x10(%rsp), %ecx
movq (%rax,%rcx,8), %rax
movl 0xc(%rsp), %ecx
movl $0x0, (%rax,%rcx,4)
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x9a99c
jmp 0x9a9e7
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x9a905
jmp 0x9a9f9
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x9a8a8
jmp 0x9aa0b
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x9a853
jmp 0x9aa1d
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x9a806
movq $0x0, 0x3fd20(%rip) # 0xda758
movq $0x0, 0x3fd1d(%rip) # 0xda760
movq $0x0, 0x3fd1a(%rip) # 0xda768
movq $0x0, 0x3fd17(%rip) # 0xda770
movq $0x0, 0x3fd14(%rip) # 0xda778
movq $0x0, 0x3fd11(%rip) # 0xda780
movq $0x0, 0x3fd0e(%rip) # 0xda788
movq $0x0, 0x3fd0b(%rip) # 0xda790
movq $0x0, 0x3fd08(%rip) # 0xda798
movq $0x0, 0x3fd05(%rip) # 0xda7a0
movq $0x0, 0x3fd02(%rip) # 0xda7a8
movq $0x0, 0x3fcff(%rip) # 0xda7b0
movq $0x0, 0x3fcfc(%rip) # 0xda7b8
movq $0x0, 0x3fcf9(%rip) # 0xda7c0
movq $0x0, 0x3fcf6(%rip) # 0xda7c8
movq $0x0, 0x3fcf3(%rip) # 0xda7d0
movq $0x0, 0x3fcf0(%rip) # 0xda7d8
movq $0x0, 0x3fced(%rip) # 0xda7e0
movq $0x0, 0x3fcea(%rip) # 0xda7e8
movl $0x0, 0x40cf0(%rip)     # 0xdb7f8
movl $0x0, 0x40cea(%rip)     # 0xdb7fc
movl $0x0, 0x40ce4(%rip)     # 0xdb800
movl $0x0, 0x40cde(%rip)     # 0xdb804
movl $0x0, 0x40cd8(%rip)     # 0xdb808
movl $0x0, 0x40cd2(%rip)     # 0xdb80c
movl $0x0, 0x40ccc(%rip)     # 0xdb810
movl $0x0, 0x40cc6(%rip)     # 0xdb814
movl $0x0, 0x40cc0(%rip)     # 0xdb818
movl $0x0, 0x40cba(%rip)     # 0xdb81c
movl $0x0, 0x410b4(%rip)     # 0xdbc20
movl $0x0, 0x414ba(%rip)     # 0xdc030
movl $0x0, 0x418c0(%rip)     # 0xdc440
movw $0x0, 0xa(%rsp)
movzwl 0xa(%rsp), %eax
cmpl $0x100, %eax            # imm = 0x100
jge 0x9ace7
movzwl 0xa(%rsp), %eax
movl %eax, %ecx
leaq 0x3fc4b(%rip), %rax     # 0xda7f0
movq $0x0, (%rax,%rcx,8)
movzwl 0xa(%rsp), %eax
movl %eax, %ecx
leaq 0x40435(%rip), %rax     # 0xdaff0
movq $0x0, (%rax,%rcx,8)
movzwl 0xa(%rsp), %eax
movl %eax, %ecx
leaq 0x41c7f(%rip), %rax     # 0xdc850
movl $0x0, (%rax,%rcx,4)
movzwl 0xa(%rsp), %eax
movl %eax, %ecx
leaq 0x4206a(%rip), %rax     # 0xdcc50
movl $0x0, (%rax,%rcx,4)
movzwl 0xa(%rsp), %eax
movl %eax, %ecx
leaq 0x42455(%rip), %rax     # 0xdd050
movl $0x0, (%rax,%rcx,4)
movzwl 0xa(%rsp), %eax
movl %eax, %ecx
leaq 0x42840(%rip), %rax     # 0xdd450
movl $0x0, (%rax,%rcx,4)
movzwl 0xa(%rsp), %eax
movl %eax, %ecx
leaq 0x42c2b(%rip), %rax     # 0xdd850
movl $0x0, (%rax,%rcx,4)
movzwl 0xa(%rsp), %eax
movl %eax, %ecx
leaq 0x43016(%rip), %rax     # 0xddc50
movl $0x0, (%rax,%rcx,4)
movzwl 0xa(%rsp), %eax
movl %eax, %ecx
leaq 0x43401(%rip), %rax     # 0xde050
movl $0x0, (%rax,%rcx,4)
movzwl 0xa(%rsp), %eax
movl %eax, %ecx
leaq 0x437ec(%rip), %rax     # 0xde450
movl $0x0, (%rax,%rcx,4)
movzwl 0xa(%rsp), %eax
movl %eax, %ecx
leaq 0x43bd7(%rip), %rax     # 0xde850
movl $0x0, (%rax,%rcx,4)
movzwl 0xa(%rsp), %eax
movl %eax, %ecx
leaq 0x40b92(%rip), %rax     # 0xdb820
movl $0x0, (%rax,%rcx,4)
movzwl 0xa(%rsp), %eax
movl %eax, %ecx
leaq 0x40f8d(%rip), %rax     # 0xdbc30
movl $0x0, (%rax,%rcx,4)
movzwl 0xa(%rsp), %eax
movl %eax, %ecx
leaq 0x41388(%rip), %rax     # 0xdc040
movl $0x0, (%rax,%rcx,4)
movzwl 0xa(%rsp), %eax
movl %eax, %ecx
leaq 0x41783(%rip), %rax     # 0xdc450
movl $0x0, (%rax,%rcx,4)
movw 0xa(%rsp), %ax
addw $0x1, %ax
movw %ax, 0xa(%rsp)
jmp 0x9ab87
addq $0x38, %rsp
retq
nopl (%rax)