Speed up mpd start on rpi zero w

Need help with MPD?
Post Reply
arne123
Posts: 6
Joined: January 6th, 2021, 8:55 pm

Speed up mpd start on rpi zero w

Post by arne123 »

Dear all,

I am trying to speed up the mpd start on an phonibox installation using mpd on a rpi zero w.
I searched around in the forum, and I already got some improvements:

- disabled auto-update
- compiled actual mpd version on the target (0.23)
- starting network after mpd
- avoid parallel starting of other services in parallel to mpd

The biggest influence I think was the new version, and of course the more or less unique execution (CPU load during mpd start is up to 95%, and its a single core).
so with all that I came down from 26s for mpd to now about 8 sec.
But still 8 sec! (What the hell is mpd doing here, bitcoin mining?)

So my question is, does anybody see some further improvements I can make?
Possibly there are some features I don't need and I can disable them either by configuration, build settings or even code modifications.

I hope this already helps, I made an strace of a manual mpd start:
https://paste.ubuntu.com/p/rbYjzKJThz/
what is obvious an consistence over 3 traces the delay before the last call to clock_gettime, which take between somehow 3!
There are also other call to clock_gettime where you can observe an delay. The pattern is identical between the traces.
Then there are some call to mprotect, with some delay, also identical pattern.

btw, this are the error messages I get:

exception: Default TCP listener setup failed, but this is okay because we have a $XDG_RUNTIME_DIR listener: Failed to bind to '[::]:6600'
exception: Failed to bind socket: Address already in use
exception: Unrecognized tag 'Conductor', discarding database file
exception: Input plugin 'tidal' is unavailable: No Tidal application token configured
exception: Input plugin 'qobuz' is unavailable: No Qobuz app_id configured

tidal and qobuz I disabled in the configuration, as well as port settings (I understood there is an service mpd.socket taking care of this,) anyway, it is working.

Remark:
During compilation I got the warning of an outdated libboost 1.67 which is known as buggy.
As far as I tried upgrading libboost will be nightmare on this target, so I would prefer to deal with the outdated version.

br
Arne
Last edited by arne123 on January 11th, 2021, 10:29 pm, edited 1 time in total.
max
Forum team
Posts: 1248
Joined: January 15th, 2013, 3:43 pm

Re: Speed up mpd sart on rpi zero w

Post by max »

What does the first column in your strace mean?
arne123
Posts: 6
Joined: January 6th, 2021, 8:55 pm

Re: Speed up mpd sart on rpi zero w

Post by arne123 »

it is strace -rT

-r Print a relative timestamp upon entry to each system call.
-T Show the time spent in system calls.

first column r, in <> at the end T
max
Forum team
Posts: 1248
Joined: January 15th, 2013, 3:43 pm

Re: Speed up mpd sart on rpi zero w

Post by max »

So that's redundant and useless. Better use "-tttT".
arne123
Posts: 6
Joined: January 6th, 2021, 8:55 pm

Re: Speed up mpd start on rpi zero w

Post by arne123 »

One Important notice:
I just figured out that that the delay before the last clock_gettime is the time before I hit ctrl+c to stop the trace, so this one is not an issue.

@max,
-rT it is not redundant, T shows the time duration of a syscall, r shows the start between syscall, so r includes mpd code execution as well r -T = mpd-code
-tttT" looks like https://paste.ubuntu.com/p/N6jMSksbk7/
max
Forum team
Posts: 1248
Joined: January 15th, 2013, 3:43 pm

Re: Speed up mpd start on rpi zero w

Post by max »

Your strace shows that MPD startup takes just 200ms. That's pretty good, isn't it?
But prior to that, the dynamic loader takes 8 seconds (!!) to load all shared libraries. This could be optimized by reducing the number of plugins enabled at compile time, or by linking MPD statically. (Maybe prelinking could help.)
Anyway, the slowness has nothing to do with MPD, it's not specific to MPD, so MPD-specific optimizations won't help.
arne123
Posts: 6
Joined: January 6th, 2021, 8:55 pm

Re: Speed up mpd start on rpi zero w

Post by arne123 »

thank you very much for your help in interpretation of the strace and your recommendations.

I disabled a lot of options which I think I don't need and recompiled, this already saved 2.3s (8s to 5.7 s)
I am not familar with meson, so I changed the defaults in the meson_options.txt (is there a smarter way (except cmdline -D? )
https://paste.ubuntu.com/p/q6QcjYRQJJ/

now the strace looks like:
https://paste.ubuntu.com/p/GYMQH6X85m/

there are still a lot of shared libraries loaded, do you see further improvements possible?
Basically I just need mp3 from local file system, playlists, tcp control, alsa,

I could not not figure out how I can do static linking, do I just need to add or change some options in the jeson build file?

br
Arne
max
Forum team
Posts: 1248
Joined: January 15th, 2013, 3:43 pm

Re: Speed up mpd start on rpi zero w

Post by max »

It needs 0.4 seconds just to find one single shared library. Your strace suggests that your library search path is extremely long. Did you do any special hacks to make it that long? For example, set LD_LIBRARY_PATH?
arne123
Posts: 6
Joined: January 6th, 2021, 8:55 pm

Re: Speed up mpd start on rpi zero w

Post by arne123 »

Thanks again for your hints,

I figured out that the mpd runpath was messed up, so with
(readelf -d ./mpd | grep runpath)

patchelf --set-rpath '/lib/arm-linux-gnueabihf/' mpd

now most shared libraries are found on the first try.
(can be verifyd with $ LD_DEBUG=libs ldd mpd)

the strace also looks much cleaner, but there was no improvement in boot time for this.
https://paste.ubuntu.com/p/X3689KKzNR/

for most libraries I think are the static version on the system (.a) as well, I will give it a try to link them static

br
Arne
max
Forum team
Posts: 1248
Joined: January 15th, 2013, 3:43 pm

Re: Speed up mpd start on rpi zero w

Post by max »

This has reduced the startup time by 2 seconds, that's a lot!

To reduce this further, you need to reduce the number of library dependencies further. For example, it loads libharfbuzz, which is a text layout library, but MPD doesn't render any text ... this is probably pulled in by FFmpeg. You can either rebuild FFmpeg without this dependency, or build MPD without FFmpeg. MPD doesn't need FFmpeg - for most codecs, there are better native libraries. Only some exotic codecs need FFmpeg.
arne123
Posts: 6
Joined: January 6th, 2021, 8:55 pm

Re: Speed up mpd start on rpi zero w

Post by arne123 »

Great!

I removed further encoder plugins, now I am at 1.7s at boot (from systemd, not considering the strace)
I have not been entirely sure what encoders I need, therefore I was careful while removing.

strace is now like:
https://paste.ubuntu.com/p/MY2G544z33/

ldd is now:
https://paste.ubuntu.com/p/4nFvrytcBp/

before it was:
https://paste.ubuntu.com/p/scjJqrt8fj/
(the original I don't have)

anyway, great success so far.

Can you tell me until which line in the strace you can see that the startup has finished?

I trid to configure static bulild, so far with no success,
also to configure the runpath at buildtime wasn't successful.

meson . out --buildtype=debugoptimized -Db_ndebug=true -Dsndfile=disabled -Dffmpeg=disabled -Dfaad=disabled -Daudiofile=disabled -Ddefault_library=static -Dinstall_rpath=/lib/arm-linux-gnueabihf/ --reconfigure
any hint here?

after strip the binary now is just 660k
max
Forum team
Posts: 1248
Joined: January 15th, 2013, 3:43 pm

Re: Speed up mpd start on rpi zero w

Post by max »

The line where MPD opens its configuration file:

openat(AT_FDCWD, "/etc/mpd.conf", O_RDONLY|O_NOCTTY|O_LARGEFILE|O_CLOEXEC) = 3

.. is the point where MPD takes over control. Before that, no MPD code runs, it's all the ld.so loading shared libraries.

The static build is mostly about the libraries MPD uses, not MPD itself (MPD itself is only the final piece, but that works only if everything else is static already). So if you're serious about that, recompile all or most of the required libraries as static libraries. There will be many serious problems with that, so do that only if the remaining MPD startup time is still too painful for you. But 1.7s sounds pretty good already for a device with such an extremely slow storage.
Post Reply