DEV Community

Cover image for SPO600: Lab 4 - Building GCC
Amir Mullagaliev
Amir Mullagaliev

Posted on

SPO600: Lab 4 - Building GCC

Table of Contents

Introduction

For those who have never read my blog, I am writing about the Labs and Project for Seneca Polytechnic's Software Optimization and Portability course, or SPO600.

Today, I am going to talk about lab number four, during this lab, I am going to install and build the GCC compiler using Makefile.

First of all, I would like to share all of the resources I used in order to finish this lab:

What is GCC?

Before I start, you have to understand what it is GCC. The GNU Compiler Collection(GCC) is a collection of compilers from the GNU Project that supports various programming languages, hardware architectures and operating systems.

Where to Get Source Code?

The very first step of building the GCC is to get source code. I used the code provided by GNU here. Therefore, cloned it:

git clone git://gcc.gnu.org/git/gcc.git
Enter fullscreen mode Exit fullscreen mode

Since we are using two servers with two different architectures in this course: aarch64 and x86, I have to execute all the steps explained in this blog for both of them.

Going forward, let's understand the difference in those specifications that matter most in terms of this lab and make a prediction based on it. Mostly, we care about the speed of the build.

Aarch64 vs x86

CPU Specifications

Feature x86_64 aarch64
CPU Cores 10 cores, 20 threads (2 threads per core) 16 cores, 16 threads (1 thread per core)
Clock Speed 3.70GHz base, up to 4.70GHz Not specified
Vendor Intel ARM

Cache Memory

Cache Level x86_64 aarch64
L1 Data 320 KiB total (32 KiB per core) 512 KiB total (32 KiB per core)
L1 Instruction 320 KiB total (32 KiB per core) 768 KiB total (48 KiB per core)
L2 10 MiB total (1 MiB per core) 8 MiB total (1 MiB per 2 cores)
L3 19.3 MiB (shared) 8 MiB (shared)

Based on the architectural comparison, some factors will likely influence the build performance.

  • Thread Advantage: The x86_64 system has 20 threads vs. 16 threads on the ARM system.

  • Cache Considerations: ARM system has larger L1 caches, however x86_64 system has a significantly larger L3 cache - 19.3MiB vs 8MiB. GCC includes processing large amounts of code since x86_64 has a larger L3 cache, which may provide a meaningful advantage.

  • As of now, I know that x86_64 has extensive specialized instruction sets that may accelerate certain compilation tasks, while the ARM system uses a more streamlined approach.

My prediction is that the x86_64 system will complete the GCC build faster.

Build Preparation

First of all, I created two new directories:

mkdir -p git gcc-build-001
Enter fullscreen mode Exit fullscreen mode

Then, I went to the git directory and cloned gcc:

cd git
git clone git://gcc.gnu.org/git/gcc.git
Enter fullscreen mode Exit fullscreen mode

As a next step, I had to configure the GCC source code for a custom build, since we already have GCC located at /usr/local, we want to install another version at ~/gcc-test-001:

~/git/gcc/configure --prefix=$HOME/gcc-test-001
Enter fullscreen mode Exit fullscreen mode

Screen

Everyone who wants to build GCC understands that it takes at least 20 minutes in the best case, and it may take up to several hours. Therefore, if something goes wrong and you are connected to the server where the build is happening, it will crash.

To prevent users from it, bash uses a screen tool. It detaches the session and allows the user to do whatever he wants in parallel to anything happening on that detached session. Surprisingly, it is a pretty simple tool to utilize.

  • Create a detached session:
screen -RaD
Enter fullscreen mode Exit fullscreen mode
  • Run anything continues you want:
make
Enter fullscreen mode Exit fullscreen mode
  • Leave the session while it finishes the task using this key combination:
Ctrl+A+D
Enter fullscreen mode Exit fullscreen mode
  • Reconnect to the session:
screen -RaD
Enter fullscreen mode Exit fullscreen mode

Time to Build!

We are ready to start building the GCC before we have to detach the session:

screen -RaD
Enter fullscreen mode Exit fullscreen mode

Going forward, our Makefile is already located at ~/gcc-build-001, so we have to be inside this directory in order to start the build:

cd gcc-build-001
Enter fullscreen mode Exit fullscreen mode

Once we are inside the appropriate directory, it is time to perform the build by typing make. However, we want to compare the time it takes and save the stdout and stderr inside of the build.log file. Therefore, we need a little more complex command than just make:

time make -j 24 |& tee build.log
Enter fullscreen mode Exit fullscreen mode

I'll be back once it builds on both architectures with the results!

x86

x86_64 won the competition and finished first with a time of 47m37s.647 as I predicted!

Image description

AArch64

It took a year to finish. With a time of 124m55s, aarch64 architecture loses...as...expected...

Image description

Install the Build

After you've performed a build, it is time to install it by simply typing make install.

IMPORTANT!

To prove that we have successfully installed gcc, I will provide a result that I had a different version of gcc installed earlier by admin or by system.

Image description

Now I am ready to install it!

As you remember, during the building preparation stage, we configured the GCC source code for the custom build by typing ~/git/gcc/configure --prefix=$HOME/gcc-test-001. Eventually, after we have performed the installment, we can see that ~/gcc-test-001 appeared!

Image description

There is now located the installed GCC! To use it, we have to make a small adjustment to the system :

PATH=$HOME/gcc-test-001/bin:$PATH
Enter fullscreen mode Exit fullscreen mode

... to include the bin directory within the installation directory as the first directory!

Result

NOTE! As you can see, we have installed an experimental or development version of GCC, which differs from the initial one!

Image description

Building C Programs

This Development version of GCC is capable of creating and compiling simple C programs:

Image description

I could have stopped here. However, we have to run a couple of more experiments!

Experiments

This lab provides us only two experiments that answers two qustions:

  • How long does it take to rebuild GCC if we change the timestamp of one file?

  • How long does it take to rebuild GCC without making any changes, just by invoking the make command?

Changed Timestamp Experiment

First of all, we have to figure out how to change a timestamp...

I've done some research, and it turns out that it is as simple as using the touch command... Honestly, I used this command a lot, but to create new files, I have never applied this command to existing ones. It proves that every new day, we learn new things, no matter how much we already know...

Secondly, we have to find the file called passes.cc.

Steps I had taken to complete this experiment:

  • Find the file called passes.cc:
find ~/git/gcc -name "passes.cc"

result:
/home/amullagaliev/git/gcc/gcc/passes.cc
Enter fullscreen mode Exit fullscreen mode
  • Update timestamp:
touch /home/amullagaliev/git/gcc/gcc/passes.cc
Enter fullscreen mode Exit fullscreen mode
  • Get to ~/gcc-build-001
cd ~/gcc-build-001
Enter fullscreen mode Exit fullscreen mode
  • Rebuild the software by re-issuing the make command.

Result

Fortunately, it took only 59s to rebuild.

Image description

Rebuild Software Without Making Any Changes

This is the easiest part of the lab; I just have to re-issue the make command without any prior manipulations! Obviously, to measure time, I used: time make -j 24 |& build.log.

Result

It was the fastest build today that only took 15s. I am so happy to see this number :D

Image description

These experiments went as I expected.

Conclusion

This lab took so much time due to the build time of GCC. However, I really enjoyed it. I am satisfied with the results, even though I had to start from scratch and rebuild twice.

Unfortunately, I wasn't able to go through all the steps on aarch64, the only thing that I performed there was the build since the server doesn't have enough space... It has only 12MB which isn't enough for the installment of GCC.

Image description

Image description

Anyway, I may reach back with the experimental results later once we get some more space at the Aarch64 server. Thanks a lot to those who read this, I put a lot of effort into my blog posts!

Top comments (0)