ref: fd1e36f98d997c8183d3040b66fa6f9cd91e825b
dir: /sys_plan9.c/
#include "flisp.h"
#include "timefuncs.h"
#include <tos.h>
double
sec_realtime(void)
{
vlong t = nsec();
vlong ns = t % 1000000000LL;
vlong s = (t - ns) / 1000000000LL;
return (double)s + (double)ns/1.0e9;
}
/*
* nsec() is wallclock and can be adjusted by timesync
* so need to use cycles() instead, but fall back to
* nsec() in case we can't
*/
uint64_t
nanosec_monotonic(void)
{
static uint64_t fasthz, xstart;
uint64_t x, div;
if(fasthz == ~0ULL)
return nsec() - xstart;
if(fasthz == 0){
if(_tos->cyclefreq){
fasthz = _tos->cyclefreq;
cycles(&xstart);
} else {
fasthz = ~0ULL;
xstart = nsec();
}
return 0;
}
cycles(&x);
x -= xstart;
/* this is ugly */
for(div = 1000000000ULL; x < 0x1999999999999999ULL && div > 1 ; div /= 10ULL, x *= 10ULL);
return x / (fasthz / div);
}
void
timestring(double s, char *buf, int sz)
{
Tm tm;
snprint(buf, sz, "%τ", tmfmt(tmtime(&tm, s, tzload("local")), nil));
}
double
parsetime(const char *s)
{
Tm tm;
if(tmparse(&tm, "?WWW, ?MM ?DD hh:mm:ss ?Z YYYY", s, tzload("local"), nil) == nil)
return -1;
return tmnorm(&tm);
}
void
sleep_ms(int ms)
{
if(ms != 0)
sleep(ms);
}
int
ftruncate(int f, off_t sz)
{
Dir d;
nulldir(&d);
d.length = sz;
return dirfwstat(f, &d) > 0 ? 0 : -1;
}
extern uchar bootcode[];
extern ulong bootlen;
void
main(int argc, char **argv)
{
argv0 = argv[0];
setfcr(FPPDBL|FPRNR|FPOVFL);
tmfmtinstall();
flmain(bootcode, bootlen, argc, argv);
}