Skip to content

Commit e2e83b6

Browse files
committed
Merge branch 'master' into feature/smp
Merge erlang:monotonic_time changes and timer test fix.
2 parents 52382aa + 0277324 commit e2e83b6

File tree

10 files changed

+105
-4
lines changed

10 files changed

+105
-4
lines changed

src/libAtomVM/nifs.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ static term nif_erlang_list_to_integer_1(Context *ctx, int argc, term argv[]);
120120
static term nif_erlang_list_to_float_1(Context *ctx, int argc, term argv[]);
121121
static term nif_erlang_list_to_atom_1(Context *ctx, int argc, term argv[]);
122122
static term nif_erlang_list_to_existing_atom_1(Context *ctx, int argc, term argv[]);
123+
static term nif_erlang_monotonic_time_1(Context *ctx, int argc, term argv[]);
123124
static term nif_erlang_iolist_size_1(Context *ctx, int argc, term argv[]);
124125
static term nif_erlang_iolist_to_binary_1(Context *ctx, int argc, term argv[]);
125126
static term nif_erlang_open_port_2(Context *ctx, int argc, term argv[]);
@@ -453,6 +454,12 @@ static const struct Nif concat_nif =
453454
.nif_ptr = nif_erlang_concat_2
454455
};
455456

457+
static const struct Nif monotonic_time_nif =
458+
{
459+
.base.type = NIFFunctionType,
460+
.nif_ptr = nif_erlang_monotonic_time_1
461+
};
462+
456463
static const struct Nif system_time_nif =
457464
{
458465
.base.type = NIFFunctionType,
@@ -1288,6 +1295,28 @@ term nif_erlang_make_ref_0(Context *ctx, int argc, term argv[])
12881295
return term_from_ref_ticks(ref_ticks, ctx);
12891296
}
12901297

1298+
term nif_erlang_monotonic_time_1(Context *ctx, int argc, term argv[])
1299+
{
1300+
UNUSED(ctx);
1301+
UNUSED(argc);
1302+
1303+
struct timespec ts;
1304+
sys_monotonic_time(&ts);
1305+
1306+
if (argv[0] == SECOND_ATOM) {
1307+
return make_maybe_boxed_int64(ctx, ts.tv_sec);
1308+
1309+
} else if (argv[0] == MILLISECOND_ATOM) {
1310+
return make_maybe_boxed_int64(ctx, ((int64_t) ts.tv_sec) * 1000 + ts.tv_nsec / 1000000);
1311+
1312+
} else if (argv[0] == MICROSECOND_ATOM) {
1313+
return make_maybe_boxed_int64(ctx, ((int64_t) ts.tv_sec) * 1000000 + ts.tv_nsec / 1000);
1314+
1315+
} else {
1316+
RAISE_ERROR(BADARG_ATOM);
1317+
}
1318+
}
1319+
12911320
term nif_erlang_system_time_1(Context *ctx, int argc, term argv[])
12921321
{
12931322
UNUSED(ctx);

src/libAtomVM/nifs.gperf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ erlang:system_info/1, &system_info_nif
8686
erlang:system_flag/2, &system_flag_nif
8787
erlang:whereis/1, &whereis_nif
8888
erlang:++/2, &concat_nif
89+
erlang:monotonic_time/1, &monotonic_time_nif
8990
erlang:system_time/1, &system_time_nif
9091
erlang:tuple_to_list/1, &tuple_to_list_nif
9192
erlang:universaltime/0, &universaltime_nif

src/libAtomVM/sys.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ void sys_signal(GlobalContext *glb);
8585
*/
8686
void sys_time(struct timespec *t);
8787

88+
/**
89+
* @brief gets monotonic time
90+
*
91+
* @details gets monotonic time.
92+
* @param t the timespec that will be updated.
93+
*/
94+
void sys_monotonic_time(struct timespec *t);
95+
8896
/**
8997
* @brief Loads a BEAM module using platform dependent methods.
9098
*

src/platforms/esp32/components/avm_sys/sys.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@ void sys_time(struct timespec *t)
189189
t->tv_nsec = tv.tv_usec * 1000;
190190
}
191191

192+
void sys_monotonic_time(struct timespec *t)
193+
{
194+
int64_t us_since_boot = esp_timer_get_time();
195+
196+
t->tv_sec = us_since_boot / 1000000;
197+
t->tv_nsec = us_since_boot * 1000;
198+
}
199+
192200
void sys_init_platform(GlobalContext *glb)
193201
{
194202
struct ESP32PlatformData *platform = malloc(sizeof(struct ESP32PlatformData));

src/platforms/generic_unix/lib/sys.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,14 @@ void sys_time(struct timespec *t)
330330
}
331331
}
332332

333+
void sys_monotonic_time(struct timespec *t)
334+
{
335+
if (UNLIKELY(clock_gettime(CLOCK_MONOTONIC, t))) {
336+
fprintf(stderr, "Failed clock_gettime.\n");
337+
AVM_ABORT();
338+
}
339+
}
340+
333341
Module *sys_load_module(GlobalContext *global, const char *module_name)
334342
{
335343
TRACE("sys_load_module: Going to load: %s\n", module_name);

src/platforms/stm32/src/lib/sys.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ void sys_time(struct timespec *t)
7777
sys_clock_gettime(t);
7878
}
7979

80+
void sys_monotonic_time(struct timespec *t)
81+
{
82+
sys_clock_gettime(t);
83+
}
84+
8085
uint64_t sys_millis(GlobalContext *glb)
8186
{
8287
UNUSED(glb);

tests/erlang_tests/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,8 @@ compile_erlang(test_refc_binaries)
428428
compile_erlang(test_sub_binaries)
429429
compile_erlang(bs_append_extra_words)
430430

431+
compile_erlang(test_monotonic_time)
432+
431433
compile_erlang(spawn_opt_monitor_normal)
432434
compile_erlang(spawn_opt_monitor_throw)
433435
compile_erlang(spawn_opt_demonitor_normal)
@@ -843,6 +845,8 @@ add_custom_target(erlang_test_modules DEPENDS
843845
bs_restore2_start_offset_no_fp.beam
844846
bs_append_extra_words.beam
845847

848+
test_monotonic_time.beam
849+
846850
spawn_opt_monitor_normal.beam
847851
spawn_opt_monitor_throw.beam
848852
spawn_opt_demonitor_normal.beam
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
%
2+
% This file is part of AtomVM.
3+
%
4+
% Copyright 2023 Davide Bettio <[email protected]>
5+
%
6+
% Licensed under the Apache License, Version 2.0 (the "License");
7+
% you may not use this file except in compliance with the License.
8+
% You may obtain a copy of the License at
9+
%
10+
% http://www.apache.org/licenses/LICENSE-2.0
11+
%
12+
% Unless required by applicable law or agreed to in writing, software
13+
% distributed under the License is distributed on an "AS IS" BASIS,
14+
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
% See the License for the specific language governing permissions and
16+
% limitations under the License.
17+
%
18+
% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
19+
%
20+
21+
-module(test_monotonic_time).
22+
23+
-export([start/0]).
24+
25+
start() ->
26+
T1 = erlang:monotonic_time(millisecond),
27+
receive
28+
after 1 -> ok
29+
end,
30+
T2 = erlang:monotonic_time(millisecond),
31+
test_diff(T2 - T1).
32+
33+
test_diff(X) when is_integer(X) andalso X >= 0 ->
34+
1;
35+
test_diff(X) when X < 0 ->
36+
0.

tests/libs/estdlib/test_timer.erl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ test() ->
3232
ok.
3333

3434
test_timer() ->
35-
T0 = erlang:system_time(millisecond),
35+
T0 = erlang:monotonic_time(millisecond),
3636
ok = timer:sleep(101),
37-
T1 = erlang:system_time(millisecond),
37+
T1 = erlang:monotonic_time(millisecond),
3838
ok = etest:assert_true((T1 - T0) >= 101),
3939
ok.
4040

@@ -59,9 +59,9 @@ timer_loop(0) ->
5959
{error, SomethingElse}
6060
end;
6161
timer_loop(I) ->
62-
T0 = erlang:system_time(millisecond),
62+
T0 = erlang:monotonic_time(millisecond),
6363
ok = timer:sleep(101),
64-
T1 = erlang:system_time(millisecond),
64+
T1 = erlang:monotonic_time(millisecond),
6565
ok = etest:assert_true((T1 - T0) >= 101),
6666
timer_loop(I - 1).
6767

tests/test.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,8 @@ struct Test tests[] = {
469469
TEST_CASE_COND(bs_restore2_start_offset, 823, SKIP_NO_FP),
470470
TEST_CASE_COND(bs_restore2_start_offset_no_fp, 823, SKIP_FP),
471471

472+
TEST_CASE_EXPECTED(test_monotonic_time, 1),
473+
472474
// Tests relying on echo driver
473475
TEST_CASE_ATOMVM_ONLY(pingpong, 1),
474476

0 commit comments

Comments
 (0)