????cgroups?????
????Linux ????????????????????????? cgroups ????????????????? cgroups ?????? Linux ?????????????????????? cgroups ???????? Linux?????????????????? VFS (Virtual File System)?? VFS ???????????????????????????????????????????????????? API ???? cgroups ?????? VFS ????????????????cgroups ?? VFS ?????ν??????? cgroups ??????????????????? VFS ??????????????????? cgroups ???????????
????VFS
????VFS ?????????????????????????????????????????????????????????? API ????VFS ??????????????????????????????????????? VFS ?????????????? VFS ?У????????????д??????????? ????????????????е?????????????????????????????????????????????????????????VFS???????? c ??????????????????????
?????????????
????VFS ??????????а???????????????????
?????????????(superblock object)???????????????????????????????ext2??ext3????Щ?????????????????????????дsocket??socket????????????????????дcgroups????????? cgroups ????????
??????????????(inode object)??????????????????????????????????????????inode ????????????????????е?洢????????????socket???????inode????socket????????????????cgroups?????????????????inode?????? cgroup ????????????????????????????????????????????? inode_operations ????壬??????嶨???????????????д????????????????????????
???????????(file object)???????????????????????????????????????????????????????????????????????????б????????????????? file_operations ????壬?????????????????????????д???????????????????????????????д???????????????? file_operations ?ж????????? ???????????????????file_operations ?ж?????????????豸??д??????????socket???????file_operations ?ж?????? socket ????? send/recv ?????????????cgroups?????????????????file_operations ?ж????????? cgroup ?????????????
???????????(dentry object)?????????????У?????????????·???е?????????????·?????????????????????????????????????????????????? inode ??????????????????棬????????????????
????cgroups??????????
???????? VFS ???????????????????? VFS ???????????????Щ???????????Щ?????ж????????????cgroup ??????????????????????? cgroups ????Щ???????塣
???????????? cgroups ????????????壺
????static struct file_system_type cgroup_fs_type = {
????.name = "cgroup"??
????.mount = cgroup_mount??
????.kill_sb = cgroup_kill_sb??
????};
????????????????????????????ж?????? cgroup ????????????е?????????????? cgroups ?????????????????????cgroup_mount ??????????????????????????? cgroups_root??cgroups????????????????????????
?????????? cgroups ?????????????????
????static const struct super_operations cgroup_ops = {
????.statfs = simple_statfs??
????.drop_inode = generic_delete_inode??
????.show_options = cgroup_show_options??
????.remount_fs = cgroup_remount??
????};
??????????в?????????????????????????????????????????????????????? super_operations ????????????????????????????????豸?????????????????????? fseek ????????λ??????????????? socket ???? cgroups ????????????????????????????????
????????????? cgroups ??????? inode ????? file ???????????????????
static const struct inode_operations cgroup_dir_inode_operations = {
.lookup = cgroup_lookup??
.mkdir = cgroup_mkdir??
.rmdir = cgroup_rmdir??
.rename = cgroup_rename??
};
static const struct file_operations cgroup_file_operations = {
.read = cgroup_file_read??
.write = cgroup_file_write??
.llseek = generic_file_llseek??
.open = cgroup_file_open??
.release = cgroup_file_release??
};
?????????????о???Щ????????????????????????????Щ?????????????cgroups ?????? VFS ??????????????????? cgroups ???????????????? cgroups ?????????Щ???????С?
????????????????????????????? cgroups ??????????????? VFS ?????? cgroups ???????????????????????????? cgroups ?????????????????????
????cgroups??÷???
????cgroups?????????

????Linux?У???????????mount??????? cgroups ???????????: mount -t cgroup -o subsystems name /cgroup/name?????? subsystems ??????????? cgroups ?????? /cgroup/name ??????????????????????????????????д????????cgroups ??????
??????????? cpuset?? cpu?? cpuacct?? memory 4??subsystem??/cgroup/cpu_and_mem ???£???????? mount -t cgroup -o remount??cpu??cpuset??memory cpu_and_mem /cgroup/cpu_and_mem
??????centos???棬?????yum install libcgroup?????cgroups???????? /etc/cgconfig.conf ????л???????? cgroups ??????????:
????mount {
????cpuset = /cgroup/cpuset;
????cpu = /cgroup/cpu;
????cpuacct = /cgroup/cpuacct;
????memory = /cgroup/memory;
????devices = /cgroup/devices;
????freezer = /cgroup/freezer;
????net_cls = /cgroup/net_cls;
????blkio = /cgroup/blkio;
????}
???????????????????????????? mount ????????mount -t cgroup -o cpuset cpuset /cgroup/cpuset??????????????????????Щ?????????????????????
????????????
???????????? cgroups ??????????????????????????????潨??????л??????cgcreate???????????? cgroups ?????е??????????????cgcreate -t sankuai:sankuai -g cpu:test?????? cpu ??????????????? test ????????????????
????[root@idx cpu]
????# ls
????cgroup.event_control cgroup.procs cpu.cfs_period_us cpu.cfs_quota_us cpu.rt_period_us cpu.rt_runtime_us cpu.shares cpu.stat lxc notify_on_release release_agent tasks test
?????????????д?????????? test ?????????????????????????????????????????涼??????ж????????????????????????????????????????????????ο? cgroups ?????? cgset ????????????? cgroups ??????????????? cgset -r
????parameter=value path_to_cgroup??
???????????????? cgroups ?????????????? cgdelete ?????????????????? test ?????????? cgdelete -r cpu:test??????????
???????????? cgroups ??????ж???????????????? pid д??????????? task ????С????????? cgclassify ??????????? cgclassify -g subsystems:path_to_cgroup pidlist????????????? cgexec ?????? cgroups ??????????????gexec -g subsystems:path_to_cgroup command arguments.
????????е?????
?????????????????ж??? Docker ????????????????????£???????? Docker ????????? Container ????????????????????????????????? cgroups ???????????????????????????? Container ?е????????????Щ???? PID ???????? cgroups ?????????????Щ????????????????????
???????????????????????????????£?????????????????????????? cgroups ???????????????????????飬?????????????????????? cgroups ????У?????????????????
????????????????飬????????????????????????????????“??????”????????????????Щ???????????????????????????????????????ж????????????????????????????????????????“?С??”????????????????????????????????????????????????檔??????????“?С??”????????????μ??????????????????????????????????????????????????????cpu????????????????50%??
?????????? cpu ???????洴??????? halfapi ??????cgcreate abc:abc -g cpu:halfapi??
??????????????????д???????????echo 50000 > /cgroup/cpu/halfapi/cpu.cfs_quota_us??cpu.cfs_quota_us?е???????100000??д??50000?????????50%?? cpu ???????
???????????cgroups????????????cgexec -g “cpu:/halfapi” php halfapi.php half >/dev/null 2>&1
?????? cgroups ????????????????????????????????? cpu ??????????????????? nice ??????????????????????? cpulimit ?????????????y???? cpu ????????????Щ????????????????????????????????????????????? Docker ????????????????????????????????????????????
??????????? cgroups ???????????????????????????????????????????????????????????????? cgroups ?????????????????????????????????????????????? ????????????豸???????io?????????cgroups ???????????????????????????
??????????
????????????????? cgroups ??????е???????????????? cgroups ?????? VFS ???????????????????????????? cgroups ????÷??????????????????? cgroups ??????е??????????????? cgroups ????????????????????
????????????????????μ??????????????? cgroups ???????????????????????????????? cgroups ?????????????????????????????????????????????????