Getting Information from /proc - Ubuntu Linux Toolbox: 1000+ Commands for Power Users (2013)

Ubuntu Linux Toolbox: 1000+ Commands for Power Users (2013)

Appendix C

Getting Information from /proc

IN THIS APPENDIX

· Viewing /proc information

· Changing /proc information variables

Originally intended to be a location for storing information used by running processes, the /proc filesystem eventually became the primary location for storing all kinds of information used by the Linux kernel. Despite the emergence of /sys to provide a more orderly framework for kernel information, many Linux utilities still gather and present data about your running system from /proc.

If you are someone who prefers to cut out the middleman, you can bypass utilities that read /proc files and read (and sometimes even write to) /proc files directly. By checking /proc, you can find out the state of processes, hardware devices, kernel subsystems, and other attributes of Linux.

Viewing /proc Information

Checking out information in files from the /proc directory can be done by using a simple cat command. In /proc, there is a separate directory for each running process (named by its process ID) that contains information about the process. There are also /proc files that contain data for all kinds of other things, such as your computer’s CPU, memory usage, software versions, disk partitions, and so on.

The following examples illustrate some of the information you can get from your Linux system’s /proc directory:

$ cat /proc/cmdline Shows options passed to the boot prompt

BOOT_IMAGE=/vmlinuz-3.2.0-37-generic root=/dev/mapper/ubuntutb-root ro

$ cat /proc/cpuinfo Shows information about your processor

processor : 0

vendor_id : GenuineIntel

cpu family : 6

model : 23

model name : Pentium(R) Dual-Core CPU E6300 @ 2.80GHz

stepping : 10

microcode : 0xa07

cpu MHz : 1603.000

cache size : 2048 KB

...

In the preceding example, the MHz speed may be well below your actual system speed if a CPU governor such as cpuspeed is running. The next example lists character and block devices:

$ cat /proc/devices Shows existing character and block devices

Character devices:

1 mem

4 /dev/vc/0

4 tty

4 ttyS

5 /dev/tty

...

Block devices:

1 ramdisk

259 blkext

7 loop

8 sd

$ cat /proc/diskstats Display disks, partitions, and statistics

1 0 ram0 0 0 0 0 0 0 0 0 0 0 0

1 1 ram1 0 0 0 0 0 0 0 0 0 0 0

...

8 0 sda 21883 11293 874900 194544 7813 9966 932416 1073344

0 120276 1267864

8 1 sda1 278 25 2310 11636 8 0 16 476 0 11960 12112

8 2 sda2 2 0 12 144 0 0 0 0 0 144 144

...

7 0 loop0 0 0 0 0 0 0 0 0 0 0 0

In the diskstats output just shown, you can see ramdisk (ram0, ram1, and so on) and loopback (loop0, loop1, and so on) devices. For hard disk partitions, the example shows statistics for the whole hard disk (sda) and each partition (sda1, sda2, and so on).

The 11 fields for the entire hard disk show (from left to right): total number of reads, number of reads merged, number of sectors read, number of milliseconds spent by all reads, number of writes completed, number of writes merged, number of sectors written, number of milliseconds spent writing, number of input/output requests currently in progress, number of milliseconds spent doing input/output, and weighted number of milliseconds spent doing input/output. Fields for a particular partition show (from left to right): number of reads issued, number of sectors read, number of writes issued, and number of sectors written.

$ cat /proc/filesystems List filesystem types in current kernel

nodev sysfs nodev means type is not used by any device

nodev rootfs

...

ext4 ext4 is used on a mounted block device

iso9660 iso9660 is used on a mounted block device

$ cat /proc/interrupts View IRQ channel assignments

CPU0 CPU1 CPU2 CPU3

0: 126 0 0 0 IO-APIC-edge timer

1: 3815 2223 4786 2596 IO-APIC-edge i8042

8: 1 0 0 0 IO-APIC-edge rtc0

9: 15351 14722 5296 4894 IO-APIC-fasteoi acpi

12: 268497 419436 322826 145849 IO-APIC-edge i8042

...

$ cat /proc/iomem Show physical memory addresses

00000000-0000ffff : reserved

00010000-0009efff : System RAM

0009f000-0009ffff : reserved

000a0000-000bffff : PCI Bus 0000:00

000c0000-000c7fff : Video ROM

000d0000-000dffff : PCI Bus 0000:00

000e0000-000fffff : reserved

000f0000-000fffff : System ROM

00100000-bdd9ffff : System RAM

01000000-0166a78b : Kernel code

...

$ cat /proc/ioports Show virtual memory addresses

0000-001f : dma1

0020-0021 : pic1

0040-0043 : timer0

0050-0053 : timer1

0060-0060 : keyboard

0064-0064 : keyboard

0070-0071 : rtc0

0080-008f : dma page reg

00a0-00a1 : pic2

00c0-00df : dma2

00f0-00ff : fpu

...

$ cat /proc/loadavg Shows 1, 5, and 15 minute load averages,

1.77 0.56 0.19 2/247 1869 running processes/total and highest PID

$ cat /proc/meminfo Shows available RAM and swap

MemTotal: 4014504 kB

MemFree: 3202120 kB

Buffers: 90048 kB

Cached: 492060 kB

SwapCached: 0 kB

Active: 377604 kB

Inactive: 274020 kB

Active(anon): 70136 kB

Inactive(anon): 25448 kB

Active(file): 307468 kB

...

$ cat /proc/misc Shows name/minor number of devices

229 fuse registered with misc major device (10)

236 device-mapper

173 agpgart

...

$ cat /proc/modules Shows loaded modules, memory size,

instances loaded, dependencies

load state, and kernel memory

bnep 18281 2 - Live 0x0000000000000000

rfcomm 47604 0 - Live 0x0000000000000000

bluetooth 180153 10 bnep,rfcomm, Live 0x0000000000000000

parport_pc 32866 0 - Live 0x0000000000000000

ppdev 17113 0 - Live 0x0000000000000000

ext2 73795 1 - Live 0x0000000000000000

snd_hda_codec_realtek 224173 1 - Live 0x0000000000000000

snd_hda_intel 33773 3 - Live 0x0000000000000000

...

$ cat /proc/mounts Show mounted local/remote file system info

rootfs / rootfs rw 0 0

sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0

proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0

udev /dev devtmpfs rw,relatime,size=19981k,nr_inodes=499543,mode=755 0 0

/dev/sda1 /boot ext2 rw,relatime,errors=continue 0 0

...

$ cat /proc/partitions Show mounted local disk partitions

major minor #blocks name

8 0 156290904 sda

8 1 248832 sda1

8 2 1 sda2

8 5 156039168 sda5

252 0 93495296 dm-0

252 1 4157440 dm-1

11 0 1048575 sr0

...

$ cat /proc/mdstat If using software RAID, show RAID status

Personalities : [raid1]

read_ahead 1024 sectors

Event: 1

md0 : active raid1 sdb1[1] sda2[0]

69738048 blocks [2/2] [UU]

unused devices: <none>

The /proc/mdstat file contains detailed status information on your software RAID devices if you have set up such a software RAID device. In this example, md0 is a RAID1 (mirror) composed of the /dev/sdb1 and /dev/sda1partitions. On the following line, there is one U for each healthy RAID member. If you lose a drive, the output would appear as [U_].

$ cat /proc/stat Shows kernel stats since system boot

cpu 58394 4008 18635 50620848 9216 0 151 0 0 0

cpu0 29693 2416 9551 25311091 4459 0 87 0 0 0

cpu1 28701 1592 9084 25309756 4757 0 64 0 0 0

intr 7720709 889 11 0 0 ...

ctxt 11180402

btime 1361187396

processes 5107

procs_running 1

procs_blocked 0

The /proc/stat file contains statistics related to CPU and process activities. The cpu line shows totals for all CPUs, while separate lines for each processor (cpu0, cpu1, and so on) show stats for each CPU on the computer. There are seven fields (from left to right) of CPU information: number of normal processes executed in user mode, niced processes executed in user mode, kernel mode processes, idle processes, iowait processes (waiting for input/output to finish), servicing interrupts (IRQ), and servicing soft IRQs.

$ cat /proc/swaps List information about swap space

Filename Type Size Used Priority

/dev/sda2 partition 4157436 0 -1

$ cat /proc/uptime Seconds since system booted/total seconds idle

2300251.03 2261855.31

$ cat /proc/version List kernel version and related compiler

Linux version 3.2.0-37-generic (buildd@allspice) (gcc version 4.6.3

(Ubuntu/Linaro 4.6.3-1ubuntu5) ) #58-Ubuntu SMP

Thu Jan 24 15:28:10 UTC 2013

Changing /proc Information

On some versions of Linux, some values in the /proc/sys directory can actually be changed on the fly. On Linux systems that allow it, you could simply echo a value to any file you want to change and that change immediately takes effect.

The preferred method of changing /proc/sys information on the fly is using the sysctl command. To change those settings on a more permanent basis, you should add entries to the /etc/sysctl.conf file. Here are some examples of the sysctl command:

$ sudo sysctl -A | less Display all kernel parameters

$ sudo sysctl -w net.ipv4.ip_forward=1 Turn on IPV4 packet forwarding

See Chapter 10 as well as the sysctl and sysctl.conf man pages for further information.