...

Source file src/github.com/containerd/cgroups/freezer.go

Documentation: github.com/containerd/cgroups

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package cgroups
    18  
    19  import (
    20  	"os"
    21  	"path/filepath"
    22  	"strings"
    23  	"time"
    24  )
    25  
    26  func NewFreezer(root string) *freezerController {
    27  	return &freezerController{
    28  		root: filepath.Join(root, string(Freezer)),
    29  	}
    30  }
    31  
    32  type freezerController struct {
    33  	root string
    34  }
    35  
    36  func (f *freezerController) Name() Name {
    37  	return Freezer
    38  }
    39  
    40  func (f *freezerController) Path(path string) string {
    41  	return filepath.Join(f.root, path)
    42  }
    43  
    44  func (f *freezerController) Freeze(path string) error {
    45  	return f.waitState(path, Frozen)
    46  }
    47  
    48  func (f *freezerController) Thaw(path string) error {
    49  	return f.waitState(path, Thawed)
    50  }
    51  
    52  func (f *freezerController) changeState(path string, state State) error {
    53  	return retryingWriteFile(
    54  		filepath.Join(f.root, path, "freezer.state"),
    55  		[]byte(strings.ToUpper(string(state))),
    56  		defaultFilePerm,
    57  	)
    58  }
    59  
    60  func (f *freezerController) state(path string) (State, error) {
    61  	current, err := os.ReadFile(filepath.Join(f.root, path, "freezer.state"))
    62  	if err != nil {
    63  		return "", err
    64  	}
    65  	return State(strings.ToLower(strings.TrimSpace(string(current)))), nil
    66  }
    67  
    68  func (f *freezerController) waitState(path string, state State) error {
    69  	for {
    70  		if err := f.changeState(path, state); err != nil {
    71  			return err
    72  		}
    73  		current, err := f.state(path)
    74  		if err != nil {
    75  			return err
    76  		}
    77  		if current == state {
    78  			return nil
    79  		}
    80  		time.Sleep(1 * time.Millisecond)
    81  	}
    82  }
    83  

View as plain text