// File: /workspace/github_cpp_makefile_star2_O3/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
movq $-0x1, %rax
cmovsq %rax, %r15
leaq (,%r14,4), %r12
cmovsq %rax, %r12
movq %r15, %rdi
callq 0x8030
movq %rax, -0x58(%rbp)
movq %rax, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x81a0
movq %r12, %rdi
callq 0x8030
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x81a0
leaq -0x40(%rbp), %r12
xorl %edi, %edi
movq %r12, %rsi
callq 0x82b0
imulq $0x3b9aca00, (%r12), %rax # imm = 0x3B9ACA00
addq 0x8(%r12), %rax
movl %r14d, %r13d
movw $0x2, %r14w
movq %r15, -0x48(%rbp)
movq %rax, %r15
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x83d0
movq %rbx, %rdi
callq 0x15f1e
cmpw $0x1, %r14w
jle 0x15d45
decl %r14d
jmp 0x15d52
movq %rbx, %rdi
callq 0x16004
movw $0x2, %r14w
cmpl $0x2, %r13d
jl 0x15d27
xorl %edi, %edi
leaq -0x40(%rbp), %rsi
callq 0x82b0
imulq $0x3b9aca00, -0x40(%rbp), %r12 # imm = 0x3B9ACA00
addq -0x38(%rbp), %r12
testb $0x40, -0x2c(%rbp)
je 0x15d8d
movl $0x5, %edx
movq 0x824f(%rip), %rdi      # 0x1dfd0
leaq 0x29d8(%rip), %rsi      # 0x18760
callq 0x8350
movq %r12, -0x50(%rbp)
subq %r15, %r12
xorl %r15d, %r15d
movq 0x8(%rbx), %rax
movq 0x20050(%rax), %rax
movq (%rax,%r15,8), %rax
movq 0x8(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x8110
movl -0x30(%rbp), %edi
leaq -0x40(%rbp), %rsi
callq 0x82b0
imulq $0x3b9aca00, -0x40(%rbp), %rcx # imm = 0x3B9ACA00
addq -0x38(%rbp), %rcx
movq %rcx, %rax
movq -0x58(%rbp), %rsi
subq (%rsi,%r15,8), %rax
imulq $0x64, %rax, %rax
xorl %edx, %edx
divq %r12
movq -0x48(%rbp), %rdx
movl %eax, (%rdx,%r15,4)
movq %rcx, (%rsi,%r15,8)
testb $0x40, -0x2c(%rbp)
jne 0x15dfb
incq %r15
cmpq %r13, %r15
jne 0x15d97
jmp 0x15e57
movq 0x81ce(%rip), %rdi      # 0x1dfd0
movl %eax, %esi
callq 0x8490
movl $0x2, %edx
movq %rax, %rdi
leaq 0x294e(%rip), %rsi      # 0x18766
callq 0x8350
incq %r15
cmpq %r13, %r15
jne 0x15d97
movq 0x81a0(%rip), %r15      # 0x1dfd0
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x82f0
movsbl %al, %esi
movq %r15, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x81f0
xorl %eax, %eax
xorl %ecx, %ecx
movq -0x48(%rbp), %r15
movl (%r15,%rax,4), %edx
cmpl $0x51, %edx
jl 0x15e84
movslq %ecx, %rsi
movl %eax, %r12d
cmpl (%r15,%rsi,4), %edx
jl 0x15e77
movl %ecx, %r12d
incq %rax
movl %r12d, %ecx
cmpq %rax, %r13
jne 0x15e5f
jmp 0x15e87
movl %eax, %r12d
movq 0x8(%rbx), %rax
cmpl %r12d, 0x4(%rax)
je 0x15eec
movl %r12d, 0x4(%rax)
testb $0x40, -0x2c(%rbp)
je 0x15eec
movl $0xd, %edx
movq 0x8129(%rip), %rdi      # 0x1dfd0
leaq 0x28bb(%rip), %rsi      # 0x18769
callq 0x8350
movq 0x8116(%rip), %rdi      # 0x1dfd0
movl %r12d, %esi
callq 0x8490
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x82f0
movsbl %al, %esi
movq %r12, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x81f0
movq -0x50(%rbp), %rax
jmp 0x15d24