Mac OS Terminal’s load average feedback

November 4th, 2009 by Leandro Morgado

I discovered something by accident today which is pretty nifty.  When using Snow Leopards Terminal,  you can obtain the system’s load average with CTRL+T when a process is running. Here is an example:

shell> lmorgado$ sleep 1000
load: 0.36  cmd: sleep 19856 waiting 0.00u 0.00s
load: 0.33  cmd: sleep 19856 waiting 0.00u 0.00s

Now every time you hit CTRL+T you get the load average of the running process in the foreground:

load: 0.62  cmd: sleep 19856 waiting 0.00u 0.00s
load: 0.57  cmd: sleep 19856 waiting 0.00u 0.00s
load: 0.57  cmd: sleep 19856 waiting 0.00u 0.00s

Do you have any nifty lifty tricks up your sleeve? Feel free to share them below.

Clark Little’s amazing wave photography

July 5th, 2009 by Leandro Morgado

I came across some photos that simply left me stunned. After seeing these photos I searched for who the author was and found that they were taken in Hawaii by Clark Little. Below are the samples I received. I encourage you to check out the author’s website for more stunning art work.

A Digital Camera with a Mobile Phone

July 4th, 2009 by Leandro Morgado

I was at Staples the other day and while looking through the Digital Camera section, I found this awesome, brilliant, amazing camera with Mobile Phone for only 87€. I never knew digicam’s now came with an inbuilt mobile phone!

Here are some photos ( C/Telemóvel basically translates to With Mobile Phone) of this marvellous new piece of technology:

Camera with mobile phone - 1

Camera with mobile phone

Price of camera with mobile phone

Price of camera with mobile phone

Is this the new iPod killer?! 🙂

Jacek Yerka’s Surealism

November 13th, 2008 by Leandro Morgado

I recently received an email about Jacek Yerka’s art works and just found them amazing! Have a look for yourself at his website or as this awesome YouTube compilation video:

Enjoy!

Emacs Prank

November 7th, 2008 by Leandro Morgado

Here is a small prank I found funny:

$ sudo bash
Password:
# test -f /bin/emacs  && mv /bin/emacs /bin/emacs.NEVER
# cat <<EOF >/bin/emacs
#!/bin/bash
echo emacs: command not found
EOF
# exit
$ logout

Yes, I’m a vi dude. 🙂

MySQL Locks (and a bit of the Query Cache)

September 10th, 2008 by Leandro Morgado

MySQL uses locks for concurrency control. Whenever a client/thread acquires a lock, it will have exclusive access to that table or row (depending on the granularity of the lock). Other clients however, will be prevented from writing and possibly reading to/from the locked resource. The two main existing locks are:

READ LOCK – A read lock will allow the other clients to read from the locked resource but not write to it.
WRITE LOCK – a write lock will prevent the other clients from reading or writing to the locked resource.

Also, different storage engines have different lock granularity. For example, MyISAM will lock the whole table while InnoDB will only lock the rows it needs.

Let’s try this out using a MyISAM table. We open two concurrent sessions and in the first lock the City table from the world database (get it here).

Session 1:

mysql> LOCK TABLE City READ;
Query OK, 0 rows affected (0.19 sec)

Session 2:

mysql> SELECT * FROM City LIMIT 1;
+----+------+-------------+----------+------------+
| ID | Name | CountryCode | District | Population |
+----+------+-------------+----------+------------+
|  1 |      | AFG         | Kabol    |    1780000 |
+----+------+-------------+----------+------------+
1 row in set (1.09 sec)
 
mysql> DELETE FROM City WHERE ID=1;

Here we can see that in Session 2, we are able to read from the locked table, but the query just hangs when trying to write. This happens because Session 1 has acquired a read lock on the table. As soon as Session 1 releases the lock, Session 2 is able to write to the table.

Session 1:

mysql> UNLOCK TABLES;
Query OK, 0 rows affected (0.05 sec)

Session 2:

mysql> DELETE FROM City WHERE ID=1;
Query OK, 1 row affected (1 min 39.07 sec)

We can see that the lock was in place for over a minute before Session 1 released it and Session 2 was able to grab it. Now let’s see what happens with a WRITE LOCK.

Session 1:

mysql> LOCK TABLE City WRITE;
Query OK, 0 rows affected (0.05 sec)

Session 2:

mysql> DELETE FROM City WHERE ID=2;
Query aborted by Ctrl+C
ERROR 1317 (70100): Query execution was interrupted
mysql> SELECT * FROM City LIMIT 1;

In the example above I tried to write to City, saw that it hung waiting for the lock and then aborted the query. I then tried to do a read. Due to the write lock, Session 2 can’t do a read or a write until Session 1 releases the lock:

Session 1:

mysql> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)

Session 2:

+----+----------+-------------+----------+------------+
| ID | Name     | CountryCode | District | Population |
+----+----------+-------------+----------+------------+
|  2 | Qandahar | AFG         | Qandahar |     237500 |
+----+----------+-------------+----------+------------+
1 row in set (25.64 sec)

One thing that can confuse you is the following situation. Say you read something from a table before issuing any locks:

Session 2:

mysql> SELECT * FROM City LIMIT 1;
+----+----------+-------------+----------+------------+
| ID | Name     | CountryCode | District | Population |
+----+----------+-------------+----------+------------+
|  2 | Qandahar | AFG         | Qandahar |     237500 |
+----+----------+-------------+----------+------------+
1 row in set (0.00 sec)

Then you write lock the table (preventing reads and writes from other sessions) and try the read again:

Session 1:

mysql> LOCK TABLE City WRITE;
Query OK, 0 rows affected (0.00 sec)

Session 2:

mysql> SELECT * FROM City LIMIT 1;
+----+----------+-------------+----------+------------+
| ID | Name     | CountryCode | District | Population |
+----+----------+-------------+----------+------------+
|  2 | Qandahar | AFG         | Qandahar |     237500 |
+----+----------+-------------+----------+------------+
1 row in set (0.00 sec)

Wow! Session 2 was able to read the City table even though Session 1 has write locked it! What the hell is happening? The truth is that MySQL’s query cache is tricking you. The query cache gets checked beforehand and if the query is cached, it won’t even need to look at the table or care about locks. One way to test this is by issuing a different read query that’s not in cache:

Session 2:

mysql> SELECT * FROM City LIMIT 2;

It hangs as expected, waiting for the write lock to be released. Remember that MySQL does a hash on the SQL query and if it’s different (even in case), it won’t use the query cache. We can also try the same query after emptying the query cache:

Session 2:

mysql> RESET QUERY CACHE;
Query OK, 0 rows affected (0.00 sec)
 
mysql> SELECT * FROM City LIMIT 1;

Once again, this leaves us waiting on the lock.

Concurrency is complicated stuff and there is a lot of ground to cover. There are other ways to do locks (eg: GET_LOCK(), SELECT … FOR UPDATE) with different purposes and applications. You can find out a little bit more in the excellent MySQL documentation (1 2 3)

The query cache is very handy and allows you to boost performance greatly. As with any cache, it can be confusing, so you should remember that it will serve out cached results even when you don’t expect them (it can turn it off though). You can find more info here.

One thing you can do to get prevent your queries being served from the query cache is to issue them like this:

mysql> SELECT SQL_NO_CACHE * FROM City LIMIT 2;

I’ll close off with some notes about locking:

  1. WRITE locks have higher priority than READ locks
  2. When you issue a second LOCK TABLE, it will implicitly unlock all previously held locks.
  3. To lock mutiple tables, you need to lock them simultaneously. Eg:LOCK TABLE City WRITE, Country READ;

Happy lock picking!

Gmail + aliases

August 29th, 2008 by Leandro Morgado

Gmail has a cool nifty feature. It allows you to use append a string to your email address and still receive it. For example, say your email address is username@gmail.com. If mail is sent to username+string@gmail.com you will still receive it in your regular inbox.

Why is this useful? Some might use this to combat spam, but personally I think Gmail’s spam filter does the job.

What is really neat is that you can use Gmail filters on username+string@gmail.com to automatically label or star an email. For example, say everytime you apply for at a job board, you give out the username+jobapplications@gmail.com address. You can then apply a label filter to have all your job application related emails neatly organized with a “Jobs” label.

MySQL Passwordless Login

August 12th, 2008 by Leandro Morgado

MySQL allows user specific options to be placed in a user options file under your home directory. This is handy if you use automated scripts or want to avoid typing in your MySQL user’s password everytime you login in. To do this, create the ~/.my.cnf with the options below:

[client]
user=mysqlusername
password=mysqlpassword

Then make sure no one else on the system can read this file. It’s essential for security as it contains your password in clear text!

$ chmod 600 ~/.my.cnf
$ ls -l ~/.my.cnf
-rw------- 1 alice alice 36 Aug 12 17:49 /home/alice/.my.cnf

Now you can just type mysql to login. Note that this will work for other MySQL clients like mysqldump. And you can always override the .my.cnf settings in the command line. For example:

$ mysql -ubob -pbobspassword

You can add other parameters to this file. For a full reference check out:
http://dev.mysql.com/doc/refman/5.1/en/option-files.html

Next Entries »