// File: /workspace/github_cpp_makefile_star2_O2/lega911[P]ijson/src/service.cpp
// Function: Service::_start()
void Service::_start() {
    int threads = server->threads;
    bool log = server->log & 64;
    u64 *used = new u64[threads]();
    int *cpu = new int[threads]();
    u64 prev_time = get_ntime();
    i16 clean_tasks = 2;

    while(true) {
        usleep(500'000);  // 500ms
        _free_memory();

        if(--clean_tasks <= 0) {
            clean_tasks = 2;
            _clean_dead_tasks();
        }

        #ifndef _NOBALANCER
        if(threads > 1) {
            // balance
            u64 now = get_ntime();
            u64 frame = now - prev_time;
            prev_time = now;

            struct timespec thread_time;
            clockid_t threadClockId;

            if(log) std::cout << "cpu: ";
            u64 total = 0;
            for(int i=0; i<threads; i++) {
                auto id = server->loops[i]->get_id();
                pthread_getcpuclockid(id, &threadClockId);
                clock_gettime(threadClockId, &thread_time);
                u64 usage = thread_time.tv_sec * 1'000'000'000 + thread_time.tv_nsec;
                cpu[i] = (usage - used[i]) * 100 / frame;
                used[i] = usage;

                if(log) std::cout << cpu[i] << "% ";
            }
            if(log) std::cout << std::endl;

            int active = -1;
            int min = 0;
            for(int i=0;i<threads;i++) {
                if(cpu[i] < cpu[min]) min = i;
                if(cpu[i] > 80) continue;
                active = i;
                break;
            }

            if(active == -1) active = min;
            if(server->active_loop != active) {
                server->active_loop = active;
                if(log) std::cout << "Active loop: " << active << std::endl;
            }
        }
        #endif
    };

    delete[] used;
    delete[] cpu;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movq 0x8(%rdi), %rax
movslq 0x28(%rax), %r14
movl 0x20(%rax), %eax
movl %eax, -0x2c(%rbp)
leaq (,%r14,8), %r15
testq %r14, %r14
pushq $-0x1
popq %rax
cmovsq %rax, %r15
leaq (,%r14,4), %r12
cmovsq %rax, %r12
movq %r15, %rdi
callq 0xa030
xorl %r13d, %r13d
movq %rax, -0x50(%rbp)
movq %rax, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0xa1a0
movq %r12, %rdi
callq 0xa030
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0xa1a0
callq 0x165a1
testl %r14d, %r14d
cmovgl %r14d, %r13d
movq %r13, -0x40(%rbp)
movq %rax, -0x38(%rbp)
movw $0x2, %r13w
movq %r14, -0x48(%rbp)
movl $0x7a120, %edi          # imm = 0x7A120
callq 0xa3b0
movq %rbx, %rdi
callq 0x165ca
cmpw $0x1, %r13w
jle 0x16457
decl %r13d
jmp 0x16464
movq %rbx, %rdi
callq 0x1667e
movw $0x2, %r13w
cmpl $0x2, %r14d
jl 0x16439
callq 0x165a1
movq %rax, %r14
testb $0x40, -0x2c(%rbp)
je 0x1648b
movq 0x9b51(%rip), %rdi      # 0x1ffd0
leaq 0x32b4(%rip), %rsi      # 0x1973a
callq 0xa2e0
movq %r14, %r12
subq -0x38(%rbp), %r12
movq %r14, -0x38(%rbp)
xorl %r14d, %r14d
cmpq %r14, -0x40(%rbp)
je 0x16517
movq 0x8(%rbx), %rax
movq 0x20050(%rax), %rax
movq (%rax,%r14,8), %rax
movq 0x8(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0xa100
movl -0x30(%rbp), %edi
leaq -0x60(%rbp), %rsi
callq 0xa2a0
imulq $0x3b9aca00, -0x60(%rbp), %rcx # imm = 0x3B9ACA00
addq -0x58(%rbp), %rcx
movq %rcx, %rax
movq -0x50(%rbp), %rsi
subq (%rsi,%r14,8), %rax
imulq $0x64, %rax, %rax
xorl %edx, %edx
divq %r12
movl %eax, (%r15,%r14,4)
movq %rcx, (%rsi,%r14,8)
testb $0x40, -0x2c(%rbp)
je 0x16512
movq 0x9ad4(%rip), %rdi      # 0x1ffd0
movl %eax, %esi
callq 0xa460
movq %rax, %rdi
leaq 0x3233(%rip), %rsi      # 0x19740
callq 0xa2e0
incq %r14
jmp 0x16499
testb $0x40, -0x2c(%rbp)
je 0x16529
movq 0x9aac(%rip), %rdi      # 0x1ffd0
callq 0xa160
xorl %eax, %eax
xorl %r12d, %r12d
movq -0x48(%rbp), %r14
cmpq %rax, -0x40(%rbp)
je 0x1655a
movl (%r15,%rax,4), %ecx
cmpl $0x51, %ecx
jl 0x16557
movslq %r12d, %rsi
movl %eax, %edx
cmpl (%r15,%rsi,4), %ecx
jl 0x1654f
movl %r12d, %edx
incq %rax
movl %edx, %r12d
jmp 0x16532
movl %eax, %r12d
movq 0x8(%rbx), %rax
cmpl %r12d, 0x4(%rax)
je 0x16439
movl %r12d, 0x4(%rax)
testb $0x40, -0x2c(%rbp)
je 0x16439
movq 0x9a53(%rip), %rdi      # 0x1ffd0
leaq 0x31bf(%rip), %rsi      # 0x19743
callq 0xa2e0
movq %rax, %rdi
movl %r12d, %esi
callq 0xa460
movq %rax, %rdi
callq 0xa160
jmp 0x16439