@@ -174,10 +174,11 @@ func (r *nopRecord) Start() {
174174}
175175
176176type Monitor struct {
177- mu sync.Mutex
178- closed chan struct {}
179- records map [string ]* cgroupRecord
180- proc procfs.FS
177+ mu sync.Mutex
178+ closed chan struct {}
179+ records map [string ]* cgroupRecord
180+ proc procfs.FS
181+ rootCgroup string
181182}
182183
183184type NetworkSampler interface {
@@ -221,63 +222,92 @@ func (m *Monitor) Close() error {
221222 return nil
222223}
223224
224- func NewMonitor () (* Monitor , error ) {
225+ func (m * Monitor ) RootCgroup () string {
226+ return m .rootCgroup
227+ }
228+
229+ func NewMonitor (isolateCgroups bool ) (* Monitor , error ) {
230+ fs , err := procfs .NewDefaultFS ()
231+ if err != nil {
232+ return nil , err
233+ }
234+
235+ rootCgroup := ""
236+
225237 initOnce .Do (func () {
226238 isCgroupV2 = isCgroup2 ()
227239 if ! isCgroupV2 {
228240 return
229241 }
230- if err := prepareCgroupControllers (); err != nil {
242+
243+ cgroupPath := defaultMountpoint
244+
245+ if isolateCgroups {
246+ proc , err := fs .Self ()
247+ if err != nil {
248+ bklog .L .Warnf ("failed to get current process info: %+v" , err )
249+ return
250+ }
251+
252+ cgroups , err := proc .Cgroups ()
253+ if err != nil {
254+ bklog .L .Warnf ("failed to get current cgroups: %+v" , err )
255+ return
256+ }
257+
258+ if len (cgroups ) > 0 {
259+ rootCgroup = cgroups [0 ].Path
260+ cgroupPath = filepath .Join (cgroupPath , cgroups [0 ].Path )
261+ }
262+ }
263+
264+ if err := prepareCgroupControllers (cgroupPath ); err != nil {
231265 bklog .L .Warnf ("failed to prepare cgroup controllers: %+v" , err )
232266 }
233267 })
234268
235- fs , err := procfs .NewDefaultFS ()
236- if err != nil {
237- return nil , err
238- }
239-
240269 return & Monitor {
241- closed : make (chan struct {}),
242- records : make (map [string ]* cgroupRecord ),
243- proc : fs ,
270+ rootCgroup : rootCgroup ,
271+ closed : make (chan struct {}),
272+ records : make (map [string ]* cgroupRecord ),
273+ proc : fs ,
244274 }, nil
245275}
246276
247- func prepareCgroupControllers () error {
277+ func prepareCgroupControllers (cgroupPath string ) error {
248278 v , ok := os .LookupEnv ("BUILDKIT_SETUP_CGROUPV2_ROOT" )
249279 if ! ok {
250280 return nil
251281 }
252282 if b , _ := strconv .ParseBool (v ); ! b {
253283 return nil
254284 }
255- // move current process to init cgroup
256- if err := os .MkdirAll (filepath .Join (defaultMountpoint , initGroup ), 0755 ); err != nil {
285+ // move current process to an init cgroup subgroup
286+ if err := os .MkdirAll (filepath .Join (cgroupPath , initGroup ), 0755 ); err != nil {
257287 return err
258288 }
259- f , err := os .OpenFile (filepath .Join (defaultMountpoint , cgroupProcsFile ), os .O_RDONLY , 0 )
289+ f , err := os .OpenFile (filepath .Join (cgroupPath , cgroupProcsFile ), os .O_RDONLY , 0 )
260290 if err != nil {
261291 return err
262292 }
263293 s := bufio .NewScanner (f )
264294 for s .Scan () {
265- if err := os .WriteFile (filepath .Join (defaultMountpoint , initGroup , cgroupProcsFile ), s .Bytes (), 0 ); err != nil {
295+ if err := os .WriteFile (filepath .Join (cgroupPath , initGroup , cgroupProcsFile ), s .Bytes (), 0 ); err != nil {
266296 return err
267297 }
268298 }
269299 if err := f .Close (); err != nil {
270300 return err
271301 }
272- dt , err := os .ReadFile (filepath .Join (defaultMountpoint , cgroupControllersFile ))
302+ dt , err := os .ReadFile (filepath .Join (cgroupPath , cgroupControllersFile ))
273303 if err != nil {
274304 return err
275305 }
276306 for c := range strings .SplitSeq (string (dt ), " " ) {
277307 if c == "" {
278308 continue
279309 }
280- if err := os .WriteFile (filepath .Join (defaultMountpoint , cgroupSubtreeFile ), []byte ("+" + c ), 0 ); err != nil {
310+ if err := os .WriteFile (filepath .Join (cgroupPath , cgroupSubtreeFile ), []byte ("+" + c ), 0 ); err != nil {
281311 // ignore error
282312 bklog .L .Warnf ("failed to enable cgroup controller %q: %+v" , c , err )
283313 }
0 commit comments