]> TLD Linux GIT Repositories - rc-scripts.git/blob - rc.d/init.d/cpusets
- version 0.5.4
[rc-scripts.git] / rc.d / init.d / cpusets
1 #!/bin/sh
2 #
3 # cpusets       Create/remove cpusets
4 #
5 # chkconfig:    12345 01 99
6 # description:  Creates/Removes all cpu sets configured to \
7 #               start at boot time.
8 #
9 # probe:        true
10
11
12 . /etc/sysconfig/system
13
14 if [ "${CPUSETS:-no}" = "no" ]; then
15         case "$1" in
16         start|stop|restart)
17                 exit 0
18                 ;;
19         esac
20 fi
21
22 # Source function library.
23 . /etc/rc.d/init.d/functions
24
25 CGDIR=
26 CSUBSYS=
27 if grep -q "/dev/cgroup" /proc/mounts ; then
28         CGDIR="/dev/cgroup"
29 elif grep -q "/dev/cpuset" /proc/mounts ; then
30         CGDIR="/dev/cpuset"
31 fi
32
33 if [ -n "${CGDIR}" ]; then
34         if [ -e ${CGDIR}/cpuset.cpus ]; then
35                 CSUBSYS="cpuset."
36         elif [ ! -e ${CGDIR}/cpus ]; then
37                 nls "ERROR: CGROUP/CPUSET mounted in a way I can't recognize" >&2
38                 exit 1
39         fi
40 fi
41
42 cpuset_mount() {
43         [ -n "${CGDIR}" ] && return
44
45         if grep -q cgroup /proc/filesystems 2>/dev/null ; then
46                 mkdir -p /dev/cpuset 2>/dev/null
47                 mount -t cgroup none /dev/cpuset -ocpuset
48                 CSUBSYS="cpuset."
49         elif grep -q cpuset /proc/filesystems 2>/dev/null ; then
50                 mkdir -p /dev/cpuset 2>/dev/null
51                 mount -t cpuset none /dev/cpuset
52                 CSUBSYS=""
53         else
54                 nls "ERROR: CGROUP/CPUSET support not enabled in kernel" >&2
55                 exit 1
56         fi
57         CGDIR="/dev/cpuset"
58 }
59
60 cpuset_create() {
61         local CPUS MEMS CPU_EXCLUSIVE MEM_EXCLUSIVE NOTIFY_ON_RELEASE TASKS
62
63         . /etc/sysconfig/cpusets/cpuset-$i
64
65         if mkdir "${CGDIR}/${NAME}" >/dev/null 2>&1 ; then
66                 [ -n "$CPUS" ] && echo "$CPUS" >"${CGDIR}/${NAME}/${CSUBSYS}cpus"
67                 [ -n "$MEMS" ] && echo "$MEMS" >"${CGDIR}/${NAME}/${CSUBSYS}mems"
68                 [ -n "$CPU_EXCLUSIVE" ] && echo "$CPU_EXCLUSIVE" >"${CGDIR}/${NAME}/${CSUBSYS}cpu_exclusive"
69                 [ -n "$MEM_EXCLUSIVE" ] && echo "$MEM_EXCLUSIVE" >"${CGDIR}/${NAME}/${CSUBSYS}mem_exclusive"
70                 [ -n "$VIRTUALIZE" ] && echo "$VIRTUALIZE" >"${CGDIR}/${NAME}/${CSUBSYS}virtualize"
71                 [ -n "$NOTIFY_ON_RELEASE" ] && echo "$NOTIFY_ON_RELEASE" >"${CGDIR}/${NAME}/${CSUBSYS}notify_on_release"
72                 [ -n "$TASKS" ] && echo "$TASKS" >"${CGDIR}/${NAME}/tasks"
73                 return 0
74         fi
75         return 1
76 }
77
78 cpuset_remove() {
79         local CPUS MEMS CPU_EXCLUSIVE MEM_EXCLUSIVE NOTIFY_ON_RELEASE TASKS
80
81         . /etc/sysconfig/cpusets/cpuset-$i
82
83         # This MUST be rmdir (not rm -rf)
84         if rmdir "${CGDIR}/${NAME}" >/dev/null 2>&1 ; then
85                 return 0
86         else
87                 return 1
88         fi
89 }
90
91 cpuset_empty() {
92         if [ $(cat "${CGDIR}/$1/tasks" 2>/dev/null | wc -c) -eq 0 ] ; then
93                 # true returns zero
94                 return 0
95         else
96                 # false returns one
97                 return 1
98         fi
99 }
100
101 start() {
102         rc_splash "bootcpusets start"
103
104         for i in $cpusets_boot; do
105                 show "$(nls -n "Creating cpuset %s" "$i")"
106                 if cpuset_create $i ; then
107                         ok
108                 else
109                         fail
110                 fi
111         done
112
113         touch /var/lock/subsys/cpusets
114 }
115
116 stop() {
117         for i in $cpusets_boot; do
118                 show "$(nls -n "Removing cpuset %s" "$i")"
119                 busy
120                 if cpuset_empty $i; then
121                         if cpuset_remove $i; then
122                                 ok
123                         else
124                                 fail
125                         fi
126                 else
127                         fail
128                 fi
129         done
130
131         rm -f /var/lock/subsys/cpusets >/dev/null 2>&1
132 }
133
134 # Get list of config files
135 # ignores editor backup files and rpm blackups
136 cpuset_configs() {
137         local match="$1"
138         for a in /etc/sysconfig/cpusets/$match; do
139                 case "$a" in
140                 *rpmorig|*rpmnew|*rpmsave|*~|*.orig)
141                         continue
142                         ;;
143                 *)
144                         echo $a
145                 ;;
146                 esac
147         done
148 }
149
150 cpuset_files="$(cpuset_configs 'cpuset-*')"
151 cpusets_boot=$(
152         for i in $cpuset_files; do
153                 ONBOOT=""; . "$i" 2>/dev/null
154                 is_yes "$ONBOOT" && echo "${i##*/cpuset-}"
155         done
156 )
157
158 # See how we were called.
159 case "$1" in
160   start)
161         cpuset_mount
162         start
163         ;;
164   stop)
165         stop
166         ;;
167   status)
168         nls "Configured cpusets:"
169         echo "$cpusets_boot"
170         echo
171         nls "Currently empty cpusets:"
172         for i in $(ls /dev/cpuset 2>/dev/null); do
173                 if [ -d ${CGDIR}/$i ]; then
174                         cpuset_empty $i && echo $i
175                 fi
176         done
177         echo
178         nls "Currently active cpusets:"
179         for i in $(ls /dev/cpuset 2>/dev/null); do
180                 if [ -d ${CGDIR}/$i ]; then
181                         cpuset_empty $i || echo $i
182                 fi
183         done
184         echo
185         ;;
186   restart)
187         stop
188         cpuset_mount
189         start
190         ;;
191   *)
192         msg_usage "$0 {start|stop|restart|status}"
193         exit 3
194 esac
195
196 exit 0