Skip to content

Commit 73fc5bc

Browse files
author
Paweł Szulik
authored
Merge pull request #2847 from hustcat/cache_id
Add cache id for cache info.
2 parents ef7e64f + 7dc0126 commit 73fc5bc

File tree

5 files changed

+38
-5
lines changed

5 files changed

+38
-5
lines changed

info/v1/machine.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ type Core struct {
5454
}
5555

5656
type Cache struct {
57+
// Id of memory cache
58+
Id int `json:"id"`
5759
// Size of memory cache in bytes.
5860
Size uint64 `json:"size"`
5961
// Type of memory cache: data, instruction, or unified.

machine/topology_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ func TestTopologyWithoutNodes(t *testing.T) {
248248
sysFs := &fakesysfs.FakeSysFs{}
249249

250250
c := sysfs.CacheInfo{
251+
Id: 0,
251252
Size: 32 * 1024,
252253
Type: "unified",
253254
Level: 0,
@@ -297,7 +298,7 @@ func TestTopologyWithoutNodes(t *testing.T) {
297298
topologyJSON2, err := json.Marshal(topology[1])
298299
assert.Nil(t, err)
299300

300-
expectedTopology1 := `{"node_id":0,"memory":0,"hugepages":null,"cores":[{"core_id":0,"thread_ids":[0,2],"caches":[{"size":32768,"type":"unified","level":0}], "socket_id": 0}],"caches":null}`
301+
expectedTopology1 := `{"node_id":0,"memory":0,"hugepages":null,"cores":[{"core_id":0,"thread_ids":[0,2],"caches":[{"id":0, "size":32768,"type":"unified","level":0}], "socket_id": 0}],"caches":null}`
301302
expectedTopology2 := `
302303
{
303304
"node_id":1,
@@ -312,6 +313,7 @@ func TestTopologyWithoutNodes(t *testing.T) {
312313
],
313314
"caches":[
314315
{
316+
"id": 0,
315317
"size":32768,
316318
"type":"unified",
317319
"level":0

utils/sysfs/sysfs.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ var (
6464
)
6565

6666
type CacheInfo struct {
67+
// cache id
68+
Id int
6769
// size in bytes
6870
Size uint64
6971
// cache type - instruction, data, unified
@@ -292,14 +294,24 @@ func getCPUCount(cache string) (count int, err error) {
292294
return
293295
}
294296

295-
func (fs *realSysFs) GetCacheInfo(id int, name string) (CacheInfo, error) {
296-
cachePath := fmt.Sprintf("%s%d/cache/%s", cacheDir, id, name)
297-
out, err := ioutil.ReadFile(path.Join(cachePath, "/size"))
297+
func (fs *realSysFs) GetCacheInfo(cpu int, name string) (CacheInfo, error) {
298+
cachePath := fmt.Sprintf("%s%d/cache/%s", cacheDir, cpu, name)
299+
out, err := ioutil.ReadFile(path.Join(cachePath, "/id"))
300+
if err != nil {
301+
return CacheInfo{}, err
302+
}
303+
var id int
304+
n, err := fmt.Sscanf(string(out), "%d", &id)
305+
if err != nil || n != 1 {
306+
return CacheInfo{}, err
307+
}
308+
309+
out, err = ioutil.ReadFile(path.Join(cachePath, "/size"))
298310
if err != nil {
299311
return CacheInfo{}, err
300312
}
301313
var size uint64
302-
n, err := fmt.Sscanf(string(out), "%dK", &size)
314+
n, err = fmt.Sscanf(string(out), "%dK", &size)
303315
if err != nil || n != 1 {
304316
return CacheInfo{}, err
305317
}
@@ -325,6 +337,7 @@ func (fs *realSysFs) GetCacheInfo(id int, name string) (CacheInfo, error) {
325337
return CacheInfo{}, err
326338
}
327339
return CacheInfo{
340+
Id: id,
328341
Size: size,
329342
Level: level,
330343
Type: cacheType,

utils/sysinfo/sysinfo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ func addCacheInfo(sysFs sysfs.SysFs, node *info.Node) error {
332332

333333
for _, cache := range caches {
334334
c := info.Cache{
335+
Id: cache.Id,
335336
Size: cache.Size,
336337
Level: cache.Level,
337338
Type: cache.Type,

utils/sysinfo/sysinfo_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ func TestGetNodesInfo(t *testing.T) {
119119
}{
120120
{
121121
sysfs.CacheInfo{
122+
Id: 0,
122123
Size: 32 * 1024,
123124
Type: "unified",
124125
Level: 3,
@@ -183,6 +184,7 @@ func TestGetNodesInfo(t *testing.T) {
183184
],
184185
"caches": [
185186
{
187+
"id": 0,
186188
"size": 32768,
187189
"type": "unified",
188190
"level": 3
@@ -211,6 +213,7 @@ func TestGetNodesInfo(t *testing.T) {
211213
],
212214
"caches": [
213215
{
216+
"id": 0,
214217
"size": 32768,
215218
"type": "unified",
216219
"level": 3
@@ -222,6 +225,7 @@ func TestGetNodesInfo(t *testing.T) {
222225
},
223226
{
224227
sysfs.CacheInfo{
228+
Id: 0,
225229
Size: 32 * 1024,
226230
Type: "unified",
227231
Level: 3,
@@ -306,6 +310,7 @@ func TestGetNodesInfo(t *testing.T) {
306310
],
307311
"caches": [
308312
{
313+
"id": 0,
309314
"size": 32768,
310315
"type": "unified",
311316
"level": 3
@@ -341,6 +346,7 @@ func TestGetNodesInfo(t *testing.T) {
341346
func TestGetNodesInfoWithOfflineCPUs(t *testing.T) {
342347
fakeSys := &fakesysfs.FakeSysFs{}
343348
c := sysfs.CacheInfo{
349+
Id: 0,
344350
Size: 32 * 1024,
345351
Type: "unified",
346352
Level: 3,
@@ -430,6 +436,7 @@ func TestGetNodesInfoWithOfflineCPUs(t *testing.T) {
430436
],
431437
"caches": [
432438
{
439+
"id": 0,
433440
"size": 32768,
434441
"type": "unified",
435442
"level": 3
@@ -457,6 +464,7 @@ func TestGetNodesInfoWithOfflineCPUs(t *testing.T) {
457464
],
458465
"caches": [
459466
{
467+
"id": 0,
460468
"size": 32768,
461469
"type": "unified",
462470
"level": 3
@@ -632,6 +640,7 @@ func TestGetNodesInfoWithoutCacheInfo(t *testing.T) {
632640
func TestGetNodesInfoWithoutHugePagesInfo(t *testing.T) {
633641
fakeSys := &fakesysfs.FakeSysFs{}
634642
c := sysfs.CacheInfo{
643+
Id: 0,
635644
Size: 32 * 1024,
636645
Type: "unified",
637646
Level: 2,
@@ -698,6 +707,7 @@ func TestGetNodesInfoWithoutHugePagesInfo(t *testing.T) {
698707
],
699708
"caches": [
700709
{
710+
"id": 0,
701711
"size": 32768,
702712
"type": "unified",
703713
"level": 2
@@ -721,6 +731,7 @@ func TestGetNodesInfoWithoutHugePagesInfo(t *testing.T) {
721731
],
722732
"caches": [
723733
{
734+
"id": 0,
724735
"size": 32768,
725736
"type": "unified",
726737
"level": 2
@@ -739,6 +750,7 @@ func TestGetNodesInfoWithoutNodes(t *testing.T) {
739750
fakeSys := &fakesysfs.FakeSysFs{}
740751

741752
c := sysfs.CacheInfo{
753+
Id: 0,
742754
Size: 32 * 1024,
743755
Type: "unified",
744756
Level: 1,
@@ -801,6 +813,7 @@ func TestGetNodesInfoWithoutNodes(t *testing.T) {
801813
],
802814
"caches":[
803815
{
816+
"id": 0,
804817
"size":32768,
805818
"type":"unified",
806819
"level":1
@@ -824,6 +837,7 @@ func TestGetNodesInfoWithoutNodes(t *testing.T) {
824837
],
825838
"caches":[
826839
{
840+
"id": 0,
827841
"size":32768,
828842
"type":"unified",
829843
"level":1
@@ -1303,6 +1317,7 @@ func TestIgnoredNetworkDevices(t *testing.T) {
13031317
func TestGetCacheInfo(t *testing.T) {
13041318
fakeSys := &fakesysfs.FakeSysFs{}
13051319
cacheInfo := sysfs.CacheInfo{
1320+
Id: 0,
13061321
Size: 1024,
13071322
Type: "Data",
13081323
Level: 3,

0 commit comments

Comments
 (0)