// File: /workspace/github_cpp_makefile_star2_O2/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 $0x88, %rsp
cmpl %esi, 0x60(%rdi)
jbe 0x33fc4
movq %rdx, %r15
movq %rdi, %rbx
movq 0x38(%rdi), %rdi
movq %rdx, %rsi
callq 0x31a2e
movl %eax, 0x34(%rsp)
movq 0x38(%rbx), %rdi
movq %r15, %rsi
callq 0x31a90
movq %rax, %r14
movl 0x7c(%rbx), %eax
cmpq %rax, 0xd8(%r14)
jae 0x33fc4
leaq 0xb0(%r14), %rax
movq %rax, 0x40(%rsp)
movl 0x2c(%rbx), %eax
cmpq $0x5, %rax
ja 0x33fa3
leaq 0x13f71(%rip), %rcx     # 0x47cc4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
andl $0x0, 0x34(%rsp)
leaq 0x50(%rsp), %rsi
andl $0x0, (%rsi)
movq 0x40(%rsp), %rdi
callq 0x28510
leaq 0xb8(%r14), %rcx
cmpq %rcx, %rax
je 0x340b8
movl $0x1, 0x34(%rsp)
pushq $0x1
popq %rbp
jmp 0x340ba
leaq 0x88(%rbx), %r13
movl 0x104(%rbx), %edx
decl %edx
xorl %ebp, %ebp
movq %r13, %rdi
xorl %esi, %esi
callq 0x4121a
movl %eax, 0x34(%rsp)
movq 0x10(%r14), %rcx
movl %eax, %edx
shlq $0x6, %rdx
movl 0x4(%rcx,%rdx), %esi
cmpl 0x108(%rbx), %esi
jb 0x33deb
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
cmpl 0xf0(%rbx), %ecx
jb 0x33deb
movq %rbx, %rdi
movq %r14, %rsi
movl %eax, %edx
callq 0x337b0
testb %al, %al
jne 0x33fa3
movl 0x104(%rbx), %edx
cmpl %edx, %ebp
jae 0x33fa3
incl %ebp
decl %edx
jmp 0x33da4
leaq 0x88(%rbx), %r13
movl 0x104(%rbx), %edx
pushq $-0x1
popq %r12
decl %edx
movq %r13, %rdi
xorl %esi, %esi
callq 0x4121a
movl %eax, %ebp
movq %rbx, %rdi
movq %r14, %rsi
movl %eax, %edx
callq 0x337b0
testb %al, %al
jne 0x33e3d
movl 0x104(%rbx), %edx
incl %r12d
cmpl %edx, %r12d
jb 0x33e10
movl %ebp, 0x34(%rsp)
jmp 0x33fa3
leaq 0x88(%rbx), %r13
movl 0x104(%rbx), %edx
decl %edx
xorl %ebp, %ebp
movq %r13, %rdi
xorl %esi, %esi
callq 0x4121a
movl %eax, 0x34(%rsp)
movq 0x10(%r14), %rcx
movl %eax, %edx
shlq $0x6, %rdx
movl 0x4(%rcx,%rdx), %ecx
cmpl 0x108(%rbx), %ecx
jb 0x33e90
movq %rbx, %rdi
movq %r14, %rsi
movl %eax, %edx
callq 0x337b0
testb %al, %al
jne 0x33fa3
movl 0x104(%rbx), %edx
cmpl %edx, %ebp
jae 0x33fa3
incl %ebp
decl %edx
jmp 0x33e57
leaq 0x58(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x88(%rbx), %r13
leaq 0xb8(%r14), %r12
xorl %eax, %eax
leaq 0x38(%rsp), %rbp
movl 0x80(%rbx), %ecx
cmpq %rcx, %rax
jae 0x33f42
movl 0x104(%rbx), %edx
decl %edx
movq %r13, %rdi
xorl %esi, %esi
callq 0x4121a
movl %eax, 0x38(%rsp)
movq 0x40(%rsp), %rdi
movq %rbp, %rsi
callq 0x28510
cmpq %r12, %rax
jne 0x33f26
movl 0x38(%rsp), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x337b0
testb %al, %al
je 0x33f26
leaq 0x50(%rsp), %rdi
movq %rbp, %rsi
callq 0x27600
movq 0x78(%rsp), %rax
jmp 0x33ed3
leaq 0x60(%r14), %rdi
movq 0x70(%r14), %rax
movl (%rax), %eax
movl %eax, 0x34(%rsp)
callq 0x34320
jmp 0x33fa3
movq 0x68(%rsp), %rax
movl 0x20(%rax), %r12d
movl %r12d, 0x34(%rsp)
movl 0x108(%rbx), %ebp
leaq 0x58(%rsp), %r13
cmpq %r13, %rax
je 0x33f99
movq 0x10(%r14), %rdx
movl 0x20(%rax), %ecx
movq %rcx, %rsi
shlq $0x6, %rsi
movl 0xc(%rdx,%rsi), %edi
movl %r12d, %r8d
shlq $0x6, %r8
cmpl 0xc(%rdx,%r8), %edi
jbe 0x33f8f
addq %rsi, %rdx
cmpl %ebp, 0x4(%rdx)
jne 0x33f8f
movl %ecx, 0x34(%rsp)
movl %ecx, %r12d
movq %rax, %rdi
callq 0x73b0
jmp 0x33f5b
leaq 0x50(%rsp), %rdi
callq 0x27180
leaq 0x34(%rsp), %rsi
movq 0x40(%rsp), %r12
movq %r12, %rdi
callq 0x28510
movq %r14, %rcx
addq $0xb8, %rcx
cmpq %rcx, %rax
je 0x33fd6
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movups (%r15), %xmm0
movaps %xmm0, 0x50(%rsp)
movl 0x14(%r15), %eax
movl %eax, 0x64(%rsp)
movl 0x34(%rsp), %eax
movl %eax, 0x60(%rsp)
movq 0x10(%r14), %r15
shlq $0x6, %rax
cmpl $0x0, 0x4(%r15,%rax)
je 0x33fc4
addq %rax, %r15
cmpl $0x0, 0xc(%r15)
je 0x33fc4
movq 0x38(%rbx), %rdi
leaq 0x50(%rsp), %r13
movq %r13, %rsi
callq 0x31b22
leaq 0x34(%rsp), %rsi
movq %r12, %rdi
callq 0x27600
movq 0x30(%rbx), %rdi
movq (%rdi), %rax
movq %r13, %rsi
callq *0x98(%rax)
movq 0x38(%rbx), %rdi
movq %r13, %rsi
callq 0x31ae8
testb %al, %al
je 0x33fc4
leaq 0x2f6da(%rip), %rax     # 0x6372c
incl (%rax)
movq 0x40(%rbx), %rdi
callq 0x264ce
movl $0x98, %edi
callq 0x7370
movq %rax, %rdi
movq 0x10(%r14), %rax
movl 0x34(%rsp), %ecx
shlq $0x6, %rcx
movzwl 0x20(%rax,%rcx), %edx
pushq $0x2
popq %rsi
leaq 0x50(%rsp), %rcx
movq %rdi, 0x48(%rsp)
callq 0x3a0dc
movl 0x4(%r15), %eax
cmpl 0xc(%r15), %eax
jne 0x34115
movq 0x48(%rsp), %rsi
movq %rsi, 0x28(%r15)
movq 0x40(%rbx), %rdi
callq 0x264ec
movq 0x40(%rbx), %rdi
movq (%rdi), %rax
callq *0x30(%rax)
jmp 0x33fc4
xorl %ebp, %ebp
pushq $0x1
popq %r13
pushq $0x4c
popq %r12
movl 0x104(%rbx), %eax
cmpq %rax, %r13
jae 0x33fa3
movq 0x10(%r14), %rax
movl (%rax,%r12), %ecx
movl %ebp, %edx
shlq $0x6, %rdx
cmpl 0xc(%rax,%rdx), %ecx
jbe 0x3410c
movl -0x8(%rax,%r12), %eax
cmpl 0x108(%rbx), %eax
jne 0x3410c
movq %rbx, %rdi
movq %r14, %rsi
movl %r13d, %edx
callq 0x337b0
testb %al, %al
je 0x3410c
movl %r13d, 0x34(%rsp)
movl %r13d, %ebp
incq %r13
addq $0x40, %r12
jmp 0x340c2
leaq 0x38(%rsp), %rcx
andq $0x0, (%rcx)
movq 0x48(%rsp), %rcx
subq $-0x80, %rcx
movq %rcx, 0x40(%rsp)
xorl %r13d, %r13d
movq %r15, 0x80(%rsp)
cmpl %eax, %r13d
jae 0x34097
movq 0x38(%rbx), %rdi
movq %r15, %rsi
movl %r13d, %edx
callq 0x31c92
testb %al, %al
je 0x342d9
leaq 0x2f9d2(%rip), %rax     # 0x63b30
incl (%rax)
movl %r13d, 0x64(%rsp)
cmpb $0x1, 0x64(%rbx)
jne 0x341da
movl $0xb0, %edi
callq 0x7370
movq %rax, %r12
movzwl 0x20(%r15), %ebp
movl 0x10c(%rbx), %r14d
movq 0x30(%rbx), %rdi
movq (%rdi), %rax
leaq 0x50(%rsp), %rsi
callq *0x90(%rax)
shll $0x9, %r14d
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movups %xmm0, (%rsp)
orq $-0x1, 0x20(%rsp)
movzwl %bp, %edx
movq %r12, %rdi
pushq $0x2
popq %rsi
movl %r14d, %ecx
pushq $-0x1
popq %r8
movq %rax, %r9
callq 0x3a2c6
movq %r12, 0x38(%rsp)
movl $0x1, 0xa8(%r12)
jmp 0x342be
movl $0xa0, %edi
callq 0x7370
movq %rax, %r12
movzwl 0x20(%r15), %ebp
movl 0x10c(%rbx), %r14d
movq 0x30(%rbx), %rdi
movq (%rdi), %rax
leaq 0x50(%rsp), %rsi
callq *0x90(%rax)
pushq $-0x1
popq %r15
shll $0x9, %r14d
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rsp)
movups %xmm0, 0x8(%rsp)
orq $-0x1, 0x28(%rsp)
leaq 0x50(%rsp), %rcx
movq %rcx, (%rsp)
movzwl %bp, %edx
movq %r12, %rdi
pushq $0x2
popq %rsi
movl %r14d, %ecx
movq %r15, %r8
movq %rax, %r9
callq 0x3a176
movl $0xb0, %edi
callq 0x7370
movq %rax, %r14
movq 0x80(%rsp), %rax
movzwl 0x20(%rax), %edx
movl 0x10c(%rbx), %ecx
shll $0x9, %ecx
movq %r12, 0x18(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rsp)
orq $-0x1, 0x28(%rsp)
andq $0x0, 0x20(%rsp)
leaq 0x50(%rsp), %rax
movq %rax, (%rsp)
movq %r14, %rdi
pushq $0x2
popq %rsi
movq %r15, %r8
movq %r15, %r9
callq 0x3a270
andl $0x0, 0xa8(%r14)
movq %r14, 0x38(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x90(%r14)
movq %r14, 0x88(%r12)
movq 0x80(%rsp), %r15
movq 0x40(%rbx), %rdi
movq %r12, %rsi
callq 0x264ec
movq 0x40(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x3381a
incl %r13d
movl 0x4(%r15), %eax
jmp 0x34137
movq %rax, %rbx
movq 0x48(%rsp), %rdi
jmp 0x342ff
movq %rax, %rbx
movq %r14, %r12
jmp 0x342fc
jmp 0x342f9
movq %rax, %rbx
movq %r12, %rdi
callq 0x7340
jmp 0x34317
jmp 0x3430a
jmp 0x3430a
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x27180
movq %rbx, %rdi
callq 0x75d0
nop