Skip to content

Commit

Permalink
Add cache id for cache info.
Browse files Browse the repository at this point in the history
  • Loading branch information
hustcat committed Apr 13, 2021
1 parent ef7e64f commit d72f164
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
2 changes: 2 additions & 0 deletions info/v1/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ type Core struct {
}

type Cache struct {
// Id of memory cache
Id int `json:"id"`
// Size of memory cache in bytes.
Size uint64 `json:"size"`
// Type of memory cache: data, instruction, or unified.
Expand Down
4 changes: 3 additions & 1 deletion machine/topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ func TestTopologyWithoutNodes(t *testing.T) {
sysFs := &fakesysfs.FakeSysFs{}

c := sysfs.CacheInfo{
Id: 0,
Size: 32 * 1024,
Type: "unified",
Level: 0,
Expand Down Expand Up @@ -297,7 +298,7 @@ func TestTopologyWithoutNodes(t *testing.T) {
topologyJSON2, err := json.Marshal(topology[1])
assert.Nil(t, err)

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}`
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}`
expectedTopology2 := `
{
"node_id":1,
Expand All @@ -312,6 +313,7 @@ func TestTopologyWithoutNodes(t *testing.T) {
],
"caches":[
{
"id": 0,
"size":32768,
"type":"unified",
"level":0
Expand Down
21 changes: 17 additions & 4 deletions utils/sysfs/sysfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ var (
)

type CacheInfo struct {
// cache id
Id int
// size in bytes
Size uint64
// cache type - instruction, data, unified
Expand Down Expand Up @@ -292,14 +294,24 @@ func getCPUCount(cache string) (count int, err error) {
return
}

func (fs *realSysFs) GetCacheInfo(id int, name string) (CacheInfo, error) {
cachePath := fmt.Sprintf("%s%d/cache/%s", cacheDir, id, name)
out, err := ioutil.ReadFile(path.Join(cachePath, "/size"))
func (fs *realSysFs) GetCacheInfo(threadId int, name string) (CacheInfo, error) {
cachePath := fmt.Sprintf("%s%d/cache/%s", cacheDir, threadId, name)
out, err := ioutil.ReadFile(path.Join(cachePath, "/id"))
if err != nil {
return CacheInfo{}, err
}
var id int
n, err := fmt.Sscanf(string(out), "%d", &id)
if err != nil || n != 1 {
return CacheInfo{}, err
}

out, err = ioutil.ReadFile(path.Join(cachePath, "/size"))
if err != nil {
return CacheInfo{}, err
}
var size uint64
n, err := fmt.Sscanf(string(out), "%dK", &size)
n, err = fmt.Sscanf(string(out), "%dK", &size)
if err != nil || n != 1 {
return CacheInfo{}, err
}
Expand All @@ -325,6 +337,7 @@ func (fs *realSysFs) GetCacheInfo(id int, name string) (CacheInfo, error) {
return CacheInfo{}, err
}
return CacheInfo{
Id: id,
Size: size,
Level: level,
Type: cacheType,
Expand Down
1 change: 1 addition & 0 deletions utils/sysinfo/sysinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ func addCacheInfo(sysFs sysfs.SysFs, node *info.Node) error {

for _, cache := range caches {
c := info.Cache{
Id: cache.Id,
Size: cache.Size,
Level: cache.Level,
Type: cache.Type,
Expand Down
14 changes: 14 additions & 0 deletions utils/sysinfo/sysinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func TestGetNodesInfo(t *testing.T) {
}{
{
sysfs.CacheInfo{
Id: 0,
Size: 32 * 1024,
Type: "unified",
Level: 3,
Expand Down Expand Up @@ -183,6 +184,7 @@ func TestGetNodesInfo(t *testing.T) {
],
"caches": [
{
"id": 0,
"size": 32768,
"type": "unified",
"level": 3
Expand Down Expand Up @@ -211,6 +213,7 @@ func TestGetNodesInfo(t *testing.T) {
],
"caches": [
{
"id": 0,
"size": 32768,
"type": "unified",
"level": 3
Expand All @@ -222,6 +225,7 @@ func TestGetNodesInfo(t *testing.T) {
},
{
sysfs.CacheInfo{
Id: 0,
Size: 32 * 1024,
Type: "unified",
Level: 3,
Expand Down Expand Up @@ -306,6 +310,7 @@ func TestGetNodesInfo(t *testing.T) {
],
"caches": [
{
"id": 0,
"size": 32768,
"type": "unified",
"level": 3
Expand Down Expand Up @@ -341,6 +346,7 @@ func TestGetNodesInfo(t *testing.T) {
func TestGetNodesInfoWithOfflineCPUs(t *testing.T) {
fakeSys := &fakesysfs.FakeSysFs{}
c := sysfs.CacheInfo{
Id: 0,
Size: 32 * 1024,
Type: "unified",
Level: 3,
Expand Down Expand Up @@ -430,6 +436,7 @@ func TestGetNodesInfoWithOfflineCPUs(t *testing.T) {
],
"caches": [
{
"id": 0,
"size": 32768,
"type": "unified",
"level": 3
Expand Down Expand Up @@ -457,6 +464,7 @@ func TestGetNodesInfoWithOfflineCPUs(t *testing.T) {
],
"caches": [
{
"id": 0,
"size": 32768,
"type": "unified",
"level": 3
Expand Down Expand Up @@ -632,6 +640,7 @@ func TestGetNodesInfoWithoutCacheInfo(t *testing.T) {
func TestGetNodesInfoWithoutHugePagesInfo(t *testing.T) {
fakeSys := &fakesysfs.FakeSysFs{}
c := sysfs.CacheInfo{
Id: 0,
Size: 32 * 1024,
Type: "unified",
Level: 2,
Expand Down Expand Up @@ -698,6 +707,7 @@ func TestGetNodesInfoWithoutHugePagesInfo(t *testing.T) {
],
"caches": [
{
"id": 0,
"size": 32768,
"type": "unified",
"level": 2
Expand All @@ -721,6 +731,7 @@ func TestGetNodesInfoWithoutHugePagesInfo(t *testing.T) {
],
"caches": [
{
"id": 0,
"size": 32768,
"type": "unified",
"level": 2
Expand All @@ -739,6 +750,7 @@ func TestGetNodesInfoWithoutNodes(t *testing.T) {
fakeSys := &fakesysfs.FakeSysFs{}

c := sysfs.CacheInfo{
Id: 0,
Size: 32 * 1024,
Type: "unified",
Level: 1,
Expand Down Expand Up @@ -801,6 +813,7 @@ func TestGetNodesInfoWithoutNodes(t *testing.T) {
],
"caches":[
{
"id": 0,
"size":32768,
"type":"unified",
"level":1
Expand All @@ -824,6 +837,7 @@ func TestGetNodesInfoWithoutNodes(t *testing.T) {
],
"caches":[
{
"id": 0,
"size":32768,
"type":"unified",
"level":1
Expand Down

0 comments on commit d72f164

Please sign in to comment.