From f18a438c2cc37a99c170df7ca772ee82015e7439 Mon Sep 17 00:00:00 2001 From: Cedric Roux <cedric.roux@eurecom.fr> Date: Thu, 9 Aug 2018 12:55:44 +0200 Subject: [PATCH] T: simplify Don't use shm anymore. Use mmap directly. --- common/utils/T/T.c | 57 +++++++++++++++++++---------------- common/utils/T/T_defs.h | 2 -- common/utils/T/local_tracer.c | 38 ++++++----------------- 3 files changed, 40 insertions(+), 57 deletions(-) diff --git a/common/utils/T/T.c b/common/utils/T/T.c index 8679a4185f..40af79c1e1 100644 --- a/common/utils/T/T.c +++ b/common/utils/T/T.c @@ -91,7 +91,7 @@ static void new_thread(void *(*f)(void *), void *data) /* defined in local_tracer.c */ 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. * 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) { int socket_pair[2]; int s; - int T_shm_fd; int child1, child2; - char shm_file[128]; - - sprintf(shm_file, "/%s%d", T_SHM_FILENAME, getpid()); + int i; if (socketpair(AF_UNIX, SOCK_STREAM, 0, socket_pair)) { 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 = fork(); if (child1 == -1) abort(); if (child1 == 0) { close(socket_pair[1]); T_local_tracer_main(remote_port, wait_for_tracer, socket_pair[0], - shm_file); + T_cache); exit(0); } close(socket_pair[0]); @@ -138,6 +147,7 @@ void T_init(int remote_port, int wait_for_tracer, int dont_fork) child2 = fork(); if (child2 == -1) abort(); if (child2 != 0) { close(socket_pair[1]); + munmap(T_cache, T_CACHE_SIZE * sizeof(T_cache_t)); monitor_and_kill(child1, child2); } } @@ -148,34 +158,29 @@ void T_init(int remote_port, int wait_for_tracer, int dont_fork) 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); } void T_Config_Init(void) { -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_dont_fork; /* default is to fork, see 'T_init' to understand */ + 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_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... */ - config_get( ttraceparams,sizeof(ttraceparams)/sizeof(paramdef_t),TTRACER_CONFIG_PREFIX); + /* for a cleaner config file, TTracer params should be defined in a + * specific section... + */ + config_get(ttraceparams, + sizeof(ttraceparams) / sizeof(paramdef_t), + TTRACER_CONFIG_PREFIX); -/* compatibility: look for TTracer command line options in root section */ - config_process_cmdline( ttraceparams,sizeof(ttraceparams)/sizeof(paramdef_t),NULL); + /* compatibility: look for TTracer command line options in root section */ + 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); - } } diff --git a/common/utils/T/T_defs.h b/common/utils/T/T_defs.h index b0b9940c30..6fe11bcfc3 100644 --- a/common/utils/T/T_defs.h +++ b/common/utils/T/T_defs.h @@ -36,8 +36,6 @@ typedef struct { int length; } 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) */ #define VCD_NUM_FUNCTIONS 190 diff --git a/common/utils/T/local_tracer.c b/common/utils/T/local_tracer.c index c331daa4a9..19d0a4d717 100644 --- a/common/utils/T/local_tracer.c +++ b/common/utils/T/local_tracer.c @@ -340,51 +340,31 @@ static void wait_message(void) 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, - int local_socket, char *shm_file) + int local_socket, void *shm_array) { int s; int port = remote_port; int dont_wait = wait_for_tracer ? 0 : 1; void *f; - printf("local tracer starting\n"); + /* 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"); abort(); - } + } + + T_local_cache = shm_array; - init_shm(shm_file); s = local_socket; - printf("local tracer starting step 2\n"); + if (dont_wait) { char t = 2; - printf("local tracer in no wait mode \n"); if (write(s, &t, 1) != 1) abort(); } - printf("local tracer starting step 3\n"); + f = forwarder(port, s); - printf("local tracer main loop.... \n"); + /* read messages */ while (1) { wait_message(); -- 2.26.2