I'm not sure if this is on topic, but I've seen some hardware related questions here so I'll post it here anyway. If it's off topic I'll take it off.
Compile this C code:
int main() {
unsigned long long int i;
for (i = 0; i < 30000000000ull; ++i){}
return 0;
}
with gcc cycleTimer.c
. Then run /usr/bin/time -f "%E %P" ./a.out
.
I get the following output:
1:17.04 99%
Meaning the process takes 77 seconds to complete and uses 99% of a CPUs time. Running cpufreq-info
during the process gives
current CPU frequency is 2.30 GHz
for all four cores. Now create the assembly instructions for cycleTimer.c
with gcc cycleTimer.c -S
, and we get the following assembly code:
.file "cycleTimer.c"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movq $0, -8(%rbp)
jmp .L2
.L3:
addq $1, -8(%rbp)
.L2:
movabsq $29999999999, %rax
cmpq %rax, -8(%rbp)
jbe .L3
movl $0, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 4.8.2-19ubuntu1) 4.8.2"
.section .note.GNU-stack,"",@progbits
From this, we can see that on every iteration of the loop, four assembly instructions are being run. Therefore, to work out the instructions per second, we do (4*30000000000)/77 = 1558441558
which is 1.6 GHz, much less than 0.99*2.3
which gives 2.28 GHz of the processor that we are actually meant to be using.
Why is there such a big discrepancy? The kernel is saying that 99% of the CPU is being used. Are there any other hidden instructions being computed by the CPU we don't see with gcc -S
? Have I made a mistake somewhere?