Unix / Linux - 系統性能

Hello there, aspiring tech enthusiasts! As your friendly neighborhood computer science teacher, I'm thrilled to guide you through the fascinating world of Unix/Linux system performance. Don't worry if you're new to programming – we'll start from the basics and work our way up. So, grab a cup of coffee (or tea, if that's your thing), and let's dive in!

Unix / Linux - System Performance

性能組成部分

Before we start tinkering with our systems, it's crucial to understand what makes up system performance. Think of your computer as a finely tuned orchestra – each component plays a vital role in creating a harmonious performance.

1. CPU 使用率

The Central Processing Unit (CPU) is like the conductor of our orchestra. It's responsible for executing instructions and coordinating all the other components. When we talk about CPU usage, we're referring to how busy this conductor is.

2. 内存使用率

Memory, or RAM, is like the sheet music for our orchestra. It stores data that the CPU needs quick access to. If we run out of memory, our performance can slow down dramatically.

3. 磁盘 I/O

Disk Input/Output (I/O) is like the process of musicians picking up new instruments or sheet music. It involves reading from or writing to the hard drive, which can be a slower operation compared to accessing RAM.

4. 网络性能

Network performance is similar to how well our orchestra can hear each other and stay in sync. It involves the speed and efficiency of data transfer over the network.

性能工具

Now that we understand the components, let's look at some tools we can use to measure and improve our system's performance. I like to think of these as our orchestra's tuning instruments.

1. top 指令

The top command is like a dashboard for your system. It provides a real-time view of running processes and system resources.

$ top

This command will display a continuously updated list of processes, sorted by CPU usage by default. Let's break down what you'll see:

  • PID: Process ID
  • USER: User who owns the process
  • PR: Priority
  • NI: Nice value (affects priority)
  • VIRT: Virtual memory used
  • RES: Physical memory used
  • SHR: Shared memory
  • S: Process status
  • %CPU: CPU usage
  • %MEM: Memory usage
  • TIME+: Total CPU time
  • COMMAND: Command name

2. vmstat 指令

The vmstat command is like a stethoscope for your system's heart – it reports information about processes, memory, paging, block I/O, traps, and CPU activity.

$ vmstat 2 5

This command will display system statistics every 2 seconds, for 5 iterations. The output includes:

  • r: Number of processes waiting for run time
  • b: Number of processes in uninterruptible sleep
  • swpd: Amount of virtual memory used
  • free: Amount of idle memory
  • buff: Amount of memory used as buffers
  • cache: Amount of memory used as cache
  • si: Amount of memory swapped in from disk
  • so: Amount of memory swapped out to disk
  • bi: Blocks received from a block device
  • bo: Blocks sent to a block device
  • in: Number of interrupts per second
  • cs: Number of context switches per second
  • us: Time spent running non-kernel code
  • sy: Time spent running kernel code
  • id: Time spent idle
  • wa: Time spent waiting for I/O

3. iostat 指令

The iostat command is like a speedometer for your disk drives. It reports CPU statistics and input/output statistics for devices and partitions.

$ iostat -x 2 5

This command will display extended statistics every 2 seconds, for 5 iterations. The output includes:

  • Device: Device name
  • rrqm/s: Read requests merged per second
  • wrqm/s: Write requests merged per second
  • r/s: Reads per second
  • w/s: Writes per second
  • rkB/s: Kilobytes read per second
  • wkB/s: Kilobytes written per second
  • avgrq-sz: Average request size in sectors
  • avgqu-sz: Average queue length
  • await: Average time for I/O requests
  • svctm: Average service time
  • %util: Percentage of CPU time during which I/O requests were issued

4. netstat 指令

The netstat command is like a traffic controller for your network. It displays network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.

$ netstat -tuln

This command will display:

  • Active Internet connections (TCP and UDP)
  • Listening sockets
  • Numeric addresses and port numbers

The output includes:

  • Proto: Protocol (TCP or UDP)
  • Recv-Q: Data queued for receiving
  • Send-Q: Data queued for sending
  • Local Address: Local end of the socket
  • Foreign Address: Remote end of the socket
  • State: State of the socket

5. sar 指令

The sar (System Activity Reporter) command is like a black box recorder for your system. It collects, reports, and saves system activity information.

$ sar -u 2 5

This command will display CPU usage statistics every 2 seconds, for 5 iterations. The output includes:

  • %user: Percentage of CPU utilization at user level
  • %nice: Percentage of CPU utilization at user level with nice priority
  • %system: Percentage of CPU utilization at system level
  • %iowait: Percentage of time that the CPU was idle during which the system had an outstanding disk I/O request
  • %steal: Percentage of time spent in involuntary wait by the virtual CPU while the hypervisor was servicing another virtual processor
  • %idle: Percentage of time that the CPU was idle and the system did not have an outstanding disk I/O request

Here's a table summarizing these performance tools:

指令 目的 关键信息
top 实时系统监控 进程列表,CPU使用率,内存使用率
vmstat 虚拟内存统计 进程,内存,分页,I/O,CPU活动
iostat I/O统计 CPU统计,设备I/O统计
netstat 网络统计 网络连接,路由表,接口统计
sar 系统活动报告 CPU使用率,内存使用率,I/O活动

Remember, becoming proficient with these tools is like learning to play an instrument – it takes practice! Don't be discouraged if it seems overwhelming at first. Start with one tool, understand its output thoroughly, and then move on to the next.

In my years of teaching, I've found that students who regularly experiment with these tools on their own systems develop a much deeper understanding of system performance. It's like learning to hear the individual instruments in an orchestra – suddenly, you can pinpoint exactly where improvements can be made.

So, dear students, I encourage you to fire up your terminal and start exploring. Who knows? You might just become the next great system performance conductor!

Credits: Image by storyset