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 Firefly Media Server to serve music to my Roku Soundbridge and Mediatomb for videos.

The Media Server Update Problem

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 inotify, and it seems to work quite well. But inotify is a Linux-only API and it is therefore not portable.

A Simple Media Server Update Solution

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 open(2) 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.

Some Useful DTrace Principles

While putting together this DTrace script, the following things came up which are probably useful to know for a variety of similar D scripts:

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(2) verbatim without knowing their actual values.

To match the media directory with the path given to open(2), 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 User Process Tracing chapter of the DTrace documentation.

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 same chapter of the DTrace docs.

When trying to match the arguments to open(2) 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 DTrace predicates 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() 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 -w flag to dtrace(1M) which enables destructive actions.

Looks like this script is doing its thing. Now I'm waiting for my next load of CDs to arrive so I can rip and rsync them into my auto-updating OpenSolaris home media server!

Conclusion

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 documentation and are not afraid to read the occasional line of OpenSolaris source code.

Other Useful DTrace Home Server Scripts

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:

Tiny Thinking

Copyright (C) 2010 Constantin Gonzalez, some rights reserved.
This site uses Piwik, Google Analytics, Feedburner, Amazon Affiliate and possibly other web tracking code. This site also uses Facebook "Like", Flattr and other plugins that can send private data to their websites. See Imprint for details and our information policy.
By using this site you agree to not hold the author responsible for anything related to this site.