Configuring simultaneous multithreading


By default, Compute Engine enables simultaneous multithreading (SMT) on all virtual machine (VM) instances. With SMT enabled, a single physical CPU core can run 2 virtual CPUs (vCPUs), each as separate threads. You can disable SMT by reducing the number of threads—the number of effective vCPUs—per core to 1. This document describes how and why to change the number of threads that run on each physical CPU core.

Considerations for configuring SMT

Performance
Reducing the number of threads that run on each physical CPU core can help improve performance of workloads that are highly-parallel or that perform floating point calculations.
Security
If a VM runs untrusted code, reducing the number of threads per core can help mitigate CPU vulnerabilities such as Microarchitectural Data Sampling.
Licensing
Some licensing agreements have requirements related to the number of vCPUs that you consume. Disabling SMT reduces the number of vCPUs for each core from 2 to 1, which might reduce your licensing costs. For more information, consult your licensing agreement.

Limitations

The number of threads per core is not configurable on machine types that have fewer than 2 vCPUs, for example, n1-standard-1 or shared-core machines such as e2-small.

Pricing

You are billed for the number of vCPUs defined by a VM's machine type, not the number threads that run on each core. For example, the n2-standard-8 machine type can run up to 8 vCPUs, which is 2 vCPUs for each of the 4 physical CPU cores. If you decide to only run 1 thread per core—effectively 4 vCPUs—you are still billed for 8 vCPUs. For more information about how you are billed for VMs, see VM instance pricing.

Configuring SMT on a new VM

Configure SMT on a new VM by using the following procedure.

gcloud

Configure SMT while creating a VM by using the following gcloud beta compute instances create command:

gcloud beta compute instances create VM_NAME \
    --zone=ZONE \
    --machine-type=MACHINE_TYPE \
    --threads-per-core=THREADS_PER_CORE

Replace the following:

  • VM_NAME: the name for the new VM.

  • ZONE: the zone for the new VM.

  • MACHINE_TYPE: the machine type for the new VM. Must be a machine type with 2 or more vCPUs.

  • THREADS_PER_CORE: the number of threads per physical core. Current processors support 2 threads per core for SMT, which is enabled by default. To disable SMT, set to 1.

API

Configure SMT while creating a VM by using the instances.insert method with the threadsPerCore field.

POST https://compute.googleapis.com/compute/beta/projects/PROJECT_ID/zones/ZONE/instances

{
  ...
  "advancedMachineFeatures": {
    ...
    "threadsPerCore": "THREADS_PER_CORE"
  },
  ...
}

Replace the following:

  • PROJECT_ID: the project ID.

  • ZONE: the zone for the new VM.

  • THREADS_PER_CORE: the number of threads per physical core. Current processors support 2 threads per core for SMT, which is enabled by default. To disable SMT, set to 1.

Configuring SMT on an existing VM

Configure SMT on an existing VM by using the following procedure.

gcloud

  1. Export the properties of the VM by using the following gcloud beta compute instances export command:

    gcloud beta compute instances export VM_NAME \
        --destination=YAML_FILE_PATH \
        --zone=ZONE
    

    Replace the following:

    • VM_NAME: the name of the VM from which to export properties

    • YAML_FILE_PATH: the path and file name of a .yaml file in which to save the exported configuration data

    • ZONE: the zone that contains the VM

  2. In the VM configuration file that was saved in FILE_PATH, update the value for threadsPerCore. If the value is not in the file, add the following:

    advancedMachineFeatures:
      threadsPerCore: THREADS_PER_CORE
    
  3. Update the VM with the new count of threads per core by using the following gcloud beta compute instances update-from-file command:

    gcloud beta compute instances update-from-file VM_NAME \
        --source=FILE_PATH \
        --most-disruptive-allowed-action=RESTART \
        --zone=ZONE
    

    Replace the following:

    • VM_NAME: the name of the VM to update

    • FILE_PATH: the path to the updated VM configuration file

    • ZONE: the zone that contains the VM to update

API

Update the threads per core of the VM by using the following instances.update method:

PUT https://compute.googleapis.com/compute/beta/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME?most_disruptive_allowed_action=RESTART

{
  ...
  "advanced_machine_features": {
    ...
    "threadsPerCore": "THREADS_PER_CORE"
  },
  ...
}

Replace the following:

  • PROJECT_ID: the ID of the project

  • ZONE: the zone that contains the VM

  • VM_NAME: the name of the VM from which to export properties

  • THREADS_PER_CORE: the number of threads per core

Verifying the SMT configuration

Verify the SMT configuration by using the procedure that corresponds to the OS that is running on the VM.

Linux

Verify the SMT configuration on Linux-based VMs by using the following procedure:

  1. Connect to the Linux VM.

  2. Run the lscpu command.

    lscpu
    
  3. In the output, verify the value for the number of threads per core.

    In the following sample output, the value for the number of threads per core is 1.

    ...
    CPU(s):              1
    On-line CPU(s) list: 0
    Thread(s) per core:  1
    Core(s) per socket:  1
    Socket(s):           1
    NUMA node(s):        1
    Vendor ID:           GenuineIntel
    CPU family:          6
    Model:               85
    Model name:          Intel(R) Xeon(R) CPU
    ...
    

Windows

Verify the SMT configuration on Windows-based VMs by using the following procedure:

  1. Connect to the Windows VM.

  2. Open the command-line interface for Windows Management Instrumentation (WMI).

    wmic
    
  3. View the number of cores and the number of logical processors by running the following command:

    CPU Get NumberOfCores,NumberofLogicalProcessors
    
  4. In the output, verify the value for the number of threads per core.

    In the following sample output, the value for the number of threads per core is 1.

    NumberOfCores  NumberOfLogicalProcessors
    1              1
    

What's next