Top Banner
Topics in Software Engineering Steven Lamerton Software Engineering Group, STFC
18

Topics in Software Engineering - ccp-wsi.ac.uk · PDF file . Optimisation Flags ... cppcheck •Simple command line interface •Finds a wide range of issues:

Mar 17, 2018

Download

Documents

lamliem
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Topics in Software Engineering - ccp-wsi.ac.uk · PDF file  . Optimisation Flags ... cppcheck •Simple command line interface •Finds a wide range of issues:

Topics in Software Engineering

Steven Lamerton

Software Engineering Group, STFC

Page 2: Topics in Software Engineering - ccp-wsi.ac.uk · PDF file  . Optimisation Flags ... cppcheck •Simple command line interface •Finds a wide range of issues:

Overview

• Compiler flags

• Static analysis

• Debugging

• Profiling

• Visualisation

Page 3: Topics in Software Engineering - ccp-wsi.ac.uk · PDF file  . Optimisation Flags ... cppcheck •Simple command line interface •Finds a wide range of issues:

Compiler Flags

Page 4: Topics in Software Engineering - ccp-wsi.ac.uk · PDF file  . Optimisation Flags ... cppcheck •Simple command line interface •Finds a wide range of issues:

Compiler Flags

• Compilers have lots (and lots and lots) of flags to tune their operation, but a basic set can make a big difference

• The flags I talk about here are primarily for GCC and will work mostly work for C, C++ and Fortran

• Clang has many similar flags, but not entirely the same, check their documentation for more details

• GCC documentation available at: https://gcc.gnu.org/onlinedocs/gcc/Invoking-GCC.html

Page 5: Topics in Software Engineering - ccp-wsi.ac.uk · PDF file  . Optimisation Flags ... cppcheck •Simple command line interface •Finds a wide range of issues:

Optimisation Flags

• -O[0|1|2|3] Increasing levels of optimisation, none is the default

• -Os Optimise for size

• -Ofast Disregard standards compliance for potential speed

• -Og Optimise for debugging experience

Use -O3 in production, -Og when developing

Page 6: Topics in Software Engineering - ccp-wsi.ac.uk · PDF file  . Optimisation Flags ... cppcheck •Simple command line interface •Finds a wide range of issues:

Results

0

5000

10000

15000

20000

25000

30000

35000

40000

45000

50000

0

10000

20000

30000

40000

50000

60000

70000

80000

-O0 -O1 -O2 -O3 -Ofast -Og -Os

Mil

liseco

nd

s

Byte

s

Size Time

Page 7: Topics in Software Engineering - ccp-wsi.ac.uk · PDF file  . Optimisation Flags ... cppcheck •Simple command line interface •Finds a wide range of issues:

Warning Flags

• -Wall Enables most warnings, all are easy to work around

• -Wextra Additional warnings not included in –Wall

• -Wpedantic Issues warnings required by ISO standards

• -Werror Turns warnings into errors

Use these all the time

Page 8: Topics in Software Engineering - ccp-wsi.ac.uk · PDF file  . Optimisation Flags ... cppcheck •Simple command line interface •Finds a wide range of issues:

Debugging and Profiling Flags

• -g Produce debugging information whilst compiling, should be used with -Og for optimal experience

• -pg Generate profiling information for gprof

• -fsanitize=[address|thread|leak|undefined] Enable runtime checks for various error types

Page 9: Topics in Software Engineering - ccp-wsi.ac.uk · PDF file  . Optimisation Flags ... cppcheck •Simple command line interface •Finds a wide range of issues:

Other Useful Flags

• -std=[c99|c11|c++11|c++14|f2003|f2008] Set the language standard to be used

• -ffpe-trap=invalid Break on NaNs (Fortran only)

Page 10: Topics in Software Engineering - ccp-wsi.ac.uk · PDF file  . Optimisation Flags ... cppcheck •Simple command line interface •Finds a wide range of issues:

OpenFOAM Options

• Only two options in OpenFOAM:

– WM_COMPILE_OPTION=Opt

– WM_COMPILE_OPTION=Debug

• Opt implies -O3

• Debug implies –O0 -g

• Both enable a range of warnings including -Wall -Wextra

Page 11: Topics in Software Engineering - ccp-wsi.ac.uk · PDF file  . Optimisation Flags ... cppcheck •Simple command line interface •Finds a wide range of issues:

Exercise

• Two options, either use your own code or take a copy of the tarball from https://www.ccp-wsi.ac.uk/?q=software_engineering_workshop

– The tarball includes codes to try both warning and optimisation flags

• Try using different levels of optimisation and warnings

• To test performance call your executable using the time command: time <your executable>

Page 12: Topics in Software Engineering - ccp-wsi.ac.uk · PDF file  . Optimisation Flags ... cppcheck •Simple command line interface •Finds a wide range of issues:

Static Analysis

Page 13: Topics in Software Engineering - ccp-wsi.ac.uk · PDF file  . Optimisation Flags ... cppcheck •Simple command line interface •Finds a wide range of issues:

Introduction

• Static analysis is the analysis of a code without running it, usually by inspecting the source code

• Used to find classes of error not picked up by compilers

• Many options available both free and commercial, we will focus on cppcheck as it is good and widely available

Page 14: Topics in Software Engineering - ccp-wsi.ac.uk · PDF file  . Optimisation Flags ... cppcheck •Simple command line interface •Finds a wide range of issues:

cppcheck

• Simple command line interface

• Finds a wide range of issues:

– Bounds checking

– Memory leaks

– Resource leaks

– Condition checks

– Unused functions / variables

– Unreachable code

Page 15: Topics in Software Engineering - ccp-wsi.ac.uk · PDF file  . Optimisation Flags ... cppcheck •Simple command line interface •Finds a wide range of issues:

cppcheck

• cppcheck [OPTIONS] [files or paths]

• --enable=all Enable all checks, subset are also available

• --std=[c89|c99|c11|c++03|c++11] Set the language standard

• --inconclusive Show inconclusive results

• -q Only print warnings / errors

Page 16: Topics in Software Engineering - ccp-wsi.ac.uk · PDF file  . Optimisation Flags ... cppcheck •Simple command line interface •Finds a wide range of issues:

Examples

• Found suspicious equality comparison. Did you intend to assign a value instead?

• Expression 'exp(x) - 1' can be replaced by 'expm1(x)' to avoid loss of precision.

• Technically the member function 'Foam::convexPolyhedral::edgeCutLabel' can be const.

• Variable 'phi' is assigned a value that is never used.

Page 17: Topics in Software Engineering - ccp-wsi.ac.uk · PDF file  . Optimisation Flags ... cppcheck •Simple command line interface •Finds a wide range of issues:

062 if (!phicp.coupled())

063 {

064 phicp == 0;

065 }

Page 18: Topics in Software Engineering - ccp-wsi.ac.uk · PDF file  . Optimisation Flags ... cppcheck •Simple command line interface •Finds a wide range of issues:

Exercise

• Similarly to before, either use your own code or some from the tarball

• Note that the classes of bugs are quite different to those shown by compiler warning flags

• As with compiler warnings it is best to use static analysis fro the start