CS 485 Lab 1
VMs, Compiling, and Performance Profiling

In this lab, you will first learn how to access and use the Virtual Machine (VM) that has been set up for you in OpenStack. You will then use your VM to experiment with compiler optimizations and performance profiling.

Due: In class, 29 January, 2016.

Part 1: Accessing Your Virtual Machine (VM)

Each of you has been given your own virtual machine that you will use throughout this class for your assignments. See also the specific instructions for accessing your VM. You can access the console of your VM from a web browser at any time.

(Note: If you are using Mac OS we recommend using Safari or Chrome. Firefox has issues with the mouse).

You can also use ssh or putty to remotely login to your VM. For many of the assignments and labs, remotely logging into your VM is all that is required. For this one you will need the VM console, though.

Because you are the only one using your virtual machine, you will be able to reboot your machine if it hangs or crashes. You will be writing systems and networking software, so it is not unusual to cause the machine to hang or stop responding. In addition to showing you how to login to/access your VM, we will also show you how to reboot your VM should it encounter problems.

Part 2: Compiling and Profiling

In this lab you will experiment with parameters to the gcc C compiler to modify the type of executable that it produces. We will also introduce you to some programs that can help you profile your program to identify where the bottlenecks are. In particular, you will use the gprof and valgrind programs to profile the performance of a simple test program. The instructions for the lab are both shown below and are also contained in the test program that you will be using. You can obtain a copy of the test program by following the link to test.c, or, you can use the wget program:
      wget http://www.cs.uky.edu/~neil/485/labs/1/test.c

The web site http://www.ibm.com/developerworks/library/l-gnuprof.html has a nice description of how to use the gprof utility. Please consult that site for additional information. The following code is derived from code on that page but shows an additional level of procedure calls.

A nice overview of the features and command line options available in gprof can be found at http://web.eecs.umich.edu/~sugih/pointers/gprof_quick.html.

Your goal in this exercise is to use the gcc, gprof, and valgrind programs to analyze the performance of the program below. You should follow the steps below and should save your session in a file called mysession.txt. You should then upload your mysession.txt file to the CS Portal: https://www.cs.uky.edu/csportal.

Steps to Perform

YOU MUST PERFORM THIS EXERCISE ON YOUR VM. Later steps require that you be using the VM console, not ssh.

  1. Start recording your session.
    1. Use the script command (script mysession.txt)
  2. Read through the test program to see what it is doing.
    1. Use one of less, vim, emacs, gedit, or some other program to display the source code.
  3. Compile with varying levels of optimizations and time the runtime
    1. Compile with no optimizations using the -O0 option (gcc -Wall -O0 -o test0 test.c)
      That's capital oh, followed by the digit zero.
    2. Run and time the program running 100 iterations (time ./test0 100)
    3. Run and time the program running 500 iterations (time ./test0 500)
    4. How much longer did it take to run 500 iterations? (echo "test0 took FILL_IN_THE_BLANK times as long to run 500 iterations")
      You don't have to be exact here: the nearest integer is fine. You can use the program bc to do the calculation if you like.
    5. Compile with max optimizations using the -O3 option (gcc -Wall -O3 -o test3 test.c)
    6. Run and time the program running 100 iterations (time ./test3 100)
    7. Run and time the program running 500 iterations (time ./test3 500)
    8. How much longer did it take to run 500 iterations? (echo "test3 took FILL_IN_THE_BLANK times as long to run 500 iterations")
      You don't have to be exact here. If the numbers are so small as to be reported as zero, say "1 times as long".
  4. Profile both versions of the program using gprof
    Note: Do the following steps twice: first with a test program with no optimizations (-O0), and then with a test program with max optimizations (-O3).
    1. Compile with profiling and debugging options (-pg and -g) and with the appropriate optimization. For example, with no optimization it would be gcc -Wall -pg -g -O0 -o test0 test.c
    2. Run the program (./test0 100). This creates the file gmon.out.
    3. Print a "flat profile" (gprof -p ./test0)
    4. Print a "call stack profile" (gprof -q ./test0)
      Note: You are likely to receive an error running gprof on test3. This is expected.
    (Remember to repeat steps 1-4 using maximum optimizations and the program name test3)
  5. Now profile both versions of the program using valgrind
    Note: Again, do the following steps twice: first with test0, then with test3.
    1. Run valgrind to create a callgrind.out profile file (valgrind --tool=callgrind ./test0 100)
    2. Run kcachegrind to view the profile (kcachegrind) This command will not produce output in your mysession.txt file.
      (This step requires the VM console, not ssh.)
  6. Stop recording your session.
    1. Type exit to exit the new shell started by script.
  7. Now open a web browser on your VM and upload mysession.txt to https://www.cs.uky.edu/csportal/