Search code examples
google-cloud-platformcloudgoogle-compute-engine

What is the difference between VCPU and CPU in google cloud platform


What is the difference between 1 VCPU and 1 CPU in google cloud. How to fetch the CPU count and VCPU count for a list of compute instances using GCP client libraries


Solution

  • In general, 1 vCPU is not equal to 1 CPU. A vCPU is a virtual CPU that is assigned to a VM. A CPU is a physical chip that is installed in a computer.

    In Google Cloud, a vCPU is typically equivalent to one physical CPU core. However, there are some machine types that use hyperthreading, which means that a single vCPU can be used to run two threads simultaneously.

    So, if you have a machine type that uses hyperthreading, then 1 vCPU will be equivalent to 2 physical CPU cores.

    To fetch the CPU count and vCPU count for a list of compute instances using GCP client libraries, you can use the following code:

    import googleapiclient.discovery
    compute = googleapiclient.discovery.build('compute', 'v1')
    instances = compute.instances().list(project='newProject').execute()
    
    for instance in instances['items']:
     cpu_count = instance['machineType']['numCpus']
     vcpu_count = instance['vcpus']
     print('Instance {} has {} CPUs and {} vCPUs'.format(instance['name'], cpu_count, vcpu_count))