Skip to content

Commit

Permalink
automatic SHM network startup/shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
pahihu committed May 26, 2023
1 parent b7f9ec3 commit c2a24fd
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Makefile.macOS
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# CC = clang-mp-15 -flto -ffunction-sections -fdata-sections -Wl,-dead_strip
# CC = clang-mp-15 -flto -fprofile-generate
# CC = clang-mp-15 -flto -fprofile-use=t4.profdata -ffunction-sections -fdata-sections -Wl,-dead_strip
CC = gcc-mp-12 -ffloat-store -frounding-math -flto -ffunction-sections -fdata-sections -Wl,-dead_strip
# CC = gcc-mp-12 -ffloat-store -frounding-math -flto -ffunction-sections -fdata-sections -Wl,-dead_strip
# CC = gcc-mp-12 -ffloat-store -frounding-math -fprofile-generate -flto
# CC = gcc-mp-12 -ffloat-store -frounding-math -fprofile-use -flto
CC = gcc-mp-12 -ffloat-store -frounding-math -fprofile-use -flto -ffunction-sections -fdata-sections -Wl,-dead_strip
#
# 64bit emulator on Linux-x64, 32bit emulator on Linux-armhf
# CC = gcc -ffloat-store -frounding-math
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ The compiled binaries are in the `bin/` directory.
Changes
=======

* startup/shutdown of SHM link transputer networks using the `-sn` flag
* binaries use profile guided optimization (PGO)
* added some instruction combinations, which are detected at runtime
* simplified OReg clearing
Expand Down
Binary file modified bin/t4netlnx64
Binary file not shown.
Binary file modified bin/t4netmac64
Binary file not shown.
Binary file modified bin/t4netwin64.exe
Binary file not shown.
112 changes: 105 additions & 7 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
#ifdef _MSC_VER
#include <io.h>
#include <fcntl.h>
#include <process.h>
typedef intptr_t pid_t;
#else
#include <unistd.h>
#endif
#include "t4debug.h"
#include "processor.h"
Expand Down Expand Up @@ -80,6 +84,8 @@ int copy = FALSE;
int exitonerror = false;
int peeksize = 8;
int nodeid = -1;
int nxputers = 0;
pid_t xputers[1024 + 1];

int membits = 0;

Expand Down Expand Up @@ -138,6 +144,45 @@ enum {INIT, GETK, POLL} t_state;
#endif
#endif

#ifdef _MSC_VER
pid_t SpawnVP (const char *file, char *argv[])
{
return _spawnvp (_P_NOWAIT, file, argv);
}
#else
pid_t SpawnVP (const char *file, char *argv[])
{
pid_t child = fork();

if (child < 0)
return child;

if (0 == child)
{
execvp (file, argv);
handler (-1);
}
return child;
}
#endif

void stop_xputers (void)
{
extern u_char *SharedEvents;

if (nxputers && SharedEvents)
{
int i;

for (i = 1; -1 != xputers[i]; i++)
{
SharedEvents[i] = 1;
xputers[i] = -1;
}
}
}


/* 201113AP Gavin Crate */
int isswopt(int c)
{
Expand Down Expand Up @@ -185,7 +230,7 @@ int main (int argc, char **argv)
{
static char CopyFileName[256];
static char InpFileName[256], OutFileName[256];
char IBoardSize[32];
char IBoardSize[32], SpyNet[256];
int reset = FALSE;
int arg;
int temp;
Expand Down Expand Up @@ -229,9 +274,9 @@ int main (int argc, char **argv)
#ifndef NDEBUG
printf(" -sg Halt on uninitialized memory read.\n");
#endif
printf(" -sl Links in shared memory.\n");
printf(" -sl Use SHM (shared memory) links.\n");
printf(" -sm #bits Memory size in address bits (default 21, 2Mbyte).\n");
printf(" -sn id Node ID.\n");
printf(" -sn id Node ID. (If id < 0, start SHM network with -id nodes).\n");
#ifdef T4PROFILE
printf(" -su Instruction profiling.\n");
#endif
Expand Down Expand Up @@ -385,7 +430,7 @@ int main (int argc, char **argv)
}
if ((membits < 16) || (membits > 30))
{
printf ("\nMemory bits should be in [16,30] range!\n");
printf ("\nMemory bits should be in range [16,30] (64K,1024M)!\n");
handler (-1);
}
}
Expand All @@ -408,9 +453,21 @@ int main (int argc, char **argv)
printf ("\nBad number after -sn\n");
handler (-1);
}
if ((nodeid < 0) || (nodeid > 8191))
if (nodeid < 0)
{
/* Network startup */
nxputers = -nodeid;
if ((nxputers < 1) || (nxputers > 1024))
{
printf ("\nNumber of transputers should be in range [1,1024]!\n");
handler (-1);
}
nodeid = 0;
shlinks = TRUE;
}
if ((nodeid < 0) || (nodeid > 1024))
{
printf ("\nNode ID should be in [0,8191] range!\n");
printf ("\nNode ID should be in range [0,1023]!\n");
handler (-1);
}
}
Expand Down Expand Up @@ -629,6 +686,40 @@ int main (int argc, char **argv)
if (verbose)
printf ("-I-EMU414: Done.\n");
fclose (NetIn);

if (nxputers)
{
char *cmdargs[8];
char snid[8];
int i;

for (i = 1; i < 1025; i++)
xputers[i] = -1;

sprintf (SpyNet, "SPYNET=%s", NetConfigName);
putenv (SpyNet);
for (i = 1; i < nxputers; i++)
{
sprintf (snid, "%d", i);

cmdargs[0] = "t4";
cmdargs[1] = "-sl";
cmdargs[2] = "-sn";
cmdargs[3] = snid;
cmdargs[4] = 800 == Txxx ? "-s8" : NULL;
cmdargs[5] = NULL;
if ((xputers[i] = SpawnVP ("t4", cmdargs)) < 0)
{
printf ("-E-EMU414: Failed to start processor %d!\n", i);
handler (-1);
}
printf ("-I-EMU414: Started processor %d (pid=%d)\n", i, xputers[i]);
if (1 == i)
sleep (1);
}
if (nxputers > 2)
sleep (1);
}
}

/* Initialize processor. */
Expand Down Expand Up @@ -768,12 +859,16 @@ int main (int argc, char **argv)
#endif

/* Close boot file. */
fclose (CopyIn);
if (CopyIn)
fclose (CopyIn);
if (InpFile)
fclose (InpFile);
if (OutFile)
fclose (OutFile);


stop_xputers ();

close_channels ();

#ifdef T4PROFILE
Expand Down Expand Up @@ -846,8 +941,11 @@ void handler (int signal)
if (OutFile)
fclose (OutFile);

stop_xputers ();

close_channels ();


#ifdef CURTERM
prepterm (0);
exit (-1);
Expand Down
4 changes: 2 additions & 2 deletions netcfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ typedef struct _NETURL {
char *url;
} NETURL;

#define MAX_NETURL 1024
#define MAX_NETURL 4096
NETURL NetURLs[MAX_NETURL];
int nNetURL;

Expand All @@ -27,7 +27,7 @@ typedef struct _NETLINK {
int othernode, otherlink;
} NETLINK;

#define MAX_NETLINK (MAX_NETURL * 4)
#define MAX_NETLINK MAX_NETURL
NETLINK NetLinks[MAX_NETLINK];
int nNetLink;

Expand Down
32 changes: 28 additions & 4 deletions p.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ int32_t quitstatus;
void handler (int);

u_char *SharedLinks;
u_char *SharedEvents;

#define StrPrio(p) ((p) ? "Lo" : "Hi")

Expand Down Expand Up @@ -1197,6 +1198,7 @@ void close_channels (void)
if (SharedLinks)
{
shlink_detach (SharedLinks);
SharedLinks = NULL; SharedEvents = NULL;
if (0 == nodeid)
shlink_free ();

Expand Down Expand Up @@ -1300,11 +1302,16 @@ void open_channel (uint32_t addr)
if (NULL == SharedLinks)
{
if ((1 == nodeid) || (0 == maxnode))
SharedLinks = shlink_alloc (NetConfigName, sharedSize);
{
SharedLinks = shlink_alloc (NetConfigName, sharedSize + 1024);
if (SharedLinks)
memset (SharedLinks, 0, sharedSize + 1024);
}
else
SharedLinks = shlink_attach (NetConfigName, sharedSize);
SharedLinks = shlink_attach (NetConfigName, sharedSize + 1024);
if (NULL == SharedLinks)
handler (-1);
SharedEvents = SharedLinks + sharedSize;
}
if (chanIn)
{
Expand Down Expand Up @@ -1700,10 +1707,20 @@ void mainloop (void)

/* Move timers on if necessary, and increment timeslice counter. */
if (++count1 > delayCount1)
{
if (SharedEvents && (nodeid >= 0))
{
if (SharedEvents[nodeid])
return;
}
update_time ();
}

if (ReadGotoSNP)
start_process ();
{
if (start_process ())
return;
}

/* Execute an instruction. */
ResetRounding = FALSE;
Expand Down Expand Up @@ -4439,7 +4456,7 @@ int run_process (void)
}

/* Start a process. */
void start_process (void)
int start_process (void)
{
int active, links_active;

Expand Down Expand Up @@ -4498,6 +4515,11 @@ void start_process (void)
active = TRUE;
break;
}
if (SharedEvents && (nodeid >= 0))
{
if (SharedEvents[nodeid])
return 1;
}
} while (active);

if (!active)
Expand All @@ -4511,6 +4533,8 @@ void start_process (void)
timeslice = 0;

PROFILE(profile[PRO_STARTP]++);

return 0;
}

/* Save the current process and start a new process. */
Expand Down
2 changes: 1 addition & 1 deletion processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ void operate (void);
void schedule (uint32_t);
void UpdateWdescReg (uint32_t);
int run_process (void);
void start_process (void);
int start_process (void);
void deschedule (void);
void D_check (void);
void interrupt (void);
Expand Down
18 changes: 9 additions & 9 deletions shlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void *shlink_attach (const char *fnm, int size)
}

if (verbose)
printf ("-I-EMU414: Attached SharedChannels at #%p.\n", addr);
printf ("-I-EMU414: Attached SHMLinks at #%p.\n", addr);

return addr;
}
Expand Down Expand Up @@ -96,7 +96,7 @@ void *shlink_alloc (const char *fnm, int size)
}

if (verbose)
printf ("-I-EMU414: Allocated SharedChannels at #%p.\n", addr);
printf ("-I-EMU414: Allocated SHMLinks at #%p.\n", addr);
return addr;
}

Expand Down Expand Up @@ -135,19 +135,19 @@ void* shlink_attach (const char *fnm, int size)
if (shmid == -1)
{
ret = errno;
printf ("-E-EMU414: Cannot get SharedChannels (%s).\n", strerror (ret));
printf ("-E-EMU414: Cannot get SHMLinks (%s).\n", strerror (ret));
return NULL;
}

addr = shmat (shmid, 0, 0);
if (addr == (void *)-1)
{
ret = errno;
printf ("-E-EMU414: Unable to attach SharedChannels (%s).\n", strerror (ret));
printf ("-E-EMU414: Unable to attach SHMLinks (%s).\n", strerror (ret));
return NULL;
}
if (verbose)
printf ("-I-EMU414: Attached SharedChannels at #%p.\n", addr);
printf ("-I-EMU414: Attached SHMLinks at #%p.\n", addr);
return addr;
}

Expand All @@ -172,29 +172,29 @@ void* shlink_alloc (const char *fnm, int size)
shmid = shmget (shmkey, 0, 0);
if (shmid != -1)
{
printf ("-E-EMU414: SharedChannels already exists.\n");
printf ("-E-EMU414: SHMLinks already exists.\n");
return NULL;
}

shmid = shmget (shmkey, size, (SHM_R|SHM_W|(SHM_R>>3) |(SHM_W>>3)|IPC_CREAT));
if (shmid == -1)
{
printf ("-E-EMU414: Unable to create SharedChannels (%s).\n", strerror (errno));
printf ("-E-EMU414: Unable to create SHMLinks (%s).\n", strerror (errno));
return NULL;
}

addr = shmat (shmid, 0, 0);
if (addr == (void *)-1)
{
printf ("-E-MU414: Unable to attach to SharedChannels (%s).\n", strerror (errno));
printf ("-E-EMU414: Unable to attach to SHMLinks (%s).\n", strerror (errno));
shmctl (shmid, (IPC_RMID), &sbuf);
return NULL;
}

memset (addr, 0, size);

if (verbose)
printf ("-I-EMU414: Allocated SharedChannels at #%p.\n", addr);
printf ("-I-EMU414: Allocated SHMLinks at #%p.\n", addr);
return addr;
}

Expand Down

0 comments on commit c2a24fd

Please sign in to comment.