Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alarm history stack or queue?

I'm trying to develop an alarm history structure to be stored in non-volatile flash memory. Flash memory has a limited number of write cycles so I need a way to add records to the structure without rewriting all of the flash pages in the structure each time or writing out updated pointers to the head/tail of the queue.

Additionally once the available flash memory space has been used I want to begin overwriting records previously stored in flash starting with the first record added first-in-first-out. This makes me think a circular buffer would work best for adding items. However when viewing records I want the structure to work like a stack. E.g. The records would be displayed in reverse chronological order last-in-first-out.

Structure size, head, tail, indexes can not be stored unless they are stored in the record itself since if they were written out each time to a fixed location it would exceed the maximum write cycles on the page where they were stored.

So should I use a stack, a queue, or some hybrid structure? How should I store the head, tail, size information in flash so that it can be re-initialized after power-up?

like image 393
mjh2007 Avatar asked Nov 18 '09 19:11

mjh2007


3 Answers

See a related question Circular buffer in Flash.

like image 126
Juha Syrjälä Avatar answered Oct 03 '22 15:10

Juha Syrjälä


Lookup ring-buffer

Assuming you can work out which is the last entry (from a time stamp etc so don't need to write a marker) this also has the best wear leveling performance.

like image 42
Martin Beckett Avatar answered Oct 03 '22 14:10

Martin Beckett


Edit: Doesn't apply to the OP's flash controller: You shouldn't have to worry about wear leveling in your code. The flash memory controller should handle this behind the scenes.

However, if you still want to go ahead an do this, just use a regular circular buffer, and keep pointers to the head and tail of the stack.

You could also consider using a Least Recently Used cache to manage where on flash to store data.

like image 20
Ben S Avatar answered Oct 03 '22 14:10

Ben S