int pp_checkpoint(void *buf, int *size) {
    int   ferr;

    if (gim_backup) {
        if (gverbosepp)
            pp_printf("waiting for checkpoint.\n");
        ferr = pp_recv(buf, size);
    } else {
        int            msgid;
        MS_Result_Type results;
        if (gverbosepp)
            pp_printf("sending checkpoint.\n");
        ferr = XMSG_LINK_(TPT_REF(gpeer_phandle),               // phandle
                          &msgid,                      // msgid
                          NULL,                        // reqctrl
                          0,                           // reqctrlsize
                          NULL,                        // replyctrl
                          0,                           // replyctrlmax
                          (char *) buf,                // reqdata
                          (ushort) *size,              // reqdatasize
                          NULL,                        // replydata
                          0,                           // replydatamax
                          0,                           // linkertag
                          0,                           // pri
                          0,                           // xmitclass
                          0);                          // linkopts
        TEST_CHK_FEOK(ferr);
        // ignore checkpoint error
        ferr = XMSG_BREAK_(msgid, (short *) &results, TPT_REF(gpeer_phandle));
        TEST_CHK_FEIGNORE(ferr);
    }
    return ferr;
}
void pp_init() {
    int ferr;
    int len;
    int recvbuf[3];

    if (pp_is_backup()) {
        gim_backup = true;
        while (!gtakeover && !gshutdown) {
            len = sizeof(recvbuf);
            ferr = pp_checkpoint(recvbuf, &len);
            if (ferr == XZFIL_ERR_OK)
                XMSG_REPLY_(gsre.sre_msgId,    // msgid
                            NULL,              // replyctrl
                            0,                 // replyctrlsize
                            NULL,              // replydata
                            0,                 // replydatasize
                            0,                 // errorclass
                            NULL);             // newphandle
            if (gshutdown)
                pp_shutdown();
        }
        if (gverbosepp)
            pp_printf("the backup is now the primary process.\n");
        sprintf(gdisplay_name, "%s-%d-%d-%d-P", gname, gnid, gpid, getpid());
    } else {
        strcpy(gdisplay_name, gname);
        sprintf(&gdisplay_name[strlen(gdisplay_name)],
                "-%d-%d-%d-P", gnid, gpid, getpid());
        if (gverbosepp)
            pp_printf("we are the primary process.\n");
    }
    pp_start_backup(-1);
}
Beispiel #3
0
/* A shell command, for testing write*/
static int cmd_w1_w(const char *args[])
{
	int page;
	int max_blen = 32;
	int blen;
	char pn[32] = "\0";

	if (args[0] == '\0' || args[1] == '\0') {
		pp_printf("This commands needs arguments, "
			  "w1w PAGE_EEPROM STRING\n");
		return 0;
	}

	page = atoi(args[0]);
	blen = strlen(args[1]);

	if (blen > max_blen)
		blen = max_blen;
	memcpy(pn, args[1], blen);

	if (!w1_write_eeprom_bus
	    (&wrpc_w1_bus, page * 0x20, (uint8_t *) pn, blen))
		pp_printf("Write success\n");

	return 0;
}
Beispiel #4
0
/* show the content of the init script*/
int w1_eeprom_init_show()
{
	uint8_t buf[W1_BUF];
	uint16_t used, i, j;

	if (w1_read_eeprom_bus(&wrpc_w1_bus, EE_BASE_INIT, buf, W1_BUF) < 0)
		return EE_RET_1WERR;

	used = (uint16_t) buf[0];
	if (used == 0xff) {
		used = 0;
		pp_printf("Empty init script.. \n");
	}
	for (i = 1; i <= used; i++) {

		if (w1_read_eeprom_bus(&wrpc_w1_bus,
				       EE_BASE_INIT + (EE_BASE_CAL * i), buf,
				       W1_BUF) < 0)
			return EE_RET_1WERR;

		pp_printf("Command %d ", i);
		for (j = 0; j < strlen((char *)buf); ++j)
			mprintf("%c", buf[j]);
	}
	return 0;
}
Beispiel #5
0
/* add commands to the init script */
int w1_eeprom_init_add(const char *args[])
{
	uint8_t i = 1;
	uint8_t buf[W1_BUF];
	uint8_t num_cmd[W1_BUF];
	uint16_t used;
	if (w1_read_eeprom_bus(&wrpc_w1_bus, EE_BASE_INIT, num_cmd, W1_BUF) < 0)
		return EE_RET_1WERR;
	used = num_cmd[0];
	if (used == 0xff)
		used = 0;	/*this means the memory is blank */

	while (args[i] != '\0') {
		if (strlen((char *)buf) + strlen((char *)args[i]) > W1_BUF) {
			pp_printf("Sorry but the command is too large \n");
			return EE_RET_1WERR;
		}
		strcat((char *)buf, (char *)args[i]);
		strcat((char *)buf, " ");
		i++;
	}
	buf[strlen((char *)buf) - 1] = '\n';	/*replace last separator */

	if (w1_write_eeprom_bus
	    (&wrpc_w1_bus, EE_BASE_INIT + (EE_BASE_CAL * (used + 1)), buf,
	     W1_BUF) < 0)
		return EE_RET_1WERR;
	used++;
	pp_printf("%d commands in init script \n", used);
	num_cmd[0] = used;	/* update the number of scripts */
	if (w1_write_eeprom_bus(&wrpc_w1_bus, EE_BASE_INIT, num_cmd,
				W1_BUF) < 0)
		return EE_RET_1WERR;
	return 0;
}
Beispiel #6
0
static int cmd_help(const char *args[])
{
	struct wrc_shell_cmd *p;

	pp_printf("Available commands:\n");
	for (p = __cmd_begin; p < __cmd_end; p++)
		pp_printf("  %s\n", p->name);
	return 0;
}
Beispiel #7
0
/* Delays for master must have been calibrated while running as slave */
static int calib_t24p_master(uint32_t *value)
{
	int rv;

	rv = storage_phtrans(value, 0);
	if(rv < 0) {
		pp_printf("Error %d while reading t24p from storage\n", rv);
		return rv;
	}
	pp_printf("t24p read from storage: %d ps\n", *value);
	return rv;
}
void pp_check_mon_msg(char *mon_msg) {
    MS_Mon_Msg *msg = (MS_Mon_Msg *) mon_msg;

    switch (msg->type) {
    case MS_MsgType_Close:    // process close notification
        if (gverbosepp)
            pp_printf("close notice received from %s.\n",
                      msg->u.close.process_name);
        if (strcmp(gname, msg->u.close.process_name) != 0) {
            gopen_count--;
            if (gverbosepp)
                pp_printf("disconnecting from client\n");
        }
        break;
    case MS_MsgType_NodeDown: // node is down notification
        if (gverbosepp)
            pp_printf("node own notice not currently supported.\n");
        if (gpeer_nid == msg->u.down.nid)
            gtakeover = pp_takeover();
        break;
    case MS_MsgType_NodeUp:   // node is up notification
        if (gverbosepp)
            pp_printf("node Up notice not currently supported.\n");
        if (gpeer_nid == msg->u.up.nid)
            gtakeover = pp_takeover();
        break;
    case MS_MsgType_Open:     // process open notification
        if (gverbosepp)
            pp_printf("open notice received from %s.\n",
                      msg->u.open.target_process_name);
        if (strcmp(gname, msg->u.open.target_process_name) == 0) {
            gpeer_nid = msg->u.open.nid;
            gpeer_pid = msg->u.open.pid;
            if (gverbosepp)
                pp_printf("peer p-id=%d/%d\n", gpeer_nid, gpeer_pid);
            pp_pairinfo(false);
        }
        break;
    case MS_MsgType_ProcessDeath: // process death notification
        if (gverbosepp)
            pp_printf("processDeath notice received from p-id=%d/%d, peer-p-id=%d/%d.\n",
                      msg->u.death.nid, msg->u.death.pid, gpeer_nid, gpeer_pid);
        if ((gpeer_nid == msg->u.death.nid) &&
                (gpeer_pid == msg->u.death.pid))
            gtakeover = pp_takeover();
        break;
    case MS_MsgType_Shutdown: // shutdown notification
        if (gverbosepp)
            pp_printf("shutdown notice received.\n");
        gshutdown = true;
        break;
    default:
        if (gverbosepp)
            pp_printf("invalid notice message type received.\n");
        abort(); // oops
    }
}
void pp_start_backup(int nid) {
    int ferr;

    if (gshutdown) {
        if (gverbosepp)
            pp_printf("NOT starting backup process - shutdown.\n");
        return;
    }

    nid = nid; // no-warn
    ferr = msg_mon_get_process_info(NULL, &nid, &gpeer_pid);
    TEST_CHK_FEOK(ferr);
    for (;;) {
        gpeer_nid = -1;
        if (gnid == 1)
            gpeer_nid = 2;
        else
            gpeer_nid = 1;
        gpeer_pid = -1;
        if (gverbosepp)
            pp_printf("starting backup process with open, nid=%d.\n", gpeer_nid);
        ferr = msg_mon_start_process(gprog,                  // prog
                                     gname,                  // name
                                     NULL,                   // ret name
                                     gargc,
                                     gargp,
                                     TPT_REF(gpeer_phandle),
                                     0,                      // open
                                     &gpeer_oid,
                                     MS_ProcessType_TSE,     // type
                                     0,                      // priority
                                     0,                      // debug
                                     1,                      // backup
                                     &gpeer_nid,             // nid
                                     &gpeer_pid,             // pid
                                     NULL,                   // infile
                                     NULL);                  // outfile
        if (ferr == XZFIL_ERR_OK)
            break;
        printf("backup did not start error=%d\n", ferr);
        sleep(1);
    }
    TEST_CHK_FEOK(ferr);
    ferr = msg_mon_open_process_backup(gname,
                                       TPT_REF(gpeer_phandle),
                                       &gpeer_oid);
    TEST_CHK_FEOK(ferr);
    if (gverbosepp)
        pp_printf("after start_backup - peer p-id=%d/%d\n",
                  gpeer_nid, gpeer_pid);
    pp_pairinfo(true);
}
Beispiel #10
0
int cb_save_restart(int cmd, void *data)
{
    if (cmd == CMD_ENTER)
    {
        cfg_write(&current_config);

        pp_printf("\033[2J\033[;H");
        pp_printf("Rebooting device...\n");
        delay(2000000);
        asm volatile ("li t0,0");
        asm volatile ("jr t0");

        //	exit(0);
    }
Beispiel #11
0
void memtest_progress_start(char *title, int pass) {
    int j;

    /* Fill with dots. */
    pp_printf("Test %s [pass %d, errors so far: %d]\n", title, pass, errors); /* Print title. */
    progress_printed = 0;
}
Beispiel #12
0
/* A shell command, for testing write*/
int w1_eeprom_init_readcmd(uint8_t *buf, uint8_t next)
{
	static uint16_t read_cmd;
	static uint16_t num_cmd;
	uint8_t tmp_buf[W1_BUF];

	if (next == 0) {
		if (w1_read_eeprom_bus
		    (&wrpc_w1_bus, EE_BASE_INIT, tmp_buf, W1_BUF) < 0)
			return EE_RET_1WERR;
		num_cmd = (uint16_t) tmp_buf[0];
		if (num_cmd == 0xff)
			num_cmd = 0;
		read_cmd = 0;
	}
	pp_printf("Init script with %d commands\n", num_cmd);
	if (num_cmd == 0 || (num_cmd == read_cmd))
		return 0;

	read_cmd++;

	if (w1_read_eeprom_bus
	    (&wrpc_w1_bus, EE_BASE_INIT + (EE_BASE_CAL * read_cmd), buf,
	     W1_BUF) < 0)
		return EE_RET_1WERR;

	return (int32_t) strlen((char *)buf);
}
Beispiel #13
0
/*SoftPLL must be locked prior calling this function*/
static int calib_t24p_slave(uint32_t *value)
{
	int rv;
	uint32_t prev;

	rxts_calibration_start();
	while (!(rv = rxts_calibration_update(value)))
		/* FIXME: timeout */;
	if (rv < 0) {
		/* Fall back on master == eeprom-or-error */
		return calib_t24p_master(value);
	}

	/*
	 * Let's see if we have a matching value in EEPROM:
	 * accept a 200ps difference, otherwise rewrite eeprom
	 */
	rv = storage_phtrans(&prev, 0 /* rd */);
	if (rv < 0 || (prev < *value - 200) || (prev > *value + 200)) {
		rv = storage_phtrans(value, 1);
		pp_printf("Wrote new t24p value: %d ps (%s)\n", *value,
			  rv < 0 ? "Failed" : "Success");
	}
	return rv;
}
Beispiel #14
0
void memtest_progress_step(size_t curr, size_t size, char c) {
    pp_printf("%d %%        \r", (curr * 100 )/  size); /* Print title. */

//    size_t chars = ((unsigned long long)curr*progress_full)/size, j;

//    for (j = 0; j < chars-progress_printed; j++) printf("%c",c);
//    progress_printed = chars;
}
Beispiel #15
0
/* erase the complete sfp data base */
int w1_eemprom_sfpdb_erase()
{
	uint8_t buf[W1_BUF];
	int n;

	if (w1_read_eeprom_bus(&wrpc_w1_bus, EE_BASE_SFP, buf, W1_BUF) < 0)
		return -1;
	n = buf[4];
	buf[4] = 0;		/*| cal_ph_trans (4B) | SFP count (1B) | */
	if (w1_write_eeprom_bus(&wrpc_w1_bus, EE_BASE_SFP, buf, W1_BUF) < 0) {
		pp_printf("Error erasing SFP DB\n");
		return -1;
	}

	pp_printf("%d SFPs in DB erased\n", n);
	return 0;
}
Beispiel #16
0
static void
print_insn_with_notes (pretty_printer *pp, const_rtx x)
{
  pp_string (pp, print_rtx_head);
  print_insn (pp, x, 1);
  pp_newline (pp);
  if (INSN_P (x) && REG_NOTES (x))
    for (rtx note = REG_NOTES (x); note; note = XEXP (note, 1))
      {
	pp_printf (pp, "%s      %s ", print_rtx_head,
		   GET_REG_NOTE_NAME (REG_NOTE_KIND (note)));
	if (GET_CODE (note) == INT_LIST)
	  pp_printf (pp, "%d", XINT (note, 0));
	else
	  print_pattern (pp, XEXP (note, 0), 1);
	pp_newline (pp);
      }
}
Beispiel #17
0
static int cmd_verbose(const char *args[])
{
	int v;
	v = args[0][0] - '0';

	if (args[0])
		pp_global_flags = pp_diag_parse((char *)args[0]);
	pp_printf("PPSI verbosity: %08lx\n", pp_global_flags);
	return 0;
}
Beispiel #18
0
/* legacy function for 'calibration force' command */
int measure_t24p(uint32_t *value)
{
	int rv;
	pp_printf("Waiting for link...\n");
	while (!ep_link_up(NULL))
		timer_delay_ms(100);

	spll_init(SPLL_MODE_SLAVE, 0, 1);
	pp_printf("Locking PLL...\n");
	while (!spll_check_lock(0))
		timer_delay_ms(100);
	pp_printf("\n");

	pp_printf("Calibrating RX timestamper...\n");
	rxts_calibration_start();

	while (!(rv = rxts_calibration_update(value))) ;
	return rv;
}
bool pp_takeover() {
    bool activate = false;

    if (gshutdown) {
    } else if (gim_backup) {
        if (gverbosepp)
            pp_printf("mark primary down, takeover and start backup.\n");
        gpeer_nid = -1;
        gpeer_pid = -1;
        gim_backup = false;
        sprintf(gdisplay_name, "%s-%d-%d-%d-P(B)", gname, gnid, gpid, getpid());
        activate = true;
    } else {
        if (gverbosepp)
            pp_printf("mark backup down and restart backup.\n");
        pp_start_backup(-1);
    }

    return activate;
}
Beispiel #20
0
/* clean up the init script */
int w1_eeprom_init_erase()
{
	uint8_t buf[W1_BUF];
	buf[0] = 0xff;

	if (w1_write_eeprom_bus(&wrpc_w1_bus, EE_BASE_INIT, buf, W1_BUF) < 0)
		return EE_RET_1WERR;

	pp_printf("Erase db of init\n");
	return 0;
}
Beispiel #21
0
static int cmd_syslog(const char *args[])
{
	char b1[32], b2[32];

	if (args[0] && !strcmp(args[0], "off")) {
		syslog_addr.daddr = 0;
		return 0;
	}
	if (!args[1]) {
		pp_printf("use: syslog <ipaddr> <macaddr> (or just \"off\"\n");
		return -1;
	}
	decode_ip(args[0], (void *)&syslog_addr.daddr);
	decode_mac(args[1], syslog_mac);
	pp_printf("Syslog parameters: %s, %s\n",
		  format_ip(b1, (void *)&syslog_addr.daddr),
		  format_mac(b2, syslog_mac));
	tics = 0; /* send the first frame immediately to the new host */
	return 0;
}
Beispiel #22
0
Datei: main.c Projekt: olofk/opa
int main() {
  int sum = 0;
  int i, j;
  
  pp_printf("I say: '%s'", hello);
  
  for (j = 0; j < 2; ++j) {
    // first iteration should be full of cache misses. second should be a rocket.
    for (i = 0; i < 16; ++i) {
      sum += buf1[i] * buf2[i] + (buf2[i] << i);
      buf1[i] = sum;
    }
  }
  
  pp_printf("Result: %x %d", sum, sum);
  
  // Run the suduko solver
  suduko();

  return sum;
}
Beispiel #23
0
/* A shell command, for testing write/read*/
static int cmd_w1_test(const char *args[])
{
	int page;
	int errors = 0;
	int blen;
	int i = 0;
	char pn[32] = "testing";
	uint8_t *b = (void *)pn;

	blen = strlen(pn);
	for (page = 0; page < 80; page++) {
		if (!w1_write_eeprom_bus(&wrpc_w1_bus, page * 0x20, b, blen)) {
			pp_printf("Page %i: success\n", page);
		} else {
			pp_printf("Page %i: error\n", page);
			errors++;
		}
	}
	pp_printf("Write Errors: %d \n", errors);

	usleep(1000 * 1000);

	for (page = 0; page < 80; page++) {
		w1_read_eeprom_bus(&wrpc_w1_bus, page * 0x20, b, 32);

		blen = strlen(pn);

		pp_printf("Page %i: ", page);
		for (i = 0; i < blen; i++)
			pp_printf("%c", pn[i]);
		pp_printf("\n");
	}
	return 0;
}
Beispiel #24
0
void sendECHO() {
	unsigned char buf[500];
	unsigned int sum;

	pp_printf("> sending ECHO packet\n");

	// ------------- Ethernet ------------
	// MAC address
	memcpy(buf+ETH_DEST,   hisMAC, 6);
	memcpy(buf+ETH_SOURCE, myMAC,  6);
	// ethertype IP
	buf[ETH_TYPE+0] = 0x08;
	buf[ETH_TYPE+1] = 0x00;

	// ------------ IP --------------
	buf[IP_VERSION] = 0x45;
	buf[IP_TOS] = 0;
	buf[IP_LEN+0] = (hisBodyLen+24) >> 8;
	buf[IP_LEN+1] = (hisBodyLen+24) & 0xff;
	buf[IP_ID+0] = 0;
	buf[IP_ID+1] = 0;
	buf[IP_FLAGS+0] = 0;
	buf[IP_FLAGS+1] = 0;
	buf[IP_TTL] = 63;
	buf[IP_PROTOCOL] = 1; /* ICMP */
	buf[IP_CHECKSUM+0] = 0;
	buf[IP_CHECKSUM+1] = 0;
	memcpy(buf+IP_SOURCE, myIP, 4);
	memcpy(buf+IP_DEST, hisIP, 4);

	// ------------ ICMP ---------
	buf[ICMP_TYPE] = 0x0; // echo reply
	buf[ICMP_CODE] = 0;
	buf[ICMP_CHECKSUM+0] = 0;
	buf[ICMP_CHECKSUM+1] = 0;
	memcpy(buf+ICMP_QUENCH, hisBody, hisBodyLen);
	if ((hisBodyLen & 1) != 0)
		buf[ICMP_QUENCH+hisBodyLen] = 0;

	sum = ipv4_checksum((unsigned short*)(buf+ICMP_TYPE), (hisBodyLen+4+1)/2);
	buf[ICMP_CHECKSUM+0] = sum >> 8;
	buf[ICMP_CHECKSUM+1] = sum & 0xff;

	sum = ipv4_checksum((unsigned short*)(buf+IP_VERSION), 10);
	buf[IP_CHECKSUM+0] = sum >> 8;
	buf[IP_CHECKSUM+1] = sum & 0xff;

	tx_packet(buf, hisBodyLen+ICMP_QUENCH);
	sawPING = 0;
}
Beispiel #25
0
/* A shell command, for testing read*/
static int cmd_w1_r(const char *args[])
{
	int page;
	int i = 0;
	int blen;
	uint8_t pn[32] = "\0";

	if (args[0] == '\0') {
		pp_printf("This commands needs arguments, w1r PAGE_EEPROM\n");
		return 0;
	}

	page = atoi(args[0]);
	w1_read_eeprom_bus(&wrpc_w1_bus, page * 0x20, pn, 32);

	blen = strlen((char *)pn);

	for (i = 0; i < blen; i++)
		pp_printf("%c", pn[i]);
	pp_printf("\n");

	return 0;
}
bool pp_is_backup() {
    bool                     backup;
    int                      disable;
    int                      ferr;
    MS_Mon_Node_Info_Type    node_info;
    MS_Mon_Process_Info_Type proc_info;

    ferr = msg_mon_get_process_info_detail(gname, &proc_info);
    TEST_CHK_FEOK(ferr);
    gnid = proc_info.nid;
    gpid = proc_info.pid;
    backup = proc_info.backup ? true : false;
    if (backup) {
        gpeer_nid = proc_info.parent_nid;
        gpeer_pid = proc_info.parent_pid;
        // set this up in case register fails
        strcpy(gdisplay_name, gname);
        sprintf(&gdisplay_name[strlen(gdisplay_name)],
                "-%d-%d-%d-B", gnid, gpid, getpid());
        if (gverbosepp)
            pp_printf("we are the backup process.\n");
        disable = msg_test_assert_disable();
        ferr = msg_mon_register_death_notification(gpeer_nid, gpeer_pid);
        if (ferr != XZFIL_ERR_OK) {
            if (gverbosepp)
                pp_printf("msg_mon_register_death_notification FAILED ferr=%d.\n", ferr);
            ferr = msg_mon_get_node_info_detail(gpeer_nid, &node_info);
            myassert(ferr == XZFIL_ERR_OK);
            if (gverbosepp)
                pp_printf("node-state=%d.\n", node_info.node[0].state);
            if (node_info.node[0].state == MS_Mon_State_Shutdown)
                gshutdown = true;
        }
        msg_test_assert_enable(disable);
    }
    return backup;
}
Beispiel #27
0
Datei: main.c Projekt: olofk/opa
int my_getchar() {
  static int but = 0, old = 0;
  
  while (1) {
    unsigned int x = *stdout;

    // Report button activity
    old = but;
    but = x >> 31;
    if (but != old) {
      pp_printf("Button: %s", but?"pushed":"released");
    }
    
    // Read data from JTAG?
    if (0 != (x & 0x100)) {
      return x & 0xFF;
    }
  }
}
Beispiel #28
0
static int cmd_init(const char *args[])
{
	if (args[0] && !strcasecmp(args[0], "erase")) {
		if (w1_eeprom_init_erase() < 0)
			mprintf("Could not erase init script\n");
	} else if (args[0] && !strcasecmp(args[0], "purge")) {
		/*no purge function */
	} else if (args[1] && !strcasecmp(args[0], "add")) {

		if (w1_eeprom_init_add(args) < 0)
			mprintf("Could not add the command\n");
		else
			mprintf("Command added\n");
	} else if (args[0] && !strcasecmp(args[0], "show")) {
		if (w1_eeprom_init_show() < 0)
			pp_printf("Error reading EEPROM\n");
	} else if (args[0] && !strcasecmp(args[0], "boot")) {
		shell_boot_script();
	}

	return 0;
}
Beispiel #29
0
/*
 * I should include some incomprehensible software devepoler bullshit here
 * but I am a hardware/gatewary gay, I'll just say: needed by malloc
 *
 */
caddr_t _sbrk ( int increment ) {

    caddr_t prevHeap;
    caddr_t nextHeap;

    if (heap == NULL) {
        /* first allocation is at heap start */
        heap = (caddr_t)&_HEAP_START; 
        /*_HEAP_START value, &_HEAP_START address, be carefull with that!!! */
    }

    prevHeap = heap;

   /*
    * Although it is recommended to return data aligned on a 4 byte boundary,
    *
    * nextHeap = (caddr_t)(((unsigned int)(heap + increment) + aling) & ~aling)
    * 
    * in our case, we don't need to do it
    */

	nextHeap = (caddr_t)((unsigned int)(heap + increment));
	register caddr_t stackPtr asm ("sp");

    /*
     * What this thig intends to do is to check if it is enough space
     * or a collision with stack coming the other way takes place.
     * Our stack is above start of heap.
     */
    if ((((caddr_t)&_HEAP_START < stackPtr) && (nextHeap > stackPtr)) ||
         (nextHeap >= (caddr_t)&_HEAP_END)) {
		return NULL; /* the heap bitch ran out of space - no more memory */
    } else {
        heap = nextHeap;
        if (0)
			pp_printf("size %u\n", (unsigned int)(heap)-(unsigned int)(&_HEAP_START));
        return (caddr_t) prevHeap;
    }
}
int main(int argc, char *argv[]) {
    bool          chook = false;
    int           client = false;
    int           dinx;
    int           disable;
    int           ferr;
    int           inx;
    char          lname[10];
    bool          mq = false;
    int           oid;
    void         *res;
    bool          rnd = false;
    int           sleept;
    int           snid;
    int           spid;
    int           status;
    TAD           zargs[] =  {
        { "-chook",     TA_Bool, TA_NOMAX,    &chook      },
        { "-client",    TA_Bool, TA_NOMAX,    &client     },
        { "-dloop",     TA_Int,  MAX_OUT,     &gdloop     },
        { "-dsize",     TA_Int,  TA_NOMAX,    &gdsize     },
        { "-ksleep",    TA_Int,  TA_NOMAX,    &gksleep    },
        { "-loop",      TA_Int,  TA_NOMAX,    &gloop      },
        { "-maxc",      TA_Int,  MAX_THR,     &gmaxc      },
        { "-maxs",      TA_Int,  MAX_THR,     &gmaxs      },
        { "-mq",        TA_Bool, TA_NOMAX,    &mq         },
        { "-nodecycle", TA_Bool, TA_NOMAX,    &gnodecycle },
        { "-rnd",       TA_Bool, TA_NOMAX,    &rnd        },
        { "-server",    TA_Ign,  TA_NOMAX,    NULL        },
        { "-v",         TA_Bool, TA_NOMAX,    &gverbose   },
        { "-vpp",       TA_Bool, TA_NOMAX,    &gverbosepp },
        { "",           TA_End,  TA_NOMAX,    NULL        }
    };

    arg_proc_args(zargs, false, argc, argv);
    if (client && chook)
        test_debug_hook("c", "c");
    msfs_util_init(&argc, &argv, msg_debug_hook);
    util_test_start(client);
    if (!client)
        setup_sig();
    ferr = msg_mon_process_startup(true);             // system messages
    TEST_CHK_FEOK(ferr);
    gargc = argc; // after msg_init - it removes args
    gargp = argv;
    sprintf(gprog, "%s/%s", getenv("PWD"), argv[0]);
    gvirtual = (getenv("SQ_VIRTUAL_NODES") != NULL);
    ferr = msg_mon_get_my_process_name(gname, sizeof(gname));
    assert(ferr == XZFIL_ERR_OK);
    if (client) {
        msg_mon_enable_mon_messages(true); // get mon messages
        ferr = msg_mon_get_process_info(NULL, &gcnid, NULL);
        assert(ferr == XZFIL_ERR_OK);
        ferr = XCONTROLMESSAGESYSTEM(XCTLMSGSYS_SETSENDLIMIT,
                                     (short) (gmaxc * gdloop));
        assert(ferr == XZFIL_ERR_OK);
        srand(1);
        sleept = 1000;
        start_server(argc, argv);
        sleep(1);
        ferr = msg_mon_open_process((char *) "$srv",              // name
                                    TPT_REF(gphandle),
                                    &oid);
        TEST_CHK_FEOK(ferr);
        ferr = XPROCESSHANDLE_DECOMPOSE_(TPT_REF(gphandle), &snid, &spid);
        TEST_CHK_FEOK(ferr);
        for (inx = 0; inx < gloop; inx++) {
            if (mq)
                printf("client: inx=%d\n", inx);
            for (dinx = 0; dinx < gmaxc; dinx++) {
                sprintf(lname, "c%d", dinx);
                gthrc[dinx] = new MyThread(client_thr, lname, dinx);
            }
            disable = msg_test_assert_disable();
            for (dinx = 0; dinx < gmaxc; dinx++)
                gthrc[dinx]->start();
            if (rnd)
                sleept = (int) ((float) gksleep * (rand_r(&gseed) / (RAND_MAX + 1.0)));
            usleep(sleept);
            for (dinx = 0; dinx < gmaxc; dinx++) {
                status = gthrc[dinx]->join(&res);
                TEST_CHK_STATUSOK(status);
                if (gverbose)
                    printf("joined with client %d\n", dinx);
            }
            for (dinx = 0; dinx < gmaxc; dinx++)
                delete gthrc[dinx];
            msg_test_assert_enable(disable);
        }
    } else {
        msg_mon_enable_mon_messages(true); // get mon messages
        ferr = XCONTROLMESSAGESYSTEM(XCTLMSGSYS_SETRECVLIMIT,
                                     XMAX_SETTABLE_RECVLIMIT);
        assert(ferr == XZFIL_ERR_OK);
        pp_init();
        if (!gshutdown) {
            for (dinx = 0; dinx < gmaxs; dinx++) {
                char lname[10];
                sprintf(lname, "s%d", dinx);
                gthrs[dinx] = new SB_Thread::Thread(server_thr, lname);
            }
            for (dinx = 0; dinx < gmaxs; dinx++)
                gthrs[dinx]->start();
            for (dinx = 0; dinx < gmaxs; dinx++) {
                status = gthrs[dinx]->join(&res);
                TEST_CHK_STATUSOK(status);
                if (gverbose)
                    printf("joined with server %d\n", dinx);
            }
        }
        if (gverbosepp)
            pp_printf("exit pid=%d\n", getpid());
    }

    if (client) {
        sleep(1);
        ferr = msg_mon_close_process(TPT_REF(gphandle));
        TEST_CHK_FEOK(ferr);
    }
    ferr = msg_mon_process_shutdown();
    TEST_CHK_FEOK(ferr);
    util_test_finish(client);
    return 0;
}