// File: /workspace/github_cpp_makefile_star2_O3/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 $0x2b8, %rsp            # imm = 0x2B8
movq %rsi, %rbx
movl %edi, %ebp
leaq 0xb0(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x85fa(%rip), %rsi      # 0x10020
leaq 0x85fa(%rip), %rdx      # 0x10027
leaq 0xa0(%rsp), %rdi
callq 0xa1a2
movq 0x10547(%rip), %rax     # 0x17f88
movl (%rax), %edx
movl %edx, %ecx
shrl %ecx
leaq 0x20(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x74e0
movq 0xa0(%rsp), %rdi
cmpq %r12, %rdi
je 0x7a76
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x73b0
leaq 0x20(%rsp), %rdi
callq 0x7440
movq %rax, 0xa0(%rsp)
xorl %edi, %edi
callq 0xab6d
leaq 0x8592(%rip), %rsi      # 0x10028
leaq 0x8597(%rip), %rcx      # 0x10034
leaq 0xa0(%rsp), %rdi
movq %rax, %rdx
callq 0x7700
movq %rax, %r14
xorl %edi, %edi
callq 0xab6d
leaq 0x8587(%rip), %rsi      # 0x10045
leaq 0x858c(%rip), %rcx      # 0x10051
movq %r14, %rdi
movq %rax, %rdx
callq 0x7700
leaq 0x8596(%rip), %rsi      # 0x1006d
leaq 0x8595(%rip), %rdx      # 0x10073
movq %rax, %rdi
callq 0x76c0
leaq 0x8591(%rip), %rsi      # 0x1007e
leaq 0x8591(%rip), %rdx      # 0x10085
movq %rax, %rdi
callq 0x76c0
leaq 0x218(%rsp), %rdi
callq 0x7150
xorps %xmm0, %xmm0
leaq 0x1f0(%rsp), %r9
movaps %xmm0, (%r9)
movaps %xmm0, 0x10(%r9)
leaq 0xa0(%rsp), %rdi
leaq 0x20(%rsp), %rcx
movl %ebp, %esi
movq %rbx, %rdx
xorl %r8d, %r8d
callq 0x8d8b
leaq 0xa0(%rsp), %rdi
leaq 0x218(%rsp), %rsi
xorl %edx, %edx
callq 0x7670
leaq 0xa0(%rsp), %rdi
callq 0x94d4
leaq 0x1f0(%rsp), %rdi
callq 0x8e86
leaq 0x218(%rsp), %rdi
callq 0x7210
leaq 0xa0(%rsp), %rdi
movq %r12, (%rdi)
leaq 0x8507(%rip), %rsi      # 0x1008e
leaq 0x8504(%rip), %rdx      # 0x10092
callq 0xa1a2
leaq 0x228(%rsp), %rbx
leaq 0xa0(%rsp), %rsi
movq %rbx, %rdi
callq 0xdff8
movq %rax, %r14
leaq 0x230(%rsp), %r15
movq 0xa0(%rsp), %rdi
cmpq %r12, %rdi
je 0x7bd3
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x73b0
cmpq %r15, %r14
je 0x7c02
movq 0x103d1(%rip), %rdi     # 0x17fb0
leaq 0x20(%rsp), %rsi
callq 0x70b0
leaq 0x850d(%rip), %rsi      # 0x100fd
movl $0x1, %edx
movq %rax, %rdi
callq 0x7420
jmp 0x80d3
leaq 0xa0(%rsp), %rdi
movq %r12, (%rdi)
leaq 0x8414(%rip), %rsi      # 0x10028
leaq 0x8418(%rip), %rdx      # 0x10033
callq 0xa1a2
leaq 0xa0(%rsp), %rsi
movq %rbx, %rdi
callq 0xdff8
movq %rax, %r15
movq 0xa0(%rsp), %rdi
cmpq %r12, %rdi
je 0x7c50
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x73b0
cmpq %r14, %r15
je 0x7cb7
leaq 0xa0(%rsp), %rdi
movq %r12, (%rdi)
leaq 0x83c1(%rip), %rsi      # 0x10028
leaq 0x83c5(%rip), %rdx      # 0x10033
callq 0xa1a2
leaq 0x218(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x73e0
movq %rax, %rdi
callq 0xe081
movq (%rax), %rax
movq %rax, 0x10736(%rip)     # 0x183d0
movq 0xa0(%rsp), %rdi
cmpq %r12, %rdi
je 0x7cb7
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x73b0
leaq 0xa0(%rsp), %rdi
movq %r12, (%rdi)
leaq 0x837c(%rip), %rsi      # 0x10045
leaq 0x8380(%rip), %rdx      # 0x10050
callq 0xa1a2
leaq 0xa0(%rsp), %rsi
movq %rbx, %rdi
callq 0xdff8
movq %rax, %r15
movq 0xa0(%rsp), %rdi
cmpq %r12, %rdi
je 0x7d05
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x73b0
cmpq %r14, %r15
je 0x7d6c
leaq 0xa0(%rsp), %rdi
movq %r12, (%rdi)
leaq 0x8329(%rip), %rsi      # 0x10045
leaq 0x832d(%rip), %rdx      # 0x10050
callq 0xa1a2
leaq 0x218(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x73e0
movq %rax, %rdi
callq 0xe081
movq (%rax), %rax
movq %rax, 0x10689(%rip)     # 0x183d8
movq 0xa0(%rsp), %rdi
cmpq %r12, %rdi
je 0x7d6c
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x73b0
leaq 0xa0(%rsp), %rdi
movq %r12, (%rdi)
leaq 0x82ef(%rip), %rsi      # 0x1006d
leaq 0x82ed(%rip), %rdx      # 0x10072
callq 0xa1a2
leaq 0xa0(%rsp), %rsi
movq %rbx, %rdi
callq 0xdff8
movq %rax, %r15
movq 0xa0(%rsp), %rdi
cmpq %r12, %rdi
je 0x7dba
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x73b0
cmpq %r14, %r15
je 0x7dc6
movb $0x1, 0x1061a(%rip)     # 0x183e0
movq 0x101e3(%rip), %rdi     # 0x17fb0
leaq 0x8320(%rip), %rsi      # 0x100f4
movl $0xa, %edx
callq 0x7420
movq 0x101cb(%rip), %rdi     # 0x17fb0
leaq 0x8313(%rip), %rsi      # 0x100ff
movl $0x42, %edx
callq 0x7420
movq 0x101b3(%rip), %rdi     # 0x17fb0
leaq 0x833e(%rip), %rsi      # 0x10142
movl $0xe, %edx
callq 0x7420
movq 0x105bb(%rip), %rsi     # 0x183d0
movq 0x10194(%rip), %rdi     # 0x17fb0
callq 0x7280
leaq 0x82d5(%rip), %rsi      # 0x100fd
movl $0x1, %edx
movq %rax, %rdi
callq 0x7420
movq 0x10174(%rip), %rdi     # 0x17fb0
leaq 0x830e(%rip), %rsi      # 0x10151
movl $0xe, %edx
callq 0x7420
movq 0x10584(%rip), %rsi     # 0x183d8
movq 0x10155(%rip), %rdi     # 0x17fb0
callq 0x7280
leaq 0x8296(%rip), %rsi      # 0x100fd
movl $0x1, %edx
movq %rax, %rdi
callq 0x7420
movq 0x10135(%rip), %rdi     # 0x17fb0
leaq 0x82de(%rip), %rsi      # 0x10160
movl $0xc, %edx
callq 0x7420
movzbl 0x1054d(%rip), %esi     # 0x183e0
movq 0x10116(%rip), %rdi     # 0x17fb0
callq 0x7600
leaq 0x8257(%rip), %rsi      # 0x100fd
movl $0x1, %edx
movq %rax, %rdi
callq 0x7420
movq 0x100f6(%rip), %rdi     # 0x17fb0
leaq 0x823c(%rip), %rsi      # 0x100fd
movl $0x1, %edx
callq 0x7420
leaq 0xa0(%rsp), %rdi
callq 0x8ebe
movq 0x104f1(%rip), %rsi     # 0x183d0
leaq 0x10502(%rip), %rdi     # 0x183e8
callq 0x8f0c
xorl %edi, %edi
callq 0x7360
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x104d1(%rip)     # 0x183d0
je 0x7f6c
xorl %ebp, %ebp
leaq 0x8189(%rip), %r12      # 0x10093
leaq 0x8195(%rip), %r15      # 0x100a6
leaq 0x5d4(%rip), %r13       # 0x84ec
leaq (,%rbp,8), %rsi
addq 0x104c1(%rip), %rsi     # 0x183e8
movq %r12, %rdi
callq 0x7660
testl %eax, %eax
jne 0x8014
movq 0x104aa(%rip), %rax     # 0x183e8
movq (%rax,%rbp,8), %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x73d0
testl %eax, %eax
jne 0x801b
incq %rbp
movq 0x10472(%rip), %rax     # 0x183d0
cmpq %rax, %rbp
jb 0x7f18
testq %rax, %rax
jne 0x8185
xorl %ebx, %ebx
leaq 0x8172(%rip), %r13      # 0x100e7
leaq 0x8181(%rip), %rbp      # 0x100fd
movq 0x18(%rsp), %r14
movl $0x1, %edi
callq 0x72c0
xorl %edi, %edi
callq 0x7360
movq %rax, %r15
movq 0x10014(%rip), %rdi     # 0x17fb0
movq %rbx, %rsi
callq 0x7280
movq %rax, %r12
movl $0x7, %edx
movq %rax, %rdi
leaq 0x8129(%rip), %rsi      # 0x100df
callq 0x7420
movl 0x1043f(%rip), %eax     # 0x18400
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
subq %r14, %r15
xorl %edx, %edx
idivq %r15
movq %r12, %rdi
movq %rax, %rsi
callq 0x7370
movq %rax, %r15
movl $0xc, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x7420
movl 0x10410(%rip), %esi     # 0x18404
movq %r15, %rdi
callq 0x7280
movl $0x1, %edx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x7420
incq %rbx
jmp 0x7f81
movl $0x18, %edx
jmp 0x808e
movq 0xffce(%rip), %r14      # 0x17ff0
leaq 0x8096(%rip), %rsi      # 0x100bf
movl $0x1c, %edx
movq %r14, %rdi
callq 0x7420
movq 0x103ab(%rip), %rax     # 0x183e8
movq (%rax,%rbp,8), %rdi
callq 0x7380
movq %rax, %r15
testq %rax, %rax
je 0x806a
movq %r15, %rdi
callq 0x7160
movq 0xff93(%rip), %rdi      # 0x17ff0
movq %r15, %rsi
movq %rax, %rdx
callq 0x7420
jmp 0x8082
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x7630
movl $0x2, %edx
leaq 0x804e(%rip), %r15      # 0x100dc
movq 0xff5b(%rip), %rdi      # 0x17ff0
movq %r15, %rsi
callq 0x7420
cmpq $0x0, 0x1032b(%rip)     # 0x183d0
je 0x80c6
xorl %r14d, %r14d
movq 0x10337(%rip), %rax     # 0x183e8
movq (%rax,%r14,8), %rdi
callq 0x71a0
incq %r14
cmpq 0x1030c(%rip), %r14     # 0x183d0
jb 0x80aa
leaq 0xa0(%rsp), %rdi
callq 0x8f3e
movq 0xfee6(%rip), %rax      # 0x17fc0
addq $0x10, %rax
leaq 0x288(%rsp), %rdi
movq %rax, -0x70(%rdi)
callq 0x9f76
leaq 0x258(%rsp), %rdi
callq 0xa008
movq %rbx, %rdi
callq 0xa080
leaq 0x88(%rsp), %rbx
movq %rbx, %rdi
callq 0xa112
movq -0x28(%rbx), %rdi
testq %rdi, %rdi
je 0x8149
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x73b0
xorl %eax, %eax
movq %rax, 0x60(%rsp)
movl %eax, 0x68(%rsp)
movq %rax, 0x70(%rsp)
movl %eax, 0x78(%rsp)
movq %rax, 0x80(%rsp)
leaq 0x48(%rsp), %rbx
movq %rbx, %rdi
callq 0xa15a
movq -0x28(%rbx), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x8171
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x73b0
xorl %eax, %eax
addq $0x2b8, %rsp            # imm = 0x2B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r15d, %r15d
xorl %r14d, %r14d
movq 0x10256(%rip), %rax     # 0x183e8
movq (%rax,%r14,8), %rbx
leaq 0xb8(%rsp), %rdi
callq 0xe5ba
movl $0x10, %edi
callq 0x7390
movq %rax, %r13
movl $0x150, %edi            # imm = 0x150
callq 0x7390
movq %rax, %rbp
movq %rax, %rdi
callq 0xf50a
movq %r15, %rax
sarq $0x20, %rax
leaq 0xfacd(%rip), %rcx      # 0x17ca0
movq %rcx, (%rbp)
movq %rax, 0x138(%rbp)
movq %rbx, 0x140(%rbp)
xorl %eax, %eax
movq %rax, 0x148(%rbp)
movq %rbp, (%r13)
movq %r13, %r12
addq $0x8, %r12
movq %rax, 0x8(%r13)
movl $0x18, %edi
callq 0x7390
movq %rax, %rbx
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x8(%rbx)
leaq 0xfac9(%rip), %rax      # 0x17ce8
movq %rax, (%rbx)
movq %rbp, 0x10(%rbx)
movq %rbx, (%r12)
movq $0x0, 0x8(%rsp)
leaq 0x8(%rsp), %rdi
callq 0x9f44
movq 0x10(%rbp), %rax
testq %rax, %rax
je 0x8262
movl 0x8(%rax), %eax
testl %eax, %eax
jne 0x8290
movq (%r12), %rbx
movq %rbp, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
testq %rbx, %rbx
jne 0x826c
jmp 0x8270
movq %rbp, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
lock
incl 0x8(%rbx)
movq %rbp, %rdi
addq $0x10, %rdi
movq %rbp, 0x8(%rbp)
leaq 0x10(%rsp), %rbx
movq %rbx, %rsi
callq 0xf792
movq %rbx, %rdi
callq 0x9f44
movq %r13, %rdi
callq 0xf490
movl $0x18, %edi
callq 0x7390
movq %r13, 0x10(%rax)
movq %rax, %rdi
leaq 0xa0(%rsp), %rsi
callq 0x7050
incq 0xb0(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0xf7d8
incq %r14
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %r15
cmpq 0x100ee(%rip), %r14     # 0x183d0
jb 0x818b
jmp 0x7f6c
jmp 0x8425
jmp 0x8425
jmp 0x839f
movq %rax, %rbx
movq %r13, %rdi
callq 0x9ef4
jmp 0x834e
movq %rax, %rbx
jmp 0x832d
movq %rax, %rdi
callq 0x7140
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x8(%rax)
callq 0x7530
movq %rax, %rbx
callq 0x75e0
movq %r12, %rdi
callq 0x9f44
jmp 0x834e
jmp 0x839f
movq %rax, %rbx
movl $0x150, %esi            # imm = 0x150
movq %rbp, %rdi
callq 0x73b0
jmp 0x834e
movq %rax, %rbx
movl $0x10, %esi
movq %r13, %rdi
callq 0x73b0
leaq 0xb8(%rsp), %rdi
callq 0xf7d8
jmp 0x8434
movq %rax, %rbx
jmp 0x835b
jmp 0x8431
jmp 0x8431
jmp 0x8425
jmp 0x83ac
jmp 0x8425
jmp 0x83ac
jmp 0x8425
jmp 0x83ac
jmp 0x8425
jmp 0x83ac
jmp 0x83ac
jmp 0x8431
movq %rax, %rdi
callq 0x8c3c
jmp 0x8431
movq %rax, %rbx
movq 0xa0(%rsp), %rdi
cmpq %r12, %rdi
je 0x8441
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x73b0
jmp 0x8441
jmp 0x8425
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x94d4
jmp 0x83e9
movq %rax, %rbx
leaq 0x1f0(%rsp), %rdi
callq 0x8e86
jmp 0x8441
jmp 0x842a
movq %rax, %rbx
movq 0xa0(%rsp), %rdi
cmpq %r12, %rdi
je 0x84e4
movq 0xb0(%rsp), %rsi
jmp 0x84dc
movq %rax, %rbx
jmp 0x84e4
jmp 0x8431
movq %rax, %rbx
jmp 0x8441
movq %rax, %rbx
jmp 0x8477
jmp 0x8431
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x8f3e
movq 0xfb78(%rip), %rax      # 0x17fc0
addq $0x10, %rax
leaq 0x288(%rsp), %rdi
movq %rax, -0x70(%rdi)
callq 0x9f76
leaq 0x258(%rsp), %rdi
callq 0xa008
leaq 0x228(%rsp), %rdi
callq 0xa080
leaq 0x88(%rsp), %r14
movq %r14, %rdi
callq 0xa112
movq -0x28(%r14), %rdi
testq %rdi, %rdi
je 0x84bc
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x73b0
xorl %eax, %eax
movq %rax, 0x60(%rsp)
movl %eax, 0x68(%rsp)
movq %rax, 0x70(%rsp)
movl %eax, 0x78(%rsp)
movq %rax, 0x80(%rsp)
leaq 0x48(%rsp), %r14
movq %r14, %rdi
callq 0xa15a
movq -0x28(%r14), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x84e4
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x73b0
movq %rbx, %rdi
callq 0x7680