Skip to content

fix: replace gettimeofday with clock_gettime#3

Merged
MaxXSoft merged 3 commits intopku-minic:masterfrom
hsqStephenZhang:master
Mar 27, 2026
Merged

fix: replace gettimeofday with clock_gettime#3
MaxXSoft merged 3 commits intopku-minic:masterfrom
hsqStephenZhang:master

Conversation

@hsqStephenZhang
Copy link
Copy Markdown
Contributor

compiler-dev's docker's perf output always shown "time elapsed: 0H-0M-0S-0us", which is caused by usage of gettimeofday

This PR aims at replacing gettimeofday with clock_gettime to make the perf work.

Comment thread src/nolibc/time.h Outdated
Comment on lines +14 to +16
#ifndef SYS_clock_gettime64
#define SYS_clock_gettime64 403
#endif
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This should be put to nolibc/sys.h, and make sure it works on x86-64/aarch64/riscv32/riscv64 + Linux/macOS.

Maybe we need a CI to ensure this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I've already added CI to this repo, hope it will work correctly after you update this PR.

Comment thread src/nolibc/time.c
Comment on lines +15 to +17
int do_clock_gettime64(int clk_id, struct timespec64 *tp) {
return SYSCALL2(SYS_clock_gettime64, clk_id, tp);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This can be merged into clock_gettime function.

Comment thread src/sysy.c Outdated
#else
#include <sys/time.h>
#include <unistd.h>
int clock_gettime(int clk_id, struct timespec *tp);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is there another clock_gettime declaration here? Is it generated by AI?

Comment thread src/nolibc/time.h Outdated
Comment on lines +23 to +24
// CLOCK_REALTIME: 0
// CLOCK_MONOTONIC: 1
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's best to define them as macros.

Comment thread src/nolibc/time.h Outdated
Comment on lines +19 to +20
int tv_sec;
int tv_nsec;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why use int here? They should be time_t and long: https://man7.org/linux/man-pages/man3/clock_gettime.3.html#DESCRIPTION

Comment thread src/nolibc/time.h
Comment on lines 7 to 12
struct timeval {
long tv_sec;
long tv_usec;
};

int gettimeofday(struct timeval *tv, void *tz);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we never use gettimeofday, we can remove it.

@MaxXSoft MaxXSoft force-pushed the master branch 3 times, most recently from ca8bb40 to 5b2bef4 Compare March 20, 2026 07:55
@hsqStephenZhang hsqStephenZhang force-pushed the master branch 6 times, most recently from a072406 to ff73079 Compare March 22, 2026 21:21
Comment thread src/nolibc/sys.h
#ifdef SYS_LINUX
#define CLOCK_REALTIME 0
#define CLOCK_MONOTONIC 1
#ifdef SYS_X86_64
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please move these to time.h, just as we did in io.h:

#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2

Comment thread src/nolibc/sys.h Outdated
#define SYS_WRITE 64
#define SYS_EXIT 93
#define SYS_GETTIMEOFDAY 169
#define SYS_CLOCK_GETTIME64 403
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please ensure that the definition of the SYS_* macros is consistent across all platforms.

I suggest you remove gettimeofday and use clock_gettime to implement starttime and stoptime (for all platforms), just like in your previous commit.

Copy link
Copy Markdown
Member

@MaxXSoft MaxXSoft left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution!

I previously had some misunderstandings about clock_gettime on macOS, mistakenly believing it could also be implemented using a system call, but that's not actually the case. Therefore, some of the feedback I provided in previous reviews was unreasonable, and I apologize for making you revise it so many times.

I will fix this part more consistently later.

@MaxXSoft MaxXSoft merged commit b171d9b into pku-minic:master Mar 27, 2026
11 checks passed
@hsqStephenZhang
Copy link
Copy Markdown
Contributor Author

Thank you for your contribution!

I previously had some misunderstandings about clock_gettime on macOS, mistakenly believing it could also be implemented using a system call, but that's not actually the case. Therefore, some of the feedback I provided in previous reviews was unreasonable, and I apologize for making you revise it so many times.

I will fix this part more consistently later.

It's totally ok, I appreciated your comments, my original implementation was not robust enough, thanks for pointing that out. The CI is also useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants