// File: /workspace/github_cpp_makefile_star2_O3/CMU-SAFARI[P]MQSim/src/ssd/GC_and_WL_Unit_Page_Level.cpp
// Function: SSD_Components::GC_and_WL_Unit_Page_Level::Check_gc_required(unsigned int, NVM::FlashMemory::Physical_Page_Address const&)
void GC_and_WL_Unit_Page_Level::Check_gc_required(const unsigned int free_block_pool_size, const NVM::FlashMemory::Physical_Page_Address& plane_address)
	{
		if (free_block_pool_size < block_pool_gc_threshold) {
			flash_block_ID_type gc_candidate_block_id = block_manager->Get_coldest_block_id(plane_address);
			PlaneBookKeepingType* pbke = block_manager->Get_plane_bookkeeping_entry(plane_address);

			if (pbke->Ongoing_erase_operations.size() >= max_ongoing_gc_reqs_per_plane) {
				return;
			}

			switch (block_selection_policy) {
				case SSD_Components::GC_Block_Selection_Policy_Type::GREEDY://Find the set of blocks with maximum number of invalid pages and no free pages
				{
					gc_candidate_block_id = 0;
					if (pbke->Ongoing_erase_operations.find(0) != pbke->Ongoing_erase_operations.end()) {
						gc_candidate_block_id++;
					}
					for (flash_block_ID_type block_id = 1; block_id < block_no_per_plane; block_id++) {
						if (pbke->Blocks[block_id].Invalid_page_count > pbke->Blocks[gc_candidate_block_id].Invalid_page_count
							&& pbke->Blocks[block_id].Current_page_write_index == pages_no_per_block
							&& is_safe_gc_wl_candidate(pbke, block_id)) {
							gc_candidate_block_id = block_id;
						}
					}
					break;
				}
				case SSD_Components::GC_Block_Selection_Policy_Type::RGA:
				{
					std::set<flash_block_ID_type> random_set;
					while (random_set.size() < rga_set_size) {
						flash_block_ID_type block_id = random_generator.Uniform_uint(0, block_no_per_plane - 1);
						if (pbke->Ongoing_erase_operations.find(block_id) == pbke->Ongoing_erase_operations.end()
							&& is_safe_gc_wl_candidate(pbke, block_id)) {
							random_set.insert(block_id);
							}
					}
					gc_candidate_block_id = *random_set.begin();
					for(auto &block_id : random_set) {
						if (pbke->Blocks[block_id].Invalid_page_count > pbke->Blocks[gc_candidate_block_id].Invalid_page_count
							&& pbke->Blocks[block_id].Current_page_write_index == pages_no_per_block) {
							gc_candidate_block_id = block_id;
						}
					}
					break;
				}
				case SSD_Components::GC_Block_Selection_Policy_Type::RANDOM:
				{
					gc_candidate_block_id = random_generator.Uniform_uint(0, block_no_per_plane - 1);
					unsigned int repeat = 0;

					//A write frontier block should not be selected for garbage collection
					while (!is_safe_gc_wl_candidate(pbke, gc_candidate_block_id) && repeat++ < block_no_per_plane) {
						gc_candidate_block_id = random_generator.Uniform_uint(0, block_no_per_plane - 1);
					}
					break;
				}
				case SSD_Components::GC_Block_Selection_Policy_Type::RANDOM_P:
				{
					gc_candidate_block_id = random_generator.Uniform_uint(0, block_no_per_plane - 1);
					unsigned int repeat = 0;

					//A write frontier block or a block with free pages should not be selected for garbage collection
					while ((pbke->Blocks[gc_candidate_block_id].Current_page_write_index < pages_no_per_block || !is_safe_gc_wl_candidate(pbke, gc_candidate_block_id))
						&& repeat++ < block_no_per_plane) {
						gc_candidate_block_id = random_generator.Uniform_uint(0, block_no_per_plane - 1);
					}
					break;
				}
				case SSD_Components::GC_Block_Selection_Policy_Type::RANDOM_PP:
				{
					gc_candidate_block_id = random_generator.Uniform_uint(0, block_no_per_plane - 1);
					unsigned int repeat = 0;

					//The selected gc block should have a minimum number of invalid pages
					while ((pbke->Blocks[gc_candidate_block_id].Current_page_write_index < pages_no_per_block 
						|| pbke->Blocks[gc_candidate_block_id].Invalid_page_count < random_pp_threshold
						|| !is_safe_gc_wl_candidate(pbke, gc_candidate_block_id))
						&& repeat++ < block_no_per_plane) {
						gc_candidate_block_id = random_generator.Uniform_uint(0, block_no_per_plane - 1);
					}
					break;
				}
				case SSD_Components::GC_Block_Selection_Policy_Type::FIFO:
					gc_candidate_block_id = pbke->Block_usage_history.front();
					pbke->Block_usage_history.pop();
					break;
				default:
					break;
			}

			//This should never happen, but we check it here for safty
			if (pbke->Ongoing_erase_operations.find(gc_candidate_block_id) != pbke->Ongoing_erase_operations.end()) {
				return;
			}
			
			NVM::FlashMemory::Physical_Page_Address gc_candidate_address(plane_address);
			gc_candidate_address.BlockID = gc_candidate_block_id;
			Block_Pool_Slot_Type* block = &pbke->Blocks[gc_candidate_block_id];

			//No invalid page to erase
			if (block->Current_page_write_index == 0 || block->Invalid_page_count == 0) {
				return;
			}
			
			//Run the state machine to protect against race condition
			block_manager->GC_WL_started(gc_candidate_address);
			pbke->Ongoing_erase_operations.insert(gc_candidate_block_id);
			address_mapping_unit->Set_barrier_for_accessing_physical_block(gc_candidate_address);//Lock the block, so no user request can intervene while the GC is progressing
			
			//If there are ongoing requests targeting the candidate block, the gc execution should be postponed
			if (block_manager->Can_execute_gc_wl(gc_candidate_address)) {
				Stats::Total_gc_executions++;
				tsu->Prepare_for_transaction_submit();

				NVM_Transaction_Flash_ER* gc_erase_tr = new NVM_Transaction_Flash_ER(Transaction_Source_Type::GC_WL, pbke->Blocks[gc_candidate_block_id].Stream_id, gc_candidate_address);
				//If there are some valid pages in block, then prepare flash transactions for page movement
				if (block->Current_page_write_index - block->Invalid_page_count > 0) {
					NVM_Transaction_Flash_RD* gc_read = NULL;
					NVM_Transaction_Flash_WR* gc_write = NULL;
					for (flash_page_ID_type pageID = 0; pageID < block->Current_page_write_index; pageID++) {
						if (block_manager->Is_page_valid(block, pageID)) {
							Stats::Total_page_movements_for_gc++;
							gc_candidate_address.PageID = pageID;
							if (use_copyback) {
								gc_write = new NVM_Transaction_Flash_WR(Transaction_Source_Type::GC_WL, block->Stream_id, sector_no_per_page * SECTOR_SIZE_IN_BYTE,
									NO_LPA, address_mapping_unit->Convert_address_to_ppa(gc_candidate_address), NULL, 0, NULL, 0, INVALID_TIME_STAMP);
								gc_write->ExecutionMode = WriteExecutionModeType::COPYBACK;
								tsu->Submit_transaction(gc_write);
							} else {
								gc_read = new NVM_Transaction_Flash_RD(Transaction_Source_Type::GC_WL, block->Stream_id, sector_no_per_page * SECTOR_SIZE_IN_BYTE,
									NO_LPA, address_mapping_unit->Convert_address_to_ppa(gc_candidate_address), gc_candidate_address, NULL, 0, NULL, 0, INVALID_TIME_STAMP);
								gc_write = new NVM_Transaction_Flash_WR(Transaction_Source_Type::GC_WL, block->Stream_id, sector_no_per_page * SECTOR_SIZE_IN_BYTE,
									NO_LPA, NO_PPA, gc_candidate_address, NULL, 0, gc_read, 0, INVALID_TIME_STAMP);
								gc_write->ExecutionMode = WriteExecutionModeType::SIMPLE;
								gc_write->RelatedErase = gc_erase_tr;
								gc_read->RelatedWrite = gc_write;
								tsu->Submit_transaction(gc_read);//Only the read transaction would be submitted. The Write transaction is submitted when the read transaction is finished and the LPA of the target page is determined
							}
							gc_erase_tr->Page_movement_activities.push_back(gc_write);
						}
					}
				}
				block->Erase_transaction = gc_erase_tr;
				tsu->Submit_transaction(gc_erase_tr);

				tsu->Schedule();
			}
		}
	}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
cmpl %esi, 0x60(%rdi)
jbe 0x4046e
movq %rdx, %r15
movq %rdi, %rbx
movq 0x38(%rdi), %rdi
movq %rdx, %rsi
callq 0x3d678
movl %eax, %ebp
movl %eax, 0x34(%rsp)
movq 0x38(%rbx), %rdi
movq %r15, %rsi
callq 0x3d6de
movq %rax, %r14
movl 0x7c(%rbx), %eax
cmpq %rax, 0xd8(%r14)
jae 0x4046e
movl 0x2c(%rbx), %eax
cmpq $0x5, %rax
ja 0x40101
leaq 0x1a021(%rip), %rcx     # 0x59df4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x0, 0x34(%rsp)
movq 0xc0(%r14), %rax
xorl %ebp, %ebp
testq %rax, %rax
je 0x3fe1d
leaq 0xb8(%r14), %rcx
movq %rax, %rdx
movq 0x10(%rax), %rax
testq %rax, %rax
jne 0x3fdf9
cmpq %rcx, %rdx
je 0x3fe1d
cmpl $0x0, 0x20(%rdx)
jne 0x3fe1d
movl $0x1, 0x34(%rsp)
movl $0x1, %ebp
cmpl $0x2, 0x104(%rbx)
jb 0x40101
movl $0x1, %r12d
movl $0x4c, %r13d
movq 0x10(%r14), %rax
movl (%rax,%r13), %ecx
movl %ebp, %edx
shlq $0x6, %rdx
cmpl 0xc(%rax,%rdx), %ecx
jbe 0x3fe71
movl -0x8(%rax,%r13), %eax
cmpl 0x108(%rbx), %eax
jne 0x3fe71
movq %rbx, %rdi
movq %r14, %rsi
movl %r12d, %edx
callq 0x3f9ac
testb %al, %al
je 0x3fe71
movl %r12d, 0x34(%rsp)
movl %r12d, %ebp
incq %r12
movl 0x104(%rbx), %eax
addq $0x40, %r13
cmpq %rax, %r12
jb 0x3fe36
jmp 0x40101
leaq 0x88(%rbx), %r12
movl 0x104(%rbx), %edx
decl %edx
xorl %r13d, %r13d
movq %r12, %rdi
xorl %esi, %esi
callq 0x532cc
movl %eax, %ebp
movl %eax, 0x34(%rsp)
movq 0x10(%r14), %rax
movl %ebp, %ecx
shlq $0x6, %rcx
movl 0x4(%rax,%rcx), %edx
cmpl 0x108(%rbx), %edx
jb 0x3fee3
addq %rcx, %rax
movl 0xc(%rax), %eax
cmpl 0xf0(%rbx), %eax
jb 0x3fee3
movq %rbx, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x3f9ac
testb %al, %al
jne 0x40101
movl 0x104(%rbx), %edx
cmpl %edx, %r13d
jae 0x40101
incl %r13d
decl %edx
jmp 0x3fe9a
leaq 0x88(%rbx), %r12
movl 0x104(%rbx), %edx
decl %edx
xorl %r13d, %r13d
movq %r12, %rdi
xorl %esi, %esi
callq 0x532cc
movl %eax, %ebp
movq %rbx, %rdi
movq %r14, %rsi
movl %eax, %edx
callq 0x3f9ac
movl 0x104(%rbx), %edx
testl %edx, %edx
sete %cl
orb %al, %cl
jne 0x3ff60
decl %edx
movq %r12, %rdi
xorl %esi, %esi
callq 0x532cc
movl %eax, %ebp
movq %rbx, %rdi
movq %r14, %rsi
movl %eax, %edx
callq 0x3f9ac
testb %al, %al
jne 0x3ff60
incl %r13d
movl 0x104(%rbx), %edx
cmpl %edx, %r13d
jb 0x3ff33
movl %ebp, 0x34(%rsp)
jmp 0x40101
leaq 0x88(%rbx), %r12
movl 0x104(%rbx), %edx
decl %edx
xorl %r13d, %r13d
movq %r12, %rdi
xorl %esi, %esi
callq 0x532cc
movl %eax, %ebp
movl %eax, 0x34(%rsp)
movq 0x10(%r14), %rax
movl %ebp, %ecx
shlq $0x6, %rcx
movl 0x4(%rax,%rcx), %eax
cmpl 0x108(%rbx), %eax
jb 0x3ffb6
movq %rbx, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x3f9ac
testb %al, %al
jne 0x40101
movl 0x104(%rbx), %edx
cmpl %edx, %r13d
jae 0x40101
incl %r13d
decl %edx
jmp 0x3ff7b
leaq 0x58(%rsp), %rcx
movl $0x0, (%rcx)
xorl %eax, %eax
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
cmpl $0x0, 0x80(%rbx)
je 0x400ef
leaq 0x88(%rbx), %r12
leaq 0xb8(%r14), %rbp
leaq 0x50(%rsp), %r13
movl 0x104(%rbx), %edx
decl %edx
movq %r12, %rdi
xorl %esi, %esi
callq 0x532cc
movl %eax, 0x44(%rsp)
movq 0xc0(%r14), %rcx
testq %rcx, %rcx
je 0x4004f
movq %rbp, %rdx
xorl %esi, %esi
cmpl %eax, 0x20(%rcx)
setb %sil
cmovaeq %rcx, %rdx
movq 0x10(%rcx,%rsi,8), %rcx
testq %rcx, %rcx
jne 0x4002e
cmpq %rbp, %rdx
je 0x4004f
cmpl 0x20(%rdx), %eax
jae 0x4006d
movq %rbx, %rdi
movq %r14, %rsi
movl %eax, %edx
callq 0x3f9ac
testb %al, %al
je 0x4006d
movq %r13, %rdi
leaq 0x44(%rsp), %rsi
callq 0x30732
movl 0x80(%rbx), %eax
cmpq %rax, 0x78(%rsp)
jb 0x40009
movq 0x68(%rsp), %rax
movl 0x20(%rax), %ebp
movl %ebp, 0x34(%rsp)
leaq 0x58(%rsp), %rcx
cmpq %rcx, %rax
je 0x400f7
movq 0x10(%r14), %r12
movl 0x108(%rbx), %r13d
movl 0x20(%rax), %ecx
movq %rcx, %rdx
shlq $0x6, %rdx
movl 0xc(%r12,%rdx), %esi
movl %ebp, %edi
shlq $0x6, %rdi
cmpl 0xc(%r12,%rdi), %esi
jbe 0x400c6
addq %r12, %rdx
cmpl %r13d, 0x4(%rdx)
jne 0x400c6
movl %ecx, 0x34(%rsp)
movl %ecx, %ebp
movq %rax, %rdi
callq 0x7410
leaq 0x58(%rsp), %rcx
cmpq %rcx, %rax
jne 0x4009b
jmp 0x400f7
leaq 0x60(%r14), %rdi
movq 0x70(%r14), %rax
movl (%rax), %ebp
movl %ebp, 0x34(%rsp)
callq 0x404bc
jmp 0x40101
movl 0x78(%rsp), %ebp
movl %ebp, 0x34(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x304fe
movq 0xc0(%r14), %rax
testq %rax, %rax
je 0x4013c
leaq 0xb8(%r14), %rdx
movq %rdx, %rcx
xorl %esi, %esi
cmpl %ebp, 0x20(%rax)
setb %sil
cmovaeq %rax, %rcx
movq 0x10(%rax,%rsi,8), %rax
testq %rax, %rax
jne 0x40117
cmpq %rdx, %rcx
je 0x4013c
cmpl 0x20(%rcx), %ebp
jae 0x4046e
movups (%r15), %xmm0
movaps %xmm0, 0x50(%rsp)
movl 0x14(%r15), %eax
movl %eax, 0x64(%rsp)
movl %ebp, 0x60(%rsp)
movq 0x10(%r14), %r15
movl %ebp, %eax
shlq $0x6, %rax
cmpl $0x0, 0x4(%r15,%rax)
je 0x4046e
addq %rax, %r15
cmpl $0x0, 0xc(%r15)
je 0x4046e
movq %r14, %r12
addq $0xb0, %r12
movq 0x38(%rbx), %rdi
leaq 0x50(%rsp), %r13
movq %r13, %rsi
callq 0x3d770
leaq 0x34(%rsp), %rsi
movq %r12, %rdi
callq 0x30732
movq 0x30(%rbx), %rdi
movq (%rdi), %rax
movq %r13, %rsi
callq *0x98(%rax)
movq 0x38(%rbx), %rdi
movq %r13, %rsi
callq 0x3d736
testb %al, %al
je 0x4046e
leaq 0x30594(%rip), %rax     # 0x7075c
incl (%rax)
movq 0x40(%rbx), %rdi
callq 0x2f762
movl $0x98, %edi
callq 0x73a0
movq %rax, %r13
movq 0x10(%r14), %rax
movl 0x34(%rsp), %ecx
shlq $0x6, %rcx
movzwl 0x20(%rax,%rcx), %edx
leaq 0x50(%rsp), %rcx
movq %r13, %rdi
movl $0x2, %esi
callq 0x47fd4
movl 0x4(%r15), %eax
cmpl 0xc(%r15), %eax
sete %cl
testl %eax, %eax
sete %al
orb %cl, %al
jne 0x40438
movq %r13, %rax
subq $-0x80, %rax
movq %rax, 0x88(%rsp)
xorl %ebp, %ebp
movq %r15, 0x90(%rsp)
movq %r13, 0x48(%rsp)
movq 0x38(%rbx), %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x3d8e0
testb %al, %al
je 0x4042c
leaq 0x3090a(%rip), %rax     # 0x70b60
incl (%rax)
movl %ebp, 0x64(%rsp)
cmpb $0x1, 0x64(%rbx)
jne 0x402e1
movl $0xb0, %edi
callq 0x73a0
movq %rax, %r13
movzwl 0x20(%r15), %r14d
movl 0x10c(%rbx), %r12d
movq 0x30(%rbx), %rdi
movq (%rdi), %rax
leaq 0x50(%rsp), %rsi
callq *0x90(%rax)
shll $0x9, %r12d
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movups %xmm0, (%rsp)
movq $-0x1, 0x20(%rsp)
movzwl %r14w, %edx
movq %r13, %rdi
movl $0x2, %esi
movl %r12d, %ecx
movq $-0x1, %r8
movq %rax, %r9
callq 0x481c8
movl $0x1, 0xa8(%r13)
movq %r13, %r12
movq %r13, 0x38(%rsp)
movq 0x48(%rsp), %r13
jmp 0x403d2
movl $0xa0, %edi
callq 0x73a0
movq %rax, 0x38(%rsp)
movzwl 0x20(%r15), %r14d
movl 0x10c(%rbx), %r12d
movq 0x30(%rbx), %rdi
movq (%rdi), %rax
leaq 0x50(%rsp), %r13
movq %r13, %rsi
callq *0x90(%rax)
shll $0x9, %r12d
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rsp)
movups %xmm0, 0x8(%rsp)
movq %r13, (%rsp)
movq $-0x1, 0x28(%rsp)
movzwl %r14w, %edx
movq 0x38(%rsp), %rdi
movl $0x2, %esi
movl %r12d, %ecx
movq $-0x1, %r8
movq %rax, %r9
callq 0x4806e
movl $0xb0, %edi
callq 0x73a0
movq %rax, %r12
movzwl 0x20(%r15), %edx
movl 0x10c(%rbx), %ecx
shll $0x9, %ecx
movq 0x38(%rsp), %rax
movq %rax, 0x18(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rsp)
movq %r13, (%rsp)
movq $-0x1, 0x28(%rsp)
movq $0x0, 0x20(%rsp)
movq %r12, %rdi
movl $0x2, %esi
movq $-0x1, %r8
movq $-0x1, %r9
callq 0x4816c
movl $0x0, 0xa8(%r12)
movq 0x48(%rsp), %r13
movq %r13, 0x90(%r12)
movq 0x38(%rsp), %rax
movq %r12, 0x88(%rax)
movq 0x40(%rbx), %r15
leaq 0x78(%r15), %r14
movl $0x18, %edi
callq 0x73a0
movq 0x38(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq %rax, %rdi
movq %r14, %rsi
callq 0x7060
incq 0x88(%r15)
movl $0x18, %edi
callq 0x73a0
movq %r12, 0x10(%rax)
movq %rax, %rdi
movq 0x88(%rsp), %rsi
callq 0x7060
incq 0x90(%r13)
movq 0x90(%rsp), %r15
incl %ebp
cmpl 0x4(%r15), %ebp
jb 0x40239
movq %r13, 0x28(%r15)
movq 0x40(%rbx), %r15
leaq 0x78(%r15), %r14
movl $0x18, %edi
callq 0x73a0
movq %r13, 0x10(%rax)
movq %rax, %rdi
movq %r14, %rsi
callq 0x7060
incq 0x88(%r15)
movq 0x40(%rbx), %rdi
movq (%rdi), %rax
callq *0x30(%rax)
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %r13, %rdi
jmp 0x4049d
movq %rax, %rbx
movq %r12, %rdi
jmp 0x4049d
movq 0x38(%rsp), %rdi
jmp 0x4049a
movq %r13, %rdi
movq %rax, %rbx
callq 0x7370
jmp 0x404b3
jmp 0x404a6
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x304fe
movq %rbx, %rdi
callq 0x7620
nop