mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-13 22:49:11 +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>
|
||||
]]
|
Loading…
Add table
Add a link
Reference in a new issue