Skip to content

Commit 2fca90b

Browse files
authored
WasmFS: Use emscripten_date_now for time computations (#20014)
That gives millisecond precision, which I think is probably more than enough, and as the test updates show, it reduces code size.
1 parent 353e904 commit 2fca90b

13 files changed

+49
-47
lines changed

system/lib/standalone/standalone.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,23 @@ int emscripten_resize_heap(size_t size) {
172172
return 0;
173173
}
174174

175-
weak double emscripten_get_now(void) {
175+
// Call clock_gettime with a particular clock and return the result in ms.
176+
static double clock_gettime_ms(clockid_t clock) {
176177
struct timespec ts;
177-
if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
178+
if (clock_gettime(clock, &ts)) {
178179
return 0;
179180
}
180-
// emscripten_get_now returns time in milliseconds (as a double)
181181
return (double)ts.tv_sec * 1000 + (double)ts.tv_nsec / 1000000;
182182
}
183183

184+
weak double emscripten_get_now(void) {
185+
return clock_gettime_ms(CLOCK_MONOTONIC);
186+
}
187+
188+
weak double emscripten_date_now(void) {
189+
return clock_gettime_ms(CLOCK_REALTIME);
190+
}
191+
184192
// C++ ABI
185193

186194
#if EMSCRIPTEN_NOCATCH

system/lib/wasmfs/file.h

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
#include "support.h"
1313
#include <assert.h>
14+
#include <emscripten.h>
1415
#include <emscripten/html5.h>
1516
#include <map>
1617
#include <mutex>
1718
#include <optional>
1819
#include <sys/stat.h>
19-
#include <time.h>
2020
#include <variant>
2121
#include <vector>
2222
#include <wasi/api.h>
@@ -97,9 +97,7 @@ class File : public std::enable_shared_from_this<File> {
9797
protected:
9898
File(FileKind kind, mode_t mode, backend_t backend)
9999
: kind(kind), mode(mode), backend(backend) {
100-
struct timespec ts;
101-
clock_gettime(CLOCK_REALTIME, &ts);
102-
atime = mtime = ctime = ts;
100+
atime = mtime = ctime = emscripten_date_now();
103101
}
104102

105103
// A mutex is needed for multiple accesses to the same file.
@@ -111,9 +109,9 @@ class File : public std::enable_shared_from_this<File> {
111109

112110
mode_t mode = 0; // User and group mode bits for access permission.
113111

114-
struct timespec atime; // Time when the content was last accessed.
115-
struct timespec mtime; // Time when the file content was last modified.
116-
struct timespec ctime; // Time when the file node was last modified.
112+
double atime; // Time when the content was last accessed, in ms.
113+
double mtime; // Time when the file content was last modified, in ms.
114+
double ctime; // Time when the file node was last modified, in ms.
117115

118116
// Reference to parent of current file node. This can be used to
119117
// traverse up the directory tree. A weak_ptr ensures that the ref
@@ -317,35 +315,29 @@ class File::Handle {
317315
// directory, for example).
318316
file->mode = (file->mode & S_IFMT) | (mode & ~S_IFMT);
319317
}
320-
struct timespec getCTime() {
318+
double getCTime() {
321319
return file->ctime;
322320
}
323-
void setCTime(struct timespec time) { file->ctime = time; }
321+
void setCTime(double time) { file->ctime = time; }
324322
// updateCTime() updates the ctime to the current time.
325323
void updateCTime() {
326-
struct timespec ts;
327-
clock_gettime(CLOCK_REALTIME, &ts);
328-
file->ctime = ts;
324+
file->ctime = emscripten_date_now();
329325
}
330-
struct timespec getMTime() {
326+
double getMTime() {
331327
return file->mtime;
332328
}
333-
void setMTime(struct timespec time) { file->mtime = time; }
329+
void setMTime(double time) { file->mtime = time; }
334330
// updateMTime() updates the mtime to the current time.
335331
void updateMTime() {
336-
struct timespec ts;
337-
clock_gettime(CLOCK_REALTIME, &ts);
338-
file->mtime = ts;
332+
file->mtime = emscripten_date_now();
339333
}
340-
struct timespec getATime() {
334+
double getATime() {
341335
return file->atime;
342336
}
343-
void setATime(struct timespec time) { file->atime = time; }
337+
void setATime(double time) { file->atime = time; }
344338
// updateATime() updates the atime to the current time.
345339
void updateATime() {
346-
struct timespec ts;
347-
clock_gettime(CLOCK_REALTIME, &ts);
348-
file->atime = ts;
340+
file->atime = emscripten_date_now();
349341
}
350342

351343
// Note: parent.lock() creates a new shared_ptr to the same Directory

system/lib/wasmfs/syscalls.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <sys/stat.h>
2323
#include <sys/statfs.h>
2424
#include <syscall_arch.h>
25-
#include <time.h>
2625
#include <unistd.h>
2726
#include <utility>
2827
#include <vector>
@@ -346,6 +345,18 @@ backend_t wasmfs_get_backend_by_path(const char* path) {
346345
return parsed.getFile()->getBackend();
347346
}
348347

348+
static timespec ms_to_timespec(double ms) {
349+
long long seconds = ms / 1000;
350+
timespec ts;
351+
ts.tv_sec = seconds; // seconds
352+
ts.tv_nsec = (ms - (seconds * 1000)) * 1000 * 1000; // nanoseconds
353+
return ts;
354+
}
355+
356+
static double timespec_to_ms(timespec ts) {
357+
return double(ts.tv_sec) * 1000 + double(ts.tv_nsec) / (1000 * 1000);
358+
}
359+
349360
int __syscall_newfstatat(int dirfd, intptr_t path, intptr_t buf, int flags) {
350361
// Only accept valid flags.
351362
if (flags & ~(AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW)) {
@@ -386,9 +397,9 @@ int __syscall_newfstatat(int dirfd, intptr_t path, intptr_t buf, int flags) {
386397
buffer->st_blocks = (buffer->st_size + 511) / 512;
387398
// Specifies the preferred blocksize for efficient disk I/O.
388399
buffer->st_blksize = 4096;
389-
buffer->st_atim = lockedFile.getATime();
390-
buffer->st_mtim = lockedFile.getMTime();
391-
buffer->st_ctim = lockedFile.getCTime();
400+
buffer->st_atim = ms_to_timespec(lockedFile.getATime());
401+
buffer->st_mtim = ms_to_timespec(lockedFile.getMTime());
402+
buffer->st_ctim = ms_to_timespec(lockedFile.getCTime());
392403
return __WASI_ERRNO_SUCCESS;
393404
}
394405

@@ -1135,16 +1146,13 @@ int __syscall_utimensat(int dirFD, intptr_t path_, intptr_t times_, int flags) {
11351146

11361147
// TODO: Handle tv_nsec being UTIME_NOW or UTIME_OMIT.
11371148
// TODO: Check for write access to the file (see man page for specifics).
1138-
struct timespec aTime, mTime;
1149+
double aTime, mTime;
11391150

11401151
if (times == NULL) {
1141-
struct timespec ts;
1142-
clock_gettime(CLOCK_REALTIME, &ts);
1143-
aTime = ts;
1144-
mTime = ts;
1152+
aTime = mTime = emscripten_date_now();
11451153
} else {
1146-
aTime = times[0];
1147-
mTime = times[1];
1154+
aTime = timespec_to_ms(times[0]);
1155+
mTime = timespec_to_ms(times[1]);
11481156
}
11491157

11501158
auto locked = parsed.getFile()->locked();

test/other/metadce/test_metadce_cxx_wasmfs.imports

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
env.__cxa_throw
2-
env._emscripten_get_now_is_monotonic
32
env._wasmfs_copy_preloaded_file_data
43
env._wasmfs_get_num_preloaded_dirs
54
env._wasmfs_get_num_preloaded_files
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
12559
1+
12478

test/other/metadce/test_metadce_cxx_wasmfs.sent

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
__cxa_throw
2-
_emscripten_get_now_is_monotonic
32
_wasmfs_copy_preloaded_file_data
43
_wasmfs_get_num_preloaded_dirs
54
_wasmfs_get_num_preloaded_files
@@ -12,7 +11,6 @@ _wasmfs_stdin_get_char
1211
abort
1312
emscripten_date_now
1413
emscripten_err
15-
emscripten_get_now
1614
emscripten_memcpy_big
1715
emscripten_out
1816
emscripten_resize_heap
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
164404
1+
163645
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1+
q
12
r
23
s
34
t
4-
u

test/other/metadce/test_metadce_files_wasmfs.funcs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
$__clock_gettime
21
$__cxx_global_array_dtor
32
$__cxx_global_array_dtor.1
43
$__cxx_global_array_dtor.2

test/other/metadce/test_metadce_files_wasmfs.imports

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,3 @@ a.m
1414
a.n
1515
a.o
1616
a.p
17-
a.q
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7116
1+
7107

test/other/metadce/test_metadce_files_wasmfs.sent

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,3 @@ m
1414
n
1515
o
1616
p
17-
q
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
52326
1+
51645

0 commit comments

Comments
 (0)