aboutsummaryrefslogtreecommitdiffstats
path: root/src/notify.c
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2008-03-26 10:38:54 +0000
committerEric Wong <normalperson@yhbt.net>2008-03-26 10:38:54 +0000
commitbf05ce161fb0ace60aa621b6e5dae00b49729b29 (patch)
tree449d600e4c9e030b1c53753aa31f33f933f63580 /src/notify.c
parent1d97bbbdd9d6015246b6b894dcc41209e7cce369 (diff)
downloadmpd-bf05ce161fb0ace60aa621b6e5dae00b49729b29.tar.gz
mpd-bf05ce161fb0ace60aa621b6e5dae00b49729b29.tar.xz
mpd-bf05ce161fb0ace60aa621b6e5dae00b49729b29.zip
notify the decoder instead of polling 100hz
When the decoder process is faster than the player process, all decodedd buffers are full at some point in time. The decoder has to wait for buffers to become free (finished playing). It used to do this by polling the buffer status 100 times a second. This generates a lot of unnecessary CPU wakeups. This patch adds a way for the player process to notify the decoder process that it may continue its work. We could use pthread_cond for that, unfortunately inter-process mutexes/conds are not supported by some kernels (Linux), so we cannot use this light-weight method until mpd moves to using threads instead of processes. The other method would be semaphores, which historically are global resources with a unique name; this historic API is cumbersome, and I wanted to avoid it. I came up with a quite naive solution for now: I create an anonymous pipe with pipe(), and the decoder process reads on that pipe. Until the player process sends data on it as a signal, the decoder process blocks. This can be optimized in a number of ways: - if the decoder process is still working (instead of waiting for buffers), we could save the write() system call, since there is nobody waiting for the notification. [ew: I tried this using a counter in shared memory, didn't help] - the pipe buffer will be full at some point, when the decoder thread is too slow. For this reason, the writer side of the pipe is non-blocking, and mpd can ignore the resulting EWOULDBLOCK. - since we have shared memory, we could check whether somebody is actually waiting without a context switch, and we could just not write the notification byte. [ew: tried same method/result as first point above] - if there is already a notification in the pipe, we could also not write another one. [ew: tried same method/result as first/third points above] - the decoder will only consume 64 bytes at a time. If the pipe buffer is full, this will result in a lot of read() invocations. This does not hurt badly, but on a heavily loaded system, this might add a little bit more load. The preceding optimizations however are able eliminate the this. - finally, we should use another method for inter process notifications - maybe kill() or just make mpd use threads, finally. In spite of all these possibilities to optimize this code further, this pipe notification trick is faster than the 100 Hz poll. On my machine, it reduced the number of wakeups to less than 30%. git-svn-id: https://svn.musicpd.org/mpd/trunk@7215 09075e82-0dd4-0310-85a5-a0d7c8717e4f
Diffstat (limited to 'src/notify.c')
-rw-r--r--src/notify.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/notify.c b/src/notify.c
new file mode 100644
index 000000000..85db46192
--- /dev/null
+++ b/src/notify.c
@@ -0,0 +1,70 @@
+/* the Music Player Daemon (MPD)
+ * Copyright (C) 2008 Max Kellermann <max@duempel.org>
+ * This project's homepage is: http://www.musicpd.org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include "notify.h"
+
+#include <assert.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+int set_nonblock(int fd)
+{
+ int ret;
+
+ assert(fd >= 0);
+
+ ret = fcntl(fd, F_GETFL, 0);
+ if (ret < 0)
+ return ret;
+
+ return fcntl(fd, F_SETFL, ret|O_NONBLOCK);
+}
+
+int initNotify(Notify *notify)
+{
+ int ret;
+
+ ret = pipe(notify->fds);
+ if (ret < 0)
+ return -1;
+
+ ret = set_nonblock(notify->fds[1]);
+ if (ret < 0) {
+ close(notify->fds[0]);
+ close(notify->fds[1]);
+ return -1;
+ }
+
+ return 0;
+}
+
+int waitNotify(Notify *notify)
+{
+ char buffer[64];
+ ssize_t nbytes;
+
+ nbytes = read(notify->fds[0], buffer, sizeof(buffer));
+ return (int)nbytes;
+}
+
+int signalNotify(Notify *notify)
+{
+ char buffer[1] = { 0 };
+
+ return (int)write(notify->fds[1], &buffer, sizeof(buffer));
+}