// File: /workspace/github_cpp_makefile_star2_O1/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 0x40b7d
movq %rdx, %r15
movq %rdi, %rbx
movq 0x38(%rdi), %rdi
movq %rdx, %rsi
callq 0x3e0d0
movl %eax, 0x34(%rsp)
movq 0x38(%rbx), %rdi
movq %r15, %rsi
callq 0x3e136
movq %rax, %r14
movl 0x7c(%rbx), %eax
cmpq %rax, 0xd8(%r14)
jae 0x40b7d
movl 0x2c(%rbx), %eax
cmpq $0x5, %rax
ja 0x40b2e
leaq 0x195fb(%rip), %rcx     # 0x59df4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x0, 0x34(%rsp)
movq 0xc0(%r14), %rdx
leaq 0xb8(%r14), %rax
movq %rax, %rcx
testq %rdx, %rdx
je 0x40a98
movq %rdx, %rcx
movq 0x10(%rdx), %rdx
jmp 0x4081b
leaq 0x88(%rbx), %r12
movl 0x104(%rbx), %edx
decl %edx
xorl %ebp, %ebp
movq %r12, %rdi
xorl %esi, %esi
callq 0x53c28
movl %eax, 0x34(%rsp)
movq 0x10(%r14), %rcx
movl %eax, %edx
shlq $0x6, %rdx
movl 0x4(%rcx,%rdx), %esi
cmpl 0x108(%rbx), %esi
jb 0x40885
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
cmpl 0xf0(%rbx), %ecx
jb 0x40885
movq %rbx, %rdi
movq %r14, %rsi
movl %eax, %edx
callq 0x403ac
testb %al, %al
jne 0x40b2e
movl 0x104(%rbx), %edx
cmpl %edx, %ebp
jae 0x40b2e
incl %ebp
decl %edx
jmp 0x4083e
leaq 0x88(%rbx), %r12
movl 0x104(%rbx), %edx
decl %edx
movq %r12, %rdi
xorl %esi, %esi
callq 0x53c28
movl %eax, 0x34(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
movl %eax, %edx
callq 0x403ac
testb %al, %al
jne 0x40b2e
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl 0x104(%rbx), %edx
incl %ebp
cmpl %edx, %ebp
jae 0x40b2e
decl %edx
movq %r12, %rdi
xorl %esi, %esi
callq 0x53c28
movl %eax, 0x34(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
movl %eax, %edx
callq 0x403ac
testb %al, %al
je 0x408d0
jmp 0x40b2e
leaq 0x88(%rbx), %r12
movl 0x104(%rbx), %edx
decl %edx
xorl %ebp, %ebp
movq %r12, %rdi
xorl %esi, %esi
callq 0x53c28
movl %eax, 0x34(%rsp)
movq 0x10(%r14), %rcx
movl %eax, %edx
shlq $0x6, %rdx
movl 0x4(%rcx,%rdx), %ecx
cmpl 0x108(%rbx), %ecx
jb 0x40950
movq %rbx, %rdi
movq %r14, %rsi
movl %eax, %edx
callq 0x403ac
testb %al, %al
jne 0x40b2e
movl 0x104(%rbx), %edx
cmpl %edx, %ebp
jae 0x40b2e
incl %ebp
decl %edx
jmp 0x40917
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 0x40a26
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 0x53c28
movl %eax, 0x44(%rsp)
movq 0xc0(%r14), %rdx
movq %rbp, %rcx
testq %rdx, %rdx
je 0x409e0
movq %rbp, %rcx
xorl %esi, %esi
cmpl %eax, 0x20(%rdx)
setb %sil
cmovaeq %rdx, %rcx
movq 0x10(%rdx,%rsi,8), %rdx
testq %rdx, %rdx
jne 0x409c9
movq %rbp, %rdx
cmpq %rbp, %rcx
je 0x409f2
cmpl 0x20(%rcx), %eax
cmovbq %rbp, %rcx
movq %rcx, %rdx
cmpq %rbp, %rdx
jne 0x40a15
movq %rbx, %rdi
movq %r14, %rsi
movl %eax, %edx
callq 0x403ac
testb %al, %al
je 0x40a15
movq %r13, %rdi
leaq 0x44(%rsp), %rsi
callq 0x30f50
movl 0x80(%rbx), %eax
cmpq %rax, 0x78(%rsp)
jb 0x409a1
movq 0x68(%rsp), %rax
movl 0x20(%rax), %ecx
movl %ecx, 0x34(%rsp)
leaq 0x58(%rsp), %rcx
cmpq %rcx, %rax
je 0x40a89
movl 0x34(%rsp), %r13d
movq 0x10(%r14), %r12
movl 0x108(%rbx), %ebp
movl 0x20(%rax), %ecx
movq %rcx, %rdx
shlq $0x6, %rdx
movl 0xc(%r12,%rdx), %esi
movl %r13d, %edi
shlq $0x6, %rdi
cmpl 0xc(%r12,%rdi), %esi
jbe 0x40a77
addq %r12, %rdx
cmpl %ebp, 0x4(%rdx)
jne 0x40a77
movl %ecx, 0x34(%rsp)
movl %ecx, %r13d
movq %rax, %rdi
callq 0x7410
leaq 0x58(%rsp), %rcx
cmpq %rcx, %rax
jne 0x40a4b
leaq 0x50(%rsp), %rdi
callq 0x30d22
jmp 0x40b2e
movq %rax, %rdx
cmpq %rax, %rcx
je 0x40aab
cmpl $0x0, 0x20(%rcx)
cmovneq %rax, %rcx
movq %rcx, %rdx
cmpq %rax, %rdx
je 0x40ab8
movl $0x1, 0x34(%rsp)
cmpl $0x2, 0x104(%rbx)
jb 0x40b2e
movl $0x1, %r12d
movl $0x4c, %r13d
movq 0x10(%r14), %rax
movl (%rax,%r13), %ecx
movl 0x34(%rsp), %edx
shlq $0x6, %rdx
cmpl 0xc(%rax,%rdx), %ecx
jbe 0x40b07
movl -0x8(%rax,%r13), %eax
cmpl 0x108(%rbx), %eax
jne 0x40b07
movq %rbx, %rdi
movq %r14, %rsi
movl %r12d, %edx
callq 0x403ac
testb %al, %al
je 0x40b07
movl %r12d, 0x34(%rsp)
incq %r12
movl 0x104(%rbx), %eax
addq $0x40, %r13
cmpq %rax, %r12
jb 0x40acd
jmp 0x40b2e
leaq 0x60(%r14), %rdi
movq 0x70(%r14), %rax
movl (%rax), %eax
movl %eax, 0x34(%rsp)
callq 0x40efe
movq 0xc0(%r14), %rdx
leaq 0xb8(%r14), %rax
movq %rax, %rcx
testq %rdx, %rdx
je 0x40b62
movl 0x34(%rsp), %esi
movq %rax, %rcx
xorl %edi, %edi
cmpl %esi, 0x20(%rdx)
setb %dil
cmovaeq %rdx, %rcx
movq 0x10(%rdx,%rdi,8), %rdx
testq %rdx, %rdx
jne 0x40b4b
movq %rax, %rdx
cmpq %rax, %rcx
je 0x40b78
movl 0x34(%rsp), %edx
cmpl 0x20(%rcx), %edx
cmovbq %rax, %rcx
movq %rcx, %rdx
cmpq %rax, %rdx
je 0x40b8f
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movups (%r15), %xmm0
movaps %xmm0, 0x50(%rsp)
movq 0x10(%r15), %rax
movq %rax, 0x60(%rsp)
movl 0x34(%rsp), %eax
movl %eax, 0x60(%rsp)
movq 0x10(%r14), %r15
shlq $0x6, %rax
cmpl $0x0, 0x4(%r15,%rax)
je 0x40b7d
addq %rax, %r15
cmpl $0x0, 0xc(%r15)
je 0x40b7d
movq %r14, %r12
addq $0xb0, %r12
movq 0x38(%rbx), %rdi
leaq 0x50(%rsp), %r13
movq %r13, %rsi
callq 0x3e1c8
leaq 0x34(%rsp), %rsi
movq %r12, %rdi
callq 0x30f50
movq 0x30(%rbx), %rdi
movq (%rdi), %rax
movq %r13, %rsi
callq *0x98(%rax)
movq 0x38(%rbx), %rdi
movq %r13, %rsi
callq 0x3e18e
testb %al, %al
je 0x40b7d
leaq 0x2fb46(%rip), %rax     # 0x7075c
incl (%rax)
movq 0x40(%rbx), %rdi
callq 0x2fd9e
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 0x48a30
movl 0x4(%r15), %eax
cmpl 0xc(%r15), %eax
je 0x40e87
cmpl $0x0, 0x4(%r15)
je 0x40e87
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 0x3e338
testb %al, %al
je 0x40e7b
leaq 0x2febb(%rip), %rax     # 0x70b60
incl (%rax)
movl %ebp, 0x64(%rsp)
cmpb $0x1, 0x64(%rbx)
jne 0x40d30
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 0x48c24
movl $0x1, 0xa8(%r13)
movq %r13, %r12
movq %r13, 0x38(%rsp)
movq 0x48(%rsp), %r13
jmp 0x40e21
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 0x48aca
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 0x48bc8
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 0x40c88
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)
jmp 0x40b7d
movq %rax, %rbx
movq %r13, %rdi
jmp 0x40edf
movq %rax, %rbx
movq %r12, %rdi
jmp 0x40edf
movq 0x38(%rsp), %rdi
jmp 0x40edc
movq %r13, %rdi
movq %rax, %rbx
callq 0x7370
jmp 0x40ef5
jmp 0x40ee8
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x30d22
movq %rbx, %rdi
callq 0x7620
nop