- Use logging instead of print statements
- Allows output to go to files, sockets, remote servers, etc. instead of just
stdout - Supports different severity levels (INFO, DEBUG, WARN, ERROR), allowing for filtering
- Color code them for improved readability
- Allows output to go to files, sockets, remote servers, etc. instead of just
systemmdin Linux systems places logs in/var/log/journal, accessed viajournalctlcommand
- Debugger benefits
- Halt execution of program at a certain line or when conditions are met
- Step through program incrementally
- Inspect variable values after crashing
- Debuggers
- C++:
gdb - Python:
pdboripdb(similar but with tab completion, syntax highlighting, etc.)
- C++:
- System calls are used by programs to perform actions via the kernel;
straceallows you to track when they occur - Firefox/Chrome are good debugging tools for web dev - inspect site elements, live modification, etc.
- Static analysis programs take source code as input and analyze it with coding rules, allowing you to find issues without running code - especially helps for time-intensive programs
- Options for many languages here: https://github.com/analysis-tools-dev/static-analysis
- Types of time
- Real: time from start to finish of program, including time taken by other processes
- User: time spent in CPU running user-level code
- Sys: time spent in CPU running kernel-level code
- Profiler types
- Tracing: execute with code, take note of every function call. At the end, able to tell time spent in different functions
- Problem: lots of overhead, affect performance of original program
- Sampling: on a fixed time interval, stops program, looks at stack trace to see where in program you are, and enough samples will be able to tell where time is spent
- Tracing: execute with code, take note of every function call. At the end, able to tell time spent in different functions
- Profiler examples
cProfile: Python tracing profiler that measures time per function callpython -m cProfile -s tottime SCRIPT.py 1000 '^(import|\s*def)[^,]*$' *.py- Can be hard to parse through, since you might want to measure a function that calls other third-party library functions
line_profiler: Python profiler that uses@profiledecorator above functions to identify which one to measurekernprof -lv SCRIPT.py
- Memory profiling: can help identify memory leaks and inefficiencies
- C++:
valgrind - Python:
memory_profiler
- C++:
- Event profiling: ignores specifics, treats program as a black box
perf: hardware-agnostic evaluation, reporting system events that occurred from a certain commandperf stat COMMAND ARG_1 ARG_2: gets counts of different events related to a command
- Profiling visualization tools
- Flame graph:
perf COMMAND report flamegraph - Call graphs:
pycallgraph graphviz -- ./SCRIPT.py
- Flame graph:
- Resource monitoring: determine what resource is the main bottleneck/constraint
htop: generaliotop: I/O usagedu,ncdu: disk usagefree: memorylsof: open filesss,ip: network connectionsnethogs,iftop: network usage
hyperfine: black box speed comparison between command line programshyperfine --warmup 3 'COMMAND_1' 'COMMAND_2'
- eBPF: perform kernel tracing of user programs