Monday, January 22, 2007

Some might've noticed that the check-cpu script in the MySQLBUILD directory does not always succeed in detecting the right processor:

roland@roland-laptop:/opt/mysql/5.1/bitkeeper/BUILD$ ./check-cpuBUILD/check-cpu: Oops, could not find out what kind of cpu this machine is using.

When you dig a little deeper into that script, you will notice that the name of you particular processor (in my case: Intel(R) Core(TM)2 CPU T5600 @ 1.83GHz) is indeed not there and thus not checked. Finally you may find that the model and name of your processor are passed to the gcc compiler.

As far as I cans see the information is used to set the values of the --mtune and --march compiler options. What those are? Hehe! Ok, here's a partial result from man gcc:

-march=cpu-type: Generate instructions for the machine type cpu-type. The choices for cpu-type are the same as for -mtune. Moreover, specifying -march=cpu-type implies -mtune=cpu-type.

Mmm, not sure why -mtune=cpu-type is there in the first place then...

Anybody?

(PS: use /proc/cpuinfo to find out what kind of processor you really have....)

-mtune is used predominantly for instruction scheduling, gcc uses it when it's reordering instructions to favour a specific micro-architecture over others. Code which is tuned for one microarchitecture will run on another chip of the same architecture, but the reordering may hurt performance on these chips.

-mcpu tells it to generate code SPECIFICALLY for a certain chip, which obviously includes instruction reordering (thus why -mcpu implies -mtune), but will also cause the compiler to emit instructions specific to the target processor. This does not guarantee binary compatibility with other chips based on a different microarchitecture.

This is why I hate blogs / wiki answers. People post answers when they don't really know all the facts, even *after* the issue has been correctly addressed by a previous comment! I'm concluding that these type of people just like to here themselves type. :|

-march={cpu} : Generate a binary *only* for the specific cpu type (and later, compatible models [most later cpu's support earlier optimizations, or gracefully bypass them]). This flag should generate the faster code on x86 architectures. Fall back to the most recent (mainstream) model in your vendors line if yours is not yet available.

-mtune={cpu} : Tunes the binary for given cpu on x86 architecture. This means that the binary will still execute on a 386 (if it did in the first place), while also including optimization for the cpu supplied to the '-mtune=' switch.