Skip to content

Commit

Permalink
fix FDB_ALIGN on 64bit platoform
Browse files Browse the repository at this point in the history
  • Loading branch information
pikasTech committed Jul 2, 2024
1 parent 3e1cf4d commit a241e7b
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 12 deletions.
6 changes: 5 additions & 1 deletion examples/flashdb/flashdb_tsdb1.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

tsdb = flashdb.TSDB("env", DB_PATH, max_len=512)

for i in range(100):
tic = time.time() * 1000
for i in range(10):
blob_i = struct.pack('i', i)
time.sleep(0.001)
ret = tsdb.tsl_append(blob_i)

toc = time.time() * 1000
assert ret == 0


Expand All @@ -25,5 +27,7 @@ def callback(tsl, user_data) -> int:


assert tsdb.tsl_iter(callback, 'user_data') == 0
assert tsdb.tsl_iter_reverse(callback, 'user_data_reverse') == 0
assert tsdb.tsl_iter_by_time(tic, toc, callback, 'user_data_by_time') == 0

print('PASS')
3 changes: 3 additions & 0 deletions package/flashdb/_flashdb.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class TSDB:

def tsl_append(self, blob: any) -> int: ...
def tsl_iter(self, callback: any, user_data: any) -> int: ...
def tsl_iter_reverse(self, callback: any, user_data: any) -> int: ...
def tsl_iter_by_time(self, from_time: int64, to_time: int64,
callback: any, user_data: any) -> int: ...


class TSL:
Expand Down
37 changes: 35 additions & 2 deletions package/flashdb/_flashdb_TSDB.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static void unlock(fdb_db_t db)
}
*/
static fdb_time_t get_time(void) {
// ms to s
// ns to ms
return pika_platform_get_tick() / 1000;
}
#endif
Expand Down Expand Up @@ -242,14 +242,47 @@ pika_bool _flashdb_TSL_iter_callback(fdb_tsl_t tsl, void* arg) {
return res;
}

static int _TSDB_iter(PikaObj* self,
Arg* callback,
Arg* user_data,
pika_bool is_reverse) {
fdb_tsdb_t tsdb = _OBJ2TSDB(self);
tsdb_foreach_context context = {
.callback = callback,
.user_data = user_data,
.tsdb = tsdb,
};
if (is_reverse) {
fdb_tsl_iter_reverse(tsdb, _flashdb_TSL_iter_callback, &context);
} else {
fdb_tsl_iter(tsdb, _flashdb_TSL_iter_callback, &context);
}
return 0;
}

int _flashdb_TSDB_tsl_iter(PikaObj* self, Arg* callback, Arg* user_data) {
return _TSDB_iter(self, callback, user_data, pika_false);
}

int _flashdb_TSDB_tsl_iter_reverse(PikaObj* self,
Arg* callback,
Arg* user_data) {
return _TSDB_iter(self, callback, user_data, pika_true);
}

int _flashdb_TSDB_tsl_iter_by_time(PikaObj* self,
int64_t from_time,
int64_t to_time,
Arg* callback,
Arg* user_data) {
fdb_tsdb_t tsdb = _OBJ2TSDB(self);
tsdb_foreach_context context = {
.callback = callback,
.user_data = user_data,
.tsdb = tsdb,
};
fdb_tsl_iter(tsdb, _flashdb_TSL_iter_callback, &context);
fdb_tsl_iter_by_time(tsdb, from_time, to_time, _flashdb_TSL_iter_callback,
&context);
return 0;
}

Expand Down
3 changes: 2 additions & 1 deletion package/flashdb/fdb_low_lvl.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
/* Return the most contiguous size aligned at specified width. RT_ALIGN(13, 4)
* would return 16.
*/
#define FDB_ALIGN(size, align) (((size) + (align)-1) & ~((align)-1))

/* align by write granularity */
#define FDB_WG_ALIGN(size) (FDB_ALIGN(size, (FDB_WRITE_GRAN + 7) / 8))
#define FDB_ALIGN align_by // fix ALIGN issue on 64bit platform
/**
* Return the down number of aligned at specified width. RT_ALIGN_DOWN(13, 4)
* would return 12.
Expand Down
2 changes: 1 addition & 1 deletion package/flashdb/fdb_tsdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

#include <inttypes.h>
#include <string.h>
#include "flashdb.h"
#include "fdb_low_lvl.h"
#include "flashdb.h"

#define FDB_LOG_TAG "[tsl]"
/* rewrite log prefix */
Expand Down
7 changes: 7 additions & 0 deletions package/flashdb/flashdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ def __init__(self, name: str, path: str, max_len: int = 1024,
user_data=None):
super().__init__(name, path, max_len, user_data)

def tsl_iter_by_time(self, from_time, to_time,
callback: any, user_data: any) -> int:
print('tsl_iter_by_time')
print('from_time:', from_time)
print('to_time:', to_time)
return super().tsl_iter_by_time(from_time, to_time, callback, user_data)


class TSL(_flashdb.TSL):
pass
3 changes: 2 additions & 1 deletion port/linux/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@
"_fuzzypid_rulebase.h": "c",
"fuzzy_pid.h": "c",
"_fuzzypid_piddirect.h": "c",
"pika_lvgl_lv_event.h": "c"
"pika_lvgl_lv_event.h": "c",
"fdb_cfg.h": "c"
},
"python.formatting.provider": "none",
"clangd.arguments": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
/* Return the most contiguous size aligned at specified width. RT_ALIGN(13, 4)
* would return 16.
*/
#define FDB_ALIGN(size, align) (((size) + (align)-1) & ~((align)-1))

/* align by write granularity */
#define FDB_WG_ALIGN(size) (FDB_ALIGN(size, (FDB_WRITE_GRAN + 7) / 8))
#define FDB_ALIGN align_by // fix ALIGN issue on 64bit platform
/**
* Return the down number of aligned at specified width. RT_ALIGN_DOWN(13, 4)
* would return 12.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

#include <inttypes.h>
#include <string.h>
#include "flashdb.h"
#include "fdb_low_lvl.h"
#include "flashdb.h"

#define FDB_LOG_TAG "[tsl]"
/* rewrite log prefix */
Expand Down Expand Up @@ -622,7 +622,6 @@ static int search_start_tsl_addr(fdb_tsdb_t db,
fdb_time_t to) {
struct fdb_tsl tsl;
while (pika_true) {
size_t align = LOG_IDX_DATA_SIZE;
tsl.addr.index =
start + FDB_ALIGN((end - start) / 2, LOG_IDX_DATA_SIZE);
read_tsl(db, &tsl);
Expand Down
2 changes: 1 addition & 1 deletion src/PikaVersion.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
#define PIKA_VERSION_MINOR 13
#define PIKA_VERSION_MICRO 3

#define PIKA_EDIT_TIME "2024/07/02 19:47:25"
#define PIKA_EDIT_TIME "2024/07/03 00:31:02"
4 changes: 2 additions & 2 deletions src/dataMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ struct Pool{
};
/* clang-format on */

#define align_by(size, aline) \
(((size) == 0) ? 0 : (((size)-1) / (aline) + 1) * (aline))
#define align_by(size, align) \
(((size) == 0) ? 0 : (((size)-1) / (align) + 1) * (align))

void pikaFree(void* mem, uint32_t size);
void* pikaMalloc(uint32_t size);
Expand Down

0 comments on commit a241e7b

Please sign in to comment.