cgroup--(2)使用

adtxl
2021-09-29 / 0 评论 / 714 阅读 / 正在检测是否收录...

转载自https://tiewei.github.io/posts/howto-use-cgroup

1. 目录结构

cgroup对资源的管理是一个树形结构,类似进程。

相同点 - 分层结构,子进程/cgroup继承父进程/cgroup

不同点 - 进程是一个单根树状结构(pid=0为根),而cgroup整体来看是一个多树的森林结构(hierarchy为根)。

一个典型的hierarchy挂载目录如下

/cgroup/
├── blkio                           <--------------- hierarchy/root cgroup
│   ├── blkio.io_merged             <--------------- subsystem parameter
... ...
│   ├── blkio.weight
│   ├── blkio.weight_device
│   ├── cgroup.event_control
│   ├── cgroup.procs
│   ├── lxc                         <--------------- cgroup
│   │   ├── blkio.io_merged         <--------------- subsystem parameter
│   │   ├── blkio.io_queued
... ... ...
│   │   └── tasks                   <--------------- task list
│   ├── notify_on_release
│   ├── release_agent
│   └── tasks
...

2. cgroup操作准则与方法

2.1 操作准则

1.一个hierarchy可以有多个 subsystem (mount 的时候hierarchy可以attach多个subsystem)

eg.

mount -t cgroup -o cpu,cpuset,memory cpu_and_mem /cgroup/cpu_and_mem

image.png

2.一个已经被挂载的 subsystem 只能被再次挂载在一个空的 hierarchy 上 (已经mount一个subsystem的hierarchy不能挂载一个已经被其它hierarchy挂载的subsystem)

Any single subsystem (such as cpu) cannot be attached to more than one hierarchy if one of those hierarchies has a different subsystem attached to it already.

image67423406c305d0aa.png

3.每个task只能在一个hierarchy的唯一一个cgroup里(不能在同一个hierarchy下有超过一个cgroup的tasks里同时有这个进程的pid)

imagee7aeff8856badc43.png

4.子进程在被fork出时自动继承父进程所在cgroup,但是fork之后就可以按需调整到其他cgroup

image31cc3399f2f56ca0.png

5.其它

  • 限制一个task的唯一方法就是将其加入到一个cgroup的task里
  • 多个subsystem可以挂载到一个hierarchy里, 然后通过不同的cgroup中的subsystem参数来对不同的task进行限额
  • 如果一个hierarchy有太多subsystem,可以考虑重构 - 将subsystem挂到独立的hierarchy; 相应的, 可以将多个hierarchy合并成一个hierarchy
  • 因为可以只挂载少量subsystem, 可以实现只对task单个方面的限额; 同时一个task可以被加到多个hierarchy中,从而实现对多个资源的控制

2.2 操作方法

2.2.1 挂载subsystem

  • 利用cgconfig服务及其配置文件 /etc/cgconfig.conf - 服务启动时自动挂载

subsystem = /cgroup/hierarchy;

  • 命令行操作

mount -t cgroup -o subsystems name /cgroup/name

  • 取消挂载

umount /cgroup/name

eg. 挂载 cpuset, cpu, cpuacct, memory 4个subsystem到/cgroup/cpu_and_mem 目录(hierarchy)

  mount {
      cpuset  = /cgroup/cpu_and_mem;
      cpu    = /cgroup/cpu_and_mem;
      cpuacct = /cgroup/cpu_and_mem;
      memory  = /cgroup/cpu_and_mem;
  }

or

mount -t cgroup -o remount,cpu,cpuset,memory cpu_and_mem /cgroup/cpu_and_mem

2.2.2 新建/删除 cgroup

  • 利用cgconfig服务及其配置文件 /etc/cgconfig.conf - 服务启动时自动挂载
group <name> {
    [<permissions>]    <controller> {  <param name> = <param value>;
        …
    }
    …
}
  • 命令行操作
  • 新建1 cgcreate -t uid:gid -a uid:gid -g subsystems:path
  • 新建2 mkdir /cgroup/hierarchy/name/child_name
  • 删除1 cgdelete subsystems:path (使用 -r 递归删除)
  • 删除2 rm -rf /cgroup/hierarchy/name/child_name (cgconfig service not running)

2.2.3 权限管理

利用cgconfig服务及其配置文件 /etc/cgconfig.conf - 服务启动时自动挂载

perm {
    task {
        uid = <task user>;
        gid = <task group>;
    }
    admin {
      uid = <admin name>;
      gid = <admin group>;
    }
}
  • 命令行操作 chown

eg.

  group daemons {
      cpuset {
          cpuset.mems = 0;
          cpuset.cpus = 0;
      }
  }
  group daemons/sql {
      perm {
          task {
              uid = root;
              gid = sqladmin;
          } admin {
              uid = root;
              gid = root;
          }
      }
      cpuset {
          cpuset.mems = 0;
          cpuset.cpus = 0;
      }
  }

or

  ~]$ mkdir -p /cgroup/red/daemons/sql
  ~]$ chown root:root /cgroup/red/daemons/sql/*
  ~]$ chown root:sqladmin /cgroup/red/daemons/sql/tasks
  ~]$ echo 0 > /cgroup/red/daemons/cpuset.mems
  ~]$ echo 0 > /cgroup/red/daemons/cpuset.cpus
  ~]$ echo 0 > /cgroup/red/daemons/sql/cpuset.mems
  ~]$ echo 0 > /cgroup/red/daemons/sql/cpuset.cpus

2.2.4 cgroup参数设定

命令行1 cgset -r parameter=value path_to_cgroup
命令行2 cgset --copy-from path_to_source_cgroup path_to_target_cgroup
文件 echo value > path_to_cgroup/parameter
eg.

  cgset -r cpuset.cpus=0-1 group1
  cgset --copy-from group1/ group2/
  echo 0-1 > /cgroup/cpuset/group1/cpuset.cpus

2.2.5 添加task

命令行添加进程 cgclassify -g subsystems:path_to_cgroup pidlist
文件添加进程 echo pid > path_to_cgroup/tasks
在cgroup中启动进程 cgexec -g subsystems:path_to_cgroup command arguments
在cgroup中启动服务 echo 'CGROUP_DAEMON="subsystem:control_group"' >> /etc/sysconfig/<service>
利用cgrulesengd服务初始化,在配置文件/etc/cgrules.conf

user<:command> subsystems control_group

其中:
+用户user的所有进程的subsystems限制的group为control_group
+<:command>是可选项,表示对特定命令实行限制
+user可以用@group表示对特定的 usergroup 而非user
+可以用*表示全部
+%表示和前一行的该项相同

eg.

  cgclassify -g cpu,memory:group1 1701 1138
  echo -e "1701\n1138" |tee -a /cgroup/cpu/group1/tasks /cgroup/memory/group1/tasks
  cgexec -g cpu:group1 lynx http://www.redhat.com
  sh -c "echo \$$ > /cgroup/lab1/group1/tasks && lynx http://www.redhat.com"

通过/etc/cgrules.conf 对特定服务限制

  maria          devices        /usergroup/staff
  maria:ftp      devices        /usergroup/staff/ftp
  @student       cpu,memory     /usergroup/student/
  %              memory         /test2/

2.2.6 其他

  • cgsnapshot会根据当前cgroup情况生成/etc/cgconfig.conf文件内容
gsnapshot  [-s] [-b FILE] [-w FILE] [-f FILE] [controller]
  -b, --blacklist=FILE  Set the blacklist configuration file (default /etc/cgsnapshot_blacklist.conf)
  -f, --file=FILE       Redirect the output to output_file
  -s, --silent          Ignore all warnings
  -t, --strict          Don't show the variables which are not on the whitelist
  -w, --whitelist=FILE  Set the whitelist configuration file (don't used by default)
  • 查看进程在哪个cgroup
ps -O cgroup
或
cat /proc/<PID>/cgroup
  • 查看subsystem mount情况

cat /proc/cgroups

lssubsys -m <subsystems>

  • 查看cgroup
    lscgroup
  • 查看cgroup参数值
cgget -r parameter list_of_cgroups
cgget -g <controllers>:<path>
  • cgclear删除hierarchy极其所有cgroup
  • 事件通知API - 目前只支持memory.oom_control
  • 更多

    man 1 cgclassify — the cgclassify command is used to move running tasks to one or more cgroups.
    man 1 cgclear — the cgclear command is used to delete all cgroups in a hierarchy.
    man 5 cgconfig.conf — cgroups are defined in the cgconfig.conf file.
    man 8 cgconfigparser — the cgconfigparser command parses the cgconfig.conf file and mounts hierarchies.
    man 1 cgcreate — the cgcreate command creates new cgroups in hierarchies.
    man 1 cgdelete — the cgdelete command removes specified cgroups.
    man 1 cgexec — the cgexec command runs tasks in specified cgroups.
    man 1 cgget — the cgget command displays cgroup parameters.
    man 1 cgsnapshot — the cgsnapshot command generates a configuration file from existing subsystems.
    man 5 cgred.conf — cgred.conf is the configuration file for the cgred service.
    man 5 cgrules.conf — cgrules.conf contains the rules used for determining when tasks belong to certain cgroups.
    man 8 cgrulesengd — the cgrulesengd service distributes tasks to cgroups.
    man 1 cgset — the cgset command sets parameters for a cgroup.
    man 1 lscgroup — the lscgroup command lists the cgroups in a hierarchy.
    man 1 lssubsys — the lssubsys command lists the hierarchies containing the specified subsystems.
0

评论 (0)

取消