diff --git a/deepin-devicemanager-server/deepin-deviceinfo/src/loadinfo/deviceinterface.cpp b/deepin-devicemanager-server/deepin-deviceinfo/src/loadinfo/deviceinterface.cpp index 67708560..6d9717c0 100644 --- a/deepin-devicemanager-server/deepin-deviceinfo/src/loadinfo/deviceinterface.cpp +++ b/deepin-devicemanager-server/deepin-deviceinfo/src/loadinfo/deviceinterface.cpp @@ -10,8 +10,13 @@ #include #include #include +#include +#include + #include +constexpr char kGraphicsMemory[] { "Graphics Memory" }; + using namespace PolkitQt1; bool DeviceInterface::getUserAuthorPasswd() { @@ -24,6 +29,52 @@ bool DeviceInterface::getUserAuthorPasswd() return result == Authority::Yes; } +bool DeviceInterface::getGpuMemInfoForFTDTM(QMap &mapInfo) +{ + const QString filePath = "/sys/kernel/debug/gc/total_mem"; + QString totalValue; + + QFile file(filePath); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + qCritical() << "Error opening /sys/kernel/debug/gc/total_mem:" << file.errorString(); + return false; + } + + QString content = QString::fromUtf8(file.readAll()); + file.close(); + + if (content.isEmpty()) { + qCritical() << "Error: /sys/kernel/debug/gc/total_mem File is empty!"; + return false; + } + + QRegularExpression regex(R"((\d+(?:\.\d+)?)\s*\(?(MB|GB|KB|B)\)?)", + QRegularExpression::CaseInsensitiveOption); + QRegularExpressionMatch memInfoMatch = regex.match(content); + + if (!memInfoMatch.hasMatch()) { + qCritical() << "Error: Failed to find memory info"; + return false; + } + + double value = memInfoMatch.captured(1).toDouble(); + QString unit = memInfoMatch.captured(2).toUpper(); + + if (unit == "MB") { + totalValue = QString("%1GB").arg(value / 1024.0, 0, 'f', 2); + } else if (unit == "GB") { + totalValue = QString("%1GB").arg(value, 0, 'f', 2); + } else if (unit == "KB") { + totalValue = QString("%1GB").arg(value / (1024.0 * 1024.0), 0, 'f', 2); + } else if (unit == "B") { + totalValue = QString("%1GB").arg(value / (1024.0 * 1024.0 * 1024.0), 0, 'f', 2); + } + + mapInfo.insert(kGraphicsMemory, totalValue); + + return true; +} + DeviceInterface::DeviceInterface(const char *name, QObject *parent) : QObject(parent) { @@ -59,24 +110,17 @@ void DeviceInterface::setMonitorDeviceFlag(bool flag) } } -QString DeviceInterface::getGpuInfoByCustom(const QString &cmd, const QStringList &arguments) +QString DeviceInterface::getGpuInfoForFTDTM() { - QString gpuinfo; - QProcess process; - QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); - if (arguments.size() > 1) { - env.insert("DISPLAY", arguments[0]); - env.insert("XAUTHORITY", arguments[1]); - } - process.setProcessEnvironment(env); - process.start(cmd, arguments); - if (!process.waitForFinished()) { - qCritical() << QString("Error executing %1 :").arg(cmd) << process.errorString(); - return gpuinfo; + static QString gpuMemInfo { "" }; + if (gpuMemInfo.isEmpty()) { + QMap mapInfo; + if (getGpuMemInfoForFTDTM(mapInfo)) { + for (auto it = mapInfo.begin(); it != mapInfo.end(); ++it) { + QString tmpInfo = it.key() + ": " + it.value() + "\n"; + gpuMemInfo.append(tmpInfo); + } + } } - - if (process.exitCode() == 0) - gpuinfo = QString::fromLocal8Bit(process.readAllStandardOutput()); - - return gpuinfo; + return gpuMemInfo; } diff --git a/deepin-devicemanager-server/deepin-deviceinfo/src/loadinfo/deviceinterface.h b/deepin-devicemanager-server/deepin-deviceinfo/src/loadinfo/deviceinterface.h index 01c65610..e3407535 100644 --- a/deepin-devicemanager-server/deepin-deviceinfo/src/loadinfo/deviceinterface.h +++ b/deepin-devicemanager-server/deepin-deviceinfo/src/loadinfo/deviceinterface.h @@ -39,10 +39,11 @@ public slots: */ Q_SCRIPTABLE void setMonitorDeviceFlag(bool flag); - Q_SCRIPTABLE QString getGpuInfoByCustom(const QString &cmd, const QStringList &arguments); + Q_SCRIPTABLE QString getGpuInfoForFTDTM(); private: bool getUserAuthorPasswd(); + bool getGpuMemInfoForFTDTM(QMap &mapInfo); }; #endif // DEVICEINTERFACE_H diff --git a/deepin-devicemanager/src/GenerateDevice/CustomGenerator.cpp b/deepin-devicemanager/src/GenerateDevice/CustomGenerator.cpp index d3ab7ade..a4b72130 100644 --- a/deepin-devicemanager/src/GenerateDevice/CustomGenerator.cpp +++ b/deepin-devicemanager/src/GenerateDevice/CustomGenerator.cpp @@ -20,15 +20,9 @@ CustomGenerator::CustomGenerator(QObject *parent) void CustomGenerator::generatorGpuDevice() { - QString cmd = CommonTools::getGpuInfoCommandFromDConfig(); - if (cmd.isEmpty()) { - DeviceGenerator::generatorGpuDevice(); - return; - } - QString tmpGpuInfo = CommonTools::preGenerateGpuInfo(); if (tmpGpuInfo.isEmpty()) { - qCritical() << "Failed to get gpu info by commad " << cmd; + qCritical() << "Error: Failed to get gpu info!"; return; } diff --git a/deepin-devicemanager/src/GenerateDevice/DBusInterface.cpp b/deepin-devicemanager/src/GenerateDevice/DBusInterface.cpp index f08eb45f..59ba79bc 100644 --- a/deepin-devicemanager/src/GenerateDevice/DBusInterface.cpp +++ b/deepin-devicemanager/src/GenerateDevice/DBusInterface.cpp @@ -46,18 +46,6 @@ void DBusInterface::refreshInfo() mp_Iface->asyncCall("refreshInfo"); } -bool DBusInterface::getGpuInfoByCustom(const QString &cmd, const QStringList &arguments, QString &gpuInfo) -{ - QDBusReply replyList = mp_Iface->call("getGpuInfoByCustom", cmd, arguments); - if (replyList.isValid()) { - gpuInfo = replyList.value(); - return true; - } else { - qCritical() << "Error: failed to call dbus to get gpu memery info! "; - return false; - } -} - void DBusInterface::init() { // 1. 连接到dbus diff --git a/deepin-devicemanager/src/GenerateDevice/DBusInterface.h b/deepin-devicemanager/src/GenerateDevice/DBusInterface.h index c6a01898..a154a337 100644 --- a/deepin-devicemanager/src/GenerateDevice/DBusInterface.h +++ b/deepin-devicemanager/src/GenerateDevice/DBusInterface.h @@ -47,8 +47,6 @@ class DBusInterface */ void refreshInfo(); - bool getGpuInfoByCustom(const QString &cmd, const QStringList &arguments, QString &gpuInfo); - protected: DBusInterface(); diff --git a/deepin-devicemanager/src/Tool/commontools.cpp b/deepin-devicemanager/src/Tool/commontools.cpp index a8046d54..d3d21fba 100644 --- a/deepin-devicemanager/src/Tool/commontools.cpp +++ b/deepin-devicemanager/src/Tool/commontools.cpp @@ -268,42 +268,40 @@ QString CommonTools::getGpuInfoCommandFromDConfig() QString CommonTools::preGenerateGpuInfo() { - static QString gpuInfo { "" }; - - if (gpuInfo.isEmpty()) { - QStringList arguments; - QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); - QString display = env.value("DISPLAY"); - QString xauthority = env.value("XAUTHORITY"); - if (display.isEmpty() || xauthority.isEmpty()) { - qCritical() << "DISPLAY or XAUTHORITY is not set!"; - } else { - arguments << display << xauthority; + static QString gpuBaseInfo { "" }; + static QString gpuMemInfo { "" }; + + if (gpuBaseInfo.isEmpty()) { + QMap mapInfo; + if (getGpuBaseInfo(mapInfo)) { + for (auto it = mapInfo.begin(); it != mapInfo.end(); ++it) { + QString tmpInfo = it.key() + ": " + it.value() + "\n"; + gpuBaseInfo.append(tmpInfo); + } } + } + if (gpuBaseInfo.isEmpty()) { + qCritical() << "Error: failed to get gpu base info!"; + return ""; + } + + if (gpuMemInfo.isEmpty()) { QDBusInterface iface("org.deepin.DeviceInfo", "/org/deepin/DeviceInfo", "org.deepin.DeviceInfo", QDBusConnection::systemBus()); if (iface.isValid()) { - QDBusReply replyList = iface.call("getGpuInfoByCustom", arguments, gpuInfo); + QDBusReply replyList = iface.call("getGpuInfoForFTDTM"); if (replyList.isValid()) { - gpuInfo = replyList.value(); + gpuMemInfo = replyList.value(); } else { qCritical() << "Error: failed to call dbus to get gpu memery info! "; } } - - QMap mapInfo; - if (getGpuBaseInfo(mapInfo)) { - for (auto it = mapInfo.begin(); it != mapInfo.end(); ++it) { - QString tmpInfo = it.key() + ": " + it.value() + "\n"; - gpuInfo.append(tmpInfo); - } - } } - return gpuInfo; + return (gpuBaseInfo + gpuMemInfo); } bool CommonTools::getGpuBaseInfo(QMap &mapInfo) diff --git a/deepin-devicemanager/src/main.cpp b/deepin-devicemanager/src/main.cpp index 62f41ab4..b7ca09ff 100644 --- a/deepin-devicemanager/src/main.cpp +++ b/deepin-devicemanager/src/main.cpp @@ -112,12 +112,6 @@ int main(int argc, char *argv[]) if (dbus.registerService("com.deepin.DeviceManagerNotify")) { dbus.registerObject("/com/deepin/DeviceManagerNotify", &app, QDBusConnection::ExportScriptableSlots); app.parseCmdLine(); - - QString cmd = CommonTools::getGpuInfoCommandFromDConfig(); - if (!cmd.isEmpty()) { - CommonTools::preGenerateGpuInfo(); - } - app.activateWindow(); return app.exec(); } else {