mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 19:43:32 +00:00
80b211e314
This change introduces a `-W /dev/pts/1` flag to redbean. What it does is use the mincore() system call to create a dual-screen terminal display that lets you troubleshoot the virtual address space. This is useful since page faults are an important thing to consider when using a forking web server. Now we have a colorful visualization of which pages are going to fault and which ones are resident in memory. The memory monitor, if enabled, spawns as a thread that just outputs ANSI codes to the second terminal in a loop. In order to make this happen using the new clone() polyfill, stdio is now thread safe. This change also introduces some new demo pages to redbean. It also polishes the demos we already have, to look a bit nicer and more presentable for the upcoming release, with better explanations too.
62 lines
1.8 KiB
C
62 lines
1.8 KiB
C
/*-*- mode:c; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
|
|
│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
#include "libc/macros.internal.h"
|
|
#include "libc/time/tz.internal.h"
|
|
// clang-format off
|
|
/* Return the difference between two timestamps. */
|
|
|
|
/*
|
|
** This file is in the public domain, so clarified as of
|
|
** 1996-06-05 by Arthur David Olson.
|
|
*/
|
|
|
|
/* Return -X as a double. Using this avoids casting to 'double'. */
|
|
static inline double
|
|
dminus(double x)
|
|
{
|
|
return -x;
|
|
}
|
|
|
|
double
|
|
difftime(time_t time1, time_t time0)
|
|
{
|
|
/*
|
|
** If double is large enough, simply convert and subtract
|
|
** (assuming that the larger type has more precision).
|
|
*/
|
|
if (sizeof(time_t) < sizeof(double)) {
|
|
double t1 = time1, t0 = time0;
|
|
return t1 - t0;
|
|
}
|
|
|
|
/*
|
|
** The difference of two unsigned values can't overflow
|
|
** if the minuend is greater than or equal to the subtrahend.
|
|
*/
|
|
if (!TYPE_SIGNED(time_t))
|
|
return time0 <= time1 ? time1 - time0 : dminus(time0 - time1);
|
|
|
|
/* Use uintmax_t if wide enough. */
|
|
if (sizeof(time_t) <= sizeof(uintmax_t)) {
|
|
uintmax_t t1 = time1, t0 = time0;
|
|
return time0 <= time1 ? t1 - t0 : dminus(t0 - t1);
|
|
}
|
|
|
|
/*
|
|
** Handle cases where both time1 and time0 have the same sign
|
|
** (meaning that their difference cannot overflow).
|
|
*/
|
|
if ((time1 < 0) == (time0 < 0))
|
|
return time1 - time0;
|
|
|
|
/*
|
|
** The values have opposite signs and uintmax_t is too narrow.
|
|
** This suffers from double rounding; attempt to lessen that
|
|
** by using long double temporaries.
|
|
*/
|
|
{
|
|
long double t1 = time1, t0 = time0;
|
|
return t1 - t0;
|
|
}
|
|
}
|