<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Why are the records getting dirty in fds example</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/59746/why-are-the-records-getting-dirty-in-fds-example</link><description>I am trying to use the fds example provided in the sdk16, while viewing the debugging prints I see the following, 
 &amp;lt;info&amp;gt; app: Initializing fds... &amp;lt;info&amp;gt; app: Event: FDS_EVT_INIT received (NRF_SUCCESS) &amp;lt;info&amp;gt; app: Found 1 valid records. &amp;lt;info&amp;gt; app: Found</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 02 Apr 2020 12:05:13 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/59746/why-are-the-records-getting-dirty-in-fds-example" /><item><title>RE: Why are the records getting dirty in fds example</title><link>https://devzone.nordicsemi.com/thread/243176?ContentTypeID=1</link><pubDate>Thu, 02 Apr 2020 12:05:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:86ce8125-482c-4091-9ee9-59d8deda7521</guid><dc:creator>Embedded_Engineer</dc:creator><description>&lt;p&gt;That was the case.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;.data.length_words = (sizeof(four_word_cfg) + 3) / sizeof(uint32_t),&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Why are the records getting dirty in fds example</title><link>https://devzone.nordicsemi.com/thread/243157?ContentTypeID=1</link><pubDate>Thu, 02 Apr 2020 11:16:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:88d59d72-732d-4977-bdb5-5f6121ec13c6</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Are you sure you did only write one word? Note that the data.length_words parameter in the fds_record_t struct takes the number of words, not the number of bytes, to be written. If you use sizeof() or similar to get the size of the struct, you need to divide by 4.&lt;/p&gt;
&lt;p&gt;I modified the FDS example to output stats.words_used and it seems to work correctly:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;fds example:~$ stat
total pages:    3
total records:  2
valid records:  2
dirty records:  0
largest contig: 1022
freeable words: 0 (0 bytes)
words used:     12
fds example:~$ write 0xFF 0xFF TEST
writing record to flash...
file: 0xFF, key: 0xFF, &amp;quot;TEST&amp;quot;, len: 4 bytes
&amp;lt;info&amp;gt; app: Event: FDS_EVT_WRITE received (NRF_SUCCESS)
&amp;lt;info&amp;gt; app: Record ID:  0x0003
&amp;lt;info&amp;gt; app: File ID:    0x00FF
&amp;lt;info&amp;gt; app: Record key: 0x00FF
fds example:~$ stat
total pages:    3
total records:  3
valid records:  3
dirty records:  0
largest contig: 1022
freeable words: 0 (0 bytes)
words used:     16&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Why are the records getting dirty in fds example</title><link>https://devzone.nordicsemi.com/thread/243146?ContentTypeID=1</link><pubDate>Thu, 02 Apr 2020 10:25:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6f362853-0521-408b-86bd-e7d6a9502877</guid><dc:creator>Embedded_Engineer</dc:creator><description>&lt;p&gt;Refer the declaration below,&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;typedef struct
{
  uint32_t boot_count;
  
} minimal_configuration_t;

&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I am updating single word record in flash,&lt;/p&gt;
&lt;p&gt;After record has been successfully updated I am comparing the words (stat.words_used) written in flash,&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;
================================================================================
                  F L A S H   S T A T S  ( F D S )
================================================================================
| BEFORE | AFTER | DESCRIPTION
|-------------------------------------------------------------------------------
| 3      | 3     |   Pages Available.
| 0      | 0     |   Open records.
| 1      | 1     |   Valid records.
| 74     | 75    |   Dirty records (ready to be garbage collected).
| 7      | 7     |   Words reserved by fds_reserve().
| 680    | 687   |   Words written to flash.
| 1022   | 1022  |   Largest number of free contiguous words in file system.
| 662    | 669   |   Words that can be reclaimed by garbage.
| 0      | 0     |   Corrupted records.
================================================================================
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Before: 680&lt;/p&gt;
&lt;p&gt;After : 687&lt;/p&gt;
&lt;p&gt;Difference = 7&lt;/p&gt;
&lt;p&gt;Since the length of new record&amp;#39;s header is 3 and length of the data i.e. boot_count is 1 word. Shouldn&amp;#39;t the difference be 4 instead of 7?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Why are the records getting dirty in fds example</title><link>https://devzone.nordicsemi.com/thread/242983?ContentTypeID=1</link><pubDate>Wed, 01 Apr 2020 13:29:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:eb1381b9-9bb8-4775-9ed3-b00d652b6e7d</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;That is almost correct. Each page contains a&amp;nbsp;&lt;a title="Page tag" href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v16.0.0/lib_fds_format.html?cp=7_1_3_56_2_1#lib_fds_format_page"&gt;Page tag&lt;/a&gt;, which occupies the first two words. This limits the available words for each page to 1022. Also, note that each record has a 3-word header, which needs to be accounted for when calculating the available space for actual data.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Why are the records getting dirty in fds example</title><link>https://devzone.nordicsemi.com/thread/242967?ContentTypeID=1</link><pubDate>Wed, 01 Apr 2020 13:07:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4f8333fa-aa2f-4343-8cc6-c12c33219c9b</guid><dc:creator>Embedded_Engineer</dc:creator><description>&lt;p&gt;&lt;span class="user-name"&gt;&amp;nbsp;&lt;a class="internal-link view-user-profile" href="https://devzone.nordicsemi.com/members/joh2"&gt;J&amp;oslash;rgen Holmefjord&lt;/a&gt; &lt;a class="internal-link view-user-profile" href="https://devzone.nordicsemi.com/members/joh2"&gt; &lt;/a&gt;&lt;/span&gt;Thank you very much for the details.&lt;/p&gt;
[quote userid="14926" url="~/f/nordic-q-a/59746/why-are-the-records-getting-dirty-in-fds-example/242886"]You can call&amp;nbsp;&lt;a href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v16.0.0/group__fds.html#ga5d3f04e9755408293408be377f7eeaf9"&gt;fds_stat&lt;/a&gt;() to get statistics about the available flash size[/quote]
&lt;p&gt;Running stats yields,&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;================================================
        FLASH STATS (FDS)
================================================
3 pages Available.
0 open records.
1 valid records.
51 dirty records (ready to be garbage collected).
0 words reserved by fds_reserve().
472 words written to flash.
1022 largest number of free contiguous words in the file system.
459 words that can be reclaimed by garbage.
0 corrupted records&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;From flash_bounds_set() I see flash size as 0x3000,&lt;/p&gt;
&lt;p&gt;flash_size&amp;nbsp; = (FDS_PHY_PAGES * FDS_PHY_PAGE_SIZE * sizeof(uint32_t))&lt;/p&gt;
&lt;p&gt;= 3 x 1024 x 4&amp;nbsp;&lt;/p&gt;
&lt;p&gt;= 12kB&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;FDS_VIRTUAL_PAGES is 3, where FDS_VIRTUAL_PAGE_SIZE is 1024. As you mentioned one of the page will be used as SWAP page. So we have two pages out of three and by one page we mean we have 1024 WORDS i.e. 4096 bytes. Is this Correct?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;From the stats we see 472 words written to flash. Which leaves us with 2048 - 427 words?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Why are the records getting dirty in fds example</title><link>https://devzone.nordicsemi.com/thread/242886?ContentTypeID=1</link><pubDate>Wed, 01 Apr 2020 09:41:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:74806880-7830-4cb7-9423-2f66e6473ddd</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;The purpose of creating a new record and then invalidating the old one is not solely to prevent data loss due to power loss, this is also a more effective way to store data in flash. Flash can only be erased a whole page at a time, meaning that deleting a record and writing a new one back to the same location would require the entire flash page to be erased in between. &lt;a href="https://infocenter.nordicsemi.com/topic/ps_nrf52840/nvmc.html#unique_797338916"&gt;Erasing a flash page&lt;/a&gt; takes a long time, and the flash has a limited number of write/erase cycles (&lt;a href="https://infocenter.nordicsemi.com/topic/ps_nrf52840/abs_max_ratings.html"&gt;endurance&lt;/a&gt;) before it starts to fail. Spreading the writes out over the entire page will provide basic flash wear leveling, increase the lifetime of the flash.&lt;/p&gt;
&lt;p&gt;The maximum data you can store before running garbage collection depends on the number of reserved pages for FDS (&lt;a href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v16.0.0/group__fds__config.html#ga5a6ee4b21f32ba0776a116880156c992"&gt;FDS_VIRTUAL_PAGES&lt;/a&gt;). Note that one of the pages (SWAP page) will be reserved for garbage collection. During garbage collection, valid records are copied to the SWAP page before the DATA page is erased. The old SWAP page is then promoted to a DATA page, while the old DATA page will become the new SWAP page.&lt;/p&gt;
&lt;p&gt;You can call&amp;nbsp;&lt;a href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v16.0.0/group__fds.html#ga5d3f04e9755408293408be377f7eeaf9"&gt;fds_stat&lt;/a&gt;() to get statistics about the available flash size. It is not recommended to run garbage collection too often, as this will increase the wear on flash. We recommend calling it only when FDS has filled all available space, or when it is getting close to full.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;br /&gt;Jørgen&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Why are the records getting dirty in fds example</title><link>https://devzone.nordicsemi.com/thread/242851?ContentTypeID=1</link><pubDate>Wed, 01 Apr 2020 08:25:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:89ee2065-04e3-41eb-9600-3624dcc7adef</guid><dc:creator>Embedded_Engineer</dc:creator><description>&lt;p&gt;After going through&amp;nbsp;&lt;a href="https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk5.v14.0.0%2Flib_fds_functionality.html"&gt;fds functionality&lt;/a&gt; It seems that when a record is to be updated (fds_record_update), a new record is created and old record is invalided solely to prevent loss of data due to power loss in the middle of operation. A new record descriptor is returned for updated record.&lt;/p&gt;
&lt;p&gt;Thus frequent updates will consume flash space and its a good idea to clean it up using garbage collection.&lt;/p&gt;
&lt;p&gt;1. The size of record varies depending upon the data stored. What is the maximum data that can be stored after which garbage collection becomes necessary? Is it FDS_VIRTUAL_PAGE_SIZE&amp;nbsp; per page?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>