Last active
April 17, 2017 18:06
-
-
Save simonklee/9a22e127f6a9f4ad52a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ -z "$1" ]; then | |
MB=1024 | |
else | |
MB="$1" | |
fi | |
if [ -z "$2" ]; then | |
browser="google-chrome-beta" | |
else | |
browser="$2" | |
fi | |
if [ -z "$3" ]; then | |
url="http://www.kogama.com/games/play/68818/?da=1&webgl=1" | |
else | |
url="$3" | |
fi | |
if type $browser &>/dev/null; then | |
browser="`which ${browser}`" | |
else | |
exit 1; | |
fi | |
re='^[0-9]+$' | |
if ! [[ $MB =~ $re ]] ; then | |
echo "error: Not a number" >&2; exit 1 | |
fi | |
BYTES=$(echo $((${MB}*1024*1024)) | cut -d. -f1) | |
SHARES=1024 | |
SWAPPINESS=0 | |
echo "Memory available $MB MB" | |
echo "`${browser} --version`" | |
# make sure a "chrome" cgroup exists in the user tree | |
[ -e /sys/fs/cgroup/memory/user.slice/user-${UID}.slice/chrome ] || \ | |
sudo cgcreate -a ${USER} -g memory,cpu:user.slice/user-${UID}.slice/chrome | |
# fix privileges | |
sudo chown -R ${USER} /sys/fs/cgroup/user.slice/user-${UID}.slice/chrome | |
sudo chmod -R 777 /sys/fs/cgroup/user.slice/user-${UID}.slice/chrome | |
# set the limits | |
echo ${BYTES} > /sys/fs/cgroup/user.slice/user-${UID}.slice/chrome/memory.limit_in_bytes | |
echo ${SHARES} > /sys/fs/cgroup/user.slice/user-${UID}.slice/chrome/cpu.shares | |
echo ${SWAPPINESS} > /sys/fs/cgroup/user.slice/user-${UID}.slice/chrome/memory.swappiness | |
# reparent ourselves, just because we're nice and doing things the right ways | |
# and stuff (doesn't really do much) | |
cgclassify -g memory,cpu:user.slice/user-${UID}.slice/chrome $$ | |
# start browser | |
if [[ $browser == *"firefox"* ]]; then | |
cgexec -g memory,cpu:user.slice/user-${UID}.slice/chrome "${browser}" -private-window $url | |
else | |
cgexec -g memory,cpu:user.slice/user-${UID}.slice/chrome "${browser}" --incognito $url | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment