+ - 0:00:00
Notes for current slide
Notes for next slide

A Sampling of Other Things

1 / 21

Today

  • Profiling

  • Static Analysis

  • Developer Surveys

2 / 21

Profiling

What is it?

What's it good for?

When will you use it

(other than 281?)

What are its limitations

3 / 21

Getting started with perf

  • Install the tool
sudo apt install linux-tools-common linux-tools-`uname -r`
4 / 21

Getting started with perf

  • Install the tool
sudo apt install linux-tools-common linux-tools-`uname -r`
  • Write a simple program
int main() {
return 0;
}
  • And profile it
$ make main
cc main.c -o main
$ perf record ./main
$ ls
main main.c perf.data
$ perf report
5 / 21
mmdarden@c4cs-w17:~$ perf
The program 'perf' is currently not installed. You can install it by typing:
sudo apt install linux-tools-common
mmdarden@c4cs-w17:~$ sudo apt install linux-tools-common
mmdarden@c4cs-w17:~$ perf
WARNING: perf not found for kernel 4.4.0-31
You may need to install the following packages for this specific kernel:
linux-tools-4.4.0-31-generic
linux-cloud-tools-4.4.0-31-generic
You may also want to install one of the following packages to keep up to date:
linux-tools-generic
linux-cloud-tools-generic
mmdarden@c4cs-w17:~$ sudo apt install linux-tools-generic
mmdarden@c4cs-w17:~$ sudo apt install linux-tools-`uname -r`
mmdarden@c4cs-w17:~$ perf
usage: perf [--version] [--help] [OPTIONS] COMMAND [ARGS]

Getting something useful from perf

  • Need a program that takes some time
void child() {
int i;
for (i=0; i < 0xFFFFFFF; i++) { // 7 F's
asm("nop;");
}
}
int main() {
int i;
for (i=0; i < 0xFFFFFFF; i++) { // 7 F's
asm("nop;");
}
child();
}
$ make main
cc main.c -o main
$ perf record ./main
$ perf report
6 / 21

Understanding a little how perf works

$ perf record -F1 ./main
$ perf report
$ perf record -F100000 ./main
$ perf report
  • What does -F do?
    • (Try man perf-report, you can use / to search in man)
7 / 21

Bonus: Try perf script (raw list of events)

Can we profile library code?

  • Let's write a lot of 0's
#include <string.h>
...
for (i=0; i < 0xFFFFF; i++) { // 7 F's -> 5 F's
...
char buf[0xFFFF];
...
// asm("nop");
memset(buf, 0, 0xFFFF);
8 / 21

Some libraries are uglier :(

  • Add a printf
#include <stdio.h>
...
for (i=0; i < 0xFFFFF; i++) { // 7 F's -> 5 F's
...
// asm("nop");
printf("%d\n", i);

This can make profiling real code hard

  • Don't go down blind alleys (e.g. perf annotate --stdio)
9 / 21

(5-10 min) Try it out

Pick any prior code you've written and try profiling it

$ perf record ./your_program
$ perf report

> The bigger the better

Are the results what you expect?

10 / 21

Closing thoughts on profiling

When should you profile your code?

How often should you profile your code?

11 / 21

Closing thoughts on profiling

When should you profile your code?

How often should you profile your code?


Other questions, thoughts about profiling?

12 / 21

Static Analysis

What is it?

Why is it useful?

When should you run it?

13 / 21

Just a little history first

Linting - the original static analysis

14 / 21

Just a little history first

Linting - the original static analysis

  • The point: The line between style and correctness is blurry
15 / 21

Just a little history first

Linting - the original static analysis

  • The point: The line between style and correctness is blurry

Today, the lines between compilers, linters, and static analyzers are blurring

16 / 21

Static Analysis in action: cppcheck

sudo apt install cppcheck

Check a single file:

mmdarden@c4cs-w17:~/share/281$ cppcheck my_compress.cpp
Checking my_compress.cpp...
[my_compress.cpp:445]: (error) Memory leak: dict
Checking my_compress.cpp: DEBUG...
Checking my_compress.cpp: DEBUG2...

Check a whole project for everything

mmdarden@c4cs-w17:~/share/281$ cppcheck --enable=all .
...
17 / 21

Static Analysis in action: scan-build

sudo apt install clang

This tool dynamically re-writes make rules (!)

  • Won't work if you've hardcoded g++ (should be $(CXX))
bad: bad.cpp
g++ bad.cpp
good: good.cpp
$(CXX) $(CPPFLAGS) $(CXXFLAGS) good.cpp
mmdarden@c4cs-w17:~/share/281$ scan-build make
...
scan-build: 7 bugs found.
scan-build: Run 'scan-view /tmp/scan-build-2016-11-30' to examine bug reports
18 / 21

(10 min) Try it out

Try running cppcheck and scan-build on an old project

$ cppcheck --enable=all .
$ scan-build make

Did they find any errors?

Try running them on a current project

19 / 21

Developer Surveys

C4CS

StackOverflow.com

20 / 21

Next Week

Two special topics lectures

- Wed: TBD

- Fri: TBD

Come to at least one (like usual)

- Consider coming to both if you can

- Should be fun :)


warning: end-of-semester slightly shrinks the window to turn in Advanced Exercise 13

- Double check the OH on the course calendar!

21 / 21

Today

  • Profiling

  • Static Analysis

  • Developer Surveys

2 / 21
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow