Archive for the ‘Operating Systems’ Category

Monstrous polymorphism and a Python post-import hook decorator

Wednesday, April 15th, 2009

I queue up a few thousand things to do before I get on an airplane: synchronize two-thousand Google Reader entries, load up a bunch of websites I’ve been meaning to read, and make sure for-fun projects are pulled from their most updated branches.

Then, once I get up in the air, I realize that I don’t really want to do 90% of those things crammed into a seat with no elbow room. I end up doing one or two. Along with reading Stevey’s Drunken Blog Rant: When Polymorphism Fails, this entry is all the productivity I can claim. The full code repository for this entry is online if you’d like to follow along.

Polymorphism Recap

The word “polymorphic” comes from Greek roots meaning “many shaped.” (Or they lied to me in school — one of those.) From a worldly perspective I can see this meaning two things:

  • A single object can take on many shapes, or
  • Requirements for a general “shape” can be satisfied by different categories of objects.

As it turns out, both of these concepts apply to the Object-Oriented programming, but the canonical meaning is the latter. [*] As Yegge says:

If you have a bunch of similar objects [...], and they’re all supposed to respond differently to some situation, then you add a virtual method to them and implement it differently for each object.

(If you don’t know what a virtual method is, the Wikipedia page has an alternate explanation.)

Yegge’s Example

Yegge demonstrates that strictly adhering to the principles of polymorphism does not always produce the best design:

Let’s say you’ve got a big installed base of monsters. [...] Now let’s say one of your users wants to come in and write a little OpinionatedElf monster. [...] Let’s say the OpinionatedElf’s sole purpose in life is to proclaim whether it likes other monsters or not. It sits on your shoulder, and whenever you run into, say, an Orc, it screams bloodthirstily: “I hate Orcs!!! Aaaaaargh!!!” (This, incidentally, is how I feel about C++.)

The polymorphic approach to this problem is simple: go through every one of your 150 monsters and add a doesMrOpinionatedElfHateYou() method.

This is a great counterexample — it induces an instant recognition of absurdity.

He then touches on the fact that dynamic languages allow you to do neat things consistent with polymorphism due to the flexibility of the object structure (which is typically just a hash map from identifiers to arbitrary object values):

I guess if you could somehow enumerate all the classes in the system, and check if they derive from Monster, then you could do this whole thing in a few lines of code. In Ruby, I bet you can… but only for the already-loaded classes. It doesn’t work for classes still sitting on disk! You could solve that, but then there’s the network…

This is clearly impractical, but I figured there was some exploratory value to implementing this challenge in Python. This entry is a small walk-through for code to detect interface conformity by inspection, enumerate the classes in the environment, manipulate classes in place, and add an import hook to manipulate classes loaded from future modules.

The Antagonist

Double entendre intended. :-)

class OpinionatedElf(object):
 
    is_liked_by_class_name = {
        'OpinionatedElf': True,
        'Orc': False,
        'Troll': False}
 
    def __init__(self, name):
        self.name = name
 
    def be_scary(self):
        print("I'm small, ugly, and don't like the cut of your jib!")
 
    def proclaim_penchance(self, other):
        if not IMonster.is_conforming(other):
            print("I can't even tell what that is!")
            return
        is_liked = other.is_liked_by_elf()
        class_name = other.__class__.__name__
        if is_liked is None:
            print("I'm not sure how I feel about %s" % class_name)
            return
        emotion = 'love' if is_liked else 'hate'
        print('I %s %s!!! Aaaaaargh!!!' % (emotion, other.__class__.__name__))

Determining which Classes are Monsters

First of all, Python doesn’t require (nor does it encourage) a rigid type hierarchy. Python’s all about the interfaces, which are often implicit. Step one is to create a way to recognize classes that implement the monster interface: [%]

class IMonster(object):
 
    required_methods = ['be_scary']
 
    def be_scary(self):
        raise NotImplementedError
 
    @classmethod
    def is_conforming(cls, object):
        result = all(callable(getattr(object, attr_name, None))
            for attr_name in cls.required_methods)
        logging.debug('%s conforms? %s', object, result)
        return result
 
assert IMonster.is_conforming(IMonster)

This is a simple little class — there are better third party libraries to use if you want real interface functionality (i.e. more generic conformity testing and Design By Contract).

Enumerating the Classes in the Environment

All of the modules that have been loaded into the Python environment are placed into sys.modules. By inspecting each of these modules, we can manipulate the classes contained inside if they conform to our monster interface.

for name, module in sys.modules.iteritems():
    extend_monsters(module)

The extend_monsters function is a bit nuanced because immutable modules also live in sys.modules. We skip those, along with abstract base classes, which have trouble with inspect.getmembers():

def extend_monsters(module, extension_tag='_opinionated_extended'):
    """Extend monsters in the module's top-level namespace to
    tell if they are liked by the :class:`OpinionatedElf`.
    and tag it with the :param:`extension_tag` as a flag name.
    Do not attempt to extend already-flagged modules.
    Do not clobber existing methods with the extension method name.
 
    Warning: swallows exceptional cases where :param:`module`
        is builtin, frozen, or None.
    """ 
    name = module.__name__ if module else None
    logging.info('Instrumenting module %s', name)
    if not module or imp.is_builtin(name) or imp.is_frozen(name) \
            or getattr(module, extension_tag, False):
        logging.info('Skipping module: %s', name)
        return
    module._opinionated_instrumented = True
    classes = inspect.getmembers(module, inspect.isclass)
    for name, cls in classes:
        logging.debug('%s: %s', name, cls)
        try:
            conforming = IMonster.is_conforming(cls)
        except AttributeError, e:
            if '__abstractmethods__' in str(e): # Abstract class.
                continue
            raise
        if not conforming:
            continue
        class_name = cls.__name__
        logging.debug('Instrumenting class %s', class_name)
        attr_name = 'is_liked_by_elf'
        if hasattr(cls, attr_name): # Don't clobber existing methods.
            logging.warn('Method already exists: %s', cls)
            continue
        logging.info('Setting %s on %s', attr_name, class_name)
        setattr(cls, attr_name,
            lambda self: OpinionatedElf.is_liked_by_class_name.get(
                self.__class__.__name__, None))

If we were going to be thorough, we would recurse on the members of the class to see if the class scope was enclosing any more IMonster classes, but you’re never really going to find them all: if a module defines a monster class in a function-local scope, there’s no good way to get the local class statement and modify it through inspection.

In any case, we’re at the point where we can modify all monsters in the top-level namespace of already-loaded modules. What about modules that we have yet to load?

Post-import Hook

There is no standard post-import hook (that I know of) in Python. PEP 369 looks promising, but I couldn’t find any record of additional work being done on it. The current import hooks, described in PEP 302, are all pre-import hooks. As such, you have to decorate the __import__ builtin, wrapping the original with your intended post-input functionality, like so: [^]

def import_decorator(old_import, post_processor):
    """
    :param old_import: The import function to decorate, most likely
        ``__builtin__.__import__``.
    :param post_processor: Function of the form
        `post_processor(module) -> module`.
    :return: A new import function, most likely to be assigned to
        ``__builtin__.__import__``.
    """
    assert all(callable(fun) for fun in (old_import, post_processor))
    def new_import(*args, **kwargs):
        module = old_import(*args, **kwargs)
        return post_processor(module)
    return new_import

After which we can replace the old __import__ with its decorated counterpart:

__builtin__.__import__ = import_decorator(__builtin__.__import__,
    extend_monsters)

The Network

Yegge brings up the issue of dynamically generated classes by mentioning network communications, calling to mind examples such as Java’s RMI and CORBA. This is a scary place to go, even just conceptualizing. If metaclasses are used, I don’t see any difficulty in decorating __new__ with the same kind of inspection we employed above; however, code generation presents potentially insurmountable problems.

Decorating the eval family of functions to modify new classes created seems possible, but it would be challenging and requires additional research on my part. exec is a keyword/statement, which I would think is a hopeless cause.

Footnotes

[*] In accordance with the former, an object can implement many interfaces.
[^] This function is actually a generic decorate-with-post-processing closure, but I added the references to import for more explicit documentation.

Thoughts on desktop Linux incompatibilities with iPhone and Android

Friday, September 19th, 2008

Linux users want music-player/phone integration. Linux users want to sync all of their data — contacts, emails, calendars, bookmarks, documents, ebooks, music, photos, videos — at the touch of a button. Linux users want 3G data rates. Linux users want a state of the art, coordinated mobile platform.

If FLOSS developers are so prone to scratching their own itches, why doesn’t there exist such a thing?

Because large scale mobile device companies box us out.

The iPhone Platform

I believe that Linux users who purchase their iPhone with the intent of jailbreaking it to fake compatibility are doing the Linux community a great disservice. They are purchasing a device which is made with the intent of not working with your computer. There’s no more mass storage device. There’s no longer a known iTunesDB format. The iPhone goes so far to obscure our intended usage that the community-recommend method of gaining functionality was to use an arbitrary code execution exploit. This is what we’re driven to do. Do you want to support this behavior with your $200-500?

From a technological standpoint, our historical success at reverse engineering is very cool. It demonstrates the community’s technical prowess through our ability to overcome artificial barriers. Despite the coolness factor, however, we can not and should not rely on our ability to kluge around obstacles in our path. Why? Because it doesn’t allow us to make any definitive progress. It constantly puts us several steps behind the capabilities of a “properly” functioning device, both due to the difficulty of finding a solution and the misdirection of creative energy. One can’t reasonably expect to build a working, Linux-compatible platform on top of a series of hacks that could potentially break with any minor release.

Even more insulting is the message that alternative solutions that work within the system are unwelcome. In my mind, the rallying cry of the Linux community should be “iPhone != iTunes“. Ideally, the community could write an iTunes replacement application that played Ogg Vorbis and FLAC files. Let’s enumerate some problems that this would solve for FLOSS developers and enthusiasts:

  1. We wouldn’t have to reverse engineer the new iTunesDB format (or anything having to do with iTunes).
  2. We wouldn’t have to reverse engineer the new iPhone USB protocol.
  3. We would be starting a platform with a solid base that we could build upon. We would no longer be at the mercy of a development shop that clearly doesn’t care about our demographic.
  4. We could have it connect to a small socket server on our local machines and automatically sync music over WiFi.
  5. We could play Ogg Vorbis files, for God’s sake!

We could write a whole suite of totally legitimate applications for the iPhone to perform compatible iPhone-native-application-like functionality, all within the artificial constraints of the iPhone! There’s nothing stopping us — except for the distribution mechanism. If Apple is at all amenable to our cause, the rejection of competitive apps will have to stop. Again: we should not have to void our warranties to use our product in legitimate ways on our competitive computing platforms.

Sadly, even if iTunes-store enlightenment came to fruition, we’d still be screwed. Platform restrictions disallow several key abilities. Case in point, we could not background our iTunes-replacement music player while we browsed the web (or did anything else, for that matter). We find ourselves at the mercy of the exposed API and Human Interface restrictions. Although this is unfortunate, it’s decidedly better than founding a platform on our ability to hack around the poor design decisions of others.

The Android Platform

I’m much less well informed about the Android platform and the upcoming HTC Dream mobile device. Nobody is well informed at this point — almost exactly one month from the expected release date — much to the chagrin of potential customers. There are early indications that Linux desktop compatibility will not be supported natively on this platform either. As a Linux user, I can only cross my fingers and hope that Android will be as open as Google makes it out to be, while keeping a close watch on the potentially hazardous centralized distribution model.

Food For Thought

Since this article is supposed to contain my “thoughts on” the subject, I feel I should also share this little tidbit that keeps rattling around in my head. I’m not drawing any conclusions, just providing the reader with another, incomplete step in my thought process.

Monopoly law exists, in part, to disallow certain practices that are thought to be detrimental to “consumer welfare”. From Wikipedia (emphasis added):

Competition law does not make merely having a monopoly illegal, but rather abusing the power that a monopoly may confer, for instance through exclusionary practices.

Update: September 20, 2008

An application named MailWrangler was also barred from the Apple Store for vaguely duplicating the functionality of Mail.app. From Angelo DiNardi’s article (same link as above):

Normally to check multiple Gmail accounts in mobile Safari you would have to log in and out of all of the accounts, typing the username and password for each. Using just the Apple Mail application you aren’t able to see threaded views, your google contacts, archive (quickly), star, etc without going through the hassles that are present when using Gmail’s IMAP on the iPhone.

This is another case of barring an application that offers features for a smaller demographic. I personally can’t see why Apple is so “afraid” — let third party apps spring up for specialized features, so long as they don’t violate the device’s terms of use. If you feel like incorporating those features into Mail.app somewhere down the road, the other applications will die out naturally.

I feel sincere sympathy for Angelo; however, on the desktop Linux side we’re at an even greater disadvantage — for us, there isn’t even similar functionality available on the iPhone platform. To just sync our music, we have to void our warranties. The only thing we can possibly do without voiding our warranties is write an app with similar functionality to the iTunes music player and acquire it through the Apple Store. Forbidding us from doing this makes legitimate desktop Linux use impossible — for what advantage?

State of the X-Window

Wednesday, February 13th, 2008

I just watched a presentation on X.org’s history, architectural overview, and recent developments, entitled State of the X-Window. If you use an operating system that runs X, I recommend you check it out.

The thing that I found most personally relevant is that there is currently a bug which prevents framebuffer re-allocation. As a result, the framebuffer size is fixed until the X-server is restarted!

This has always been a pain for me, since I can hotplug my external monitor into my laptop flawlessly, but can’t switch out of clone to dual head without restarting X (since that would require a framebuffer size increase). The server developer promises that it’ll be fixed in the next few months — I’ll certainly be excited to have that working!

procfs and preload

Thursday, November 15th, 2007

Two of the cool utilities that I’ve checked out lately have centered around /proc. /proc is a virtual filesystem mountpoint — the filesystem entities are generated on the fly by the kernel. The filesystem entities provide information about the kernel state and, consequently, the currently running processes. [*]

The utilities are preload and powertop. Both are written in C, though I think that either of them could be written more clearly in Python.

preload

Preload’s premise is fascinating. Each shared library that a running process is using via MMIO can be queried via /proc/[pid]/maps, which contains entries of the form:

[vm_start_addr]-[vm_end_addr] [perms] [file_offset] [device_major_id]:[device_minor_id] [inode_num] [file_path]

Preload uses a Markov chain to decide which shared library pages to “pre-load” into the page cache by reading and analyzing these maps over time. Preload’s primary goal was to reduce login times by pre-emptively warming up a cold page cache, which it was successful in doing. The catch is that running preload was shown to decrease performance once the cache was warmed up, indicating that it may have just gotten in the way of the native Linux page cache prefetch algorithm. [$]

There are a few other things in /proc that preload uses, like /proc/meminfo, but querying the maps is the meat and potatoes. I was thinking of porting it to Python so that I could understand the structure of the program better, but the fact that the daemon caused a performance decrease over a warm cache turned me off the idea.

Footnotes:

[*] A cool side note — all files in /proc have a file size of 0 except kcore and self.
[$] The page_cache_readahead() function in the Linux kernel.

References:

I look less leet around campus because I dual boot Vista

Tuesday, November 13th, 2007

A few weeks back I got a brand new X60 tablet… to replace my relatively new X41 tablet. What can I say? I’m a sucker for high resolution screens… Lots of code and documentation needs to be viewed in parallel, and the X60 has a beautiful SXGA+ (1400×1050) configuration on its little 12″ display (let’s just say it’s a good thing I’m near-sighted). [0] I’m planning on giving the X41 tablet to my sister for the start of her college career, since it’s fully functional, in good condition, and sells for under $800 on ebay.

The Operating Systems Practicum that I’m taking (CS415, my CS project course) forces us to use Microsoft Visual Studio by having a huge, ugly, Windows API-based codebase. I tried to port the first project to make it POSIX compliant, but I couldn’t figure out how to manipulate the stack pointer in gcc assembly — this was necessary to port their user-level threading library implementation. The whole mess was terribly x86 dependent to begin with.

As a result, I need to use Microsoft Visual Studio this semester, and so I decided that I would just squeeze the NTFS partition that Lenovo graciously provided to me, as opposed to obliterating it the first chance that I got. [1] I now dual boot, and many people believe that my leetness level has dropped considerably. To compensate, I did a:

% sudo apt-get install compizconfig-settings-manager

and turned my minimize effect to:

Burn @ 200ms @ (type=Normal|Dialog|ModalDialog|Utility|Unknown)

Which made all my windows go up in flames. Of course, this made me totally leet… for about five minutes, at which point I turned compiz off because I didn’t want Xorg using up unnecessary resources. [2]

[0] My one gripe is that it shipped with a single pixel that’s stuck bright green — single pixels aren’t covered under Lenovo’s pixel replacement policy. Argh!
[1] Not-so-graciously charging me the Windows Tax for an OS that I can get for free through MSDNAA.

[2] More evidence that I’ll probably never be a Mac OS user.