// File: /workspace/github_cpp_makefile_star2_O2/shancheng[P]shancheng/db/sqlite_busyhandler_test.cpp
// Function: main
int main(int argc, char* argv[]) {
  po::options_description opts("Options");
  opts.add_options()
    ("threadcount",  po::value<size_t>(), "set thread count")
    ("busytimeout",  po::value<size_t>(), "set busy timeout value (ms)")
    ("debug",                             "debug mode")
    ("help,h",                            "show the help");

  po::variables_map vm;
  po::store(po::parse_command_line(argc, argv, opts), vm);
  po::notify(vm);
  if (vm.count("help") > 0 ) {
    cout << opts << "\n";
    return 0;
  }
  if (vm.count("threadcount") > 0) {
    option.threadcount = vm["threadcount"].as<size_t>();
  }
  if (vm.count("busytimeout") > 0) {
    option.busytimeout = vm["busytimeout"].as<size_t>();
  }
  if (vm.count("debug") > 0) {
    option.debug = true;
  }
  option.Print(cout);

  boost::thread_group threadgroup;
  dbvec.resize(option.threadcount);
  time_t begin = time(NULL);
  for (size_t i = 0; i < option.threadcount; ++i) {
    if (sqlite3_open("concurrencytest.db", &dbvec[i]) != SQLITE_OK) {
      cerr << "Failed to open database\n";
      goto cleanup;
    }
    if (sqlite3_busy_handler(dbvec[i], BusyHandler, NULL) != SQLITE_OK) {
      cerr << "Failed to set busy handler (" << sqlite3_errmsg(dbvec[i]) << ")\n";
      goto cleanup;
    }
  }
  for (size_t i = 0; i < option.threadcount; ++i) {
    threadgroup.create_thread(Job(i, dbvec[i]));
  }
  for (size_t i = 0; ; ++i) {
    sleep(1);
    time_t now = time(NULL);
    cout << i << " IOPS: " << 1000L * total_iocount / (now - begin) << " busy count:" << total_busycount << "\n";
  }
  threadgroup.join_all();

cleanup:
  for (size_t i = 0; i < option.threadcount; ++i) {
    sqlite3_close(dbvec[i]);
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2c8, %rsp            # imm = 0x2C8
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x668b(%rip), %rsi      # 0xe004
leaq 0x30(%rsp), %rdi
leaq 0x1a8(%rsp), %rdx
callq 0x86e4
movq 0xf5fe(%rip), %rax      # 0x16f90
movl (%rax), %edx
movl %edx, %ecx
shrl %ecx
leaq 0x248(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x74e0
leaq 0x30(%rsp), %rdi
callq 0x7748
leaq 0x248(%rsp), %rdi
callq 0x7420
movq %rax, 0x30(%rsp)
callq 0x871f
leaq 0x663a(%rip), %rsi      # 0xe00c
leaq 0x663f(%rip), %rcx      # 0xe018
leaq 0x30(%rsp), %rdi
movq %rax, %rdx
callq 0x76f0
movq %rax, %r14
callq 0x871f
leaq 0x6634(%rip), %rsi      # 0xe029
leaq 0x6639(%rip), %rcx      # 0xe035
movq %r14, %rdi
movq %rax, %rdx
callq 0x76f0
leaq 0x6643(%rip), %rsi      # 0xe051
leaq 0x6642(%rip), %rdx      # 0xe057
movq %rax, %rdi
callq 0x76a0
leaq 0x663e(%rip), %rsi      # 0xe062
leaq 0x663e(%rip), %rdx      # 0xe069
movq %rax, %rdi
callq 0x76a0
leaq 0x1a8(%rsp), %rdi
callq 0x7140
xorps %xmm0, %xmm0
leaq 0x180(%rsp), %r9
movaps %xmm0, (%r9)
movaps %xmm0, 0x10(%r9)
leaq 0x30(%rsp), %rdi
leaq 0x248(%rsp), %rcx
movl %ebp, %esi
movq %rbx, %rdx
xorl %r8d, %r8d
callq 0x8726
leaq 0x30(%rsp), %rdi
leaq 0x1a8(%rsp), %rsi
xorl %edx, %edx
callq 0x7650
leaq 0x30(%rsp), %rdi
callq 0x8cb6
leaq 0x180(%rsp), %rdi
callq 0x87d6
leaq 0x1a8(%rsp), %rdi
callq 0x71e0
leaq 0x65c5(%rip), %rsi      # 0xe072
leaq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x86e4
leaq 0x1b8(%rsp), %rbx
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
callq 0x87e6
movq %rax, %r14
leaq 0x30(%rsp), %rdi
callq 0x7748
testq %r14, %r14
je 0x7b0b
movq 0xf4c6(%rip), %rdi      # 0x16fb0
leaq 0x248(%rsp), %rsi
callq 0x70b0
leaq 0x65e3(%rip), %rsi      # 0xe0e1
movq %rax, %rdi
callq 0x7370
jmp 0x7e04
leaq 0x64fa(%rip), %rsi      # 0xe00c
leaq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x86e4
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
callq 0x87e6
movq %rax, %r14
leaq 0x30(%rsp), %rdi
callq 0x7748
testq %r14, %r14
je 0x7b84
leaq 0x64c5(%rip), %rsi      # 0xe00c
leaq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x86e4
leaq 0x1a8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x73d0
movq %rax, %rdi
callq 0xc9a2
movq (%rax), %rax
movq %rax, 0xf856(%rip)      # 0x173d0
leaq 0x30(%rsp), %rdi
callq 0x7748
leaq 0x649e(%rip), %rsi      # 0xe029
leaq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x86e4
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
callq 0x87e6
movq %rax, %r14
leaq 0x30(%rsp), %rdi
callq 0x7748
testq %r14, %r14
je 0x7bfd
leaq 0x6469(%rip), %rsi      # 0xe029
leaq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x86e4
leaq 0x1a8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x73d0
movq %rax, %rdi
callq 0xc9a2
movq (%rax), %rax
movq %rax, 0xf7e5(%rip)      # 0x173d8
leaq 0x30(%rsp), %rdi
callq 0x7748
leaq 0x644d(%rip), %rsi      # 0xe051
leaq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x86e4
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
callq 0x87e6
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x7748
testq %rbx, %rbx
je 0x7c39
movb $0x1, 0xf7a7(%rip)      # 0x173e0
movq 0xf370(%rip), %rdi      # 0x16fb0
leaq 0x6491(%rip), %rsi      # 0xe0d8
callq 0x7370
movq 0xf35d(%rip), %rdi      # 0x16fb0
leaq 0x6489(%rip), %rsi      # 0xe0e3
callq 0x7370
movq 0xf34a(%rip), %rdi      # 0x16fb0
leaq 0x64b9(%rip), %rsi      # 0xe126
callq 0x7370
movq 0xf757(%rip), %rsi      # 0x173d0
movq %rax, %rdi
callq 0x7250
leaq 0x6459(%rip), %rsi      # 0xe0e1
movq %rax, %rdi
callq 0x7370
movq 0xf319(%rip), %rdi      # 0x16fb0
leaq 0x6497(%rip), %rsi      # 0xe135
callq 0x7370
movq 0xf72e(%rip), %rsi      # 0x173d8
movq %rax, %rdi
callq 0x7250
leaq 0x6428(%rip), %rsi      # 0xe0e1
movq %rax, %rdi
callq 0x7370
movq 0xf2e8(%rip), %rdi      # 0x16fb0
leaq 0x6475(%rip), %rsi      # 0xe144
callq 0x7370
movzbl 0xf705(%rip), %esi      # 0x173e0
movq %rax, %rdi
callq 0x75f0
leaq 0x63f7(%rip), %rsi      # 0xe0e1
movq %rax, %rdi
callq 0x7370
movq 0xf2b7(%rip), %rdi      # 0x16fb0
leaq 0x63e1(%rip), %rsi      # 0xe0e1
callq 0x7370
leaq 0x30(%rsp), %rdi
callq 0x8800
movq 0xf6ba(%rip), %rsi      # 0x173d0
leaq 0xf6cb(%rip), %rdi      # 0x173e8
callq 0x8838
xorl %r13d, %r13d
xorl %edi, %edi
callq 0x7340
movq %rax, 0x28(%rsp)
leaq 0x633f(%rip), %r15      # 0xe077
leaq 0x634b(%rip), %r14      # 0xe08a
movq 0xf2aa(%rip), %rbx      # 0x16ff0
leaq 0x412(%rip), %r12       # 0x815f
xorl %ebp, %ebp
cmpq 0xf67a(%rip), %rbp      # 0x173d0
jae 0x7e32
movq 0xf685(%rip), %rsi      # 0x173e8
addq %r13, %rsi
movq %r15, %rdi
callq 0x7640
testl %eax, %eax
jne 0x7dcf
movq 0xf66f(%rip), %rax      # 0x173e8
movq (%rax,%r13), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x73c0
testl %eax, %eax
jne 0x7d94
incq %rbp
addq $0x8, %r13
jmp 0x7d4f
movq 0xf255(%rip), %rdi      # 0x16ff0
leaq 0x6301(%rip), %rsi      # 0xe0a3
callq 0x7370
movq %rax, %rbx
movq 0xf637(%rip), %rax      # 0x173e8
movq (%rax,%r13), %rdi
callq 0x7360
movq %rbx, %rdi
movq %rax, %rsi
callq 0x7370
movq %rax, %rbx
leaq 0x62f1(%rip), %r14      # 0xe0c0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7370
xorl %ebx, %ebx
cmpq 0xf5ed(%rip), %rbx      # 0x173d0
jae 0x7dfa
movq 0xf5fc(%rip), %rax      # 0x173e8
movq (%rax,%rbx,8), %rdi
callq 0x7190
incq %rbx
jmp 0x7ddc
leaq 0x30(%rsp), %rdi
callq 0x886a
leaq 0x1a8(%rsp), %rdi
callq 0x88c0
leaq 0x248(%rsp), %rdi
callq 0x88f2
xorl %eax, %eax
addq $0x2c8, %rsp            # imm = 0x2C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r15d, %r15d
xorl %r12d, %r12d
cmpq 0xf591(%rip), %r12      # 0x173d0
jae 0x7f8e
movq 0xf59c(%rip), %rax      # 0x173e8
movq (%rax,%r12,8), %r14
leaq 0x48(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0xcdee
pushq $0x10
popq %rdi
callq 0x7380
movq %rax, %rbx
movl $0x150, %edi            # imm = 0x150
callq 0x7380
movq %rax, %r13
movq %rax, %rdi
callq 0xd878
movq %r15, %rax
sarq $0x20, %rax
leaq 0xee1b(%rip), %rcx      # 0x16ca8
movq %rcx, (%r13)
movq %rax, 0x138(%r13)
movq %r14, 0x140(%r13)
andq $0x0, 0x148(%r13)
movq %r13, (%rbx)
movq %rbx, %r14
addq $0x8, %r14
andq $0x0, 0x8(%rbx)
pushq $0x18
popq %rdi
callq 0x7380
movq %rax, %rbp
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x8(%rbp)
leaq 0xee1a(%rip), %rax      # 0x16cf0
movq %rax, (%rbp)
movq %r13, 0x10(%rbp)
movq %rbp, (%r14)
andq $0x0, 0x8(%rsp)
leaq 0x8(%rsp), %rdi
callq 0x9556
movq 0x10(%r13), %rax
testq %rax, %rax
je 0x7f15
movl 0x8(%rax), %eax
testl %eax, %eax
jne 0x7f3e
movq (%r14), %rbp
movq %r13, 0x8(%rsp)
movq %rbp, 0x10(%rsp)
testq %rbp, %rbp
jne 0x7f1f
jmp 0x7f23
movq %r13, 0x8(%rsp)
movq %rbp, 0x10(%rsp)
lock
incl 0x8(%rbp)
addq $0x8, %r13
movq %r13, %rdi
leaq 0x8(%rsp), %rsi
callq 0xdb2e
leaq 0x10(%rsp), %rdi
callq 0x9556
movq %rbx, %rdi
callq 0xd83e
movq %rbx, 0x18(%rsp)
movq %rbx, 0x8(%rsp)
leaq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0xcdc0
andq $0x0, 0x18(%rsp)
leaq 0x18(%rsp), %rdi
callq 0xcdcc
leaq 0x20(%rsp), %rdi
callq 0xcdda
incq %r12
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %r15
jmp 0x7e38
xorl %ebx, %ebx
movq 0xf019(%rip), %r14      # 0x16fb0
leaq 0x6125(%rip), %r15      # 0xe0c3
leaq 0x6126(%rip), %r12      # 0xe0cb
leaq 0x6135(%rip), %r13      # 0xe0e1
pushq $0x1
popq %rdi
callq 0x7290
xorl %edi, %edi
callq 0x7340
movq %rax, %rbp
movq %r14, %rdi
movq %rbx, %rsi
callq 0x7250
movq %rax, %rdi
movq %r15, %rsi
callq 0x7370
movq %rax, %rdi
movl 0xf423(%rip), %eax      # 0x17400
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
subq 0x28(%rsp), %rbp
xorl %edx, %edx
idivq %rbp
movq %rax, %rsi
callq 0x7350
movq %rax, %rdi
movq %r12, %rsi
callq 0x7370
movl 0xf3fd(%rip), %esi      # 0x17404
movq %rax, %rdi
callq 0x7250
movq %rax, %rdi
movq %r13, %rsi
callq 0x7370
incq %rbx
jmp 0x7fac
jmp 0x8130
jmp 0x8124
jmp 0x8124
jmp 0x8130
jmp 0x8124
jmp 0x80dc
jmp 0x8124
jmp 0x80dc
jmp 0x8124
jmp 0x80dc
jmp 0x8124
jmp 0x80dc
jmp 0x80dc
jmp 0x8130
movq %rax, %r15
leaq 0x18(%rsp), %rdi
callq 0xcdcc
jmp 0x80cc
movq %rax, %r15
jmp 0x8095
movq %rax, %rdi
callq 0x7130
movq (%r13), %rax
movq %r13, %rdi
callq *0x8(%rax)
callq 0x7530
movq %rax, %r15
callq 0x75d0
movq %r14, %rdi
callq 0x9556
jmp 0x80bc
movq %rax, %rdi
callq 0x862a
movq %rax, %r15
movl $0x150, %esi            # imm = 0x150
movq %r13, %rdi
callq 0x73a0
jmp 0x80bc
movq %rax, %r15
pushq $0x10
popq %rsi
movq %rbx, %rdi
callq 0x73a0
jmp 0x80cc
movq %rax, %r15
leaq 0x20(%rsp), %rdi
callq 0xcdda
jmp 0x8133
jmp 0x8130
jmp 0x8130
movq %rax, %r15
leaq 0x30(%rsp), %rdi
callq 0x7748
jmp 0x813d
jmp 0x8124
movq %rax, %r15
leaq 0x30(%rsp), %rdi
callq 0x8cb6
jmp 0x80ff
movq %rax, %r15
leaq 0x180(%rsp), %rdi
callq 0x87d6
jmp 0x813d
jmp 0x8129
movq %rax, %r15
leaq 0x30(%rsp), %rdi
callq 0x7748
jmp 0x8157
movq %rax, %r15
jmp 0x8157
movq %rax, %r15
jmp 0x813d
movq %rax, %r15
jmp 0x814a
jmp 0x8130
movq %rax, %r15
leaq 0x30(%rsp), %rdi
callq 0x886a
leaq 0x1a8(%rsp), %rdi
callq 0x88c0
leaq 0x248(%rsp), %rdi
callq 0x88f2
movq %r15, %rdi
callq 0x7660