oreilly.comSafari Books Online.Conferences.


Optimizing Linux System Performance

by Swayam Prakasha

Performance optimization in Linux doesn't always mean what we might think. It's not just a matter of outright speed; sometimes it's about tuning the system so that it fits into a small memory footprint. You'd be hard-pressed to find a programmer that does not want to make programs run faster, regardless of the platform. Linux programmers are no exception; some take an almost fanatical approach to the job of optimizing their code for performance. As hardware becomes faster, cheaper, and more plentiful, some argue that performance optimization is less critical--particularly people that try to enforce deadlines on software development. Not so, even today's most advanced hardware, combined with the latest in compiler optimization technology cannot come even close to the performance benefits that can be attained by fixing some small programs, or even going with an entirely different and much faster design.

Several ideas can be applied to programs that will make them perform better. By keeping these ideas in mind while writing (and revising) the code, we can expect better and faster programs. When we talk about performance, we need to consider several different things. One is the absolute amount of time it takes the software to complete a given task. Consider an example where even if a web server serves the client requests perfectly well, there can be a delay of a few seconds before the server begins to send the pages to the client every time. In such a case, the web server is failing to perform adequately in terms of the total time required to complete the task.

Another thing to consider is the amount of CPU time required by a program. CPU time is a measure of the time spent by the computer's processors to execute the code. Many programs tend to spend most of their time waiting for something to happen--input to arrive, output to be written to disk, etc. While waiting, the CPU will usually be serving other requests and hence the program is not using CPU time. However, some programs may be primarily CPU-bound programs and for such programs, a savings in the amount of CPU time required may result in a substantial savings in absolute time.

It is important to note here that if your program uses a lot of CPU time, it can slow down all the processes on your system. The CPU time can further be separated into system and user time. The system time is the amount of CPU time used by the kernel on your behalf. This could accrue by calling functions such as open() and fork(). The user time (i.e., amount of CPU time used by your program) might be used by string manipulations and arithmetic. A third thing to consider for performance is the time spent doing I/O. If we consider some programs such as network servers, they spend most of their lives handling I/O. Other programs spend little time with tasks related to I/O. Thus, I/O optimization can be very critical with some projects and completely unimportant with some others.

The performance optimization basically consists of the following steps

  1. Define the performance problem.
  2. Identify the bottlenecks and carry out a root cause analysis.
  3. Remove the bottlenecks by appropriate methodologies.
  4. Repeat steps 2 and 3 until we have a satisfactory resolution.

It is important to note here that bottlenecks occur at various points in a system. Determining the bottlenecks is a step-by-step procedure of narrowing down the root causes. Performance optimization is relatively a complex process that requires correlating many types of information with source code to locate and analyze performance problem bottlenecks.

When focusing on performance optimization, a system administrator needs certain tools to measure and monitor the situations as well as to identify the bottlenecks. On Linux, various tools are available to do this. Top is a nice little program and it provides real time information on the system level. The top utility is an interactive one and it provides a snapshot of the system at the moment it is being viewed. Mtop is a top-like utility for monitoring MySQL. It shows any slow queries or locks that are active at the time, with the exact SQL that is executed. Sysstat, procfs, sysctl and sysfs are valuable Linux tools and configuration commands that can be used for Linux performance measurement and tuning. Another tool, sar, can be used to collect a wealth of information on system activity (such as CPU utilization, memory usage, network and buffer usage, etc.) and it can be used to pinpoint a potential bottleneck.

Problems with Loops

Let us first understand the performance problems caused by loops. Loops magnify the effects of otherwise minor performance problems. This is because the code within the loop will get executed several times. Always make sure to move the code outside the loop that need not be executed each time through the loop.

Let us consider the following code segment

     int five;
     int cnt;
     int end;
     int 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."

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.

Pages: 1, 2, 3

Next Pagearrow

Linux Online Certification

Linux/Unix System Administration Certificate Series
Linux/Unix System Administration Certificate Series — This course series targets both beginning and intermediate Linux/Unix users who want to acquire advanced system administration skills, and to back those skills up with a Certificate from the University of Illinois Office of Continuing Education.

Enroll today!

Linux Resources
  • Linux Online
  • The Linux FAQ
  • Linux Kernel Archives
  • Kernel Traffic

  • Sponsored by: