urbit/v/loop.c

827 lines
14 KiB
C
Raw Normal View History

2013-09-29 00:21:18 +04:00
/* v/loop.c
**
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
2014-01-27 22:48:55 +04:00
#include <sys/types.h>
2013-09-29 00:21:18 +04:00
#include <unistd.h>
#include <setjmp.h>
#include <gmp.h>
#include <sigsegv.h>
#include <stdint.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <uv.h>
#include <errno.h>
#include <curses.h>
#include <termios.h>
#include <term.h>
#include "all.h"
#include "v/vere.h"
2014-09-11 04:01:32 +04:00
#if 0
2013-09-29 00:21:18 +04:00
static jmp_buf Signal_buf;
#ifndef SIGSTKSZ
# define SIGSTKSZ 16384
#endif
static uint8_t Sigstk[SIGSTKSZ];
2014-09-11 04:01:32 +04:00
uint8_t u3_Critical;
2013-09-29 00:21:18 +04:00
typedef enum {
sig_none,
sig_overflow,
sig_interrupt,
2013-09-29 00:21:18 +04:00
sig_terminate,
sig_memory,
2013-09-29 00:21:18 +04:00
sig_assert,
sig_timer
2014-09-11 04:01:32 +04:00
} u3_kill;
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
volatile u3_kill Sigcause; // reasons for exception
2013-09-29 00:21:18 +04:00
static void _lo_cont(void *arg1, void *arg2, void *arg3)
{
(void)(arg1);
(void)(arg2);
(void)(arg3);
siglongjmp(Signal_buf, 1);
}
2013-09-29 00:21:18 +04:00
static void
_lo_signal_handle_over(int emergency, stackoverflow_context_t scp)
{
2014-09-11 04:01:32 +04:00
if ( u3_Critical ) {
2013-09-29 00:21:18 +04:00
// Careful not to grow the stack during critical sections.
//
write(2, "stack disaster\n", strlen("stack disaster" + 2));
abort();
}
#if 0
if ( 1 == emergency ) {
write(2, "stack emergency\n", strlen("stack emergency" + 2));
abort();
} else
2013-09-29 00:21:18 +04:00
#endif
{
Sigcause = sig_overflow;
sigsegv_leave_handler(_lo_cont, NULL, NULL, NULL);
2013-09-29 00:21:18 +04:00
}
}
static void
_lo_signal_handle_term(int x)
{
2014-09-11 04:01:32 +04:00
if ( !u3_Critical ) {
2013-09-29 00:21:18 +04:00
Sigcause = sig_terminate;
2014-11-05 04:18:47 +03:00
u3_Host.liv = c3n;
2013-09-29 00:21:18 +04:00
longjmp(Signal_buf, 1);
}
}
static void
_lo_signal_handle_intr(int x)
{
2014-09-11 04:01:32 +04:00
if ( !u3_Critical ) {
2013-09-29 00:21:18 +04:00
Sigcause = sig_interrupt;
longjmp(Signal_buf, 1);
}
}
static void
_lo_signal_handle_alrm(int x)
{
2014-09-11 04:01:32 +04:00
if ( !u3_Critical ) {
2013-09-29 00:21:18 +04:00
Sigcause = sig_timer;
longjmp(Signal_buf, 1);
}
}
/* _lo_signal_done():
*/
static void
_lo_signal_done()
{
// signal(SIGINT, SIG_IGN);
signal(SIGTERM, SIG_IGN);
signal(SIGVTALRM, SIG_IGN);
stackoverflow_deinstall_handler();
{
struct itimerval itm_u;
timerclear(&itm_u.it_interval);
timerclear(&itm_u.it_value);
setitimer(ITIMER_VIRTUAL, &itm_u, 0);
}
2014-09-11 04:01:32 +04:00
u3_unix_ef_move();
2013-09-29 00:21:18 +04:00
}
/* _lo_signal_deep(): start deep processing; set timer for sec_w or 0.
*/
static void
_lo_signal_deep(c3_w sec_w)
{
2014-09-11 04:01:32 +04:00
u3_unix_ef_hold();
2013-09-29 00:21:18 +04:00
stackoverflow_install_handler(_lo_signal_handle_over, Sigstk, SIGSTKSZ);
signal(SIGINT, _lo_signal_handle_intr);
signal(SIGTERM, _lo_signal_handle_term);
{
struct itimerval itm_u;
timerclear(&itm_u.it_interval);
itm_u.it_value.tv_sec = sec_w;
itm_u.it_value.tv_usec = 0;
setitimer(ITIMER_VIRTUAL, &itm_u, 0);
}
signal(SIGVTALRM, _lo_signal_handle_alrm);
}
2014-09-11 04:01:32 +04:00
#endif
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
/* u3_loop_signal_memory(): end computation for out-of-memory.
2013-09-29 00:21:18 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_loop_signal_memory()
2013-09-29 00:21:18 +04:00
{
fprintf(stderr, "\r\nout of memory\r\n");
2014-04-16 05:35:25 +04:00
c3_assert(0);
2014-09-11 04:01:32 +04:00
#if 0
2013-09-29 00:21:18 +04:00
Sigcause = sig_memory;
longjmp(Signal_buf, 1);
2014-09-11 04:01:32 +04:00
#endif
2013-09-29 00:21:18 +04:00
}
/* _lo_init(): initialize I/O across the process.
*/
static void
_lo_init()
{
2014-11-20 03:46:16 +03:00
c3_l cod_l;
cod_l = u3a_lush(c3__unix);
2014-10-12 11:17:06 +04:00
u3_unix_io_init();
2014-11-20 03:46:16 +03:00
u3a_lop(cod_l);
cod_l = u3a_lush(c3__ames);
2014-10-12 11:17:06 +04:00
u3_ames_io_init();
2014-11-20 03:46:16 +03:00
u3a_lop(cod_l);
cod_l = u3a_lush(c3__term);
2014-10-12 11:17:06 +04:00
u3_term_io_init();
2014-11-20 03:46:16 +03:00
u3a_lop(cod_l);
cod_l = u3a_lush(c3__http);
2014-10-12 11:17:06 +04:00
u3_http_io_init();
2014-11-20 03:46:16 +03:00
u3a_lop(cod_l);
cod_l = u3a_lush(c3__cttp);
2014-10-12 11:17:06 +04:00
u3_cttp_io_init();
2014-11-20 03:46:16 +03:00
u3a_lop(cod_l);
cod_l = u3a_lush(c3__save);
2014-11-01 01:36:01 +03:00
u3_save_io_init();
2014-11-20 03:46:16 +03:00
u3a_lop(cod_l);
cod_l = u3a_lush(c3__temp);
u3_temp_io_init();
2014-11-20 03:46:16 +03:00
u3a_lop(cod_l);
2013-09-29 00:21:18 +04:00
}
2014-01-17 12:12:05 +04:00
/* _lo_talk(): bring up listeners across the process.
*/
static void
_lo_talk()
{
2014-11-20 03:46:16 +03:00
c3_l cod_l;
cod_l = u3a_lush(c3__unix);
2014-10-12 11:17:06 +04:00
u3_unix_io_talk();
2014-11-20 03:46:16 +03:00
u3a_lop(cod_l);
cod_l = u3a_lush(c3__ames);
2014-10-12 11:17:06 +04:00
u3_ames_io_talk();
2014-11-20 03:46:16 +03:00
u3a_lop(cod_l);
cod_l = u3a_lush(c3__http);
2014-10-12 11:17:06 +04:00
u3_http_io_talk();
2014-11-20 03:46:16 +03:00
u3a_lop(cod_l);
cod_l = u3a_lush(c3__term);
2014-10-12 11:17:06 +04:00
u3_term_io_talk();
2014-11-20 03:46:16 +03:00
u3a_lop(cod_l);
2014-01-17 12:12:05 +04:00
}
2014-09-11 04:01:32 +04:00
/* u3_lo_exit(): terminate I/O across the process.
2013-09-29 00:21:18 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_lo_exit(void)
2013-09-29 00:21:18 +04:00
{
2014-11-20 03:46:16 +03:00
c3_l cod_l;
cod_l = u3a_lush(c3__unix);
2014-10-12 11:17:06 +04:00
u3_unix_io_exit();
2014-11-20 03:46:16 +03:00
u3a_lop(cod_l);
cod_l = u3a_lush(c3__ames);
2014-10-12 11:17:06 +04:00
u3_ames_io_exit();
2014-11-20 03:46:16 +03:00
u3a_lop(cod_l);
cod_l = u3a_lush(c3__term);
2014-10-12 11:17:06 +04:00
u3_term_io_exit();
2014-11-20 03:46:16 +03:00
u3a_lop(cod_l);
cod_l = u3a_lush(c3__http);
2014-10-12 11:17:06 +04:00
u3_http_io_exit();
2014-11-20 03:46:16 +03:00
u3a_lop(cod_l);
cod_l = u3a_lush(c3__cttp);
2014-10-12 11:17:06 +04:00
u3_cttp_io_exit();
2014-11-20 03:46:16 +03:00
u3a_lop(cod_l);
cod_l = u3a_lush(c3__save);
2014-11-01 01:36:01 +03:00
u3_save_io_exit();
2014-11-20 03:46:16 +03:00
u3a_lop(cod_l);
cod_l = u3a_lush(c3__temp);
u3_temp_io_exit();
2014-11-20 03:46:16 +03:00
u3a_lop(cod_l);
2013-09-29 00:21:18 +04:00
}
/* _lo_poll(): reset event flags across the process.
*/
static void
_lo_poll(void)
{
2014-11-20 03:46:16 +03:00
c3_l cod_l;
cod_l = u3a_lush(c3__ames);
2014-09-11 04:01:32 +04:00
u3_ames_io_poll();
2014-11-20 03:46:16 +03:00
u3a_lop(cod_l);
cod_l = u3a_lush(c3__http);
2014-09-11 04:01:32 +04:00
u3_http_io_poll();
2014-11-20 03:46:16 +03:00
u3a_lop(cod_l);
cod_l = u3a_lush(c3__term);
2014-09-11 04:01:32 +04:00
u3_term_io_poll();
2014-11-20 03:46:16 +03:00
u3a_lop(cod_l);
cod_l = u3a_lush(c3__save);
2014-11-01 01:36:01 +03:00
u3_save_io_poll();
2014-11-20 03:46:16 +03:00
u3a_lop(cod_l);
cod_l = u3a_lush(c3__unix);
2014-09-11 04:01:32 +04:00
u3_unix_io_poll();
2014-11-20 03:46:16 +03:00
u3a_lop(cod_l);
cod_l = u3a_lush(c3__temp);
u3_temp_io_poll();
2014-11-20 03:46:16 +03:00
u3a_lop(cod_l);
2013-09-29 00:21:18 +04:00
}
2013-11-09 03:37:38 +04:00
#if 0
2013-09-29 00:21:18 +04:00
/* _lo_how(): print how.
*/
static const c3_c*
2014-09-11 04:01:32 +04:00
_lo_how(u3_noun how)
2013-09-29 00:21:18 +04:00
{
switch ( how ) {
default: c3_assert(0); break;
case c3__ames: return "ames";
2014-10-15 06:25:23 +04:00
case c3__temp: return "temp";
2013-09-29 00:21:18 +04:00
case c3__term: return "cons";
case c3__htcn: return "http-conn";
case c3__htls: return "http-lisn";
case c3__save: return "save";
case c3__unix: return "unix";
}
}
2013-11-09 03:37:38 +04:00
#endif
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
/* u3_lo_bail(): clean up all event state.
2013-09-29 00:21:18 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_lo_bail(void)
2013-09-29 00:21:18 +04:00
{
fflush(stdout);
2014-09-11 04:01:32 +04:00
u3_lo_exit();
2013-09-29 00:21:18 +04:00
exit(1);
}
/* _lo_tape(): dump a tape, old style. Don't do this.
*/
static void
2014-09-11 04:01:32 +04:00
_lo_tape(FILE* fil_u, u3_noun tep)
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
u3_noun tap = tep;
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
while ( u3_nul != tap ) {
2013-09-29 00:21:18 +04:00
c3_c car_c;
2014-09-11 04:01:32 +04:00
if ( u3h(tap) >= 127 ) {
2013-09-29 00:21:18 +04:00
car_c = '?';
2014-09-11 04:01:32 +04:00
} else car_c = u3h(tap);
2013-09-29 00:21:18 +04:00
putc(car_c, fil_u);
2014-09-11 04:01:32 +04:00
tap = u3t(tap);
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
u3z(tep);
2013-09-29 00:21:18 +04:00
}
/* _lo_wall(): dump a wall, old style. Don't do this.
*/
static void
2014-09-11 04:01:32 +04:00
_lo_wall(u3_noun wol)
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
FILE* fil_u = u3_term_io_hija();
u3_noun wal = wol;
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
while ( u3_nul != wal ) {
_lo_tape(fil_u, u3k(u3h(wal)));
2013-09-29 00:21:18 +04:00
putc(13, fil_u);
putc(10, fil_u);
2014-09-11 04:01:32 +04:00
wal = u3t(wal);
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
u3_term_io_loja(0);
u3z(wol);
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
/* u3_lo_tank(): dump single tank.
2013-09-29 00:21:18 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_lo_tank(c3_l tab_l, u3_noun tac)
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
u3_lo_punt(tab_l, u3nc(tac, u3_nul));
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
/* u3_lo_punt(): dump tank list.
2013-09-29 00:21:18 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_lo_punt(c3_l tab_l, u3_noun tac)
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
u3_noun blu = u3_term_get_blew(0);
c3_l col_l = u3h(blu);
u3_noun cat = tac;
2013-09-29 00:21:18 +04:00
// We are calling nock here, but hopefully need no protection.
//
2014-11-06 03:20:01 +03:00
while ( c3y == u3r_du(cat) ) {
2014-11-06 06:10:22 +03:00
u3_noun wol = u3dc("wash", u3nc(tab_l, col_l), u3k(u3h(cat)));
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
_lo_wall(wol);
cat = u3t(cat);
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
u3z(tac);
u3z(blu);
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
/* u3_lo_sway(): print trace.
2013-09-29 00:21:18 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_lo_sway(c3_l tab_l, u3_noun tax)
2013-09-29 00:21:18 +04:00
{
2014-11-06 06:10:22 +03:00
u3_noun mok = u3dc("mook", 2, tax);
2014-09-11 04:01:32 +04:00
u3_lo_punt(tab_l, u3k(u3t(mok)));
u3z(mok);
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
/* _lo_time(): set time.
2013-09-29 00:21:18 +04:00
*/
2014-09-11 04:01:32 +04:00
static void
_lo_time(void)
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
struct timeval tim_tv;
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
gettimeofday(&tim_tv, 0);
2014-11-06 03:20:01 +03:00
u3v_time(u3_time_in_tv(&tim_tv));
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
/* u3_lo_open(): begin callback processing.
2013-09-29 00:21:18 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_lo_open(void)
2013-09-29 00:21:18 +04:00
{
2014-11-30 13:06:41 +03:00
if ( u3C.wag_w & (u3o_debug_ram | u3o_check_corrupt) ) {
2014-11-29 05:19:11 +03:00
//
// Assumption: there are no noun roots outside u3A.
//
2014-11-26 02:31:35 +03:00
u3m_grab(u3_none);
2014-10-15 06:02:37 +04:00
}
2014-11-30 13:06:41 +03:00
#if 0
if ( u3C.wag_w & u3o_debug_cpu ) {
struct itimerval itm_u;
getitimer(ITIMER_VIRTUAL, &itm_u);
fprintf(stderr, "tv_sec %d, tv_usec %d, value %d/%d\r\n",
itm_u.it_interval.tv_sec,
itm_u.it_interval.tv_usec,
itm_u.it_value.tv_sec,
itm_u.it_interval.tv_usec);
}
#endif
2014-09-11 04:01:32 +04:00
_lo_time();
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
/* u3_lo_shut(): end callback processing.
2013-09-29 00:21:18 +04:00
*/
void
2014-11-06 06:10:22 +03:00
u3_lo_shut(c3_o inn)
2013-09-29 00:21:18 +04:00
{
2014-11-26 02:31:35 +03:00
// u3m_grab(u3_none);
2013-11-12 11:09:11 +04:00
2013-09-29 00:21:18 +04:00
// process actions
//
2014-09-11 04:01:32 +04:00
u3_raft_work();
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
// u3_lo_grab("lo_shut b", u3_none);
2013-11-12 11:09:11 +04:00
2013-09-29 00:21:18 +04:00
// update time
//
2014-09-11 04:01:32 +04:00
_lo_time();
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
// u3_lo_grab("lo_shut c", u3_none);
2013-11-12 11:09:11 +04:00
2013-09-29 00:21:18 +04:00
// for input operations, poll fs (XX not permanent)
// XX remove raty_lead guard
2013-09-29 00:21:18 +04:00
//
2014-11-05 04:18:47 +03:00
if ( c3y == inn ) {
2014-09-11 04:01:32 +04:00
u3_unix_ef_look();
2015-03-19 00:25:01 +03:00
u3_raft_work();
_lo_time();
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
// u3_lo_grab("lo_shut d", u3_none);
2013-11-12 11:09:11 +04:00
2013-09-29 00:21:18 +04:00
// clean shutdown
//
2014-11-05 04:18:47 +03:00
if ( c3n == u3_Host.liv ) {
2013-09-29 00:21:18 +04:00
// direct save and die
//
2014-09-11 04:01:32 +04:00
// u3_lo_grab("lo_exit", u3_none);
// u3_loom_save(u3A->ent_d);
// u3_loom_exit();
2014-11-30 13:06:41 +03:00
u3t_damp();
2014-09-11 04:01:32 +04:00
u3_lo_exit();
2013-09-29 00:21:18 +04:00
2014-12-16 03:30:23 +03:00
// save a checkpoint before exiting
u3e_save();
2014-09-11 04:01:32 +04:00
exit(u3_Host.xit_i);
2013-09-29 00:21:18 +04:00
}
else {
// poll arvo to generate any event binding changes
//
_lo_poll();
}
}
2013-11-09 03:37:38 +04:00
#if 0
2013-09-29 00:21:18 +04:00
// _lo_bench_noop(): benchmark no-op events.
//
static void
_lo_bench_noop(c3_w num_w)
{
c3_w i_w;
2013-09-29 00:21:18 +04:00
for ( i_w = 0; i_w < num_w; i_w++ ) {
2014-09-11 04:01:32 +04:00
u3_reck_plan(u3A, u3nq(u3_blip, c3__term, 1, u3_nul),
u3nc(c3__noop, u3_nul));
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
u3_raft_work(u3A);
2013-09-29 00:21:18 +04:00
}
// _lo_bench_scot_p(): benchmark prettyprint.
//
static void
_lo_bench_scot_p(c3_w num_w)
{
c3_w i_w;
for ( i_w = 0; i_w < num_w; i_w++ ) {
2014-11-06 06:10:22 +03:00
u3_noun soc = u3dc("scot", 'p', u3k(u3A->now));
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
u3z(soc);
2013-09-29 00:21:18 +04:00
}
}
// _lo_bench_slay_p(): benchmark prettyprint.
//
static void
_lo_bench_slay_p(c3_w num_w)
{
c3_w i_w;
for ( i_w = 0; i_w < num_w; i_w++ ) {
2014-11-06 06:10:22 +03:00
u3_noun soc = u3dc("scot", 'p', u3k(u3A->now));
u3_noun dub = u3do("slay", soc);
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
u3z(dub);
2013-09-29 00:21:18 +04:00
}
}
// _lo_bench_scot_da(): benchmark prettyprint.
//
static void
_lo_bench_scot_da(c3_w num_w)
{
c3_w i_w;
for ( i_w = 0; i_w < num_w; i_w++ ) {
2014-11-06 06:10:22 +03:00
u3_noun soc = u3dc("scot", c3__da, u3k(u3A->now));
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
u3z(soc);
2013-09-29 00:21:18 +04:00
}
}
// _lo_bench_dec(): benchmark decrement.
//
static void
_lo_bench_dec(c3_w num_w)
{
c3_w i_w;
for ( i_w = 0; i_w < num_w; i_w++ ) {
2014-11-06 06:10:22 +03:00
u3_noun soc = u3do("dec", u3k(u3A->now));
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
u3z(soc);
2013-09-29 00:21:18 +04:00
}
}
// _lo_bench_scot_ud(): benchmark prettyprint.
//
static void
_lo_bench_scot_ud(c3_w num_w)
{
c3_w i_w;
for ( i_w = 0; i_w < num_w; i_w++ ) {
2014-11-06 06:10:22 +03:00
u3_noun soc = u3dc("scot", c3__ud, u3k(u3A->now));
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
u3z(soc);
2013-09-29 00:21:18 +04:00
}
}
// _lo_bench(): lo-tech profiling.
//
static void
_lo_bench(const c3_c* lab_c, void (*fun)(c3_w), c3_w num_w)
{
2014-09-11 04:01:32 +04:00
u3_noun old, new;
2013-09-29 00:21:18 +04:00
uL(fprintf(uH, "bench: %s: start...\n", lab_c));
2014-09-11 04:01:32 +04:00
u3_reck_time(u3A);
old = u3k(u3A->now);
2013-09-29 00:21:18 +04:00
fun(num_w);
2014-09-11 04:01:32 +04:00
u3_reck_time(u3A);
new = u3k(u3A->now);
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
c3_w tms_w = (c3_w)u3_time_gap_ms(old, new);
2013-09-29 00:21:18 +04:00
if ( tms_w > (10 * num_w) ) {
uL(fprintf(uH, "bench: %s*%d: %d ms, %d ms each.\n",
2013-09-29 00:21:18 +04:00
lab_c, num_w, tms_w, (tms_w / num_w)));
}
else {
uL(fprintf(uH, "bench: %s*%d: %d ms, %d us each.\n",
2013-09-29 00:21:18 +04:00
lab_c, num_w, tms_w, ((tms_w * 1000) / num_w)));
}
}
}
2013-11-09 03:37:38 +04:00
#endif
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
/* u3_lo_show(): generic noun print.
2013-09-29 00:21:18 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_lo_show(c3_c* cap_c, u3_noun nun)
2013-09-29 00:21:18 +04:00
{
2014-11-06 06:10:22 +03:00
u3_noun pav = u3dc("pave", c3__noun, nun);
2014-11-06 03:20:01 +03:00
c3_c* txt_c = (c3_c*)u3r_tape(pav);
2013-09-29 00:21:18 +04:00
fprintf(stderr, "%s: %s\r\n", cap_c, txt_c);
2014-09-11 04:01:32 +04:00
u3z(pav);
2013-09-29 00:21:18 +04:00
free(txt_c);
}
static void
_lo_slow()
{
#if 0
_lo_bench("scot %p", _lo_bench_scot_p, 256);
_lo_bench("scot %da", _lo_bench_scot_da, 256);
_lo_bench("scot %ud", _lo_bench_scot_ud, 256);
_lo_bench("slay %p", _lo_bench_slay_p, 256);
_lo_bench("noop", _lo_bench_noop, 256);
#endif
}
2014-09-11 04:01:32 +04:00
/* u3_lo_loop(): begin main event loop.
2013-09-29 00:21:18 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_lo_loop()
2013-09-29 00:21:18 +04:00
{
uv_loop_t* lup_u = uv_default_loop();
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
u3_Host.lup_u = lup_u;
2013-09-29 00:21:18 +04:00
signal(SIGPIPE, SIG_IGN); // pipe, schmipe
// signal(SIGIO, SIG_IGN); // linux is wont to produce for some reason
_lo_init();
2014-12-01 03:06:08 +03:00
2014-09-11 04:01:32 +04:00
u3_raft_init();
2014-03-01 02:52:00 +04:00
2015-01-15 22:10:29 +03:00
#if 1
2014-12-01 03:06:08 +03:00
if ( _(u3_Host.ops_u.dry) ) {
u3t_boff();
u3t_damp();
u3_lo_exit();
fprintf(stderr, "dry run: exit\r\n");
exit(0);
}
2015-01-15 22:10:29 +03:00
else
#endif
{
2014-12-01 03:06:08 +03:00
if ( c3n == u3_Host.ops_u.bat ) {
uv_run(u3L, UV_RUN_DEFAULT);
}
2014-03-01 02:52:00 +04:00
}
2014-01-16 05:41:30 +04:00
}
2014-09-11 04:01:32 +04:00
/* u3_lo_lead(): actions on promotion to leader.
2014-01-16 05:41:30 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_lo_lead(void)
2014-01-16 05:41:30 +04:00
{
// Further server configuration.
//
{
2014-09-11 04:01:32 +04:00
u3_http_ef_bake();
}
2014-01-17 12:12:05 +04:00
_lo_talk();
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
u3_unix_ef_look();
2014-11-06 03:20:01 +03:00
u3v_plan(u3nt(u3_blip, c3__ames, u3_nul),
2014-09-11 04:01:32 +04:00
u3nc(c3__kick, u3k(u3A->now)));
2013-09-29 00:21:18 +04:00
}
_lo_poll();
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
#if 0
u3_loom_save(u3A->ent_d);
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
u3_Host.sav_u.ent_d = rec_u->ent_d;
2013-09-29 00:21:18 +04:00
#endif
2014-11-05 04:18:47 +03:00
if ( c3y == u3_Host.ops_u.nuu ) {
2014-09-11 04:01:32 +04:00
u3_term_ef_boil(1);
2013-09-29 00:21:18 +04:00
}
#if 1
_lo_slow();
#endif
}
2014-09-11 04:01:32 +04:00
#if 0
2013-09-29 00:21:18 +04:00
/* _lo_mark_reck(): mark a reck.
*/
static c3_w
2014-09-11 04:01:32 +04:00
_lo_mark_reck(u3_reck* rec_u)
2013-09-29 00:21:18 +04:00
{
c3_w siz_w = 0;
c3_w egg_w;
2014-11-06 03:20:01 +03:00
siz_w += u3m_mark_noun(rec_u->ken);
siz_w += u3m_mark_noun(rec_u->roc);
2013-09-29 00:21:18 +04:00
2014-11-06 03:20:01 +03:00
siz_w += u3m_mark_noun(rec_u->yot);
siz_w += u3m_mark_noun(rec_u->now);
siz_w += u3m_mark_noun(rec_u->wen);
siz_w += u3m_mark_noun(rec_u->sen);
siz_w += u3m_mark_noun(rec_u->own);
siz_w += u3m_mark_noun(rec_u->roe);
siz_w += u3m_mark_noun(rec_u->key);
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
u3_cart* egg_u;
2013-09-29 00:21:18 +04:00
egg_w = 0;
for ( egg_u = rec_u->ova.egg_u; egg_u; egg_u = egg_u->nex_u ) {
2014-11-06 03:20:01 +03:00
egg_w += u3m_mark_noun(egg_u->vir);
2013-09-29 00:21:18 +04:00
}
siz_w += egg_w;
}
#if 0
fprintf(stderr, "ken %d, roc %d, yot %d, roe %d, egg %d\r\n",
ken_w, roc_w, yot_w, roe_w, egg_w);
#endif
return siz_w;
}
/* _lo_mark(): mark the whole vere system.
*/
static c3_w
_lo_mark()
{
c3_w siz_w;
2014-11-06 03:20:01 +03:00
siz_w = u3m_mark_internal();
2014-09-11 04:01:32 +04:00
siz_w += _lo_mark_reck(u3_Host.arv_u);
2013-09-29 00:21:18 +04:00
return siz_w;
}
2014-09-11 04:01:32 +04:00
#endif
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
#if 0
/* _lo_word(): print a word to the passed stream.
2013-09-29 00:21:18 +04:00
*/
static void
_lo_word(FILE* fil_u, c3_w wod_w)
2013-09-29 00:21:18 +04:00
{
2014-11-06 06:10:22 +03:00
u3_noun top = c3y;
2013-09-29 00:21:18 +04:00
if ( wod_w / (1000 * 1000 * 1000) ) {
fprintf(fil_u, "%u.", wod_w / (1000 * 1000 * 1000));
2013-09-29 00:21:18 +04:00
wod_w %= (1000 * 1000 * 1000);
2014-11-05 04:18:47 +03:00
top = c3n;
2013-09-29 00:21:18 +04:00
}
if ( wod_w / (1000 * 1000) ) {
2014-11-05 04:18:47 +03:00
fprintf(fil_u, ((top == c3y) ? "%u." : "%03u."),
wod_w / (1000 * 1000));
2013-09-29 00:21:18 +04:00
wod_w %= (1000 * 1000);
2014-11-05 04:18:47 +03:00
top = c3n;
2013-09-29 00:21:18 +04:00
}
if ( wod_w / 1000 ) {
2014-11-05 04:18:47 +03:00
fprintf(fil_u, ((top == c3y) ? "%u." : "%03u."), wod_w / 1000);
2013-09-29 00:21:18 +04:00
wod_w %= 1000;
2014-11-05 04:18:47 +03:00
top = c3n;
2013-09-29 00:21:18 +04:00
}
2014-11-05 04:18:47 +03:00
fprintf(fil_u, ((top == c3y) ? "%u" : "%03u"), wod_w);
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
/* u3_lo_grab(): garbage-collect the world, plus roots.
2013-09-29 00:21:18 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_lo_grab(c3_c* cap_c, u3_noun som, ...)
2013-09-29 00:21:18 +04:00
{
c3_w siz_w, lec_w;
siz_w = _lo_mark();
{
va_list vap;
2014-09-11 04:01:32 +04:00
u3_noun tur;
2013-09-29 00:21:18 +04:00
va_start(vap, som);
2014-09-11 04:01:32 +04:00
if ( som != u3_none ) {
2014-11-06 03:20:01 +03:00
siz_w += u3m_mark_noun(som);
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
while ( u3_none != (tur = va_arg(vap, u3_noun)) ) {
2014-11-06 03:20:01 +03:00
siz_w += u3m_mark_noun(tur);
2013-09-29 00:21:18 +04:00
}
}
va_end(vap);
}
2014-11-06 03:20:01 +03:00
lec_w = u3m_sweep(siz_w);
2013-09-29 00:21:18 +04:00
2014-11-05 04:18:47 +03:00
// if ( lec_w || (c3y == u3_Flag_Verbose) )
2013-11-12 11:09:11 +04:00
if ( lec_w || !strcmp("init", cap_c) ) {
FILE* fil_u = uH;
fprintf(fil_u, "%s: gc: ", cap_c);
2013-09-29 00:21:18 +04:00
if ( lec_w ) {
_lo_word(fil_u, 4 * lec_w);
fprintf(fil_u, " bytes shed; ");
2013-09-29 00:21:18 +04:00
}
_lo_word(fil_u, 4 * siz_w);
uL(fprintf(fil_u, " bytes live\n"));
2013-11-09 03:37:38 +04:00
#if 0
if ( lec_w ) {
uL(fprintf(uH, "zero garbage tolerance!\n"));
2014-09-11 04:01:32 +04:00
u3_lo_exit();
2013-11-09 03:37:38 +04:00
c3_assert(0);
exit(1);
}
#endif
2013-09-29 00:21:18 +04:00
}
2014-11-05 04:18:47 +03:00
u3_wire_lan(u3_Wire) = c3y;
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
#endif