CS 270 Lab 1

VMs, Compiling C programs, and Performance Profiling

This exercise first shows you how to access and use your Virtual Machine (VM). It then has you use your VM to compile a program, including compile-time optimizations, and then how to profile its performance.

Accessing Your Virtual Machine (VM)

You have your own VM to use throughout this course for your assignments. Specific instructions for accessing your VM are here and will also be given out during the lab period. You can access the console of your VM from any web browser at any time. You can also use NoMachine, 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.

Because you are the only one using your VM, you may reboot it 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.

Compiling and Profiling

In this exercise you will experiment with parameters to the gcc C compiler to modify the type of executable that it produces. You will see 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/~raphael/courses/CS270/laboratory1/test.c

The web site http://www.thegeekstuff.com/2012/08/gprof-tutorial has a nice description of how to use the gprof utility. Please consult that site for additional information.

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.

The set of steps you need to perform are:

 * A. Record your session.
 *   1. Use the script command: script mysession.txt. While it is running, you should never look
 *      at mysession.txt, because that observation is also put into the file,
 *      leading to an unbounded file.  If you accidentally exit from the script
 *      program, you can restart it in append mode: script -a mysession.txt 
 * B. 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 program.
 * C. Compile with varying levels of optimizations and time the runtime
 *   1. Compile with no optimizations using the -O0 option (gcc -Wall -O0 -o test test.c)
 *   2. Run and time the program (time ./test 1000)
 *   3. Compile with max optimizations using the -O3 option (gcc -Wall -O3 -o test test.c)
 *   4. Run and time the program (time ./test 1000)
 *   5. Say which is faster (echo "The FILL_IN_THE_BLANK was faster.  It took FILL_IN_THE_BLANK time.")
 *      The echo program simply outputs what you tell it, but that output also
 *      appears in mysession.txt.
 * D. Profile both versions of the program using gprof.
 *    (Do the following steps twice: first with a test program
 *     with no optimizations (-O0), and then with a test program
 *     with max optimizations (-O3).
 *   Steps to use gprof:
 *   1. compile with profiling and debugging options (-p and -g)
 *       and with the appropriate optimization.  For
 *       example, with no optimization it would be gcc -Wall -p -g -O0 -o test test.c
 *   2. run the program (./test 1000)
 *   3. print a "flat profile" (gprof -p -b ./test)
 *   4. print a "call stack profile" (gprof -q -b ./test)
 * (Remember to repeat the above steps using max optimizations)
 * E. Now profile both versions of the program using valgrind.
 *    (Do the following steps twice: first with a test program
 *     with no optimizations (-O0), and then with a test program
 *     with max optimizations (-O3).
 *   Steps to use valgrind/kcachegrind:
 *   1. run valgrind to create a callgrind.out profile file
 *      (valgrind --tool=callgrind ./test 1000)
 *      This step will take about a minute.
 *   2. run kcachegrind to view the profile (kcachegrind)
 *      This command builds a new window and does not produce output in your mysession.txt file.
 * F. Stop recording your session.
 *   1. Type exit.  This command causes script to terminate.
 * G. Now open a web browser on your VM and upload mysession.txt to https://www.cs.uky.edu/csportal.