优化虚拟机(kvm)性能

This commit is contained in:
gfdgd xi 2023-08-09 22:19:56 +08:00
parent acff44eb2d
commit afefb854b6
8 changed files with 69 additions and 10 deletions

@ -22,6 +22,10 @@ if [[ $? == 0 ]] && [[ -f "$HOME/Qemu/Windows/Windows.qcow2" ]]; then
python3 ./VM/StartQemu.py python3 ./VM/StartQemu.py
exit exit
fi fi
# 查看CPU个数
CpuSocketNum=`cat /proc/cpuinfo | grep "cpu cores" | uniq | wc -l`
# 查看CPU核心数
CpuCoreNum=`grep 'core id' /proc/cpuinfo | sort -u | wc -l`
# 查看逻辑CPU的个数 # 查看逻辑CPU的个数
CpuCount=`cat /proc/cpuinfo| grep "processor"| wc -l` CpuCount=`cat /proc/cpuinfo| grep "processor"| wc -l`
@ -30,11 +34,11 @@ if [[ $? == 0 ]] && [[ -f "$HOME/Qemu/Windows/Windows.qcow2" ]]; then
use=$(echo "scale=4; $MemTotal / 3" | bc) use=$(echo "scale=4; $MemTotal / 3" | bc)
if [[ `arch` == "x86_64" ]]; then if [[ `arch` == "x86_64" ]]; then
echo X86 架构,使用 kvm 加速 echo X86 架构,使用 kvm 加速
kvm --hda "$HOME/Qemu/Windows/Windows.qcow2" -soundhw all -smp $CpuCount -m ${use}G -net user,hostfwd=tcp::3389-:3389 -display vnc=:5 -display gtk -usb -nic model=rtl8139 kvm -cpu host --hda "$HOME/Qemu/Windows/Windows.qcow2" -soundhw all -smp $CpuCount,sockets=$CpuSocketNum,cores=$(($CpuCoreNum / $CpuCount)),threads=$(($CpuSocketNum / $CpuCoreNum / $CpuCount)) -m ${use}G -net user,hostfwd=tcp::3389-:3389 -display vnc=:5 -display gtk -usb -nic model=rtl8139
exit exit
fi fi
echo 非 X86 架构,不使用 kvm 加速 echo 非 X86 架构,不使用 kvm 加速
qemu-system-x86_64 --hda "$HOME/Qemu/Windows/Windows.qcow2" -soundhw all -smp $CpuCount -m ${use}G -net user,hostfwd=tcp::3389-:3389 -display vnc=:5 -display gtk -usb -nic model=rtl8139 qemu-system-x86_64 --hda "$HOME/Qemu/Windows/Windows.qcow2" -soundhw all -smp $CpuCount,sockets=$CpuSocketNum,cores=$(($CpuCoreNum / $CpuCount)),threads=$(($CpuSocketNum / $CpuCoreNum / $CpuCount)) -m ${use}G -net user,hostfwd=tcp::3389-:3389 -display vnc=:5 -display gtk -usb -nic model=rtl8139
exit exit
fi fi
zenity --question --no-wrap --text="检查到您未创建所指定的虚拟机,是否创建虚拟机并继续?\n如果不创建将无法使用" zenity --question --no-wrap --text="检查到您未创建所指定的虚拟机,是否创建虚拟机并继续?\n如果不创建将无法使用"

@ -12,6 +12,7 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <infoutils.h> #include <infoutils.h>
#include "qemu.h" #include "qemu.h"
#include <QProcess>
// 懒得用 QThread 了(要继承) // 懒得用 QThread 了(要继承)
#include <thread> #include <thread>
using namespace std; using namespace std;
@ -27,6 +28,37 @@ void buildvbox::CleanScreen(){
system("cls"); system("cls");
} }
// 获取 CPU 个数
int buildvbox::GetCPUSocket(){
// 获取命令返回值
QProcess process;
process.start("bash", QStringList() << "-c" << "cat /proc/cpuinfo | grep \"cpu cores\" | uniq | wc -l");
process.waitForStarted();
process.waitForFinished();
int value = process.readAllStandardOutput().toInt();
process.close();
// 判断异常值,例如没挂载 /proc
if(value <= 0){
value = 1;
}
return value;
}
// 获取 CPU 核心数
int buildvbox::GetCPUCore(){
QProcess process;
process.start("bash", QStringList() << "-c" << "grep 'core id' /proc/cpuinfo | sort -u | wc -l");
process.waitForStarted();
process.waitForFinished();
int value = process.readAllStandardOutput().toInt();
process.close();
// 判断异常值,例如没挂载 /proc
if(value <= 0){
value = 1;
}
return value;
}
QString buildvbox::GetNet(){ QString buildvbox::GetNet(){
QList<QNetworkInterface> netList = QNetworkInterface::allInterfaces(); QList<QNetworkInterface> netList = QNetworkInterface::allInterfaces();
foreach(QNetworkInterface net, netList){ foreach(QNetworkInterface net, netList){
@ -114,7 +146,7 @@ buildvbox::buildvbox(QString isoPath, int id, int vm){
vm.MountISO("/usr/share/virtualbox/VBoxGuestAdditions.iso", "storage_controller_1", 1, 1); vm.MountISO("/usr/share/virtualbox/VBoxGuestAdditions.iso", "storage_controller_1", 1, 1);
}*/ }*/
vm.SetCPU(get_nprocs()); vm.SetCPU(get_nprocs(), GetCPUSocket(), GetCPUCore());
long memory = 0; long memory = 0;
long memoryAll = 0; long memoryAll = 0;
long swap = 0; long swap = 0;
@ -174,7 +206,7 @@ buildvbox::buildvbox(QString isoPath, int id, int vm){
vm.MountISO("/usr/share/virtualbox/VBoxGuestAdditions.iso", "storage_controller_1", 1, 1); vm.MountISO("/usr/share/virtualbox/VBoxGuestAdditions.iso", "storage_controller_1", 1, 1);
} }
vm.SetCPU(get_nprocs_conf()); vm.SetCPU(get_nprocs(), GetCPUSocket(), GetCPUCore());
long memory = 0; long memory = 0;
long memoryAll = 0; long memoryAll = 0;
long swap = 0; long swap = 0;

@ -14,6 +14,8 @@ public:
void CleanScreen(); void CleanScreen();
QString GetNet(); QString GetNet();
int Download(QString url, QString path, QString fileName); int Download(QString url, QString path, QString fileName);
int GetCPUSocket();
int GetCPUCore();
}; };
#endif // BUILDVBOX_H #endif // BUILDVBOX_H

@ -53,8 +53,14 @@ int qemu::BootFirst(QString bootDrive){
int qemu::SetNetBridge(QString netDriver){ int qemu::SetNetBridge(QString netDriver){
return 0; return 0;
} }
int qemu::SetCPU(int number){ int qemu::SetCPU(int number, int cpuNum, int coreNum){
commandOption += "-smp " + QString::number(number) + " "; // commandOption += "-smp " + QString::number(number) + " ";
// 调整调用方法
//qDebug() << number << " " << cpuNum << " " << coreNum;
qDebug() << "Socket: " << cpuNum;
qDebug() << "Core: " << coreNum;
qDebug() << "Threads: " << number;
commandOption += "-smp " + QString::number(number) + ",sockets=" + QString::number(cpuNum) + ",cores=" + QString::number(coreNum / cpuNum) + ",threads=" + QString::number(number / cpuNum / coreNum) + " ";
return 0; return 0;
} }
int qemu::SetMemory(int memory){ int qemu::SetMemory(int memory){
@ -70,7 +76,7 @@ int qemu::SetRemoteConnectSetting(int port){
int qemu::Start(bool unShown){ int qemu::Start(bool unShown){
qDebug() << commandOption; qDebug() << commandOption;
if(Command().GetCommand("arch").replace("\n", "").replace(" ", "") == "x86_64"){ if(Command().GetCommand("arch").replace("\n", "").replace(" ", "") == "x86_64"){
return system(("kvm " + commandOption + " &").toLatin1()); return system(("kvm -cpu host " + commandOption + " &").toLatin1());
} }
return system(("qemu-system-x86_64 -nic model=rtl8139 " + commandOption + " &").toLatin1()); return system(("qemu-system-x86_64 -nic model=rtl8139 " + commandOption + " &").toLatin1());
} }
@ -113,3 +119,6 @@ int qemu::SetKeyboardPS2(){
int qemu::OpenUSB(){ int qemu::OpenUSB(){
return 0; return 0;
} }
int EnabledUEFI(bool status){
}

@ -21,7 +21,7 @@ public:
int MountISO(QString isoPath, QString controlName="storage_controller_1", int port=1, int device=0); int MountISO(QString isoPath, QString controlName="storage_controller_1", int port=1, int device=0);
int BootFirst(QString bootDrive); int BootFirst(QString bootDrive);
int SetNetBridge(QString netDriver); int SetNetBridge(QString netDriver);
int SetCPU(int number); int SetCPU(int number, int cpuNum, int coreNum);
int SetMemory(int memory); int SetMemory(int memory);
int SetRemote(bool setting); int SetRemote(bool setting);
int SetRemoteConnectSetting(int port=5540); int SetRemoteConnectSetting(int port=5540);
@ -38,6 +38,7 @@ public:
int SetMousePS2(); int SetMousePS2();
int SetKeyboardPS2(); int SetKeyboardPS2();
int OpenUSB(); int OpenUSB();
int EnabledUEFI(bool status);
private: private:
QString commandOption; QString commandOption;

@ -44,7 +44,7 @@ int vbox::SetNetBridge(QString netDriver){
return system(("\"" + managerPath + "\" modifyvm \"" + name + return system(("\"" + managerPath + "\" modifyvm \"" + name +
"\" --nic1 bridged --cableconnected1 on --nictype1 82540EM --bridgeadapter1 \"" + netDriver + "\" --intnet1 brigh1 --macaddress1 auto").toLatin1()); "\" --nic1 bridged --cableconnected1 on --nictype1 82540EM --bridgeadapter1 \"" + netDriver + "\" --intnet1 brigh1 --macaddress1 auto").toLatin1());
} }
int vbox::SetCPU(int number){ int vbox::SetCPU(int number, int cpuNum, int coreNum){
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --cpus " + QString::number(number)).toLatin1()); return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --cpus " + QString::number(number)).toLatin1());
} }
int vbox::SetMemory(int memory){ int vbox::SetMemory(int memory){
@ -101,3 +101,10 @@ int vbox::SetKeyboardPS2(){
int vbox::OpenUSB(){ int vbox::OpenUSB(){
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --usbohci on").toLatin1()); return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --usbohci on").toLatin1());
} }
int vbox::EnabledUEFI(bool status){
if(status){
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --firmware=efi").toLatin1());
}
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --firmware=bios").toLatin1());
}

@ -21,7 +21,7 @@ public:
int MountISO(QString isoPath, QString controlName="storage_controller_1", int port=1, int device=0); int MountISO(QString isoPath, QString controlName="storage_controller_1", int port=1, int device=0);
int BootFirst(QString bootDrive); int BootFirst(QString bootDrive);
int SetNetBridge(QString netDriver); int SetNetBridge(QString netDriver);
int SetCPU(int number); int SetCPU(int number, int cpuNum, int coreNum);
int SetMemory(int memory); int SetMemory(int memory);
int SetRemote(bool setting); int SetRemote(bool setting);
int SetRemoteConnectSetting(int port=5540); int SetRemoteConnectSetting(int port=5540);
@ -38,6 +38,7 @@ public:
int SetMousePS2(); int SetMousePS2();
int SetKeyboardPS2(); int SetKeyboardPS2();
int OpenUSB(); int OpenUSB();
int EnabledUEFI(bool status);
private: private:

@ -0,0 +1,3 @@
[main acff44e] 修复选择vbox-
2 files changed, 1198 insertions(+), 2 deletions(-)
create mode 100644 VM-source/.qtc_clangd/compile_commands.json