Upload
others
View
7
Download
0
Embed Size (px)
Citation preview
CS101 Introduction to computing
Supplementary Materials
A. Sahu and S. V .RaoDept of Comp. Sc. & Engg.Dept of Comp. Sc. & Engg.
Indian Institute of Technology Guwahati
Outline• Input/Output redirections• Compiling and Linking in Big Projects• Compiling and Linking in Big Projects
–Compiling Multiple Files–Creating Static library –Creating Static library
• Profiling–Function ProfilingFunction Profiling–Line ProfilingVisualizing Profiling–Visualizing Profiling
Input Redirection • If program takes many inputs: Entering inputs every time
• Store the input in a file and use it int main() {int main() {int A[10], S[10]; for (count=0; count<10; count++){
scanf("%f", &A[count]);S[i] = A[count]*A[count];
}printf(“Square of Numbers are:\n”);for (count=0; count<10; count++){
printf(“%d ” S[count]);printf( %d , S[count]);}return 0;
Input Redirection • If program takes many inputs: Entering inputs every time
• Store the input in a file and use it• Also store out of the program by redirectionAlso store out of the program by redirection $ gcc test.c$ / I
4 5 8 12 18 5 8 9 14 20
$ ./a.out < Input.txt Square of Numbers are:
10 values stored in a file Input.txt
16 25 64 144 324 25 64 81 196 400 $ /a out < Input txt > Output txt$ ./a.out < Input.txt > Output.txt
Compiling multiple Files
• $ gcc –c foo.c//foo.cint foo3x(int x){ $ gcc c foo.c
• $ gcc –c bar.c( ){
return 3*x;}
• $ gcc foo.o bar.o}
//bar.ci t i (){ • $./a.outintmain(){
int x;f 3 (10)x=foo3x(10);
printf(“%d”,x);return 0
5
return 0;}
Linker and LoaderLinker and Loader
• Compiler in Action…p$gcc foo.c bar.c –o a.out
foo c bar.cfoo.crun compiler preprocessor
run assembler (as)foo.s bar.s
foo.o bar.olinker
a.out a.out = fully linked executable
What is Linker ?What is Linker ?• Combines multiple relocatable object files• Produces fully linked executable – directly loadable in memory
• How?– Symbol resolution – associating one symbol definition with each symbol reference
–Relocation – relocating different sections of input relocatable files
Object filesObject files• Types –
–Relocatable : Requires linking to create executable
– Executable : Loaded directly into memory for execution
– Shared Objects : Linked dynamically, at run time or load timetime or load time
Linking with Static Libraries• Collection of concatenated object files – stored on disk in a particular format – archive
• An input to Linker– Referenced object files copied to executableReferenced object files copied to executable
libm.afoo o
bar.olibc a
printf.o & fopen.o
foo.o libc.a
linker(ld)
fully linked executable object file
a.out
Creating Static Library i t i (){//b
//foo.cint foo3x(int x){
intmain(){//bar.cint x;f 3 (10)( ){
return 3*x;}
x=foo3x(10);printf(“%d”,x);t 0
• $ gcc ‐c foo.c
} return 0;}
• $ ar rcs libfoo.a foo.o //it create libfoo.a• $ gcc bar c ‐L ‐lfoo$ gcc bar.c L. lfoo• $ ./a.out
10
Dynamic Linking – Shared LibrariesDynamic Linking Shared Libraries• Addresses disadvantages of static librariesg
– Ensures one copy of text & data in memory –Change in shared library does not requireChange in shared library does not require executable to be built againLoaded at run time by dynamic linker at– Loaded at run‐time by dynamic linker, at arbitrary memory address, linked with programs in memoryin memory
–On loading, dynamic linker relocates text & data of shared objectof shared object
Shared Libraries ..(Cntd)Li k t libf (PIC) f b• Linker creates libfoo.so (PIC) from a.o b.o
• a.out – partially executable – dependency on libfoo.so• Dynamic linker maps shared library into program’s address spaceDynamic linker maps shared library into program s address space
Linker
a.o b.o
‐fPICLinker
libfoo.so (position independent bar.o
‐fPIC
Linkershared object)
Partially linked a.outLoader
Dynamic linker
executable – dependency on
libfoo so ylibfoo.sofully linked executable in
memory
Creating Dynamic Library i t i (){//b
//foo.cint foo3x(int x){
intmain(){//bar.cint x;f 3 (10)( ){
return 3*x;}
x=foo3x(10);printf(“%d”,x);t 0
• $gcc ‐c ‐fPIC foo.c
} return 0;}
• $gcc ‐shared ‐Wl,‐soname,libfoo.so.1 ‐o libfoo.so.1 foo.o
• $ gcc bar.c ‐L. –lfoo• $ export LD_LIBRARY_PATH=. • $ ./a.out
13
Basic Optimizations for Serial Code• Scalar Profiling
–Manual Instrumentation (get wall time,Manual Instrumentation (get_wall_time, clock_t)Function and line based profiling (gprof gcov)– Function and line based profiling (gprof, gcov)
–Memory Profiling (valgrind, callgraph)–Hardware Performance Counter (oprofile,likwid)
• Common sense of Optimization–Do less work avoid expensive Ops shrinkDo less work, avoid expensive Ops, shrink working set
14A Sahu
Manual InstrumentationManual Instrumentation
• System Status• System Status–$uptime, $top , $vmstat–$systemmonitor, $gnome‐system‐monitor
• $time ./a.out$time ./a.out– real time/wall clock time
ti d t ti– cpu time and system time– cputime=sys time+usr time
• Using get_wall_time, clock_t15A Sahu
Manual Instrumentation• $time command and Using get wall time• $time command and Using get_wall_time,
#include <time.h>
int main(){clock t t; double Etime;_ ; ;t = clock(); //Do some Work//Do some Workt = clock() - t;Etime=((double)t)/CLOCKS PER SEC;Etime ((double)t)/CLOCKS_PER_SEC;printf(“ETime =%f seconds”,Etime)return 0;
16A Sahu
return 0;}
Profiler: Hotspot AnalyzerProfiler: Hotspot Analyzer• Given a program• Finding out part of the program which takes maximum amount of time
• Optimizing hot‐spot area reduce the execution time significantlyg y
• Suppose a program spend 99% of time in a small function/codesmall function/code– Optimizing that code will result better performance
Function and line based profilingg• GNU profile (gprof)
$gcc –p test c$gcc p test.c$./a.out$gprof ./a.out >&FPprofile.txt$gprof ./a.out &FPprofile.txt
• GNU coverage (gcov)$gcc ‐fprofile‐arcs ‐ftest‐coverage tmp.c$gcc fprofile arcs ftest coverage tmp.c$./a.out$gcov tmp.c$g p
File 'tmp.c'Lines executed:87.50% of 8Creating 'tmp.c.gcov'
Gprof Example#include <stdio.h>void FunA() {int i=0,g=0;while(i++<100000){ g+ i; }
int main(){int iter=5000;
{ g+=i; }}void FunB() {
while(iter--) {FunA();F B()void FunB() {
int i=0,g=0;while(i++<400000)
FunB();}return 0;
{ g+=i; }}
return 0;}
Gprof Example: Flat Profile
Fl filFlat profile:
Each sample counts as 0 01 secondsEach sample counts as 0.01 seconds.% cumulative self self total time seconds seconds calls ms/call ms/call name / /80.26 5.55 5.55 5000 1.11 1.11 FunB20.94 6.99 1.45 5000 0.29 0.29 FunA
Gprof Example: Call GraphCall graphindex % time self children called name
<spontaneous>[1] 100.0 0.00 6.99 main [1]
5 55 0 00 5000/5000 FunB [2]5.55 0.00 5000/5000 FunB [2]1.45 0.00 5000/5000 FunA [3]
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐5.55 0.00 5000/5000 main [1]
[2] 79.3 5.55 0.00 5000 FunB [2]‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
1.45 0.00 5000/5000 main [1][3] 20 7 1 45 0 00 5000 FunA [3][3] 20.7 1.45 0.00 5000 FunA [3]
Gcov output
#include <stdio.h>int main (){
‐: 1:#include <stdio.h>1: 2:int main (){int main (){
int i, total;total = 0;
1: 2:int main (){‐: 3: int i, total;1: 4: total = 0;
for (i = 0; i < 10; i++)total += i;
if (total != 45)
11: 5: for (i = 0; i < 10; i++)10: 6: total += i;1: 7: if (total != 45)( )
printf ("Failure\n");else printf ("Success\n");return 0;
( )#####:8: printf ("Failure\n");1: 9: else printf ("Success\n");1: 10: return 0;return 0;
}1: 10: return 0;‐: 11:}
valgrind• CallGraph, Profiler, Memory Check…
– Many more– Many more – From C code, one can use API of valgrind
• Program analysis tools are useful• Program analysis tools are useful–Bug detectors, Profilers, Visualizers
• Dynamic binary analysis (DBA) tools–Analyse a program’s machine code at run‐time
–Augment original code with analysis code
23
Valgrindid k1(i t ) {void Work1(int n) { int i=0,j=0,k=0; while(i++<n){
while(j++<n){while(k++<n);}}
}}
void Work2(int n) { int i=0; while(i++<n);}void Maneger(int n1, int n2){
Work1(n1); Work2(n2);Work1(n1); Work2(n2); }void Projects1() { Maneger(1000000, 1000);}void Projects2() { Maneger(100, 1000000);}
int main() {Projects1(); Projects2(); return 0;
}
Valgrind: How to useValgrind: How to use
• $gcc ‐pg ‐o Valgrindtest Valgrindtest c$gcc pg o Valgrindtest Valgrindtest.c• $valgrind ‐‐tool=callgrind ./Valgrindtest$k h i d `l ll i d * | il `• $kcachegrind `ls ‐tr callgrind.out.* | tail ‐1`
Valgrind: Call GraphValgrind: Call Graph
Gprof, gcov, valgrindGprof, gcov, valgrind
• All the examples are available in our course websitep
http://jatinga.iitg.ernet.in/~asahu/cs528/CallGraphValgi drind
http://jatinga.iitg.ernet.in/~asahu/cs528/GProfExampleshttp://jatinga iitg ernet in/~asahu/cs528/GcovExamplehttp://jatinga.iitg.ernet.in/ asahu/cs528/GcovExample• Down load and run $sh Run.sh$sh Run.sh
ThanksThanks