优化虚拟机(kvm)性能

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

View File

@@ -12,6 +12,7 @@
#include <QCoreApplication>
#include <infoutils.h>
#include "qemu.h"
#include <QProcess>
// 懒得用 QThread 了(要继承)
#include <thread>
using namespace std;
@@ -27,6 +28,37 @@ void buildvbox::CleanScreen(){
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(){
QList<QNetworkInterface> netList = QNetworkInterface::allInterfaces();
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.SetCPU(get_nprocs());
vm.SetCPU(get_nprocs(), GetCPUSocket(), GetCPUCore());
long memory = 0;
long memoryAll = 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.SetCPU(get_nprocs_conf());
vm.SetCPU(get_nprocs(), GetCPUSocket(), GetCPUCore());
long memory = 0;
long memoryAll = 0;
long swap = 0;

View File

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

View File

@@ -53,8 +53,14 @@ int qemu::BootFirst(QString bootDrive){
int qemu::SetNetBridge(QString netDriver){
return 0;
}
int qemu::SetCPU(int number){
commandOption += "-smp " + QString::number(number) + " ";
int qemu::SetCPU(int number, int cpuNum, int coreNum){
// 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;
}
int qemu::SetMemory(int memory){
@@ -70,7 +76,7 @@ int qemu::SetRemoteConnectSetting(int port){
int qemu::Start(bool unShown){
qDebug() << commandOption;
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());
}
@@ -113,3 +119,6 @@ int qemu::SetKeyboardPS2(){
int qemu::OpenUSB(){
return 0;
}
int EnabledUEFI(bool status){
}

View File

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

View File

@@ -44,7 +44,7 @@ int vbox::SetNetBridge(QString netDriver){
return system(("\"" + managerPath + "\" modifyvm \"" + name +
"\" --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());
}
int vbox::SetMemory(int memory){
@@ -101,3 +101,10 @@ int vbox::SetKeyboardPS2(){
int vbox::OpenUSB(){
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());
}

View File

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