Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to 64bit timestamps in libc #16401

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 9 additions & 20 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,21 +397,6 @@ LibraryManager.library = {
return ((Date.now() - _clock.start) * ({{{ cDefine('CLOCKS_PER_SEC') }}} / 1000))|0;
},

time__sig: 'ii',
time: function(ptr) {
{{{ from64('ptr') }}};
var ret = (Date.now()/1000)|0;
if (ptr) {
{{{ makeSetValue('ptr', 0, 'ret', 'i32') }}};
}
return ret;
},

difftime__sig: 'dii',
difftime: function(time1, time0) {
return time1 - time0;
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we can land the moving of these two functions to native code separately, before landing this PR.

The difftime change looks like a no-brainer.. the time change probably is too, but could effect code size.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The time change is responsible for the 2/3% code size regression on the test-other test suite.

I am very tempted to remove most of the date stuff from the JS side, and just have a single _time_js function which just returns Date.now() as a double.

This should help with bundle size and performance, because we won't have to pass i64s on the wasm/js boundary anymore

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, this was what I was worried about with increasing the width of time_t. Any refactors that move stuff into native code without regressing size sounds good to me.

Can I recommend a PR that just moves time() and timediff() first.. rather than trying to make one big refactor.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I am going to do that

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tooks inspiration from your suggestions and created a PR to move a whole bunch of these functions into native code: #16439. I'm not sure if here is still work to do to avoid passing time_t values but this seems like a good improvment.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry for the (big) delay in my response.
I was working on something very similar, but didn't have time to finish
This is great! I will continue my work on this PR asap


_mktime_js__sig: 'ii',
_mktime_js: function(tmPtr) {
var date = new Date({{{ makeGetValue('tmPtr', C_STRUCTS.tm.tm_year, 'i32') }}} + 1900,
Expand Down Expand Up @@ -1275,7 +1260,8 @@ LibraryManager.library = {
setErrNo({{{ cDefine('EINVAL') }}});
return -1;
}
{{{ makeSetValue('tp', C_STRUCTS.timespec.tv_sec, '(now/1000)|0', 'i32') }}}; // seconds
// struct timespec { time_t tv_sec; long tv_nsec; };
{{{ makeSetValue('tp', C_STRUCTS.timespec.tv_sec, 'Math.floor(now/1000)', 'i64') }}}; // seconds
{{{ makeSetValue('tp', C_STRUCTS.timespec.tv_nsec, '((now % 1000)*1000*1000)|0', 'i32') }}}; // nanoseconds
return 0;
},
Expand All @@ -1293,16 +1279,18 @@ LibraryManager.library = {
setErrNo({{{ cDefine('EINVAL') }}});
return -1;
}
{{{ makeSetValue('res', C_STRUCTS.timespec.tv_sec, '(nsec/1000000000)|0', 'i32') }}};
// struct timespec { time_t tv_sec; long tv_nsec; };
{{{ makeSetValue('res', C_STRUCTS.timespec.tv_sec, 'Math.floor(nsec/1000000000)', 'i64') }}};
{{{ makeSetValue('res', C_STRUCTS.timespec.tv_nsec, 'nsec', 'i32') }}} // resolution is nanoseconds
return 0;
},
gettimeofday__sig: 'iii',
// http://pubs.opengroup.org/onlinepubs/000095399/basedefs/sys/time.h.html
gettimeofday: function(ptr) {
var now = Date.now();
{{{ makeSetValue('ptr', C_STRUCTS.timeval.tv_sec, '(now/1000)|0', 'i32') }}}; // seconds
{{{ makeSetValue('ptr', C_STRUCTS.timeval.tv_usec, '((now % 1000)*1000)|0', 'i32') }}}; // microseconds
// struct timeval { time_t tv_sec; suseconds_t tv_usec; };
{{{ makeSetValue('ptr', C_STRUCTS.timeval.tv_sec, 'Math.floor(now/1000)', 'i64') }}}; // seconds
{{{ makeSetValue('ptr', C_STRUCTS.timeval.tv_usec, 'Math.floor((now % 1000)*1000)', 'i64') }}}; // microseconds
return 0;
},

Expand All @@ -1312,7 +1300,8 @@ LibraryManager.library = {

ftime: function(p) {
var millis = Date.now();
{{{ makeSetValue('p', C_STRUCTS.timeb.time, '(millis/1000)|0', 'i32') }}};
// struct timeb { time_t time; unsigned short millitm; short timezone; short dstflag; };
{{{ makeSetValue('p', C_STRUCTS.timeb.time, 'Math.floor(millis/1000)', 'i64') }}};
{{{ makeSetValue('p', C_STRUCTS.timeb.millitm, 'millis % 1000', 'i16') }}};
{{{ makeSetValue('p', C_STRUCTS.timeb.timezone, '0', 'i16') }}}; // Obsolete field
{{{ makeSetValue('p', C_STRUCTS.timeb.dstflag, '0', 'i16') }}}; // Obsolete field
Expand Down
6 changes: 3 additions & 3 deletions src/library_syscall.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ var SyscallsLibrary = {
{{{ makeSetValue('buf', C_STRUCTS.stat.st_size, 'stat.size', 'i64') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_blksize, '4096', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_blocks, 'stat.blocks', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_atim.tv_sec, '(stat.atime.getTime() / 1000)|0', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_atim.tv_sec, 'Math.floor(stat.atime.getTime() / 1000)', 'i64') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_atim.tv_nsec, '0', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_mtim.tv_sec, '(stat.mtime.getTime() / 1000)|0', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_mtim.tv_sec, 'Math.floor(stat.mtime.getTime() / 1000)', 'i64') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_mtim.tv_nsec, '0', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ctim.tv_sec, '(stat.ctime.getTime() / 1000)|0', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ctim.tv_sec, 'Math.floor(stat.ctime.getTime() / 1000)', 'i64') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ctim.tv_nsec, '0', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ino, 'stat.ino', 'i64') }}};
return 0;
Expand Down
2 changes: 0 additions & 2 deletions src/preamble_minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ function abort(what) {
throw {{{ ASSERTIONS ? 'new Error(what)' : 'what' }}};
}

#if SAFE_HEAP
// Globals used by JS i64 conversions (see makeSetValue)
var tempDouble;
var tempI64;
#endif

var tempRet0 = 0;
var setTempRet0 = (value) => { tempRet0 = value };
Expand Down
4 changes: 2 additions & 2 deletions system/lib/libc/musl/arch/emscripten/bits/alltypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ typedef long double double_t;
#endif

#if defined(__NEED_time_t) && !defined(__DEFINED_time_t)
typedef long time_t;
typedef long long time_t;
#define __DEFINED_time_t
#endif

#if defined(__NEED_suseconds_t) && !defined(__DEFINED_suseconds_t)
typedef long suseconds_t;
typedef long long suseconds_t;
#define __DEFINED_suseconds_t
#endif

Expand Down
5 changes: 3 additions & 2 deletions system/lib/libc/musl/arch/emscripten/bits/alltypes.h.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#define _REDIR_TIME64 1
#define _Addr __PTRDIFF_TYPE__
#define _Int64 __INT64_TYPE__
#define _Reg __PTRDIFF_TYPE__
Expand All @@ -14,8 +15,8 @@ TYPEDEF float float_t;
TYPEDEF double double_t;

TYPEDEF struct { long long __ll; long double __ld; } max_align_t;
TYPEDEF long time_t;
TYPEDEF long suseconds_t;
TYPEDEF long long time_t;
TYPEDEF long long suseconds_t;

TYPEDEF struct { union { int __i[10]; unsigned __s[10]; } __u; } pthread_attr_t;
TYPEDEF struct { union { int __i[7]; void *__p[7]; } __u; } pthread_mutex_t;
Expand Down
40 changes: 20 additions & 20 deletions tests/reference_struct_info.json
Original file line number Diff line number Diff line change
Expand Up @@ -1392,30 +1392,30 @@
"sin6_scope_id": 24
},
"stat": {
"__size__": 88,
"__size__": 112,
"__st_dev_padding": 4,
"__st_ino_truncated": 8,
"__st_rdev_padding": 32,
"st_atim": {
"__size__": 8,
"tv_nsec": 60,
"__size__": 16,
"tv_nsec": 64,
"tv_sec": 56
},
"st_blksize": 48,
"st_blocks": 52,
"st_ctim": {
"__size__": 8,
"tv_nsec": 76,
"tv_sec": 72
"__size__": 16,
"tv_nsec": 96,
"tv_sec": 88
},
"st_dev": 0,
"st_gid": 24,
"st_ino": 80,
"st_ino": 104,
"st_mode": 12,
"st_mtim": {
"__size__": 8,
"tv_nsec": 68,
"tv_sec": 64
"__size__": 16,
"tv_nsec": 80,
"tv_sec": 72
},
"st_nlink": 16,
"st_rdev": 28,
Expand Down Expand Up @@ -1443,21 +1443,21 @@
"timeSpentInStatus": 16
},
"timeb": {
"__size__": 12,
"dstflag": 8,
"millitm": 4,
"__size__": 16,
"dstflag": 12,
"millitm": 8,
"time": 0,
"timezone": 6
"timezone": 10
},
"timespec": {
"__size__": 8,
"tv_nsec": 4,
"__size__": 16,
"tv_nsec": 8,
"tv_sec": 0
},
"timeval": {
"__size__": 8,
"__size__": 16,
"tv_sec": 0,
"tv_usec": 4
"tv_usec": 8
},
"tm": {
"__size__": 44,
Expand All @@ -1474,9 +1474,9 @@
"tm_zone": 40
},
"utimbuf": {
"__size__": 8,
"__size__": 16,
"actime": 0,
"modtime": 4
"modtime": 8
}
}
}
6 changes: 3 additions & 3 deletions tools/system_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,8 @@ def get_files(self):
'ctime.c',
'gmtime.c',
'localtime.c',
'difftime.c',
'time.c',
'nanosleep.c',
'clock_nanosleep.c',
'ctime_r.c',
Expand Down Expand Up @@ -1600,13 +1602,11 @@ def get_files(self):
'__year_to_secs.c',
'clock.c',
'clock_gettime.c',
'difftime.c',
'gettimeofday.c',
'localtime_r.c',
'gmtime_r.c',
'mktime.c',
'timegm.c',
'time.c'])
'timegm.c'])
# It is more efficient to use JS for __assert_fail, as it avoids always
# including fprintf etc.
files += files_in_path(
Expand Down