Tagged Command Queuing

Tagged Command Queuing (TCQ) is a function that allows multiple commands to be sent to a SCSI disk drive (includes Fibre Channel drives), and now some SATA drives.  The “twist” when you do command queuing on SATA drives, is that they don’t used the same name – they call it Native Command Queuing (NCQ).  I don’t think this is a good name because it might imply that command queuing is or was native in the SATA world.  But I suppose the name was picked more for sales people and not technically oriented geeks like me (off soapbox now).

Why Command Queuing?

So you may be asking the question: “Why do we do queuing?”  There are really a few main reasons which are:

Speed, efficiency, less stress on the physical parts, which results in longer device lifetime.

How Command Queuing Works

I will try to keep this as simple as possible by using an analogy or two.  Try to think of a disk drive as an elevator, and the sectors (or blocks) where it stores the data are labeled from zero (0) to some maximum number (let’s say 100 to keep it easy). Each command that accesses data will include a “Where” field, which is normally called the Logical Block Address (LBA).  Think of that as the “floor number” of this elevator.

Now imagine that the disk drive is the elevator sitting at Floor 0 (ground floor) and the operating system sends 5 commands to the drive, that all go to different floors and in a different order.  For example

  • Command 01 moves data to floor 90 (LBA=90)
  • Command 02 moves data from floor 10 (LBA=10)
  • Command 03 moves data to floor 80 (LBA=80)
  • Command 04 moves data from floor 20 (LBA=20)
  • Command 05 moves data to floor 70 (LBA=70)

elevator iconThe doors close and the elevator starts to do its thing (move the commands and data).  Now if the elevator operator (the device firmware) had to do each of these commands in the order they were received, then that would take quite a bit of time and the elevator would have to move quite a distance between each command (first to floor 90, then to floor 10,  then back to 80, and so on).  As you can imagine, that would also add quite a bit of physical movement and is not very efficient.

But now think about how a real elevator works.  These same five commands jump on the elevator at the ground floor and the doors close.  What happens next? The elevator doesn’t care about the sequence the buttons for each floor were pressed, it just cares about which floor is closest to its current location, and what direction it is going.  This simple “elevator algorithm” would result in the following command execution sequence:

  • Command 02 moves data from floor 10 (LBA=10)
  • Command 04 moves data from floor 20 (LBA=20)
  • Command 05 moves data to floor 70 (LBA=70)
  • Command 03 moves data to floor 80 (LBA=80)
  • Command 01 moves data to floor 90 (LBA=90)

Note that the elevator only moved between floors (moved the head on the drive) in the most efficient manor, and did not bounce back and forth (seek) multiple times.  The result is that the overall processing time will be reduced and there is less physical movement when compared to doing the commands in the original order that they were given.

So its all about speed and efficiency.  If you have any questions, leave a comment.

Incoming search terms for the article: