NerdNOS.net Logo

Custom compiling in GCC for your specific system

Dan\'s post for SMP GCC compiling got my gears turning realizing that not everyone makes full use of the compiler flags when they build their packages. Hence, I write this.

When you either write your own code or download software to use, you usually have to compile it if you haven\'t decided to use a binary. Sometimes you have to use a binary when the code won\'t compile on your system for whatever reason but you always try to avoid that because of speed and overhead.

When you compile software, the massive library built into your compiler adds in all the instructions that are described by your programming language for you to compute after the linking stage. However as chips change so do the variances in instructions. There are common instructions that make up the x86 architecture but each chip family has added in their own optimizations in a battle for chip speed supremecy.

Your compiler (GCC) has the code within its libraries to optimize for your chipset. Newer chips may not be supported in your libraries for your version of GCC, you can check with the GCC website to see if your chip is supported in a later version. They will have articles describing chipset support and will list plans to either support yours or not. Almost everyone is supported except early adopters or people who use proprietary chipsets.

For examples sake today I\'m going to detail two different make commands along with their timed outputs so you can see the difference. I\'ll also touch on the compiler flags. The software I\'m going to use for demonstration purposes is ubench. We will compile with and without the compiler optimizations and note the differences in speeds.

Ubench - Ubench is a simple program to calculate how strong your CPU and memory is. It uses algorythms to see how many calculation cycles can be completed in the 3 minute slot, then reports them back to you. The higher the numbers, the better. Please take note that comparing these numbers across various platforms really doesn\'t say that one system is better than another at everything, it\'s just better at running Ubench tests. It really only helps us to test the same system after modifications even though we do like to drag race the results and talk **** with each other because we\'re Nerds.

With all that said let\'s get to compiling!

Ubench

The first test will be a ubench run using only very generic compiler flags. It is the equivalent of a vanilla install.

Below is the output of me doing the basic \"./configure && make\"


nerdnos# ./configure
==== Now type make to compile ubench for FreeBSD
nerdnos# make
cc -c signals.c
signals.c: In function `sigHandler\':
signals.c:35: warning: passing arg 1 of `siglongjmp\' from incompatible pointer type
cc -c cpubench.c
cpubench.c: In function `cpubench\':
cpubench.c:130: warning: passing arg 1 of `sigsetjmp\' from incompatible pointer type
cc -c membench.c
membench.c: In function `membench\':
membench.c:118: warning: passing arg 1 of `sigsetjmp\' from incompatible pointer type
cc -c ubench.c
cc -lm -o ubench signals.o cpubench.o membench.o ubench.o
-rwxr-xr-x 1 root wheel 10144 Jul 1 10:32 ubench


Don\'t worry about the warnings you see, if warnings scare you then you shouldn\'t be using any version of BSD/Linux haha. Now let\'s run this newly compiled ubench and see where it places us. You\'ll notice it says CC instead of GCC, CC is the Unix standard so in this case it points to CC which then points to GCC. Go figure, I guess it\'s for continuities sake.

The results are in:
nerdnos# ./ubench
Unix Benchmark Utility v.0.3
Copyright (C) July, 1999 PhysTech, Inc.
Author: Sergei Viznyuk
http://www.phystech.com/download/ubench.html
FreeBSD 6.2-STABLE FreeBSD 6.2-STABLE #3: Mon Jun 18 00:30:14 CDT 2007
root@nerdnos:/usr/obj/usr/src/sys/STREAMLINE i386
Ubench CPU: 79575
Ubench MEM: 43628
--------------------
Ubench AVG: 61601


Take note the CPU and memory scores. Let\'s compile again with some new flags, see the results, then I\'ll go over the differences. For this demonstration I have used the FreeBSD ports version of Ubench which the only real differences are it\'s been made easier to install and they offer compiler flags to make sure the regular scores aren\'t skewed away from regular systems like Linux. The reason I use this is it easily recognizes the compiler flags I\'ve added to my make.conf file in /etc.


===> Configuring for ubench-0.32
==== Now type make to compile ubench for FreeBSD
===> Building for ubench-0.32
cc -march=pentium3 -O2 -pipe -DFreeBSD -c signals.c
cc -march=pentium3 -O2 -pipe -DFreeBSD -c cpubench.c
cc -march=pentium3 -O2 -pipe -DFreeBSD -c membench.c
cc -march=pentium3 -O2 -pipe -DFreeBSD -c ubench.c
cc -march=pentium3 -O2 -pipe -DFreeBSD -lm -o ubench signals.o cpubench.o membench.o ubench.o


Now we run it...


nerdnos# ubench
Unix Benchmark Utility v.0.3
Copyright (C) July, 1999 PhysTech, Inc.
Author: Sergei Viznyuk
http://www.phystech.com/download/ubench.html
FreeBSD 6.2-STABLE FreeBSD 6.2-STABLE #3: Mon Jun 18 00:30:14 CDT 2007
root@nerdnos:/usr/obj/usr/src/sys/STREAMLINE i386
Ubench CPU: 127230
Ubench MEM: 46157
--------------------
Ubench AVG: 86693


Ok, as you can see the CPU performance jumped by almost half. We used two flags to get to this:

-march=pentium3 - the -march flag stands for Architecture. Some possible options are amd64, pentium3, pentium4, k8, etc. Since we use Katmai core P3 Xeons we fall into the pentium3 category.

-O2 - -O2 is the built in GCC optimization code. there are three different flags for the O flag each being 1, 2, or 3. Basically they are groups of compiler optimizations that take shortcuts to make code execute quicker.

-pipe - -pipe simply makes compiling faster.

Now that we see the difference let\'s remind ourselves that if you recompile your whole system using the flags that are best for you it\'s possible to cut down on the execute times of all your software, including operations in your kernel.

BEWARE the -O flags, going overboard with this will give compiler errors and sometimes system instability, usually -O3 is too much, -O2 is safer but also sometimes causes problems. Just be forewarned, you may have to back it off for certain applications.

There you have it. Now you can go off to google and find out which -march flag best suits your system and start experimenting.

Written by Ghost on 2008-10-13 15:50:18

Login to NerdNOS
Username:  
Password:  
Register with NerdNOS