CPU/Disk/Memory related task

CPU = brain, executes instruction RAM = short-term memory, Disk = long-term memory, GPU = specialized brain for graphics/math.

CPU

chevron-rightCPU theory and commandshashtag

Got it 👍 This is a big topic, so I’ll give you a complete reference on Linux CPU commands + theory. I’ll break it into:

  1. Basics of CPU in Linux

  2. Important files in /proc and /sys

  3. Common commands for CPU info & usage

  4. Performance monitoring tools

  5. Advanced profiling & debugging tools

  6. CPU-related system tuning


1. Basics of CPU in Linux

  • CPU (Central Processing Unit) executes instructions.

What is a CPU?

CPU stands for Central Processing Unit. It’s often called the “brain of the computer” because it executes instructions/program and controls most operations of the system.

Main Functions of a CPU

  1. Fetch – Gets instructions from memory (RAM).

  2. Decode – Interprets what the instruction means.

  3. Execute – Performs the required action (arithmetic, logic, data movement).

  4. Store – Writes the result back to memory or registers.

This cycle is called the Instruction Cycle.

  • Modern CPUs have:

    • Cores → independent processing units.

    • Threads (logical CPUs) → hardware multithreading (e.g., Intel Hyper-Threading).

    • Clock speed → cycles per second (GHz).

    • Caches → L1/L2/L3 memory close to CPU for speed.

  • Linux sees CPUs as logical processors (/proc/cpuinfo).

  • CPU usage = time spent in:

    • user (running user processes)

    • system (kernel code)

    • idle (nothing to do)

    • iowait (waiting for I/O)

    • steal (time stolen by hypervisor in VMs)

    • irq/softirq (handling interrupts)


2. Important Files

  • /proc/cpuinfo → detailed CPU info (model, cores, flags).

  • /proc/stat → CPU usage statistics.

  • /proc/loadavg → system load averages.

  • /sys/devices/system/cpu/ → CPU topology, governors, scaling, etc.

  • /proc/interrupts → CPU interrupt distribution.


3. Commands for CPU Info & Usage

CPU Information

lscpu                # Detailed CPU architecture info
cat /proc/cpuinfo    # Per-core info
nproc                # Number of available CPUs
getconf _NPROCESSORS_ONLN   # Online processors
dmidecode -t processor      # BIOS/firmware CPU info

CPU Usage (Snapshot)

top          # Realtime processes + CPU usage
htop         # Better interactive process viewer
uptime       # Load averages
mpstat -P ALL 1   # Per-CPU usage (from sysstat package)
sar -u 1 5        # Historical CPU utilization

Process CPU Usage

ps -eo pid,ppid,cmd,%cpu,%mem --sort=-%cpu | head   # Top CPU hogs
pidstat -u 1                                        # Per-process CPU stats

4. Performance Monitoring Tools

  • top/htop → live monitoring

  • atop → advanced system monitor

  • glances → all-in-one system monitor

  • dstat → resource usage over time

  • vmstat 1 → CPU, memory, processes

  • iostat -c 1 → CPU usage stats


5. Advanced Profiling & Debugging Tools

  • perf (Linux Performance Counters)

    perf stat -a sleep 5       # CPU events for 5s
    perf top                   # Live CPU function usage
  • strace -c -p → syscall profiling per process

  • perf record / perf report → detailed profiling

  • ftrace → kernel tracing

  • bpftrace / eBPF tools → modern tracing, CPU flamegraphs


6. CPU Tuning & Management

CPU Frequency Scaling

cpupower frequency-info      # Show CPU frequency
cpupower frequency-set -g performance   # Change governor

Governors:

  • performance → max speed

  • powersave → min speed

  • ondemand / schedutil → dynamic scaling

CPU Affinity (bind processes to cores)

taskset -c 0,2 <command>   # Run on CPU 0 and 2
ps -o pid,psr,comm -p <pid>  # Show CPU core of process

NUMA (Non-Uniform Memory Access)

numactl --hardware    # Show NUMA nodes
numactl --cpunodebind=0 command   # Bind to NUMA node

Summary

  • Use lscpu and cat /proc/cpuinfo for CPU details.

  • Use top/htop/mpstat/sar for CPU usage.

  • Use perf/strace/eBPF for deep analysis.

  • Use taskset/numactl/cpupower for tuning and control.


👉 Pavan, do you want me to also prepare a cheat sheet table with commands + short descriptions (so you can quickly refer during work/interview)?

chevron-rightMemory Theory and commandshashtag

Perfect 👍 Let’s do the same structured deep-dive for Linux Memory — just like we did for CPU.


📘 Linux Memory Commands & Theory

1. Basics of Memory in Linux

  • Memory (RAM) is fast, volatile storage (retaining data only as long as there is a power supply connected) used by CPU to run programs.

  • Linux uses virtual memory:

    • RAM (physical memory)

    • Swap space (disk used when RAM is full)

  • Memory management includes:

    • Paging – moving fixed-size pages between RAM & disk.

    • Swapping – moving entire processes.

    • Cache & Buffers – Linux caches disk/filesystem data in RAM to speed up performance.

Memory Types (as seen in Linux):

  • Used memory – actively in use by processes.

  • Free memory – not used at all.

  • Buffers – metadata for block devices.

  • Cache – cached file data (reclaimable if needed).

  • Swap – disk space used as extra RAM.


2. Important Files

  • /proc/meminfo → detailed memory info.

  • /proc/swaps → swap usage.

  • /proc/<pid>/status → memory usage of a process.

  • /sys/devices/system/memory/ → memory hotplug info.

  • /proc/vmstat → virtual memory statistics.


3. Commands for Memory Info & Usage

Memory Information

free -h            # Human-readable memory usage
cat /proc/meminfo  # Detailed memory stats
vmstat -s          # Memory statistics
dmidecode -t memory   # Hardware memory info

Memory Usage (Snapshot)

top           # Shows memory usage per process
htop          # Better visualization
glances       # All-in-one system monitor

Per-Process Memory

ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head   # Top memory hogs
smem -r        # Memory usage with shared/private breakdown
pmap <pid>     # Show memory map of a process
cat /proc/<pid>/status | grep VmRSS   # Resident Set Size of a process

4. Performance Monitoring Tools

  • vmstat 1 → CPU, memory, paging.

  • dstat --mem → memory over time.

  • sar -r 1 5 → memory utilization (sysstat package).

  • atop → advanced system monitor.

  • /proc/meminfo + watch → live memory stats.


5. Advanced Profiling & Debugging Tools

  • valgrind / massif → profile memory usage of programs.

  • memleax → detect memory leaks in running processes.

  • perf stat -e page-faults → track memory page faults.

  • bcc/eBPF tools (memleak, cachestat, oomkill) → advanced memory tracing.

  • numastat → NUMA memory stats.


6. Memory Tuning & Management

Swap Management

swapon -s       # Show active swap
swapon /path/to/swapfile   # Enable swap
swapoff -a      # Disable all swap

Adjust Swappiness (tendency to use swap)

cat /proc/sys/vm/swappiness
sysctl -w vm.swappiness=10

Clear Cache (careful in production!)

echo 1 > /proc/sys/vm/drop_caches    # Clear pagecache
echo 2 > /proc/sys/vm/drop_caches    # Clear dentries and inodes
echo 3 > /proc/sys/vm/drop_caches    # Clear all caches

Memory Limits (per process)

ulimit -m <bytes>   # Limit max memory for a shell/process
cgroups (systemd)   # Limit memory usage at container/service level

✅ Summary

  • Use free -h, cat /proc/meminfo for memory details.

  • Use top/htop/glances for usage.

  • Use pmap/smem for per-process analysis.

  • Use valgrind, bcc/eBPF, perf for deep memory profiling.

  • Tune with swappiness, drop_caches, cgroups.


👉 Pavan, do you want me to also prepare a cheat sheet table (commands + short description) for memory (like a one-page reference for interviews & work)?

chevron-rightDisk Theory and commandshashtag

Perfect 👍 Let’s make the Disk Commands & Theory in Linux reference, same structured way like CPU and Memory.


📘 Linux Disk Commands & Theory

1. Basics of Disks in Linux

  • Disk storage = permanent data storage (HDDs, SSDs, NVMe, SAN).

  • Linux sees disks as block devices (/dev/sda, /dev/nvme0n1, etc.).

  • Disks are divided into:

    • Partitions (/dev/sda1, /dev/sda2)

    • Filesystems (ext4, xfs, btrfs, etc.)

  • Disk I/O = Input/Output operations between memory and disk.

  • Metrics to monitor:

    • Capacity – total vs. used.

    • Throughput – MB/s read/write.

    • IOPS – I/O operations per second.

    • Latency – time taken per operation.

    • Utilization – percentage busy.


2. Important Files

  • /proc/partitions → partitions info.

  • /proc/diskstats → detailed disk I/O statistics.

  • /sys/block/* → block device attributes (scheduler, queue size).

  • /etc/fstab → filesystem mount configuration.

  • /proc/mounts → currently mounted filesystems.


3. Commands for Disk Info & Usage

Disk Information

lsblk            # Block devices, partitions, mount points
fdisk -l         # Partition tables
parted -l        # More partition info
blkid            # Block device attributes (UUID, type)
ls -l /dev/disk/by-uuid/   # UUID links

Disk Usage (Capacity)

df -h            # Human-readable disk usage
du -sh /path     # Disk usage of a directory
du -sh *         # Disk usage of files/folders in current dir
find / -xdev -type f -size +100M   # Find large files

Disk I/O Usage (Snapshot)

iostat -xz 1     # Disk utilization, IOPS, latency (sysstat package)
dstat -d 1       # Disk read/write per second
iotop            # Top processes by I/O usage
vmstat -d        # Disk statistics

4. Performance Monitoring Tools

  • iostat → throughput, IOPS, latency.

  • iotop → per-process I/O usage.

  • dstat --disk → live disk stats.

  • atop → disk performance + bottlenecks.

  • collectl → lightweight all-in-one.

  • sar -d 1 5 → historical disk activity.


5. Advanced Profiling & Debugging Tools

  • blktrace → trace block layer I/O operations.

  • bpftrace/eBPF → monitor disk latency, queue depth.

  • fio → disk benchmarking (IOPS, throughput, latency).

  • dd → quick read/write performance check.

    dd if=/dev/zero of=/tmp/testfile bs=1G count=1 oflag=dsync
  • smartctl (from smartmontools) → check disk health.

    smartctl -a /dev/sda

6. Disk Tuning & Management

Filesystem Management

mount /dev/sda1 /mnt         # Mount a partition
umount /mnt                  # Unmount
tune2fs -l /dev/sda1         # ext filesystem parameters
xfs_info /mountpoint         # xfs filesystem info

I/O Scheduler (for performance tuning)

cat /sys/block/sda/queue/scheduler
echo deadline > /sys/block/sda/queue/scheduler

Schedulers:

  • cfq → default, fair sharing.

  • deadline → low-latency, predictable.

  • noop → minimal scheduling (good for SSDs).

Disk Quotas (limit user/group usage)

edquota -u username
repquota -a

✅ Summary

  • Use lsblk, fdisk, parted for disk info.

  • Use df, du for capacity usage.

  • Use iostat, iotop, dstat for I/O performance.

  • Use fio, blktrace, smartctl for advanced testing & debugging.

  • Tune with I/O scheduler, quotas, filesystem settings.


👉 Pavan, do you want me to also prepare a single cheat sheet table that combines CPU + Memory + Disk commands (one-page reference for SRE/DevOps interviews)?

Last updated