DMA -- very much faster harddisk access
From Linux Solutions
Contents |
Switching on and off DMA
DMA stands for Direct Memory Access. This means that some components of the computer are able to copy data to the memory (RAM) directly. Normally this is done through the CPU. When it is done through the CPU, the CPU is busy coping data. So there is less capacity for applications, which are running much slower. Additionally: It takes much longer to transfer data through the CPU, because it has to be loaded into the CPU and than from the CPU to the memory. DMA does it directly. It mainly used on Harddrives, Soundcards, Video, Network Cards, etc.
DMA for Harddrive
Test, if it is on:
hdparm -d /dev/hda
It says either using_dma = 1 (on) or using_dma = 0 (off)
You can switch it on by
hdparm -d1 /dev/hda
and off by
hdparm -d0 /dev/hda
Unfortunately, this configuration becomes lost after reboot.
Boot-Script: Switching DMA permanently on
However, this little script (attachment) turns it on permanently
How it works:
1 #!/bin/bash 2 if [ "$(hdparm -d /dev/hda | tail -n1 | grep on)" != "" ]; then 3 echo DMA already enabled ... 4 exit 0 5 fi 6 if [ -e /etc/init.d/enabledma ]; then 7 echo "Already installed: /etc/init.d/enabledma" 8 exit 0 9 fi 10 cat <<EOF > /etc/init.d/enabledma 11 #!/bin/bash 12 echo "Enabling DMA for /dev/hda..." 13 hdparm -d1 /dev/hda 14 EOF 15 if ! [ -e /etc/init.d/enabledma ]; then 16 echo "Installation failed" 17 exit 0 18 fi 19 chmod a+rx /etc/init.d/enabledma 20 for i in 2 3 4 5; do 21 if [ -e /etc/init.d/rc$i.d ]; then 22 ln -s /etc/init.d/enabledma /etc/init.d/rc$i.d/K00enabledma 23 fi 24 if [ -e /etc/rc$i.d ]; then 25 ln -s /etc/init.d/enabledma /etc/rc$i.d/K00enabledma 26 fi 27 done 28 /etc/init.d/enabledma 29 echo "Installation completed."
Redirecting outputs to another Program as Parameter
Line 2: Test if DMA is already enabled, exit if yes. Execute hdparm -d. Get the last line of its output: tail -n1. And grep it for on. If on if in this line, grep writes this line out. "$(command)" executes command and puts all outputs of command insteat of $() into the line.
Example:
andreas@Whitey:/tmp > date Tue Nov 7 00:42:45 EAT 2006 andreas@Whitey:/tmp > $(date) bash: Tue: command not found andreas@Whitey:/tmp > echo command parameter $(date) command parameter Tue Nov 7 00:43:14 EAT 2006
If-Example
if [ a != b ]; then c fi
executes c if there a and b are NOT equal. If DMA enabled, a line comes out of the $()-command, what is not equal "" => exit.
Line 6: Tests if file /etc/init.d/enabledma exits. If it does, exit.
Line 10: cat <<EOF > file writes all what is typed in into file. Until
EOF is typed in. In this case, lines 11, 12, and 13 are written into
/etc/init.d/enabledma. Note, that there is an empty line between 11
and 12 which is written to the file too.
Line 15: Like line 6 but tests if file do not exists. If file
/etc/init.d/enabledma does not exits right after if was written, it is
assumed that the installation failed. Exit.
chmod
Line 19: The script, which was written in line 10 is now set to readable and executable for all users.
chmod XYZ file X: u: the user who owns the file g: other users in the file's group, o: other users not in the file's group a: all users Y: +: set right -: remove right Z: r: readable w: writable x: executable (in case of directories: browsable)
loop: for
Line 20: Loop:
for i in 1 2 3; do a; b; done
executes a and b N times. N is the number of parameter between the in and the ;. In this case three times. Additionally: The environment variable $i is set to each parameter one after the other.
Example:
andreas@Whitey:/tmp > for i in 2 3 4 5; do echo $i; done 2 3 4 5
Example 2: Rename all files in the actual directory from filename for filename.txt:
for i in *; do echo mv $i ${i}.txt; done
Remove echo to execute the mv-command in real. The ${i} means the same as $i but it is possible to write something right after it.
In this case the loop is used to go through all runlevels in which the dma should be enabled: 2 3 4 5.
Runlevel: Where to put the Script
E.g. every script in the directory /etc/init.d/rc3.d (or /etc/rc3.d, depends on the distribution) is executed when the system starts in runlevel 3.
# Runlevel 0 is halt. # Runlevel 1 is single-user. # Runlevels 2-5 are multi-user. (5 is for X -- graphical user interface, I think) # Runlevel 6 is reboot.
in line 21 it is tested if directory /etc/init.d/rc$i.d exists. If yes, a symbolic links is set from the dma-script /etc/init.d/enabledma, written in line 10, is set to /etc/init.d/rc$i.d/K00enabledma. So we hope it is executed during the next startup. Note: Because of the loop, the command is executed for the runlevels 2 3 4 5. These numbers are put into the commandline instead of the $i.
The files in these startup-directory are executed in there alphabetical order. K00 should be executed right in the beginning. S99 at the end. If you want to put you script into the startup-sequence, do this:
- put your script into /etc/init.d/script
- make it executable
- link it into the runlevel, where you want it to be executed:
- e.g.: ln /etc/init.d/script /etc/init.d/rc5.d/S99script
Line 24 and 25 does the same for the other possibility of the
startup-directory.
Line 28 executes the dma-ebable-script and enables dma for now.
Andreas B.M. Hofmeier