visual.c
15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/*
* Copyright (C) 1996-1998 by the Board of Trustees
* of Leland Stanford Junior University.
*
* This file is part of the SimOS distribution.
* See LICENSE file for terms of the license.
*
*/
/* ***********************************************************
* visual.c
* TCP/IP connection to let SimOS talk to an outside
* visualization process
* ***********************************************************/
/* Statistics report and control module for simos.
* Creates an independent process (the "control process") that
* responds to requests over a socket asynchronously from the
* simulation itself.
*
* The control process also communicates with the simos process.
* Currently this is used to do real-time statisitics reporting.
* This could be easily adapting to adding a control interface
* to simos to, for example, change cpu simulators during a
* simulation.
*/
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include "visual.h"
#include "sim_error.h"
#include "eventcallback.h"
#include "cpu_interface.h"
#include "gdb_interface.h"
#include "simutil.h"
#include "tcl_init.h"
#include "machine_params.h"
extern int getdtablesize(void);
#define VISUAL_MAXCONN 4
#define VISUAL_MAXSTREAM 8
#define MINBUFFER_SIZE (16 * 1024)
typedef struct {
char name[64];
int sd; /* socket */
} VisualStream;
static struct VisualState {
int maxId;
int listenSD;
EventCallbackHdr eventCB;
int context;
struct Connection {
char *host;
int port;
int sd; /* socket */
VisualStream stream[VISUAL_MAXSTREAM];
int id;
int bufSize;
char *buf;
int pos;
} conn[ VISUAL_MAXCONN];
} visualState;
static void VisualCallback( int cpuNum, EventCallbackHdr *hdr, void *arg);
static bool VisualControl (void) ;
static int PushString(int sd,char *string);
int VisualPort = 0;
int VisualSamplePeriod = 50000;
static int pauseSimulation = FALSE;
static struct Connection *VisualGetConnection(void);
static int VisualOpenStream(char *name, char *hostname, int portnumber);
static int VisualCloseStream(char *name);
int VisualOpenCmd(Tcl_Interp *interp, int argc, char *argv[]);
int VisualPutsCmd(Tcl_Interp *interp, int argc, char *argv[]);
int VisualCloseCmd(Tcl_Interp *interp, int argc, char *argv[]);
static tclcmd visualCmds[] = {
{ "open", 5, VisualOpenCmd, " open name host port"},
{ "close", 3, VisualCloseCmd, " close name"},
{ "puts", 4, VisualPutsCmd, " puts name string"}
};
/*
* VisualInitComm -- To initialize this module, three separate calls
* must be made. This routine should be called before the control
* process is forked. After the fork, the simos process should call
* SimCntrl_InitSimComm, and the control process SimCntrl_InitCntrlComm.
*
* These calls setup a communication mechanism between the simos and
* control processes, and create a socket to listen for incoming
* control connections. The socket address is the first available
* socket with number SIMOS_CNTRL_PORT or greater.
*
* Return value -- -1 on error, 0 otherwise.
*/
void
Visual_Init(void) {
int isError = 0;
struct sockaddr_in name;
int portToTry;
bool foundPort;
visualState.maxId = 1;
visualState.context = -1;
if (!VisualPort) {
/* CPUWarning("SIMOS: VisualPort not set: -- visual output not enabled \n"); */
return;
}
if( !isError ) {
visualState.listenSD = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if (visualState.listenSD < 0 ) {
perror( "opening request socket" );
}
}
if( !isError ) {
bzero((char *)&name, sizeof(struct sockaddr_in));
name.sin_family = AF_INET;
name.sin_addr.s_addr = INADDR_ANY;
portToTry = VisualPort;
foundPort = FALSE;
ASSERT (portToTry);
do {
name.sin_port = htons( portToTry );
isError = bind(visualState.listenSD , (void*)&name, sizeof(name) );
if( !isError ) {
foundPort = TRUE;
CPUWarning( "SIMOS: visual port: %d\n", portToTry );
} else {
switch( errno ) {
case EADDRNOTAVAIL:
case EADDRINUSE:
portToTry++;
break;
default:
foundPort = TRUE;
perror( "bind request socket" );
break;
}
}
} while( !foundPort );
}
if( !isError ) {
isError = listen( visualState.listenSD, 1 );
if( isError ) {
perror( "listen" );
}
}
/* pauseSimulation -- if true, SimOS stays in VisualControl,
suspending the simulation and accepting Tcl commands from the front end */
Tcl_LinkVar(TCLInterp, "PauseSimulation", (char *) &pauseSimulation,
TCL_LINK_BOOLEAN);
Tcl_CreateCommand(TCLInterp, "visual", DispatchCmd,
(ClientData)visualCmds, (Tcl_CmdDeleteProc*)NULL);
VisualCallback(0,&visualState.eventCB,0);
}
static void
VisualCallback(int cpuNum, EventCallbackHdr *hdr, void *arg)
{
bool poll = TRUE;
/* Keep calling VisualControl until it performs no actions --
this allows multiple requests on the same socket to be serviced */
while (poll || pauseSimulation) {
poll = VisualControl();
}
EventDoCallback(cpuNum,VisualCallback,hdr,arg,VisualSamplePeriod);
}
/*
* accept the new connection
*/
void
VisualAccept(int listenPort)
{
int con;
int in,fromAddrLen;
struct sockaddr_in fromAddr;
struct hostent *fromHost;
for(con=0;con<VISUAL_MAXCONN && visualState.conn[con].sd ; con++)
continue;
fromAddrLen = sizeof( fromAddr );
if (con == VISUAL_MAXCONN ) {
CPUWarning("\nSIMOS: ALREADY HAVE %d visualization connections. Drop this one \n",
VISUAL_MAXCONN);
return;
}
in = accept( listenPort, (struct sockaddr *)&fromAddr, &fromAddrLen );
if (in < 0 ) {
perror("accepting connection");
}
visualState.conn[con].sd = in;
fromHost = gethostbyaddr(
(void*)&fromAddr.sin_addr,
sizeof(fromAddr.sin_addr),
fromAddr.sin_family
);
if( fromHost == NULL ) {
perror( "resolving connecting hostname" );
visualState.conn[con].host = SaveString( "<unavailable>" );
} else {
visualState.conn[con].host = SaveString(fromHost->h_name);
}
visualState.conn[con].port = fromAddr.sin_port;
visualState.conn[con].id = visualState.maxId++;
CPUWarning("Control: connect from %s:%d for connId=%d socket=%d\n",
visualState.conn[con].host,
visualState.conn[con].port,
visualState.conn[con].id,
visualState.conn[con].sd);
}
static void
VisualConnect(struct Connection *conn, char *buf)
{
struct hostent *dest_host;
struct sockaddr_in dest_socket;
char hostname[32];
int portnumber;
if (sscanf(buf,"VISUALstream %s %d",hostname,&portnumber) !=2 ) {
CPUError("expecting <stream host port> got <%s>\n",buf);
}
if (VisualGetStream("log")) {
VisualCloseStream("log");
}
if (VisualOpenStream("log", hostname, portnumber) == 0) {
CPUWarning("Error opening VISUALstream %s %d\n", hostname, portnumber);
}
}
static char *
VisualRPC(struct Connection *conn, char *buf)
{
int code;
int ok = 0;
int context = VisualSetContext(conn->id, &ok);
SIM_DEBUG(('v', "VISUALrpc: (%2d)%s",conn->id,buf));
/*
* Special case stream. I am too lazy to write the Tcl command for now
*/
if (!strncmp(buf,"VISUALstream",strlen("VISUALstream"))) {
VisualConnect(conn,buf);
return "OK";
}
code = Tcl_Eval(TCLInterp, buf);
VisualSetContext(context, &ok);
if (code==TCL_OK || *TCLInterp->result) {
return TCLInterp->result;
} else {
return "ERR";
}
}
/* *****************************************************************************
* VisualCmd(Connection *)
* ****************************************************************************/
static void
VisualCmd(struct Connection *conn)
{
int i,j;
char *result;
char readBuffer = 'x';
/*
*for now, screw the non-blocking stuff.
* Block until script has been received
*/
if (!conn->buf) {
conn->buf = malloc(MINBUFFER_SIZE);
conn->bufSize = MINBUFFER_SIZE;
}
conn->pos = 0;
for(;;) {
if (conn->pos >= MINBUFFER_SIZE-2) {
char *str;
conn->bufSize *= 2;
str = malloc(conn->bufSize);
strcpy(str,conn->buf);
free(conn->buf);
conn->buf = str;
}
i = read(conn->sd,&readBuffer,1);
if (i < 0 ) {
perror("VisualCmd (read)");
return; /* notreached */
}
if (i==0) {
/*
* User-specific closing too ?
*/
conn->sd = 0;
conn->id = 0;
for (j = 0; j < VISUAL_MAXSTREAM; j++) {
if (conn->stream[j].sd) {
close(conn->stream[j].sd);
conn->stream[j].sd = 0;
}
}
return;
}
conn->buf[conn->pos++] = readBuffer;
if (readBuffer == '\n') {
conn->buf[conn->pos] = '\0';
if (Tcl_CommandComplete(conn->buf)) {
result = VisualRPC(conn,conn->buf);
SIM_DEBUG(('v', "VisualRPC -> %s\n",result));
if (!*result) {
result = "OK";
}
PushString(conn->sd,result);
return;
}
}
}
}
/*
* VisualControl
*/
static bool
VisualControl(void)
{
fd_set reads;
int i;
bool return_val;
struct timeval timeout;
static int width = -1;
/* debug subsystem needs to poll its incoming connection at a
* similar frequency as visual, let's use the same select() call
*/
int debug_fd;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
if (!visualState.listenSD) {
return FALSE;
}
FD_ZERO(&reads );
FD_SET (visualState.listenSD, &reads );
for (i=0;i<VISUAL_MAXCONN;i++) {
if (visualState.conn[i].sd) {
FD_SET(visualState.conn[i].sd,&reads);
}
}
debug_fd = Simdebug_pollfd();
if (debug_fd != -1) {
FD_SET(debug_fd,&reads);
}
if (width < 0) {
width = getdtablesize();
}
i = select( width, &reads, NULL, NULL,&timeout );
if (i<0) {
perror("VisualControl select");
return FALSE;
} else if (i == 0) {
return FALSE;
}
/* return value TRUE indicates go around the loop again.
* we can't do this in the case of calling the debugger
* because the pending data might not get sucked in here.
*/
return_val = TRUE;
if (FD_ISSET( visualState.listenSD, &reads ) ) {
VisualAccept(visualState.listenSD);
}
for (i=0;i<VISUAL_MAXCONN;i++) {
if (visualState.conn[i].sd &&
FD_ISSET(visualState.conn[i].sd,&reads)) {
VisualCmd(&visualState.conn[i]);
}
}
if (FD_ISSET( debug_fd, &reads)) {
Simdebug_fdactive();
return_val = FALSE;
}
return return_val;
}
/*
* VisualContext.
* This keeps track and restores the context of execution of the currently
* interpreted script.
* The context of an annotation is inherenited from the context that installed
* it
*/
int
VisualGetContext(void)
{
return visualState.context;
}
int
VisualSetContext(int context , int *old)
{
int i;
if (old) {
*old = visualState.context;
}
if (context > 0) {
for(i=0;i<VISUAL_MAXCONN;i++) {
if (visualState.conn[i].id == context) {
visualState.context = context;
return 1;
}
}
return 0;
}
visualState.context = context;
return 1;
}
static int
PushString(int sd, char *string)
{
int len = strlen(string);
char strl[16];
int nc = PrintLLD(strl,(uint64)len);
sprintf(strl+nc, " ");
if( write(sd,strl,8)!=8) {
perror("PushString-len");
return 0;
}
if (write(sd,string,len) != len) {
perror("PushString");
return 0;
}
return len;
}
void
VisualPushString(char *string)
{
int sd = VisualGetStream("log");
if (sd <= 0) {
CPUWarning("VISUAL: Trying to push string to non-opened stream: %s\n",
string);
return;
}
PushString(sd, string);
SIM_DEBUG(('v', "VisualPushString: %s ", string));
}
static struct Connection *
VisualGetConnection(void)
{
int i;
int ctxt = VisualGetContext();
for (i=0;i<VISUAL_MAXCONN;i++) {
if (ctxt == visualState.conn[i].id) {
return &(visualState.conn[i]);
}
}
return NULL;
}
static int
VisualOpenStream(char *name, char *hostname, int portnumber)
{
int s, sd;
struct hostent *dest_host;
struct sockaddr_in dest_socket;
struct Connection *conn = VisualGetConnection();
if (conn == NULL) {
CPUWarning("Must issue visual command from remote connection\n");
return 0;
}
if (VisualGetStream(name) > 0) {
CPUWarning("Stream %s is already open\n", name);
return 0;
}
for (s = 0; (s < VISUAL_MAXSTREAM) && (conn->stream[s].sd > 0); s++) {}
if (s == VISUAL_MAXSTREAM) {
CPUWarning("Cannot open any more streams on this connection\n");
return 0;
}
dest_host = gethostbyname(hostname);
if( dest_host == NULL ) {
CPUWarning("Error resolving stream host name\n");
return 0;
}
dest_socket.sin_family = dest_host->h_addrtype;
dest_socket.sin_port = htons((u_short) portnumber);
bcopy((void*)dest_host->h_addr_list[0],
(void*)&dest_socket.sin_addr,
dest_host->h_length);
sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (connect(sd, (struct sockaddr *)&dest_socket, sizeof(dest_socket))) {
CPUWarning("Error connecting to data stream socket\n");
return 0;
}
ASSERT(sd >= 0);
conn->stream[s].sd = sd;
strcpy(conn->stream[s].name, name);
CPUWarning("Connecting to data stream socket '%s' on %s:%d\n",
name, hostname, portnumber);
return sd;
}
int
VisualGetStream(char *name)
{
int s;
struct Connection *conn = VisualGetConnection();
if (conn == NULL) {
CPUWarning("Must issue visual command from remote connection\n");
return 0;
}
for (s = 0; s < VISUAL_MAXSTREAM; s++) {
if (!strcmp(conn->stream[s].name, name)) {
return conn->stream[s].sd;
}
}
return 0;
}
static int
VisualCloseStream(char *name)
{
int s;
struct Connection *conn = VisualGetConnection();
if (conn == NULL) {
CPUWarning("Must issue visual command from remote connection\n");
return 0;
}
for (s = 0; s < VISUAL_MAXSTREAM; s++) {
if (!strcmp(conn->stream[s].name, name)) {
close(conn->stream[s].sd);
conn->stream[s].sd = 0;
return 1;
}
}
CPUWarning("Cannot close nonexistent stream %s\n", name);
return 0;
}
int
VisualOpenCmd(Tcl_Interp *interp, int argc, char *argv[])
{
char *name;
char *hostname;
int portnumber;
name = argv[2];
hostname = argv[3];
if (Tcl_GetInt(interp, argv[4], &portnumber) != TCL_OK) {
return TCL_ERROR;
}
if (VisualOpenStream(name, hostname, portnumber) == 0) {
CPUWarning("Error opening stream %s\n", name);
return TCL_ERROR;
}
return TCL_OK;
}
int
VisualCloseCmd(Tcl_Interp *interp, int argc, char *argv[])
{
if (VisualCloseStream(argv[2])) {
return TCL_OK;
} else {
return TCL_ERROR;
}
}
int
VisualPutsCmd(Tcl_Interp *interp, int argc, char *argv[])
{
char *name = argv[2];
char *string = argv[3];
int sd = VisualGetStream(name);
if (sd == 0) {
CPUWarning("visual puts: invalid stream identifier %s\n", name);
return TCL_ERROR;
}
if (PushString(sd, string) == 0) {
/*return TCL_ERROR;*/
return TCL_OK;
} else {
return TCL_OK;
}
}