Wednesday, October 27, 2010

Profiling is fun - or - getting munin-cgi-graph fast

I have worked with operations and infrastructure in and around the field of Linux for ages, and in such I job I've not gotten to program a lot. But some. Todays post may be trivial for those that program more than me, but I got a rush out of it.

I've been using some spare time to help out with the munin 2.0 alpha. So I have been profiling it to optimize performance. The original objective was to find out what munin-cgi-graph was using more than a second for when making a single graph with rrdgraph. Especially since it appeared to take significantly under a second to run the rrdgraph command.

I did the work on a site with about 150 hosts and some thousands of "services" (fewer actual plugins since there were quite a few multigraph plugins). This makes a lot of stuff heavier, the nested $config hash with all configuration in (250K lines) takes 10-20 seconds to read from a file on disk.

There are many things in Munin that can be optimized but for munin-cgi-graph the very worst single thing was unneeded use of regular expressions. Regular expressions are very very handy, but in many many cases not needed. String and sub-string matching is a lot faster than regular expressions - but more tedious to type. In the case of munin_find_field regular expressions were definitely not needed in any way. All calls to the procedure showed simple full string matches. I found a few other places with un-needed regular expressions and made some simple replacements like this:

-            next if $key =~ /^#%#/;
+ next if substr($key,0,3) eq '#%#';

This went a very long way.

But the main thing I realized was that munin_find_field was being used way too much, and after restructuring how the work given it was looped over it all went a lot faster. In the end graphing time went from over 1 second to under 0.1 second pr. single graph. That was smooth enough to look at loading in firefox that any remaining regrets I may have had about removing munin-graph from trunk some weeks ago evaporated. It's fast enough.

We're looking to put the same optimizations in Munin 1.4, tonite a guy here is going to profile a full run of munin-graph to see how it works out and what new sore thumbs might stick out. The full run he's doing normally takes 40 minutes(!) without profiling. -- Yes, I know, 40 minutes is too long, and this is why we're switching to cgi graphing. We just have to make sure the cgi graphing is fast!

For profiling I can recommend Devel::NYTprof. It was very easy to use and made it easy to see our hotspot(s).

Friday, October 8, 2010

Back up your phone - no windows needed

Clifford Stoll of " The Cuckoo's Egg" semi-fame once wrote in the rather more forgettable "Silicon Snake Oil" that no-one ever got a email that was worth keeping. I will take this as a sign of his Californian superficiality rather than a genuine insight (I know, he seems to be from Buffalo, New York, but he's an expat living in now), as I have several emails I want to keep. And I also have MMSes and SMSes I want to keep, mostly from my wife. They are snapshots of our family life, things she wanted to show me and tell me while I was otherwise engaged. Very real and personal things that are worth keeping.

Clifford also said that e-commerce was un-viable. See, not good with the crystal ball was he.

So after using mobile phones for more than 10 years I finally get around to making backups.

I have a Nokia E52 phone. This runs Nokias Symbian, 3rd or 5th edition or some such. I recently asked my colleagues, free software users one and all, how to back up this phone. gnokii I was told. And I also found gammu/wammu which is based on the same phone interface library for Nokia Phones.

Unfortunately Nokia Symbian phones from the last 3 (? or 5 or 7 or so) years have a very limited AT command set and all in all the AT commands they support are not suitable for SMS retrieval. gnokii and friends work around this by providing a interface applet for the phone called "gnapplet". Unfortunately that does not support recent Nokia Symbian phones. So neither gnokii nor [gw]ammu works. Of the lot wammu seemed the friendliest though. If only my phone was old enough!

I booted up my tame Windows install in VMWare player and installed Nokias Ovi Suite. And it synced the phones messages. The SMS messages. No MMS, i.e. no pictures of the kids and wife. WTF? And also no way to export to any kind of file format. Not exactly safe keeping. WTF²?

Nokia has a app store too now, so I could timmidly ask "Is there an app for that?".

Yes. Just one. SyncLion. It purports to syncronize SMS, MMS and contacts with any IMAP server. First I had to fiddle with my Dovecot configuration because SyncLion was playing with namespaces and Dovecot was having none of it which resulted in "Unknown namespace" messages in SyncLion.

namespace private { separator = . prefix = Mobile. list = yes location = maildir:~/Maildir/mobile }

(I learnt what SyncLion was saying by enabeling raw logging in Dovecot). After that it started copying everything to our IMAP server. Thunderbird showed the new namespace, and I could direct it to subscribe to the three folders in the Mobile namespace: Contacts, Messages and Notes. Roundcube our webmail does not seem to see the Mobile namespace in any way and is unable to show anything from it.

Each of the three folders were full of what I expected. Including MMSes! Way! So now I can copy the messages I want to keep to some safe email folder, and keep them there forever, or until both the disk and the backup fails.

There are some issues I noticed though:

  • The Sync part of SyncLion is not complete. If I delete a message on the phone it does not disappear from the IMAP folder.
  • Nor does deleting a message in the IMAP folder remove it from the phone.
  • The emails that are composed could very well embed the image (and video) files in the emails, but the image (and video) files are attachments that I need to double-click to view.
  • Contrary-wise the contacts are easily human readable, but there is no vCard attached so I can't double-click and save.
BUT, the SMSes and MMSes are now possible to keep safe.