core: Support time functions.

* lib/linux/clock_gettime.c: New file, move from gnu.c.
* lib/linux/gettimeofday.c: New file, move from tcc.c.
* lib/linux/time.c: New file, move from tcc.c.
* lib/linux/libc.c: Include them.
* lib/linux/gnu.c (clock_gettime): Remove.
* lib/linux/tcc.c (time, gettimeofday): Remove.
* include/time.h (CLOCK_PROCESS_CPUTIME_ID): New define.
* src/posix.c (init_time, current_time, gettimeofday_,
seconds_and_nanoseconds_to_long, get_internal_run_time): New function.
* src/mes.c (scm_symbol_internal_time_units_per_second): New symbol.
(main): Call init_time.
This commit is contained in:
Jan Nieuwenhuizen 2018-10-20 14:18:04 +02:00
parent 9b32098573
commit 37cba9c93b
No known key found for this signature in database
GPG Key ID: F3C1A0D9C1D65273
10 changed files with 141 additions and 21 deletions

View File

@ -54,6 +54,7 @@ struct timespec
#endif // __MES_STRUCT_TIMESPEC
#define CLOCK_PROCESS_CPUTIME_ID 2
int clock_gettime (clockid_t clk_id, struct timespec *tp);
struct tm *localtime (time_t const *timep);
struct tm *gmtime (time_t const *time);

27
lib/linux/clock_gettime.c Normal file
View File

@ -0,0 +1,27 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of GNU Mes.
*
* GNU Mes 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 Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* GNU Mes 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <time.h>
int
clock_gettime (clockid_t clk_id, struct timespec *tp)
{
return _sys_call2 (SYS_clock_gettime, (long)clk_id, (long)tp);
}

27
lib/linux/gettimeofday.c Normal file
View File

@ -0,0 +1,27 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of GNU Mes.
*
* GNU Mes 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 Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* GNU Mes 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <time.h>
int
gettimeofday (struct timeval *tv, struct timezone *tz)
{
return _sys_call2 (SYS_gettimeofday, (long)tv, (long)tz);
}

View File

@ -173,9 +173,3 @@ chdir (char const *file_name)
{
return _sys_call1 (SYS_chdir, (long)file_name);
}
int
clock_gettime (clockid_t clk_id, struct timespec *tp)
{
return _sys_call2 (SYS_clock_gettime, (long)clk_id, (long)tp);
}

View File

@ -149,3 +149,7 @@ fsync (int filedes)
{
return _sys_call1 (SYS_fsync, (int)filedes);
}
#include "linux/clock_gettime.c"
#include "linux/gettimeofday.c"
#include "linux/time.c"

View File

@ -60,15 +60,3 @@ getcwd (char *buffer, size_t size)
{
return _sys_call2 (SYS_getcwd, (long)buffer, (long)size);
}
time_t
time (time_t *result)
{
return _sys_call1 (SYS_time, (long)result);
}
int
gettimeofday (struct timeval *tv, struct timezone *tz)
{
return _sys_call2 (SYS_gettimeofday, (long)tv, (long)tz);
}

27
lib/linux/time.c Normal file
View File

@ -0,0 +1,27 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of GNU Mes.
*
* GNU Mes 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 Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* GNU Mes 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <time.h>
time_t
time (time_t *result)
{
return _sys_call1 (SYS_time, (long)result);
}

View File

@ -205,6 +205,7 @@ struct scm scm_type_variable = {TSYMBOL, "<cell:variable>",0};
struct scm scm_type_vector = {TSYMBOL, "<cell:vector>",0};
struct scm scm_type_broken_heart = {TSYMBOL, "<cell:broken-heart>",0};
struct scm scm_symbol_internal_time_units_per_second = {TSYMBOL, "internal-time-units-per-second",0};
struct scm scm_symbol_compiler = {TSYMBOL, "%compiler",0};
struct scm scm_symbol_arch = {TSYMBOL, "%arch",0};

View File

@ -26,7 +26,7 @@
//#define MES_MINI 1
#if POSIX
long ARENA_SIZE = 100000000;
long ARENA_SIZE = 100000000; // 2.3GiB
#else
long ARENA_SIZE = 300000; // 32b: 3MiB, 64b: 6 MiB
#endif
@ -271,6 +271,7 @@ struct scm scm_type_variable = {TSYMBOL, "<cell:variable>",0};
struct scm scm_type_vector = {TSYMBOL, "<cell:vector>",0};
struct scm scm_type_broken_heart = {TSYMBOL, "<cell:broken-heart>",0};
struct scm scm_symbol_internal_time_units_per_second = {TSYMBOL, "internal-time-units-per-second",0};
struct scm scm_symbol_compiler = {TSYMBOL, "%compiler",0};
struct scm scm_symbol_arch = {TSYMBOL, "%arch",0};
@ -2546,6 +2547,7 @@ main (int argc, char *argv[])
SCM a = mes_environment (argc, argv);
a = mes_builtins (a);
a = init_time (a);
m0 = make_initial_module (a);
g_macros = make_hash_table_ (0);

View File

@ -18,10 +18,12 @@
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
int readchar ();
@ -303,3 +305,50 @@ waitpid_ (SCM pid, SCM options)
int child = waitpid (VALUE (pid), &status, VALUE (options));
return cons (MAKE_NUMBER (child), MAKE_NUMBER (status));
}
#if __x86_64__
/* Nanoseconds on 64-bit systems with POSIX timers. */
#define TIME_UNITS_PER_SECOND 1000000000
#else
/* Milliseconds for everyone else. */
#define TIME_UNITS_PER_SECOND 1000
#endif
struct timespec g_start_time;
SCM
init_time (SCM a) ///((internal))
{
clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &g_start_time);
a = acons (cell_symbol_internal_time_units_per_second, MAKE_NUMBER (TIME_UNITS_PER_SECOND), a);
}
SCM
current_time ()
{
return MAKE_NUMBER (time (0));
}
SCM
gettimeofday_ () ///((name . "gettimeofday"))
{
struct timeval time;
gettimeofday (&time, 0);
return cons (MAKE_NUMBER (time.tv_sec), MAKE_NUMBER (time.tv_usec));
}
long
seconds_and_nanoseconds_to_long (long s, long ns)
{
return s * TIME_UNITS_PER_SECOND
+ ns / (1000000000 / TIME_UNITS_PER_SECOND);
}
SCM
get_internal_run_time ()
{
struct timespec ts;
clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &ts);
long time = seconds_and_nanoseconds_to_long (ts.tv_sec - g_start_time.tv_sec,
ts.tv_nsec - g_start_time.tv_nsec);
return MAKE_NUMBER (time);
}