ARMmbed / mbed-os

Arm Mbed OS is a platform operating system designed for the internet of things
https://mbed.com
Other
4.66k stars 2.97k forks source link

TDBStore init cause too much times when have a lot of keys #15158

Open CamelShoko opened 2 years ago

CamelShoko commented 2 years ago

Description of defect

I use spi-flash as blockdevie with TDBStore, an have create 400+ keys, when system reboot, at program begin called function 'TDBStore::init()' will cause 15 seconds or more, the spi-flash clock speed is 16MHz.

Target(s) affected by this defect ?

TDBStore

Toolchain(s) (name and version) displaying this defect ?

arm-none-eabi-gcc (GNU Arm Embedded Toolchain 9-2020-q2-update) 9.3.1 20200408 (release)

What version of Mbed-os are you using (tag or sha) ?

mbed-os-6.6.0

What version(s) of tools are you using. List all that apply (E.g. mbed-cli)

mbed-cli (version: 1.10.4)

How is this defect reproduced ?

always

CamelShoko commented 2 years ago

CODE:

KVDB::KVDB(PinName mosi, PinName miso, PinName sclk, PinName csel, PinName power, int freq, const char *prefix)
    : _bd(mosi, miso, sclk, csel, freq),
      _power_ctrl(power, PIN_OUTPUT, OpenDrainNoPull, SPIF_POWER_CTRL_ON),
      _tdbstore(&_bd),
      _prefix(prefix)
{
    _initialize = false;
    rtos::Kernel::Clock::time_point begin = Kernel::Clock::now();
    lock();
    int res = _tdbstore.init();
    if (res != MBED_SUCCESS) {
        tr_error("TDBStore:%s:%u: init [%d]", __FUNCTION__, __LINE__, res);
    }
    _initialize = true;
    unlock();
    milliseconds elapsed = Kernel::Clock::now() - begin;
    tr_warn("TDBStore:%s:%u: block_device bringup elapsed [%d] ms", __FUNCTION__, __LINE__, int(elapsed.count()));
}

LOG:

[2021-10-29T06:54:21.371Z][1635490461371ms][INFO][SFDP]: Density bits: 134217727, device size: 16777216 bytes [2021-10-29T06:54:37.158Z][1635490477158ms][WARN][KVDB]: TDBStore:KVDB:23: block_device bringup elapsed [15789] ms

CamelShoko commented 2 years ago

This test under too much key created.

numbers of key: 450

The keys create with function: int TDBStore::set(const char key, const void buffer, size_t size, uint32_t create_flags)

0xc0170 commented 2 years ago

I can't find any timing related information but what would be your expectation this operation should take? What operation takes actually the most time within init() method? Is it ret = build_ram_table(); ?

Some details for initialization are in the design document located at storage/docs/TDBStore/TDBStore_design.md

CamelShoko commented 2 years ago

i have a lot of hardware boards, only two boards have it.

0xc0170 commented 2 years ago

I don't follow. The information you provided are not enough to understand what is the problem - anyone being able to help would need more information of the isssue - is one target slower than other or it takes generally too long (what actually is taking that long, if it's real issue or it is designed that way or we will need to do optimizations).

CamelShoko commented 2 years ago

You can see my code and log. This long time the log show 15789 ms elapsed the _tdbstore.init() do. This phenomenon only on 2 boards, i have test it on 10 boards.

mcuxmx commented 2 years ago

I had the same problem, I made the following changes and the speed improved a lot.

https://github.com/ARMmbed/mbed-os/blob/b5e7dd9d32a21a1f68e84ca7837b0975209b0cff/storage/blockdevice/COMPONENT_SPIF/source/SPIFBlockDevice.cpp#L490-L493

#if 0
   // Read Data
    for (bd_size_t i = 0; i < size; i++) {
        buffer[i] = _spi.write(0);
    }
#else
   _spi.write( NULL, 0, (char *)buffer, size );
#endif
CamelShoko commented 2 years ago

I had the same problem, I made the following changes and the speed improved a lot.

https://github.com/ARMmbed/mbed-os/blob/b5e7dd9d32a21a1f68e84ca7837b0975209b0cff/storage/blockdevice/COMPONENT_SPIF/source/SPIFBlockDevice.cpp#L490-L493

#if 0
   // Read Data
    for (bd_size_t i = 0; i < size; i++) {
        buffer[i] = _spi.write(0);
    }
#else
   _spi.write( NULL, 0, (char *)buffer, size );
#endif

oh i will test it, thankyou very much.