// File: kentcz[P]rapl-tools/PowerMonitor.cpp
// Function: main
int main(int argc, char *argv[]) {

	// Default settings
	int runtime = 60;         // run for 60 seconds	
	int ms_pause = 100;       // sample every 100ms
	bool use_outfile = false; // no output file
	ofstream outfile;

	/**
	 *  Read Commandline Parameters
	 *
	 *  -p -- pause duration (in milliseconds)
	 *  -o -- write output to a file
	 *  -t -- run time (in sec) 
	 *
	 *  Example: ./a.out -p 500 -o outfile.csv
	 *
	 **/
	char input_char;
	while ((input_char = getopt(argc, argv, "p:o:")) != -1) {
		switch (input_char) {
		case 'p':
			ms_pause = atoi(optarg);
			break;
		case 't':
			runtime = atoi(optarg);
			break;
		case 'o':
			printf("ouput file:%s\n",optarg);
			use_outfile = true;
			outfile.open(optarg, ios::out | ios::trunc);
			break;
		default:
			abort();
		}
	}

	Rapl *rapl = new Rapl();
	while (rapl->total_time() < runtime) {
		usleep(1000 * ms_pause);
		rapl->sample();

		// Write sample to outfile
		if (use_outfile) {
			outfile << rapl->pkg_current_power() << ","
					<< rapl->pp0_current_power() << ","
					<< rapl->pp1_current_power() << ","
					<< rapl->dram_current_power() << ","
					<< rapl->total_time() << endl;
		}

		// Write sample to terminal
		cout << "\33[2K\r" // clear line
				<< "power=" << rapl->pkg_current_power()
				<< "\tTime=" << rapl->current_time();
		cout.flush();
	}

	// Print totals
	cout << endl 
		<< "\tTotal Energy:\t" << rapl->pkg_total_energy() << " J" << endl
		<< "\tAverage Power:\t" << rapl->pkg_average_power() << " W" << endl
		<< "\tTime:\t" << rapl->total_time() << " sec" << endl;

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x380, %rsp            # imm = 0x380
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x3c, -0x14(%rbp)
movl $0x64, -0x18(%rbp)
movb $0x0, -0x19(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x21d0
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x1c5f(%rip), %rdx      # 0x4004
callq 0x2060
movb %al, -0x221(%rbp)
movsbl %al, %eax
cmpl $-0x1, %eax
je 0x249c
movsbl -0x221(%rbp), %eax
movl %eax, -0x244(%rbp)
subl $0x6f, %eax
je 0x2418
jmp 0x23d0
movl -0x244(%rbp), %eax
subl $0x70, %eax
je 0x23ed
jmp 0x23dd
movl -0x244(%rbp), %eax
subl $0x74, %eax
je 0x2404
jmp 0x2492
movq 0x3bcc(%rip), %rax      # 0x5fc0
movq (%rax), %rdi
callq 0x21b0
movl %eax, -0x18(%rbp)
jmp 0x2497
movq 0x3bb5(%rip), %rax      # 0x5fc0
movq (%rax), %rdi
callq 0x21b0
movl %eax, -0x14(%rbp)
jmp 0x2497
movq 0x3ba1(%rip), %rax      # 0x5fc0
movq (%rax), %rsi
leaq 0x1be0(%rip), %rdi      # 0x4009
xorl %eax, %eax
callq 0x2040
jmp 0x2432
movb $0x1, -0x19(%rbp)
movq 0x3b83(%rip), %rax      # 0x5fc0
movq (%rax), %rax
movq %rax, -0x250(%rbp)
movl $0x10, %edi
movl $0x20, %esi
callq 0x2980
movl %eax, -0x248(%rbp)
jmp 0x245e
movl -0x248(%rbp), %edx
movq -0x250(%rbp), %rsi
leaq -0x220(%rbp), %rdi
callq 0x2050
jmp 0x2479
jmp 0x2497
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x230(%rbp)
movl %eax, -0x234(%rbp)
jmp 0x295d
callq 0x2080
jmp 0x2397
movl $0x120, %edi            # imm = 0x120
callq 0x2120
movq %rax, -0x258(%rbp)
jmp 0x24af
movq -0x258(%rbp), %rdi
callq 0x29a0
jmp 0x24bd
movq -0x258(%rbp), %rax
movq %rax, -0x240(%rbp)
movq -0x240(%rbp), %rdi
callq 0x3330
movsd %xmm0, -0x260(%rbp)
jmp 0x24e1
movsd -0x260(%rbp), %xmm1
cvtsi2sdl -0x14(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x2794
movl -0x18(%rbp), %eax
imull $0x3e8, %eax, %edi      # imm = 0x3E8
callq 0x2170
jmp 0x2508
movq -0x240(%rbp), %rdi
callq 0x2e30
jmp 0x2516
testb $0x1, -0x19(%rbp)
je 0x26ce
movq -0x240(%rbp), %rdi
callq 0x3130
movsd %xmm0, -0x268(%rbp)
jmp 0x2536
movsd -0x268(%rbp), %xmm0
leaq -0x220(%rbp), %rdi
callq 0x2210
movq %rax, -0x270(%rbp)
jmp 0x2553
movq -0x270(%rbp), %rdi
leaq 0x1ab7(%rip), %rsi      # 0x4018
callq 0x2110
movq %rax, -0x278(%rbp)
jmp 0x256f
movq -0x240(%rbp), %rdi
callq 0x3180
movsd %xmm0, -0x280(%rbp)
jmp 0x2585
movsd -0x280(%rbp), %xmm0
movq -0x278(%rbp), %rdi
callq 0x2210
movq %rax, -0x288(%rbp)
jmp 0x25a2
movq -0x288(%rbp), %rdi
leaq 0x1a68(%rip), %rsi      # 0x4018
callq 0x2110
movq %rax, -0x290(%rbp)
jmp 0x25be
movq -0x240(%rbp), %rdi
callq 0x31e0
movsd %xmm0, -0x298(%rbp)
jmp 0x25d4
movsd -0x298(%rbp), %xmm0
movq -0x290(%rbp), %rdi
callq 0x2210
movq %rax, -0x2a0(%rbp)
jmp 0x25f1
movq -0x2a0(%rbp), %rdi
leaq 0x1a19(%rip), %rsi      # 0x4018
callq 0x2110
movq %rax, -0x2a8(%rbp)
jmp 0x260d
movq -0x240(%rbp), %rdi
callq 0x3240
movsd %xmm0, -0x2b0(%rbp)
jmp 0x2623
movsd -0x2b0(%rbp), %xmm0
movq -0x2a8(%rbp), %rdi
callq 0x2210
movq %rax, -0x2b8(%rbp)
jmp 0x2640
movq -0x2b8(%rbp), %rdi
leaq 0x19ca(%rip), %rsi      # 0x4018
callq 0x2110
movq %rax, -0x2c0(%rbp)
jmp 0x265c
movq -0x240(%rbp), %rdi
callq 0x3330
movsd %xmm0, -0x2c8(%rbp)
jmp 0x2672
movsd -0x2c8(%rbp), %xmm0
movq -0x2c0(%rbp), %rdi
callq 0x2210
movq %rax, -0x2d0(%rbp)
jmp 0x268f
movq -0x2d0(%rbp), %rdi
movq 0x391b(%rip), %rsi      # 0x5fb8
callq 0x2140
jmp 0x26a4
jmp 0x26ce
movq -0x258(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x230(%rbp)
movl %eax, -0x234(%rbp)
movl $0x120, %esi            # imm = 0x120
callq 0x2130
jmp 0x295d
movq 0x38fb(%rip), %rdi      # 0x5fd0
leaq 0x193e(%rip), %rsi      # 0x401a
callq 0x2110
movq %rax, -0x2d8(%rbp)
jmp 0x26ea
movq -0x2d8(%rbp), %rdi
leaq 0x1928(%rip), %rsi      # 0x4020
callq 0x2110
movq %rax, -0x2e0(%rbp)
jmp 0x2706
movq -0x240(%rbp), %rdi
callq 0x3130
movsd %xmm0, -0x2e8(%rbp)
jmp 0x271c
movsd -0x2e8(%rbp), %xmm0
movq -0x2e0(%rbp), %rdi
callq 0x2210
movq %rax, -0x2f0(%rbp)
jmp 0x2739
movq -0x2f0(%rbp), %rdi
leaq 0x18e0(%rip), %rsi      # 0x4027
callq 0x2110
movq %rax, -0x2f8(%rbp)
jmp 0x2755
movq -0x240(%rbp), %rdi
callq 0x3520
movsd %xmm0, -0x300(%rbp)
jmp 0x276b
movsd -0x300(%rbp), %xmm0
movq -0x2f8(%rbp), %rdi
callq 0x2210
jmp 0x2781
movq 0x3848(%rip), %rdi      # 0x5fd0
callq 0x20a0
jmp 0x278f
jmp 0x24cb
movq 0x3835(%rip), %rdi      # 0x5fd0
movq 0x3816(%rip), %rsi      # 0x5fb8
callq 0x2140
movq %rax, -0x308(%rbp)
jmp 0x27b0
movq -0x308(%rbp), %rdi
leaq 0x1870(%rip), %rsi      # 0x402e
callq 0x2110
movq %rax, -0x310(%rbp)
jmp 0x27cc
movq -0x240(%rbp), %rdi
callq 0x32e0
movsd %xmm0, -0x318(%rbp)
jmp 0x27e2
movsd -0x318(%rbp), %xmm0
movq -0x310(%rbp), %rdi
callq 0x2210
movq %rax, -0x320(%rbp)
jmp 0x27ff
movq -0x320(%rbp), %rdi
leaq 0x1831(%rip), %rsi      # 0x403e
callq 0x2110
movq %rax, -0x328(%rbp)
jmp 0x281b
movq -0x328(%rbp), %rdi
movq 0x378f(%rip), %rsi      # 0x5fb8
callq 0x2140
movq %rax, -0x330(%rbp)
jmp 0x2837
movq -0x330(%rbp), %rdi
leaq 0x17fc(%rip), %rsi      # 0x4041
callq 0x2110
movq %rax, -0x338(%rbp)
jmp 0x2853
movq -0x240(%rbp), %rdi
callq 0x32a0
movsd %xmm0, -0x340(%rbp)
jmp 0x2869
movsd -0x340(%rbp), %xmm0
movq -0x338(%rbp), %rdi
callq 0x2210
movq %rax, -0x348(%rbp)
jmp 0x2886
movq -0x348(%rbp), %rdi
leaq 0x17be(%rip), %rsi      # 0x4052
callq 0x2110
movq %rax, -0x350(%rbp)
jmp 0x28a2
movq -0x350(%rbp), %rdi
movq 0x3708(%rip), %rsi      # 0x5fb8
callq 0x2140
movq %rax, -0x358(%rbp)
jmp 0x28be
movq -0x358(%rbp), %rdi
leaq 0x1789(%rip), %rsi      # 0x4055
callq 0x2110
movq %rax, -0x360(%rbp)
jmp 0x28da
movq -0x240(%rbp), %rdi
callq 0x3330
movsd %xmm0, -0x368(%rbp)
jmp 0x28f0
movsd -0x368(%rbp), %xmm0
movq -0x360(%rbp), %rdi
callq 0x2210
movq %rax, -0x370(%rbp)
jmp 0x290d
movq -0x370(%rbp), %rdi
leaq 0x1742(%rip), %rsi      # 0x405d
callq 0x2110
movq %rax, -0x378(%rbp)
jmp 0x2929
movq -0x378(%rbp), %rdi
movq 0x3681(%rip), %rsi      # 0x5fb8
callq 0x2140
jmp 0x293e
movl $0x0, -0x4(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x2190
movl -0x4(%rbp), %eax
addq $0x380, %rsp            # imm = 0x380
popq %rbp
retq
leaq -0x220(%rbp), %rdi
callq 0x2190
movq -0x230(%rbp), %rdi
callq 0x2200
nopw %cs:(%rax,%rax)
nop