Use the sar tool to detect system performance bottlenecks
sar The command is used to collect, report, or save UNIX/Linux system activity information. It saves selected counters to the operating system's /var/log/sa/sadd file. From the collected data, you can get a lot of information about your server:
- CPU Usage
- Memory pages and usage
- Network I/O and Transfer Statistics
- Process Creation Activity
- All block device activities
- Interruptions per second, etc.
sar The output of the command can be used to identify server bottlenecks. However, it can be difficult to analyze the information provided by the sar command, so use the kSar tool. The kSar tool can plot the output of the sar command into an easy-to-understand chart based on time periods.
sar, sa1, and sa2 commands are part of the sysstat package. It is a collection of performance monitoring tools included with Linux.
- sar:Display data
- sa1 and sa2: Collect and save data for later analysis. sa2 The shell script writes a daily report in the /var/log/sa directory. sa1 The shell script writes daily system activity information to a file in the form of binary data.
- sadc - System activity data collector. You can configure various options by modifying the sa1 and sa2 scripts. They are located in the following directories:
- /usr/lib64/sa/sa1 (64-bit) or /usr/lib/sa/sa1 (32-bit) - it calls sadcGo to log reports to /var/log/sa/sadX format.
- /usr/lib64/sa/sa2 (64-bit) or /usr/lib/sa/sa2 (32-bit) - it calls sarGo and log reports to /var/log/sa/sarX format.
在一个基于 CentOS/RHEL 的系统上,输入如下的 yum 命令[1] 去安装 sysstat:
1 |
|
示例输出如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
编辑 /etc/sysconfig/sysstat 文件去指定日志文件保存多少天(最长为一个月):
1 |
|
示例输出如下 :
1 2 3 |
|
保存并关闭这个文件。
默认的 cron 作业位于[2] /etc/cron.d/sysstat:
1 |
|
示例输出如下:
1 2 3 4 |
|
使用一个文本编辑器去编辑 /etc/cron.d/sysstat 文件,比如使用 vim 命令,输入如下:
1 |
|
像下面的示例那样更新这个文件,以记录所有的硬盘统计数据(-d 选项强制记录每个块设备的统计数据,而 -I 选项强制记录所有系统中断的统计数据):
1 2 3 4 |
|
在 CentOS/RHEL 7.x 系统上你需要传递 -S DISK 选项去收集块设备的数据。传递 -S XALL 选项去采集如下所列的数据:
- 磁盘
- 分区
- 系统中断
- SNMP
- IPv6
1 2 3 4 5 6 |
|
保存并关闭这个文件。
输入如下命令:
1 2 |
|
示例输出如下:
1 |
|
对于 CentOS/RHEL 7.x,运行如下的命令:
1 2 3 |
|
示例输出:
1 2 3 4 5 6 7 8 |
|
使用 sar 命令去显示操作系统中选定的累积活动计数器输出。在这个示例中,运行 sar 命令行,去实时获得 CPU 使用率的报告:
1 |
|
示例输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
其中:
- 3 表示间隔时间
- 10 表示次数
查看进程创建的统计数据,输入:
1 |
|
查看 I/O 和传输率统计数据,输入:
1 |
|
查看内存页面统计数据,输入:
1 |
|
查看块设备统计数据,输入:
1 |
|
查看所有中断的统计数据,输入:
1 |
|
查看网络设备特定的统计数据,输入:
1 2 |
|
查看 CPU 特定的统计数据,输入:
1 2 3 |
|
查看队列长度和平均负载的统计数据,输入:
1 |
|
查看内存和交换空间的使用统计数据,输入:
1 2 |
|
查看 inode、文件、和其它内核表统计数据状态,输入:
1 |
|
查看系统切换活动统计数据,输入:
1 |
|
查看交换统计数据,输入:
1 |
|
查看一个 PID 为 3256 的 Apache 进程,输入:
1 |
|
sar 和 sadf 提供了基于命令行界面的输出。这种输出可能会使新手用户/系统管理员感到无从下手。因此,你需要使用 kSar,它是一个图形化显示你的 sar 数据的 Java 应用程序。它也允许你以 PDF/JPG/PNG/CSV 格式导出数据。你可以用三种方式去加载数据:本地文件、运行本地命令、以及通过 SSH 远程运行的命令。kSar 可以处理下列操作系统的 sar 输出:
- Solaris 8, 9 和 10
- Mac OS/X 10.4+
- Linux (Systat Version >= 5.0.5)
- AIX (4.3 & 5.3)
- HPUX 11.00+
访问 官方[3] 网站去获得最新版本的源代码。使用 wget[4] 去下载源代码,输入:
1 |
|
首先要确保你的机器上 JAVA jdk[5] 已安装并能够正常工作。输入下列命令去启动 kSar:
1 |
|
kSar welcome screen
接下来你将看到 kSar 的主窗口,和有两个菜单的面板。
kSar - the main window
左侧有一个列表,是 kSar 根据数据已经解析出的可用图表的列表。右侧窗口将展示你选定的图表。
首先,你需要从命名为 server1 的服务器上采集 sar 命令的统计数据。输入如下的命令:
1 |
|
接下来,使用 scp 命令从本地桌面拷贝到远程电脑上:
1 |
|
切换到 kSar 窗口,点击 “Data” > “Load data from text file” > 从 /tmp/ 中选择 sar.data.txt> 点击 “Open” 按钮。
现在,图表类型树已经出现在左侧面板中并选定了一个图形:
Linux kSar Processes for server1
Linux Disk I/O Stats Using kSar
Linux Memory paging and its utilization stats
放大和缩小
通过移动你可以交互式缩放图像的一部分。在要缩放的图像的左上角点击并按下鼠标,移动到要缩放区域的右下角,可以选定要缩放的区域。返回到未缩放状态,点击并拖动鼠标到除了右下角外的任意位置,你也可以点击并选择 zoom 选项。
了解 kSar 图像和 sar 数据
我强烈建议你去阅读 sar 和 sadf 命令的 man 页面:
1 2 |
|
使用 sar 命令和 kSar 工具,可以得到内存、CPU、以及其它子系统的详细快照。例如,如果 CPU 使用率在一个很长的时间内持续高于 80%,有可能就是出现了一个 CPU 瓶颈。使用 sar -x ALL 你可以找到大量消耗 CPU 的进程。
mpstat 命令[6] 的输出(sysstat 包的一部分)也会帮你去了解 CPU 的使用率。但你可以使用 kSar 很容易地去分析这些信息。
Perform the following adjustments to the CPU:
- Make sure there are no unnecessary processes running in the background. Turn off all unnecessary services on Linux[7].
- Use cron[8] Run a task (for example, backup) at an off-peak time.
- Use top and ps commands[9] to find all non-critical background jobs/services. Use the renice command [10] to adjust low priority jobs.
- Use the taskset command to set the CPU used by the process [11] (offload the CPU used), that is, bind the process to a different CPU. For example, running MySQL database on CPU #2 and Apache on CPU #3.
- Make sure your system is using the latest drivers and firmware.
- Add additional CPUs to the system if possible.
- Use a faster CPU for single-threaded applications (e.g., Lighttpd web server application).
- Use multiple CPUs for multi-threaded applications (e.g., MySQL database server applications).
- Use multiple compute nodes and set up a load balancer for a web application[12].
isag command graphically displays system activity data stored in a binary file from previous runs of the sar command. The isag command references sar and extracts its data to plot the graph. isag has fewer options compared to kSar.
Fig.06: isag CPU utilization graphs
The above is the detailed content of Use the sar tool to detect system performance bottlenecks. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.
