3 ms·
Performance. get_current_cpu_cache_line_size would need to run some code to determine the cache line size, and that code takes longer to run than using a cache
by JoshTriplett 10y ago
Performance. get_current_cpu_cache_line_size would need to run some code to determine the cache line size, and that code takes longer to run than using a cached value.
Along similar lines, if you have an optimized routine using specific CPU instructions, you don't want to call CPUID (or equivalent) on every call to find out if you have those instructions; you want to call it once and cache the answer. If it can return different answers on different CPUs in the same system, you need to use a different mechanism instead, such as asking the OS for the least common denominator features of available CPUs, or notifying the OS that you need to run on one of the more capable CPUs that has the feature you need.
- cm2187 10y agoStupid question but does that work in a virtualised environment where your program can be live-migrated to another physical machine with a different CPU?
- mjg59 10y agoNope. There's not really any alternative other than "Don't do that", or limit migration to machines that have a superset of the instructions on the original machine.
- cm2187 10y agoI presume that AWS or Azure wouldn't do that?
- pm215 10y agoUsually you configure the VM to only report CPUID values corresponding to lowest-common-denominator features on everything you might want to migrate to. Then as long as the guest code plays nicely and looks at the CPUID feature flags to see what it can use, it'll migrate happily. QEMU has support for this, for instance.
- tcas 10y agoI believe virtual machines will typically alter the CPUID a bit: https://tech.mendix.com/linux/2016/08/18/xen-cpuid-masking/ https://tech.mendix.com/linux/2016/08/18/xen-cpuid-masking/ You'll choose a lowest common denominator of features sets.
- Someone 10y agoAs others said, one typically pretends to run a fixed CPU on all CPUs. Also, for this specific case, I doubt migration will keep around the contents of the cache lines and their 'dirty' bits (corollary: it will be possible to reliably detect a move, if one is willing to continuously run code that detects cache-line timing differences)