Outline. Cgroup hierarchies

Similar documents
Outline. Cgroup hierarchies

Control Groups (cgroups)

What s new in control groups (cgroups) v2

Chapter Two. Lesson A. Objectives. Exploring the UNIX File System and File Security. Understanding Files and Directories

Appendix A GLOSSARY. SYS-ED/ Computer Education Techniques, Inc.

The Classical OS Model in Unix

Manage Directories and Files in Linux. Objectives. Understand the Filesystem Hierarchy Standard (FHS)

Lecture Topics. Announcements. Today: Threads (Stallings, chapter , 4.6) Next: Concurrency (Stallings, chapter , 5.

The Unix Shell & Shell Scripts

Unix as a Platform Exercises. Course Code: OS-01-UNXPLAT

Unix Processes. What is a Process?

User Namespaces. Linux Capabilities and Namespaces. Outline. Michael Kerrisk, man7.org c 2018 March 2018

Welcome to getting started with Ubuntu Server. This System Administrator Manual. guide to be simple to follow, with step by step instructions

5/8/2012. Creating and Changing Directories Chapter 7

Introduction to Linux. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

Roll No. :... Invigilator's Signature : UNIX AND SHELL PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Linux Command Line Primer. By: Scott Marshall

How many of you have never built a NetBSD kernel?

Introduction to Linux

UNIX System Programming Lecture 3: BASH Programming

UNIT I Linux Utilities

Introduction to Linux

Checking Resource Usage in Fedora (Linux)

CENG 334 Computer Networks. Laboratory I Linux Tutorial

Introduction to Linux

Slurm Support for Linux Control Groups

Introduction to Linux. Roman Cheplyaka

Perl and R Scripting for Biologists

Introduction to Linux

CHAPTER 3 SHELL PROGRAMS: SCRIPTS

(MCQZ-CS604 Operating Systems)

Mid Term from Feb-2005 to Nov 2012 CS604- Operating System

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

INSTALLING INSTALLING INSTALLING

IBM AIX Basic Operations V5.

APPLIED INFORMATICS Processes. Bash characteristics. Command type. Aliases.

COSC243 Part 2: Operating Systems

INSE Lab 1 Introduction to UNIX Fall 2017

CSE506: Operating Systems CSE 506: Operating Systems

Disks, Filesystems 1

Embedded Linux Systems. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Understanding user namespaces

A Brief Introduction to Unix

Introduction of Linux

Chapter 1 - Introduction. September 8, 2016

$Id: asg4-shell-tree.mm,v :36: $

Unix API Books. Linux Kernel Books. Assignments and Exams. Grading for CSC 256. Operating Systems 8/31/2018. CSC 256/456 Fall

Introduction to remote command line Linux. Research Computing Team University of Birmingham

Scripting Languages Course 1. Diana Trandabăț

OS Containers. Michal Sekletár November 06, 2016

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT

An introduction to cgroups and cgroupspy tags = [ python, docker, coreos',

Use of interaction networks in teaching Minix

System Programming. Introduction to Unix

Read the relevant material in Sobell! If you want to follow along with the examples that follow, and you do, open a Linux terminal.

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

First of all, these notes will cover only a small subset of the available commands and utilities, and will cover most of those in a shallow fashion.

CSE 303 Lecture 2. Introduction to bash shell. read Linux Pocket Guide pp , 58-59, 60, 65-70, 71-72, 77-80

Containers. Pablo F. Ordóñez. October 18, 2018

5/20/2007. Touring Essential Programs

CS246 Spring14 Programming Paradigm Notes on Linux

INSTALLING INSTALLING INSTALLING

Using bash. Administrative Shell Scripting COMP2101 Fall 2017

The Unix Environment for Programming (COMP433)

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

SOFTWARE ARCHITECTURE 3. SHELL

Linux & Shell Programming 2014

Operating System Labs. Yuanbin Wu

Operating Systems. II. Processes

CS 450 Operating System Week 4 Lecture Notes

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

Disks, Filesystems, Booting Todd Kelley CST8177 Todd Kelley 1

Introduction to Linux Part I: The Filesystem Luca Heltai

ECE 550D Fundamentals of Computer Systems and Engineering. Fall 2017

UNIX Tutorial One

Reading Assignment 4. n Chapter 4 Threads, due 2/7. 1/31/13 CSE325 - Processes 1

Presented by Bill Genske Gary Jackson

Question No: 1 In capacity planning exercises, which tools assist in listing and identifying processes of interest? (Choose TWO correct answers.

UNIT I Linux Utilities and Working with Bash

Passthrough in QEMU/KVM on Linux

Project 3: An Introduction to File Systems. COP 4610 / CGS 5765 Principles of Operating Systems

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

Please choose the best answer. More than one answer might be true, but choose the one that is best.

Exercise Sheet 2. (Classifications of Operating Systems)

Operating Systems Design 12a. Special File Systems

PESIT Bangalore South Campus

Processes and Threads. Processes and Threads. Processes (2) Processes (1)

System control mechanisms

An introduction to Docker

First of all, these notes will cover only a small subset of the available commands and utilities, and will cover most of those in a shallow fashion.

Operating Systems. Copyleft 2005, Binnur Kurt

Operating Systems 2014 Assignment 4: File Systems

CSE506: Operating Systems CSE 506: Operating Systems

System Administration

Operating Systems 3. Operating Systems. Content. What is an Operating System? What is an Operating System? Resource Abstraction and Sharing

Rover Documentation Tracing with Perf, Conversion to CTF, and analysis with TraceCompass

4.8 Summary. Practice Exercises

Announcement. Exercise #2 will be out today. Due date is next Monday

Transcription:

Outline 15 Cgroups 15-1 15.1 Introduction to cgroups v1 and v2 15-3 15.2 Cgroups v1: hierarchies and controllers 15-17 15.3 Cgroups v1: populating a cgroup 15-24 15.4 Cgroups v1: a survey of the controllers 15-38 15.5 Cgroups /proc files 15-56 Cgroup hierarchies Cgroup == collection of processes cgroup hierarchy == hierarchical arrangement of cgroups Implemented via a cgroup pseudo-filesystem Structure and membership of cgroup hierarchy is defined by: 1 Mounting a cgroup filesystem 2 Creating a subdirectory structure that reflects desired cgroup hierarchy 3 Moving processes within hierarchy by writing their PIDs to special files in cgroup subdirectories System Programming for Linux Containers c 2017, Michael Kerrisk Cgroups 15-18 15.2

Attaching a controller to a hierarchy A controller is attached to a hierarchy by mounting a cgroup filesystem: # mkdir -p / sys / fs/ cgroup / pids # Create mount point # mount -t cgroup -o pids none / sys / fs/ cgroup / pids Here, pids controller was mounted none can be replaced by any suitable mnemonic name Not interpreted by system, but appears in /proc/mounts System Programming for Linux Containers c 2017, Michael Kerrisk Cgroups 15-19 15.2 Attaching a controller to a hierarchy To see which cgroup filesystems are mounted and their attached controllers: # mount grep cgroup none on / sys /fs/ cgroup / pids type cgroup (rw, pids ) # cat / proc / mounts grep cgroup none / sys /fs/ cgroup / pids cgroup rw,..., pids 0 0 Unmounting filesystem detaches the controller: # umount / sys /fs/ cgroup / pids But..., filesystem will remain (invisibly) mounted if it contains child cgroups I.e., must move all processes to root cgroup, and remove child cgroups, to truly unmount System Programming for Linux Containers c 2017, Michael Kerrisk Cgroups 15-20 15.2

Attaching controllers to hierarchies A controller can be attached to only one hierarchy Mounting same controller at different mount point simply creates second view of same hierarchy Multiple controllers can be attached to same hierarchy: # mkdir -p / sys /fs/ cgroup / mem_cpu # mount -t cgroup -o memory, cpu none \ / sys /fs/ cgroup / mem_cpu In effect, resources associated with those controllers are being managed together Or, all controllers can be attached to one hierarchy: # mount -t cgroup -o all none / some / mount / point -o all is the default if no controller is specified System Programming for Linux Containers c 2017, Michael Kerrisk Cgroups 15-21 15.2 Creating cgroups When a new hierarchy is created, all tasks on system are part of root cgroup for that hierarchy New cgroups are created by creating subdirectories under cgroup mount point: # mkdir / sys /fs/ cgroup / memory /g1 Relationships between cgroups are reflected by creating nested (arbitrarily deep) subdirectory structure Meaning of hierarchical relationship depends on controller System Programming for Linux Containers c 2017, Michael Kerrisk Cgroups 15-22 15.2

Destroying cgroups An empty cgroup can be destroyed by removing directory Empty == last process in cgroup terminates or migrates to another cgroup and last child cgroup is removed Presence of zombie process does not prevent removal of cgroup directory Not necessary (or possible) to delete attribute files inside cgroup directory before deleting it System Programming for Linux Containers c 2017, Michael Kerrisk Cgroups 15-23 15.2

Outline 15 Cgroups 15-1 15.1 Introduction to cgroups v1 and v2 15-3 15.2 Cgroups v1: hierarchies and controllers 15-17 15.3 Cgroups v1: populating a cgroup 15-24 15.4 Cgroups v1: a survey of the controllers 15-38 15.5 Cgroups /proc files 15-56 Files for managing cgroup membership To manage cgroup membership, each subdirectory in a hierarchy includes two automagically created files: cgroup.procs tasks System Programming for Linux Containers c 2017, Michael Kerrisk Cgroups 15-26 15.3

Tasks? Cgroups v1 draws distinction between process and task Task == kernel scheduling entity From scheduler s perspective, processes and threads are pretty much the same thing... (Threads just share more state than processes) Multithreaded (MT) process == set of tasks with same thread group ID (TGID) TGID == PID! Each thread has unique thread ID (TID) Here, TID means kernel thread ID I.e., value returned by clone(2) and gettid(2) Not same as POSIX threads pthread_t (But there is 1:1 relationship in NPTL implementation...) System Programming for Linux Containers c 2017, Michael Kerrisk Cgroups 15-27 15.3 Placing a process in a cgroup To move a process to a cgroup, write its PID to cgroup.procs file in corresponding subdirectory # echo $$ > / sys /fs/ cgroup / memory /g1/ cgroup. procs In multithreaded process, moves all threads to cgroup... Can write only one PID at a time write() fails with EINVAL Writing 0 to cgroup.procs moves writing process to cgroup Some shells don t check for error return from write(2) So we might not see error for an invalid PID... Use /bin/echo since it checks for errors from write(2) System Programming for Linux Containers c 2017, Michael Kerrisk Cgroups 15-28 15.3

Viewing cgroup membership To see PIDs in cgroup, read cgroup.procs file PIDs are newline-separated Zombie processes do not appear in list List is not guaranteed to be sorted or free of duplicates PID might be moved out and back into cgroup or recycled while reading list System Programming for Linux Containers c 2017, Michael Kerrisk Cgroups 15-29 15.3 Placing a thread (task) in a cgroup Writing a PID to cgroup.procs moves all threads in thread group to a cgroup Each cgroup directory also has a tasks file... Writing a TID to tasks moves that thread to cgroup This feature goes away in cgroups v2... Reading tasks shows all TIDs in cgroup System Programming for Linux Containers c 2017, Michael Kerrisk Cgroups 15-30 15.3

Cgroup membership details Within a hierarchy, a task can be member of just one cgroup That association defines attributes / parameters that apply to the task Adding a task to a different cgroup automatically removes it from previous cgroup A task can be a member of multiple cgroups, each of which is in a different hierarchy On fork(), child inherits cgroup memberships of parent Afterward, cgroup memberships of parent and child can be independently changed System Programming for Linux Containers c 2017, Michael Kerrisk Cgroups 15-31 15.3 Cgroup release Consider the following scenario: We create a cgroup subdirectory Some processes are moved into cgroup Eventually, all of those processes terminate (or leave the cgroup) Who cleans up/gets notified when last process leaves cgroup? We might want cgroup subdirectory to be removed Manager process might want to know when all workers have terminated System Programming for Linux Containers c 2017, Michael Kerrisk Cgroups 15-32 15.3

Cgroup release release_agent in cgroup root directory Contains pathname of binary/script that is executed (as root) when cgroup becomes empty E.g., this program might remove cgroup subdirectory Release agent gets one command-line argument: pathname of cgroup subdirectory that has become empty Can also be specified as mount option mount -o release_agent =/ path / to/ executable notify_on_release in each cgroup subdirectory Should release_agent be run when cgroup becomes empty? (0 == no, 1 == yes) Initial setting for this file is inherited from cgroup parent System Programming for Linux Containers c 2017, Michael Kerrisk Cgroups 15-33 15.3 Mounting a named hierarchy with no controller Can mount a named hierarchy with no attached controller: # mount -t cgroup cgroup -o none, name = somename \ / some / mount / point Named hierarchies can be used to organize and track processes E.g., PIDs can be moved into cgroup.procs, and will automatically disappear on process termination (And we can use release_agent, etc.) systemd creates such a hierarchy for its management of processes Mounted at /sys/fs/cgroup/systemd Cgroups v1 only System Programming for Linux Containers c 2017, Michael Kerrisk Cgroups 15-34 15.3

Exercises 1 In this exercise, we create a cgroup, place a process in the cgroup, and then migrate that process to a different cgroup. If the memory cgroup is not already mounted, mount it: # cat / proc / mounts grep cgroup # Is cgroup mounted? # mkdir -p / sys /fs/ cgroup / memory # mount - t cgroup - o memory none / sys / fs/ cgroup / memory # cd / sys / fs/ cgroup / memory Note: some systems (e.g., Debian) provide a patched kernel that disables the memory controller by default. If you find that you can t mount the memory controller, it may be necessary to reboot the kernel with the cgroup_enable=memory command-line option. Alternatively, you could use a different controller for this exercise. Create two subdirectories, m1 and m2, in the memory cgroup root directory. Execute the following command, and note the PID assigned to the resulting process: # sleep 300 & Write the PID of the process created in the previous step into the file m1/cgroup.procs, and verify by reading the file contents. Now write the PID of the process into the file m2/cgroup.procs. Is the PID still visible in the file m1/cgroup.procs? Explain. System Programming for Linux Containers c 2017, Michael Kerrisk Cgroups 15-35 15.3 Exercises 2 In this exercise, we demonstrate the operation of a release_agent. Create a shell script, /tmp/release.sh, with the following content: #! / bin /sh date > / tmp / release. log echo $* >> / tmp / release. log Make the script executable: chmod +x /tmp/release.sh If the memory cgroup is not already mounted, mount it # cat / proc / mounts grep cgroup # Is cgroup mounted? # mkdir -p / sys /fs/ cgroup / memory # mount - t cgroup - o memory none / sys / fs/ cgroup / memory Change your working directory to the memory root cgroup and write the pathname of the script to the release_agent file: # cd / sys / fs/ cgroup / memory # echo / tmp / release. sh > release_agent System Programming for Linux Containers c 2017, Michael Kerrisk Cgroups 15-36 15.3

Exercises Create two subdirectories, x1 and x2, and write the value 1 to the notify_on_release file in x1: # mkdir x1 x2 # echo 1 > x1/ notify_on_release Execute the following command, and note the PID assigned to the resulting process: # sleep 300 & Write the PID of the process created in the previous step into the file x1/cgroup.procs. Now write the PID of the process into the file x2/cgroup.procs. Inspect the contents of the file /tmp/release.log. You should see one line containing the date and time, and a second line containing the pathname /x1. (If /tmp/release.log does not exist, review the above steps, kill the sleep process, and repeat the exercise.) System Programming for Linux Containers c 2017, Michael Kerrisk Cgroups 15-37 15.3