profiling unter linux - hynek.me fileprofiling unter linux von langsam nach schnell hynek...

45
Profiling unter Linux Von langsam nach schnell Hynek Schlawack [email protected] 3. Februar 2010

Upload: vokhuong

Post on 26-Aug-2019

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Profiling unter LinuxVon langsam nach schnell

Hynek Schlawack

[email protected]

3. Februar 2010

Page 2: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Ich

2004 Praktika bei u.A. Netopsystem AG (jetzt NOSMicro Ltd.)

seit 2004 mit Unterbrechungen bei Variomedia AG2006 Diplomarbeit über Analyse & Optimierung

...Anhänger von effektiver SoftwareC-Fanboy

Page 3: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Fragestellung

1. Warum ist meine Software langsam?2. ...und was kann ich dagegen tun?

Page 4: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Was macht Software langsam?

I NachlässigkeitI ÜbereifrigkeitI Schlechte KommunikationI Schachteln(Schachteln(Schachteln()))

Page 5: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Ultimatives Beispiel für Nachlässigkeit

for (int i = 0; i < 42; i++)v[i] += atoi(str);

Page 6: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Beispiele für Übereifrigkeit

I Mega-APIsI XML für allesI Feature creepI Hammer-Nagel-SyndromI Voreilige Optimierung

Page 7: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Speicher ist billig, mehr Code ist okay!!!1!

I NeinI CPU-CachesI Speicherbus

Page 8: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Wer sagt das!?

Alan Cox (Linux) via Andi Kleen (dito) am Linux Kongress2006

Speicherzugriff von heute == Festplattenzugriff vongestern.

Page 9: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Zahlen!

mul statt shift 5 TicksBranch falsch vorrausgesagt 10 TicksCache Miss 250 Ticks

Quelle: http://dl.fefe.de/optimizer-isec.pdf

Page 10: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Aber, aber...Optimierung ist doch was Gutes?

Donald Knuth (TAoCP/TeX!) / C. A. R. Hoare (Quicksort!)

Premature optimization isthe root of all evil.

Page 11: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Schön! Und wo kommt Profiling ins Spiel?

Daniel J. Bernstein (qmail, djbdns)

Profile. Don’t speculate.

Page 12: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Spekulation möglich über...

I NotwendigkeitI Ist dieser Code schuld?

I WirksamkeitI Ist der Code nun wirklich schneller?

Page 13: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Spekulative Optimierung

intfoo(int x){return x * 2;

}

intbar(int x){return x << 1;

}

$ gcc -O3 t.c -o t.o -c

Page 14: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Spekulative Optimierung$ objdump -d t.o

00000000 <foo>:0: 55 push %ebp1: 89 e5 mov %esp,%ebp3: 8b 45 08 mov 0x8(%ebp),%eax6: 5d pop %ebp7: 01 c0 add %eax,%eax9: c3 ret

[...]00000010 <bar>:10: 55 push %ebp11: 89 e5 mov %esp,%ebp13: 8b 45 08 mov 0x8(%ebp),%eax16: 5d pop %ebp17: 01 c0 add %eax,%eax19: c3 ret

Compiler besser in Low-Level-Annahmen!

Page 15: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Spekulative Optimierung

voidcopy1(char *dest, char *source, unsigned int length){for (int i = 0; i < length; i++)dest[i] = source[i] ;

}

voidcopy2(char *dest, char *source, unsigned int length){while (length--)*dest++ = *source++;

}

copy1() ist schneller! Besser: Absicht vermitteln!

Page 16: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Profiling ermöglicht...

I ...die Untersuchung wo ein Programm seine Zeit verbringtI ...das Finden von Flaschenhälsen⇒ Notwendigkeit!I ...das Überprüfen der Auswirkungen von Änderungen⇒

Wirksamkeit!

Page 17: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Super!

Page 18: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Ansätze

I Manuell messenI time()/clock()/gettimeofday()I rdtsc

I SamplingI gprofI OProfile

I InstrumentationI CallgrindI (Java/Skriptsprachen)

Page 19: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Manuelles Messen

I am präzisestenI am umständlichstenI Bottlenecks müssen von Hand gefunden werden

Page 20: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

clock()/time()/gettimeofday()

I ISO CI Genauigkeit abhängig von libcI reicht für Ad-Hoc-Messungen

Page 21: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

RDTSC

I Read Time Stamp CounterI Ticks seit letztem ResetI Taktrate für Zeit notwendigI Unportabel⇒ Portable Implementierung:

http://www.fftw.org/cycle.h

Page 22: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

unsigned long longrdtsc(void){unsigned int a, d;__asm__ __volatile__("rdtsc" : "=d" (d),

"=a" (a));

return ((unsigned long long) d << 32) | \((unsigned long long) a);

}

Page 23: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Beispiel für RDTSC: Welche Funktion ist schneller?

voidmeasure(void (*func)(char *, char *, unsigned int)){char *src, *dst;long long a, b;

src = malloc(512); dst = malloc(512);

func(dst, src, 512);a = rdtsc();func(dst, src, 512);b = rdtsc();

printf("%d\n", b-a);

free(src); free(dst);}

Page 24: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Beispiel für RDTSC: Welche Funktion ist schneller?

intmain(void){measure(copy1);measure(copy2);

}

$ gcc t.c -O3 -std=c99$ ./a.out16803180

pWn3d!

Page 25: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Manuelles Messen

I Ideal zum Vergleich von zwei ImplementierungenI Tickszählen aber nur bei hochfrequentieren

Lowlevel-Funktionen sinnvoll!I Big Picture nicht vergessen!

Page 26: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

gprof

I GNU-Projekt (binutils)I SamplingI produziert Callgraphen

Page 27: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

gprof

I Ungenau: nur ca. 100 Samples/sek!I benötigt CompileroptionenI Bonus: Thread FUBAR

Page 28: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Ulrich Drepper (glibc) anno 2002

gprof is uselss in today world(Typo und Oettinger-Englisch beibehalten)

Page 29: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

I KernelerweiterungI SamplingI nutzt CPU-Features

Page 30: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

I kann Kernelcode und Interrupts profilenI einstellbare GranularitätI benötigt root-Zugriff

Page 31: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

I Wird über Daemonprozess “opcontrol” bedientI Überwacht Counter, untersucht in definierten Abständen

alle ProzesseI Anschließend Analyse mittels “opreport” möglich

Page 32: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

- Beispielaufruf

$ opcontrol --reset

$ opcontrol --setup --callgraph=42 \

--event=CPU_CLK_UNHALTED:2200000:0:1:1

$ opcontrol --start

$ ./programm

$ opcontrol --shutdown

Page 33: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

- Beispielausgabe

$ opreport --demangle=smart --symbols ./pingpong_neon

CPU: Athlon, speed 2194.44 MHz (estimated)

Counted CPU_CLK_UNHALTED events (Cycles outside of halt state) \

with a unit mask of 0x00 (No unit mask) count 45000

samples % image name symbol name

6110 15.7726 pingpong_neon progress_engine

3252 8.3949 pingpong_neon NEON_Put

2805 7.2410 pingpong_neon internal_send

[...]

Page 34: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

I Callgraphen auch möglichI Ideal zum Gesamtüberblick über größere SystemeI Hat kaum Einfluss aufs vermessende System

Page 35: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Callgrind

I Virtuelle Maschine durch ValgrindI Einheit: VM-Befehle (Ucode)I Kann Cachenutzung messenI Liefert präzise Callgraphen und Callcounter

Page 36: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Callgrind==20660== Events : Ir Dr Dw I1mr D1mr D1mw I2mr D2mr D2mw

==20660== Collected : 741317 249914 157204 2713 3706 591 1913 1907 436

==20660==

==20660== I refs: 741,317

==20660== I1 misses: 2,713

==20660== L2i misses: 1,913

==20660== I1 miss rate: 0.36%

==20660== L2i miss rate: 0.25%

==20660==

==20660== D refs: 407,118 (249,914 rd + 157,204 wr)

==20660== D1 misses: 4,297 ( 3,706 rd + 591 wr)

==20660== L2d misses: 2,343 ( 1,907 rd + 436 wr)

==20660== D1 miss rate: 1.0% ( 1.4% + 0.3% )

==20660== L2d miss rate: 0.5% ( 0.7% + 0.2% )

==20660==

==20660== L2 refs: 7,010 ( 6,419 rd + 591 wr)

==20660== L2 misses: 4,256 ( 3,820 rd + 436 wr)

==20660== L2 miss rate: 0.3% ( 0.3% + 0.2% )

Page 37: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

KCachegrind

Page 38: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

KCachegrind

Page 39: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Fazit

Allheilmittel gibt es nicht!I gprof ist nutzlosI Bester zeitlicher Überblick: OProfileI Beste Callgraphen/-counter: CallgrindI Genauste Messung: CPU-Tick-Counter

Kombinieren!

Page 40: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Ich habe meinen Bottleneck, was nun?

I Algorithmus/DatenstrukturenI Compiler-Features ausreizenI Inlining/Loop-UnrollingI Assembler (Vektorisierung)

Page 41: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Algorithmen/Datenstrukturen

I Bubblesort⇐⇒ Mergesort⇐⇒ Sortiert einfügenI Arrays⇐⇒ Listen⇐⇒ BäumeI Worst Case⇐⇒ Common Case hinterfragen

Page 42: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Compilerfeatures ausreizen

I Optimierungsoptionen: -O1/2/3/s, -march=ARCH,-ffast-math

I Branchprediction mit __builtin_expect(EXPR, RESULT)

I Automatisch BP: -fprofile-generate/-fprofile-use

Page 43: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Inlining/Loop-Unrolling

I Sprünge minimierenI Nur wenn man weiß, was man tut!I Schmankerl: Duff’s Device

Page 44: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Duff’s Device

voidcopy3(char *to, char *from, unsigned long count){

long n = (count + 7) / 8;switch (count % 8) {case 0: do { *to++ = *from++;case 7: *to++ = *from++;case 6: *to++ = *from++;case 5: *to++ = *from++;case 4: *to++ = *from++;case 3: *to++ = *from++;case 2: *to++ = *from++;case 1: *to++ = *from++;

} while(--n > 0);}

}

Page 45: Profiling unter Linux - hynek.me fileProfiling unter Linux Von langsam nach schnell Hynek Schlawack schlawack@variomedia.de 3. Februar 2010

Zusammenfassung

I Profile. Don’t speculate.I Richtige Werkzeug fürs richtige ProfilingI Versucht nicht zu clever zu sein