首页 > 解决方案 > 为什么在 Ubuntu VirtualBox 上只报告一个内核(使用 std::async 时发现)?

问题描述

我在 VirtualBox 上的 Ubuntu 上玩 std::async() 。出于某种原因,两个任务的 std::asyc() 我仍然没有得到超过 100% 的 CPU 使用率。我已经提到了用于启动异步任务的 std::launch:async 标志。我期待超过 100% 的使用率,因为我有六核 AMD ryzen 5。当我在 ubuntu 上打印 cpu 信息时,我得到了关注

#include<iostream>
#include<random>
#include<set>
#include<algorithm>
#include<future>

std::set<int> unique_random(uint32_t numElem)
{
   std::set<int> retVal;
   std::random_device rd;
   std::mt19937 gen(rd());
   std::uniform_int_distribution<> dist(0, numElem-1);

   std::generate_n(std::inserter(retVal, retVal.end()), numElem, [&](){ return dist(gen);});

   return retVal;
}


int main()
{
    std::cout << "uniue random numbers in first run : " << std::async(std::launch::async, unique_random, 1000000).get().size() << '\n' <<"uniue random numbers in Second run : "<< std::async(std::launch::async, unique_random, 1000000).get().size() <<'\n';;

    return 0;
}

编译:g++ async_test.cpp -pthread -o3

时间度量:/usr/bin/time ./a.out

#cat /proc/cpuinfo     
                                                                                                                         2552ms  Fri 29 May 2020 02:15:08 AM UTC
processor   : 0
vendor_id   : AuthenticAMD
cpu family  : 23
model       : 8
model name  : AMD Ryzen 5 2600X Six-Core Processor
stepping    : 2
microcode   : 0x6000626
cpu MHz     : 3593.248
cache size  : 512 KB
physical id : 0
siblings    : 1
core id     : 0
***cpu cores    : 1***
apicid      : 0
initial apicid  : 0
fpu     : yes
fpu_exception   : yes
cpuid level : 13
wp      : yes
flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid tsc_known_freq pni pclmulqdq monitor ssse3 cx16 sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx rdrand hypervisor lahf_lm cr8_legacy abm sse4a misalignsse 3dnowprefetch ssbd vmmcall fsgsbase avx2 rdseed clflushopt arat
bugs        : fxsave_leak sysret_ss_attrs null_seg spectre_v1 spectre_v2 spec_store_bypass
bogomips    : 7186.49
TLB size    : 2560 4K pages
clflush size    : 64
cache_alignment : 64
address sizes   : 48 bits physical, 48 bits virtual
power management:

在这里,如果您看到它只报告 1 个核心。所以我知道为什么我的 CPU 使用率没有超过 100%,但为什么 Ubuntu 只显示 1 个核心?

是 VirtualBox 的一些错误还是我在这里遗漏了什么?

谢谢

标签: c++ubuntuvirtual-machinevirtualbox

解决方案


推荐阅读