// File: /workspace/github_cpp_makefile_star2_O2/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 $0x88, %rsp
movl %edx, %ebx
movq %rdi, %r12
movq (%rdi), %rax
movq 0x8(%rdi), %rdi
movq 0x1d8(%rax), %r15
callq 0x1d5d4
movq %rax, 0x8(%rsp)
leaq 0x60(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x58(%rsp), %rsi
movq %r15, %rdi
movl %ebx, %edx
callq 0x1df5a
andq $0x0, 0x20(%rsp)
leaq 0x40(%rsp), %rax
movq %rax, 0x8(%rax)
movq %rax, (%rax)
andq $0x0, 0x10(%rax)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
leaq 0x28(%rsp), %rax
movq %rax, 0x8(%rax)
movq %rax, (%rax)
andq $0x0, 0x10(%rax)
pushq $0x50
popq %rdi
callq 0x6410
movq %rax, %rbx
movq 0x10(%r12), %rdx
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x194b6
movq 0x70(%rsp), %r14
leaq 0x40(%rsp), %r13
leaq 0x28(%rsp), %rbp
leaq 0x60(%rsp), %rax
cmpq %rax, %r14
je 0x145dc
movl 0x24(%r14), %esi
movl %esi, 0x4(%rsp)
movq %r12, %rdi
movq %r13, %rdx
callq 0x1483c
testb %al, %al
je 0x145bd
movq %rbx, %rdi
movq %r13, %rsi
callq 0x189c4
testb %al, %al
je 0x145ca
movq %rbx, %rdi
movq %r13, %rsi
callq 0x189f8
leaq 0x10(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq 0xe05a
movq %rbp, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x14b74
movq %r14, %rdi
callq 0x62c0
movq %rax, %r14
jmp 0x14565
leaq 0x28(%rsp), %rdi
leaq 0x40(%rsp), %rdx
movq %rdi, %rsi
callq 0x14b74
leaq 0x4(%rsp), %rsi
movq 0x8(%rsp), %rdi
callq 0x1dde4
testb %al, %al
je 0x146b1
movl 0x4(%rsp), %eax
movl %eax, (%rbx)
movq %rbx, %rdi
callq 0x18a22
testb %al, %al
je 0x1471e
movl 0x18(%rbx), %edx
subl 0x14(%rbx), %edx
movl 0x4(%rsp), %esi
movq 0x8(%rsp), %rdi
callq 0x1df82
movq %rbx, %rdi
callq 0x194d2
movq 0x10(%rsp), %r14
cmpq 0x18(%rsp), %r14
je 0x14642
movl (%r14), %esi
movq %r15, %rdi
callq 0x1df78
addq $0x4, %r14
jmp 0x1462a
movq %rbx, %rdi
callq 0x18894
movq %rbx, %rdi
callq 0x63f0
leaq 0x28(%rsp), %rsi
callq 0x148f0
movq 0x18(%rsp), %r15
subq 0x10(%rsp), %r15
shrq $0x2, %r15
leaq 0x58(%rsp), %rdi
callq 0xe0ec
leaq 0x28(%rsp), %rdi
callq 0xe0c8
leaq 0x40(%rsp), %rdi
callq 0xe0c8
leaq 0x10(%rsp), %rdi
callq 0xe11a
leaq 0x58(%rsp), %rdi
callq 0xe080
movl %r15d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1d920(%rip), %r12     # 0x31fd8
movq (%r12), %r14
callq 0x63c0
leaq 0xb46c(%rip), %rsi      # 0x1fb34
leaq 0xbe31(%rip), %rcx      # 0x20500
leaq 0xbe34(%rip), %r8       # 0x2050a
xorl %r15d, %r15d
pushq $0x54
popq %r9
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x6580
movq (%r12), %rdi
movl 0x4(%rsp), %edx
leaq 0xbe20(%rip), %rsi      # 0x20519
xorl %eax, %eax
callq 0x6580
movq (%r12), %rsi
pushq $0xa
popq %rdi
callq 0x64b0
movq %rbx, %rdi
callq 0x18894
movq %rbx, %rdi
callq 0x63f0
jmp 0x14797
movq 0x1d8b3(%rip), %r12     # 0x31fd8
movq (%r12), %r14
callq 0x63c0
leaq 0xab70(%rip), %rsi      # 0x1f2a5
leaq 0xbdc4(%rip), %rcx      # 0x20500
leaq 0xbdc7(%rip), %r8       # 0x2050a
xorl %r15d, %r15d
pushq $0x5e
popq %r9
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x6580
movq (%r12), %rcx
leaq 0xb25f(%rip), %rdi      # 0x1f9c1
pushq $0x38
popq %rsi
pushq $0x1
popq %rdx
callq 0x6750
movq (%r12), %rsi
pushq $0xa
popq %rdi
callq 0x64b0
movq %rbx, %rdi
callq 0x18894
movq %rbx, %rdi
callq 0x63f0
movl 0x4(%rsp), %esi
movq 0x8(%rsp), %rdi
callq 0x1df6e
leaq 0x28(%rsp), %rsi
callq 0x148f0
jmp 0x14674
movq %rax, %r14
movq %rbx, %rdi
callq 0x63f0
jmp 0x147c3
jmp 0x147c0
movq %rax, %r14
jmp 0x147e1
jmp 0x147c0
jmp 0x147c0
jmp 0x147c0
movq %rax, %r14
leaq 0x28(%rsp), %rdi
callq 0xe0c8
leaq 0x40(%rsp), %rdi
callq 0xe0c8
leaq 0x10(%rsp), %rdi
callq 0xe11a
leaq 0x58(%rsp), %rdi
callq 0xe080
movq %r14, %rdi
callq 0x6680
nop