Jump to content

Systemd / Supervisor scripts (And how to make Emby faster)


Karbowiak

Recommended Posts

Karbowiak

Systemd script (Thanks to alazare619)

/etc/systemd/system/multi-user.target.wants/emby-daemon.service

[Unit]
Description=Emby Daemon
After=syslog.target network.target
 
[Service]
User=<service username>
Group=<service group>
 
Type=simple
Environment=MONO_THREADS_PER_CPU="250",MONO_GC_PARAMS="nursery-size=256m"
# systemctl_restart.sh needs to be created it  should basically just be the following
# #!/bin/sh
# /usr/bin/sudo systemctl restart emby-daemon.service
ExecStart=/usr/bin/mono-sgen --optimize=all /usr/lib/emby-server/MediaBrowser.Server.Mono.exe -programdata /var/lib/emby-server -ffmpeg /usr/bin/ffmpeg -ffprobe /usr/bin/ffprobe -restartpath /usr/lib/emby-server/systemctl_restart.sh
TimeoutStopSec=20
KillMode=process
Restart=always
 
[Install]
WantedBy=multi-user.target

Supervisor (Thanks Karbowiak (yes, that's me..))
 

/etc/supervisor/conf.d/emby.conf

[program:emby]
command=/usr/bin/mono-sgen --optimize=all /usr/lib/emby-server/MediaBrowser.Server.Mono.exe -programdata /var/lib/emby-server -ffmpeg /usr/bin/ffmpeg -ffprobe /usr/bin/ffprobe -restartpath /usr/lib/emby-server/restart.sh
directory=/usr/lib/emby-server
environment=MONO_THREADS_PER_CPU="500",MONO_GC_PARAMS="nursery-size=512m"
autostart=true
autorestart=true
redirect_stderr=true
user=emby

There you have the scripts. And to the keen eyed you'll notice --optimize=all
What does this do? Well, it turns on the optimizer flags in Mono, which makes Emby feel A LOT faster.
Especially on mobile.

But after turning it on, the Android app feels several times faster than it did before, and the web interface on desktop is also quite a bit faster.

Ontop of that we've decided to up the mono_threads_per_cpu and nursery-size (Default is 100 and 64MB)

So there you have it, init scripts for supervisor and systemd, and an optimizer flag that makes emby feel a lot faster. (Plus with supervisor and systemd Emby comes back up after a crash, by itself \:D/)

Obligatory cat tax:
241.gif

  • Like 4
Link to comment
Share on other sites

I will love to test this now and include the new values as default in the next package release. :)

Anyway the users will be able to tune this as they like just editing emby-server.conf and changing the values.

 

@Luke: do you think there is any constrains to enable this in general? Both, mono flag (optimize=all), amount of threads and nursery size? What if there is not enough memory to allocate to nursery, would mono/Emby start up at all?

I'm just thinking about user running servers with 512/1GB of RAM.

 

Thanks

Edited by fc7
Link to comment
Share on other sites

  • 2 weeks later...

Systemd script (Thanks to alazare619)

 

/etc/systemd/system/multi-user.target.wants/emby-daemon.service

[Unit]
Description=Emby Daemon
After=syslog.target network.target
 
[Service]
User=<service username>
Group=<service group>
 
Type=simple
Environment=MONO_THREADS_PER_CPU="250",MONO_GC_PARAMS="nursery-size=256m"
# systemctl_restart.sh needs to be created it  should basically just be the following
# #!/bin/sh
# /usr/bin/sudo systemctl restart emby-daemon.service
ExecStart=/usr/bin/mono-sgen --optimize=all /usr/lib/emby-server/MediaBrowser.Server.Mono.exe -programdata /var/lib/emby-server -ffmpeg /usr/bin/ffmpeg -ffprobe /usr/bin/ffprobe -restartpath /usr/lib/emby-server/systemctl_restart.sh
TimeoutStopSec=20
KillMode=process
Restart=always
 
[Install]
WantedBy=multi-user.target

Supervisor (Thanks Karbowiak (yes, that's me..))

 

/etc/supervisor/conf.d/emby.conf

[program:emby]
command=/usr/bin/mono-sgen --optimize=all /usr/lib/emby-server/MediaBrowser.Server.Mono.exe -programdata /var/lib/emby-server -ffmpeg /usr/bin/ffmpeg -ffprobe /usr/bin/ffprobe -restartpath /usr/lib/emby-server/restart.sh
directory=/usr/lib/emby-server
environment=MONO_THREADS_PER_CPU="500",MONO_GC_PARAMS="nursery-size=512m"
autostart=true
autorestart=true
redirect_stderr=true
user=emby

There you have the scripts. And to the keen eyed you'll notice --optimize=all

What does this do? Well, it turns on the optimizer flags in Mono, which makes Emby feel A LOT faster.

Especially on mobile.

 

But after turning it on, the Android app feels several times faster than it did before, and the web interface on desktop is also quite a bit faster.

 

Ontop of that we've decided to up the mono_threads_per_cpu and nursery-size (Default is 100 and 64MB)

 

So there you have it, init scripts for supervisor and systemd, and an optimizer flag that makes emby feel a lot faster. (Plus with supervisor and systemd Emby comes back up after a crash, by itself \:D/)

 

Obligatory cat tax:

241.gif

thx for this, how to implement this if i'm not using a service, do i just paste on the cli the following before starting mediabrowser:

 

[jody@darkstar ~]$ export MONO_THREADS_PER_CPU=500

[jody@darkstar ~]$ export MONO_GC_PARAMS=nursery-size=512m

 

would the amount of threads not depend on your hardware what kind of CPU? I also read on mono side that too high setting will decrease performance so how to find optimal setting? what is your CPU?

And nursery size, same question, is it not hardware dependant? is that memory? I have 12GB of RAM what would be a good setting for that? what do you have?

 

why are you using "mono-sgen" over just "mono"? 

why are you passing the ffmpeg and ffprobe locations (-ffmpeg /usr/bin/ffmpeg -ffprobe /usr/bin/ffprobe)? 

thx in advance :)

Link to comment
Share on other sites

hurricanehrndz

mono is now symlinked to mono-sgen by default. So mono-sgen is used in order to track the process better.

  • Like 1
Link to comment
Share on other sites

Karbowiak

@@jody you could just export the env variables before launching it manually, sure, but you should really use a service handler like systemd or supervisor to keep it alive.
As for the hardware part, sure, i just set mine to 500 threads pr. cpu since it gave a nice speed boost over 100, as for the nursery size, i have 32GB of memory atm, and the default was 64MB, so figured 512 would be more than enough :D

mono-sgen uses a better garbage collector, so that's why, and the ffmpeg and ffprobe locations is defined in the default launch script (ditto for mono-sgen)

Basically i'm just launching it the same way the init script does, except for the --optimize=all parameter and upping the threads and nursery size in the env variables.

  • Like 1
Link to comment
Share on other sites

  • 1 month later...

Is there an update to this "fix" / optimization (or more likely I'm just a super noob, lol)? I have an Ubuntu 14.04 based install of Emby Server set up (finished setup early October ish) and unfortunately I do not have either of those files to edit as suggested by the OP. I do however know/think that there was some "change" to the way Ubuntu handles services and such (the whole "init vs init.d" thing i think) and so I dug around my init.d folder and found emby-server (this i believe is what is called when I type sudo service emby-server start|restart|stop|etc). I went in to edit that and found mostly variables and stuff i wasnt too sure of, but I found only the Stop and a few other commands but not the Start command, btu i was able to follow the trail to another file in /usr/lib/emby-server/ called emby-server.sh (this appears to be what is called by the init.d script when the "Start" command is given).

In there I found the line: 

env MONO_THREADS_PER_CPU=100 MONO_GC_PARAMS=nursery-size=64m $MONO_EXEC $EMBY_EXEC $EMBY_OPTS > $LOG_FILE 2>&1

 

I got excited and thought, BINGO! I adjusted the values from 100 to 250 and 64 to 256 as suggested. I also added --optimize-all after the $MONO_EXEC variable because I assumed that this would be the variable calling the bin or executable or whatever its called, that matches the one being edited in OPs post). However, after a reboot of my system, the server is not working properly

 

The server will take a start command and give a PID but the stop does not work (it says unknown command). The web interface does not work :(

 

I discovered that I can adjust the THreads and nursery size, reboot and have no issues but I cannot at this point find where to add the --optimize-all tag

 

Ultimately, I am getting errors when trying to watch videos on two of my devices (lower end old laptop and less old desktop) and am looking to find the root cause. My server has 8gb of RAM which i suspect is plenty but my CPU is an Intel Xeon 3050 and is at the low end of that series. I am upgrading to the best Xeon CPU my server mobo will support (xeon 3330 or 3320) in hopes that it will help with the issues but if anyone has any input on my main issue that would be very much appreciated, though im mainly here to figure out how to apply this optimization. Thanks!

Link to comment
Share on other sites

hurricanehrndz

Is there an update to this "fix" / optimization (or more likely I'm just a super noob, lol)? I have an Ubuntu 14.04 based install of Emby Server set up (finished setup early October ish) and unfortunately I do not have either of those files to edit as suggested by the OP. I do however know/think that there was some "change" to the way Ubuntu handles services and such (the whole "init vs init.d" thing i think) and so I dug around my init.d folder and found emby-server (this i believe is what is called when I type sudo service emby-server start|restart|stop|etc). I went in to edit that and found mostly variables and stuff i wasnt too sure of, but I found only the Stop and a few other commands but not the Start command, btu i was able to follow the trail to another file in /usr/lib/emby-server/ called emby-server.sh (this appears to be what is called by the init.d script when the "Start" command is given).

In there I found the line: 

env MONO_THREADS_PER_CPU=100 MONO_GC_PARAMS=nursery-size=64m $MONO_EXEC $EMBY_EXEC $EMBY_OPTS > $LOG_FILE 2>&1

 

I got excited and thought, BINGO! I adjusted the values from 100 to 250 and 64 to 256 as suggested. I also added --optimize-all after the $MONO_EXEC variable because I assumed that this would be the variable calling the bin or executable or whatever its called, that matches the one being edited in OPs post). However, after a reboot of my system, the server is not working properly

 

The server will take a start command and give a PID but the stop does not work (it says unknown command). The web interface does not work :(

 

I discovered that I can adjust the THreads and nursery size, reboot and have no issues but I cannot at this point find where to add the --optimize-all tag

 

Ultimately, I am getting errors when trying to watch videos on two of my devices (lower end old laptop and less old desktop) and am looking to find the root cause. My server has 8gb of RAM which i suspect is plenty but my CPU is an Intel Xeon 3050 and is at the low end of that series. I am upgrading to the best Xeon CPU my server mobo will support (xeon 3330 or 3320) in hopes that it will help with the issues but if anyone has any input on my main issue that would be very much appreciated, though im mainly here to figure out how to apply this optimization. Thanks!

Please view the /etc/default/emby-server file. And add your adjustments to /etc/emby-server.conf. The 14.04 package has the optimize flag enabled.  Below is the default file, please leave as is and make your adjustments in /etc/emby-server.conf.

 27 EMBY_USER="emby"
 28 EMBY_GROUP="emby"
 29 EMBY_DIR="/usr/lib/emby-server"
 30 EMBY_BIN="/usr/lib/emby-server/bin/MediaBrowser.Server.Mono.exe"
 31 EMBY_DATA="/var/lib/emby-server"
 32 EMBY_PIDFILE="/var/run/emby-server.pid"
 33 EMBY_ADD_OPTS=""
 34 MONO_BIN="/usr/bin/mono-sgen"
 35 MONO_OPTS="--optimize=all"
 36 MONO_ENV="MONO_THREADS_PER_CPU=500 MONO_GC_PARAMS=nursery-size=128m"

Edited by hurricanehrndz
Link to comment
Share on other sites

 

Please view the /etc/default/emby-server file. And add your adjustments to /etc/emby-server.conf. The 14.04 package has the optimize flag enabled.  Below is the default file, please leave as is and make your adjustments in /etc/emby-server.conf.

 27 EMBY_USER="emby"
 28 EMBY_GROUP="emby"
 29 EMBY_DIR="/usr/lib/emby-server"
 30 EMBY_BIN="/usr/lib/emby-server/bin/MediaBrowser.Server.Mono.exe"
 31 EMBY_DATA="/var/lib/emby-server"
 32 EMBY_PIDFILE="/var/run/emby-server.pid"
 33 EMBY_ADD_OPTS=""
 34 MONO_BIN="/usr/bin/mono-sgen"
 35 MONO_OPTS="--optimize=all"
 36 MONO_ENV="MONO_THREADS_PER_CPU=500 MONO_GC_PARAMS=nursery-size=128m"

Sadly I do not have an /etc/default/emby-server file and disappointingly I lack a number of the variables you listed from the /etc/emby-server file (emby group, emby dir, emby add opts, mono bin, mono opts, and mono env are all not present. I can assure you I have not knowingly edited these files, I did not know they were there until just now (well the one that actually is there).

 

Anyway, thanks SOOOOOO much for your help, it is massively appreciated. Any and all assistance is so amazing. Thanks again!

Link to comment
Share on other sites

Ops! They will be in the new package soon.

 

oops?! that doesnt sound good? lol. I hope i didnt break anything during my install. Should I be worried? When you say new package....do you mean a new installer for Linux? or like an update for current installs? Will i need to reinstall? I'm really sorry for so many questions. Thanks a bunch for your help btw.

Link to comment
Share on other sites

hurricanehrndz

It just means I gave you info about an upcoming release rather than the current version.

 

Sent from my D6603 using Tapatalk

Link to comment
Share on other sites

MSattler

It just means I gave you info about an upcoming release rather than the current version.

 

Sent from my D6603 using Tapatalk

 

 

With these optimizations in place it would be cool if someone did a comparison of load/response times with Windows/Emby on a server, and then Linux/Emby on a server, just to see which is faster.  From past experience, Windows has always been faster for me, but with these optimizations, and with potentially less overheard in Linux it would be neat to see how it compares.

Link to comment
Share on other sites

With these optimizations in place it would be cool if someone did a comparison of load/response times with Windows/Emby on a server, and then Linux/Emby on a server, just to see which is faster.  From past experience, Windows has always been faster for me, but with these optimizations, and with potentially less overheard in Linux it would be neat to see how it compares.

 

Yeah, it seems like Emby was written for Windows originally, but I really don't want to have to manage a Windows server (or pay for the license for one, even if just a workstation OS version) on my network. I am a Windows guy at my job but prefer to use *nix based "stuff" at home. Currently my whole home network / lab is Linux based (with the exception of my dad's Windows workstation and the Win 7 drive i have in my white box that i rarely boot to).

 

It seems to be working well for me, but I am getting crashes on a few machines via the web browser, so I am hoping to upgrade my server CPU and optimize the installation to get things running well on all my machines.

Link to comment
Share on other sites

  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...