noise func

This commit is contained in:
udo
2021-12-27 02:52:17 +00:00
parent af29d51536
commit 3bc6fe7f90
9 changed files with 653 additions and 530 deletions
Binary file not shown.
+32 -33
View File
@@ -227,18 +227,18 @@ FastCGIServer::process(int timeout_ms)
FD_ZERO(&fs_read); FD_ZERO(&fs_read);
FD_ZERO(&fs_write); FD_ZERO(&fs_write);
for (std::vector<int>::const_iterator it = listen_sockets.begin(); for(auto socket_handle : listen_sockets)
it != listen_sockets.end(); ++it) { {
FD_SET(*it, &fs_read); FD_SET(socket_handle, &fs_read);
nfd = std::max(nfd, *it); nfd = std::max(nfd, socket_handle);
} }
for (std::map<int, Connection*>::const_iterator it = read_sockets.begin(); for(auto con : read_sockets)
it != read_sockets.end(); ++it) { {
FD_SET(it->first, &fs_read); FD_SET(con.first, &fs_read);
if (!it->second->output_buffer.empty()) if (!con.second->output_buffer.empty())
FD_SET(it->first, &fs_write); FD_SET(con.first, &fs_write);
nfd = std::max(nfd, it->first); nfd = std::max(nfd, con.first);
} }
int select_result = select(nfd + 1, &fs_read, &fs_write, NULL, int select_result = select(nfd + 1, &fs_read, &fs_write, NULL,
@@ -249,27 +249,22 @@ FastCGIServer::process(int timeout_ms)
else else
throw std::runtime_error("select() failed"); throw std::runtime_error("select() failed");
for (std::vector<int>::const_iterator it = listen_sockets.begin(); for(auto socket_handle : listen_sockets)
it != listen_sockets.end(); ++it) if (FD_ISSET(socket_handle, &fs_read))
if (FD_ISSET(*it, &fs_read)) { {
int read_socket = accept(*it, NULL, NULL); int posix_con = accept(socket_handle, NULL, NULL);
if (read_socket == -1) if (posix_con == -1)
throw std::runtime_error("accept() failed"); throw std::runtime_error("accept() failed");
Connection* connection = new Connection; read_sockets[posix_con] = new Connection();
try {
read_sockets.insert(std::map<int, Connection*>::value_type(
read_socket, connection));
} catch (...) {
delete connection;
throw;
}
} }
for (std::map<int, Connection*>::iterator it = read_sockets.begin(); for (std::map<int, Connection*>::iterator it = read_sockets.begin();
it != read_sockets.end();) { it != read_sockets.end();)
{
int read_socket = it->first; int read_socket = it->first;
if (FD_ISSET(read_socket, &fs_read)) { if (FD_ISSET(read_socket, &fs_read))
{
int read_result = read(read_socket, buffer, sizeof(buffer)); int read_result = read(read_socket, buffer, sizeof(buffer));
if (read_result == -1) if (read_result == -1)
if (errno == ECONNRESET) if (errno == ECONNRESET)
@@ -285,7 +280,8 @@ FastCGIServer::process(int timeout_ms)
} }
if (!it->second->output_buffer.empty() && if (!it->second->output_buffer.empty() &&
FD_ISSET(read_socket, &fs_write)) { FD_ISSET(read_socket, &fs_write))
{
process_connection_write(*it->second); process_connection_write(*it->second);
int write_result = write(read_socket, int write_result = write(read_socket,
it->second->output_buffer.data(), it->second->output_buffer.data(),
@@ -295,7 +291,8 @@ FastCGIServer::process(int timeout_ms)
it->second->output_buffer.erase(0, write_result); it->second->output_buffer.erase(0, write_result);
} }
if (it->second->close_socket && it->second->output_buffer.empty()) { if (it->second->close_socket && it->second->output_buffer.empty())
{
close_socket: close_socket:
int close_result = close(it->first); int close_result = close(it->first);
if (close_result == -1 && errno != ECONNRESET) if (close_result == -1 && errno != ECONNRESET)
@@ -499,17 +496,19 @@ void
FastCGIServer::process_write_request(Connection& connection, RequestID id, FastCGIServer::process_write_request(Connection& connection, RequestID id,
RequestInfo& request) RequestInfo& request)
{ {
if (!request.out.empty()) { if (!request.out.empty())
{
write_data(connection.output_buffer, id, request.out, FCGI_STDOUT); write_data(connection.output_buffer, id, request.out, FCGI_STDOUT);
request.out.clear(); request.out.clear();
} }
if (!request.err.empty()) { if (!request.err.empty())
{
write_data(connection.output_buffer, id, request.err, FCGI_STDERR); write_data(connection.output_buffer, id, request.err, FCGI_STDERR);
request.err.clear(); request.err.clear();
} }
if ((request.in_closed || request.status != 0) && if ((request.in_closed || request.status != 0) &&
!request.output_closed) { !request.output_closed)
{
request.out = request.out =
var_dump(request.header, "", "\r\n") + var_dump(request.header, "", "\r\n") +
var_dump(request.set_cookies, "", "\r\n") + var_dump(request.set_cookies, "", "\r\n") +
@@ -525,11 +524,11 @@ FastCGIServer::process_write_request(Connection& connection, RequestID id,
write_data(connection.output_buffer, id, request.out, FCGI_STDOUT); write_data(connection.output_buffer, id, request.out, FCGI_STDOUT);
write_data(connection.output_buffer, id, request.err, FCGI_STDERR); write_data(connection.output_buffer, id, request.err, FCGI_STDERR);
request.time_end = microtime(); request.stats.time_end = microtime();
printf("(r) pid:%i\t%s\t%0.6fs\t%0.1fkB\n", printf("(r) pid:%i\t%s\t%0.6fs\t%0.1fkB\n",
my_pid, my_pid,
request.params["REQUEST_URI"].c_str(), request.params["REQUEST_URI"].c_str(),
request.time_end - request.time_start, request.stats.time_end - request.stats.time_start,
(f32)(request.out.length()/1024) (f32)(request.out.length()/1024)
); );
+65
View File
@@ -397,3 +397,68 @@ String ob_get_close()
return(result); return(result);
} }
#define BIT_NOISE1 0xB5297A4D
#define BIT_NOISE2 0x68E31DA4
#define BIT_NOISE3 0x1B56C4E9
// based on Squirrel3 https://www.youtube.com/watch?v=LWFzPP8ZbdU&t=2666s
u32 noise32(u32 index, u32 seed)
{
u32 r = index;
r *= BIT_NOISE1;
r += seed;
r ^= (r >> 8);
r += BIT_NOISE2;
r ^= (r << 8);
r *= BIT_NOISE3;
r ^= (r >> 8);
return(r);
}
#define BIT_NOISE61 0x5134811636f8cc8a
#define BIT_NOISE62 0xb8E31DA41B56C4E9
#define BIT_NOISE63 0x18cd227aaa1168c1
u64 noise64(u64 index, u64 seed)
{
u64 r = index;
r *= BIT_NOISE61;
r += seed;
r ^= (r >> 8);
r += BIT_NOISE62;
r ^= (r << 8);
r *= BIT_NOISE63;
r ^= (r >> 8);
return(r);
}
#define MAX_64 0xffffffffffffffff
f64 noise01(u64 index, u64 seed)
{
return((float)noise64(index, seed)/(float)MAX_64);
}
u64 generate_int(u64 from, u64 to, u64 index, u64 seed)
{
u64 b = 1 + to - from;
return(from + (noise64(index, seed) % b));
}
#include <tgmath.h>
f64 generate_float(f64 from, f64 to, u64 index, u64 seed, f64 decimal_precision)
{
f64 b = to - from;
return(from + fmod( decimal_precision*(f64)noise64(index, seed), b));
}
u64 draw_int(u64 from, u64 to)
{
return(generate_int(from, to, context->random_index++, context->random_seed));
}
f64 draw_float(f64 from, f64 to, f64 decimal_precision)
{
return(generate_float(from, to, context->random_index++, context->random_seed, decimal_precision));
}
+8
View File
@@ -29,3 +29,11 @@ void ob_start();
void ob_clear(); void ob_clear();
String ob_get_clear(); String ob_get_clear();
String ob_get(); String ob_get();
u32 noise32(u32 index, u32 seed = 0);
u64 noise64(u64 index, u64 seed = 0);
f64 noise01(u64 index, u64 seed = 0);
u64 generate_int(u64 from, u64 to, u64 index, u64 seed = 0);
f64 generate_float(f64 from, f64 to, u64 index, u64 seed = 0, f64 decimal_precision = 0.000000000001);
u64 draw_int(u64 from, u64 to);
f64 draw_float(f64 from, f64 to, f64 decimal_precision = 0.000000000001);
+6 -4
View File
@@ -153,7 +153,7 @@ struct ServerSettings {
String COMPILER_SYS_PATH = "."; String COMPILER_SYS_PATH = ".";
u32 LISTEN_PORT = 9993; u32 LISTEN_PORT = 9993;
u64 SESSION_TIME = 60*60*24*30; u64 SESSION_TIME = 60*60*24*30;
u32 WORKER_COUNT = 4; u32 WORKER_COUNT = 1;
u32 MAX_MEMORY = 1024*1024*16; u32 MAX_MEMORY = 1024*1024*16;
}; };
@@ -236,6 +236,9 @@ struct Request {
StringMap header; StringMap header;
StringList set_cookies; StringList set_cookies;
u64 random_seed;
u64 random_index;
MemoryArena* request_arena; MemoryArena* request_arena;
String in; String in;
@@ -246,12 +249,11 @@ struct Request {
bool is_finished = false; bool is_finished = false;
struct Stats {
u32 bytes_written;
f64 time_init; f64 time_init;
f64 time_start; f64 time_start;
f64 time_end; f64 time_end;
struct Stats {
u32 bytes_written;
} stats; } stats;
struct Resources { struct Resources {
+4 -2
View File
@@ -11,7 +11,7 @@ int handle_request(FastCGIRequest& request) {
// server receives all parameters. There may be more data coming on the // server receives all parameters. There may be more data coming on the
// standard input stream. // standard input stream.
request.request_arena = request_arena; request.request_arena = request_arena;
request.time_init = microtime(); request.stats.time_init = microtime();
request.ob_start(); // this one is for the headers request.ob_start(); // this one is for the headers
request.ob_start(); // this one is for the content request.ob_start(); // this one is for the content
if (request.params.count("REQUEST_URI")) if (request.params.count("REQUEST_URI"))
@@ -45,9 +45,11 @@ int handle_complete(FastCGIRequest& request) {
context = &request; context = &request;
request.server = &server_state; request.server = &server_state;
request.time_start = microtime(); request.stats.time_start = microtime();
request.header["Content-Type"] = context->server->config.CONTENT_TYPE; request.header["Content-Type"] = context->server->config.CONTENT_TYPE;
request.get = parse_query(request.params["QUERY_String"]); request.get = parse_query(request.params["QUERY_String"]);
request.random_index = 0;
request.random_seed = noise64(*reinterpret_cast<u64*>(&request.stats.time_start));
if(request.params["HTTP_COOKIE"].length() > 0) if(request.params["HTTP_COOKIE"].length() > 0)
request.cookies = parse_cookies(request.params["HTTP_COOKIE"]); request.cookies = parse_cookies(request.params["HTTP_COOKIE"]);
+1
View File
@@ -24,6 +24,7 @@ RENDER()
<li><a href="memcached.uce">Memcached</a></li> <li><a href="memcached.uce">Memcached</a></li>
<li><a href="mysql.uce">MySQL Connector</a></li> <li><a href="mysql.uce">MySQL Connector</a></li>
<li><a href="fileio.uce">File I/O</a></li> <li><a href="fileio.uce">File I/O</a></li>
<li><a href="random.uce">RNG/Noise</a></li>
</ul> </ul>
<pre><? <pre><?
print("Worker PID: ", my_pid, "\n"); print("Worker PID: ", my_pid, "\n");
+46
View File
@@ -0,0 +1,46 @@
RENDER()
{
<>
<link rel="stylesheet" href='style.css'></link>
<h1>
<a href="index.uce">UCE Test</a>:
Random
</h1>
</>
<>
Generate Some Numbers
<pre><?
u64 max_64 = 0xffffffffffffffff;
u64 max_32 = 0xffffffff;
for(auto i = 0; i < 100; i++)
{
print(i, ": ", (float)noise32(i)/(float)max_32, " / ", (float)noise64(i)/(float)max_64, " / ", noise01(i), " / ",
generate_int(0, 4, i), " / ", generate_float(0.0, 4.0, i), "\n");
}
?></pre>
Draw Some Numbers
<div>
Seed <?= context->random_seed ?>
</div>
<pre><?
for(auto i = 0; i < 100; i++)
{
print(i, ": ", draw_int(0, 4), " / ", draw_float(0.0, 4.0), "\n");
}
?></pre>
Params
<pre><?= var_dump(context->params) ?></pre>
</>
}