thread profiling in Python

Python has accumulated a lot of… character over the years.  We’ve got no less then 3 profiling libraries for single threaded execution and a multi-threaded profiler with an incompatible interface (Yappi).  Since many applications use more then one thread, this can be a bit annoying.

Yappi works most of the time.  Except, sometimes it doesn’t, and randomly causes your application to hang.  (I blame signals, personally). The other issue is that Yappi doesn’t have a way of collecting call-stack information. (I don’t necessarily care that memcpy takes all of the time, I want to know who called memcpy). In particular, the lovely gprof2dot can take in pstats dumps and output a very nice profile graph.

To address this for my uses, I glom together cProfile runs from multiple threads. In case it might be useful for other people I wrote a quick gist illustrating how to do it. To make it easy to drop in, I monkey-patch the Thread.run method, but you can use a more maintainable approach if you like (I create a subclass ProfileThread in my applications).

Posted in Uncategorized | Leave a comment

Swig+Directors = Subclassing from Python!

Swig is a fabulous tool — I generally rely on it to extricate myself from the holes I’ve managed to dig myself into using C++.  Swig parses C++ code and generates wrappers for a whole bunch of target languages — I normally use it to build Python interfaces to my C++ code.

A cool feature that I’ve never made use of before is “directors” — these let you write subclasses for your C++ code in Python/(whatever language use desire).  In particular, this provides a relatively easy mechanism for writing callbacks using Python.  Here’s a quick example:

// rpc.h
class RPCHandler {
public:
void fire(const Request&, Response*) = 0;
}

class RPC {
public:
void register_handler(const std::string& name, RPCHandler*);
};

Normally, I’d make a subclass of RPCHandler in C++ and register it with my RPC server. But with SWIG, I can actually write this using Python:

class MyHandler(wrap.RPCHandler):
  def fire(req, resp):
    resp.write('Hello world!')

It’s relatively straightforward to setup. I write an interface file describing my application:

// wrap.swig
// Our output module will be called 'wrap'; enable director support.
%module(directors="1") wrap
%feature("director") RPCHandler;

// Generate wrappers for our RPC code
%include "rpc.h"

// When compiling the wrapper code, include our original header.
%{
#include "rpc.h"
%}

That’s it! Now we can run swig: swig -c++ -python -O -o wrap.cc wrap.swig

Swig will generate wrap.cc (which we compile and link into our application), and a wrap.py file, which we can use from Python.

Posted in Uncategorized | Leave a comment

ah, latex

I thought I wanted to customize the layout of my document a little bit, so I started looking at the various styles that are available.

Then I came upon the memoir package (which is supposed to help with these things); the 550 page manual that comes with it has so effectively scared the crap out of me that I’m now looking at my crappy looking document and thinking: “heck, it looks good enough”.

I will gladly leave typography to the typographers.

Posted in Uncategorized | Leave a comment

vim

I’m a frequent (some might say avid) user of the Vim text editor. I’ve been using it off and on for the past 15 years, and it’s frequently saved me quite a bit of time with the handy macro system.

Now, if you’ve ever used Vim before you’re probably familiar with this intro screen:

vim

Somehow, amazingly, and I’m sure like everyone else, I had managed to go on for all this time without really ever typing :help uganda. Last night, while installing Vim on my new laptop, I finally did. I saw Bram’s visit report. I was really touched at how much they were doing with relatively limited resources. And so I finally made a small donation to ICCF; I even got a personal email from Bram in reply, which was nice.

Anyway, if you’re a Vim user, I encourage you to do the same.

Posted in Uncategorized | Leave a comment

kde desktop magic

Something miraculous that I just realized: middle clicking (aka pasting the X buffer) on the KDE desktop creates a “post-it” note with the contents of the clipboard.

Whether I will ever make use of this feature is unclear but it’s definitely the “right” behavior and a nice thought on the developers part.

Posted in Uncategorized | Leave a comment

Big Gummy Bears

Normally, when confronted with (inevitably weird and annoying) YouTube commercials, I’m hovering over the “Skip Ad” link, waiting for it to be enabled.

But today, I saw an advertisement so odd, that I was forced to watch through the whole thing just to figure out if it was a parody. It wasn’t. Congratulations Vat19, on a successful commercial – if I’m ever in the market for giant gummy bears, I’ll come your way.

Who thinks up these things?

Posted in Uncategorized | Leave a comment

Fiber to the people

My home internet sucks, relatively speaking. Anytime something shows up as “HD”, I know that it’s not going to work out for me. This is not at all surprising, given that I only have one choice (Time Warner) and they continually send me advertisements offering to spend $100 a month in order to get the bandwidth I’m supposed to get for $50. The sad thing is that if you look at the wireless routers visible from my apartment (> 50), everyone in the building (and nearby buildings) has the same problem. If only we could just share one good connection, we’d all be so much happier.

So here’s what I think should happen.

Apartment buildings should have fiber run to the building, run ethernet/wireless to each floor, and charge $40 a month to access it. Why? Because they’d make money off of it, that’s why. After the initial cost to run the fiber, they could contract with Cogent/Level 3/ATT to provide transit. Based on my crude knowledge of the state of connection costs from 5 years ago, it would cost about $5 a month to give every user 10Mb/s of dedicated service.

And everyone in the building would get 10 times the bandwidth of Time Warner to boot. Hurray!

I’ve been daydreaming about starting a company that contracts to do just this (drag fiber to buildings and contract for support). I know, I know, a lot of this types of companies already exist, but still… let me daydream.

Posted in Uncategorized | Leave a comment

Tesla GPU not showing up for cudaDeviceCount/getDevice

I spent a few hours today debugging this — GPU programming is its own special form of hell. It turns out I had to disable ECC and reset the GPU:

nvidia-smi -i 1 -e 0
nvidia-smi -i 1 -r

Naturally, there was no indication of an ECC error having occurred, and just resetting the GPU’s doesn’t help matters.

Sigh.

Posted in Uncategorized | Leave a comment

why couldn’t john kerry be like this when he was running for president?

Sigh. The John Kerry post-2008 is just so much more relaxed and entertaining. I’d vote for him. (Admittedly, I voted for him in 2008 as well).

How to Beat Jet Lag: Forget About It (John Kerry)

I can’t say my past experiments with jet lag remedies have been very scientific. When I’m flying, I usually take an Ambien and listen to one of my own speeches on my iPod. I’m out in seconds. But it doesn’t always work, and sometimes you’ll have some funny moments from being overtired. There was an incident in New Orleans, at Mardi Gras, in 1997. But the video has been destroyed and I gave the beads back.”

Posted in Uncategorized | Leave a comment