USB sticks: charging can't be ignored





On the Internet, there was a riot called "why charge a USB flash drive." At first glance, it’s funny, but I propose to think again and understand the issue. After reading this article, you will start charging your flash drives too.



Personally, the chief designer of one of the enterprises of the radio-electronic industry threw off a link to an article with the wording "do you want to neigh?"



A couple of related links:





Of course, I laughed, but not over what you thought. I had to take part in the development of solid-state drives based on NAND-flash memory, including for USB sticks. At first glance, for a tech-savvy person, it seems obvious that connecting the drive to the charger is meaningless, since besides supplying power, no other actions are performed with the drive, therefore, we will get nothing except heat dissipation. On the Internet there are many different "revelations", chuckles and even outright banter over poor humanitarians who ask whether it is worth charging flash drives. You may laugh, but I saw with my own eyes how the developers of flash drives "charged" their products by installing them in chargers for a while!



A USB flash drive is a block storage device: the entire address space is divided into 512-byte sectors. The operating system can access the address of a specific sector (LBA) and read or overwrite it, everything is simple.



Now let's take a look under the hood of our flash drive ...



The flash drive includes microcircuits:



  • controller;
  • NAND-flash memory.


Memory microcircuits are arranged in a rather specific way in terms of addressing, writing and storing data, which is dictated by its architecture (and we must love it as it is). The following rules are defined for NAND-flash:



  • The memory is divided into blocks of the order of Megabytes;
  • Before writing to a memory block, it must be erased. When erased, all data bytes in the block are set to 0xFF;
  • The block consists of pages, tens of kilobytes in size;
  • , ;
  • ;
  • .


Here, this set of rules turns such a simple action (from the user's point of view) how to write one sector to a USB flash drive into a real puzzle for the developer of the controller of this same flash drive.







Try to imagine how you would solve this problem, and you will see similarities with the toy Tower of Hanoi .



In order to "make friends" the user-friendly block addressing of the drive and the sadist-friendly addressing of NAND-flash memory, a processor is spinning inside the flash drive controller, which implements the address translation algorithm, aka FTL (Flash Translation Layer). The tasks of the FTL algorithm include:



  • building and maintaining a page mapping table;
  • Garbage collection;
  • wear leveling of NAND-flash memory blocks.


Knowledgeable people will say that there are also other tasks, such as tracking bad blocks of NAND-flash memory (bad block management), but I don’t want to talk about this now, because it does not apply to the current case ...



A little educational program on the indicated points :



Page mapping



Well, here, perhaps, everything is clear ... The logical address space of the drive (LBA) is translated into the addresses of blocks and pages of NAND-flash memory (physical address) through a huge array, the index of which stands for LBA, and the value of the element is the physical address. If it is necessary to rewrite one page, then the data of this page is written to a free block in order, and then, in the array, the page number is replaced with the newly written one. When a student buys a 32GB flash drive and discovers that it has only 29 GB, the student does not yet know that it was not the Chinese who stole the missing space in the factory, but the developers of the FTL algorithm. To be able to write data to the drive.







Garbage collection



And what will happen to the page that has lost its relevance? The data written in it is no longer needed, but we cannot erase it, because erasing is allowed only in blocks, and in the same block there may still be actual pages. Sooner or later, a situation will develop when we no longer have free blocks in which we can write pages. But, in the remaining blocks, here and there there will be irrelevant pages. To prevent this from happening, the drives run the functionality of a "garbage collector", which is engaged in finding "leaky" blocks, which have the least number of relevant pages, and transfers the actual pages to a new block. Thus, the "leaky" block is completely freed from the actual pages and can be erased ... And in the new block, all pages remain relevant. Reminiscent of defragmentation.







Wear leveling



Nothing lasts forever under the moon, especially NAND flash. It just so happened that NAND-flash memory has a limited resource, which is expressed by a limited number of block erasure cycles (block age). Blocks that have been erased more times (old blocks) are more likely to fail than those less worn out (young blocks).



Have you ever thought about moving the FAT table from zero addresses of the drive somewhere else so as not to wipe a hole in the address space of the flash drive? This is unlikely and should not be thought about. Because a block wear leveling mechanism works in the flash drive. Its essence is that young blocks are forced to swap places with old ones, so that the wear of all blocks is uniform during the operation of the drive. So the drive has a chance to live happily ever after until all of its blocks die in one day.







And now about the main thing - why "charge" the flash drive?



1) Have you ever had a situation where you are going to wash your socks every day, but every evening it turns out that you are not up to it? And then there comes such a moment that there are simply no clean socks left for tomorrow! And then you have to sacrifice sleep to wash your socks. It's even worse if you have to be late for work with a hairdryer in hand.



Much the same happens with the garbage collection algorithm in FTL. The user often uses a USB flash drive to transfer some data from one computer to another. In this case, the scenario of actions is as follows: plug the USB flash drive into the computer - write files quickly - pull out the USB flash drive - run to another computer - plug in the USB flash drive - read the files. After a while, the user begins to notice that his drive is slow. Usually, in such cases, they sin on the fact that β€œwell, it's just a cheap, old flash drive. Here, I'll buy a new one, it will fly! " And it really will be! But she, most likely, will suffer the same fate after some time. The fact is that the scenario of such "short films" does not allow the garbage collection algorithm to release blocks for writing, which sooner or later will lead to the fact that there will simply be no more free blocks.And then the controller is forced to first deal with freeing blocks, and then only writing your files to them, hence the loss of speed. To be ready to receive your data at full speed, the drive needs time off to β€œwash socks” beforehand. As you might guess, "charging" the flash drive removes this problem, giving the controller enough time to restore order in the data.







2) The problem of lack of "personal time" of the controller is also relevant for the wear leveling algorithm. The Wear Leveling algorithm is executed by the controller when the drive is idle, while there are no tasks to write or read user data. If the drive works in the "short films" mode, then there is simply no time to level out the block wear. Uneven wear of blocks leads to the fact that old blocks fail. At the same time, the number of blocks available for writing decreases until a critical moment comes, when there simply will not be any free blocks for writing, although the NAND-flash memory as a whole has not yet been worn out and could serve for a long time.







3) Evolution has taught us to bury valuables dear to us somewhere far from the passages. This works well for treasures and uninhabited islands. But with digital data and NAND, the opposite is true. Surely, you have had cases when you threw some photos from a friend's wedding onto a flash drive, the flash drive lay in a drawer for a year (as it seemed to you, safe and sound), and then some of the photos were only half read. The fact is that information once recorded in NAND-flash memory can "rot" over time. The memory manufacturer does not guarantee 100% data retention, but simply announces the likelihood of bit errors.



Of course, the drive controller solves the problem of eliminating bit errors by adding redundant code to the data, but no matter how large this code, over time, the charge in the NAND memory cells dissolves, and the number of bit errors can exceed any correcting ability. Data should not be left alone for a long time in NAND-flash memory, it must be looked after. Namely - periodically overwrite. The correct controller periodically re-reads data, checks the number of bit errors and rewrites data if necessary (until the number of bit errors exceeds the allowable value). Of course, the controller also needs "free time" for this.



From all of the above, I will summarize: do not hesitate to "charge" the flash drives, this has a beneficial effect on their speed and reliability. And if techies hit you, just give them a link to this article.



All Articles