Sunday, December 28, 2008

World of Goo

Just tried out the demo of World of Goo, a really fun and cheap puzzle game. And to make it all much more fun, its only $20 and is devoid of any form of DRM or copy-protection. Fun, fun!

Thursday, December 25, 2008

Git rocks!

When I started using Git for my home hacking projects I mostly liked it because it was easy to set up a server via ssh without having to do a full Subversion setup and it was something I wanted to learn. I never really imagined that the distributed stuff would come in handy. Today, the remote machine I usually push/pull against was down, and I needed to sync a bunch of stuff down to my laptop. With Subversion I would have lost; ending up with manually copying files. With Git, it was as easy as

laptop$ git clone hacks hacks.git --bare
homepc$ git remote add mylaptop ssh://[user]@[host]/hacks.git
homepc$ git push mylaptop

Done. Happy, happy!

Saturday, November 22, 2008

Project Euler, problem 31

I solved another Project Euler problem today using constraints. The problem was to figure out how many ways you can combine English coins to form £2. Here's the constraint variant.

currency(List, Sum) :-
List = [N1,N2,N5,N10,N20,N50,N100,N200],
200 #= N1 + N2 * 2 + N5 * 5 + N10 * 10 + N20 * 20 + N50 * 50 + N100 * 100 + N200 * 200,
N1 #>= 0, N2 #>= 0, N5 #>= 0, N10 #>= 0,
N20 #>= 0, N50 #>= 0, N100 #>= 0, N200 #>= 0,
fd_labeling(List).

ways(Sum, Number) :-
findall(List, currency(List, Sum), Ways),
length(Ways).

Note that you have to explicitly inform the solver to use 0 or more coins of each denomination.

Edit: replaced "Sum" with 200 in the currency/2 predicate.

Wednesday, November 12, 2008

Ljud och ljusspel i Engelska parken

Har ni vägarna förbi Uppsala så måste ni gå och besöka Engelska parken kvällstid nu i november:

Wednesday, November 5, 2008

A tribute to John Williams

A really impressive four-voice a capella tribute to John Williams:

260 bytes ought to be enough for anyone, right!

I'm approaching the point were I'm just going to assume that Windows is a game-console which can be used to play Grand Theft Auto on, and just ignore the fact that people actually want to use it as a "normal" operating system. If anyone walks in to my office and talks about windows, I'll just look at them with a puzzling look on my face.

What pushed me a little closer this time was that my colleague managed to break out nightly build (just as we're about to build a snapshot for other departments to start using) by checking in a directory. This directory, called "launches", caused Subversion to fail in a checkout step during the nightly build complaining that "The filename or extension is too long". WTF! IDIOTS!

Tuesday, November 4, 2008

Ubuntu 8.10

I installed Intrepid Ibex this morning on my HP 6710b laptop, and I have to say that I am impressed. It took around an hour (most of the time downloading and installing packages) and after rebooting It Just Worked. One reboot.

It also automatically detected my external monitor automatically (well, I had to switch of the "mirror screens" option in the screen resolutions settings). So now I have three monitors, two for my laptop, and one for my PC. I can use the same keyboard by connecting the PC using Synergy.

I haven't tried any of the new shiny features, such as creating an USB stick with Ubuntu on it, though. (Too bad Emacs still does not come by default with xft font support. I'll just have to get that manually.)

Sunday, November 2, 2008

Constraint programming

I've exercises the programming part brain by solving problems at ProjectEuler.net now and then. Has some fun problems which usually don't take too much time to solve (I haven't gotten to the difficult ones yet...). Today I finally got to exercise my knowledge of constraint-programming in Prolog. My master's thesis was about implementing multithreading in Prolog, and my supervisor is a die-hard constraint programmer, so he would never forgive me if I solved the problem by brute force. :-)

The problem in question is Problem 9: find the only Pythagorean triplet {X,Y,Z} such that X + Y + Z = 1000. Here's the constraint solution:

triplet([X, Y, Z]) :-
X #\= 0, Y #\= 0, Z #\= 0,
X**2 + Y**2 #= Z**2,
X + Y + Z #= 1000,
fd_labeling([X, Y, Z]).

It isn't the shortest solution, but it has a clearness to it which is just amazing. It almost feels like cheating. (If you want to try it, you can use GNU Prolog).

Friday, October 31, 2008

Ruby performance bug

I got a bugreport the other day about a piece of Ruby code which was executing much slower than it really should. It's basically a piece of code which unpacks a zip-archive using rubyzip. Since I like TDD, I started by writing a test-case which would exhibit the behavior: create a zip-archive with 500 smallish files (around 100 bytes). Unpacking this took in the order of 10 seconds (as a baseline, unpacking the same zipfile using "unzip" takes .03 seconds). There are a lot of other things going on (YAML-parsing, for example), so we don't need to be on par with unzip, but 10 seconds is way too much.

I managed to narrow down the problem to a function which is called to print the name of each file as it is being unpacked. Removing the call to this function cuts the time from 10 seconds to 3 seconds. But why? The code looks essentially like this:

if @@current_obj != @obj
# do stuff
@@current_obj = @obj
end

If I comment out the @@current_obj = @obj line, things go fast (~3 seconds), and if I keep the assignment, it takes ~10 seconds. Surely a simple assigment can't take (10 - 3)/500 = 14 msecs?

If turned out that the real problem was the comparison. My guess is that Ruby attempts to make a deep comparison (@obj isn't very large, but making a deep comparison might take some time). Also, when I removed the assignment at the end of the if-statement, the comparison was always made with nil, which should be very fast (and constant).

So, I changed the comparison to
    
if @@current_obj.object_id != @obj.object_id
# do stuff
end

which prevents Ruby from making a deep comparison.

Friday, October 24, 2008

Merging in Subversion

Now I've done my second Subversion merge using the new 1.5 mergeinfo, and both times I've hit the same problem when trying to "reintegrate" the branch. Subversion complains:
   Some revisions have been merged under it that have not been merged
into the reintegration target; merge them first, then retry.
This post on CollabNet's Subversion blog helped me out. The problem boils down to the fact that there is "subtree" mergeinfo in the branch and/or trunk which prevents the merge from completing. Usually you can safely delete the subtree merge info:
   svn propdel svn:mergeinfo <subtree> -R
I honestly don't understand why this issue arises or why Subversion is not able to resolve it. Despite having mergeinfo is much better than not having it, I can't help feeling that this is a kludge to handle a feature which Subversion wasn't at all designed for.

Tuesday, October 7, 2008

New shiny colors

Time for some new colors on this blog...

The 500-mile email

Very funny anecdote on mysteriously failing emails: The case of the 500-mile email. (It's from 2002, but it was the first time I heard about it.)

Microsoft bashing day (again)

For some strange reason, I still get surprised at how bad Microsoft is at designing user-interfaces. And now I'm not even talking about the graphical ones. The command-line has long been viewed at Microsoft as something inherently evil: everything must have a graphical interface to be deemed usable; command-line tools are by definition user-hostile. Which, of course, is not true. Badly designed command-line tools are user-hostile, just as badly designed graphical user-interfaces are user-hostile. And Microsoft is pretty good at doing both.

Microsoft does not lack good interface designers. Where they really put in the effort, the result is sometimes really good. For example, I like the new Office-toolbar and the interactive display of keyboard shortcuts (even if it's a little annoying to have to relearn the entire user-interface. Every single command seems to have been moved), and the new dialogs with big descriptive buttons instead of just yes/no buttons. Also, I like the new Google Chrome. No wait, wrong company.

But the command-line tools seldom get any attention (with the possible exception of PowerShell, which I haven't tried yet). When will we get a new Windows console, for example? With a real font selection dialog, proper resizing, etc.?

The tool which actually caused my bloodpressure to exceed the "must-blog-about-it" point, is CACLS. CACLS is a tool for displaying or modifying the ACLs for files and folders. I wanted to use it to be able to remove a write-protected file (a file checked out by svn with the svn:needs-lock attribute), and I wanted to do it in an automated test.

Guess what: CACLS will ask the user for confirmation when changing permissions, and there is no option to turn it off. What is the BLOODY POINT of having a command-line tool which cannot be used from a script or bat file? The workaround is apparently to do "echo Y | cacls ...", but still. How hard can it be to add a single flag to switch on/off the question?

Now I'll go back to work and try to figure out how to prevent interactive dialogs blocking my automated build.

Friday, September 26, 2008

Thursday, September 18, 2008

Hack of the day: displaying the repository location in your bash prompt

I usually want to keep track of which repository/branch the current directory is pointing at, so I wrote the following bash script to display it in the prompt. The script can handle Git and Subversion.

function is_git_repo()
{
if [ $PWD = / ]; then
return 1;
elif [ -d $PWD/.git ]; then
return 0;
else
(cd ..; is_git_repo)
fi
}

function display_vc_location()
{
if [ -d $PWD/.svn ]; then
SVNLOC=$(svn info $PWD | sed -ne 's@URL: \(.*\)@\1@p' | cut -d/ -f4-)
SVNREV=$(svn info $PWD | sed -ne 's@Revision: \(.*\)@\1@p')
echo svn\($SVNLOC@$SVNREV\)
elif is_git_repo; then
GITLOC=$(git branch -a | grep -e '^*' | cut -d' ' -f2-)
echo git\($GITLOC\)
fi
}

Add it to your PS1 variable, like this:

PS1="... \$(display_vc_location) ..."

Exercise: extract both URL and revision without running svn info twice.

Tuesday, September 16, 2008

Haiku

Inspired by an article in Dagens Nyheter:

the moose is loose
it could not find its way out
it had to be shot

Stack overflow

Short post today. 

For those of you who haven't found it yet, head over to Stack Overflow and start contributing to the collective programming knowledge heap. It's a pretty neat site for asking and answering questions about programming.

Friday, September 5, 2008

Pirate Bay and the Arboga child murders

The two murdered children in Arboga has gotten quite a media coverage here in Sweden (article from The Local, in english). Now the entire police investigation can be downloaded from Pirate Bay. I usually don't object to what the Pirate Bay does -- usually the only damages they cause are economic ones to large media companies.

But this time they are actually taking it one step further and showing immense disrespect towards two brutally murdered children who now have photos of their autopsies publically available on the internet. And since the internet never forgets, the damage is already done. Of course, the Pirate Bay people -- arrogant as they are -- don't really care: "I don't think it is our job to judge if something is ethical or unethical" (Peter Sunde). I suppose it is just a matter of time before outright criminal material such as child porn is regularly hosted by the Pirate Bay.

I'll just have to get my hands on the next season of 24 through some other channel.

I can play GTA San Andreas again! Yipiee!

A couple of months ago, GTA:SA refused to start after a driver update (no symptoms really, the game just would not start). I didn't have time to pursue the problem at the time -- I have completed all the missions and don't really play the game too much now, but it is still fun to pick up now and then.

Now after a couple of minutes googling, I found some posts indicating that a corrupt user-settings file called gta_sa.set may cause these exact symptoms. I deleted the file (it's in the My Documents\GTA San Andreas User Files directory), and now the game starts.

I'm off to running over old ladies again!!!

Tuesday, July 22, 2008

Autobuilding (Erlang) using inotifywait

Quick hack of the day:

#!/bin/bash

ERLC="erlc -W +debug_info"
inotifywait -m -e close_write *.erl | while read f; do
file=$(echo "$f" | cut -d' ' -f1)
cmd="$ERLC $file"
clear
date
echo $cmd
$cmd && echo "Success."
done

Requires a fairly modern Linux kernel (2.6.13-ish). Run the script in a terminal, and it will automatically compile any Erlang sources as soon as you save them, and display the error messages in the terminal window. (Replace ERLC with compilation command of your choice.)

Tuesday, June 3, 2008

Goosh

Cool stuff. Command line access to Google, Wiki, and a host of other stuff.

Thursday, May 8, 2008

Can we just STOP using fixed coordinates for laying our user interface elements?

What is it with these Windows-people who still use fixed coordinates for laying out user-interfaces?! I thought that would be something that at least would be eradicated when Microsoft released the .NET platform a few years ago, but no. Every other decent UI toolkit (Qt, SWT, Gtk+, and a couple of others) has abandoned fixed coordinates for layout, but Windows people persist. Is there some misguided idea that "we MUST know exactly on the pixel how our user interface looks, and it has to look exactly the same everywhere"?

Yes, user interface layout is a difficult problem, but treating it as a drawing problem is not the solution.

Impressive data recovery

People at NASA have successfully recovered 90% of the data on a 400MB Seagate harddrive from the Colombia Space Shuttle. Yes, that's the one that crashed and burned at high altitude. Very impressive.

Monday, May 5, 2008

Microsoft is "getting the facts". Again.

Microsoft has a site which claims to help customers compare Windows Server editions with Linux. I stumbled across it while reading David M. Williams blog, and he pointed out a case study on the site involving Uppsala. Since I've lived in Uppsala for many years, I couldn't resist reading on. It turns out that there was no comparison at all. Uppsala hired a Microsoft Gold Certified partner to see if they could increase the security of their windows networks and the conclusion was that Windows Server 2008 is a better product than Windows Server 2003. No mention of Linux at all (except an off-hand remark by a server technician).

It's good to see that Microsoft still knows how to do real FUD.

Wednesday, April 16, 2008

Debugging JNI code using Eclipse and Visual Studio

Debugging Java Native Interface code (JNI) can be quite an adventure, but fortunately it works quite nice when you have Eclipse and Visual Studio (and preferrably at least 2 monitors). I have Eclipse on the primary monitor, and Visual Studio on the secondary. This is how it goes:

  1. Start your java program, with a breakpoint before entering the native method you want to debug. You probably want to break after the native code has been loaded, but you don't have to.
  2. Switch to Visual Studio, and bring up the "Attach to process" dialog (Ctrl-Alt-P). Select the java process (javaw.exe or java.exe) by typing "j" (possible several times, epending on how many processes you have starting with the letter "j"). Press return. Visual Studio should now switch to debugging and the title bar should include the test "(Running)", indicating that you are debugging a running process.
  3. Set a breakpoint to cause Visual Studio to stop in your native code.
  4. Switch back to Eclipse, and start running. When eclipse enters your native code, Visual Studio will break the debugged java program. (Eclipse may note that the debugged program is not responding, since it is suspended by Visual Studio).
  5. When you're done debugging, you can "release" the java-program using the "Stop Debugging" command in Visual Studio. This will cause the java program to continue running normally (and terminate, unless Eclipse has a breakpoint on it).

Some things you may want to pay special attention to:

  • Eclipse and Visual Studio have different shortcuts for run/step/step-over, etc. This is a major pain, and I think my muscle memory is permanently damaged when it comes to learning them.
  • You need to keep track of who is debugging. This can be confusing.

People are working on making JDT and CDT cooperate in debugging JNI code, allowing seamless stepping in and out of Java/JNI code and integrated debugger views (callstack with both Java and native frames displayed, for example), but it is still in the early prototyping phase.

Wednesday, March 26, 2008

JavaCity

Funny name for a cafe at a an Eclipse-conference.

Chocolate fountain

This was really tasty. They had one with white chocolate as well. Next year I'm hoping for one with dark chocolate.

Summarizing EclipseCon 2008

Well, my attempt at blogging at least sem-regularly during EclipseCon didn't exactly work out. Fortunately that was largely cause by my own laziness, not by the lack of interesting topics. But now that I'm back in warm and sunny Sweden (NOT!) and have slept off most of my jetlag, I'll try to do a short summary.

EclipseCon is a really big conference (well, maybe not in parity with JavaOne or OOPSLA) with over 300 talks of varying length and (wildly) varying topics. Here's what I enjoyed the most (in no particular order):
  • Eclipse, Open Source, Wall Street and Competition: Big Drama, Big Money. Brent Williams talked about how big business view Eclipse/FOSS.
  • Dan Lyons keynote.
  • Adopting and Commercializing Eclipse: Wind River's Strategy. Tomas Evenson (CTO at Wind River) covered Wind River's history behind their Eclipse adoption, and how they view their collaboration with the Eclipse community. Especially interesting, since Wind River in practice controls most of the C/C++ development efforts (CDT and DSDP).
  • Debugging Java and JNI together. A short talk presenting a way to seamlessly debug Java and JNI (Java Native Interface) code together, i.e. being able to step from Java code into JNI code (moving from JDT to CDT). Impressingly enough they pulled of a working demo as well (the short talks were only 10 minutes). Having come across JNI at more or less every place I've worked at, I've been wanting this for the last ten years. Unfortunately it is currently limited to gdb and required a specially patch version of Apache's JVM, but it is a step in the right direction for those of use who don't have the luxury of living in the padded room of the JVM.
  • The Next Wave of IDE Innovation: Eclipse and Visual Studio in 2010. Kevin McGuire and Tim Wagner talked about what new innovations we can expect in the coming years in the area of IDE-development (not necessary Eclipse) -- new input devices, multi-monitors, multi-core support. My first impression of this talk was that it was too much wild speculation and brainstorm, but I suppose that all innovation has to start that way.
I'm looking forward to EclipseCon 2009 already.

Thursday, March 20, 2008

VerMonster Sundae

We went to watch a movie yesterday, and took an icecream at Ben and Jerry's first. Not only do they have really tasty ice-creams, they have really large ones as well.

Tuesday, March 18, 2008

EclipseCon 2008: Fake Steve Jobs

Daniel Lyons (aka Fake Steve Jobs) gave a really funny keynote yesterday. If you aren't reading his blog, you're missing out on some great satire.

Monday, March 17, 2008

EclipseCon 2008, day 0

So, I've finally arrived in Santa Clara for EclipseCon. We (I'm travelling with a collegue) actually arrived yesterday after a murderous long flight from Stockholm. First 2.5 hours from Stockholm to London, 3 hours at Heathrow (which was actually quite nice, we hade plenty time to walk around the taxfree stores and have some lunch). Then an 11-hours flight to San Francisco, and as if that wasn't enough we decided to take public transport to the hotel. Let's call that idea "less than brilliant". Sufficient to say, it took us around 4.5 hours before we could finally get some sleep.

Anyway, today was much better. What could be a better way to wash off the jet-lag (SF is 8 hours behind Stockholm) than spending a day at Great America? Luckily enough, they opened for the season today and the weather was sunny and not too windy, even if the temperature could've been a little higher.

Friday, March 7, 2008

3D Excel

Excel as a 3D engine. Yet another proof that some people have way too much time on their hands.

Tuesday, March 4, 2008

SCons and VMWare

VMWare has been using SCons for a year now and are actually pretty happy about it. I would have thought that they would complain more about performance, though.

Thursday, January 17, 2008

ZFS

A friend of mine gave me a link to a set of slides about ZFS the other day. ZFS is a radical different approach to filesystems, with a lot of cool features. It leaves the WinFS vaporware gasping for air. Some of the cool features:
  • Constant-time snapshots: takes no additional space
  • Clones: writable snapshots
  • Backup/restore: incremental backups take time proportional to data changed
  • End-to-end data integrity
  • Dynamic striping: automatically distribute load across all devices
  • Multiple independent prefetch streams, automatic length and stride detection
For personal use I find the backup/restore functionality especially cool. An incremental backup is done by
# zfs send -i tank/fs@A tank/fs@B > /backup/B-A
The time it takes to do a incremental backup is proportional to the amount of data changed, so you can easily use it to drive remote duplication. The following command sends a incremental once per minute to a remote host.
# zfs send -i tank/fs@11:31 tank/fs@11:32 | ssh host zfs receive -d /tank/fs
(The examples are taken from the slides.)

The only drawback is that there is no ZFS support for Windows. Hopefully that will change in the not-too-distant-future.