Pages

Sunday, September 9, 2012

Proc File System


The /proc directory also called as /proc file system contains a hierarchy of files which represent the current state of the kernel , allowing applications and users to peer into the Kernel view of the system.

Simply a /proc file system is the view of your computer. By using the /proc file system we can find various information including hardware connected to the system , memory,cpus,hard drives and so on.

The Proc file system contains directories and virtual files. These virtual files can present information from the kernel to the user and also allows sending information from user to the kernel.
When we view the files in the /proc file system , like ‘ll /proc’



We can see a series of numbers on the left hand side. These numbers represent process running in the system. The first process that starts when the linux starts is ‘init’, hence this process will have the process ID ‘1’.When the process terminates, the corresponding number in this location vanishes.

The Important Files in the /proc file system are

/proc/Cpuinfo contains the information established by the kernel about the processor at boot time, e.g., the type of processor, including CPU family, processor information and features.

/proc/Kcore is like a ‘alias’ for the memory in the computer. So this has the same size of the amount of RAM we have and if you read it as a file , the kernel does the memory read. The contents of the file are designed to be examined by a debugger like gdb and are not in human readable format.

/proc/loadavg contains the system load averages for the last 1, 5 and 15 minutes, along with the number of processes currently running and the total number of processes.
Localhost:root-proc $ cat loadavg
0.12 0.14 0.10 1/534 15289

/proc/Meminfo contains the memory usage. It contains information about how much of memory and swap available , in use and various other information regarding memory usage of the system
localhost:root -proc $ cat meminfo
MemTotal:           4194304 kB
MemFree:            581520 kB
Buffers:               711776 kB
Cached:              604880 kB
.
.

Much of the information here is used by the free, top, and ps commands. In fact, the output of the free command is similar in appearance to the contents and structure of /proc/meminfo. But by looking directly at /proc/meminfo, more details are revealed:

MemTotal — Total amount of physical RAM, in kilobytes.
MemFree — The amount of physical RAM, in kilobytes, left unused by the system.
Buffers — The amount of physical RAM, in kilobytes, used for file buffers.
Cached — The amount of physical RAM, in kilobytes, used as cache memory.
SwapCached — The amount of swap, in kilobytes, used as cache memory.
Active — The total amount of buffer or page cache memory, in kilobytes, that is in active use. This is memory that has been recently used and is usually not reclaimed for other purposes.
Inactive — The total amount of buffer or page cache memory, in kilobytes, that are free and available. This is memory that has not been recently used and can be reclaimed for other purposes.
HighTotal and HighFree — The total and free amount of memory, in kilobytes, that is not directly mapped into kernel space. The HighTotal value can vary based on the type of kernel used.
LowTotal and LowFree — The total and free amount of memory, in kilobytes, that is directly mapped into kernel space. The LowTotal value can vary based on the type of kernel used.
SwapTotal — The total amount of swap available, in kilobytes.
SwapFree — The total amount of swap free, in kilobytes.
Dirty — The total amount of memory, in kilobytes, waiting to be written back to the disk.
Writeback — The total amount of memory, in kilobytes, actively being written back to the disk.
Mapped — The total amount of memory, in kilobytes, which have been used to map devices, files, or libraries using the mmap command.
Slab — The total amount of memory, in kilobytes, used by the kernel to cache data structures for its own use.
Committed_AS — The total amount of memory, in kilobytes, estimated to complete the workload. This value represents the worst case scenario value, and also includes swap memory.
PageTables — The total amount of memory, in kilobytes, dedicated to the lowest page table level.
VMallocTotal — The total amount of memory, in kilobytes, of total allocated virtual address space.
VMallocUsed — The total amount of memory, in kilobytes, of used virtual address space.
VMallocChunk — The largest contiguous block of memory, in kilobytes, of available virtual address space.
HugePages_Total — The total number of hugepages for the system. The number is derived by dividing Hugepagesize by the megabytes set aside for hugepages specified in /proc/sys/vm/hugetlb_pool. This statistic only appears on the x86, Itanium, and AMD64 architectures.
HugePages_Free — The total number of hugepages available for the system. This statistic only appears on the x86, Itanium, and AMD64 architectures.
Hugepagesize — The size for each hugepages unit in kilobytes. By default, the value is 4096 KB on uniprocessor kernels for 32 bit architectures. For SMP, hugemem kernels, and AMD64, the default is 2048 KB. For Itanium architectures, the default is 262144 KB. This statistic only appears on the x86, Itanium, and AMD64 architectures.

/proc/swaps measures swap space and its utilization. For a system with only one swap partition, the output of /proc/swaps may look similar to the following:
[root@ras proc]# cat swaps
Filename                                Type            Size    Used    Priority
/dev/sda9                               partition       6136788 0       -1

First column tells about the type of swap space, the total size, and the amount of space in use (in kilobytes). The priority column is useful when multiple swap files are in use. The lower the priority, the more likely the swap file is to be used.

/proc/Stat contains various statistics about the kernel activity. The amount of usage the kernel has made with the system resources.

Explaining stat
When you execute

localhost:root-proc $ cat /proc/stat
cpu  101365990 2076376 119873084 1542606033 8471788 0 2353737 2880442
cpu0 49041138 1251727 39287515 789292472 6962731 0 2140388 1837752
cpu1 52324852 824649 80585569 753313561 1509057 0 213348 1042689
intr 4781453304 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 . . . . (will show a lot of values)
ctxt 8301802579
btime 1338526184
processes 55357479
procs_running 2
procs_blocked 0

The first 3 lines starting with CPU identify the amount of time CPU has spent performing different kinds of works.
The Columns are
         User             Nice           System          Idle                IOwait        IRQ  SoftIRQ    
cpu  101365990 2076376 119873084 1542606033  8471788  0     2353737 2880442
(Not Sure about the last value)

intr 4781453304 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 . . . . (will show a lot of values)
The intr line gives you the count of interrupts served since boot time for system interrupts.This’4781453304’ is the total of all interrupts serviced.
ctxt 8301802579
The ‘ctxt’ column gives the number of context switchs across all the CPU’s.

btime 1338526184
The btime gives you the time at which system booted in seconds.

processes 55357479
This tells us about the number of process and threads created.

procs_running 2
This tells us about the number of process running.

procs_blocked 0
This tells us about the number of process blocked or waiting for an I/O operation.

/proc/uptime contains the amount of time in seconds that the system has been running, and the amount of that time that it has been idle.

Localhost:root-proc $ cat uptime
8898720.25 7970338.08

/proc/version contains the kernel version information that lists the version number, when it was compiled and who compiled it.

Localhost:root-proc $ cat version
Linux version 2.6.18-308.1.1.el5xen (mockbuild@hs20-bc2-3.build.redhat.com) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-52)) #1 SMP Fri Feb 17 17:11:09 EST 2012

/proc/Cmdline tells us about the parameters passed to kernel at the time it started.
Localhost:root-proc $ cat cmdline
 ro root=/dev/Vol00_vx1423/root console=xvc0

This tells us that the kernel is mounted as read only (ro) , located on the logical volume Vol00_vx1423.
Console says the console booting is supported and no GUI is available.

/proc/Devices This file displays the various character and block devices currently configured (not including devices whose modules are not loaded).

Localhost:root-proc $ cat devices
Character devices:
  1 mem
  4 /dev/vc/0
  4 tty
  5 /dev/tty
  5 /dev/console
  5 /dev/ptmx
  6 lp
  7 vcs
 10 misc
 13 input
 29 fb
128 ptm
136 pts
162 raw
180 usb
189 usb_device
202 cpu/msr
203 cpu/cpuid
204 xvc
253 hidraw
254 pcmcia

Block devices:
  1 ramdisk
  9 md
202 xvd
253 device-mapper
254 mdp

The output shows u the major number and the device. The output of the /proc/devices is divided into 2 sections: character devices and block devices

/proc/Device Files: Under linux each and every hardware device is treated as a file. a device file allows users to access hardware devices so that the users do not need to get into the details of the hardware. There are 2 types of device files available,

Block Special Files: These files talk to the device block by block.        Ex : hard disk
Character Files: These files talk to the device character by character.   Ex : terminal
A character file contains the first letter as 'c' and block files contain 'b' as first letter.

/proc/filesystems lists all the file systems that were compiled into our kernel.
Localhost:root-proc $ cat filesystems
nodev   sysfs
nodev   rootfs
nodev   bdev
nodev   proc
nodev   cpuset
nodev   binfmt_misc
nodev   debugfs
nodev   securityfs
nodev   sockfs
nodev   usbfs
nodev   pipefs
nodev   anon_inodefs
nodev   futexfs
nodev   tmpfs
nodev   inotifyfs
nodev   eventpollfs
nodev   devpts
            ext2
nodev   ramfs
            iso9660
nodev   mqueue
            ext3
nodev   rpc_pipefs
the ‘nodev’ column says that the file system does not require a device for mounting (virtual file system)

/proc/ crypto tells us about the installed cryptographic ciphers installed in the system

Localhost:root-proc $ cat crypto
name         : crc32c
driver         : crc32c-generic
module       : kernel
priority      : 0
type          : digest
blocksize    : 32
digestsize   : 4
name         : sha1
driver        : sha1-generic
module      : kernel
priority      : 0
type          : digest
blocksize    : 64
digestsize   : 20

/proc/dma tells us about the registered DMA channels in use
Localhost:root-proc $ cat dma
4: cascade

/proc/ interrupts records the number of interrupts per IRQ.

Localhost:root-proc $ cat interrupts
           CPU0              CPU1
256:   1583733226          0     Dynamic-irq  timer0
257:   26747867              0     Dynamic-irq  resched0
258:         37                   0     Dynamic-irq  callfunc0
259:        325                  0     Dynamic-irq  xenbus

The first column is the IRQ number, the second and third says about the cpu in the system which has its own column and its own number of interrupts per IRQ. The next column is the type of the interrupt and last column is the device that is located at that interrupt.

/proc/ iomem shows you the current map of system memory for each physical device.

 Localhost:root-proc $ cat iomem
000a0000-000bffff : Video RAM area

/proc/ ioports lists ports that are registered for input and output communication for a device.
Localhost:root-proc $ cat ioports
0000-001f : dma1
0020-0021 : pic1
0040-0043 : timer0
0050-0053 : timer1
0060-0060 : keyboard
0064-0064 : keyboard
0080-008f : dma page reg

/proc/kmg is used to hold message generated by the kernel. These messages are then picked up by dmesg

/proc/ modules list all the modules loaded into the kernel. The same information can be viewed using ‘lsmod’ command.

/proc/ mounts list all the mounts in use by system.

/proc/partitions contain partition block allocation information.
Localhost:root-proc $ cat partitions
major minor  #blocks  name

 202     0   44040192 xvda
 202     1     522081 xvda1
 202     2   41415570 xvda2
 253     0    2097152 dm-0

/proc/fs specifies which file system are exported much like nfs and so on.

/proc/tty specifies the available and currently being used tty devices on the system.

/proc/PID

As we said earlier, each process running will have a corresponding number in the /proc file system. Each process directory contains

Cmdline : command line issued when starting the process
Cwd : a sym link to the current working directory
Environ : a list of environment variables for the process. The variable is shown in caps where as the value is shown in small case
Exe: a sym link to the executable of this process
Mem : memory held by the process. This cant be viewed by users.
Root : a link to the root directory of the process
Stat : the status of the process
Statm : the status of the memory in use for the process

Localhost:root-4105 $ cat statm
159358 24073 2418 10 0 154807 0

The seven columns relate to different memory statistics for the process. From left to right, they report the following aspects of the memory used:
Total program size, in kilobytes.
Size of memory portions, in kilobytes.
Number of pages that are shared.
Number of pages that are code.
Number of pages of data/stack.
Number of library pages.
Number of dirty pages.


Status : the status of the process in detailed format.
Fd : information containing all of the file descriptors for a particular process.
Maps : list of memory maps to the various executables and library associated with the process.
Wchan: contains the name of the sys call the process is currently executing. It contains ‘-‘ if the process is not in any sys call.

More To Come , Happy Learning