-
Notifications
You must be signed in to change notification settings - Fork 90
/
info.go
234 lines (202 loc) · 5.19 KB
/
info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// SPDX-FileCopyrightText: 2018 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
package systeminfo
import (
"sync"
"time"
"github.com/godbus/dbus/v5"
"github.com/linuxdeepin/dde-daemon/loader"
systeminfo "github.com/linuxdeepin/go-dbus-factory/system/org.deepin.dde.systeminfo1"
"github.com/linuxdeepin/go-lib/dbusutil"
"github.com/linuxdeepin/go-lib/log"
)
//go:generate dbusutil-gen em -type SystemInfo
const (
dbusServiceName = "org.deepin.dde.SystemInfo1"
dbusPath = "/org/deepin/dde/SystemInfo1"
dbusInterface = dbusServiceName
)
type SystemInfo struct {
// Current deepin version, ex: "2015 Desktop"
Version string
// Distribution ID
DistroID string
// Distribution Description
DistroDesc string
// Distribution Version
DistroVer string
// CPU information
Processor string
// Disk capacity
DiskCap uint64
// Memory size
MemoryCap uint64
// System type: 32bit or 64bit
SystemType int64
// CPU max MHz
CPUMaxMHz float64
//Current Speed : when cpu max mhz is 0 use
CurrentSpeed uint64
}
type Daemon struct {
info *SystemInfo
PropsMu sync.RWMutex
systeminfo systeminfo.SystemInfo
sigSystemLoop *dbusutil.SignalLoop
*loader.ModuleBase
}
var logger = log.NewLogger("daemon/systeminfo")
func NewDaemon(logger *log.Logger) *Daemon {
daemon := new(Daemon)
daemon.ModuleBase = loader.NewModuleBase("systeminfo", daemon, logger)
return daemon
}
func (d *Daemon) GetDependencies() []string {
return []string{}
}
func (d *Daemon) Start() error {
if d.info != nil {
return nil
}
service := loader.GetService()
d.info = NewSystemInfo()
d.initSysSystemInfo()
err := service.Export(dbusPath, d.info)
if err != nil {
d.info = nil
logger.Error(err)
return err
}
err = service.RequestName(dbusServiceName)
if err != nil {
d.info = nil
logger.Error(err)
return err
}
return nil
}
func (d *Daemon) Stop() error {
if d.info == nil {
return nil
}
service := loader.GetService()
_ = service.StopExport(d.info)
d.info = nil
return nil
}
func (d *Daemon) initSysSystemInfo() {
sysBus, err := dbus.SystemBus()
if err != nil {
return
}
d.systeminfo = systeminfo.NewSystemInfo(sysBus)
d.sigSystemLoop = dbusutil.NewSignalLoop(sysBus, 10)
d.sigSystemLoop.Start()
d.systeminfo.InitSignalExt(d.sigSystemLoop, true)
// 通过 demicode 获取 "CPU 频率", 接收 org.deepin.dde.SystemInfo1 的属性 CurrentSpeed 改变信号
err = d.systeminfo.CurrentSpeed().ConnectChanged(func(hasValue bool, value uint64) {
logger.Infof("demicode hasValue : %t, CurrentSpeed : %d", hasValue, value)
if !hasValue {
return
}
d.PropsMu.Lock()
d.info.CurrentSpeed = value
//假如此时 cpu max mhz 还是0, 且 value 不是0, 则给 d.infoCPUMaxMHz 再赋值
if isFloatEqual(d.info.CPUMaxMHz, 0.0) && value != 0 {
d.info.CPUMaxMHz = float64(value)
}
d.PropsMu.Unlock()
})
if err != nil {
logger.Warning("systeminfo.CurrentSpeed().ConnectChanged err : ", err)
}
d.PropsMu.Lock()
d.info.CurrentSpeed, err = d.systeminfo.CurrentSpeed().Get(0)
if err != nil {
logger.Warning("get systeminfo.CurrentSpeed err : ", err)
d.PropsMu.Unlock()
return
}
d.info.CPUMaxMHz = float64(d.info.CurrentSpeed)
d.PropsMu.Unlock()
logger.Info("d.info.CurrentSpeed : ", d.info.CurrentSpeed, " , d.info.CPUMaxMHz : ", d.info.CPUMaxMHz)
}
func NewSystemInfo() *SystemInfo {
var info SystemInfo
tmp, _ := doReadCache(cacheFile)
if tmp != nil && tmp.isValidity() {
info = *tmp
time.AfterFunc(time.Second*10, func() {
info.init()
_ = doSaveCache(&info, cacheFile)
})
return &info
}
info.init()
go func() {
_ = doSaveCache(&info, cacheFile)
}()
return &info
}
func (info *SystemInfo) init() {
var err error
info.Processor, err = GetCPUInfo("/proc/cpuinfo")
if err != nil {
logger.Warning("Get cpu info failed:", err)
}
info.Version, err = getVersion()
if err != nil {
logger.Warning("Get version failed:", err)
}
info.DistroID, info.DistroDesc, info.DistroVer, err = getDistro()
if err != nil {
logger.Warning("Get distribution failed:", err)
}
info.MemoryCap, err = getMemoryFromFile("/proc/meminfo")
if err != nil {
logger.Warning("Get memory capacity failed:", err)
}
if systemBit() == "64" {
info.SystemType = 64
} else {
info.SystemType = 32
}
info.DiskCap, err = getDiskCap()
if err != nil {
logger.Warning("Get disk capacity failed:", err)
}
lscpuRes, err := runLscpu()
if err != nil {
logger.Warning("run lscpu failed:", err)
return
} else {
info.CPUMaxMHz, err = getCPUMaxMHzByLscpu(lscpuRes)
if err != nil {
logger.Warning(err)
} else {
if isFloatEqual(info.CPUMaxMHz, 0.0) {
//关联信号,接收system的信号 : line139
//此时若info.CurrentSpeed不为0, 则可以直接使用备用的currentspeed赋值
if info.CurrentSpeed != 0 {
info.CPUMaxMHz = float64(info.CurrentSpeed)
}
}
}
if info.Processor == "" {
info.Processor, err = getProcessorByLscpu(lscpuRes)
if err != nil {
logger.Warning(err)
}
}
}
}
func (info *SystemInfo) isValidity() bool {
if info.Processor == "" || info.DiskCap == 0 || info.MemoryCap == 0 {
return false
}
return true
}
func (*SystemInfo) GetInterfaceName() string {
return dbusInterface
}