Obsolete uptime(2).

This commit is contained in:
Jonas 'Sortie' Termansen 2013-05-14 10:07:56 +02:00
parent 71a2fef5f6
commit a15ffa955b
8 changed files with 41 additions and 48 deletions

View File

@ -24,6 +24,17 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <signal.h> #include <signal.h>
#include <unistd.h>
#include <time.h>
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*/[]) int main(int /*argc*/, char* /*argv*/[])
{ {

View File

@ -23,6 +23,16 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <time.h>
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*/[]) int main(int /*argc*/, char* /*argv*/[])
{ {

View File

@ -32,6 +32,8 @@
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <error.h> #include <error.h>
#include <time.h>
#include <timespec.h>
#include <dispd.h> #include <dispd.h>
@ -995,16 +997,16 @@ void Firework::Think(float deltatime)
} }
} }
uintmax_t lastframeat; struct timespec lastframeat;
void GameLogic() void GameLogic()
{ {
uintmax_t now; struct timespec now;
do uptime(&now); do clock_gettime(CLOCK_MONOTONIC, &now);
while ( now == lastframeat); while ( timespec_eq(now, lastframeat) );
unsigned long deltausecs = now - lastframeat; struct timespec delta = timespec_sub(now, lastframeat);
float deltatime = delta.tv_sec + delta.tv_nsec / 1E9f;
lastframeat = now; lastframeat = now;
float deltatime = deltausecs / 1000000.0f;
float timescale = 3.0; float timescale = 3.0;
deltatime *= timescale; deltatime *= timescale;
Object* first = firstobject; Object* first = firstobject;
@ -1106,7 +1108,7 @@ int atoi_safe(const char* str)
void InitGame() void InitGame()
{ {
uptime(&lastframeat); clock_gettime(CLOCK_MONOTONIC, &lastframeat);
GenerateStarfield(starfield, STARFIELD_WIDTH, STARFIELD_HEIGHT); GenerateStarfield(starfield, STARFIELD_WIDTH, STARFIELD_HEIGHT);
playership = new Spaceship(0.0, Vector(0, 0), Vector(4.0f, 0)); playership = new Spaceship(0.0, Vector(0, 0), Vector(4.0f, 0));
new AsteroidField; new AsteroidField;

View File

@ -372,7 +372,6 @@ ttyname.o \
umask.o \ umask.o \
unlinkat.o \ unlinkat.o \
unlink.o \ unlink.o \
uptime.o \
usleep.o \ usleep.o \
utimensat.o \ utimensat.o \
utimens.o \ utimens.o \

View File

@ -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); size_t readleast(int fd, void* buf, size_t least, size_t max);
pid_t sfork(int flags); pid_t sfork(int flags);
pid_t tfork(int flags, tforkregs_t* regs); 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 writeall(int fd, const void* buf, size_t count);
size_t writeleast(int fd, const void* buf, size_t least, size_t max); size_t writeleast(int fd, const void* buf, size_t least, size_t max);
#endif #endif

View File

@ -29,8 +29,7 @@
extern "C" int gettimeofday(struct timeval* tp, void* /*tzp*/) extern "C" int gettimeofday(struct timeval* tp, void* /*tzp*/)
{ {
struct timespec now; struct timespec now;
// TODO: We should be using CLOCK_REALTIME. if ( clock_gettime(CLOCK_REALTIME, &now) < 0 )
if ( clock_gettime(CLOCK_MONOTONIC, &now) < 0 )
return -1; return -1;
tp->tv_sec = now.tv_sec; tp->tv_sec = now.tv_sec;
tp->tv_usec = now.tv_nsec / 1000; tp->tv_usec = now.tv_nsec / 1000;

View File

@ -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 <http://www.gnu.org/licenses/>.
uptime.cpp
Returns current system uptime.
*******************************************************************************/
#include <sys/syscall.h>
#include <stdint.h>
#include <unistd.h>
DEFN_SYSCALL1(int, sys_uptime, SYSCALL_UPTIME, uintmax_t*);
extern "C" int uptime(uintmax_t* usecssinceboot)
{
return sys_uptime(usecssinceboot);
}

View File

@ -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 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 under the terms of the GNU General Public License as published by the Free
@ -20,8 +20,12 @@
*******************************************************************************/ *******************************************************************************/
#include <errno.h>
#include <error.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h>
#include <unistd.h> #include <unistd.h>
#include <time.h>
size_t Seconds(uintmax_t usecs) 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*/[]) int main(int /*argc*/, char* /*argv*/[])
{ {
uintmax_t usecssinceboot; struct timespec uptime;
if ( uptime(&usecssinceboot) ) { perror("uptime"); return 1; } 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(Days(usecssinceboot), "day", "days");
PrintElement(Hours(usecssinceboot), "hour", "hours"); PrintElement(Hours(usecssinceboot), "hour", "hours");
PrintElement(Minutes(usecssinceboot), "min", "mins"); PrintElement(Minutes(usecssinceboot), "min", "mins");