// File: /workspace/github_cpp_makefile_star2_O3/yangwanyuan[P]HLKVDS/src/Migrate.cc
// Function: hlkvds::Migrate::doMigrate(unsigned int, unsigned int)
uint32_t Migrate::doMigrate(uint32_t mt_vol_id, uint32_t max_seg_num) {
    Volume *ft_vol = ft_->GetVolume();
    Volume *mt_vol = mt_->GetVolume(mt_vol_id);

    std::multimap < uint32_t, uint32_t > cands_map;
    ft_vol->SortSegsByTS(cands_map, max_seg_num);

    //do merge
    bool ret = false;
    std::vector <uint32_t> free_seg_vec;

    std::list<KVSlice*> slice_list;
    std::list<KVSlice*> recycle_list;

    SegForMigrate *mig_seg = new SegForMigrate(mt_vol, idxMgr_);
    for (std::multimap<uint32_t, uint32_t>::iterator iter = cands_map.begin(); iter
            != cands_map.end(); ++iter) {
        uint32_t seg_id = iter->second;

        ret = loadKvList(seg_id, slice_list);
        if (!ret) {
            continue;
        }

        if (mig_seg->TryPutList(slice_list)) {
            mig_seg->PutList(slice_list);
            free_seg_vec.push_back(seg_id);
            recycle_list.splice(recycle_list.end(), slice_list);
        }
        else {
            recycle_list.splice(recycle_list.end(), slice_list);
            break;
        }
    }
    
    //Allocate segment id from volume
    uint32_t mig_seg_id;
    if (!mt_vol->Alloc(mig_seg_id)) {
        __WARN("The Medium Tier Volume[%d] is Full, can not allocate segment", mig_seg_id);
        delete mig_seg;
        cleanKvList(recycle_list);
        return 0;
    }
    mig_seg->SetSegId(mig_seg_id);

    //Write to volume device
    ret = mig_seg->WriteSegToDevice();
    if (!ret) {
        __ERROR("Write First GC segment to device failed, free 0 segments");
        delete mig_seg;
        mt_vol->FreeForFailed(mig_seg_id);
        cleanKvList(recycle_list);
        return 0;
    }

    uint32_t free_size = mig_seg->GetFreeSize();
    mt_vol->Use(mig_seg_id, free_size);

    mig_seg->UpdateToIndex();


    //clean work for first segment
    for (std::vector<uint32_t>::iterator iter = free_seg_vec.begin(); iter
            != free_seg_vec.end(); ++iter) {
        ft_vol->FreeForGC(*iter);
    }
    delete mig_seg;
    cleanKvList(recycle_list);

    uint32_t total_free = free_seg_vec.size();

    cands_map.clear();
    __DEBUG("Once doMigrate total free %d segments in Fast Tier!", total_free);
    return total_free;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %edx, %ebx
movq %rdi, %r13
movq (%rdi), %rax
movq 0x8(%rdi), %rdi
movq 0x1d8(%rax), %r12
callq 0x2054a
movq %rax, %rbp
leaq 0x70(%rsp), %rcx
movl $0x0, (%rcx)
xorl %eax, %eax
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
leaq 0x68(%rsp), %rsi
movq %r12, %rdi
movl %ebx, %edx
callq 0x20d58
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
xorl %eax, %eax
movq %rax, 0x20(%rsp)
leaq 0x50(%rsp), %rbx
movq %rbx, 0x8(%rbx)
movq %rbx, (%rbx)
movq %rax, 0x10(%rbx)
leaq 0x38(%rsp), %r15
movq %r15, 0x8(%r15)
movq %r15, (%r15)
movq %rax, 0x10(%r15)
movl $0x50, %edi
callq 0x6410
movq %rax, %r14
movq 0x10(%r13), %rdx
movq %rax, %rdi
movq %rbp, 0x30(%rsp)
movq %rbp, %rsi
callq 0x1caa2
movq 0x80(%rsp), %rbp
leaq 0x70(%rsp), %rax
cmpq %rax, %rbp
je 0x16e11
movl 0x24(%rbp), %esi
movl %esi, 0xc(%rsp)
movq %r13, %rdi
movq %rbx, %rdx
callq 0x1707e
testb %al, %al
je 0x16dd2
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1bedc
testb %al, %al
je 0x16de2
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1bf14
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x16d99
movl 0xc(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x16da8
leaq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x10d0a
movq 0x50(%rsp), %rsi
cmpq %rbx, %rsi
je 0x16dd2
leaq 0x38(%rsp), %rdi
movq %rbx, %rdx
callq 0x6210
movq 0x60(%rsp), %rax
addq %rax, 0x48(%rsp)
movq $0x0, 0x60(%rsp)
movq %rbp, %rdi
callq 0x62d0
movq %rax, %rbp
jmp 0x16d3e
movq 0x50(%rsp), %rsi
cmpq %rbx, %rsi
je 0x16e11
leaq 0x38(%rsp), %rbp
leaq 0x50(%rsp), %r13
movq %rbp, %rdi
movq %r13, %rdx
callq 0x6210
movq 0x10(%r13), %rax
addq %rax, 0x10(%rbp)
movq $0x0, 0x10(%r13)
leaq 0xc(%rsp), %rsi
movq 0x30(%rsp), %r13
movq %r13, %rdi
callq 0x20c92
testb %al, %al
je 0x16f10
movl 0xc(%rsp), %eax
movl %eax, (%r14)
movq %r14, %rdi
callq 0x1bf42
testb %al, %al
je 0x16f27
movl 0x18(%r14), %edx
subl 0x14(%r14), %edx
movl 0xc(%rsp), %esi
movq %r13, %rdi
callq 0x20d80
movq %r14, %rdi
callq 0x1cadc
movq 0x10(%rsp), %r13
cmpq 0x18(%rsp), %r13
je 0x16e7c
movl (%r13), %esi
movq %r12, %rdi
callq 0x20d76
addq $0x4, %r13
jmp 0x16e63
movq %r14, %rdi
callq 0x1bd44
movq %r14, %rdi
callq 0x63f0
leaq 0x38(%rsp), %rsi
callq 0x17132
movq 0x18(%rsp), %r12
subq 0x10(%rsp), %r12
shrq $0x2, %r12
leaq 0x68(%rsp), %rdi
callq 0x10cdc
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
je 0x16ec8
movq (%rdi), %r14
callq 0x63f0
movq %r14, %rdi
cmpq %r15, %r14
jne 0x16eb8
movq 0x50(%rsp), %rdi
cmpq %rbx, %rdi
je 0x16ee2
movq (%rdi), %r14
callq 0x63f0
movq %r14, %rdi
cmpq %rbx, %r14
jne 0x16ed2
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x16ef1
callq 0x63f0
leaq 0x68(%rsp), %rdi
callq 0x10c92
movl %r12d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc(%rsp), %rdi
leaq 0x38(%rsp), %rdx
movq %r14, %rsi
callq 0x8624
xorl %r12d, %r12d
jmp 0x16eae
movq 0x1d0aa(%rip), %rbp     # 0x33fd8
movq (%rbp), %r13
callq 0x63c0
leaq 0xb387(%rip), %rsi      # 0x222c5
leaq 0xc5ab(%rip), %rcx      # 0x234f0
leaq 0xc5ae(%rip), %r8       # 0x234fa
xorl %r12d, %r12d
movq %r13, %rdi
movq %rax, %rdx
movl $0x5e, %r9d
xorl %eax, %eax
callq 0x6560
movq (%rbp), %rcx
leaq 0xba44(%rip), %rdi      # 0x229b1
movl $0x38, %esi
movl $0x1, %edx
callq 0x6730
movq (%rbp), %rsi
movl $0xa, %edi
callq 0x64c0
movq %r14, %rdi
callq 0x1bd44
movq %r14, %rdi
callq 0x63f0
movl 0xc(%rsp), %esi
movq 0x30(%rsp), %rdi
callq 0x20d6c
leaq 0x38(%rsp), %rsi
callq 0x17132
jmp 0x16eae
movq %rax, %r12
movq %r14, %rdi
callq 0x63f0
jmp 0x16fd4
jmp 0x16fd1
movq %rax, %r12
jmp 0x17017
jmp 0x16fd1
jmp 0x16fd1
jmp 0x16fd1
movq %rax, %r12
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
je 0x16fee
movq (%rdi), %r14
callq 0x63f0
movq %r14, %rdi
cmpq %r15, %r14
jne 0x16fde
movq 0x50(%rsp), %rdi
cmpq %rbx, %rdi
je 0x17008
movq (%rdi), %r14
callq 0x63f0
movq %r14, %rdi
cmpq %rbx, %r14
jne 0x16ff8
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x17017
callq 0x63f0
leaq 0x68(%rsp), %rdi
callq 0x10c92
movq %r12, %rdi
callq 0x6670
nop