11package  fs
22
33import  (
4+ 	"bufio" 
45	"errors" 
56	"os" 
67	"path/filepath" 
78	"strconv" 
89	"strings" 
10+ 	"sync" 
911
1012	"golang.org/x/sys/unix" 
1113
@@ -14,6 +16,41 @@ import (
1416	"github.com/opencontainers/runc/libcontainer/configs" 
1517)
1618
19+ var  (
20+ 	cpusetLock    sync.Mutex 
21+ 	cpusetPrefix  string 
22+ 	cpusetInit    bool 
23+ )
24+ 
25+ func  getCpusetPrefix () string  {
26+ 	cpusetLock .Lock ()
27+ 	defer  cpusetLock .Unlock ()
28+ 
29+ 	if  cpusetInit  {
30+ 		return  cpusetPrefix 
31+ 	}
32+ 
33+ 	cpusetInit  =  true 
34+ 	cpusetPrefix  =  "cpuset." 
35+ 
36+ 	file , err  :=  os .Open ("/proc/self/mountinfo" )
37+ 	if  err  !=  nil  {
38+ 		return  cpusetPrefix 
39+ 	}
40+ 	defer  file .Close ()
41+ 
42+ 	scanner  :=  bufio .NewScanner (file )
43+ 
44+ 	for  scanner .Scan () {
45+ 		line  :=  scanner .Text ()
46+ 		if  strings .Contains (line , "cpuset" ) &&  strings .Contains (line , "cgroup" ) &&  strings .Contains (line , "noprefix" ) {
47+ 			cpusetPrefix  =  "" 
48+ 			break 
49+ 		}
50+ 	}
51+ 	return  cpusetPrefix 
52+ }
53+ 
1754type  CpusetGroup  struct {}
1855
1956func  (s  * CpusetGroup ) Name () string  {
@@ -26,12 +63,12 @@ func (s *CpusetGroup) Apply(path string, r *configs.Resources, pid int) error {
2663
2764func  (s  * CpusetGroup ) Set (path  string , r  * configs.Resources ) error  {
2865	if  r .CpusetCpus  !=  ""  {
29- 		if  err  :=  cgroups .WriteFile (path , "cpuset. cpus"r .CpusetCpus ); err  !=  nil  {
66+ 		if  err  :=  cgroups .WriteFile (path , getCpusetPrefix () + " cpus"r .CpusetCpus ); err  !=  nil  {
3067			return  err 
3168		}
3269	}
3370	if  r .CpusetMems  !=  ""  {
34- 		if  err  :=  cgroups .WriteFile (path , "cpuset. mems"r .CpusetMems ); err  !=  nil  {
71+ 		if  err  :=  cgroups .WriteFile (path , getCpusetPrefix () + " mems"r .CpusetMems ); err  !=  nil  {
3572			return  err 
3673		}
3774	}
@@ -83,57 +120,57 @@ func getCpusetStat(path string, file string) ([]uint16, error) {
83120func  (s  * CpusetGroup ) GetStats (path  string , stats  * cgroups.Stats ) error  {
84121	var  err  error 
85122
86- 	stats .CPUSetStats .CPUs , err  =  getCpusetStat (path , "cpuset. cpus"
123+ 	stats .CPUSetStats .CPUs , err  =  getCpusetStat (path , getCpusetPrefix () + " cpus"
87124	if  err  !=  nil  &&  ! errors .Is (err , os .ErrNotExist ) {
88125		return  err 
89126	}
90127
91- 	stats .CPUSetStats .CPUExclusive , err  =  fscommon .GetCgroupParamUint (path , "cpuset. cpu_exclusive"
128+ 	stats .CPUSetStats .CPUExclusive , err  =  fscommon .GetCgroupParamUint (path , getCpusetPrefix () + " cpu_exclusive"
92129	if  err  !=  nil  &&  ! errors .Is (err , os .ErrNotExist ) {
93130		return  err 
94131	}
95132
96- 	stats .CPUSetStats .Mems , err  =  getCpusetStat (path , "cpuset. mems"
133+ 	stats .CPUSetStats .Mems , err  =  getCpusetStat (path , getCpusetPrefix () + " mems"
97134	if  err  !=  nil  &&  ! errors .Is (err , os .ErrNotExist ) {
98135		return  err 
99136	}
100137
101- 	stats .CPUSetStats .MemHardwall , err  =  fscommon .GetCgroupParamUint (path , "cpuset. mem_hardwall"
138+ 	stats .CPUSetStats .MemHardwall , err  =  fscommon .GetCgroupParamUint (path , getCpusetPrefix () + " mem_hardwall"
102139	if  err  !=  nil  &&  ! errors .Is (err , os .ErrNotExist ) {
103140		return  err 
104141	}
105142
106- 	stats .CPUSetStats .MemExclusive , err  =  fscommon .GetCgroupParamUint (path , "cpuset. mem_exclusive"
143+ 	stats .CPUSetStats .MemExclusive , err  =  fscommon .GetCgroupParamUint (path , getCpusetPrefix () + " mem_exclusive"
107144	if  err  !=  nil  &&  ! errors .Is (err , os .ErrNotExist ) {
108145		return  err 
109146	}
110147
111- 	stats .CPUSetStats .MemoryMigrate , err  =  fscommon .GetCgroupParamUint (path , "cpuset. memory_migrate"
148+ 	stats .CPUSetStats .MemoryMigrate , err  =  fscommon .GetCgroupParamUint (path , getCpusetPrefix () + " memory_migrate"
112149	if  err  !=  nil  &&  ! errors .Is (err , os .ErrNotExist ) {
113150		return  err 
114151	}
115152
116- 	stats .CPUSetStats .MemorySpreadPage , err  =  fscommon .GetCgroupParamUint (path , "cpuset. memory_spread_page"
153+ 	stats .CPUSetStats .MemorySpreadPage , err  =  fscommon .GetCgroupParamUint (path , getCpusetPrefix () + " memory_spread_page"
117154	if  err  !=  nil  &&  ! errors .Is (err , os .ErrNotExist ) {
118155		return  err 
119156	}
120157
121- 	stats .CPUSetStats .MemorySpreadSlab , err  =  fscommon .GetCgroupParamUint (path , "cpuset. memory_spread_slab"
158+ 	stats .CPUSetStats .MemorySpreadSlab , err  =  fscommon .GetCgroupParamUint (path , getCpusetPrefix () + " memory_spread_slab"
122159	if  err  !=  nil  &&  ! errors .Is (err , os .ErrNotExist ) {
123160		return  err 
124161	}
125162
126- 	stats .CPUSetStats .MemoryPressure , err  =  fscommon .GetCgroupParamUint (path , "cpuset. memory_pressure"
163+ 	stats .CPUSetStats .MemoryPressure , err  =  fscommon .GetCgroupParamUint (path , getCpusetPrefix () + " memory_pressure"
127164	if  err  !=  nil  &&  ! errors .Is (err , os .ErrNotExist ) {
128165		return  err 
129166	}
130167
131- 	stats .CPUSetStats .SchedLoadBalance , err  =  fscommon .GetCgroupParamUint (path , "cpuset. sched_load_balance"
168+ 	stats .CPUSetStats .SchedLoadBalance , err  =  fscommon .GetCgroupParamUint (path , getCpusetPrefix () + " sched_load_balance"
132169	if  err  !=  nil  &&  ! errors .Is (err , os .ErrNotExist ) {
133170		return  err 
134171	}
135172
136- 	stats .CPUSetStats .SchedRelaxDomainLevel , err  =  fscommon .GetCgroupParamInt (path , "cpuset. sched_relax_domain_level"
173+ 	stats .CPUSetStats .SchedRelaxDomainLevel , err  =  fscommon .GetCgroupParamInt (path , getCpusetPrefix () + " sched_relax_domain_level"
137174	if  err  !=  nil  &&  ! errors .Is (err , os .ErrNotExist ) {
138175		return  err 
139176	}
@@ -172,10 +209,10 @@ func (s *CpusetGroup) ApplyDir(dir string, r *configs.Resources, pid int) error
172209}
173210
174211func  getCpusetSubsystemSettings (parent  string ) (cpus , mems  string , err  error ) {
175- 	if  cpus , err  =  cgroups .ReadFile (parent , "cpuset. cpus"err  !=  nil  {
212+ 	if  cpus , err  =  cgroups .ReadFile (parent , getCpusetPrefix () + " cpus"err  !=  nil  {
176213		return 
177214	}
178- 	if  mems , err  =  cgroups .ReadFile (parent , "cpuset. mems"err  !=  nil  {
215+ 	if  mems , err  =  cgroups .ReadFile (parent , getCpusetPrefix () + " mems"err  !=  nil  {
179216		return 
180217	}
181218	return  cpus , mems , nil 
@@ -221,12 +258,12 @@ func cpusetCopyIfNeeded(current, parent string) error {
221258	}
222259
223260	if  isEmptyCpuset (currentCpus ) {
224- 		if  err  :=  cgroups .WriteFile (current , "cpuset. cpus"parentCpus ); err  !=  nil  {
261+ 		if  err  :=  cgroups .WriteFile (current , getCpusetPrefix () + " cpus"parentCpus ); err  !=  nil  {
225262			return  err 
226263		}
227264	}
228265	if  isEmptyCpuset (currentMems ) {
229- 		if  err  :=  cgroups .WriteFile (current , "cpuset. mems"parentMems ); err  !=  nil  {
266+ 		if  err  :=  cgroups .WriteFile (current , getCpusetPrefix () + " mems"parentMems ); err  !=  nil  {
230267			return  err 
231268		}
232269	}
0 commit comments