// File: /workspace/github_cpp_makefile_star2_O1/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, -0x60(%rbp)
movq %rax, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x81a0
movq %r12, %rdi
callq 0x8030
movq %rax, -0x58(%rbp)
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x81a0
leaq -0x50(%rbp), %r15
xorl %edi, %edi
movq %r15, %rsi
callq 0x82b0
imulq $0x3b9aca00, (%r15), %rax # imm = 0x3B9ACA00
addq 0x8(%r15), %rax
movq %rax, -0x38(%rbp)
movl %r14d, %r13d
movw $0x2, %r14w
movq -0x38(%rbp), %r15
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x83d0
movq %rbx, %rdi
callq 0x1605c
cmpw $0x1, %r14w
jle 0x15e61
decl %r14d
jmp 0x15e6e
movq %rbx, %rdi
callq 0x16138
movw $0x2, %r14w
cmpl $0x2, %r13d
jl 0x15e43
xorl %edi, %edi
leaq -0x50(%rbp), %rsi
callq 0x82b0
imulq $0x3b9aca00, -0x50(%rbp), %rax # imm = 0x3B9ACA00
addq -0x48(%rbp), %rax
movq %rax, -0x38(%rbp)
testb $0x40, -0x2c(%rbp)
je 0x15ead
movl $0x5, %edx
movq 0x812f(%rip), %rdi      # 0x1dfd0
leaq 0x28b8(%rip), %rsi      # 0x18760
callq 0x8350
testl %r13d, %r13d
jle 0x15f48
movq -0x38(%rbp), %r12
subq %r15, %r12
xorl %r15d, %r15d
movq 0x8(%rbx), %rax
movq 0x20050(%rax), %rax
movq (%rax,%r15,8), %rax
movq 0x8(%rax), %rdi
leaq -0x3c(%rbp), %rsi
callq 0x8110
movl -0x3c(%rbp), %edi
leaq -0x50(%rbp), %rsi
callq 0x82b0
imulq $0x3b9aca00, -0x50(%rbp), %rcx # imm = 0x3B9ACA00
addq -0x48(%rbp), %rcx
movq %rcx, %rax
movq -0x60(%rbp), %rsi
subq (%rsi,%r15,8), %rax
imulq $0x64, %rax, %rax
xorl %edx, %edx
divq %r12
movq -0x58(%rbp), %rdx
movl %eax, (%rdx,%r15,4)
movq %rcx, (%rsi,%r15,8)
testb $0x40, -0x2c(%rbp)
je 0x15f3c
movq 0x80af(%rip), %rdi      # 0x1dfd0
movl %eax, %esi
callq 0x8490
movl $0x2, %edx
movq %rax, %rdi
leaq 0x282f(%rip), %rsi      # 0x18766
callq 0x8350
incq %r15
cmpq %r15, %r13
jne 0x15ec0
testb $0x40, -0x2c(%rbp)
je 0x15f7c
movq 0x807b(%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
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %r13d, %r13d
jle 0x15fb3
xorl %ecx, %ecx
xorl %edx, %edx
movq -0x58(%rbp), %r8
movl (%r8,%rcx,4), %esi
movslq %edx, %rdi
movl %ecx, %r12d
cmpl (%r8,%rdi,4), %esi
jl 0x15fa1
movl %edx, %r12d
cmpl $0x51, %esi
jl 0x15fb8
incq %rcx
movl %r12d, %edx
cmpq %rcx, %r13
jne 0x15f8a
jmp 0x15fba
xorl %r12d, %r12d
jmp 0x15fba
movl %ecx, %eax
cmpl $-0x1, %eax
cmovnel %eax, %r12d
movq 0x8(%rbx), %rax
cmpl %r12d, 0x4(%rax)
je 0x15e3f
movl %r12d, 0x4(%rax)
testb $0x40, -0x2c(%rbp)
je 0x15e3f
movl $0xd, %edx
movq 0x7fe7(%rip), %r15      # 0x1dfd0
movq %r15, %rdi
leaq 0x2776(%rip), %rsi      # 0x18769
callq 0x8350
movq %r15, %rdi
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
jmp 0x15e3f