Sunday, March 31, 2013

Disabling the mouse whilst typing in Linux

I don't know why this isn't turned on in every distro by now, but the magic incantation to fix the annoyance of accidentally clicking the mouse with your palms while you're typing is:
syndaemon -d -k -i 0.5

The options are: run as resident (-d), ignore modifer keys (-k, so you can still ctrl-click), and wait for half a second before enabling the mouse again (-i 0.5).

Friday, January 25, 2013

Close Shared SSH Connections Manually

Most of the time shared SSH connections are great. They speed up connecting to your frequent servers enormously.

Occasionally though, you may need to stop it (for example, you need to create a new connection with X forwarding). To do this, use ssh -O stop <hostname>.

Via StackOverflow

Wednesday, January 9, 2013

Making Mistakes in Git

I wanted to make a couple of changes to an open source library that was based on GitHub. I had made a contribution to another library before, but I didn't really do it properly. This time, I was definitely going to do it right. Here's what I learnt.

Start and End with a Branch

I thought I was being good with my git branch foo, git checkout foo skills. While I was diligent in committing to my local branch, when I was finished I merged it into my master and pushed from there.

Pro tip: git push foo origin after you've finished working in your branch.

Doing this means you can open a shiny pull request with only your change in it.

Creating Remote Branches from Locally Merged Branches

Firstly; give up on your local branch. Seriously -- delete it using git branch -D foo. Also make sure you haven't got an existing remote branch (if you do, get rid of it using git push origin :foo).

Then recreate your local branch, and use the magic git rebase -i HEAD~5 (where 5 means get the last 5 commits). This opens an editor where you can delete the commits you don't want to appear in the branch.

Once you've done this, you can git push foo origin, and you'll have a beautiful remote branch all set up for a pull request.

Thursday, November 8, 2012

Performance Monitoring in Solaris

If you're unfortunate enough to use Solaris in production, you can at least console yourself with some of the excellent performance monitoring tools the OS supports. We recently had some performance issues which we needed to diagnose on a production server -- i.e. no profiler.

top == prstat

Your first point of call for CPU monitoring is normally top, which doesn't exist on Solaris (of course). Use prstat instead. It's basically the same in terms of information.

Individual Java Thread Monitoring

prstat has another trick -- you can use it to get an individual breakdown of the threads in your process. Use:
prstat -mvL -p <processid>
To get output like:
   PID USERNAME USR SYS TRP TFL DFL LCK SLP LAT VCX ICX SCL SIG PROCESS/LWPID 
  2108 stuart   1.7 0.0 0.0 0.0 0.0  98 0.0 0.0   5   2   5   0 java/52
  2108 stuart   0.0 0.0 0.0 0.0 0.0 0.0 100 0.0 100   0 100   0 java/18
  2108 stuart   0.0 0.0 0.0 0.0 0.0 0.0 100 0.0  50   0  50   0 java/28
  2108 stuart   0.0 0.0 0.0 0.0 0.0 0.0 100 0.0   5   0   5   0 java/21
  2108 stuart   0.0 0.0 0.0 0.0 0.0 100 0.0 0.0   5   1   5   0 java/8
  2108 stuart   0.0 0.0 0.0 0.0 0.0 100 0.0 0.0   1   0   1   0 java/20
  2108 stuart   0.0 0.0 0.0 0.0 0.0 100 0.0 0.0   2   0   2   0 java/85

man prstat will explain what the columns mean. The individual threads are in the last column; you can get out a stack trace from them via pstack. In this case, I could run pstack 2108/52 to see what the top thread looks like:
-----------------  lwp# 2108 / thread# 52  --------------------
 fc829624 * *com/sun/org/apache/xml/internal/dtm/ref/dom2dtm/DOM2DTM.nextNode()Z [compiled] 
 fc2c5bb8 * *com/sun/org/apache/xml/internal/dtm/ref/dom2dtm/DOM2DTM.getHandleFromNode(Lorg/w3c/dom/Node;)I [compiled] +50 (line 1341)
 fca4f1b8 * *com/sun/org/apache/xml/internal/dtm/ref/dom2dtm/DOM2DTM.getHandleOfNode(Lorg/w3c/dom/Node;)I [compiled] +89 (line 1409)
 fca4f1b8 * *com/sun/org/apache/xml/internal/dtm/ref/DTMManagerDefault.getDTMHandleFromNode(Lorg/w3c/dom/Node;)I+219 (line 1135)
 fca1088c * *com/sun/org/apache/xpath/internal/XPathContext.getDTMHandleFromNode(Lorg/w3c/dom/Node;)I [compiled] +6 (line 364)

IO Monitoring

This one is easy -- run iostat -xtc 1 10000 to get refreshes every second for 10,000 seconds. All these devices report how busy they are; of particular interest is the column %b, which will tell you how busy the disk is.

IO Monitoring per process

As far as I can tell, this isn't possible with the built in tools. I believe you can use DTrace and iotop, but as I didn't have enough privileges on the machine I couldn't try it.

Wednesday, September 5, 2012

GZipping vs Minification of JavaScript Resources

A debate that pops up every so often in the office is whether minification is worth it when you have GZip available.

The short answer:
  • GZipping is always a good idea, and better than nothing. 
  • Minification is always a good idea, and better than nothing. 

To find this out I looked at JQuery 1.8.1 from the Google Hosted Libraries. The results:
TechniqueSize (kb)
Original256
GZip76
Minified*91
Minified + GZipped33

So — you can reduce your JS (or JQuery, at least) size by three simply by GZipping. If for some absurd reason you can't GZip then you'd best at least minify, but you get the best performance by using both.

*I'm not immediately sure what tool was used for minification

Wednesday, July 25, 2012

How much space does a Java object use?

A colleague mentioned in passing today that Java takes 128 bits to represent a 32bit integer. I couldn't believe him -- I had expected some overhead, but a ratio of 4:1? Really?

He turned out to be right. On a 32bit JVM you need 128 bits for an integer, and for a 64bit machine, you need 224 because the class pointers double in size. Ulp.

The increase in class pointer size means that code running in a 64bit JVM generally needs 70% more memory than its 32bit counterpart. There's a switch you can use on Oracle and IBM JVMs to use compressed pointers (-XX:+UseCompressedOops). I haven't found if there are any side effects to using this.

Find all this information on From Java code to Java heap: Understanding and optimizing your application's memory usage.

Thursday, May 3, 2012

Deny IFrames with X-Frame-Options

While researching an issue in which I was trying to prevent our login page from being displayed inside an iframe, I found a great StackOverflow question. The first answer was interesting, but the second was awesome!

Turns out you can prevent anybody using a modern browser from seeing your page in an iframe by using the X-Frame-Options header. Eric Lawrence details the various options here; for us the perfect version would be X-Frame-Options:SAMEORIGIN.

I still needed the frame-busting JavaScript for our issue (the login page was showing on a session timeout) but this header is my new best friend.