Skip to content

Commit ee4e916

Browse files
GongHeng2017deepin-bot[bot]
authored andcommitted
Fix: [gpu-info] The gpu mem not show.
-- Adjust code logic to show gpu mem. Log: fix issue Bug: https://pms.uniontech.com/bug-view-336355.html
1 parent f720214 commit ee4e916

7 files changed

Lines changed: 85 additions & 68 deletions

File tree

deepin-devicemanager-server/deepin-deviceinfo/src/loadinfo/deviceinterface.cpp

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@
1010
#include <QDBusMessage>
1111
#include <QProcess>
1212
#include <QDebug>
13+
#include <QFile>
14+
#include <QRegularExpression>
15+
1316
#include <polkit-qt5-1/PolkitQt1/Authority>
1417

18+
constexpr char kGraphicsMemory[] { "Graphics Memory" };
19+
1520
using namespace PolkitQt1;
1621
bool DeviceInterface::getUserAuthorPasswd()
1722
{
@@ -24,6 +29,52 @@ bool DeviceInterface::getUserAuthorPasswd()
2429
return result == Authority::Yes;
2530
}
2631

32+
bool DeviceInterface::getGpuMemInfoForFTDTM(QMap<QString, QString> &mapInfo)
33+
{
34+
const QString filePath = "/sys/kernel/debug/gc/total_mem";
35+
QString totalValue;
36+
37+
QFile file(filePath);
38+
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
39+
qCritical() << "Error opening /sys/kernel/debug/gc/total_mem:" << file.errorString();
40+
return false;
41+
}
42+
43+
QString content = QString::fromUtf8(file.readAll());
44+
file.close();
45+
46+
if (content.isEmpty()) {
47+
qCritical() << "Error: /sys/kernel/debug/gc/total_mem File is empty!";
48+
return false;
49+
}
50+
51+
QRegularExpression regex(R"((\d+(?:\.\d+)?)\s*\(?(MB|GB|KB|B)\)?)",
52+
QRegularExpression::CaseInsensitiveOption);
53+
QRegularExpressionMatch memInfoMatch = regex.match(content);
54+
55+
if (!memInfoMatch.hasMatch()) {
56+
qCritical() << "Error: Failed to find memory info";
57+
return false;
58+
}
59+
60+
double value = memInfoMatch.captured(1).toDouble();
61+
QString unit = memInfoMatch.captured(2).toUpper();
62+
63+
if (unit == "MB") {
64+
totalValue = QString("%1GB").arg(value / 1024.0, 0, 'f', 2);
65+
} else if (unit == "GB") {
66+
totalValue = QString("%1GB").arg(value, 0, 'f', 2);
67+
} else if (unit == "KB") {
68+
totalValue = QString("%1GB").arg(value / (1024.0 * 1024.0), 0, 'f', 2);
69+
} else if (unit == "B") {
70+
totalValue = QString("%1GB").arg(value / (1024.0 * 1024.0 * 1024.0), 0, 'f', 2);
71+
}
72+
73+
mapInfo.insert(kGraphicsMemory, totalValue);
74+
75+
return true;
76+
}
77+
2778
DeviceInterface::DeviceInterface(const char *name, QObject *parent)
2879
: QObject(parent)
2980
{
@@ -59,24 +110,17 @@ void DeviceInterface::setMonitorDeviceFlag(bool flag)
59110
}
60111
}
61112

62-
QString DeviceInterface::getGpuInfoByCustom(const QString &cmd, const QStringList &arguments)
113+
QString DeviceInterface::getGpuInfoForFTDTM()
63114
{
64-
QString gpuinfo;
65-
QProcess process;
66-
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
67-
if (arguments.size() > 1) {
68-
env.insert("DISPLAY", arguments[0]);
69-
env.insert("XAUTHORITY", arguments[1]);
70-
}
71-
process.setProcessEnvironment(env);
72-
process.start(cmd, arguments);
73-
if (!process.waitForFinished()) {
74-
qCritical() << QString("Error executing %1 :").arg(cmd) << process.errorString();
75-
return gpuinfo;
115+
static QString gpuMemInfo { "" };
116+
if (gpuMemInfo.isEmpty()) {
117+
QMap<QString, QString> mapInfo;
118+
if (getGpuMemInfoForFTDTM(mapInfo)) {
119+
for (auto it = mapInfo.begin(); it != mapInfo.end(); ++it) {
120+
QString tmpInfo = it.key() + ": " + it.value() + "\n";
121+
gpuMemInfo.append(tmpInfo);
122+
}
123+
}
76124
}
77-
78-
if (process.exitCode() == 0)
79-
gpuinfo = QString::fromLocal8Bit(process.readAllStandardOutput());
80-
81-
return gpuinfo;
125+
return gpuMemInfo;
82126
}

deepin-devicemanager-server/deepin-deviceinfo/src/loadinfo/deviceinterface.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ public slots:
3939
*/
4040
Q_SCRIPTABLE void setMonitorDeviceFlag(bool flag);
4141

42-
Q_SCRIPTABLE QString getGpuInfoByCustom(const QString &cmd, const QStringList &arguments);
42+
Q_SCRIPTABLE QString getGpuInfoForFTDTM();
4343

4444
private:
4545
bool getUserAuthorPasswd();
46+
bool getGpuMemInfoForFTDTM(QMap<QString, QString> &mapInfo);
4647
};
4748

4849
#endif // DEVICEINTERFACE_H

deepin-devicemanager/src/GenerateDevice/CustomGenerator.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,9 @@ CustomGenerator::CustomGenerator(QObject *parent)
2020

2121
void CustomGenerator::generatorGpuDevice()
2222
{
23-
QString cmd = CommonTools::getGpuInfoCommandFromDConfig();
24-
if (cmd.isEmpty()) {
25-
DeviceGenerator::generatorGpuDevice();
26-
return;
27-
}
28-
2923
QString tmpGpuInfo = CommonTools::preGenerateGpuInfo();
3024
if (tmpGpuInfo.isEmpty()) {
31-
qCritical() << "Failed to get gpu info by commad " << cmd;
25+
qCritical() << "Error: Failed to get gpu info!";
3226
return;
3327
}
3428

deepin-devicemanager/src/GenerateDevice/DBusInterface.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,6 @@ void DBusInterface::refreshInfo()
4646
mp_Iface->asyncCall("refreshInfo");
4747
}
4848

49-
bool DBusInterface::getGpuInfoByCustom(const QString &cmd, const QStringList &arguments, QString &gpuInfo)
50-
{
51-
QDBusReply<QString> replyList = mp_Iface->call("getGpuInfoByCustom", cmd, arguments);
52-
if (replyList.isValid()) {
53-
gpuInfo = replyList.value();
54-
return true;
55-
} else {
56-
qCritical() << "Error: failed to call dbus to get gpu memery info! ";
57-
return false;
58-
}
59-
}
60-
6149
void DBusInterface::init()
6250
{
6351
// 1. 连接到dbus

deepin-devicemanager/src/GenerateDevice/DBusInterface.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ class DBusInterface
4747
*/
4848
void refreshInfo();
4949

50-
bool getGpuInfoByCustom(const QString &cmd, const QStringList &arguments, QString &gpuInfo);
51-
5250
protected:
5351
DBusInterface();
5452

deepin-devicemanager/src/Tool/commontools.cpp

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -268,42 +268,40 @@ QString CommonTools::getGpuInfoCommandFromDConfig()
268268

269269
QString CommonTools::preGenerateGpuInfo()
270270
{
271-
static QString gpuInfo { "" };
272-
273-
if (gpuInfo.isEmpty()) {
274-
QStringList arguments;
275-
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
276-
QString display = env.value("DISPLAY");
277-
QString xauthority = env.value("XAUTHORITY");
278-
if (display.isEmpty() || xauthority.isEmpty()) {
279-
qCritical() << "DISPLAY or XAUTHORITY is not set!";
280-
} else {
281-
arguments << display << xauthority;
271+
static QString gpuBaseInfo { "" };
272+
static QString gpuMemInfo { "" };
273+
274+
if (gpuBaseInfo.isEmpty()) {
275+
QMap<QString, QString> mapInfo;
276+
if (getGpuBaseInfo(mapInfo)) {
277+
for (auto it = mapInfo.begin(); it != mapInfo.end(); ++it) {
278+
QString tmpInfo = it.key() + ": " + it.value() + "\n";
279+
gpuBaseInfo.append(tmpInfo);
280+
}
282281
}
282+
}
283283

284+
if (gpuBaseInfo.isEmpty()) {
285+
qCritical() << "Error: failed to get gpu base info!";
286+
return "";
287+
}
288+
289+
if (gpuMemInfo.isEmpty()) {
284290
QDBusInterface iface("org.deepin.DeviceInfo",
285291
"/org/deepin/DeviceInfo",
286292
"org.deepin.DeviceInfo",
287293
QDBusConnection::systemBus());
288294
if (iface.isValid()) {
289-
QDBusReply<QString> replyList = iface.call("getGpuInfoByCustom", arguments, gpuInfo);
295+
QDBusReply<QString> replyList = iface.call("getGpuInfoForFTDTM");
290296
if (replyList.isValid()) {
291-
gpuInfo = replyList.value();
297+
gpuMemInfo = replyList.value();
292298
} else {
293299
qCritical() << "Error: failed to call dbus to get gpu memery info! ";
294300
}
295301
}
296-
297-
QMap<QString, QString> mapInfo;
298-
if (getGpuBaseInfo(mapInfo)) {
299-
for (auto it = mapInfo.begin(); it != mapInfo.end(); ++it) {
300-
QString tmpInfo = it.key() + ": " + it.value() + "\n";
301-
gpuInfo.append(tmpInfo);
302-
}
303-
}
304302
}
305303

306-
return gpuInfo;
304+
return (gpuBaseInfo + gpuMemInfo);
307305
}
308306

309307
bool CommonTools::getGpuBaseInfo(QMap<QString, QString> &mapInfo)

deepin-devicemanager/src/main.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,6 @@ int main(int argc, char *argv[])
112112
if (dbus.registerService("com.deepin.DeviceManagerNotify")) {
113113
dbus.registerObject("/com/deepin/DeviceManagerNotify", &app, QDBusConnection::ExportScriptableSlots);
114114
app.parseCmdLine();
115-
116-
QString cmd = CommonTools::getGpuInfoCommandFromDConfig();
117-
if (!cmd.isEmpty()) {
118-
CommonTools::preGenerateGpuInfo();
119-
}
120-
121115
app.activateWindow();
122116
return app.exec();
123117
} else {

0 commit comments

Comments
 (0)