16 Optimizing Linux System Performance 7043
16 Optimizing Linux System Performance 7043
Page 1 of 5
http://www.linuxdevcenter.com/lpt/a/7043
13/08/2007
Page 2 of 5
five;
cnt;
end;
temp;
for (cnt=0; cnt < 2* 100000 * 7/ 15 + 6723;
cnt += (5-2)/2
{
temp = cnt / 16430;
end = cnt;
five = 5;
}
printf("printing values of five=%d and end = %d\n", five,
end);
If we carefully observe the code, we can see that several things can be moved outside the loop. The
value of "end" can be calculated only once, after the loop is through. Also, the assignment to the
variable five is a dead code and it makes much sense to take these out of the loop.
When we talk about optimizing the performance, we need to make sure that unless there is an
absolute need to use it, we should never try to use floating point data types such as "float" and
"double." This is because of the fact they take more time to calculate than do their integer
counterparts. Also, if we have a function that is called very frequently, it is better to declare it as
"inline."
http://www.linuxdevcenter.com/lpt/a/7043
13/08/2007
Page 3 of 5
Also, another way to improve the performance is to increase the block size. As we know, many
operations are done on blocks of data. By increasing the block size, we will be able to transfer more
data at once. This will reduce the frequency with which we call more time consuming.
Consider a scenario where a web server might fork for each new request. This is not a good
practice, and select() can be used for multiplexing.
exec: This is one used immediately after a fork. This call can be very expensive as the new
program will have to a lot of initialization such as loading libraries, etc.
system: This invokes a shell to run the specified command and invoking a shell can be quite
expensive. Therefore, frequently using a system is definitely a bad idea.
If we come across a code piece such as system("ls /etc"); we can see how expensive this is. The
program first has to fork and execute the shell. The shell needs to do initialization and then it forks
and executes ls. Definitely not a piece of code to desire.
The first step in getting the system tweaked for both speed and reliability is to chase down the latest
versions of required device drivers. Another useful key is to understand what the bottlenecks are and
how they can be taken care of. We can come to know about the various bottlenecks by running
various system monitoring utilities, such as the top command.
at the end of /etc/rc.d/rc.local file will set the support for 32 bit I/O.
GNU profiler (gprof)
After we have taken enough measures in optimizing our code, the compiler can be helpful with
optimization as well. One tool that we can use to analyze our program's execution is the GNU
http://www.linuxdevcenter.com/lpt/a/7043
13/08/2007
Page 4 of 5
profiler (gprof). With this, we can come to know where the program is spending most of its time.
With profile information we can determine which pieces of program are slower than expected. These
sections are definitely good candidates for to be rewritten so that program can execute faster. The
profiler collects data during the execution of a program. Profiling can be considered as another way
to learn the source code.
o sample_2007 sample_2007.c
Note here that pg option enables the basic profiling support in gcc. The program will run somewhat
slower when profiling is enabled. This is because of the fact that it needs to spend time in collecting
data as well. The profiling support in the program creates a file named gmon.out in the current
directory. This file is later used by gprof to analyze the code.
We can run the following command to get the output (which we have redirected to a file):
$ gprof sample_2007 gmon.out > output.txt
gprof is useful not only to determine how much time is spent in various routines, but it also tells you
which routines invoke other routines. By using gprof, we will be able to know which sections of our
code are causing the largest delays. Analyzing the source code with gprof is considered as an
efficient way determining which function is using a large percentage of the overall time spent in
executing the program.
References
Optimizing Linux Performance: A Hands-On Guide to Linux performance tools , by Philip G.
Ezolt, Prentice Hall PTR
Linux Debugging and Performance Tuning: Tips and Techniques, by Steve Best, Prentice Hall
PTR
http://www.linuxdevcenter.com/lpt/a/7043
13/08/2007
Page 5 of 5
http://www.gnu.org/software/binutils/manual/
http://www.yolinux.com/TUTORIALS/LinuxTutorialOptimization.html
Performance Tuning for Linux Servers, by Badari Pulavarty, Gerrit Huizenga, Sandra K.
Johnson, IBM Press
Swayam Prakasha has been working in information technology for several years, concentrating on
areas such as operating systems, networking, network security, electronic commerce, Internet
services, LDAP, and Web servers. Swayam has authored a number of articles for trade publications,
and he presents his own papers at industry conferences. Currently he works at Unisys Bangalore in
the Linux Systems Group.
http://www.linuxdevcenter.com/lpt/a/7043
13/08/2007