Understanding 'systemd-analyze' for System Performance Analysis (with examples)
- Linux
- December 17, 2024
The systemd-analyze
command is a powerful tool for administrators and developers to analyze the performance of the system’s boot process and other system functions. This tool provides insights into the time it takes for various system units, such as services, mount points, and devices, to start. It can also visualize dependencies and security scores, making it indispensable for debugging and optimizing system performance.
Use case 1: Listing All Running Units by Initialization Time
Code:
systemd-analyze blame
Motivation:
When a system administrator notices a prolonged boot time, understanding which services are delaying the startup process is crucial. The systemd-analyze blame
command lists all the running units in order of the time each took to initialize, helping to identify potential bottlenecks or problematic services.
Explanation:
systemd-analyze
: The primary command to analyze and debug the system’s boot process.blame
: This argument tells the command to list units according to the time they took to start. Units taking a longer time appear at the top.
Example output:
6.270s NetworkManager.service
4.123s docker.service
3.045s accounts-daemon.service
2.123s systemd-logind.service
...
Use case 2: Printing a Time-Critical Chain of Units
Code:
systemd-analyze critical-chain
Motivation:
Certain services are critical for the proper functioning of the system and should be initialized as soon as possible. The critical-chain
option allows administrators to identify and analyze the services that are crucial for other services to start, facilitating targeted optimizations.
Explanation:
systemd-analyze
: The command used to evaluate system performance.critical-chain
: This provides a tree view of units whose activation is crucial and blocks others from starting sooner. It helps to map dependencies and understand service start order.
Example output:
graphical.target @10.003s
└─multi-user.target @10.002s
└─docker.service @6.001s +3.001s
└─network-online.target @5.000s
└─NetworkManager.service @1.000s +4.000s
...
Use case 3: Generating an SVG File of Service Start Times
Code:
systemd-analyze plot > path/to/file.svg
Motivation: Visual representations can provide a more intuitive understanding of complex data. By generating an SVG plot, this use case provides a timeline view of when services start relative to each other and how much time they take, facilitating comprehensive performance analysis.
Explanation:
systemd-analyze
: The command used for system analysis.plot
: Instructs the command to generate a plot representing service start times.> path/to/file.svg
: Redirects the output to a file specified by the user in SVG format, which can then be viewed in a browser or image viewer.
Example output:
<SVG file is created that can be opened and viewed>
Use case 4: Plotting a Dependency Graph in SVG Format
Code:
systemd-analyze dot | dot -Tsvg > path/to/file.svg
Motivation: Understanding how services depend on each other is crucial in optimizing start times and solving issues related to service dependencies. By converting this information into an SVG format, administrators can visually analyze the service dependency graph.
Explanation:
systemd-analyze
: The command for analysis of system units.dot
: This option outputs the services’ dependencies in a format that graph visualization tools can understand.| dot -Tsvg > path/to/file.svg
: Pipes the output to the Graphvizdot
tool, converting it to an SVG file for better visual representation.
Example output:
<SVG file representing the dependency graph is created>
Use case 5: Displaying Security Scores of Running Units
Code:
systemd-analyze security
Motivation: Security is paramount in system administration. This command evaluates and reports the security status of running units, helping administrators prioritize security improvements and mitigate potential vulnerabilities.
Explanation:
systemd-analyze
: The analysis tool for system diagnostics.security
: Inspects and reports the security implications of the currentsystemd
unit configuration and provides ratings.
Example output:
UNIT EXPOSED
cron.service 9.8
sshd.service 8.9
nginx.service 7.5
...
Conclusion:
The systemd-analyze
command provides a wide range of functionalities that allow administrators to diagnose and improve system boot times and service performance comprehensively. The various use cases discussed offer tools for time analysis, critical path identification, visualizations, security scoring, and dependency mapping, making it an essential part of a system administrator’s toolkit.