// File: narges64[P]SIMBA/garbage_collection.cpp
// Function: Schedule_GC(ssd_info*, sub_request*)
bool Schedule_GC(ssd_info * ssd, sub_request * sub){
		
	local * location = sub->location; 
	unsigned int free_page = ssd->channel_head[location->channel]->lun_head[location->lun]->plane_head[location->plane]->free_page;
	unsigned int invalid_page = ssd->channel_head[location->channel]->lun_head[location->lun]->plane_head[location->plane]->invalid_page;
	unsigned int all_page = ssd->parameter->page_block*ssd->parameter->block_plane;
	gc_operation * gc_node = NULL;



	if (free_page > (all_page * ssd->parameter->gc_down_threshold)){
		return false;
	}
	gc_node = new gc_operation(location, ssd->gc_sequence_number++);

	if (add_gc_node(ssd, gc_node) != SUCCESS){
		delete gc_node;
		return false;
	}

	sub->trigger_gc = true; 	
	
	//int invalid_00 = ssd->channel_head[0]->lun_head[0]->plane_head[location->plane]->invalid_page;
	//int invalid_01 = ssd->channel_head[0]->lun_head[1]->plane_head[location->plane]->invalid_page;
	//int invalid_10 = ssd->channel_head[1]->lun_head[0]->plane_head[location->plane]->invalid_page;
	//int invalid_11 = ssd->channel_head[1]->lun_head[1]->plane_head[location->plane]->invalid_page;
	//cout << "GC starts for planes " << location->channel << location->lun << " with (" << invalid_00 << "," << invalid_01 << "," << invalid_10 << "," << invalid_11 << ")\n";
	gc_for_plane(ssd, gc_node);

	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movq -0x20(%rbp), %rcx
movslq (%rcx), %rdx
movq (%rax,%rdx,8), %rax
movq 0x50(%rax), %rax
movslq 0x4(%rcx), %rdx
movq (%rax,%rdx,8), %rax
movq (%rax), %rax
movslq 0x8(%rcx), %rcx
movq (%rax,%rcx,8), %rax
movl (%rax), %eax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movq -0x20(%rbp), %rcx
movslq (%rcx), %rdx
movq (%rax,%rdx,8), %rax
movq 0x50(%rax), %rax
movslq 0x4(%rcx), %rdx
movq (%rax,%rdx,8), %rax
movq (%rax), %rax
movslq 0x8(%rcx), %rcx
movq (%rax,%rcx,8), %rax
movl 0x8(%rax), %eax
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl 0x230(%rax), %ecx
movl 0x234(%rax), %eax
imull %ecx, %eax
movl %eax, -0x2c(%rbp)
movq $0x0, -0x38(%rbp)
movl -0x24(%rbp), %eax
cvtsi2ss %rax, %xmm0
movl -0x2c(%rbp), %eax
cvtsi2ss %rax, %xmm1
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
mulss 0x28c(%rax), %xmm1
ucomiss %xmm1, %xmm0
jbe 0xb431
movb $0x0, -0x1(%rbp)
jmp 0xb4db
movl $0x20, %edi
callq 0x2130
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rsi
movq -0x10(%rbp), %rax
movl 0x10(%rax), %edx
movl %edx, %ecx
incl %ecx
movl %ecx, 0x10(%rax)
callq 0xbab0
jmp 0xb45e
movq -0x50(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0xb4f0
cmpl $0x1, %eax
je 0xb4bf
movq -0x38(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, %rax
je 0xb49d
movq -0x58(%rbp), %rdi
callq 0xbb50
movq -0x58(%rbp), %rdi
movl $0x20, %esi
callq 0x2140
movb $0x0, -0x1(%rbp)
jmp 0xb4db
movq -0x50(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
movl $0x20, %esi
callq 0x2140
jmp 0xb4e6
movq -0x18(%rbp), %rax
movb $0x1, 0x98(%rax)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x9e20
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x60, %rsp
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x2230
nop