One of the most typical uses for a home server is to serve music or videos to home entertainment equipment. In my case, I'm using the [URL="http://fireflymediaserver.org/"]Firefly Media Server[/URL] to serve music to my Roku Soundbridge and [URL="http://mediatomb.cc/"]Mediatomb[/URL] for videos.

[B]The Media Server Update Problem[/B]

Whenever I upload new music or videos to my OpenSolaris home server (typically by rsync-ing my laptop home directory), both Firefly and Mediatomb need to be restarted so they detect that new files are sitting in their directories, waiting to be served.

The typical way to solve this in the Linux world is to use [URL="http://en.wikipedia.org/wiki/Inotify"]inotify[/URL], and it seems to work quite well. But inotify is a Linux-only API and it is therefore not portable.

The OpenSolaris way to detect changes in the file system is through file notification events. [URL="http://blogs.sun.com/darren/entry/file_notification_in_opensolaris_and"]Darren Moffat has written a nice tutorial on how to use file notification events in your code[/URL]. But this code has not been adopted by neither Firefly nor Mediatomb (yet?).

[B]A Simple Media Server Update Solution[/B]

There's another way, more simple than coding file notification events and easy to use for any kind of media server software: DTrace.

In the following script, we'll ask DTrace to watch for [URL="http://docs.sun.com/app/docs/doc/819-2241/open-2?a=view"]open&#40;2&#41;[/URL] events and filter out those that match the directories where we store our media. Then, when new files come in or are changed we'll send SIGHUP signals to our media server processes so they can re-scan their content directories.

[B]Some Useful DTrace Principles[/B]

While putting together this DTrace script, the following things came up which are probably useful to know for a variety of similar D scripts:
[LIST=1][*]DTrace already knows about the standard libc and kernel functions, so, when watching out for writes, we can just specify the O_RDWRITE and O_WRONLY flags to open&#40;2&#41; verbatim without knowing their actual values.[*]To match the media directory with the path given to open&#40;2&#41;, we need to look at arg0, which is a pointer to the given file name. But the file name resides in user space while DTrace operates in kernel space. Therefore, we need to use the DTrace copyin function to copy the file name across memory spaces.
This is a common DTrace technique described in the [URL="http://wikis.sun.com/display/DTrace/User+Process+Tracing"][I]User Process Tracing[/I] chapter of the DTrace documentation[/URL].[*]Another common thing to keep in mind: At the moment of function entry, the file name may reside on a memory page that is not yet accessible, so we can't necessarily read it yet.
We therefore capture the value of arg0 at function entry time, wait until the function has completed, then read out the file name which will then be accessible for sure. Again, this is described in the [URL="http://wikis.sun.com/display/DTrace/User+Process+Tracing"]same chapter of the DTrace docs[/URL].[*]When trying to match the arguments to open&#40;2&#41; with the media directory we want to watch, we need to test for two cases: 1) arg0 contains the full path, or 2) arg0 is relative to the current working directory. In the latter case, we need to construct the full path, then test against it.[*]If we add half a dozen CDs and 100 songs to our media library, we don't want to force a re-scan 100 times. We'll therefore use a flag that indicates that we have detected some new files, then test against the flag periodically (say, every minute or so) and only then signal the media server process to re-scan.[*]In order to make the burden on the system as light as possible, we'll try to do as much of the testing as possible inside of [URL="http://wikis.sun.com/display/DTrace/D+Program+Structure"]DTrace predicates[/URL] and only use action statements where necessary. Also, DTrace supports short-circuiting of logical AND tests, so we'll place the most likely or common tests at the beginning of our predicates.[*]We'll use the DTrace system&#40;&#41; action to signal our media server processes. This is a destructive action (well, not really, but in DTrace parlance...) which means we'll need to use the [URL="http://wikis.sun.com/display/DTrace/dtrace%281M%29+Utility"]-w flag to dtrace&#40;1M&#41;[/URL] which enables destructive actions.[/LIST][B]The Automatic Re-Scan DTrace Script[/B]

DTrace can be an amazing help for any system administrator, even on a home server. In this case, we've fixed a common problem that other OSes would need a whole new API (like inotify) to address this problem with!

Learning DTrace isn't that hard if you look at existing D scripts, check out the [URL="http://wikis.sun.com/display/DTrace/Documentation"]documentation[/URL] and are not afraid to read the occasional line of OpenSolaris source code.

[B]Other Useful DTrace Home Server Scripts[/B]

This is just the beginning. In fact, if you look around, there's a wealth of opportunities to leverage DTrace in your day to day work, even in your home server. Here are a few famous examples:
[LIST][*][URL="http://www.brendangregg.com/DTrace/opensnoop"]Brendan Gregg's opensnoop is a more sophisticated version of the above[/URL], without the system&#40;&#41; part, though. Make sure to check his other [URL="http://brendangregg.com/dtrace.html#DTraceToolkit"]DTrace tools[/URL] as well![*][URL="http://chrisgerhard.wordpress.com/tag/dtrace/"]Chris Gerhard has a wealth of DTrace related articles[/URL], just look for the dtrace tag. You may also prefer browsing [URL="http://blogs.sun.com/chrisg/"]his old blog[/URL], as some of the scripts didn't survive the migration to Wordpress very well.
My favourites are "Snapshot on unlink?" and "[URL="http://blogs.sun.com/chrisg/entry/monitoring_mounts"]Monitoring mounts[/URL]".[*]For my German speaking readers: [URL="http://raichoo.blogspot.com/search/label/Dtrace"]Raichoo has written a wealth of useful DTrace articles in German language in his blog.[/URL] Great stuff![/LIST][B]Your Turn[/B]

But that's not enough. Googling "DTrace home server" doesn't reveal much at the moment. So now it's your turn:
[LIST][*]If you haven't played with DTrace start exploring it now![*]Did you write yourself some useful D scripts already? Blog them! Or share them in the comments below![*]What other situations can you imagine where DTrace would be helpful to you home server?[/LIST]As always, feel free to share your experiences and comments below.

[url=http://feedproxy.google.com/~r/ConstantThinking/~3/IYyuNTVA0IQ/how-automatically-update-your-home-media-server-library-dtrace]Read More about [How To Automatically Update Your Home Media Server Library With DTrace...[/url]