Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC469 Tutorial2 Inline assembly, cycle counters, Gnuplot, LaTeX

Similar presentations

Presentation on theme: "CSC469 Tutorial2 Inline assembly, cycle counters, Gnuplot, LaTeX"— Presentation transcript:

1 CSC469 Tutorial2 Inline assembly, cycle counters, Gnuplot, LaTeX
Bogdan Simion

2 Today’s tutorial Three things you’ll need for the first assignment
Inline assembly for cycle counters (get data) Using Gnuplot (plot data) LaTeX (report)

3 Measuring cycles Idea: Get current value of cycle counter
Compute something Get new value of cycle counter Get elapsed time (in cycles) by subtraction Measuring the cycles elapsed can be done by following these steps: First, …

4 Measurement Code We need inline assembly to implement access_counter.
static u_int64_t start = 0; void access_counter(unsigned* hi, unsigned* low); void start_counter() { unsigned hi, lo; access_counter(&hi, &lo); start = ((u_int64_t)hi << 32) | lo; } u_int64_t get_counter() unsigned ncyc_hi, ncyc_lo; access_counter(&ncyc_hi, &ncyc_lo); return (((u_int64_t)ncyc_hi << 32) | ncyc_lo) - start; We need inline assembly to implement access_counter. In this code, we have a function that starts a counter and a function that retrieves the elapsed time Both of them need to retrieve the current value of the counter. Describe functions … To implement access_counter, we need inline assembly.

5 Inline Assembly Needed for accessing cycle counters
Key point: there is no magic asm() directive tells GCC “emit this assembly code here” You give it a template Same idea as printf() format strings Using the ‘asm’ directive, you can specify GCC to add assembly code in your program. In extended assembler, you can now specify the operands of the instruction using C expressions. You use templates much like the printf format strings.

6 Accessing the Cycle Counter
void access_counter(unsigned *hi, unsigned *lo) { asm volatile ("rdtsc; movl %%edx, %0; movl %%eax, %1" /* Format string */ : "=r" (*hi), "=r" (*lo) /* Output list */ : /* No inputs */ : "%edx", "%eax"); /* Clobber list */ } Code only works on an x86 machine compiling with GCC Emits rdtsc and two movl instructions GCC automatically adds instructions to move symbolic register value %0 into *hi and %1 into *lo. GCC also adds instructions to save and restore the registers that rdtsc clobbers. Careful with newer processors and out-of-order execution: see cpuid, rdtscp instructions. The Time Stamp Counter (TSC) is a 64-bit register present on all x86 processors since the Pentium. It counts the number of cycles since reset. The instruction RDTSC returns the TSC in EDX:EAX. - If our assembly statement must execute where we put it (i.e. must not be reordered by the compiler and put elsewhere), put the keyword “volatile” after “asm” and before the ()’s. asm volatile ( instr : outputs : inputs : clobber_list); - “=r” means output in any regular register (f for floating point register, a-d are eax-edx, S=esi, D=edi) - Clobber list is the list of registers that get clobbered so that the compiler knows about this. - Wiki(rdtsc): Starting with the Pentium Pro, Intel processors have supported out-of-order execution, where instructions are not necessarily performed in the order they appear in the executable. This can cause RDTSC to be executed later than expected, producing a misleading cycle count. This problem can be solved by executing a serializing instruction, such as CPUID, to force every preceding instruction to complete before allowing the program to continue, or by using the RDTSCP instruction, which is a serializing variant of the RDTSC instruction (starting from Intel Core i7 and AMD Athlon64 X2 CPUs).

7 For more information More information about inline assembly available online:

8 Timing with Cycle Counter
Need to convert cycles into time determine clock rate of processor count number of cycles required for some fixed number of seconds Naïve version: This is a bit too simple Assumes sleep() actually sleeps for 10 seconds May be less (if interrupted) or more (if heavy load) See “mhz: Anatomy of a micro-benchmark”, Staelin & McVoy, Usenix Technical Conference 1998 double MHz; int sleep_time = 10; start_counter(); sleep(sleep_time); MHz = get_counter() / (sleep_time * 1e6); Sleep return value: Zero if the requested time has elapsed, OR the number of seconds left to sleep if the call was interrupted by a signal handler. The sleep() function suspends execution of the calling thread until either seconds seconds have elapsed or a signal is delivered to the thread and its action is to invoke a signal-catching function or to terminate the thread or process. System activity may lengthen the sleep by an indeterminate amount.

9 Gnuplot A graphing tool Advantages Scriptable Makes pretty pictures
Easy to learn by example Good documentation: Unlike Excel, it does not use a GUI, but rather declarative statements to create everything you need on a plot/graph. It’s used by the scientific community due to its power to represent some things that are not easy to do in other graphing programs.

10 Using Gnuplot The way to operate with gnuplot …

11 Gnuplot example #!/bin/sh gnuplot << ---EOF--- set title "Activity periods, load = 2" set xlabel "Time (ms)" set nokey set noytics set term postscript eps 10 set size 0.45,0.35 set output "bars.eps" set object 1 rect from 0, 1 to 8.76, 2 fs empty set object 2 rect from 8.76, 1 to 11, 2 fc rgb "black" fs solid set object 3 rect from 11, 1 to 13, 2 fs empty set object 4 rect from 13, 1 to 14, 2 fc rgb "black" fs solid set object 5 rect from 14, 1 to 19, 2 fs empty set object 6 rect from 19, 1 to 31, 2 fc rgb "black" fs solid set object 7 rect from 31, 1 to 33, 2 fs empty set object 8 rect from 33, 1 to 34, 2 fc rgb "black" fs solid plot [0:40] [0:3] 0 ---EOF--- “plot” range – [0:40] on x axis, [0:3] on y axis; “splot” for surface – specify x,y,z ranges Either input file or function to be plotted; Using lines, bars, dots, etc e.g., plot [0:100] "filename" using 1:2 with lines plot [-3:5] asin(x), acos(x) splot [0:10][0:10][0:5] "input" using 1:2:3 with dots

12 Title, Key, etc. title - the main title
set title “Activity periods, load = 2” set xlabel “Time (ms)” set nokey set noytics title - the main title xlabel - the label of the x-axis nokey - no legend noytics - don’t print numbers on the y-axis, since we aren’t using it

13 Output Produce a postscript file called “bars.eps”
set term postscript eps 10 set size 0.45, 0.35 set output “bars.eps” Produce a postscript file called “bars.eps” size - width and height of chart More options: landscape/portrait, color, font, etc.

14 Drawing Rectangles Solid rectangle, <fill> is:
set object N rect from x0, y0 to x1, y1 <fill> Solid rectangle, <fill> is: fc rgb “black” fs solid Empty rectangle, <fill> is: fs empty N = object ID -> can add properties later with another “set object N …” instruction Rect = type (rectangle)

15 So … Write your program with cycle counters
Print active and inactive times to a log file Write a script to read your logs Scale the time durations Output a sequence of rectangles Write the rectangles into boilerplate Gnuplot script

16 Gnuplot example A contrived example of automating running an experiment, parsing the output, and generating a plot. See README.txt for details.

17 LaTeX A markup language, like HTML Easiest to learn by example
There’s lots of online documentation:

18 LaTeX – General Structure
\documentclass[options]{class} \usepackage[pkgs] definitions \begin{document} text \end{document}

19 LaTeX - Figure \begin{figure} \centering command optional required
\includegraphics[scale=1.25]{random.eps} \caption{Random periods (red).} \label{fig:random} \end{figure} Includegraphics = command [scale=1.25] = optional {random.eps} = required

20 LaTeX - Compiling Commonly: TWICE ! $ latex report.tex
$ dvips -o report.dvi $ ps2pdf TWICE ! ANYTHING at all that involves a reference – \ref (figures, sections, subsections, etc), page numbers even, all require double compilation, as the correct references are added in a second pass.

21 LaTeX: Compiling BibTex for bibliography:
$ latex report.tex $ bibtex report 1. LaTeX: finds all bib references (\cite), figure entries, sections, etc. => aux file 2. BibTeX: creates an initial set of text for the bib entries => bbl 3. LaTeX: populates the document with bib refs + update aux 4. LaTeX: knows what the correct labels are now and includes them in the final document. At the first latex run, all \cite{...} arguments are written in the file document.aux. At the bibtex run, this information is taken by bibtex and the relevant entries are put into the .bbl file. At the next run of latex, the .bbl file is included and correct labels for \cite{...} commands are written in .aux file Only at the last run, latex knows what the correct labels are and includes them in the document.

22 LaTeX: Compiling OR directly using pdflatex: $ pdflatex report.tex
$ bibtex report

23 GUI Alternatives LyX (Cross-platform): TexnicCenter (Windows)
TexnicCenter (Windows) needs MikTex or TexLive distributions of Latex:

24 Questions?

Download ppt "CSC469 Tutorial2 Inline assembly, cycle counters, Gnuplot, LaTeX"

Similar presentations

Ads by Google