unexpected minflt(minor page fault) - c++

minor page fault means:need virtual memory map real memory, but I found my test code has some min page fault when access used memory .
my test code :
#include <iostream>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
int main() {
uint64_t len = 1024ll * 128; //128k
uint32_t times = 1000000000;
char *p = new char[len];
for (uint32_t t = 0; t < times; ++t) {
for (uint64_t i = 0; i < len; ++i) {
*(p + i) = 100;
}
}
delete[] p;
return 0;
}
pidstat:
03:40:05 PM UID PID minflt/s majflt/s VSZ RSS %MEM Command
03:40:06 PM 0 42379 34.00 0.00 12672 1196 0.00 main
03:40:07 PM 0 42379 0.00 0.00 12672 1196 0.00 main
03:40:08 PM 0 42379 0.00 0.00 12672 1196 0.00 main
03:40:09 PM 0 42379 0.00 0.00 12672 1196 0.00 main
03:40:10 PM 0 42379 0.00 0.00 12672 1196 0.00 main
03:40:11 PM 0 42379 0.00 0.00 12672 1196 0.00 main
03:40:12 PM 0 42379 0.00 0.00 12672 1196 0.00 main
03:40:13 PM 0 42379 0.00 0.00 12672 1196 0.00 main
03:40:14 PM 0 42379 34.00 0.00 12672 1196 0.00 main
03:40:15 PM 0 42379 0.00 0.00 12672 1196 0.00 main
03:40:16 PM 0 42379 0.00 0.00 12672 1196 0.00 main
03:40:17 PM 0 42379 0.00 0.00 12672 1196 0.00 main
03:40:18 PM 0 42379 0.00 0.00 12672 1196 0.00 main
03:40:19 PM 0 42379 0.00 0.00 12672 1196 0.00 main
03:40:20 PM 0 42379 0.00 0.00 12672 1196 0.00 main
03:40:21 PM 0 42379 0.00 0.00 12672 1196 0.00 main
03:40:22 PM 0 42379 0.00 0.00 12672 1196 0.00 main
03:40:23 PM 0 42379 0.00 0.00 12672 1196 0.00 main
03:40:24 PM 0 42379 0.00 0.00 12672 1196 0.00 main
03:40:25 PM 0 42379 0.00 0.00 12672 1196 0.00 main
03:40:26 PM 0 42379 0.00 0.00 12672 1196 0.00 main
03:40:27 PM 0 42379 0.00 0.00 12672 1196 0.00 main
03:40:28 PM 0 42379 34.00 0.00 12672 1196 0.00 main
03:40:29 PM 0 42379 0.00 0.00 12672 1196 0.00 main
03:40:30 PM 0 42379 0.00 0.00 12672 1196 0.00 main
minflt/s:34 = 34 * 4K = 136K
my array in test code = 128K
Why dost my test code product minflt when access used memory?

Related

Compilation time profiling: What is the "phase opt and generate" stage and how can I speed it up (-ftime-report)

I am profiling the compilation time of my code to determine why the compile time is so slow. I am using gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0 and have added the compiler flag -ftime-report.
What I notice is that the compilation units that are slow to compile spend a majority of time on the phase opt and generate stage. What exactly is this stage? How can I reduce the time taken by this phase.
For reference, this is what the output for one of the compilation units looks like.
Time variable usr sys wall GGC
phase setup : 0.00 ( 0%) 0.00 ( 0%) 0.00 ( 0%) 1579 kB ( 0%)
phase parsing : 1.74 ( 20%) 0.71 ( 44%) 2.46 ( 24%) 311927 kB ( 36%)
phase lang. deferred : 1.33 ( 15%) 0.34 ( 21%) 1.67 ( 16%) 259524 kB ( 30%)
phase opt and generate : 5.68 ( 65%) 0.58 ( 36%) 6.26 ( 60%) 301021 kB ( 34%)
phase last asm : 0.00 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 2 kB ( 0%)
|name lookup : 0.44 ( 5%) 0.12 ( 7%) 0.49 ( 5%) 15499 kB ( 2%)
|overload resolution : 0.76 ( 9%) 0.22 ( 13%) 0.92 ( 9%) 130607 kB ( 15%)
garbage collection : 0.33 ( 4%) 0.01 ( 1%) 0.34 ( 3%) 0 kB ( 0%)
dump files : 0.18 ( 2%) 0.04 ( 2%) 0.10 ( 1%) 0 kB ( 0%)
callgraph construction : 0.12 ( 1%) 0.03 ( 2%) 0.14 ( 1%) 6318 kB ( 1%)
callgraph optimization : 0.16 ( 2%) 0.04 ( 2%) 0.19 ( 2%) 82 kB ( 0%)
ipa function summary : 0.02 ( 0%) 0.00 ( 0%) 0.02 ( 0%) 2289 kB ( 0%)
ipa dead code removal : 0.01 ( 0%) 0.00 ( 0%) 0.03 ( 0%) 0 kB ( 0%)
ipa inheritance graph : 0.00 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 29 kB ( 0%)
ipa virtual call target : 0.02 ( 0%) 0.00 ( 0%) 0.00 ( 0%) 3 kB ( 0%)
ipa cp : 0.01 ( 0%) 0.00 ( 0%) 0.00 ( 0%) 1140 kB ( 0%)
ipa inlining heuristics : 0.04 ( 0%) 0.00 ( 0%) 0.04 ( 0%) 2438 kB ( 0%)
ipa function splitting : 0.00 ( 0%) 0.01 ( 1%) 0.01 ( 0%) 451 kB ( 0%)
ipa profile : 0.01 ( 0%) 0.00 ( 0%) 0.00 ( 0%) 0 kB ( 0%)
ipa pure const : 0.02 ( 0%) 0.00 ( 0%) 0.05 ( 0%) 40 kB ( 0%)
ipa icf : 0.01 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 4 kB ( 0%)
ipa SRA : 0.10 ( 1%) 0.00 ( 0%) 0.05 ( 0%) 9838 kB ( 1%)
cfg cleanup : 0.08 ( 1%) 0.01 ( 1%) 0.08 ( 1%) 1621 kB ( 0%)
trivially dead code : 0.03 ( 0%) 0.00 ( 0%) 0.06 ( 1%) 0 kB ( 0%)
df scan insns : 0.02 ( 0%) 0.01 ( 1%) 0.05 ( 0%) 18 kB ( 0%)
df multiple defs : 0.02 ( 0%) 0.00 ( 0%) 0.03 ( 0%) 0 kB ( 0%)
df reaching defs : 0.06 ( 1%) 0.00 ( 0%) 0.04 ( 0%) 0 kB ( 0%)
df live regs : 0.19 ( 2%) 0.01 ( 1%) 0.25 ( 2%) 0 kB ( 0%)
df live&initialized regs : 0.05 ( 1%) 0.00 ( 0%) 0.06 ( 1%) 0 kB ( 0%)
df use-def / def-use chains : 0.03 ( 0%) 0.00 ( 0%) 0.00 ( 0%) 0 kB ( 0%)
df reg dead/unused notes : 0.08 ( 1%) 0.00 ( 0%) 0.07 ( 1%) 2152 kB ( 0%)
register information : 0.01 ( 0%) 0.00 ( 0%) 0.02 ( 0%) 0 kB ( 0%)
alias analysis : 0.03 ( 0%) 0.00 ( 0%) 0.09 ( 1%) 5413 kB ( 1%)
alias stmt walking : 0.08 ( 1%) 0.00 ( 0%) 0.13 ( 1%) 738 kB ( 0%)
register scan : 0.00 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 167 kB ( 0%)
rebuild jump labels : 0.01 ( 0%) 0.00 ( 0%) 0.00 ( 0%) 0 kB ( 0%)
preprocessing : 0.15 ( 2%) 0.21 ( 13%) 0.39 ( 4%) 11918 kB ( 1%)
parser (global) : 0.29 ( 3%) 0.21 ( 13%) 0.51 ( 5%) 105494 kB ( 12%)
parser struct body : 0.18 ( 2%) 0.04 ( 2%) 0.22 ( 2%) 39504 kB ( 5%)
parser enumerator list : 0.01 ( 0%) 0.01 ( 1%) 0.00 ( 0%) 1305 kB ( 0%)
parser function body : 0.18 ( 2%) 0.04 ( 2%) 0.15 ( 1%) 9096 kB ( 1%)
parser inl. func. body : 0.27 ( 3%) 0.02 ( 1%) 0.39 ( 4%) 33105 kB ( 4%)
parser inl. meth. body : 0.21 ( 2%) 0.06 ( 4%) 0.25 ( 2%) 23541 kB ( 3%)
template instantiation : 1.61 ( 18%) 0.43 ( 26%) 2.05 ( 20%) 346006 kB ( 40%)
constant expression evaluation : 0.05 ( 1%) 0.03 ( 2%) 0.02 ( 0%) 1470 kB ( 0%)
early inlining heuristics : 0.00 ( 0%) 0.01 ( 1%) 0.03 ( 0%) 3751 kB ( 0%)
inline parameters : 0.06 ( 1%) 0.02 ( 1%) 0.05 ( 0%) 12991 kB ( 1%)
integration : 0.12 ( 1%) 0.04 ( 2%) 0.26 ( 3%) 53810 kB ( 6%)
tree gimplify : 0.06 ( 1%) 0.02 ( 1%) 0.11 ( 1%) 20691 kB ( 2%)
tree eh : 0.02 ( 0%) 0.00 ( 0%) 0.02 ( 0%) 2821 kB ( 0%)
tree CFG construction : 0.02 ( 0%) 0.00 ( 0%) 0.00 ( 0%) 8987 kB ( 1%)
tree CFG cleanup : 0.11 ( 1%) 0.02 ( 1%) 0.13 ( 1%) 208 kB ( 0%)
tree tail merge : 0.00 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 880 kB ( 0%)
tree VRP : 0.17 ( 2%) 0.00 ( 0%) 0.18 ( 2%) 7001 kB ( 1%)
tree Early VRP : 0.05 ( 1%) 0.00 ( 0%) 0.05 ( 0%) 7256 kB ( 1%)
tree copy propagation : 0.00 ( 0%) 0.00 ( 0%) 0.05 ( 0%) 104 kB ( 0%)
tree PTA : 0.13 ( 1%) 0.05 ( 3%) 0.25 ( 2%) 1906 kB ( 0%)
tree PHI insertion : 0.01 ( 0%) 0.00 ( 0%) 0.00 ( 0%) 736 kB ( 0%)
tree SSA rewrite : 0.06 ( 1%) 0.01 ( 1%) 0.04 ( 0%) 6289 kB ( 1%)
tree SSA other : 0.00 ( 0%) 0.02 ( 1%) 0.03 ( 0%) 940 kB ( 0%)
tree SSA incremental : 0.08 ( 1%) 0.00 ( 0%) 0.03 ( 0%) 1717 kB ( 0%)
tree operand scan : 0.08 ( 1%) 0.00 ( 0%) 0.08 ( 1%) 19096 kB ( 2%)
dominator optimization : 0.18 ( 2%) 0.01 ( 1%) 0.15 ( 1%) 5240 kB ( 1%)
backwards jump threading : 0.01 ( 0%) 0.00 ( 0%) 0.00 ( 0%) 244 kB ( 0%)
tree SRA : 0.03 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 1712 kB ( 0%)
tree CCP : 0.10 ( 1%) 0.02 ( 1%) 0.10 ( 1%) 1097 kB ( 0%)
tree reassociation : 0.00 ( 0%) 0.01 ( 1%) 0.00 ( 0%) 50 kB ( 0%)
tree PRE : 0.15 ( 2%) 0.01 ( 1%) 0.18 ( 2%) 4977 kB ( 1%)
tree FRE : 0.13 ( 1%) 0.02 ( 1%) 0.12 ( 1%) 2498 kB ( 0%)
tree linearize phis : 0.02 ( 0%) 0.00 ( 0%) 0.03 ( 0%) 563 kB ( 0%)
tree forward propagate : 0.09 ( 1%) 0.00 ( 0%) 0.10 ( 1%) 1071 kB ( 0%)
tree phiprop : 0.00 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 11 kB ( 0%)
tree conservative DCE : 0.04 ( 0%) 0.01 ( 1%) 0.02 ( 0%) 133 kB ( 0%)
tree aggressive DCE : 0.04 ( 0%) 0.01 ( 1%) 0.04 ( 0%) 7238 kB ( 1%)
tree DSE : 0.00 ( 0%) 0.01 ( 1%) 0.03 ( 0%) 254 kB ( 0%)
tree loop invariant motion : 0.00 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 17 kB ( 0%)
scev constant prop : 0.00 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 112 kB ( 0%)
tree loop unswitching : 0.00 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 349 kB ( 0%)
complete unrolling : 0.01 ( 0%) 0.01 ( 1%) 0.03 ( 0%) 1141 kB ( 0%)
tree slp vectorization : 0.01 ( 0%) 0.02 ( 1%) 0.03 ( 0%) 5032 kB ( 1%)
tree iv optimization : 0.02 ( 0%) 0.00 ( 0%) 0.04 ( 0%) 2110 kB ( 0%)
predictive commoning : 0.01 ( 0%) 0.00 ( 0%) 0.02 ( 0%) 302 kB ( 0%)
gimple CSE reciprocals : 0.01 ( 0%) 0.00 ( 0%) 0.00 ( 0%) 0 kB ( 0%)
dominance computation : 0.14 ( 2%) 0.03 ( 2%) 0.16 ( 2%) 0 kB ( 0%)
out of ssa : 0.05 ( 1%) 0.00 ( 0%) 0.01 ( 0%) 55 kB ( 0%)
expand vars : 0.01 ( 0%) 0.00 ( 0%) 0.00 ( 0%) 1422 kB ( 0%)
expand : 0.03 ( 0%) 0.01 ( 1%) 0.10 ( 1%) 14790 kB ( 2%)
post expand cleanups : 0.01 ( 0%) 0.00 ( 0%) 0.00 ( 0%) 1273 kB ( 0%)
varconst : 0.00 ( 0%) 0.00 ( 0%) 0.03 ( 0%) 8 kB ( 0%)
jump : 0.01 ( 0%) 0.00 ( 0%) 0.00 ( 0%) 0 kB ( 0%)
forward prop : 0.02 ( 0%) 0.00 ( 0%) 0.03 ( 0%) 1330 kB ( 0%)
CSE : 0.13 ( 1%) 0.00 ( 0%) 0.08 ( 1%) 664 kB ( 0%)
dead code elimination : 0.00 ( 0%) 0.00 ( 0%) 0.03 ( 0%) 0 kB ( 0%)
dead store elim1 : 0.02 ( 0%) 0.00 ( 0%) 0.06 ( 1%) 1230 kB ( 0%)
dead store elim2 : 0.05 ( 1%) 0.00 ( 0%) 0.03 ( 0%) 1584 kB ( 0%)
loop init : 0.11 ( 1%) 0.02 ( 1%) 0.07 ( 1%) 8638 kB ( 1%)
loop versioning : 0.00 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 40 kB ( 0%)
loop invariant motion : 0.00 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 8 kB ( 0%)
CPROP : 0.12 ( 1%) 0.00 ( 0%) 0.06 ( 1%) 3321 kB ( 0%)
PRE : 0.08 ( 1%) 0.00 ( 0%) 0.05 ( 0%) 935 kB ( 0%)
CSE 2 : 0.07 ( 1%) 0.00 ( 0%) 0.08 ( 1%) 333 kB ( 0%)
branch prediction : 0.02 ( 0%) 0.00 ( 0%) 0.03 ( 0%) 1178 kB ( 0%)
combiner : 0.21 ( 2%) 0.00 ( 0%) 0.15 ( 1%) 7070 kB ( 1%)
if-conversion : 0.02 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 464 kB ( 0%)
integrated RA : 0.25 ( 3%) 0.01 ( 1%) 0.30 ( 3%) 20626 kB ( 2%)
LRA non-specific : 0.10 ( 1%) 0.00 ( 0%) 0.09 ( 1%) 1243 kB ( 0%)
LRA virtuals elimination : 0.02 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 834 kB ( 0%)
LRA reload inheritance : 0.01 ( 0%) 0.00 ( 0%) 0.00 ( 0%) 195 kB ( 0%)
LRA create live ranges : 0.11 ( 1%) 0.01 ( 1%) 0.13 ( 1%) 234 kB ( 0%)
LRA hard reg assignment : 0.02 ( 0%) 0.00 ( 0%) 0.02 ( 0%) 0 kB ( 0%)
LRA rematerialization : 0.04 ( 0%) 0.00 ( 0%) 0.02 ( 0%) 0 kB ( 0%)
reload : 0.00 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 0 kB ( 0%)
reload CSE regs : 0.09 ( 1%) 0.00 ( 0%) 0.06 ( 1%) 2212 kB ( 0%)
load CSE after reload : 0.06 ( 1%) 0.00 ( 0%) 0.05 ( 0%) 559 kB ( 0%)
ree : 0.00 ( 0%) 0.00 ( 0%) 0.02 ( 0%) 71 kB ( 0%)
thread pro- & epilogue : 0.03 ( 0%) 0.00 ( 0%) 0.02 ( 0%) 939 kB ( 0%)
peephole 2 : 0.01 ( 0%) 0.00 ( 0%) 0.02 ( 0%) 170 kB ( 0%)
hard reg cprop : 0.00 ( 0%) 0.00 ( 0%) 0.03 ( 0%) 15 kB ( 0%)
scheduling 2 : 0.15 ( 2%) 0.00 ( 0%) 0.16 ( 2%) 894 kB ( 0%)
machine dep reorg : 0.00 ( 0%) 0.00 ( 0%) 0.03 ( 0%) 502 kB ( 0%)
reorder blocks : 0.04 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 1015 kB ( 0%)
shorten branches : 0.02 ( 0%) 0.00 ( 0%) 0.00 ( 0%) 0 kB ( 0%)
final : 0.04 ( 0%) 0.00 ( 0%) 0.03 ( 0%) 3408 kB ( 0%)
straight-line strength reduction : 0.00 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 21 kB ( 0%)
tree loop if-conversion : 0.01 ( 0%) 0.00 ( 0%) 0.00 ( 0%) 203 kB ( 0%)
rest of compilation : 0.10 ( 1%) 0.01 ( 1%) 0.13 ( 1%) 3241 kB ( 0%)
remove unused locals : 0.02 ( 0%) 0.00 ( 0%) 0.08 ( 1%) 3 kB ( 0%)
address taken : 0.04 ( 0%) 0.01 ( 1%) 0.04 ( 0%) 0 kB ( 0%)
TOTAL : 8.75 1.63 10.40 874064 kB
Edit
I had a few people comment asking for the compiler flags, here they are:
-std=c++17 -Wall -Ofast -DNDEBUG -Wno-deprecated-declarations

Make Gcc specify which templates are taking the longest

Gcc can profile itself, and with this you can see which part of the compilation process takes the longest.
A sample output:
Time variable usr sys wall GGC
phase setup : 0.00 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 1369 kB ( 0%)
phase parsing : 5.76 ( 72%) 2.38 ( 87%) 9.27 ( 78%) 554966 kB ( 80%)
phase lang. deferred : 0.50 ( 6%) 0.16 ( 6%) 0.67 ( 6%) 62109 kB ( 9%)
phase opt and generate : 1.58 ( 20%) 0.18 ( 7%) 1.78 ( 15%) 66512 kB ( 10%)
phase last asm : 0.14 ( 2%) 0.02 ( 1%) 0.15 ( 1%) 4587 kB ( 1%)
phase finalize : 0.00 ( 0%) 0.00 ( 0%) 0.02 ( 0%) 0 kB ( 0%)
|name lookup : 0.90 ( 11%) 0.36 ( 13%) 1.71 ( 14%) 17506 kB ( 3%)
|overload resolution : 0.78 ( 10%) 0.24 ( 9%) 1.17 ( 10%) 68510 kB ( 10%)
garbage collection : 0.58 ( 7%) 0.00 ( 0%) 0.79 ( 7%) 0 kB ( 0%)
dump files : 0.07 ( 1%) 0.00 ( 0%) 0.01 ( 0%) 0 kB ( 0%)
callgraph construction : 0.31 ( 4%) 0.02 ( 1%) 0.29 ( 2%) 26559 kB ( 4%)
callgraph optimization : 0.03 ( 0%) 0.00 ( 0%) 0.05 ( 0%) 10 kB ( 0%)
ipa function summary : 0.01 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 412 kB ( 0%)
ipa inlining heuristics : 0.01 ( 0%) 0.01 ( 0%) 0.01 ( 0%) 282 kB ( 0%)
ipa pure const : 0.00 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 26 kB ( 0%)
cfg cleanup : 0.01 ( 0%) 0.00 ( 0%) 0.02 ( 0%) 1 kB ( 0%)
trivially dead code : 0.00 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 2 kB ( 0%)
df scan insns : 0.00 ( 0%) 0.01 ( 0%) 0.01 ( 0%) 2 kB ( 0%)
df live regs : 0.02 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 24 kB ( 0%)
df reg dead/unused notes : 0.01 ( 0%) 0.00 ( 0%) 0.02 ( 0%) 172 kB ( 0%)
register information : 0.01 ( 0%) 0.00 ( 0%) 0.00 ( 0%) 0 kB ( 0%)
alias stmt walking : 0.03 ( 0%) 0.01 ( 0%) 0.00 ( 0%) 241 kB ( 0%)
rebuild jump labels : 0.00 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 0 kB ( 0%)
preprocessing : 0.66 ( 8%) 0.61 ( 22%) 1.53 ( 13%) 45104 kB ( 7%)
parser (global) : 0.78 ( 10%) 0.59 ( 22%) 1.47 ( 12%) 107059 kB ( 16%)
parser struct body : 0.77 ( 10%) 0.18 ( 7%) 1.00 ( 8%) 64460 kB ( 9%)
parser enumerator list : 0.05 ( 1%) 0.02 ( 1%) 0.07 ( 1%) 2628 kB ( 0%)
parser function body : 0.20 ( 3%) 0.10 ( 4%) 0.35 ( 3%) 9952 kB ( 1%)
parser inl. func. body : 0.35 ( 4%) 0.19 ( 7%) 0.62 ( 5%) 25224 kB ( 4%)
parser inl. meth. body : 1.20 ( 15%) 0.28 ( 10%) 1.49 ( 13%) 110313 kB ( 16%)
template instantiation : 1.60 ( 20%) 0.48 ( 18%) 2.55 ( 21%) 172942 kB ( 25%)
constant expression evaluation : 0.10 ( 1%) 0.05 ( 2%) 0.08 ( 1%) 1091 kB ( 0%)
early inlining heuristics : 0.01 ( 0%) 0.00 ( 0%) 0.02 ( 0%) 292 kB ( 0%)
inline parameters : 0.01 ( 0%) 0.01 ( 0%) 0.03 ( 0%) 2592 kB ( 0%)
integration : 0.14 ( 2%) 0.08 ( 3%) 0.11 ( 1%) 8382 kB ( 1%)
tree gimplify : 0.01 ( 0%) 0.00 ( 0%) 0.04 ( 0%) 3581 kB ( 1%)
tree eh : 0.01 ( 0%) 0.00 ( 0%) 0.00 ( 0%) 373 kB ( 0%)
tree CFG construction : 0.00 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 1628 kB ( 0%)
tree CFG cleanup : 0.01 ( 0%) 0.00 ( 0%) 0.04 ( 0%) 10 kB ( 0%)
tree SSA other : 0.00 ( 0%) 0.00 ( 0%) 0.02 ( 0%) 183 kB ( 0%)
tree SSA incremental : 0.00 ( 0%) 0.00 ( 0%) 0.03 ( 0%) 100 kB ( 0%)
tree operand scan : 0.07 ( 1%) 0.00 ( 0%) 0.07 ( 1%) 2924 kB ( 0%)
tree CCP : 0.01 ( 0%) 0.00 ( 0%) 0.03 ( 0%) 118 kB ( 0%)
tree FRE : 0.02 ( 0%) 0.00 ( 0%) 0.00 ( 0%) 363 kB ( 0%)
tree forward propagate : 0.01 ( 0%) 0.00 ( 0%) 0.04 ( 0%) 112 kB ( 0%)
tree aggressive DCE : 0.00 ( 0%) 0.00 ( 0%) 0.02 ( 0%) 152 kB ( 0%)
tree DSE : 0.02 ( 0%) 0.00 ( 0%) 0.00 ( 0%) 28 kB ( 0%)
PHI merge : 0.00 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 4 kB ( 0%)
dominance computation : 0.00 ( 0%) 0.01 ( 0%) 0.01 ( 0%) 0 kB ( 0%)
expand vars : 0.01 ( 0%) 0.00 ( 0%) 0.02 ( 0%) 153 kB ( 0%)
expand : 0.03 ( 0%) 0.00 ( 0%) 0.02 ( 0%) 3103 kB ( 0%)
varconst : 0.01 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 6 kB ( 0%)
forward prop : 0.00 ( 0%) 0.00 ( 0%) 0.02 ( 0%) 176 kB ( 0%)
CSE : 0.01 ( 0%) 0.00 ( 0%) 0.02 ( 0%) 15 kB ( 0%)
dead store elim1 : 0.01 ( 0%) 0.00 ( 0%) 0.00 ( 0%) 117 kB ( 0%)
dead store elim2 : 0.02 ( 0%) 0.00 ( 0%) 0.03 ( 0%) 149 kB ( 0%)
loop init : 0.00 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 647 kB ( 0%)
branch prediction : 0.01 ( 0%) 0.02 ( 1%) 0.02 ( 0%) 229 kB ( 0%)
combiner : 0.03 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 96 kB ( 0%)
integrated RA : 0.02 ( 0%) 0.00 ( 0%) 0.03 ( 0%) 1862 kB ( 0%)
LRA non-specific : 0.01 ( 0%) 0.00 ( 0%) 0.03 ( 0%) 62 kB ( 0%)
LRA create live ranges : 0.01 ( 0%) 0.00 ( 0%) 0.00 ( 0%) 10 kB ( 0%)
reload CSE regs : 0.03 ( 0%) 0.00 ( 0%) 0.02 ( 0%) 101 kB ( 0%)
thread pro- & epilogue : 0.01 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 131 kB ( 0%)
hard reg cprop : 0.01 ( 0%) 0.00 ( 0%) 0.00 ( 0%) 8 kB ( 0%)
machine dep reorg : 0.00 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 7 kB ( 0%)
reg stack : 0.00 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 0 kB ( 0%)
final : 0.02 ( 0%) 0.00 ( 0%) 0.02 ( 0%) 1906 kB ( 0%)
symout : 0.44 ( 6%) 0.07 ( 3%) 0.49 ( 4%) 87737 kB ( 13%)
variable tracking : 0.02 ( 0%) 0.00 ( 0%) 0.03 ( 0%) 901 kB ( 0%)
var-tracking dataflow : 0.08 ( 1%) 0.00 ( 0%) 0.08 ( 1%) 34 kB ( 0%)
var-tracking emit : 0.03 ( 0%) 0.00 ( 0%) 0.02 ( 0%) 1180 kB ( 0%)
initialize rtl : 0.00 ( 0%) 0.00 ( 0%) 0.01 ( 0%) 12 kB ( 0%)
rest of compilation : 0.05 ( 1%) 0.00 ( 0%) 0.01 ( 0%) 289 kB ( 0%)
remove unused locals : 0.00 ( 0%) 0.00 ( 0%) 0.02 ( 0%) 1 kB ( 0%)
address taken : 0.00 ( 0%) 0.00 ( 0%) 0.03 ( 0%) 0 kB ( 0%)
TOTAL : 7.98 2.74 11.90 689554 kB
I would like to know other things however. For example which files are taking the longest to compile? Which functions. And in particular, it seems my compilation bottleneck is template instantiation. I would like to know which templates exactly are taking the longest.
I tried looking this up but all I find is documentation on how to generate the above table.
The table is generated by adding -ftime-report to the g++ flags.

Why does the c++ clock return smaller values after sleeping?

I am trying to measure the performance of parts of my code in order to compare different statistical methods. I noticed that the measured cpu time is significantly lower if I let the thread sleep for some time beforehand. What is going on there? Am I using clock() wrong?
I am on an ubuntu system and using mpic++.
#include <ctime>
#include <chrono>
#include <cmath>
#include <random>
#include <iostream>
#include <thread>
int main(){
//If I include this line then the measured time is 10 times smaller
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
std::default_random_engine generator;
std::normal_distribution<double> distribution = std::normal_distribution<double>(0.0,1.0);
int M= 100000;
double test = 0;
clock_t start = clock();
for(int counter=0;counter<M;counter++){
test+=distribution(generator);
}
clock_t end = clock();
std::cout << "Generated "<<M<<" values in "<<((double) (end - start)) / CLOCKS_PER_SEC<<std::endl;
std::cout<<test;
return 0;
}
If I let the thread sleep then I get:
Generated 100000 values in 0.01637
Otherwise the result is:
Generated 100000 values in 0.134786
Strace results with std::this_thread::sleep_for(std::chrono::milliseconds(1000));:
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ----------------
99.21 0.455606 455606 1 nanosleep
0.51 0.002321 18 130 read
0.06 0.000272 27 10 brk
0.04 0.000203 1 241 mmap
0.04 0.000176 2 101 11 openat
0.03 0.000117 1 178 mprotect
0.03 0.000115 1 90 close
0.02 0.000112 19 6 sched_getaffinity
0.02 0.000111 1 93 fstat
0.02 0.000072 8 9 clone
0.01 0.000053 27 2 prlimit64
0.01 0.000039 20 2 clock_gettime
0.01 0.000028 28 1 getpid
0.01 0.000028 1 24 1 futex
0.00 0.000000 0 2 write
0.00 0.000000 0 8 8 stat
0.00 0.000000 0 15 munmap
0.00 0.000000 0 2 rt_sigaction
0.00 0.000000 0 1 rt_sigprocmask
0.00 0.000000 0 78 78 access
0.00 0.000000 0 1 execve
0.00 0.000000 0 4 getdents
0.00 0.000000 0 1 arch_prctl
0.00 0.000000 0 1 set_tid_address
0.00 0.000000 0 1 set_robust_list
0.00 0.000000 0 1 getrandom
------ ----------- ----------- --------- --------- ----------------
100.00 0.459253 1003 98 total
Result without std::this_thread::sleep_for(std::chrono::milliseconds(1000));:
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ----------------
32.00 0.002080 16 130 read
20.23 0.001315 5 241 mmap
14.00 0.000910 5 178 mprotect
10.15 0.000660 7 101 11 openat
5.41 0.000352 5 78 78 access
4.66 0.000303 3 93 fstat
4.43 0.000288 3 90 close
2.81 0.000183 20 9 clone
1.82 0.000118 10 12 1 futex
1.08 0.000070 5 15 munmap
0.80 0.000052 5 10 brk
0.62 0.000040 7 6 sched_getaffinity
0.57 0.000037 19 2 write
0.43 0.000028 4 8 8 stat
0.38 0.000025 13 2 clock_gettime
0.18 0.000012 3 4 getdents
0.15 0.000010 5 2 prlimit64
0.14 0.000009 9 1 getpid
0.03 0.000002 1 2 rt_sigaction
0.03 0.000002 2 1 arch_prctl
0.03 0.000002 2 1 getrandom
0.02 0.000001 1 1 rt_sigprocmask
0.02 0.000001 1 1 set_tid_address
0.02 0.000001 1 1 set_robust_list
0.00 0.000000 0 1 execve
------ ----------- ----------- --------- --------- ----------------
100.00 0.006501 990 98 total
I found the culprit. I am working in eclipse and had a header with corresponding cpp file still in the same project. TIL that eclipse links files even if I do not include them. This file contains a class with a variable of type dealii::FullMatrix:
//Coefficients.h
#ifndef COEFFICIENTS_H_
#define COEFFICIENTS_H_
#include <deal.II/lac/full_matrix.h>
class Coefficients{
public:
Coefficients(int dim);
protected:
dealii::FullMatrix<double> values;
};
#endif /* COEFFICIENTS_H_ */
In the cpp file the constructor initializes the matrix:
//Coefficients.cpp
#include "Coefficients.h"
Coefficients::Coefficients(int dim):values(dim,dim){};
This somehow resulted in the time difference. For now I just put the constructor in my header file and that seems to solve the issue. I would be very interested if any of you know whats going on there.
Thank you for the discussion and all the interesting answers. A special thanks to n.m. for testing my program.

Code slower despite what gprof says

I've been given a c++ code to optimize, and the first step is to introduce parallelism with OpenMP. I was able to identify several functions that badly needed optimization, so I focused on them.
The problem is that the execution time has been multiplied by about 2, when the profiling files seems to tell me that it should be much faster ..
Here are the gprof profile I get without using OpenMP :
38.07 5.55 5.55 __tcf_0
20.99 8.61 3.06 86196302 0.04 0.04 is_neighbor(int, int, int, int, double)
13.24 10.54 1.93 425940 4.53 4.53 Ellips::data_fiting(double*, int, int, double) const
9.05 11.86 1.32 _fu51___ZSt4cout
5.90 12.72 0.86 5645243 0.15 0.15 Ellips::Ellips()
3.70 13.26 0.54 4013067 0.13 0.13 intersect(Ellips&, Ellips&)
2.40 13.61 0.35 dgemv_
1.44 13.82 0.21 ddot_
1.23 14.00 0.18 141257881 0.00 0.00 Configuration::get_position(int)
1.03 14.15 0.15 __tcf_0
0.82 14.27 0.12 594893 0.20 0.20 Ellips::Ellips(double, double, int, int)
0.41 14.33 0.06 7099 8.45 400.75 Configuration::Configuration(double, double, int, int, int, int, double*, double)
0.34 14.38 0.05 3203279 0.02 0.02 Ellips::operator=(Ellips const&)
0.34 14.43 0.05 ceil
0.21 14.46 0.03 dnrm2_
0.14 14.48 0.02 _fu32___ZSt4cout
0.14 14.50 0.02 dcopy_
0.14 14.52 0.02 dscal_
0.07 14.53 0.01 7775127 0.00 0.00 Configuration::get_Ellips(int)
0.07 14.54 0.01 6239588 0.00 0.00 Ellips::~Ellips()
0.07 14.55 0.01 4349523 0.00 0.00 Configuration::get_data_fit(int)
0.07 14.56 0.01 7097 1.41 1.41 Graph<float, float, float>::maxflow(bool, Block<int>*)
0.07 14.57 0.01 _fu53___ZNSs4_Rep20_S_empty_rep_storageE
0.07 14.58 0.01 floor
0.00 14.58 0.00 432232036 0.00 0.00 Configuration::save_config(std::string)
0.00 14.58 0.00 1180034 0.00 0.00 Ellips::data_fiting(double, double*, double*, double, int, int, double) const
0.00 14.58 0.00 1173980 0.00 0.00 Ellips::get_cx() const
0.00 14.58 0.00 1164513 0.00 0.02 Configuration::add_Ellips(Ellips const&, int, double)
0.00 14.58 0.00 1157360 0.00 0.00 Ellips::get_cy() const
0.00 14.58 0.00 425940 0.00 0.00 shift_cost_exp1(double, double)
0.00 14.58 0.00 23625 0.00 0.00 Graph<float, float, float>::augment(Graph<float, float, float>::arc*)
0.00 14.58 0.00 22504 0.00 0.00 Graph<float, float, float>::process_sink_orphan(Graph<float, float, float>::node*)
0.00 14.58 0.00 21293 0.00 27.35 Configuration::operator=(Configuration const&)
0.00 14.58 0.00 14203 0.00 0.23 Configuration::~Configuration()
0.00 14.58 0.00 14196 0.00 0.00 Configuration::get_nb_Ellipses()
0.00 14.58 0.00 7097 0.00 34.30 Configuration::Configuration(Ellips const&, int, double, int)
0.00 14.58 0.00 7097 0.00 0.00 Graph<float, float, float>::maxflow_init()
0.00 14.58 0.00 7097 0.00 0.00 Graph<float, float, float>::reset()
0.00 14.58 0.00 2406 0.00 0.00 Ellips::get_a() const
0.00 14.58 0.00 2406 0.00 0.00 Ellips::get_b() const
0.00 14.58 0.00 2406 0.00 0.00 Ellips::get_theta() const
0.00 14.58 0.00 1137 0.00 0.00 Graph<float, float, float>::process_source_orphan(Graph<float, float, float>::node*)
0.00 14.58 0.00 7 0.00 38.00 Configuration::Configuration(Configuration const&)
0.00 14.58 0.00 3 0.00 0.32 Configuration::Configuration()
0.00 14.58 0.00 2 0.00 0.00 min_max_val(_IplImage*, double&, double&)
0.00 14.58 0.00 1 0.00 0.00 convert_char_to_double(_IplImage*, double*)
0.00 14.58 0.00 1 0.00 0.00 Graph<float, float, float>::reallocate_nodes(int)
0.00 14.58 0.00 1 0.00 0.00 Graph<float, float, float>::Graph(int, int, void (*)(char*))
And here is the one I get with OpenMP (The code is a recursive algorithm with no real "ending", the two profiles have been obtained after about 7000 iterations of the main loop).
36.57 4.45 4.45 __tcf_0
25.72 7.58 3.13 86434458 0.04 0.04 is_neighbor(int, int, int, int, double)
12.41 9.09 1.51 _fu51___ZSt4cout
7.97 10.06 0.97 5646276 0.17 0.17 Ellips::Ellips()
4.35 10.59 0.53 4020048 0.13 0.13 intersect(Ellips&, Ellips&)
2.47 10.89 0.30 dgemv_
1.73 11.10 0.21 ddot_
1.64 11.30 0.20 141852099 0.00 0.00 Configuration::get_position(int)
1.15 11.44 0.14 7038 19.89 164.95 Configuration::Configuration(double, double, int, int, int, int, double*, double)
1.07 11.57 0.13 589659 0.22 0.22 Ellips::Ellips(double, double, int, int)
0.99 11.69 0.12 __tcf_0
0.90 11.80 0.11 422280 0.26 0.33 Ellips::data_fiting(double*, int, int, double) const
0.74 11.89 0.09 3208793 0.03 0.03 Ellips::operator=(Ellips const&)
0.41 11.94 0.05 ceil
0.25 11.97 0.03 422280 0.07 0.07 shift_cost_exp1(double, double)
0.25 12.00 0.03 GOMP_parallel_end
0.25 12.03 0.03 _fu53___ZNSs4_Rep20_S_empty_rep_storageE
0.16 12.05 0.02 21110 0.95 32.56 Configuration::operator=(Configuration const&)
0.16 12.07 0.02 7036 2.84 2.84 Graph<float, float, float>::maxflow(bool, Block<int>*)
0.16 12.09 0.02 _fu32___ZSt4cout
0.16 12.11 0.02 daxpy_
0.16 12.13 0.02 dnrm2_
0.08 12.14 0.01 1171018 0.01 0.04 Configuration::add_Ellips(Ellips const&, int, double)
0.08 12.15 0.01 GOMP_parallel_start
0.08 12.16 0.01 dcopy_
0.08 12.17 0.01 dgemm_
0.00 12.17 0.00 432088679 0.00 0.00 Configuration::save_config(std::string)
0.00 12.17 0.00 7813683 0.00 0.00 Configuration::get_Ellips(int)
0.00 12.17 0.00 6235383 0.00 0.00 Ellips::~Ellips()
0.00 12.17 0.00 4360587 0.00 0.00 Configuration::get_data_fit(int)
0.00 12.17 0.00 1187310 0.00 0.00 Ellips::data_fiting(double, double*, double*, double, int, int, double) const
0.00 12.17 0.00 1163572 0.00 0.00 Ellips::get_cx() const
0.00 12.17 0.00 1147536 0.00 0.00 Ellips::get_cy() const
0.00 12.17 0.00 35748 0.00 0.00 Graph<float, float, float>::augment(Graph<float, float, float>::arc*)
0.00 12.17 0.00 33436 0.00 0.00 Graph<float, float, float>::process_sink_orphan(Graph<float, float, float>::node*)
0.00 12.17 0.00 14081 0.00 0.00 Configuration::~Configuration()
0.00 12.17 0.00 14074 0.00 0.00 Configuration::get_nb_Ellipses()
0.00 12.17 0.00 7036 0.00 39.10 Configuration::Configuration(Ellips const&, int, double, int)
0.00 12.17 0.00 7036 0.00 0.00 Graph<float, float, float>::maxflow_init()
0.00 12.17 0.00 7036 0.00 0.00 Graph<float, float, float>::reset()
0.00 12.17 0.00 2424 0.00 0.00 Ellips::get_a() const
0.00 12.17 0.00 2424 0.00 0.00 Ellips::get_b() const
0.00 12.17 0.00 2424 0.00 0.00 Ellips::get_theta() const
0.00 12.17 0.00 2355 0.00 0.00 Graph<float, float, float>::process_source_orphan(Graph<float, float, float>::node*)
0.00 12.17 0.00 7 0.00 44.91 Configuration::Configuration(Configuration const&)
0.00 12.17 0.00 3 0.00 0.37 Configuration::Configuration()
0.00 12.17 0.00 2 0.00 0.00 min_max_val(_IplImage*, double&, double&)
0.00 12.17 0.00 1 0.00 0.00 convert_char_to_double(_IplImage*, double*)
0.00 12.17 0.00 1 0.00 0.00 Graph<float, float, float>::reallocate_nodes(int)
0.00 12.17 0.00 1 0.00 0.00 Graph<float, float, float>::Graph(int, int, void (*)(char*))
Is there a problem with how I'm using the profiler ? Or does this come from the code itself ? It takes about 12 seconds to complete 1000 iterations with OpenMP, whereas it takes about 31 seconds with OpenMP (using omp_get_wtime() and not clock())

sed to replace a character from a column with space but retianing the format

I have a file a.pdb as
ATOM 3201 CD2 LEU A 337 7.734 18.538 6.979 0.00 0.00 0.000 C
ATOM 3202 C LEU A 337 5.169 14.358 7.663 0.00 0.00 0.206 C
ATOM 3203 O LEU A 337 4.123 14.537 8.395 0.00 0.00 -0.646 OA
ATOM 3204 OXT LEU A 337 5.124 13.563 6.672 0.00 0.00 -0.646 OA
HETATM 3206 CA CA A 338 18.241 31.994 15.308 0.00 0.00 0.000 CA
HETATM 3207 CA CA A 339 16.703 30.240 22.272 0.00 0.00 0.000 CA
Desired output:
ATOM 3201 CD2 LEU 337 7.735 18.538 6.979 0.00 0.00 0.000 C
ATOM 3202 C LEU 337 5.169 14.358 7.663 0.00 0.00 0.206 C
ATOM 3203 O LEU 337 4.122 14.537 8.395 0.00 0.00 -0.646 OA
ATOM 3204 OXT LEU 337 5.124 13.562 6.671 0.00 0.00 -0.646 OA
HETATM 3206 CA CA 338 18.242 31.994 15.307 0.00 0.00 0.000 CA
HETATM 3207 CA CA 339 16.703 30.240 22.272 0.00 0.00 0.000 CA
How can I replace the letter "A" in the 22nd column or 5th block with space but retaining the format of a.pdb?
sed -r 's/^(.{21})A/\1 /' a.pdb
(.{21}) matches 21 characters and put them in capture group 1, which is then used in the substitution.
Using awk:
awk '$0=substr($0,1,21) FS substr($0,23)' file
$ awk '$0=substr($0,1,21) FS substr($0,23)' file
ATOM 3201 CD2 LEU 337 7.734 18.538 6.979 0.00 0.00 0.000 C
ATOM 3202 C LEU 337 5.169 14.358 7.663 0.00 0.00 0.206 C
ATOM 3203 O LEU 337 4.123 14.537 8.395 0.00 0.00 -0.646 OA
ATOM 3204 OXT LEU 337 5.124 13.563 6.672 0.00 0.00 -0.646 OA
HETATM 3206 CA CA 338 18.241 31.994 15.308 0.00 0.00 0.000 CA
HETATM 3207 CA CA 339 16.703 30.240 22.272 0.00 0.00 0.000 CA
You can use gsub in awk to do this...
awk '{gsub("A"," ",$5); print}' a.pdb