Unix / Linux - System Performance

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

Performance Components

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 Usage

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 Usage

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. Disk 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

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.

Performance Tools

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 Command

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 Command

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 Command

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 Command

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 Command

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:

Command Purpose Key Information
top Real-time system monitor Process list, CPU usage, memory usage
vmstat Virtual memory statistics Process, memory, paging, I/O, CPU activity
iostat I/O statistics CPU statistics, device I/O statistics
netstat Network statistics Network connections, routing tables, interface statistics
sar System activity reporter CPU usage, memory usage, I/O activity, network activity

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