-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Rouzip <1226015390@qq.com>
- Loading branch information
Showing
1 changed file
with
301 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,301 @@ | ||
package resourceexecutor | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/koordinator-sh/koordinator/pkg/koordlet/util/system" | ||
) | ||
|
||
/* | ||
TODO: forget how to use it, wait for check | ||
var ( | ||
qosClasses = []string{"", "LSR", "LS", "BE"} | ||
)*/ | ||
|
||
func TestNewResctrlReader(t *testing.T) { | ||
type args struct { | ||
content string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want reflect.Type | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "test amd", | ||
args: args{ | ||
content: "vendor_id : AuthenticAMD\n", | ||
}, | ||
want: reflect.TypeOf(&ResctrlAMDReader{}), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "test intel", | ||
args: args{ | ||
content: "vendor_id : GenuineIntel", | ||
}, | ||
want: reflect.TypeOf(&ResctrlRDTReader{}), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "test arm", | ||
args: args{ | ||
content: "vendor_id : arm", | ||
}, | ||
want: reflect.TypeOf(nil), | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
helper := system.NewFileTestUtil(t) | ||
helper.WriteProcSubFileContents("cpuinfo", tt.args.content) | ||
|
||
rr := NewResctrlReader() | ||
assert.Equal(t, tt.want, reflect.TypeOf(rr)) | ||
}) | ||
} | ||
} | ||
|
||
type MockMonData struct { | ||
cacheItems map[int]MockCacheItem | ||
} | ||
|
||
type MockCacheItem map[string]uint64 | ||
|
||
// create mock ctrl group mon_data directory and files | ||
func testingPrepareResctrlMondata(t *testing.T, sysFsRootPath, ctrlGrp string, mmd MockMonData) { | ||
resctrlMonDataPath := filepath.Join(sysFsRootPath, "resctrl", ctrlGrp, "mon_data") | ||
err := os.MkdirAll(resctrlMonDataPath, 0777) | ||
assert.NoError(t, err) | ||
for cacheId, cacheItem := range mmd.cacheItems { | ||
cacheMonDir := filepath.Join(resctrlMonDataPath, fmt.Sprintf("mon_L3_%02d", cacheId)) | ||
err = os.MkdirAll(cacheMonDir, 0777) | ||
assert.NoError(t, err) | ||
for item, value := range cacheItem { | ||
itemPath := filepath.Join(cacheMonDir, item) | ||
err = os.WriteFile(itemPath, []byte(fmt.Sprintf("%d", value)), 0666) | ||
assert.NoError(t, err) | ||
} | ||
} | ||
} | ||
|
||
// just for x86 system | ||
func TestResctrlReader(t *testing.T) { | ||
type args struct { | ||
mmds map[string]MockMonData | ||
qos string | ||
} | ||
|
||
// llc result | ||
llcTests := []struct { | ||
name string | ||
args args | ||
want map[CacheId]uint64 | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "root resctrl get llc result", | ||
args: args{ | ||
mmds: map[string]MockMonData{ | ||
"": { | ||
cacheItems: map[int]MockCacheItem{ | ||
0: { | ||
"llc_occupancy": 1, | ||
"mbm_local_bytes": 2, | ||
"mbm_total_bytes": 3, | ||
}, | ||
1: { | ||
"llc_occupancy": 4, | ||
"mbm_local_bytes": 5, | ||
"mbm_total_bytes": 6, | ||
}, | ||
}, | ||
}, | ||
}, | ||
qos: "", | ||
}, | ||
want: map[CacheId]uint64{ | ||
0: 1, | ||
1: 4, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "be qos get llc result", | ||
args: args{ | ||
mmds: map[string]MockMonData{ | ||
"": { | ||
cacheItems: map[int]MockCacheItem{ | ||
0: { | ||
"llc_occupancy": 1, | ||
"mbm_local_bytes": 2, | ||
"mbm_total_bytes": 3, | ||
}, | ||
1: { | ||
"llc_occupancy": 4, | ||
"mbm_local_bytes": 5, | ||
"mbm_total_bytes": 6, | ||
}, | ||
}, | ||
}, | ||
"BE": { | ||
cacheItems: map[int]MockCacheItem{ | ||
0: { | ||
"llc_occupancy": 11, | ||
"mbm_local_bytes": 21, | ||
"mbm_total_bytes": 31, | ||
}, | ||
1: { | ||
"llc_occupancy": 41, | ||
"mbm_local_bytes": 51, | ||
"mbm_total_bytes": 61, | ||
}, | ||
}, | ||
}, | ||
}, | ||
qos: "BE", | ||
}, | ||
want: map[CacheId]uint64{ | ||
0: 11, | ||
1: 41, | ||
}, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, test := range llcTests { | ||
t.Run("test resctrl llc collect", func(t *testing.T) { | ||
helper := system.NewFileTestUtil(t) | ||
defer helper.Cleanup() | ||
|
||
// add cpuinfo for intel cpuid | ||
helper.WriteProcSubFileContents("cpuinfo", "vendor_id : GenuineIntel\nflags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 invpcid_single intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect wbnoinvd dtherm ida arat pln pts avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq la57 rdpid fsrm md_clear pconfig flush_l1d arch_capabilities") | ||
|
||
for ctrlGrp, mmd := range test.args.mmds { | ||
testingPrepareResctrlMondata(t, system.Conf.SysFSRootDir, ctrlGrp, mmd) | ||
} | ||
|
||
data, err := NewResctrlReader().ReadResctrlL3Stat(test.args.qos) | ||
assert.NoError(t, err) | ||
|
||
for cacheId, value := range data { | ||
assert.Equal(t, test.want[cacheId], value) | ||
} | ||
}) | ||
} | ||
|
||
mbTests := []struct { | ||
name string | ||
args args | ||
want map[CacheId]system.MBStatData | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "root resctrl get llc result", | ||
args: args{ | ||
mmds: map[string]MockMonData{ | ||
"": { | ||
cacheItems: map[int]MockCacheItem{ | ||
0: { | ||
"llc_occupancy": 1, | ||
"mbm_local_bytes": 2, | ||
"mbm_total_bytes": 3, | ||
}, | ||
1: { | ||
"llc_occupancy": 4, | ||
"mbm_local_bytes": 5, | ||
"mbm_total_bytes": 6, | ||
}, | ||
}, | ||
}, | ||
}, | ||
qos: "", | ||
}, | ||
want: map[CacheId]system.MBStatData{ | ||
0: { | ||
"mbm_local_bytes": 2, | ||
"mbm_total_bytes": 3, | ||
}, | ||
1: { | ||
"mbm_local_bytes": 5, | ||
"mbm_total_bytes": 6, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "be qos get llc result", | ||
args: args{ | ||
mmds: map[string]MockMonData{ | ||
"": { | ||
cacheItems: map[int]MockCacheItem{ | ||
0: { | ||
"llc_occupancy": 1, | ||
"mbm_local_bytes": 2, | ||
"mbm_total_bytes": 3, | ||
}, | ||
1: { | ||
"llc_occupancy": 4, | ||
"mbm_local_bytes": 5, | ||
"mbm_total_bytes": 6, | ||
}, | ||
}, | ||
}, | ||
"BE": { | ||
cacheItems: map[int]MockCacheItem{ | ||
0: { | ||
"llc_occupancy": 11, | ||
"mbm_local_bytes": 21, | ||
"mbm_total_bytes": 31, | ||
}, | ||
1: { | ||
"llc_occupancy": 41, | ||
"mbm_local_bytes": 51, | ||
"mbm_total_bytes": 61, | ||
}, | ||
}, | ||
}, | ||
}, | ||
qos: "BE", | ||
}, | ||
want: map[CacheId]system.MBStatData{ | ||
0: { | ||
"mbm_local_bytes": 21, | ||
"mbm_total_bytes": 31, | ||
}, | ||
1: { | ||
"mbm_local_bytes": 51, | ||
"mbm_total_bytes": 61, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, test := range mbTests { | ||
t.Run("test resctrl mb collect", func(t *testing.T) { | ||
helper := system.NewFileTestUtil(t) | ||
defer helper.Cleanup() | ||
|
||
// add cpuinfo for intel cpuid | ||
helper.WriteProcSubFileContents("cpuinfo", "vendor_id : GenuineIntel\nflags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 invpcid_single intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect wbnoinvd dtherm ida arat pln pts avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq la57 rdpid fsrm md_clear pconfig flush_l1d arch_capabilities") | ||
|
||
for ctrlGrp, mmd := range test.args.mmds { | ||
testingPrepareResctrlMondata(t, system.Conf.SysFSRootDir, ctrlGrp, mmd) | ||
} | ||
|
||
data, err := NewResctrlReader().ReadResctrlMBStat(test.args.qos) | ||
assert.NoError(t, err) | ||
|
||
for cacheId, value := range data { | ||
assert.Equal(t, test.want[cacheId], value) | ||
} | ||
}) | ||
} | ||
} |