Set the number of threads per core


Simultaneous multithreading (SMT), which is known on Intel processors as Hyper-Threading Technology (HTT), lets a CPU core run as two hardware multithreads. On Compute Engine, each virtual CPU (vCPU) is implemented as a single hardware multithread, and two vCPUs share each physical CPU core by default.

Compute Engine lets you manually set the value for the number of threads per core. For example, workloads with the following concerns might benefit from setting the number of threads per core to 1:

  • Performance: Reducing the number of threads that run on each physical CPU core can help improve the 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. Reducing the number of threads per core from 2 to 1 might reduce your licensing costs. For more information, consult your licensing agreement.

Limitations

  • You cannot set the number of threads per core on VMs that run on the Tau T2A (Preview) or the Tau T2D machine type or on machine types that have fewer than 2 vCPUs, for example, n1-standard-1 or shared-core machines such as e2-small.

  • In the console, when the number of threads per core is set to 1, the CPU utilization metric can only scale up to a maximum of 50%. When the number of threads per core is set to 2, the CPU utilization metric can scale up to 100%.

Pricing

You are billed for the number of vCPUs defined by a VM's machine type, not the number of 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, with the n2-standard-8 machine type, 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.

Change the number of threads per core during VM creation

To change the number of threads per core during VM creation, use the console, the gcloud CLI, or the Compute Engine API.

Permissions required for this task

To perform this task, you must have the following permissions:

Console

To change the number of threads per core during VM creation, use the following console procedure:

  1. In the console, go to the Create an instance page.

    Go to Create an instance

  2. In the Name field, enter a name for the VM.

  3. Choose a Region and a Zone for the VM.

  4. Choose a Machine family and a supported Machine type.

  5. Click CPU platform and GPU.

  6. In vCPUs to core ratio, choose the number of threads per core.

  7. Finish configuring other settings for the VM and click Create.

gcloud

To change the number of threads per core during VM creation, use the following gcloud compute instances create command:

gcloud 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. Set to either 2 or 1.

API

To change the number of threads per core during VM creation, use the instances.insert method with the threadsPerCore field.

POST https://compute.googleapis.com/compute/v1/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. Set to either 2 or 1.

Change the number of threads per core on a VM

To change the number of threads per core on a VM, use the console, the gcloud CLI, or the Compute Engine API.

Permissions required for this task

To perform this task, you must have the following permissions:

  • compute.instances.get on the project
  • compute.instances.update on the project

Console

To change the number of threads per core on an existing VM, use the following console procedure:

  1. In the console, go to the VM instances page.

    Go to VM instances

  2. Click the Name of the VM.

  3. If the VM's Status is Running, click Stop.

  4. Click Edit.

  5. Click CPU platform and GPU.

  6. In vCPUs to core ratio, choose the number of threads per core.

  7. Click Save.

gcloud

To change the number of threads per core on an existing VM, do the following:

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

    gcloud 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 compute instances update-from-file command:

    gcloud 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

To change the number of threads per core on an existing VM, use the following instances.update method:

PUT https://compute.googleapis.com/compute/v1/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

View the number of threads per core

To view the number of threads per core, use the procedure that corresponds to the OS that is running on the VM.

Linux

To view the number of threads per core on Linux-based VMs, use the following procedure:

  1. Connect to the Linux VM.

  2. Run the lscpu command.

    lscpu
    
  3. In the output, view 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

To view the number of threads per core on Windows-based VMs, use 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 threads by running the following command:

    CPU Get NumberOfCores,NumberofLogicalProcessors
    
  4. In the output, view 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, and is represented by NumberOfLogicalProcessors.

    NumberOfCores  NumberOfLogicalProcessors
    1              1
    

What's next