Commit f18a438c authored by Cedric Roux's avatar Cedric Roux

T: simplify

Don't use shm anymore. Use mmap directly.
parent 781133b7
...@@ -91,7 +91,7 @@ static void new_thread(void *(*f)(void *), void *data) ...@@ -91,7 +91,7 @@ static void new_thread(void *(*f)(void *), void *data)
/* defined in local_tracer.c */ /* defined in local_tracer.c */
void T_local_tracer_main(int remote_port, int wait_for_tracer, void T_local_tracer_main(int remote_port, int wait_for_tracer,
int local_socket, char *shm_file); int local_socket, void *shm_array);
/* We monitor the tracee and the local tracer processes. /* We monitor the tracee and the local tracer processes.
* When one dies we forcefully kill the other. * When one dies we forcefully kill the other.
...@@ -114,22 +114,31 @@ void T_init(int remote_port, int wait_for_tracer, int dont_fork) ...@@ -114,22 +114,31 @@ void T_init(int remote_port, int wait_for_tracer, int dont_fork)
{ {
int socket_pair[2]; int socket_pair[2];
int s; int s;
int T_shm_fd;
int child1, child2; int child1, child2;
char shm_file[128]; int i;
sprintf(shm_file, "/%s%d", T_SHM_FILENAME, getpid());
if (socketpair(AF_UNIX, SOCK_STREAM, 0, socket_pair)) if (socketpair(AF_UNIX, SOCK_STREAM, 0, socket_pair))
{ perror("socketpair"); abort(); } { perror("socketpair"); abort(); }
/* setup shared memory */
T_cache = mmap(NULL, T_CACHE_SIZE * sizeof(T_cache_t),
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (T_cache == MAP_FAILED)
{ perror("mmap"); abort(); }
/* let's garbage the memory to catch some potential problems
* (think multiprocessor sync issues, barriers, etc.)
*/
memset(T_cache, 0x55, T_CACHE_SIZE * sizeof(T_cache_t));
for (i = 0; i < T_CACHE_SIZE; i++) T_cache[i].busy = 0;
/* child1 runs the local tracer and child2 (or main) runs the tracee */ /* child1 runs the local tracer and child2 (or main) runs the tracee */
child1 = fork(); if (child1 == -1) abort(); child1 = fork(); if (child1 == -1) abort();
if (child1 == 0) { if (child1 == 0) {
close(socket_pair[1]); close(socket_pair[1]);
T_local_tracer_main(remote_port, wait_for_tracer, socket_pair[0], T_local_tracer_main(remote_port, wait_for_tracer, socket_pair[0],
shm_file); T_cache);
exit(0); exit(0);
} }
close(socket_pair[0]); close(socket_pair[0]);
...@@ -138,6 +147,7 @@ void T_init(int remote_port, int wait_for_tracer, int dont_fork) ...@@ -138,6 +147,7 @@ void T_init(int remote_port, int wait_for_tracer, int dont_fork)
child2 = fork(); if (child2 == -1) abort(); child2 = fork(); if (child2 == -1) abort();
if (child2 != 0) { if (child2 != 0) {
close(socket_pair[1]); close(socket_pair[1]);
munmap(T_cache, T_CACHE_SIZE * sizeof(T_cache_t));
monitor_and_kill(child1, child2); monitor_and_kill(child1, child2);
} }
} }
...@@ -148,34 +158,29 @@ void T_init(int remote_port, int wait_for_tracer, int dont_fork) ...@@ -148,34 +158,29 @@ void T_init(int remote_port, int wait_for_tracer, int dont_fork)
T_socket = s; T_socket = s;
/* setup shared memory */
T_shm_fd = shm_open(shm_file, O_RDWR /*| O_SYNC*/, 0666);
shm_unlink(shm_file);
if (T_shm_fd == -1) { perror(shm_file); abort(); }
T_cache = mmap(NULL, T_CACHE_SIZE * sizeof(T_cache_t),
PROT_READ | PROT_WRITE, MAP_SHARED, T_shm_fd, 0);
if (T_cache == MAP_FAILED)
{ perror(shm_file); abort(); }
close(T_shm_fd);
new_thread(T_receive_thread, NULL); new_thread(T_receive_thread, NULL);
} }
void T_Config_Init(void) void T_Config_Init(void)
{ {
int T_port; /* by default we wait for the tracer */ int T_port; /* by default we wait for the tracer */
int T_nowait; /* default port to listen to to wait for the tracer */ int T_nowait; /* default port to listen to to wait for the tracer */
int T_dont_fork; /* default is to fork, see 'T_init' to understand */ int T_dont_fork; /* default is to fork, see 'T_init' to understand */
paramdef_t ttraceparams[] = CMDLINE_TTRACEPARAMS_DESC ; paramdef_t ttraceparams[] = CMDLINE_TTRACEPARAMS_DESC;
/* for a cleaner config file, TTracer params should be defined in a specific section... */ /* for a cleaner config file, TTracer params should be defined in a
config_get( ttraceparams,sizeof(ttraceparams)/sizeof(paramdef_t),TTRACER_CONFIG_PREFIX); * specific section...
*/
config_get(ttraceparams,
sizeof(ttraceparams) / sizeof(paramdef_t),
TTRACER_CONFIG_PREFIX);
/* compatibility: look for TTracer command line options in root section */ /* compatibility: look for TTracer command line options in root section */
config_process_cmdline( ttraceparams,sizeof(ttraceparams)/sizeof(paramdef_t),NULL); config_process_cmdline(ttraceparams,
sizeof(ttraceparams) / sizeof(paramdef_t),
NULL);
if (T_stdout == 0) { if (T_stdout == 0)
T_init(T_port, 1-T_nowait, T_dont_fork); T_init(T_port, 1-T_nowait, T_dont_fork);
}
} }
...@@ -36,8 +36,6 @@ typedef struct { ...@@ -36,8 +36,6 @@ typedef struct {
int length; int length;
} T_cache_t; } T_cache_t;
#define T_SHM_FILENAME "/T_shm_segment"
/* number of VCD functions (to be kept up to date! see in T_messages.txt) */ /* number of VCD functions (to be kept up to date! see in T_messages.txt) */
#define VCD_NUM_FUNCTIONS 190 #define VCD_NUM_FUNCTIONS 190
......
...@@ -340,51 +340,31 @@ static void wait_message(void) ...@@ -340,51 +340,31 @@ static void wait_message(void)
while (T_local_cache[T_busylist_head].busy == 0) usleep(1000); while (T_local_cache[T_busylist_head].busy == 0) usleep(1000);
} }
static void init_shm(char *shm_file)
{
int i;
int s = shm_open(shm_file, O_RDWR | O_CREAT /*| O_SYNC*/, 0666);
if (s == -1) { perror(shm_file); abort(); }
if (ftruncate(s, T_CACHE_SIZE * sizeof(T_cache_t)))
{ perror(shm_file); abort(); }
T_local_cache = mmap(NULL, T_CACHE_SIZE * sizeof(T_cache_t),
PROT_READ | PROT_WRITE, MAP_SHARED, s, 0);
if (T_local_cache == MAP_FAILED)
{ perror(shm_file); abort(); }
close(s);
/* let's garbage the memory to catch some potential problems
* (think multiprocessor sync issues, barriers, etc.)
*/
memset(T_local_cache, 0x55, T_CACHE_SIZE * sizeof(T_cache_t));
for (i = 0; i < T_CACHE_SIZE; i++) T_local_cache[i].busy = 0;
}
void T_local_tracer_main(int remote_port, int wait_for_tracer, void T_local_tracer_main(int remote_port, int wait_for_tracer,
int local_socket, char *shm_file) int local_socket, void *shm_array)
{ {
int s; int s;
int port = remote_port; int port = remote_port;
int dont_wait = wait_for_tracer ? 0 : 1; int dont_wait = wait_for_tracer ? 0 : 1;
void *f; void *f;
printf("local tracer starting\n");
/* write on a socket fails if the other end is closed and we get SIGPIPE */ /* write on a socket fails if the other end is closed and we get SIGPIPE */
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR){ if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
printf("local tracer received SIGPIPE\n"); printf("local tracer received SIGPIPE\n");
abort(); abort();
} }
T_local_cache = shm_array;
init_shm(shm_file);
s = local_socket; s = local_socket;
printf("local tracer starting step 2\n");
if (dont_wait) { if (dont_wait) {
char t = 2; char t = 2;
printf("local tracer in no wait mode \n");
if (write(s, &t, 1) != 1) abort(); if (write(s, &t, 1) != 1) abort();
} }
printf("local tracer starting step 3\n");
f = forwarder(port, s); f = forwarder(port, s);
printf("local tracer main loop.... \n");
/* read messages */ /* read messages */
while (1) { while (1) {
wait_message(); wait_message();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment