// File: CMU-SAFARI[P]MQSim/src/host/IO_Flow_Base.cpp
// Function: Host_Components::IO_Flow_Base::IO_Flow_Base(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, unsigned short, unsigned long, unsigned long, unsigned short, unsigned short, unsigned short, IO_Flow_Priority_Class::Priority, unsigned long, double, unsigned int, HostInterface_Types, Host_Components::PCIe_Root_Complex*, Host_Components::SATA_HBA*, bool, unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
IO_Flow_Base::IO_Flow_Base(const sim_object_id_type &name, uint16_t flow_id, LHA_type start_lsa_on_device, LHA_type end_lsa_on_device, uint16_t io_queue_id,
						   uint16_t nvme_submission_queue_size, uint16_t nvme_completion_queue_size,
						   IO_Flow_Priority_Class::Priority priority_class, sim_time_type stop_time, double initial_occupancy_ratio, unsigned int total_requets_to_be_generated,
						   HostInterface_Types SSD_device_type, PCIe_Root_Complex *pcie_root_complex, SATA_HBA *sata_hba,
						   bool enabled_logging, sim_time_type logging_period, std::string logging_file_path) : MQSimEngine::Sim_Object(name), flow_id(flow_id), start_lsa_on_device(start_lsa_on_device), end_lsa_on_device(end_lsa_on_device), io_queue_id(io_queue_id),
																												priority_class(priority_class), stop_time(stop_time), initial_occupancy_ratio(initial_occupancy_ratio), total_requests_to_be_generated(total_requets_to_be_generated), SSD_device_type(SSD_device_type), pcie_root_complex(pcie_root_complex), sata_hba(sata_hba),
																												STAT_generated_request_count(0), STAT_generated_read_request_count(0), STAT_generated_write_request_count(0),
																												STAT_ignored_request_count(0),
																												STAT_serviced_request_count(0), STAT_serviced_read_request_count(0), STAT_serviced_write_request_count(0),
																												STAT_sum_device_response_time(0), STAT_sum_device_response_time_read(0), STAT_sum_device_response_time_write(0),
																												STAT_min_device_response_time(MAXIMUM_TIME), STAT_min_device_response_time_read(MAXIMUM_TIME), STAT_min_device_response_time_write(MAXIMUM_TIME),
																												STAT_max_device_response_time(0), STAT_max_device_response_time_read(0), STAT_max_device_response_time_write(0),
																												STAT_sum_request_delay(0), STAT_sum_request_delay_read(0), STAT_sum_request_delay_write(0),
																												STAT_min_request_delay(MAXIMUM_TIME), STAT_min_request_delay_read(MAXIMUM_TIME), STAT_min_request_delay_write(MAXIMUM_TIME),
																												STAT_max_request_delay(0), STAT_max_request_delay_read(0), STAT_max_request_delay_write(0),
																												STAT_transferred_bytes_total(0), STAT_transferred_bytes_read(0), STAT_transferred_bytes_write(0), progress(0), next_progress_step(0),
																												enabled_logging(enabled_logging), logging_period(logging_period), logging_file_path(logging_file_path)
{
	Host_IO_Request *t = NULL;

	switch (SSD_device_type)
	{
	case HostInterface_Types::NVME:
		for (uint16_t cmdid = 0; cmdid < (uint16_t)(0xffffffff); cmdid++)
		{
			available_command_ids.insert(cmdid);
		}
		for (uint16_t cmdid = 0; cmdid < nvme_submission_queue_size; cmdid++)
		{
			request_queue_in_memory.push_back(t);
		}
		nvme_queue_pair.Submission_queue_size = nvme_submission_queue_size;
		nvme_queue_pair.Submission_queue_head = 0;
		nvme_queue_pair.Submission_queue_tail = 0;
		nvme_queue_pair.Completion_queue_size = nvme_completion_queue_size;
		nvme_queue_pair.Completion_queue_head = 0;
		nvme_queue_pair.Completion_queue_tail = 0;

		//id = 0: admin queues, id = 1 to 8, normal I/O queues
		switch (io_queue_id)
		{
		case 0:
			throw std::logic_error("I/O queue id 0 is reserved for NVMe admin queues and should not be used for I/O flows");
			/*
						nvme_queue_pair.Submission_queue_memory_base_address = SUBMISSION_QUEUE_MEMORY_0;
						nvme_queue_pair.Submission_tail_register_address_on_device = SUBMISSION_QUEUE_REGISTER_0;
						nvme_queue_pair.Completion_queue_tail = COMPLETION_QUEUE_REGISTER_0;*/
			nvme_queue_pair.Completion_queue_memory_base_address = COMPLETION_QUEUE_REGISTER_0;
			break;
		case 1:
			nvme_queue_pair.Submission_queue_memory_base_address = SUBMISSION_QUEUE_MEMORY_1;
			nvme_queue_pair.Submission_tail_register_address_on_device = SUBMISSION_QUEUE_REGISTER_1;
			nvme_queue_pair.Completion_queue_memory_base_address = COMPLETION_QUEUE_MEMORY_1;
			nvme_queue_pair.Completion_head_register_address_on_device = COMPLETION_QUEUE_REGISTER_1;
			break;
		case 2:
			nvme_queue_pair.Submission_queue_memory_base_address = SUBMISSION_QUEUE_MEMORY_2;
			nvme_queue_pair.Submission_tail_register_address_on_device = SUBMISSION_QUEUE_REGISTER_2;
			nvme_queue_pair.Completion_queue_memory_base_address = COMPLETION_QUEUE_MEMORY_2;
			nvme_queue_pair.Completion_head_register_address_on_device = COMPLETION_QUEUE_REGISTER_2;
			break;
		case 3:
			nvme_queue_pair.Submission_queue_memory_base_address = SUBMISSION_QUEUE_MEMORY_3;
			nvme_queue_pair.Submission_tail_register_address_on_device = SUBMISSION_QUEUE_REGISTER_3;
			nvme_queue_pair.Completion_queue_memory_base_address = COMPLETION_QUEUE_MEMORY_3;
			nvme_queue_pair.Completion_head_register_address_on_device = COMPLETION_QUEUE_REGISTER_3;
			break;
		case 4:
			nvme_queue_pair.Submission_queue_memory_base_address = SUBMISSION_QUEUE_MEMORY_4;
			nvme_queue_pair.Submission_tail_register_address_on_device = SUBMISSION_QUEUE_REGISTER_4;
			nvme_queue_pair.Completion_queue_memory_base_address = COMPLETION_QUEUE_MEMORY_4;
			nvme_queue_pair.Completion_head_register_address_on_device = COMPLETION_QUEUE_REGISTER_4;
			break;
		case 5:
			nvme_queue_pair.Submission_queue_memory_base_address = SUBMISSION_QUEUE_MEMORY_5;
			nvme_queue_pair.Submission_tail_register_address_on_device = SUBMISSION_QUEUE_REGISTER_5;
			nvme_queue_pair.Completion_queue_memory_base_address = COMPLETION_QUEUE_MEMORY_5;
			nvme_queue_pair.Completion_head_register_address_on_device = COMPLETION_QUEUE_REGISTER_5;
			break;
		case 6:
			nvme_queue_pair.Submission_queue_memory_base_address = SUBMISSION_QUEUE_MEMORY_6;
			nvme_queue_pair.Submission_tail_register_address_on_device = SUBMISSION_QUEUE_REGISTER_6;
			nvme_queue_pair.Completion_queue_memory_base_address = COMPLETION_QUEUE_MEMORY_6;
			nvme_queue_pair.Completion_head_register_address_on_device = COMPLETION_QUEUE_REGISTER_6;
			break;
		case 7:
			nvme_queue_pair.Submission_queue_memory_base_address = SUBMISSION_QUEUE_MEMORY_7;
			nvme_queue_pair.Submission_tail_register_address_on_device = SUBMISSION_QUEUE_REGISTER_7;
			nvme_queue_pair.Completion_queue_memory_base_address = COMPLETION_QUEUE_MEMORY_7;
			nvme_queue_pair.Completion_head_register_address_on_device = COMPLETION_QUEUE_REGISTER_7;
			break;
		case 8:
			nvme_queue_pair.Submission_queue_memory_base_address = SUBMISSION_QUEUE_MEMORY_8;
			nvme_queue_pair.Submission_tail_register_address_on_device = SUBMISSION_QUEUE_REGISTER_8;
			nvme_queue_pair.Completion_queue_memory_base_address = COMPLETION_QUEUE_MEMORY_8;
			nvme_queue_pair.Completion_head_register_address_on_device = COMPLETION_QUEUE_REGISTER_8;
			break;
		default:
			break;
		}
		break;
	default:
		break;
	}
	}

// Assembly:
subq $0xf8, %rsp
movl %r9d, 0x34(%rsp)
movq %rcx, %rax
movl 0x34(%rsp), %ecx
movq %rax, 0x38(%rsp)
movl %edx, %eax
movq 0x38(%rsp), %rdx
movq %rsi, 0x40(%rsp)
movq %rdi, %r9
movq 0x40(%rsp), %rdi
movw %ax, %si
movq 0x150(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x148(%rsp), %rax
movb 0x140(%rsp), %al
movq 0x138(%rsp), %r10
movq 0x130(%rsp), %r10
movl 0x128(%rsp), %r10d
movl 0x120(%rsp), %r10d
movq 0x118(%rsp), %r10
movl 0x110(%rsp), %r10d
movw 0x108(%rsp), %r10w
movw 0x100(%rsp), %r10w
movq %r9, 0xf0(%rsp)
movq %rdi, 0xe8(%rsp)
movw %si, 0xe6(%rsp)
movq %rdx, 0xd8(%rsp)
movq %r8, 0xd0(%rsp)
movw %cx, 0xce(%rsp)
movsd %xmm0, 0xc0(%rsp)
andb $0x1, %al
movb %al, 0xbf(%rsp)
movq 0xf0(%rsp), %rdi
movq %rdi, 0x68(%rsp)
movq 0xe8(%rsp), %rsi
callq 0x1fd60
movq 0x68(%rsp), %rdi
addq $0x30, %rdi
callq 0x1fde0
movq 0x68(%rsp), %rdi
leaq 0xa8ca5(%rip), %rax     # 0xd8b98
movq %rax, (%rdi)
leaq 0xa8cf3(%rip), %rax     # 0xd8bf0
movq %rax, 0x30(%rdi)
movw 0xe6(%rsp), %ax
movw %ax, 0x38(%rdi)
movsd 0xc0(%rsp), %xmm0
movsd %xmm0, 0x40(%rdi)
movq 0x118(%rsp), %rax
movq %rax, 0x48(%rdi)
movl 0x120(%rsp), %eax
movl %eax, 0x50(%rdi)
movl 0x128(%rsp), %eax
movl %eax, 0x54(%rdi)
movq 0x130(%rsp), %rax
movq %rax, 0x58(%rdi)
movq 0x138(%rsp), %rax
movq %rax, 0x60(%rdi)
movq 0xd8(%rsp), %rax
movq %rax, 0x68(%rdi)
movq 0xd0(%rsp), %rax
movq %rax, 0x70(%rdi)
movl 0x110(%rsp), %eax
movl %eax, 0x78(%rdi)
movw 0xce(%rsp), %ax
movw %ax, 0xb0(%rdi)
addq $0xb8, %rdi
movq %rdi, 0x48(%rsp)
callq 0x33fe0
movq 0x68(%rsp), %rdi
addq $0xe8, %rdi
movq %rdi, 0x50(%rsp)
callq 0x33ff0
movq 0x68(%rsp), %rdi
addq $0x100, %rdi            # imm = 0x100
movq %rdi, 0x58(%rsp)
callq 0x34000
movq 0x68(%rsp), %rdi
addq $0x118, %rdi            # imm = 0x118
movq %rdi, 0x60(%rsp)
callq 0x34010
movq 0x68(%rsp), %rdi
movq 0x70(%rsp), %rsi
movl $0x0, 0x150(%rdi)
movl $0x0, 0x154(%rdi)
movl $0x0, 0x158(%rdi)
movl $0x0, 0x15c(%rdi)
movl $0x0, 0x160(%rdi)
movl $0x0, 0x164(%rdi)
movl $0x0, 0x168(%rdi)
movq $0x0, 0x170(%rdi)
movq $0x0, 0x178(%rdi)
movq $0x0, 0x180(%rdi)
movq $-0x1, 0x188(%rdi)
movq $-0x1, 0x190(%rdi)
movq $-0x1, 0x198(%rdi)
movq $0x0, 0x1a0(%rdi)
movq $0x0, 0x1a8(%rdi)
movq $0x0, 0x1b0(%rdi)
movq $0x0, 0x1b8(%rdi)
movq $0x0, 0x1c0(%rdi)
movq $0x0, 0x1c8(%rdi)
movq $-0x1, 0x1d0(%rdi)
movq $-0x1, 0x1d8(%rdi)
movq $-0x1, 0x1e0(%rdi)
movq $0x0, 0x1e8(%rdi)
movq $0x0, 0x1f0(%rdi)
movq $0x0, 0x1f8(%rdi)
movq $0x0, 0x200(%rdi)
movq $0x0, 0x208(%rdi)
movq $0x0, 0x210(%rdi)
movl $0x0, 0x218(%rdi)
movl $0x0, 0x21c(%rdi)
movb 0xbf(%rsp), %al
andb $0x1, %al
movb %al, 0x220(%rdi)
movq 0x148(%rsp), %rax
movq %rax, 0x228(%rdi)
addq $0x238, %rdi            # imm = 0x238
movq %rdi, 0x78(%rsp)
callq 0x81a0
jmp 0x30153
movq 0x68(%rsp), %rdi
addq $0x258, %rdi            # imm = 0x258
movq %rdi, 0x28(%rsp)
callq 0x8700
jmp 0x3016b
movq $0x0, 0xa0(%rsp)
movl 0x128(%rsp), %eax
subl $0x1, %eax
je 0x301ba
jmp 0x30573
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
jmp 0x30591
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
jmp 0x30587
movw $0x0, 0x9e(%rsp)
movzwl 0x9e(%rsp), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
jge 0x3023e
movq 0x68(%rsp), %rdi
addq $0xb8, %rdi
leaq 0x9e(%rsp), %rsi
callq 0x34020
movb %dl, 0x1f(%rsp)
movq %rax, 0x20(%rsp)
jmp 0x301f7
movb 0x1f(%rsp), %al
movq 0x20(%rsp), %rcx
movq %rcx, 0x88(%rsp)
movb %al, 0x90(%rsp)
movw 0x9e(%rsp), %ax
addw $0x1, %ax
movw %ax, 0x9e(%rsp)
jmp 0x301c4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
jmp 0x3057d
movw $0x0, 0x86(%rsp)
movzwl 0x86(%rsp), %eax
movzwl 0x100(%rsp), %ecx
cmpl %ecx, %eax
jge 0x3028f
movq 0x68(%rsp), %rdi
addq $0xe8, %rdi
leaq 0xa0(%rsp), %rsi
callq 0x34080
jmp 0x30277
jmp 0x30279
movw 0x86(%rsp), %ax
addw $0x1, %ax
movw %ax, 0x86(%rsp)
jmp 0x30248
movq 0x68(%rsp), %rax
movw 0x100(%rsp), %cx
movw %cx, 0x84(%rax)
movw $0x0, 0x80(%rax)
movw $0x0, 0x82(%rax)
movw 0x108(%rsp), %cx
movw %cx, 0x9c(%rax)
movw $0x0, 0x98(%rax)
movw $0x0, 0x9a(%rax)
movzwl 0xce(%rsp), %eax
movq %rax, 0x10(%rsp)
subq $0x8, %rax
ja 0x3056f
movq 0x10(%rsp), %rax
leaq 0x7b667(%rip), %rcx     # 0xab960
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x10, %edi
callq 0x8200
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x8(%rsp)
leaq 0x7b6b6(%rip), %rsi     # 0xab9d4
callq 0x8610
jmp 0x30325
movq 0x8(%rsp), %rdi
movq 0xa9c8f(%rip), %rsi     # 0xd9fc0
movq 0xa9c58(%rip), %rdx     # 0xd9f90
callq 0x8710
jmp 0x305d0
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
callq 0x8330
jmp 0x3057d
movq 0x68(%rsp), %rax
movabsq $0x10000000000, %rcx    # imm = 0x10000000000
movq %rcx, 0x90(%rax)
movq $0x1010, 0x88(%rax) # imm = 0x1010
movabsq $0x1f000000000, %rcx    # imm = 0x1F000000000
movq %rcx, 0xa8(%rax)
movq $0x1013, 0xa0(%rax) # imm = 0x1013
jmp 0x30571
movq 0x68(%rsp), %rax
movabsq $0x20000000000, %rcx    # imm = 0x20000000000
movq %rcx, 0x90(%rax)
movq $0x1020, 0x88(%rax) # imm = 0x1020
movabsq $0x2f000000000, %rcx    # imm = 0x2F000000000
movq %rcx, 0xa8(%rax)
movq $0x1023, 0xa0(%rax) # imm = 0x1023
jmp 0x30571
movq 0x68(%rsp), %rax
movabsq $0x30000000000, %rcx    # imm = 0x30000000000
movq %rcx, 0x90(%rax)
movq $0x1030, 0x88(%rax) # imm = 0x1030
movabsq $0x3f000000000, %rcx    # imm = 0x3F000000000
movq %rcx, 0xa8(%rax)
movq $0x1033, 0xa0(%rax) # imm = 0x1033
jmp 0x30571
movq 0x68(%rsp), %rax
movabsq $0x40000000000, %rcx    # imm = 0x40000000000
movq %rcx, 0x90(%rax)
movq $0x1040, 0x88(%rax) # imm = 0x1040
movabsq $0x4f000000000, %rcx    # imm = 0x4F000000000
movq %rcx, 0xa8(%rax)
movq $0x1043, 0xa0(%rax) # imm = 0x1043
jmp 0x30571
movq 0x68(%rsp), %rax
movabsq $0x50000000000, %rcx    # imm = 0x50000000000
movq %rcx, 0x90(%rax)
movq $0x1050, 0x88(%rax) # imm = 0x1050
movabsq $0x5f000000000, %rcx    # imm = 0x5F000000000
movq %rcx, 0xa8(%rax)
movq $0x1053, 0xa0(%rax) # imm = 0x1053
jmp 0x30571
movq 0x68(%rsp), %rax
movabsq $0x60000000000, %rcx    # imm = 0x60000000000
movq %rcx, 0x90(%rax)
movq $0x1060, 0x88(%rax) # imm = 0x1060
movabsq $0x6f000000000, %rcx    # imm = 0x6F000000000
movq %rcx, 0xa8(%rax)
movq $0x1063, 0xa0(%rax) # imm = 0x1063
jmp 0x30571
movq 0x68(%rsp), %rax
movabsq $0x70000000000, %rcx    # imm = 0x70000000000
movq %rcx, 0x90(%rax)
movq $0x1070, 0x88(%rax) # imm = 0x1070
movabsq $0x7f000000000, %rcx    # imm = 0x7F000000000
movq %rcx, 0xa8(%rax)
movq $0x1073, 0xa0(%rax) # imm = 0x1073
jmp 0x30571
movq 0x68(%rsp), %rax
movabsq $0x80000000000, %rcx    # imm = 0x80000000000
movq %rcx, 0x90(%rax)
movq $0x1080, 0x88(%rax) # imm = 0x1080
movabsq $0x8f000000000, %rcx    # imm = 0x8F000000000
movq %rcx, 0xa8(%rax)
movq $0x1083, 0xa0(%rax) # imm = 0x1083
jmp 0x30571
jmp 0x30571
jmp 0x30575
jmp 0x30575
addq $0xf8, %rsp
retq
movq 0x28(%rsp), %rdi
callq 0x8640
movq 0x78(%rsp), %rdi
callq 0x8848
movq 0x60(%rsp), %rdi
callq 0x340f0
movq 0x58(%rsp), %rdi
callq 0x34100
movq 0x50(%rsp), %rdi
callq 0x34110
movq 0x48(%rsp), %rdi
callq 0x34170
movq 0x68(%rsp), %rdi
callq 0x1ff90
movq 0xb0(%rsp), %rdi
callq 0x8770