Saturday, March 23, 2013

How to reduce system resources


Often one might need to reduce system resources while running benchmarks; i.e. run a benchmark on a system with X amount of RAM and then run it again on 2X the amount of RAM. This post shows you how.
OS: Centos
Kernel:2.6.18-8
  • Reducing system memory by creating a ramdisk on tmpfs:
Drop cache. Note that the "echo" instruction is a one-time deal. It immediately clears the cache, after which the kernel starts caching stuff again.: https://github.com/torvalds/linux/blob/master/fs/drop_caches.c

# sync; echo 3 > /proc/sys/vm/drop_caches

How much memory do we have?
# cat /proc/meminfo | head -3

MemTotal:      3895864 kB
MemFree:       3818628 kB
Buffers:          1800 kB


Create mount point for a 2G ramdisk:
# mkdir -p /mnt/ramdisk
# mount -t tmpfs -o size=2g none /mnt/ramdisk/

Still have almost same amount of free memory:

MemTotal:      3895864 kB
MemFree:       3818024 kB
Buffers:          1944 kB


# df -h

Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                      287G   55G  218G  21% /
/dev/sda1              99M   11M   83M  12% /boot
tmpfs                 1.9G     0  1.9G   0% /dev/shm
none                  2.0G     0  2.0G   0% /mnt/ramdisk


It is rumored that older versions of the Linux kernel will let you write to RAMdisk forever. If true, this could be a very bad thing, so please use this command carefully.
# dd if=/dev/zero of=/mnt/ramdisk/dummy bs=1024 count=2095100
2095100+0 records in
2095100+0 records out
2145382400 bytes (2.1 GB) copied, 10.9631 seconds, 196 MB/s

Lets check memory usage again:
# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                      287G   55G  218G  21% /
/dev/sda1              99M   11M   83M  12% /boot
tmpfs                 1.9G     0  1.9G   0% /dev/shm
none                  2.0G  2.0G     0 100% /mnt/ramdisk
# cat /proc/meminfo | head -3
MemTotal:      3895864 kB
MemFree:       1719076 kB
Buffers:          2008 kB


Do the math to verify:
# bc -l
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.

3818024/1024/1024
3.64115142822265625000
1719076/1024/1024
1.63943862915039062500


Remove dummy file:

# rm /mnt/ramdisk/dummy
rm: remove regular file `/mnt/ramdisk/dummy'? y

Did we free up memory?
# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                      287G   55G  218G  21% /
/dev/sda1              99M   11M   83M  12% /boot
tmpfs                 1.9G     0  1.9G   0% /dev/shm
none                  2.0G     0  2.0G   0% /mnt/ramdisk
# cat /proc/meminfo | head -3
MemTotal:      3895864 kB
MemFree:       3817468 kB
Buffers:          2056 kB

  • Reducing the # of cores available to your system:
How many cores do you have?
# cat /proc/cpuinfo |grep processor
processor       : 0
processor       : 1

Stick them into a bash variable:
# CORES=`cat /proc/cpuinfo |grep processor|awk '{print $NF}'`

You can't disable core 0. Otherwise this would be a very risky script to run :-)
# for i in $CORES; do echo "CORE $i"; ls -l /sys/devices/system/cpu/cpu${i}; echo "0" > /sys/devices/system/cpu/cpu${i}/online; done
CORE 0
total 0
-r-------- 1 root root 4096 Mar 23 14:13 crash_notes
drwxr-xr-x 2 root root    0 Mar 23 13:46 topology/
-bash: /sys/devices/system/cpu/cpu0/online: Permission denied
CORE 1
total 0
-r-------- 1 root root 4096 Mar 23 14:14 crash_notes
-rw------- 1 root root    0 Mar 23 14:17 online
drwxr-xr-x 2 root root    0 Mar 23 14:17 topology/

Zapped Core 1
# cat /proc/cpuinfo |grep processor
processor       : 0

Lets bring it back online:
# for i in $CORES; do echo "CORE $i"; ls -l /sys/devices/system/cpu/cpu${i}; echo "1" > /sys/devices/system/cpu/cpu${i}/online; done
CORE 0
total 0
-r-------- 1 root root 4096 Mar 23 14:13 crash_notes
drwxr-xr-x 2 root root    0 Mar 23 13:46 topology/
-bash: /sys/devices/system/cpu/cpu0/online: Permission denied
CORE 1
total 0
-r-------- 1 root root 4096 Mar 23 14:14 crash_notes
-rw------- 1 root root    0 Mar 23 14:18 online

Did it work?
# cat /proc/cpuinfo |grep processor
processor       : 0
processor       : 1

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home