diff --git a/bench/benchctxswitch.cpp b/bench/benchctxswitch.cpp index 18d2c7ed..20089987 100644 --- a/bench/benchctxswitch.cpp +++ b/bench/benchctxswitch.cpp @@ -24,6 +24,17 @@ #include #include #include +#include +#include + +static int uptime(uintmax_t* usecs) +{ + struct timespec uptime; + if ( clock_gettime(CLOCK_BOOT, &uptime) < 0 ) + return -1; + *usecs = uptime.tv_sec * 1000000ULL + uptime.tv_nsec / 1000ULL; + return 0; +} int main(int /*argc*/, char* /*argv*/[]) { diff --git a/bench/benchsyscall.cpp b/bench/benchsyscall.cpp index 0d161b7f..df95d1d9 100644 --- a/bench/benchsyscall.cpp +++ b/bench/benchsyscall.cpp @@ -23,6 +23,16 @@ #include #include +#include + +static int uptime(uintmax_t* usecs) +{ + struct timespec uptime; + if ( clock_gettime(CLOCK_BOOT, &uptime) < 0 ) + return -1; + *usecs = uptime.tv_sec * 1000000ULL + uptime.tv_nsec / 1000ULL; + return 0; +} int main(int /*argc*/, char* /*argv*/[]) { diff --git a/games/asteroids.cpp b/games/asteroids.cpp index b4b26f65..f3151eb2 100644 --- a/games/asteroids.cpp +++ b/games/asteroids.cpp @@ -32,6 +32,8 @@ #include #include #include +#include +#include #include @@ -995,16 +997,16 @@ void Firework::Think(float deltatime) } } -uintmax_t lastframeat; +struct timespec lastframeat; void GameLogic() { - uintmax_t now; - do uptime(&now); - while ( now == lastframeat); - unsigned long deltausecs = now - lastframeat; + struct timespec now; + do clock_gettime(CLOCK_MONOTONIC, &now); + while ( timespec_eq(now, lastframeat) ); + struct timespec delta = timespec_sub(now, lastframeat); + float deltatime = delta.tv_sec + delta.tv_nsec / 1E9f; lastframeat = now; - float deltatime = deltausecs / 1000000.0f; float timescale = 3.0; deltatime *= timescale; Object* first = firstobject; @@ -1106,7 +1108,7 @@ int atoi_safe(const char* str) void InitGame() { - uptime(&lastframeat); + clock_gettime(CLOCK_MONOTONIC, &lastframeat); GenerateStarfield(starfield, STARFIELD_WIDTH, STARFIELD_HEIGHT); playership = new Spaceship(0.0, Vector(0, 0), Vector(4.0f, 0)); new AsteroidField; diff --git a/libc/Makefile b/libc/Makefile index ae7b8272..f9dfe5cd 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -372,7 +372,6 @@ ttyname.o \ umask.o \ unlinkat.o \ unlink.o \ -uptime.o \ usleep.o \ utimensat.o \ utimens.o \ diff --git a/libc/include/unistd.h b/libc/include/unistd.h index fe64f5df..e96855b9 100644 --- a/libc/include/unistd.h +++ b/libc/include/unistd.h @@ -372,7 +372,6 @@ size_t readall(int fd, void* buf, size_t count); size_t readleast(int fd, void* buf, size_t least, size_t max); pid_t sfork(int flags); pid_t tfork(int flags, tforkregs_t* regs); -int uptime(__uintmax_t* usecssinceboot); size_t writeall(int fd, const void* buf, size_t count); size_t writeleast(int fd, const void* buf, size_t least, size_t max); #endif diff --git a/libc/sys/time/gettimeofday.cpp b/libc/sys/time/gettimeofday.cpp index 14875c49..fd3feb87 100644 --- a/libc/sys/time/gettimeofday.cpp +++ b/libc/sys/time/gettimeofday.cpp @@ -29,8 +29,7 @@ extern "C" int gettimeofday(struct timeval* tp, void* /*tzp*/) { struct timespec now; - // TODO: We should be using CLOCK_REALTIME. - if ( clock_gettime(CLOCK_MONOTONIC, &now) < 0 ) + if ( clock_gettime(CLOCK_REALTIME, &now) < 0 ) return -1; tp->tv_sec = now.tv_sec; tp->tv_usec = now.tv_nsec / 1000; diff --git a/libc/uptime.cpp b/libc/uptime.cpp deleted file mode 100644 index 66f5c22a..00000000 --- a/libc/uptime.cpp +++ /dev/null @@ -1,34 +0,0 @@ -/******************************************************************************* - - Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. - - This file is part of the Sortix C Library. - - The Sortix C Library is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or (at your - option) any later version. - - The Sortix C Library is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public - License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with the Sortix C Library. If not, see . - - uptime.cpp - Returns current system uptime. - -*******************************************************************************/ - -#include -#include -#include - -DEFN_SYSCALL1(int, sys_uptime, SYSCALL_UPTIME, uintmax_t*); - -extern "C" int uptime(uintmax_t* usecssinceboot) -{ - return sys_uptime(usecssinceboot); -} diff --git a/utils/uptime.cpp b/utils/uptime.cpp index 52d1947e..a613005a 100644 --- a/utils/uptime.cpp +++ b/utils/uptime.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2011. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free @@ -20,8 +20,12 @@ *******************************************************************************/ +#include +#include #include +#include #include +#include size_t Seconds(uintmax_t usecs) { @@ -54,9 +58,12 @@ void PrintElement(size_t num, const char* single, const char* multiple) int main(int /*argc*/, char* /*argv*/[]) { - uintmax_t usecssinceboot; - if ( uptime(&usecssinceboot) ) { perror("uptime"); return 1; } + struct timespec uptime; + if ( clock_gettime(CLOCK_BOOT, &uptime) < 0 ) + error(1, errno, "clock_gettime(CLOCK_BOOT)"); + uintmax_t usecssinceboot = uptime.tv_sec * 1000000ULL + + uptime.tv_nsec / 1000ULL; PrintElement(Days(usecssinceboot), "day", "days"); PrintElement(Hours(usecssinceboot), "hour", "hours"); PrintElement(Minutes(usecssinceboot), "min", "mins");