编译程序在虚拟机中运行时,march和mtune应该设置成什么?

问题描述 投票:0回答:2

随着虚拟机从属于主机提供的任何内容,应该向 gcc 提供哪些编译器标志?

我通常会认为

-march=native
是您在为专用盒子编译时会使用的东西,但是
-march=native
将如 这篇文章 中所示的细节让我非常谨慎地使用它。

那么...在 VM 内部设置什么

-march
-mtune


举个具体的例子……

我现在的具体情况是在基于 KVM 的“云”主机内的 linux 来宾中编译 python(以及更多),我无法真正控制主机硬件(除了“简单”的东西,如 CPU GHz m CPU 计数,和可用内存)。目前,

cpuinfo
告诉我我有一个“AMD Opteron(tm) 处理器 6176”,但老实说,我(还)不知道这是否可靠,以及客人是否可以转移到我身上的不同架构来满足主机的基础设施改组需求(听起来很毛茸茸/不太可能)。

我能真正保证的是我的操作系统,它是一个 64 位 linux 内核,其中

uname -m
产生
x86_64
.

linux gcc virtualization compiler-optimization
2个回答
4
投票

GCC 4.6.3 标准 C++ 库手册(我希望是相关的)3.17.14 Intel 386 和 AMD x86-64 选项部分的一些不完整和乱序的摘录。

-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. 

-mtune=cpu-type
  Tune to cpu-type everything applicable about the generated code,  
  except for the ABI and the set of available instructions.  
  The choices for cpu-type are:
    generic
      Produce code optimized for the most common IA32/AMD64/EM64T processors. 
    native
      This selects the CPU to tune for at compilation time by determining
      the processor type of the compiling machine. 
      Using -mtune=native will produce code optimized for the local machine
      under the constraints of the selected instruction set.
      Using -march=native will enable all instruction subsets supported by
      the local machine (hence the result might not run on different machines). 

我发现最有趣的是

specifying -march=cpu-type implies -mtune=cpu-type
。我对其余部分的看法是,如果您指定 both
-march
&
-mtune
你可能太接近调整矫枉过正了。

我的建议是只使用

-m64
你应该足够安全,因为你在 x86-64 Linux 中运行,对吗?

但是如果您不需要在另一个环境中运行并且您感到幸运和容错那么

-march=native
可能也适合您。

-m32
  The 32-bit environment sets int, long and pointer to 32 bits  
  and generates code that runs on any i386 system.     
-m64
  The 64-bit environment sets int to 32 bits and long and pointer  
  to 64 bits and generates code for AMD's x86-64 architecture.

物有所值...

出于好奇,我尝试使用您引用的文章中描述的技术。我在作为 VMware Player 来宾运行的 64 位 Ubuntu 12.04 中测试了 gcc v4.6.3。 VMware VM 使用 Intel Pentium 双核 E6500 CPU 在 Windows 7 桌面上运行。

gcc 选项

-m64
被替换为
-march=x86-64 -mtune=generic
.

但是,使用

-march=native
编译会导致 gcc 使用下面所有更具体的编译器选项。

-march=core2 -mtune=core2 -mcx16 
-mno-abm -mno-aes -mno-avx -mno-bmi -mno-fma -mno-fma4 -mno-lwp 
-mno-movbe -mno-pclmul -mno-popcnt -mno-sse4.1 -mno-sse4.2 
-mno-tbm -mno-xop -msahf --param l1-cache-line-size=64 
--param l1-cache-size=32 --param l2-cache-size=2048

所以,是的,正如 gcc 文档在“使用 -march=native ... 结果可能无法在不同的机器上运行”时所说的那样。为了安全起见,您应该只使用

-m64
或者它显然等同于
-march=x86-64 -mtune=generic
进行编译。

我看不出你对此会有什么问题,因为这些编译器选项的目的是 gcc 将生成能够在 any x86-64/amd64 兼容 CPU 上正确运行的代码。 (没有?)

坦率地说,我对 gcc

-march=native
CPU 选项的具体程度感到震惊。我不知道如何使用 CPU 的 L1 缓存大小为 32k 来微调生成的代码。但显然如果有办法做到这一点,那么使用
-march=native
将允许gccto做到这一点。

我想知道这是否会导致任何明显的性能改进?


1
投票

有人认为来宾操作系统报告的 CPU 架构是您应该优化的。否则,我会称之为错误。有时可能会有错误的正当理由,但是......

请注意,并非所有管理程序都必须相同。

查看特定管理程序的邮件列表可能是个好主意。

© www.soinside.com 2019 - 2024. All rights reserved.