mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-07 03:38:31 +00:00
Add raw memory visualization tool to redbean
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.
This commit is contained in:
parent
578cb21591
commit
80b211e314
106 changed files with 1483 additions and 592 deletions
|
@ -1,9 +1,6 @@
|
|||
local mymodule = {}
|
||||
|
||||
function mymodule.hello()
|
||||
SetStatus(200)
|
||||
SetHeader('Content-Type', 'text/html; charset=US-ASCII')
|
||||
Write("<!doctype html>\r\n")
|
||||
Write("<b>Hello World!</b>\r\n")
|
||||
end
|
||||
|
||||
|
|
189
tool/net/demo/binarytrees.lua
Normal file
189
tool/net/demo/binarytrees.lua
Normal file
|
@ -0,0 +1,189 @@
|
|||
-- binary trees: benchmark game
|
||||
-- with resource limit tutorial
|
||||
-- by justine tunney
|
||||
|
||||
local outofcpu = false
|
||||
local oldsigxcpufunc = nil
|
||||
local oldsigxcpuflags = nil
|
||||
local oldsigxcpumask = nil
|
||||
|
||||
-- This is our signal handler.
|
||||
function OnSigxcpu(sig)
|
||||
-- Please note, it's dangerous to do complicated things from inside
|
||||
-- asynchronous signal handlers. Even Log() isn't async signal safe
|
||||
-- The best possible practice is to have them simply set a variable
|
||||
outofcpu = true
|
||||
end
|
||||
|
||||
-- These two functions are what's being benchmarked. It's a popular
|
||||
-- torture test for interpreted languages since it generates a lot of
|
||||
-- garbage. Lua does well on this test, going faster than many other
|
||||
-- languages, e.g. Racket, Python.
|
||||
|
||||
local function MakeTree(depth)
|
||||
if outofcpu then
|
||||
error({reason='MakeTree() caught SIGXCPU'})
|
||||
end
|
||||
if depth > 0 then
|
||||
depth = depth - 1
|
||||
left, right = MakeTree(depth), MakeTree(depth)
|
||||
return { left, right }
|
||||
else
|
||||
return { }
|
||||
end
|
||||
end
|
||||
|
||||
local function CheckTree(tree)
|
||||
if outofcpu then
|
||||
error({reason='CheckTree() caught SIGXCPU'})
|
||||
end
|
||||
if tree[1] then
|
||||
return 1 + CheckTree(tree[1]) + CheckTree(tree[2])
|
||||
else
|
||||
return 1
|
||||
end
|
||||
end
|
||||
|
||||
-- Now for our redbean web server code...
|
||||
|
||||
local function WriteForm(depth, suggestion)
|
||||
Write([[<!doctype html>
|
||||
|
||||
<title>redbean binary trees</title>
|
||||
|
||||
<style>
|
||||
body { padding: 1em; }
|
||||
h1 a { color: inherit; text-decoration: none; }
|
||||
h1 img { border: none; vertical-align: middle; }
|
||||
input { margin: 1em; padding: .5em; }
|
||||
p { word-break: break-word; max-width: 650px; }
|
||||
dt { font-weight: bold; }
|
||||
dd { margin-top: 1em; margin-bottom: 1em; }
|
||||
.hdr { text-indent: -1em; padding-left: 1em; }
|
||||
</style>
|
||||
|
||||
<h1>
|
||||
<a href="/"><img src="/redbean.png"></a>
|
||||
<a href="fetch.lua">binary trees benchmark demo</a>
|
||||
</h1>
|
||||
|
||||
<p>
|
||||
This demo is from the computer language benchmark game. It's a
|
||||
torture test for the Lua garbage collector. ProTip: Try loading
|
||||
this page while the terminal memory monitor feature is active, so
|
||||
you can see memory shuffle around in a dual screen display. We
|
||||
use setrlimit to set a 512mb memory limit. So if you specify a
|
||||
number higher than 23 (a good meaty benchmark) then the Lua GC
|
||||
should panic a bit trying to recover (due to malloc failure) and
|
||||
ultimately the worker process should die. However the server and
|
||||
your system will survive.
|
||||
</p>
|
||||
|
||||
<form action="binarytrees.lua" method="post">
|
||||
<input type="text" id="depth" name="depth" size="70"
|
||||
value="%s" placeholder="%d" onfocus="this.select()"
|
||||
autofocus>
|
||||
<input type="submit" value="run">
|
||||
</form>
|
||||
|
||||
]] % {EscapeHtml(depth), suggestion})
|
||||
end
|
||||
|
||||
local function BinaryTreesDemo()
|
||||
if GetMethod() == 'GET' or GetMethod() == 'HEAD' then
|
||||
WriteForm("18", 18)
|
||||
elseif GetMethod() == 'POST' and not HasParam('depth') then
|
||||
ServeError(400)
|
||||
elseif GetMethod() == 'POST' then
|
||||
|
||||
-- write out the page so user can submit again
|
||||
WriteForm(GetParam('depth'), 18)
|
||||
Write('<dl>\r\n')
|
||||
Write('<dt>Output\r\n')
|
||||
Write('<dd>\r\n')
|
||||
|
||||
-- impose quotas to protect the server
|
||||
unix.setrlimit(unix.RLIMIT_AS, 1024 * 1024 * 1024)
|
||||
unix.setrlimit(unix.RLIMIT_CPU, 2, 4)
|
||||
|
||||
-- when RLIMIT_AS (virtual address space) runs out of memory, then
|
||||
-- mmap() and malloc() start to fail, and lua reacts by trying to
|
||||
-- run the garbage collector over and over again. so we also place
|
||||
-- a limit on the number of "cpu time" seconds too. the behavior
|
||||
-- when we hit the soft limit, is the kernel gives us a friendly
|
||||
-- warning by sending the signal SIGXCPU which we must catch here
|
||||
-- once we catch it, we've got 2 seconds of grace time to clean up
|
||||
-- before we hit the "hard limit" and the kernel sends SIGKILL (9)
|
||||
oldsigxcpufunc,
|
||||
oldsigxcpuflags,
|
||||
oldsigxcpumask =
|
||||
unix.sigaction(unix.SIGXCPU, OnSigxcpu)
|
||||
|
||||
-- get the user-supplied parameter
|
||||
depth = tonumber(GetParam('depth'))
|
||||
|
||||
-- run the benchmark, recording how long it takes
|
||||
secs1, nanos1 = unix.clock_gettime()
|
||||
res = CheckTree(MakeTree(depth))
|
||||
secs2, nanos2 = unix.clock_gettime()
|
||||
|
||||
-- turn 128-bit timestamps into 64-bit nanosecond interval
|
||||
if secs2 == secs1 then
|
||||
nanos = nanos2 - nanos1
|
||||
else
|
||||
nanos = (secs2 - secs1) * 1000000000 + (1000000000 - nanos1) + nanos2
|
||||
end
|
||||
|
||||
-- write out result of benchmark
|
||||
Write('0%o\r\n' % {res})
|
||||
Write('<dt>Time Elapsed\r\n')
|
||||
Write('<dd>\r\n')
|
||||
Write('%g seconds, or<br>\r\n' % {nanos / 1e9})
|
||||
Write('%g milliseconds, or<br>\r\n' % {nanos / 1e6})
|
||||
Write('%g microseconds, or<br>\r\n' % {nanos / 1e6})
|
||||
Write('%d nanoseconds\r\n' % {nanos})
|
||||
Write('<dt>unix.clock_gettime() #1\r\n')
|
||||
Write('<dd>%d, %d\r\n' % {secs1, nanos1})
|
||||
Write('<dt>unix.clock_gettime() #2\r\n')
|
||||
Write('<dd>%d, %d\r\n' % {secs2, nanos2})
|
||||
Write('</dl>\r\n')
|
||||
|
||||
else
|
||||
ServeError(405)
|
||||
SetHeader('Allow', 'GET, HEAD, POST')
|
||||
end
|
||||
end
|
||||
|
||||
local function main()
|
||||
-- catch exceptions
|
||||
ok, err = pcall(BinaryTreesDemo)
|
||||
|
||||
-- we don't need to restore the old handler
|
||||
-- but it's generally a good practice to clean up
|
||||
if oldsigxcpufunc then
|
||||
unix.sigaction(unix.SIGXCPU,
|
||||
oldsigxcpufunc,
|
||||
oldsigxcpuflags,
|
||||
oldsigxcpumask)
|
||||
end
|
||||
|
||||
-- handle exceptions
|
||||
if not ok then
|
||||
|
||||
-- whenever anything, at all, goes wrong, with anything
|
||||
-- always with few exceptions close the connection asap
|
||||
SetHeader('Connection', 'close')
|
||||
|
||||
if err.reason then
|
||||
-- show our error message withoun internal code leaking out
|
||||
Write(err.reason)
|
||||
Write('\r\n')
|
||||
else
|
||||
-- just rethrow exception
|
||||
error('unexpected failure: ' .. tostring(err))
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
main()
|
49
tool/net/demo/call-lua-module.lua
Normal file
49
tool/net/demo/call-lua-module.lua
Normal file
|
@ -0,0 +1,49 @@
|
|||
-- Call Lua Module Demo
|
||||
--
|
||||
-- Your Lua modules may be stored in the /.lua/ folder.
|
||||
--
|
||||
-- In our /.init.lua global scope earlier, we ran the code:
|
||||
--
|
||||
-- mymodule = require "mymodule"
|
||||
--
|
||||
-- Which preloaded the /.lua/mymodule.lua module once into the server
|
||||
-- global memory template from which all request handlers are forked.
|
||||
-- Therefore, we can just immediately use that module from our Lua
|
||||
-- server pages.
|
||||
|
||||
Write[[<!doctype html>
|
||||
<title>redbean call lua module demo</title>
|
||||
<style>
|
||||
body { padding: 1em; }
|
||||
h1 a { color: inherit; text-decoration: none; }
|
||||
h1 img { border: none; vertical-align: middle; }
|
||||
pre { margin-left: 2em; }
|
||||
p { word-break: break-word; max-width: 650px; }
|
||||
dt { font-weight: bold; }
|
||||
dd { margin-top: 1em; margin-bottom: 1em; }
|
||||
.hdr { text-indent: -1em; padding-left: 1em; }
|
||||
</style>
|
||||
<h1>
|
||||
<a href="/"><img src="/redbean.png"></a>
|
||||
<a href="call-lua-module.lua">call lua module demo</a>
|
||||
</h1>
|
||||
<p>Your Lua modules may be stored in the /.lua/ folder.
|
||||
<p>In our <code>/.init.lua</code> global scope earlier, we ran the code:
|
||||
<pre>mymodule = require "mymodule"</pre>
|
||||
<p>Which preloaded the <code>/.lua/mymodule.lua</code> module once into
|
||||
the server global memory template from which all request handlers are
|
||||
forked. Therefore, we can just immediately use that module from our
|
||||
Lua Server Pages.
|
||||
<p>
|
||||
Your <code>mymodule.hello()</code> output is as follows:
|
||||
<blockquote>
|
||||
]]
|
||||
|
||||
mymodule.hello()
|
||||
|
||||
Write[[
|
||||
</blockquote>
|
||||
<p>
|
||||
<a href="/">go back</a>
|
||||
</p>
|
||||
]]
|
|
@ -1,41 +1,29 @@
|
|||
-- Fetch() API Demo
|
||||
|
||||
local function WriteForm(url)
|
||||
Write('<!doctype html>\r\n')
|
||||
Write([[
|
||||
Write([[<!doctype html>
|
||||
<title>redbean fetch demo</title>
|
||||
<style>
|
||||
body {
|
||||
padding: 1em;
|
||||
}
|
||||
h1 a {
|
||||
color: inherit;
|
||||
text-decoration: none;
|
||||
}
|
||||
h1 img {
|
||||
border: none;
|
||||
vertical-align: middle;
|
||||
}
|
||||
input {
|
||||
margin: 1em;
|
||||
padding: .5em;
|
||||
}
|
||||
p {
|
||||
word-break: break-word;
|
||||
}
|
||||
dd {
|
||||
margin-top: 1em;
|
||||
margin-bottom: 1em;
|
||||
}
|
||||
.hdr {
|
||||
text-indent: -1em;
|
||||
padding-left: 1em;
|
||||
}
|
||||
body { padding: 1em; }
|
||||
h1 a { color: inherit; text-decoration: none; }
|
||||
h1 img { border: none; vertical-align: middle; }
|
||||
input { margin: 1em; padding: .5em; }
|
||||
pre { margin-left: 2em; }
|
||||
p { word-break: break-word; max-width: 650px; }
|
||||
dt { font-weight: bold; }
|
||||
dd { margin-top: 1em; margin-bottom: 1em; }
|
||||
.hdr { text-indent: -1em; padding-left: 1em; }
|
||||
</style>
|
||||
<h1>
|
||||
<a href="/"><img src="/redbean.png"></a>
|
||||
<a href="fetch.lua">redbean fetch demo</a>
|
||||
</h1>
|
||||
<p>
|
||||
Your redbean is able to function as an HTTP client too.
|
||||
Lua server pages can use the <code>Fetch()</code> API to
|
||||
to send outgoing HTTP and HTTPS requests to other web
|
||||
servers. All it takes is a line of code!
|
||||
</p>
|
||||
<form action="fetch.lua" method="post">
|
||||
<input type="text" id="url" name="url" size="70"
|
||||
value="%s" placeholder="uri" autofocus>
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
if not IsPublicIp(GetClientAddr()) then
|
||||
StoreAsset('/hi', 'sup')
|
||||
end
|
||||
mymodule.hello()
|
|
@ -1,3 +1,6 @@
|
|||
-- redbean maxmind demo
|
||||
-- by justine tunney
|
||||
|
||||
local maxmind = require "maxmind"
|
||||
|
||||
local kMetroCodes = {
|
||||
|
@ -258,7 +261,7 @@ local function main()
|
|||
local ip = nil
|
||||
local geo = nil
|
||||
local asn = nil
|
||||
local value = ''
|
||||
local value = '8.8.8.8'
|
||||
if HasParam('ip') then
|
||||
local geodb = maxmind.open('/usr/local/share/maxmind/GeoLite2-City.mmdb')
|
||||
local asndb = maxmind.open('/usr/local/share/maxmind/GeoLite2-ASN.mmdb')
|
||||
|
@ -279,18 +282,58 @@ local function main()
|
|||
end
|
||||
end
|
||||
|
||||
SetHeader('Content-Type', 'text/html; charset=utf-8')
|
||||
Write('<!doctype html>\n')
|
||||
Write([[
|
||||
<title>maxmind redbean demo</title>
|
||||
Write([[<!doctype html>
|
||||
<title>redbean maxmind demo</title>
|
||||
<style>
|
||||
body { padding: 1em; }
|
||||
h1 a { color: inherit; text-decoration: none; }
|
||||
h1 img { border: none; vertical-align: middle; }
|
||||
input { margin: 1em; padding: .5em; }
|
||||
pre { margin-left: 2em; }
|
||||
p { word-break: break-word; max-width: 650px; }
|
||||
dt { font-weight: bold; }
|
||||
dd { margin-top: 1em; margin-bottom: 1em; }
|
||||
.hdr { text-indent: -1em; padding-left: 1em; }
|
||||
</style>
|
||||
<h1>
|
||||
<a href="/"><img src="/redbean.png"></a>
|
||||
<a href="maxmind.lua">redbean maxmind demo</a>
|
||||
</h1>
|
||||
<p>
|
||||
Your redbean supports MaxMind GeoLite2 which is a free database
|
||||
you have to download separately, at their <a
|
||||
href="https://www.maxmind.com/en/geolite2/signup">website
|
||||
here</a>. It's worth doing because it lets you turn IP addresses
|
||||
into geographical coordinates, addresses, name it. Being able to
|
||||
Lua script this database is going to help you address things like
|
||||
online fraud and abuse.
|
||||
</p>
|
||||
<p>
|
||||
This script is hard coded to assume the database is at the
|
||||
following paths:
|
||||
<ul>
|
||||
<li><code>/usr/local/share/maxmind/GeoLite2-City.mmdb</code>
|
||||
<li><code>/usr/local/share/maxmind/GeoLite2-ASN.mmdb</code>
|
||||
</ul>
|
||||
<p>
|
||||
Which on Windows basically means the same thing as:
|
||||
</p>
|
||||
<ul>
|
||||
<li><code>C:\usr\local\share\maxmind\GeoLite2-City.mmdb</code>
|
||||
<li><code>C:\usr\local\share\maxmind\GeoLite2-ASN.mmdb</code>
|
||||
</ul>
|
||||
<p>
|
||||
Once you've placed it there, you can fill out the form below to
|
||||
have fun crawling all the information it provides!
|
||||
</p>
|
||||
<form action="maxmind.lua" method="get">
|
||||
<input type="text" id="ip" name="ip" placeholder="8.8.8.8"
|
||||
value="]] .. value .. [[">
|
||||
value="%s" onfocus="this.select()" autofocus>
|
||||
<label for="ip">ip address</label>
|
||||
<br>
|
||||
<input type="submit" value="Lookup">
|
||||
<input type="submit" value="Lookup" autofocus>
|
||||
</form>
|
||||
]])
|
||||
]] % {EscapeHtml(value)})
|
||||
|
||||
if ip then
|
||||
Write('<h3>Maxmind Geolite DB</h3>')
|
||||
|
|
80
tool/net/demo/store-asset.lua
Normal file
80
tool/net/demo/store-asset.lua
Normal file
|
@ -0,0 +1,80 @@
|
|||
-- StoreAsset Demo
|
||||
--
|
||||
-- Redbean is able to store files into its own executable structure.
|
||||
-- This is currently only supported on a Linux, Apple, and FreeBSD.
|
||||
--
|
||||
-- By loading this page, your redbean will insert an immediate file into
|
||||
-- your redbean named `/hi`, which you can click the back button in the
|
||||
-- browser and view it on the listing page right afterwards!
|
||||
|
||||
Write[[<!doctype html>
|
||||
<title>redbean store asset demo</title>
|
||||
<style>
|
||||
body { padding: 1em; }
|
||||
h1 a { color: inherit; text-decoration: none; }
|
||||
h1 img { border: none; vertical-align: middle; }
|
||||
input { margin: 1em; padding: .5em; }
|
||||
p { word-break: break-word; max-width: 650px; }
|
||||
dt { font-weight: bold; }
|
||||
dd { margin-top: 1em; margin-bottom: 1em; }
|
||||
.hdr { text-indent: -1em; padding-left: 1em; }
|
||||
</style>
|
||||
<h1>
|
||||
<a href="/"><img src="/redbean.png"></a>
|
||||
<a href="store-asset.lua">store asset demo</a>
|
||||
</h1>
|
||||
]]
|
||||
|
||||
if IsPublicIp(GetClientAddr()) then
|
||||
Write[[
|
||||
<p>
|
||||
Bad request.
|
||||
</p>
|
||||
<p>
|
||||
This HTTP endpoint self-modifies the web server. You're
|
||||
communicating wtith this redbean over a public network.
|
||||
Therefore redbean won't service this request.
|
||||
</p>
|
||||
]]
|
||||
|
||||
elseif GetHostOs() == "WINDOWS" or
|
||||
GetHostOs() == "OPENBSD" or
|
||||
GetHostOs() == "NETBSD" then
|
||||
Write[[
|
||||
<p>
|
||||
Unsupported
|
||||
</p>
|
||||
<p>
|
||||
Sorry! Redbean's Lua StoreAsset() function is only
|
||||
supported on Linux, Apple, and FreeBSD right now.
|
||||
</p>
|
||||
]]
|
||||
|
||||
else
|
||||
StoreAsset('/hi', [[
|
||||
StoreAsset() worked!
|
||||
|
||||
This file was inserted into your redbean by the Lua StoreAsset() API
|
||||
which was invoked by you browsing to the /store-asset.lua page.
|
||||
|
||||
Enjoy your self-modifying web server!
|
||||
]])
|
||||
|
||||
Write[[
|
||||
<p>
|
||||
This Lua script has just stored a new file named <code>/hi</code>
|
||||
to your redbean zip executable. This was accomplished while the
|
||||
web server is running. It live updates, so if you click the back
|
||||
button in your browser, you should see <code>/hi</code> in the
|
||||
ZIP central directory listing, and you can send an HTTP message
|
||||
requesting it.
|
||||
</p>
|
||||
]]
|
||||
|
||||
end
|
||||
|
||||
Write[[
|
||||
<p>
|
||||
<a href="/">go back</a>
|
||||
</p>
|
||||
]]
|
|
@ -70,6 +70,7 @@ FLAGS
|
|||
-p PORT listen port [def. 8080; repeatable]
|
||||
-l ADDR listen addr [def. 0.0.0.0; repeatable]
|
||||
-c SEC configures static cache-control
|
||||
-W TTY use tty path to monitor memory pages
|
||||
-L PATH log file location
|
||||
-P PATH pid file location
|
||||
-U INT daemon set user id
|
||||
|
|
|
@ -109,6 +109,7 @@ static void *LuaUnixRealloc(lua_State *L, void *p, size_t n) {
|
|||
return p2;
|
||||
}
|
||||
if (IsLegalSize(n)) {
|
||||
WARNF("reacting to malloc() failure by running lua garbage collector...");
|
||||
luaC_fullgc(L, 1);
|
||||
p2 = realloc(p, n);
|
||||
}
|
||||
|
|
|
@ -180,10 +180,12 @@ o/$(MODE)/tool/net/demo/unix-webserver.lua.zip.o \
|
|||
o/$(MODE)/tool/net/demo/unix-dir.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/unix-info.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/fetch.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/hello.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/call-lua-module.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/store-asset.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/maxmind.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/redbean.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/opensource.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/binarytrees.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/crashreport.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/closedsource.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/printpayload.lua.zip.o \
|
||||
|
@ -227,10 +229,12 @@ o/$(MODE)/tool/net/redbean-demo.com.dbg: \
|
|||
o/$(MODE)/tool/net/demo/unix-dir.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/unix-info.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/fetch.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/hello.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/store-asset.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/call-lua-module.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/redbean.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/maxmind.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/opensource.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/binarytrees.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/crashreport.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/closedsource.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/printpayload.lua.zip.o \
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include "libc/bits/popcnt.h"
|
||||
#include "libc/bits/safemacros.internal.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/ioctl.h"
|
||||
#include "libc/calls/math.h"
|
||||
#include "libc/calls/sigbits.h"
|
||||
#include "libc/calls/strace.internal.h"
|
||||
|
@ -31,12 +32,15 @@
|
|||
#include "libc/calls/struct/rusage.h"
|
||||
#include "libc/calls/struct/sigaction.h"
|
||||
#include "libc/calls/struct/stat.h"
|
||||
#include "libc/calls/struct/termios.h"
|
||||
#include "libc/calls/ttydefaults.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/dns/dns.h"
|
||||
#include "libc/dns/hoststxt.h"
|
||||
#include "libc/dos.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/fmt/fmt.h"
|
||||
#include "libc/fmt/itoa.h"
|
||||
#include "libc/intrin/nomultics.internal.h"
|
||||
#include "libc/intrin/spinlock.h"
|
||||
|
@ -64,6 +68,7 @@
|
|||
#include "libc/runtime/gc.h"
|
||||
#include "libc/runtime/gc.internal.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#include "libc/runtime/memtrack.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/runtime/stack.h"
|
||||
#include "libc/runtime/symbols.internal.h"
|
||||
|
@ -78,6 +83,7 @@
|
|||
#include "libc/sysv/consts/af.h"
|
||||
#include "libc/sysv/consts/audit.h"
|
||||
#include "libc/sysv/consts/auxv.h"
|
||||
#include "libc/sysv/consts/clone.h"
|
||||
#include "libc/sysv/consts/dt.h"
|
||||
#include "libc/sysv/consts/ex.h"
|
||||
#include "libc/sysv/consts/exit.h"
|
||||
|
@ -102,6 +108,7 @@
|
|||
#include "libc/sysv/consts/sock.h"
|
||||
#include "libc/sysv/consts/sol.h"
|
||||
#include "libc/sysv/consts/tcp.h"
|
||||
#include "libc/sysv/consts/termios.h"
|
||||
#include "libc/sysv/consts/w.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
@ -183,9 +190,11 @@ STATIC_YOINK("zip_uri_support");
|
|||
#define VERSION 0x020000
|
||||
#define HEARTBEAT 5000 /*ms*/
|
||||
#define HASH_LOAD_FACTOR /* 1. / */ 4
|
||||
#define MONITOR_MICROS 150000
|
||||
#define READ(F, P, N) readv(F, &(struct iovec){P, N}, 1)
|
||||
#define WRITE(F, P, N) writev(F, &(struct iovec){P, N}, 1)
|
||||
#define LockInc(P) asm volatile("lock incq\t%0" : "=m"(*(P)))
|
||||
#define LockDec(P) asm volatile("lock decq\t%0" : "=m"(*(P)))
|
||||
#define AppendCrlf(P) mempcpy(P, "\r\n", 2)
|
||||
#define HasHeader(H) (!!msg.headers[H].a)
|
||||
#define HeaderData(H) (inbuf.p + msg.headers[H].a)
|
||||
|
@ -193,26 +202,34 @@ STATIC_YOINK("zip_uri_support");
|
|||
#define HeaderEqualCase(H, S) \
|
||||
SlicesEqualCase(S, strlen(S), HeaderData(H), HeaderLength(H))
|
||||
|
||||
#define TRACE_BEGIN \
|
||||
do { \
|
||||
if (!IsTiny()) { \
|
||||
if (funtrace) ++g_ftrace; \
|
||||
if (systrace) ++__strace; \
|
||||
} \
|
||||
#define TRACE_BEGIN \
|
||||
do { \
|
||||
if (!IsTiny()) { \
|
||||
if (funtrace) { \
|
||||
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
||||
} \
|
||||
if (systrace) { \
|
||||
__atomic_fetch_add(&__strace, 1, __ATOMIC_RELAXED); \
|
||||
} \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define TRACE_END \
|
||||
do { \
|
||||
if (!IsTiny()) { \
|
||||
if (funtrace) --g_ftrace; \
|
||||
if (systrace) --__strace; \
|
||||
} \
|
||||
#define TRACE_END \
|
||||
do { \
|
||||
if (!IsTiny()) { \
|
||||
if (funtrace) { \
|
||||
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
||||
} \
|
||||
if (systrace) { \
|
||||
__atomic_fetch_sub(&__strace, 1, __ATOMIC_RELAXED); \
|
||||
} \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
// letters not used: EIJNOQWXYnoqwxy
|
||||
// letters not used: EIJNOQXYnoqwxy
|
||||
// digits not used: 0123456789
|
||||
// puncts not used: !"#$%&'()*+,-./;<=>@[\]^_`{|}~
|
||||
#define GETOPTS "BSVZabdfghijkmsuvzA:C:D:F:G:H:K:L:M:P:R:T:U:c:e:l:p:r:t:"
|
||||
#define GETOPTS "BSVZabdfghijkmsuvzA:C:D:F:G:H:K:L:M:P:R:T:U:W:c:e:l:p:r:t:"
|
||||
|
||||
extern unsigned long long __kbirth;
|
||||
|
||||
|
@ -362,6 +379,7 @@ static struct Shared {
|
|||
#include "tool/net/counters.inc"
|
||||
#undef C
|
||||
} c;
|
||||
_Alignas(64) char montermlock;
|
||||
} * shared;
|
||||
|
||||
static const char kCounterNames[] =
|
||||
|
@ -392,6 +410,7 @@ static bool sslcliused;
|
|||
static bool loglatency;
|
||||
static bool terminated;
|
||||
static bool uniprocess;
|
||||
static bool memmonalive;
|
||||
static bool invalidated;
|
||||
static bool logmessages;
|
||||
static bool isinitialized;
|
||||
|
@ -404,6 +423,7 @@ static bool sslclientverify;
|
|||
static bool connectionclose;
|
||||
static bool hasonworkerstop;
|
||||
static bool isexitingworker;
|
||||
static bool terminatemonitor;
|
||||
static bool hasonworkerstart;
|
||||
static bool leakcrashreports;
|
||||
static bool hasonhttprequest;
|
||||
|
@ -420,6 +440,7 @@ static int zfd;
|
|||
static int frags;
|
||||
static int gmtoff;
|
||||
static int client;
|
||||
static int mainpid;
|
||||
static int sandboxed;
|
||||
static int changeuid;
|
||||
static int changegid;
|
||||
|
@ -459,6 +480,7 @@ static struct Strings loops;
|
|||
static size_t payloadlength;
|
||||
static size_t contentlength;
|
||||
static int64_t cacheseconds;
|
||||
static const char *monitortty;
|
||||
static const char *serverheader;
|
||||
static struct Strings stagedirs;
|
||||
static struct Strings hidepaths;
|
||||
|
@ -1188,19 +1210,20 @@ static void CallSimpleHookIfDefined(const char *s) {
|
|||
}
|
||||
|
||||
static void ReportWorkerExit(int pid, int ws) {
|
||||
--shared->workers;
|
||||
int workers;
|
||||
workers = __atomic_sub_fetch(&shared->workers, 1, __ATOMIC_SEQ_CST);
|
||||
if (WIFEXITED(ws)) {
|
||||
if (WEXITSTATUS(ws)) {
|
||||
LockInc(&shared->c.failedchildren);
|
||||
WARNF("(stat) %d exited with %d (%,d workers remain)", pid,
|
||||
WEXITSTATUS(ws), shared->workers);
|
||||
WEXITSTATUS(ws), workers);
|
||||
} else {
|
||||
DEBUGF("(stat) %d exited (%,d workers remain)", pid, shared->workers);
|
||||
DEBUGF("(stat) %d exited (%,d workers remain)", pid, workers);
|
||||
}
|
||||
} else {
|
||||
LockInc(&shared->c.terminatedchildren);
|
||||
WARNF("(stat) %d terminated with %s (%,d workers remain)", pid,
|
||||
strsignal(WTERMSIG(ws)), shared->workers);
|
||||
strsignal(WTERMSIG(ws)), workers);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4369,7 +4392,7 @@ static int LuaSetHeader(lua_State *L) {
|
|||
}
|
||||
switch (h) {
|
||||
case kHttpConnection:
|
||||
if (SlicesEqualCase(eval, evallen, "close", 5)) {
|
||||
if (!SlicesEqualCase(eval, evallen, "close", 5)) {
|
||||
luaL_argerror(L, 2, "unsupported");
|
||||
unreachable;
|
||||
}
|
||||
|
@ -6306,6 +6329,10 @@ static int ExitWorker(void) {
|
|||
isexitingworker = true;
|
||||
return eintr();
|
||||
}
|
||||
if (monitortty) {
|
||||
terminatemonitor = true;
|
||||
_spinlock(&memmonalive);
|
||||
}
|
||||
_Exit(0);
|
||||
}
|
||||
|
||||
|
@ -6405,6 +6432,161 @@ static int EnableSandbox(void) {
|
|||
}
|
||||
}
|
||||
|
||||
static int MemoryMonitor(void *arg) {
|
||||
static struct termios oldterm;
|
||||
static int tty;
|
||||
sigset_t ss;
|
||||
bool done, ok;
|
||||
size_t intervals;
|
||||
struct winsize ws;
|
||||
unsigned char rez;
|
||||
struct termios term;
|
||||
char *b, *addr, title[128];
|
||||
struct MemoryInterval *mi, *mi2;
|
||||
long i, j, k, n, x, y, pi, gen, pages;
|
||||
int rc, id, color, color2, workers;
|
||||
_spinlock(&memmonalive);
|
||||
__atomic_load(&shared->workers, &id, __ATOMIC_SEQ_CST);
|
||||
DEBUGF("(memv) started for pid %d on tid %d", getpid(), gettid());
|
||||
|
||||
sigemptyset(&ss);
|
||||
sigaddset(&ss, SIGHUP);
|
||||
sigaddset(&ss, SIGINT);
|
||||
sigaddset(&ss, SIGQUIT);
|
||||
sigaddset(&ss, SIGTERM);
|
||||
sigaddset(&ss, SIGPIPE);
|
||||
sigaddset(&ss, SIGUSR1);
|
||||
sigaddset(&ss, SIGUSR2);
|
||||
sigprocmask(SIG_BLOCK, &ss, 0);
|
||||
|
||||
_spinlock(&shared->montermlock);
|
||||
if (!id) {
|
||||
if ((tty = open(monitortty, O_RDWR | O_NOCTTY)) != -1) {
|
||||
ioctl(tty, TCGETS, &oldterm);
|
||||
term = oldterm;
|
||||
term.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
|
||||
term.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
|
||||
term.c_oflag |= OPOST | ONLCR;
|
||||
term.c_iflag |= IUTF8;
|
||||
term.c_cflag |= CS8;
|
||||
term.c_cc[VMIN] = 1;
|
||||
term.c_cc[VTIME] = 0;
|
||||
ioctl(tty, TCSETS, &term);
|
||||
WRITE(tty, "\e[?25l", 6);
|
||||
}
|
||||
}
|
||||
_spunlock(&shared->montermlock);
|
||||
|
||||
if (tty != -1) {
|
||||
for (gen = 0, mi = 0, b = 0;;) {
|
||||
__atomic_load(&terminatemonitor, &done, __ATOMIC_SEQ_CST);
|
||||
if (done) break;
|
||||
__atomic_load(&shared->workers, &workers, __ATOMIC_SEQ_CST);
|
||||
if (id) id = MAX(1, MIN(id, workers));
|
||||
if (!id && workers) {
|
||||
usleep(50000);
|
||||
continue;
|
||||
}
|
||||
|
||||
++gen;
|
||||
__atomic_load(&_mmi.i, &intervals, __ATOMIC_SEQ_CST);
|
||||
if ((mi2 = realloc(mi, (intervals += 3) * sizeof(*mi)))) {
|
||||
mi = mi2;
|
||||
mi[0].x = (intptr_t)_base >> 16;
|
||||
mi[0].size = _etext - _base;
|
||||
mi[1].x = (intptr_t)_etext >> 16;
|
||||
mi[1].size = _edata - _etext;
|
||||
mi[2].x = (intptr_t)_edata >> 16;
|
||||
mi[2].size = _end - _edata;
|
||||
_spinlock(&_mmi.lock);
|
||||
if (_mmi.i == intervals - 3) {
|
||||
memcpy(mi + 3, _mmi.p, _mmi.i * sizeof(*mi));
|
||||
ok = true;
|
||||
} else {
|
||||
ok = false;
|
||||
}
|
||||
_spunlock(&_mmi.lock);
|
||||
if (!ok) {
|
||||
WARNF("(memv) retrying due to contention on mmap table");
|
||||
continue;
|
||||
}
|
||||
|
||||
ws.ws_col = 80;
|
||||
ws.ws_row = 40;
|
||||
getttysize(tty, &ws);
|
||||
|
||||
appendr(&b, 0);
|
||||
appends(&b, "\e[H\e[1m");
|
||||
|
||||
for (pi = k = x = y = i = 0; i < intervals; ++i) {
|
||||
addr = (char *)((int64_t)((uint64_t)mi[i].x << 32) >> 16);
|
||||
color = 0;
|
||||
appendf(&b, "\e[0m%lx", addr);
|
||||
pages = (mi[i].size + PAGESIZE - 1) / PAGESIZE;
|
||||
for (j = 0; j < pages; ++j) {
|
||||
rc = mincore(addr + j * PAGESIZE, PAGESIZE, &rez);
|
||||
if (!rc) {
|
||||
if (rez & 1) {
|
||||
color2 = 42;
|
||||
} else {
|
||||
color2 = 41;
|
||||
}
|
||||
} else {
|
||||
errno = 0;
|
||||
color2 = 0;
|
||||
}
|
||||
if (color != color2) {
|
||||
color = color2;
|
||||
appendf(&b, "\e[%dm", color);
|
||||
}
|
||||
appendw(&b, ' ');
|
||||
}
|
||||
}
|
||||
|
||||
appendf(&b,
|
||||
"\e[0m ID=%d PID=%d WS=%dx%d WORKERS=%d MODE=" MODE
|
||||
" GEN=%ld\e[J",
|
||||
id, getpid(), ws.ws_col, ws.ws_row, workers, gen);
|
||||
|
||||
_spinlock(&shared->montermlock);
|
||||
WRITE(tty, b, appendz(b).i);
|
||||
appendr(&b, 0);
|
||||
usleep(MONITOR_MICROS);
|
||||
_spunlock(&shared->montermlock);
|
||||
} else {
|
||||
// running out of memory temporarily is a real possibility here
|
||||
// the right thing to do, is stand aside and let lua try to fix
|
||||
WARNF("(memv) we require more vespene gas");
|
||||
usleep(MONITOR_MICROS);
|
||||
}
|
||||
}
|
||||
|
||||
if (!id) {
|
||||
appendr(&b, 0);
|
||||
appends(&b, "\e[H\e[J\e[?25h");
|
||||
WRITE(tty, b, appendz(b).i);
|
||||
ioctl(tty, TCSETS, &oldterm);
|
||||
}
|
||||
|
||||
DEBUGF("(memv) exiting...");
|
||||
close(tty);
|
||||
free(mi);
|
||||
free(b);
|
||||
}
|
||||
_spunlock(&memmonalive);
|
||||
DEBUGF("(memv) done");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int MonitorMemory(void) {
|
||||
return clone(MemoryMonitor,
|
||||
mmap(0, FRAMESIZE, PROT_READ | PROT_WRITE,
|
||||
MAP_STACK | MAP_ANONYMOUS, -1, 0),
|
||||
FRAMESIZE,
|
||||
CLONE_VM | CLONE_THREAD | CLONE_FS | CLONE_FILES | CLONE_SIGHAND,
|
||||
0, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
static int HandleConnection(size_t i) {
|
||||
int pid, rc = 0;
|
||||
clientaddrsize = sizeof(clientaddr);
|
||||
|
@ -6423,6 +6605,10 @@ static int HandleConnection(size_t i) {
|
|||
} else {
|
||||
switch ((pid = fork())) {
|
||||
case 0:
|
||||
if (monitortty) {
|
||||
memmonalive = false;
|
||||
MonitorMemory();
|
||||
}
|
||||
meltdown = false;
|
||||
__isworker = true;
|
||||
connectionclose = false;
|
||||
|
@ -6441,7 +6627,7 @@ static int HandleConnection(size_t i) {
|
|||
HandleForkFailure();
|
||||
return 0;
|
||||
default:
|
||||
++shared->workers;
|
||||
LockInc(&shared->workers);
|
||||
close(client);
|
||||
ReseedRng(&rng, "parent");
|
||||
if (hasonprocesscreate) {
|
||||
|
@ -6551,11 +6737,11 @@ static int HandleReadline(void) {
|
|||
if (status < 0) {
|
||||
if (status == -1) {
|
||||
OnTerm(SIGHUP); // eof
|
||||
INFOF("got repl eof");
|
||||
VERBOSEF("(repl) eof");
|
||||
return -1;
|
||||
} else if (errno == EINTR) {
|
||||
errno = 0;
|
||||
INFOF("got repl interrupt");
|
||||
VERBOSEF("(repl) interrupt");
|
||||
return -1;
|
||||
} else if (errno == EAGAIN) {
|
||||
errno = 0;
|
||||
|
@ -6715,7 +6901,7 @@ static void HandleShutdown(void) {
|
|||
// this function coroutines with linenoise
|
||||
static int EventLoop(int ms) {
|
||||
long double t;
|
||||
DEBUGF("EventLoop()");
|
||||
DEBUGF("(repl) event loop");
|
||||
while (!terminated) {
|
||||
errno = 0;
|
||||
if (zombied) {
|
||||
|
@ -6761,7 +6947,7 @@ static void ReplEventLoop(void) {
|
|||
static uint32_t WindowsReplThread(void *arg) {
|
||||
int sig;
|
||||
lua_State *L = GL;
|
||||
DEBUGF("WindowsReplThread()");
|
||||
DEBUGF("(repl) started windows thread");
|
||||
lua_repl_blocking = true;
|
||||
lua_repl_completions_callback = HandleCompletions;
|
||||
lua_initrepl(L, "redbean");
|
||||
|
@ -6781,7 +6967,7 @@ static uint32_t WindowsReplThread(void *arg) {
|
|||
if ((sig = linenoiseGetInterrupt())) {
|
||||
raise(sig);
|
||||
}
|
||||
DEBUGF("WindowsReplThread() exiting");
|
||||
DEBUGF("(repl) terminating windows thread");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -6798,7 +6984,6 @@ static void SigInit(void) {
|
|||
InstallSignalHandler(SIGUSR1, OnUsr1);
|
||||
InstallSignalHandler(SIGUSR2, OnUsr2);
|
||||
InstallSignalHandler(SIGPIPE, SIG_IGN);
|
||||
/* TODO(jart): SIGXCPU and SIGXFSZ */
|
||||
}
|
||||
|
||||
static void TlsInit(void) {
|
||||
|
@ -6915,6 +7100,7 @@ static void GetOpts(int argc, char *argv[]) {
|
|||
CASE('u', uniprocess = true);
|
||||
CASE('g', loglatency = true);
|
||||
CASE('m', logmessages = true);
|
||||
CASE('W', monitortty = optarg);
|
||||
CASE('l', ProgramAddr(optarg));
|
||||
CASE('H', ProgramHeader(optarg));
|
||||
CASE('L', ProgramLogPath(optarg));
|
||||
|
@ -6968,6 +7154,7 @@ void RedBean(int argc, char *argv[]) {
|
|||
reader = read;
|
||||
writer = WritevAll;
|
||||
gmtoff = GetGmtOffset((lastrefresh = startserver = nowl()));
|
||||
mainpid = getpid();
|
||||
CHECK_GT(CLK_TCK, 0);
|
||||
CHECK_NE(MAP_FAILED,
|
||||
(shared = mmap(NULL, ROUNDUP(sizeof(struct Shared), FRAMESIZE),
|
||||
|
@ -6983,7 +7170,9 @@ void RedBean(int argc, char *argv[]) {
|
|||
GetOpts(argc, argv);
|
||||
LuaInit();
|
||||
oldloglevel = __log_level;
|
||||
if (uniprocess) shared->workers = 1;
|
||||
if (uniprocess) {
|
||||
shared->workers = 1;
|
||||
}
|
||||
SigInit();
|
||||
Listen();
|
||||
TlsInit();
|
||||
|
@ -7008,6 +7197,12 @@ void RedBean(int argc, char *argv[]) {
|
|||
inbuf = inbuf_actual;
|
||||
isinitialized = true;
|
||||
CallSimpleHookIfDefined("OnServerStart");
|
||||
if (monitortty && (daemonize || uniprocess)) {
|
||||
monitortty = 0;
|
||||
}
|
||||
if (monitortty) {
|
||||
MonitorMemory();
|
||||
}
|
||||
#ifdef STATIC
|
||||
EventLoop(HEARTBEAT);
|
||||
#else
|
||||
|
@ -7032,6 +7227,10 @@ void RedBean(int argc, char *argv[]) {
|
|||
TlsDestroy();
|
||||
MemDestroy();
|
||||
}
|
||||
if (monitortty) {
|
||||
terminatemonitor = true;
|
||||
_spinlock(&memmonalive);
|
||||
}
|
||||
INFOF("(srvr) shutdown complete");
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue