From 94352664ec6e9949778a4cdfacb09f34af909de4 Mon Sep 17 00:00:00 2001 From: donassbr Date: Fri, 20 Apr 2007 15:50:47 +0000 Subject: [PATCH] Initial structure to implement gras using simix. Modifie Makefile.am to compile the "new" gras if --enable-simix is used. git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@3437 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- src/Makefile.am | 47 +- src/gras_simix/DataDesc/gras_simix_cbps.c | 391 ++++ src/gras_simix/DataDesc/gras_simix_datadesc.c | 250 +++ .../DataDesc/gras_simix_datadesc_interface.h | 43 + .../DataDesc/gras_simix_datadesc_private.h | 265 +++ .../DataDesc/gras_simix_ddt_convert.c | 269 +++ .../DataDesc/gras_simix_ddt_create.c | 1042 +++++++++ .../DataDesc/gras_simix_ddt_exchange.c | 972 ++++++++ .../DataDesc/gras_simix_ddt_parse.c | 752 +++++++ .../DataDesc/gras_simix_ddt_parse.yy.c | 1985 +++++++++++++++++ .../DataDesc/gras_simix_ddt_parse.yy.h | 71 + .../DataDesc/gras_simix_ddt_parse.yy.l | 240 ++ src/gras_simix/Msg/gras_simix_msg.c | 835 +++++++ src/gras_simix/Msg/gras_simix_msg_interface.h | 50 + src/gras_simix/Msg/gras_simix_msg_private.h | 132 ++ src/gras_simix/Msg/gras_simix_rl_msg.c | 181 ++ src/gras_simix/Msg/gras_simix_rpc.c | 231 ++ src/gras_simix/Msg/gras_simix_sg_msg.c | 135 ++ src/gras_simix/Msg/gras_simix_timer.c | 175 ++ .../Transport/gras_simix_rl_transport.c | 226 ++ .../Transport/gras_simix_sg_transport.c | 152 ++ .../Transport/gras_simix_transport.c | 620 +++++ .../gras_simix_transport_interface.h | 119 + .../gras_simix_transport_plugin_file.c | 250 +++ .../gras_simix_transport_plugin_sg.c | 331 +++ .../gras_simix_transport_plugin_tcp.c | 783 +++++++ .../Transport/gras_simix_transport_private.h | 109 + src/gras_simix/Virtu/gras_simix_gras_module.c | 249 +++ src/gras_simix/Virtu/gras_simix_process.c | 172 ++ src/gras_simix/Virtu/gras_simix_rl_dns.c | 49 + src/gras_simix/Virtu/gras_simix_rl_emul.c | 45 + src/gras_simix/Virtu/gras_simix_rl_process.c | 48 + src/gras_simix/Virtu/gras_simix_rl_time.c | 50 + src/gras_simix/Virtu/gras_simix_sg_dns.c | 19 + src/gras_simix/Virtu/gras_simix_sg_emul.c | 172 ++ src/gras_simix/Virtu/gras_simix_sg_process.c | 176 ++ src/gras_simix/Virtu/gras_simix_sg_time.c | 45 + .../Virtu/gras_simix_virtu_interface.h | 37 + .../Virtu/gras_simix_virtu_private.h | 45 + src/gras_simix/Virtu/gras_simix_virtu_rl.h | 15 + src/gras_simix/Virtu/gras_simix_virtu_sg.h | 58 + src/gras_simix/gras_simix_gras.c | 142 ++ 42 files changed, 11961 insertions(+), 17 deletions(-) create mode 100644 src/gras_simix/DataDesc/gras_simix_cbps.c create mode 100644 src/gras_simix/DataDesc/gras_simix_datadesc.c create mode 100644 src/gras_simix/DataDesc/gras_simix_datadesc_interface.h create mode 100644 src/gras_simix/DataDesc/gras_simix_datadesc_private.h create mode 100644 src/gras_simix/DataDesc/gras_simix_ddt_convert.c create mode 100644 src/gras_simix/DataDesc/gras_simix_ddt_create.c create mode 100644 src/gras_simix/DataDesc/gras_simix_ddt_exchange.c create mode 100644 src/gras_simix/DataDesc/gras_simix_ddt_parse.c create mode 100644 src/gras_simix/DataDesc/gras_simix_ddt_parse.yy.c create mode 100644 src/gras_simix/DataDesc/gras_simix_ddt_parse.yy.h create mode 100644 src/gras_simix/DataDesc/gras_simix_ddt_parse.yy.l create mode 100644 src/gras_simix/Msg/gras_simix_msg.c create mode 100644 src/gras_simix/Msg/gras_simix_msg_interface.h create mode 100644 src/gras_simix/Msg/gras_simix_msg_private.h create mode 100644 src/gras_simix/Msg/gras_simix_rl_msg.c create mode 100644 src/gras_simix/Msg/gras_simix_rpc.c create mode 100644 src/gras_simix/Msg/gras_simix_sg_msg.c create mode 100644 src/gras_simix/Msg/gras_simix_timer.c create mode 100644 src/gras_simix/Transport/gras_simix_rl_transport.c create mode 100644 src/gras_simix/Transport/gras_simix_sg_transport.c create mode 100644 src/gras_simix/Transport/gras_simix_transport.c create mode 100644 src/gras_simix/Transport/gras_simix_transport_interface.h create mode 100644 src/gras_simix/Transport/gras_simix_transport_plugin_file.c create mode 100644 src/gras_simix/Transport/gras_simix_transport_plugin_sg.c create mode 100644 src/gras_simix/Transport/gras_simix_transport_plugin_tcp.c create mode 100644 src/gras_simix/Transport/gras_simix_transport_private.h create mode 100644 src/gras_simix/Virtu/gras_simix_gras_module.c create mode 100644 src/gras_simix/Virtu/gras_simix_process.c create mode 100644 src/gras_simix/Virtu/gras_simix_rl_dns.c create mode 100644 src/gras_simix/Virtu/gras_simix_rl_emul.c create mode 100644 src/gras_simix/Virtu/gras_simix_rl_process.c create mode 100644 src/gras_simix/Virtu/gras_simix_rl_time.c create mode 100644 src/gras_simix/Virtu/gras_simix_sg_dns.c create mode 100644 src/gras_simix/Virtu/gras_simix_sg_emul.c create mode 100644 src/gras_simix/Virtu/gras_simix_sg_process.c create mode 100644 src/gras_simix/Virtu/gras_simix_sg_time.c create mode 100644 src/gras_simix/Virtu/gras_simix_virtu_interface.h create mode 100644 src/gras_simix/Virtu/gras_simix_virtu_private.h create mode 100644 src/gras_simix/Virtu/gras_simix_virtu_rl.h create mode 100644 src/gras_simix/Virtu/gras_simix_virtu_sg.h create mode 100644 src/gras_simix/gras_simix_gras.c diff --git a/src/Makefile.am b/src/Makefile.am index 60716ec002..8270ca699b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -56,16 +56,20 @@ EXTRA_DIST= \ \ simdag/private.h \ \ - gras/Transport/transport_interface.h \ - gras/Virtu/virtu_interface.h gras/Virtu/virtu_private.h\ - gras/Virtu/virtu_rl.h gras/Virtu/virtu_sg.h \ - gras/DataDesc/ddt_parse.yy.l gras/DataDesc/ddt_parse.yy.c \ - \ amok/Bandwidth/bandwidth_private.h \ amok/amok_modinter.h if USE_SIMIX - EXTRA_DIST+=include/simix/simix.h include/simix/datatypes.h simix/msg_simix_private.h + EXTRA_DIST+=include/simix/simix.h include/simix/datatypes.h simix/msg_simix_private.h \ + gras_simix/Transport/gras_simix_transport_interface.h \ + gras_simix/Virtu/gras_simix_virtu_interface.h gras_simix/Virtu/gras_simix_virtu_private.h\ + gras_simix/Virtu/gras_simix_virtu_rl.h gras_simix/Virtu/gras_simix_virtu_sg.h \ + gras_simix/DataDesc/gras_simix_ddt_parse.yy.l gras_simix/DataDesc/gras_simix_ddt_parse.yy.c +else + EXTRA_DIST+=gras/Transport/transport_interface.h \ + gras/Virtu/virtu_interface.h gras/Virtu/virtu_private.h\ + gras/Virtu/virtu_rl.h gras/Virtu/virtu_sg.h \ + gras/DataDesc/ddt_parse.yy.l gras/DataDesc/ddt_parse.yy.c endif # gras_private.h @@ -135,7 +139,6 @@ COMMON_SRC=\ xbt/config.c \ xbt/cunit.c \ xbt/graphxml_parse.c \ - \ gras/gras.c \ \ gras/Transport/transport.c gras/Transport/transport_private.h \ @@ -150,7 +153,6 @@ COMMON_SRC=\ gras/Msg/msg_interface.h gras/Msg/msg_private.h \ \ gras/Virtu/process.c gras/Virtu/gras_module.c - RL_SRC= \ gras/rl_stubs.c xbt/xbt_thread.c \ \ @@ -179,15 +181,7 @@ SG_SRC= \ surf/workstation_KCCFLN05.c \ \ simdag/sd_global.c simdag/sd_link.c simdag/sd_task.c \ - simdag/sd_workstation.c \ - \ - gras/Transport/sg_transport.c gras/Transport/transport_plugin_sg.c \ - \ - gras/Virtu/sg_emul.c \ - gras/Virtu/sg_process.c gras/Virtu/sg_time.c \ - gras/Virtu/sg_dns.c\ - \ - gras/Msg/sg_msg.c + simdag/sd_workstation.c if HAVE_SDP SG_SRC+= surf/sdp.c @@ -216,14 +210,33 @@ MSG_SIMIX_SRC= msg_simix/msg_simix_config.c \ msg_simix/msg_simix_environment.c \ msg_simix/msg_simix_deployment.c +GRAS_SRC= gras/Transport/sg_transport.c gras/Transport/transport_plugin_sg.c \ + \ + gras/Virtu/sg_emul.c \ + gras/Virtu/sg_process.c gras/Virtu/sg_time.c \ + gras/Virtu/sg_dns.c\ + \ + gras/Msg/sg_msg.c + +GRAS_SIMIX_SRC= gras_simix/Transport/gras_simix_sg_transport.c gras_simix/Transport/gras_simix_transport_plugin_sg.c \ + \ + gras_simix/Virtu/gras_simix_sg_emul.c \ + gras_simix/Virtu/gras_simix_sg_process.c gras_simix/Virtu/gras_simix_sg_time.c \ + gras_simix/Virtu/gras_simix_sg_dns.c\ + \ + gras_simix/Msg/gras_simix_sg_msg.c + if USE_SIMIX SG_SRC+=$(SIMIX_SRC) SG_SRC+=$(MSG_SIMIX_SRC) + SG_SRC+=$(GRAS_SIMIX_SRC) EXTRA_DIST+=$(MSG_SRC) else EXTRA_DIST+=$(SIMIX_SRC) EXTRA_DIST+=$(MSG_SIMIX_SRC) + EXTRA_DIST+=$(GRAS_SIMIX_SRC) SG_SRC+=$(MSG_SRC) + SG_SRC+=$(GRAS_SRC) endif AMOK_SRC= \ diff --git a/src/gras_simix/DataDesc/gras_simix_cbps.c b/src/gras_simix/DataDesc/gras_simix_cbps.c new file mode 100644 index 0000000000..649bb1a4b7 --- /dev/null +++ b/src/gras_simix/DataDesc/gras_simix_cbps.c @@ -0,0 +1,391 @@ +/* $Id$ */ + +/* cbps - persistant states for callbacks */ + +/* Copyright (c) 2003 Olivier Aumage. */ +/* Copyright (c) 2003, 2004 Martin Quinson. */ +/* All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "xbt/ex.h" +#include "gras/DataDesc/datadesc_private.h" +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_ddt_cbps,gras_ddt,"callback persistant state"); + +typedef struct { + gras_datadesc_type_t type; + void *data; +} s_gras_cbps_elm_t, *gras_cbps_elm_t; + +typedef struct s_gras_cbps { + xbt_dynar_t lints; /* simple stack of long integers (easy interface) */ + + xbt_dict_t space; /* varname x dynar of gras_cbps_elm_t */ + xbt_dynar_t frames; /* of dynar of names defined within this frame + (and to pop when we leave it) */ + xbt_dynar_t globals; +} s_gras_cbps_t; + +static void free_string(void *d){ + free(*(void**)d); +} + +gras_cbps_t gras_cbps_new(void) { + gras_cbps_t res; + + res=xbt_new(s_gras_cbps_t,1); + + res->lints = xbt_dynar_new(sizeof(int), NULL); + res->space = xbt_dict_new(); + /* no leak, the content is freed manually on block_end */ + res->frames = xbt_dynar_new(sizeof(xbt_dynar_t), NULL); + res->globals = xbt_dynar_new(sizeof(char*), NULL); + + gras_cbps_block_begin(res); + + return res; +} + +void gras_cbps_free(gras_cbps_t *state) { + + xbt_dynar_free( &( (*state)->lints ) ); + + gras_cbps_block_end(*state); + xbt_dict_free ( &( (*state)->space ) ); + xbt_dynar_free( &( (*state)->frames ) ); + xbt_dynar_free( &( (*state)->globals ) ); + + free(*state); + *state = NULL; +} + +void gras_cbps_reset(gras_cbps_t state) { + + xbt_dynar_reset(state->lints); + + xbt_dict_reset (state->space); + + xbt_dynar_reset(state->frames); + xbt_dynar_reset(state->globals); +} + +/** \brief Declare a new element in the PS, and give it a value. + * + * If an element of that + * name already exists, it is masked by the one given here, and will be + * seeable again only after a pop to remove the value this push adds. + */ +void +gras_cbps_v_push(gras_cbps_t ps, + const char *name, + void *data, + gras_datadesc_type_t ddt) { + + xbt_dynar_t varstack=NULL,frame; + gras_cbps_elm_t var; + char *varname = (char*)xbt_strdup(name); + xbt_ex_t e; + + DEBUG2("push(%s,%p)",name,(void*)data); + + TRY { + varstack = xbt_dict_get(ps->space, name); + } CATCH(e) { + if (e.category != mismatch_error) + RETHROW; + + DEBUG1("Create a new variable stack for '%s' into the space",name); + varstack = xbt_dynar_new(sizeof (gras_cbps_elm_t *), NULL); + xbt_dict_set(ps->space, varname, (void **)varstack, NULL); + xbt_ex_free(e); + /* leaking, you think? only if you do not close all the openned blocks ;)*/ + } + + var = xbt_new0(s_gras_cbps_elm_t,1); + var->type = ddt; + var->data = data; + + xbt_dynar_push(varstack, &var); + + xbt_dynar_pop(ps->frames, &frame); + DEBUG4("Push %s (%p @%p) into frame %p",varname,(void*)varname,(void*)&varname,(void*)frame); + xbt_dynar_push(frame, &varname); + xbt_dynar_push(ps->frames, &frame); +} + +/** \brief Retrieve an element from the PS, and remove it from the PS. + * + * If it's not present in the current block, it will fail (throwing not_found) + * and not search in upper blocks since this denotes a programmation error. + */ +void +gras_cbps_v_pop (gras_cbps_t ps, + const char *name, + gras_datadesc_type_t *ddt, + void **res) { + xbt_dynar_t varstack,frame; + gras_cbps_elm_t var = NULL; + void *data = NULL; + xbt_ex_t e; + + DEBUG1("pop(%s)",name); + TRY { + varstack = xbt_dict_get(ps->space, name); + } CATCH(e) { + if (e.category != mismatch_error) + RETHROW; + + xbt_ex_free(e); + THROW1(not_found_error,1,"Asked to pop the non-existant %s", name); + } + xbt_dynar_pop(varstack, &var); + + if (!xbt_dynar_length(varstack)) { + DEBUG1("Last incarnation of %s poped. Kill it",name); + xbt_dict_remove(ps->space, name); + xbt_dynar_free(&varstack); + } + + if (ddt) + *ddt = var->type; + data = var->data; + + free(var); + + xbt_dynar_pop(ps->frames, &frame); + { + int l = xbt_dynar_length(frame); + + while (l--) { + char *_name = NULL; + + _name = xbt_dynar_get_as(frame, l, char*); + if (!strcmp(name, _name)) { + xbt_dynar_remove_at(frame, l, &_name); + free(_name); + break; + } + } + } + xbt_dynar_push(ps->frames, &frame); + + *res = data; +} + +/** \brief Change the value of an element in the PS. + * + * If it's not present in the current block, look in the upper ones. + * If it's not present in any of them, modify in the globals + * If not present there neither, the code may segfault (Oli?). + * + * Once a reference to an element of that name is found somewhere in the PS, + * its value is changed. + */ +void +gras_cbps_v_set (gras_cbps_t ps, + const char *name, + void *data, + gras_datadesc_type_t ddt) { + + xbt_dynar_t dynar = NULL; + gras_cbps_elm_t elm = NULL; + + DEBUG1("set(%s)",name); + dynar = xbt_dict_get_or_null(ps->space, name); + + if (dynar == NULL) { + dynar = xbt_dynar_new(sizeof (gras_cbps_elm_t), NULL); + xbt_dict_set(ps->space, name, (void **)dynar, NULL); + + elm = xbt_new0(s_gras_cbps_elm_t,1); + xbt_dynar_push(ps->globals, &name); + } else { + xbt_dynar_pop(dynar, &elm); + } + + elm->type = ddt; + elm->data = data; + + xbt_dynar_push(dynar, &elm); + +} + +/** \brief Get the value of an element in the PS without modifying it. + * + * (note that you get the content of the data struct and not a copy to it) + * If it's not present in the current block, look in the upper ones. + * If it's not present in any of them, look in the globals + * If not present there neither, the code may segfault (Oli?). + */ +void * +gras_cbps_v_get (gras_cbps_t ps, + const char *name, + /* OUT */gras_datadesc_type_t *ddt) { + + xbt_dynar_t dynar = NULL; + gras_cbps_elm_t elm = NULL; + + DEBUG1("get(%s)",name); + dynar = xbt_dict_get(ps->space, name); + xbt_dynar_pop(dynar, &elm); + xbt_dynar_push(dynar, &elm); + + if (ddt) { + *ddt = elm->type; + } + + return elm->data; + +} + +/** \brief Begins a new block. + * + * Blocks are usefull to remove a whole set of declarations you don't even know + * + * E.g., they constitute an elegent solution to recursive data structures. + * + * push/pop may be used in some cases for that, but if your recursive data + * struct contains other structs needing themselves callbacks, you have to + * use block_{begin,end} to do the trick. + */ + +void +gras_cbps_block_begin(gras_cbps_t ps) { + + xbt_dynar_t dynar = NULL; + + DEBUG0(">>> Block begin"); + dynar = xbt_dynar_new(sizeof (char *), NULL); + xbt_dynar_push(ps->frames, &dynar); +} + +/** \brief End the current block, and go back to the upper one. */ +void +gras_cbps_block_end(gras_cbps_t ps) { + + xbt_dynar_t frame = NULL; + int cursor = 0; + char *name = NULL; + + xbt_assert0(xbt_dynar_length(ps->frames), + "More block_end than block_begin"); + xbt_dynar_pop(ps->frames, &frame); + + xbt_dynar_foreach(frame, cursor, name) { + + xbt_dynar_t varstack = NULL; + gras_cbps_elm_t var = NULL; + + DEBUG2("Get ride of %s (%p)",name,(void*)name); + varstack = xbt_dict_get(ps->space, name); + xbt_dynar_pop(varstack, &var); + + if (!xbt_dynar_length(varstack)) { + xbt_dict_remove(ps->space, name); + xbt_dynar_free_container(&varstack); /*already empty, save a test ;) */ + } + + if (var->data) free(var->data); + free(var); + free(name); + } + xbt_dynar_free_container(&frame);/* we just emptied it */ + DEBUG0("<<< Block end"); +} + + +/** \brief Push a new integer value into the cbps. */ +void +gras_cbps_i_push(gras_cbps_t ps, + int val) { + DEBUG1("push %d as a size",val); + xbt_dynar_push_as(ps->lints,int,val); +} +/** \brief Pop the lastly pushed integer value from the cbps. */ +int +gras_cbps_i_pop(gras_cbps_t ps) { + int ret; + + xbt_assert0(xbt_dynar_length(ps->lints) > 0, + "gras_cbps_i_pop: no value to pop"); + ret = xbt_dynar_pop_as(ps->lints,int); + DEBUG1("pop %d as a size",ret); + return ret; +} + +/** \brief Generic cb returning the lastly pushed value + * + * Used by \ref gras_datadesc_ref_pop_arr + */ +int gras_datadesc_cb_pop(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) { + int res = gras_cbps_i_pop(vars); + DEBUG1("Pop %d as a size",res); + return res; +} + +/* ************************* */ +/* **** PUSHy callbacks **** */ +/* ************************* */ + +/** \brief Cb to push an integer. Must be attached to the field you want to push */ +void gras_datadesc_cb_push_int(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) { + int *i = (int*)data; + gras_cbps_i_push(vars, (int) *i); +} + +/** \brief Cb to push an unsigned integer. Must be attached to the field you want to push */ +void gras_datadesc_cb_push_uint(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) { + unsigned int *i = (unsigned int*)data; + gras_cbps_i_push(vars, (int) *i); +} + +/** \brief Cb to push an long integer. Must be attached to the field you want to push + */ +void gras_datadesc_cb_push_lint(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) { + long int *i = (long int*)data; + gras_cbps_i_push(vars, (int) *i); +} +/** \brief Cb to push an unsigned long integer. Must be attached to the field you want to push + */ +void gras_datadesc_cb_push_ulint(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) { + unsigned long int *i = (unsigned long int*)data; + gras_cbps_i_push(vars, (int) *i); +} + +/* ************************************ */ +/* **** PUSHy multiplier callbacks **** */ +/* ************************************ */ +/** \brief Cb to push an integer as multiplier. Must be attached to the field you want to push */ +void gras_datadesc_cb_push_int_mult(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) { + int old = *(int*)data; + int new = gras_cbps_i_pop(vars); + DEBUG2("push %d x %d as a size",old,new); + gras_cbps_i_push(vars, old*new); +} + +/** \brief Cb to push an unsigned integer as multiplier. Must be attached to the field you want to push */ +void gras_datadesc_cb_push_uint_mult(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) { + unsigned int old = *(unsigned int*)data; + unsigned int new = gras_cbps_i_pop(vars); + + DEBUG2("push %d x %d as a size",old,new); + gras_cbps_i_push(vars, (int) (old*new)); +} + +/** \brief Cb to push an long integer as multiplier. Must be attached to the field you want to push + */ +void gras_datadesc_cb_push_lint_mult(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) { + long int i = *(long int*)data; + i *= gras_cbps_i_pop(vars); + gras_cbps_i_push(vars, (int) i); +} +/** \brief Cb to push an unsigned long integer as multiplier. Must be attached to the field you want to push + */ +void gras_datadesc_cb_push_ulint_mult(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) { + unsigned long int old = *(unsigned long int*)data; + unsigned long int new = gras_cbps_i_pop(vars); + + DEBUG2("push %ld x %ld as a size",old,new); + gras_cbps_i_push(vars, (int) (old * new) ); +} diff --git a/src/gras_simix/DataDesc/gras_simix_datadesc.c b/src/gras_simix/DataDesc/gras_simix_datadesc.c new file mode 100644 index 0000000000..11ec857d64 --- /dev/null +++ b/src/gras_simix/DataDesc/gras_simix_datadesc.c @@ -0,0 +1,250 @@ +/* $Id$ */ + +/* datadesc - data description in order to send/recv it in GRAS */ + +/* Copyright (c) 2003 Olivier Aumage. */ +/* Copyright (c) 2003, 2004 Martin Quinson. */ +/* All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "gras/DataDesc/datadesc_private.h" + +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_ddt,gras,"Data description"); +/* FIXME: make this host-dependent using a trick such as UserData*/ +/*@null@*/xbt_set_t gras_datadesc_set_local=NULL; + + +/* callback for array size when sending strings */ +static int +_strlen_cb(/*@unused@*/gras_datadesc_type_t type,/*@unused@*/gras_cbps_t vars, void *data) { + + return 1+(int)strlen(data); +} + + +/** + * gras_datadesc_init: + * + * Initialize the datadesc module. + * FIXME: We assume that when neither signed nor unsigned is given, + * that means signed. To be checked by configure. + **/ +void +gras_datadesc_init(void) { + gras_datadesc_type_t ddt; /* What to add */ + + /* only initialize once */ + if (gras_datadesc_set_local != NULL) + return; + + VERB0("Initializing DataDesc"); + + gras_datadesc_set_local = xbt_set_new(); + + + /* all known datatypes */ + + ddt = gras_datadesc_scalar("signed char", + gras_ddt_scalar_char, + e_gras_dd_scalar_encoding_sint); + ddt = gras_datadesc_scalar("char", + gras_ddt_scalar_char, + e_gras_dd_scalar_encoding_sint); + ddt = gras_datadesc_scalar("unsigned char", + gras_ddt_scalar_char, + e_gras_dd_scalar_encoding_uint); + + ddt = gras_datadesc_scalar("signed short int", + gras_ddt_scalar_short, + e_gras_dd_scalar_encoding_sint); + ddt = gras_datadesc_scalar("short int", + gras_ddt_scalar_short, + e_gras_dd_scalar_encoding_sint); + ddt = gras_datadesc_scalar("short", + gras_ddt_scalar_short, + e_gras_dd_scalar_encoding_sint); + ddt = gras_datadesc_scalar("unsigned short int", + gras_ddt_scalar_short, + e_gras_dd_scalar_encoding_uint); + + ddt = gras_datadesc_scalar("signed int", + gras_ddt_scalar_int, + e_gras_dd_scalar_encoding_sint); + ddt = gras_datadesc_scalar("int", + gras_ddt_scalar_int, + e_gras_dd_scalar_encoding_sint); + ddt = gras_datadesc_scalar("unsigned int", + gras_ddt_scalar_int, + e_gras_dd_scalar_encoding_uint); + + ddt = gras_datadesc_scalar("signed long int", + gras_ddt_scalar_long, + e_gras_dd_scalar_encoding_sint); + ddt = gras_datadesc_scalar("long int", + gras_ddt_scalar_long, + e_gras_dd_scalar_encoding_sint); + ddt = gras_datadesc_scalar("long", + gras_ddt_scalar_long, + e_gras_dd_scalar_encoding_sint); + ddt = gras_datadesc_scalar("unsigned long int", + gras_ddt_scalar_long, + e_gras_dd_scalar_encoding_uint); + + ddt = gras_datadesc_scalar("signed long long int", + gras_ddt_scalar_long_long, + e_gras_dd_scalar_encoding_sint); + ddt = gras_datadesc_scalar("long long int", + gras_ddt_scalar_long_long, + e_gras_dd_scalar_encoding_sint); + ddt = gras_datadesc_scalar("unsigned long long int", + gras_ddt_scalar_long_long, + e_gras_dd_scalar_encoding_uint); + + ddt = gras_datadesc_scalar("data pointer", + gras_ddt_scalar_pdata, + e_gras_dd_scalar_encoding_uint); + ddt = gras_datadesc_scalar("function pointer", + gras_ddt_scalar_pfunc, + e_gras_dd_scalar_encoding_uint); + + ddt = gras_datadesc_scalar("float", + gras_ddt_scalar_float, + e_gras_dd_scalar_encoding_float); + ddt = gras_datadesc_scalar("double", + gras_ddt_scalar_double, + e_gras_dd_scalar_encoding_float); + + ddt = gras_datadesc_array_dyn("char[]", + gras_datadesc_by_name("char"), + _strlen_cb); + ddt = gras_datadesc_ref("string",ddt); + + /* specific datatype: the exception type (for RPC) */ + ddt = gras_datadesc_struct("ex_t"); + gras_datadesc_struct_append(ddt,"msg",gras_datadesc_by_name("string")); + gras_datadesc_struct_append(ddt,"category",gras_datadesc_by_name("int")); + gras_datadesc_struct_append(ddt,"value",gras_datadesc_by_name("int")); + + gras_datadesc_struct_append(ddt,"remote",gras_datadesc_by_name("short int")); + + gras_datadesc_struct_append(ddt,"host",gras_datadesc_by_name("string")); + gras_datadesc_struct_append(ddt,"procname",gras_datadesc_by_name("string")); + gras_datadesc_struct_append(ddt,"pid",gras_datadesc_by_name("long int")); + gras_datadesc_struct_append(ddt,"file",gras_datadesc_by_name("string")); + gras_datadesc_struct_append(ddt,"line",gras_datadesc_by_name("int")); + gras_datadesc_struct_append(ddt,"func",gras_datadesc_by_name("string")); + gras_datadesc_struct_append(ddt,"used",gras_datadesc_by_name("int")); + gras_datadesc_cb_field_push(ddt,"used"); + gras_datadesc_struct_append(ddt,"bt_strings", + gras_datadesc_ref_pop_arr(gras_datadesc_by_name("string"))); + + gras_datadesc_struct_close(ddt); + + /* specific datatype: xbt_peer_t */ + ddt = gras_datadesc_struct("s_xbt_peer_t"); + gras_datadesc_struct_append(ddt,"name",gras_datadesc_by_name("string")); + gras_datadesc_struct_append(ddt,"port",gras_datadesc_by_name("int")); + gras_datadesc_struct_close(ddt); + + ddt = gras_datadesc_ref("xbt_peer_t",ddt); + + /* Dict containing the constant value (for the parsing macro) */ + gras_dd_constants = xbt_dict_new(); + +} + +/** + * gras_datadesc_exit: + * + * Finalize the datadesc module + **/ +void +gras_datadesc_exit(void) { + VERB0("Exiting DataDesc"); + xbt_set_free(&gras_datadesc_set_local); + xbt_dict_free(&gras_dd_constants); + DEBUG0("Exited DataDesc"); +} + +/** This is mainly to debug */ +const char * +gras_datadesc_get_name(gras_datadesc_type_t ddt) { + return ddt?(const char*)ddt->name:"(null)"; +} +/** This is mainly to debug */ +int +gras_datadesc_get_id(gras_datadesc_type_t ddt) { + return ddt->code; +} + +/** + * gras_datadesc_size: + * + * Returns the size occuped by data of this type (on the current arch). + * + */ +int gras_datadesc_size(gras_datadesc_type_t type) { + return type?type->size[GRAS_THISARCH]:0; +} + +/** + * gras_datadesc_type_dump: + * + * For debugging purpose + */ +void gras_datadesc_type_dump(const gras_datadesc_type_t ddt){ + int cpt; + + printf("DataDesc dump:"); + if(!ddt) { + printf("(null)\n"); + return; + } + printf ("%s (ID:%d)\n",ddt->name,ddt->code); + printf (" category: %s\n",gras_datadesc_cat_names[ddt->category_code]); + + printf (" size["); + for (cpt=0; cpt0?", ":"", + cpt == GRAS_THISARCH ? ">":"", + ddt->size[cpt], + cpt == GRAS_THISARCH ? "<":""); + } + printf ("]\n"); + + printf (" alignment["); + for (cpt=0; cpt0?", ":"", + cpt == GRAS_THISARCH ? ">":"", + ddt->alignment[cpt], + cpt == GRAS_THISARCH ? "<":""); + } + printf ("]\n"); + + printf (" aligned_size["); + for (cpt=0; cpt0?", ":"", + cpt == GRAS_THISARCH ? ">":"", + ddt->aligned_size[cpt], + cpt == GRAS_THISARCH ? "<":""); + } + printf ("]\n"); + if (ddt->category_code == e_gras_datadesc_type_cat_struct) { + gras_dd_cat_struct_t struct_data; + gras_dd_cat_field_t field; + + struct_data = ddt->category.struct_data; + xbt_dynar_foreach(struct_data.fields, cpt, field) { + printf(">>> Dump field #%d (%s) (offset=%ld)\n", + cpt,field->name,field->offset[GRAS_THISARCH]); + gras_datadesc_type_dump(field->type); + printf("<<< end dump field #%d (%s)\n",cpt,field->name); + } + } + fflush(stdout); +} diff --git a/src/gras_simix/DataDesc/gras_simix_datadesc_interface.h b/src/gras_simix/DataDesc/gras_simix_datadesc_interface.h new file mode 100644 index 0000000000..3611ee6108 --- /dev/null +++ b/src/gras_simix/DataDesc/gras_simix_datadesc_interface.h @@ -0,0 +1,43 @@ +/* $Id$ */ + +/* datadesc - describing the data to exchange */ + +/* module's public interface exported within GRAS, but not to end user. */ + +/* Copyright (c) 2003 Olivier Aumage. */ +/* Copyright (c) 2003, 2004 Martin Quinson. */ +/* All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#ifndef GRAS_DATADESC_INTERFACE_H +#define GRAS_DATADESC_INTERFACE_H + +XBT_PUBLIC(gras_datadesc_type_t) gras_datadesc_by_id (long int code); + +/* to debug */ +XBT_PUBLIC(void) gras_datadesc_type_dump(const gras_datadesc_type_t ddt); +XBT_PUBLIC(const char *) gras_datadesc_arch_name(int code); + +/* compare two data type description */ +XBT_PUBLIC(int) +gras_datadesc_type_cmp(const gras_datadesc_type_t d1, + const gras_datadesc_type_t d2); + +/* Access function */ +XBT_PUBLIC(int) gras_datadesc_size(gras_datadesc_type_t type); +/* Described data exchanges: direct use */ +XBT_PUBLIC(int) gras_datadesc_copy(gras_datadesc_type_t type, void *src, void *dst); +XBT_PUBLIC(void) gras_datadesc_send(gras_socket_t sock, gras_datadesc_type_t type, void *src); +XBT_PUBLIC(void) gras_datadesc_recv(gras_socket_t sock, gras_datadesc_type_t type, + int r_arch, void *dst); + +/* Described data exchanges: IDL compilation FIXME: not implemented*/ +void gras_datadesc_gen_cpy(gras_datadesc_type_t type, void *src, void **dst); +void gras_datadesc_gen_send(gras_socket_t sock, gras_datadesc_type_t type, void *src); +void gras_datadesc_gen_recv(gras_socket_t sock, gras_datadesc_type_t type, + int r_arch, void *dst); + + +#endif /* GRAS_DATADESC_INTERFACE_H */ diff --git a/src/gras_simix/DataDesc/gras_simix_datadesc_private.h b/src/gras_simix/DataDesc/gras_simix_datadesc_private.h new file mode 100644 index 0000000000..0e79380174 --- /dev/null +++ b/src/gras_simix/DataDesc/gras_simix_datadesc_private.h @@ -0,0 +1,265 @@ +/* $Id$ */ + +/* datadesc - describing the data to exchange */ + +/* module's private interface masked even to other parts of GRAS. */ + +/* Copyright (c) 2003 Olivier Aumage. */ +/* Copyright (c) 2003, 2004 Martin Quinson. */ +/* All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#ifndef GRAS_DATADESC_PRIVATE_H +#define GRAS_DATADESC_PRIVATE_H + +#include "xbt/sysdep.h" +#include "xbt/log.h" +#include "xbt/dynar.h" +#include "xbt/dict.h" +#include "xbt/set.h" + +#include "portable.h" /* GRAS_THISARCH */ + +#include "gras/transport.h" /* socket handling */ + +#include "gras_modinter.h" /* module init/exit */ +#include "gras/datadesc.h" /* rest of module public interface */ + +#include "gras/DataDesc/datadesc_interface.h" /* semi-public API */ + +/** + * ddt_aligned: + * + * Align the data v on the boundary a. + */ +#define ddt_aligned(v, a) (((v) + (a - 1)) & ~(a - 1)) + +/*@null@*/extern xbt_set_t gras_datadesc_set_local; +void gras_ddt_freev(void *ddt); +/******************************************* + * Descriptions of all known architectures * + *******************************************/ + +#define gras_arch_count 11 +typedef enum { + gras_ddt_scalar_char = 0, + gras_ddt_scalar_short = 1, + gras_ddt_scalar_int = 2, + gras_ddt_scalar_long = 3, + gras_ddt_scalar_long_long = 4, + + gras_ddt_scalar_pdata = 5, + gras_ddt_scalar_pfunc = 6, + + gras_ddt_scalar_float = 7, + gras_ddt_scalar_double = 8 +} gras_ddt_scalar_type_t; + +typedef struct { + const char *name; + + int endian; + + int sizeofs[9]; /* char,short,int,long,long_long,pdata,pfunc,float,double */ + int boundaries[9]; /* idem */ +} gras_arch_desc_t; + +extern const gras_arch_desc_t gras_arches[gras_arch_count]; +extern const char *gras_datadesc_cat_names[9]; + +/**********************************************************/ +/* Actual definitions of the stuff in the type descriptor */ +/**********************************************************/ + +/** + * e_gras_datadesc_type_category: + * + * Defines all possible type categories. + */ +typedef enum e_gras_datadesc_type_category { + + /* if you edit this, also fix gras_datadesc_cat_names in ddt_exchange.c */ + + e_gras_datadesc_type_cat_undefined = 0, + + e_gras_datadesc_type_cat_scalar = 1, + e_gras_datadesc_type_cat_struct = 2, + e_gras_datadesc_type_cat_union = 3, + e_gras_datadesc_type_cat_ref = 4, /* ref to an uniq element */ + e_gras_datadesc_type_cat_array = 5, + + e_gras_datadesc_type_cat_invalid = 6 +} gras_datadesc_type_category_t; + +/*------------------------------------------------*/ +/* definitions of specific data for each category */ +/*------------------------------------------------*/ +/** + * s_gras_dd_cat_field: + * + * Fields of struct and union + */ +typedef struct s_gras_dd_cat_field { + + char *name; + long int offset[gras_arch_count]; + gras_datadesc_type_t type; + + gras_datadesc_type_cb_void_t send; + gras_datadesc_type_cb_void_t recv; + +} s_gras_dd_cat_field_t,*gras_dd_cat_field_t; + +void gras_dd_cat_field_free(void *f); + +/** + * gras_dd_cat_scalar_t: + * + * Specific fields of a scalar + */ +enum e_gras_dd_scalar_encoding { + e_gras_dd_scalar_encoding_undefined = 0, + + e_gras_dd_scalar_encoding_uint, + e_gras_dd_scalar_encoding_sint, + e_gras_dd_scalar_encoding_float, + + e_gras_dd_scalar_encoding_invalid +}; +typedef struct s_gras_dd_cat_scalar { + enum e_gras_dd_scalar_encoding encoding; + gras_ddt_scalar_type_t type; /* to check easily that redefinition matches */ +} gras_dd_cat_scalar_t; + +/** + * gras_dd_cat_struct_t: + * + * Specific fields of a struct + */ +typedef struct s_gras_dd_cat_struct { + xbt_dynar_t fields; /* elm type = gras_dd_cat_field_t */ + int closed; /* gras_datadesc_declare_struct_close() was called */ +} gras_dd_cat_struct_t; + +/** + * gras_dd_cat_union_t: + * + * Specific fields of a union + */ +typedef struct s_gras_dd_cat_union { + gras_datadesc_type_cb_int_t selector; + xbt_dynar_t fields; /* elm type = gras_dd_cat_field_t */ + int closed; /* gras_datadesc_declare_union_close() was called */ +} gras_dd_cat_union_t; + +/** + * gras_dd_cat_ref_t: + * + * Specific fields of a reference + */ +typedef struct s_gras_dd_cat_ref { + gras_datadesc_type_t type; + + /* callback used to return the referenced type number */ + gras_datadesc_selector_t selector; +} gras_dd_cat_ref_t; + + +/** + * gras_dd_cat_array_t: + * + * Specific fields of an array + */ +typedef struct s_gras_dd_cat_array { + gras_datadesc_type_t type; + + /* element_count == 0 means dynamically defined */ + unsigned long int fixed_size; + + /* callback used to return the dynamic length */ + gras_datadesc_type_cb_int_t dynamic_size; + +} gras_dd_cat_array_t; + +/** + * u_gras_datadesc_category: + * + * Specific data to each possible category + */ +union u_gras_datadesc_category { + void *undefined_data; + gras_dd_cat_scalar_t scalar_data; + gras_dd_cat_struct_t struct_data; + gras_dd_cat_union_t union_data; + gras_dd_cat_ref_t ref_data; + gras_dd_cat_array_t array_data; +}; + +/****************************************/ +/* The holy grail: type descriptor type */ +/****************************************/ +/** + * s_gras_datadesc_type: + * + * Type descriptor. + */ +typedef struct s_gras_datadesc_type { + /* headers for the data set */ + unsigned int code; + char *name; + unsigned int name_len; + + /* payload */ + unsigned long int size[gras_arch_count]; + + unsigned long int alignment[gras_arch_count]; + unsigned long int aligned_size[gras_arch_count]; + + enum e_gras_datadesc_type_category category_code; + union u_gras_datadesc_category category; + + gras_datadesc_type_cb_void_t send; + gras_datadesc_type_cb_void_t recv; + + /* flags */ + int cycle :1; + + /* random value for users (like default value or whatever) */ + char extra[SIZEOF_MAX]; + +} s_gras_datadesc_type_t; + +/*************************** + * constructor/desctructor * + ***************************/ +void gras_datadesc_free(gras_datadesc_type_t *type); + + gras_datadesc_type_t + gras_datadesc_scalar(const char *name, + gras_ddt_scalar_type_t type, + enum e_gras_dd_scalar_encoding encoding); + +/**************************************************** + * Callback persistant state constructor/destructor * + ****************************************************/ +gras_cbps_t gras_cbps_new(void); +void gras_cbps_free(gras_cbps_t *state); +void gras_cbps_reset(gras_cbps_t state); + +/*************** + * Convertions * + ***************/ +void +gras_dd_convert_elm(gras_datadesc_type_t type, int count, + int r_arch, + void *src, void *dst); + +/******************************************************************** + * Dictionnary containing the constant values for the parsing macro * + ********************************************************************/ +extern xbt_dict_t gras_dd_constants; /* lives in ddt_parse.c of course */ + +#endif /* GRAS_DATADESC_PRIVATE_H */ + diff --git a/src/gras_simix/DataDesc/gras_simix_ddt_convert.c b/src/gras_simix/DataDesc/gras_simix_ddt_convert.c new file mode 100644 index 0000000000..fd20823961 --- /dev/null +++ b/src/gras_simix/DataDesc/gras_simix_ddt_convert.c @@ -0,0 +1,269 @@ +/* $Id$ */ + +/* ddt_remote - Stuff needed to get datadescs about remote hosts */ + +/* Copyright (c) 2003 Olivier Aumage. */ +/* Copyright (c) 2003, 2004 Martin Quinson. */ +/* All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +/************************************************************************/ +/* C combines the power of assembler with the portability of assembler. */ +/************************************************************************/ + +#include "gras/DataDesc/datadesc_private.h" + +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_ddt_convert,gras_ddt, + "Inter-architecture convertions"); + +/*** + *** Table of all known architectures: + *** + l_C:1/1:_I:2/2:4/4:4/4:8/8:_P:4/4:4/4:_D:4/4:8/8:) gras_arch=0; gras_arch_name=little32;; + l_C:1/1:_I:2/2:4/4:4/4:8/4:_P:4/4:4/4:_D:4/4:8/4:) gras_arch=1; gras_arch_name=little32_4;; + + l_C:1/1:_I:2/2:4/4:8/8:8/8:_P:8/8:8/8:_D:4/4:8/8:) gras_arch=2; gras_arch_name=little64;; + + B_C:1/1:_I:2/2:4/4:4/4:8/8:_P:4/4:4/4:_D:4/4:8/8:) gras_arch=3; gras_arch_name=big32;; + B_C:1/1:_I:2/2:4/4:4/4:8/8:_P:4/4:4/4:_D:4/4:8/4:) gras_arch=4; gras_arch_name=big32_8_4;; + B_C:1/1:_I:2/2:4/4:4/4:8/4:_P:4/4:4/4:_D:4/4:8/4:) gras_arch=5; gras_arch_name=big32_4;; + B_C:1/1:_I:2/2:4/2:4/2:8/2:_P:4/2:4/2:_D:4/2:8/2:) gras_arch=6; gras_arch_name=big32_2;; + + B_C:1/1:_I:2/2:4/4:8/8:8/8:_P:8/8:8/8:_D:4/4:8/8:) gras_arch=7; gras_arch_name=big64;; + + ***/ + +const gras_arch_desc_t gras_arches[gras_arch_count] = { + + {"little32_1", 0, {1,2,4,4,8, 4,4, 4,8}, /* little endian, 1 byte alignement (win32) */ + {1,1,1,1,1, 1,1, 1,1}}, + + {"little32_2", 0, {1,2,4,4,8, 4,4, 4,8}, /* little endian, 2 bytes alignements (win32) */ + {1,2,2,2,2, 2,2, 2,2}}, + + {"little32_4", 0, {1,2,4,4,8, 4,4, 4,8}, /* little endian, 4 bytes alignements (win32 and linux x86) */ + {1,2,4,4,4, 4,4, 4,4}}, + + {"little32_8", 0, {1,2,4,4,8, 4,4, 4,8}, /* little endian, 8 bytes alignement (win32) */ + {1,2,4,4,8, 4,4, 4,8}}, + + {"little64", 0, {1,2,4,8,8, 8,8, 4,8}, /* alpha, ia64 */ + {1,2,4,8,8, 8,8, 4,8}}, + + {"big32_8", 1, {1,2,4,4,8, 4,4, 4,8}, + {1,2,4,4,8, 4,4, 4,8}}, + + {"big32_8_4", 1, {1,2,4,4,8, 4,4, 4,8}, /* AIX */ + {1,2,4,4,8, 4,4, 4,4}}, + + {"big32_4", 1, {1,2,4,4,8, 4,4, 4,8}, /* G5 */ + {1,2,4,4,4, 4,4, 4,4}}, + + {"big32_2", 1, {1,2,4,4,8, 4,4, 4,8}, /* ARM */ + {1,2,2,2,2, 2,2, 2,2}}, + + {"big64", 1, {1,2,4,8,8, 8,8, 4,8}, /* sparc */ + {1,2,4,8,8, 8,8, 4,8}}, + + {"big64_8_4", 1, {1,2,4,8,8, 8,8, 4,8}, /* aix with -maix64 */ + {1,2,4,8,8, 8,8, 4,4}} +}; + +const char *gras_datadesc_arch_name(int code) { + if (code < 0 || code >= gras_arch_count) + return "[unknown arch]"; + return gras_arches[code].name; +} + + +/** + * Local function doing the grunt work + */ +static void +gras_dd_reverse_bytes(void *to, + const void *from, + size_t length); + +/** + * gras_dd_convert_elm: + * + * Convert the element described by @type comming from architecture @r_arch. + * The data to be converted is stored in @src, and is to be stored in @dst. + * Both pointers may be the same location if no resizing is needed. + */ +void +gras_dd_convert_elm(gras_datadesc_type_t type, int count, + int r_arch, + void *src, void *dst) { + gras_dd_cat_scalar_t scal = type->category.scalar_data; + int cpt; + const void *r_data; + void *l_data; + unsigned long r_size, l_size; + /* Hexadecimal displayer + union { + char c[sizeof(int)]; + int i; + } tester; + */ + + xbt_assert(type->category_code == e_gras_datadesc_type_cat_scalar); + xbt_assert(r_arch != GRAS_THISARCH); + + r_size = type->size[r_arch]; + l_size = type->size[GRAS_THISARCH]; + DEBUG4("r_size=%lu l_size=%lu, src=%p dst=%p", + r_size,l_size,src,dst); + + DEBUG2("remote=%c local=%c", gras_arches[r_arch].endian?'B':'l', + gras_arches[GRAS_THISARCH].endian?'B':'l'); + + if(r_size != l_size) { + for(cpt = 0, r_data = src, l_data = dst; + cpt < count; + cpt++, + r_data = (char *)r_data + r_size, + l_data = (char *)l_data + l_size) { + + /* + fprintf(stderr,"r_data="); + for (cpt=0; cpt 127) != (*l_sign > 127)) { + if(*r_sign > 127) + *l_sign += 128; + else + *l_sign -= 128; + } + } + } else { + DEBUG1("Extend %d bytes", sizeChange); + if (scal.encoding != e_gras_dd_scalar_encoding_sint) { + DEBUG0("This is signed"); + padding = 0; /* pad unsigned with 0 */ + } else { + /* extend sign */ + r_sign = gras_arches[r_arch].endian ? ((unsigned char*)r_data + r_size - 1) + : (unsigned char*)r_data; + padding = (*r_sign > 127) ? 0xff : 0; + } + + memset(l_data, padding, l_size); + memcpy(!gras_arches[r_arch].endian ? l_data : ((char *)l_data + sizeChange), + r_data, r_size); + + /* + fprintf(stderr,"r_data="); + for (cpt=0; cpt 1) { + + for(cpt = 0, r_data=dst, l_data=dst; + cpt < count; + cpt++, + r_data = (char *)r_data + l_size, /* resizing already done */ + l_data = (char *)l_data + l_size) { + + DEBUG1("Flip elm %d",cpt); + gras_dd_reverse_bytes(l_data, r_data, l_size); + } + } + +} + +static void +gras_dd_reverse_bytes(void *to, + const void *from, + size_t length) { + + char charBegin; + const char *fromBegin; + const char *fromEnd; + char *toBegin; + char *toEnd; + + for(fromBegin = (const char *)from, + fromEnd = fromBegin + length - 1, + toBegin = (char *)to, + toEnd = toBegin + length - 1; + + fromBegin <= fromEnd; + + fromBegin++, fromEnd--, + toBegin++, toEnd--) { + + charBegin = *fromBegin; + *toBegin = *fromEnd; + *toEnd = charBegin; + } +} + + +/** + * gras_arch_selfid: + * + * returns the ID of the architecture the process is running on + */ +int +gras_arch_selfid(void) { + return GRAS_THISARCH; +} diff --git a/src/gras_simix/DataDesc/gras_simix_ddt_create.c b/src/gras_simix/DataDesc/gras_simix_ddt_create.c new file mode 100644 index 0000000000..c720f9c769 --- /dev/null +++ b/src/gras_simix/DataDesc/gras_simix_ddt_create.c @@ -0,0 +1,1042 @@ +/* $Id$ */ + +/* ddt_new - creation/deletion of datatypes structs (private to this module)*/ + +/* Copyright (c) 2003 Olivier Aumage. */ +/* Copyright (c) 2003, 2004 Martin Quinson. */ +/* All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "xbt/misc.h" /* min()/max() */ +#include "xbt/ex.h" +#include "gras/DataDesc/datadesc_private.h" + +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_ddt_create,gras_ddt,"Creating new datadescriptions"); + +/*** prototypes ***/ +static gras_dd_cat_field_t + gras_dd_find_field(gras_datadesc_type_t type, + const char *field_name); +/** + * gras_ddt_freev: + * + * gime that memory back, dude. I mean it. + */ +void gras_ddt_freev(void *ddt) { + gras_datadesc_type_t type= (gras_datadesc_type_t)ddt; + + if (type) { + gras_datadesc_free(&type); + } +} + +static gras_datadesc_type_t gras_ddt_new(const char *name) { + gras_datadesc_type_t res; + + XBT_IN1("(%s)",name); + res=xbt_new0(s_gras_datadesc_type_t,1); + + res->name = (char*)strdup(name); + res->name_len = strlen(name); + res->cycle = 0; + + xbt_set_add(gras_datadesc_set_local, + (xbt_set_elm_t)res,&gras_ddt_freev); + XBT_OUT; + return res; +} + +/** @brief retrieve an existing message type from its name (or NULL if it does not exist). */ +gras_datadesc_type_t gras_datadesc_by_name_or_null (const char *name) { + xbt_ex_t e; + gras_datadesc_type_t res = NULL; + + TRY { + res = gras_datadesc_by_name(name); + } CATCH(e) { + res = NULL; + xbt_ex_free(e); + } + return res; +} +/** + * Search the given datadesc (or raises an exception if it can't be found) + */ +gras_datadesc_type_t gras_datadesc_by_name(const char *name) { + xbt_ex_t e; + gras_datadesc_type_t res = NULL; + volatile int found = 0; + TRY { + res = (gras_datadesc_type_t)xbt_set_get_by_name(gras_datadesc_set_local,name); + found = 1; + } CATCH(e) { + if (e.category != not_found_error) + RETHROW; + xbt_ex_free(e); + } + if (!found) + THROW1(not_found_error,0,"No registred datatype of that name: %s",name); + + return res; +} + +/** + * Retrieve a type from its code (or NULL if not found) + */ +gras_datadesc_type_t gras_datadesc_by_id(long int code) { + xbt_ex_t e; + gras_datadesc_type_t res=NULL; + TRY { + res = (gras_datadesc_type_t)xbt_set_get_by_id(gras_datadesc_set_local,code); + } CATCH(e) { + if (e.category != not_found_error) + RETHROW; + xbt_ex_free(e); + res = NULL; + } + return res; +} + +/** + * Create a new scalar and give a pointer to it + */ +gras_datadesc_type_t + gras_datadesc_scalar(const char *name, + gras_ddt_scalar_type_t type, + enum e_gras_dd_scalar_encoding encoding) { + + gras_datadesc_type_t res; + long int arch; + + XBT_IN; + res = gras_datadesc_by_name_or_null(name); + if (res) { + xbt_assert1(res->category_code == e_gras_datadesc_type_cat_scalar, + "Redefinition of type %s does not match", name); + xbt_assert1(res->category.scalar_data.encoding == encoding, + "Redefinition of type %s does not match", name); + xbt_assert1(res->category.scalar_data.type == type, + "Redefinition of type %s does not match", name); + VERB1("Discarding redefinition of %s",name); + return res; + } + res = gras_ddt_new(name); + + for (arch = 0; arch < gras_arch_count; arch ++) { + res->size[arch] = gras_arches[arch].sizeofs[type]; + res->alignment[arch] = gras_arches[arch].boundaries[type]; + res->aligned_size[arch] = ddt_aligned(res->size[arch], res->alignment[arch]); + } + + res->category_code = e_gras_datadesc_type_cat_scalar; + res->category.scalar_data.encoding = encoding; + res->category.scalar_data.type = type; + XBT_OUT; + + return res; +} + + +/** Frees one struct or union field */ +void gras_dd_cat_field_free(void *f) { + gras_dd_cat_field_t field = *(gras_dd_cat_field_t *)f; + XBT_IN; + if (field) { + if (field->name) + free(field->name); + free(field); + } + XBT_OUT; +} + +/** \brief Declare a new structure description */ +gras_datadesc_type_t + gras_datadesc_struct(const char *name) { + + gras_datadesc_type_t res; + long int arch; + + XBT_IN1("(%s)",name); + res = gras_datadesc_by_name_or_null(name); + if (res) { + /* FIXME: Check that field redefinition matches */ + xbt_assert1(res->category_code == e_gras_datadesc_type_cat_struct, + "Redefinition of type %s does not match", name); + VERB1("Discarding redefinition of %s",name); + return res; + } + res = gras_ddt_new(name); + + for (arch=0; archsize[arch] = 0; + res->alignment[arch] = 0; + res->aligned_size[arch] = 0; + } + res->category_code = e_gras_datadesc_type_cat_struct; + res->category.struct_data.fields = + xbt_dynar_new(sizeof(gras_dd_cat_field_t), + &gras_dd_cat_field_free); + + XBT_OUT; + return res; +} + +/** \brief Append a new field to a structure description */ +void +gras_datadesc_struct_append(gras_datadesc_type_t struct_type, + const char *name, + gras_datadesc_type_t field_type) { + + gras_dd_cat_field_t field; + int arch; + + xbt_assert2(field_type, + "Cannot add the field '%s' into struct '%s': its type is NULL", + name,struct_type->name); + XBT_IN3("(%s %s.%s;)",field_type->name,struct_type->name,name); + if (struct_type->category.struct_data.closed) { + VERB1("Ignoring request to add field to struct %s (closed. Redefinition?)", + struct_type->name); + return; + } + + xbt_assert1(field_type->size != 0, + "Cannot add a dynamically sized field in structure %s", + struct_type->name); + + field=xbt_new(s_gras_dd_cat_field_t,1); + field->name = (char*)strdup(name); + + DEBUG0("----------------"); + DEBUG3("PRE s={size=%ld,align=%ld,asize=%ld}", + struct_type->size[GRAS_THISARCH], + struct_type->alignment[GRAS_THISARCH], + struct_type->aligned_size[GRAS_THISARCH]); + + + for (arch=0; archoffset[arch] = ddt_aligned(struct_type->size[arch], + field_type->alignment[arch]); + + struct_type->size[arch] = field->offset[arch] + field_type->size[arch]; + struct_type->alignment[arch] = max(struct_type->alignment[arch], + field_type->alignment[arch]); + struct_type->aligned_size[arch] = ddt_aligned(struct_type->size[arch], + struct_type->alignment[arch]); + } + field->type = field_type; + field->send = NULL; + field->recv = NULL; + + xbt_dynar_push(struct_type->category.struct_data.fields, &field); + + DEBUG3("Push a %s into %s at offset %ld.", + field_type->name, struct_type->name,field->offset[GRAS_THISARCH]); + DEBUG3(" f={size=%ld,align=%ld,asize=%ld}", + field_type->size[GRAS_THISARCH], + field_type->alignment[GRAS_THISARCH], + field_type->aligned_size[GRAS_THISARCH]); + DEBUG3(" s={size=%ld,align=%ld,asize=%ld}", + struct_type->size[GRAS_THISARCH], + struct_type->alignment[GRAS_THISARCH], + struct_type->aligned_size[GRAS_THISARCH]); + XBT_OUT; +} + +/** \brief Close a structure description + * + * No new field can be added afterward, and it is mandatory to close the structure before using it. + */ +void +gras_datadesc_struct_close(gras_datadesc_type_t struct_type) { + XBT_IN; + struct_type->category.struct_data.closed = 1; + DEBUG4("structure %s closed. size=%ld,align=%ld,asize=%ld", + struct_type->name, + struct_type->size[GRAS_THISARCH], + struct_type->alignment[GRAS_THISARCH], + struct_type->aligned_size[GRAS_THISARCH]); +} + +/** + * gras_datadesc_cycle_set: + * + * Tell GRAS that the pointers of the type described by ddt may present + * some loop, and that the cycle detection mechanism is needed. + * + * Note that setting this option when not needed have a rather bad effect + * on the performance (several times slower on big data). + */ +void +gras_datadesc_cycle_set(gras_datadesc_type_t ddt) { + ddt->cycle = 1; +} + +/** + * gras_datadesc_cycle_unset: + * + * Tell GRAS that the pointers of the type described by ddt do not present + * any loop and that cycle detection mechanism are not needed. + * (default) + */ +void +gras_datadesc_cycle_unset(gras_datadesc_type_t ddt) { + ddt->cycle = 0; +} + +/** \brief Declare a new union description */ +gras_datadesc_type_t + gras_datadesc_union(const char *name, + gras_datadesc_type_cb_int_t selector) { + + gras_datadesc_type_t res; + int arch; + + XBT_IN1("(%s)",name); + xbt_assert0(selector, + "Attempt to creat an union without field_count function"); + + res = gras_datadesc_by_name_or_null(name); + if (res) { + /* FIXME: Check that field redefinition matches */ + xbt_assert1(res->category_code == e_gras_datadesc_type_cat_union, + "Redefinition of type %s does not match", name); + xbt_assert1(res->category.union_data.selector == selector, + "Redefinition of type %s does not match", name); + VERB1("Discarding redefinition of %s",name); + return res; + } + + res = gras_ddt_new(name); + + for (arch=0; archsize[arch] = 0; + res->alignment[arch] = 0; + res->aligned_size[arch] = 0; + } + + res->category_code = e_gras_datadesc_type_cat_union; + res->category.union_data.fields = + xbt_dynar_new(sizeof(gras_dd_cat_field_t*), + &gras_dd_cat_field_free); + res->category.union_data.selector = selector; + + return res; +} + +/** \brief Append a new field to an union description */ +void gras_datadesc_union_append(gras_datadesc_type_t union_type, + const char *name, + gras_datadesc_type_t field_type) { + + gras_dd_cat_field_t field; + int arch; + + XBT_IN3("(%s %s.%s;)",field_type->name,union_type->name,name); + xbt_assert1(field_type->size != 0, + "Cannot add a dynamically sized field in union %s", + union_type->name); + + if (union_type->category.union_data.closed) { + VERB1("Ignoring request to add field to union %s (closed)", + union_type->name); + return; + } + + field=xbt_new0(s_gras_dd_cat_field_t,1); + + field->name = (char*)strdup(name); + field->type = field_type; + /* All offset are left to 0 in an union */ + + xbt_dynar_push(union_type->category.union_data.fields, &field); + + for (arch=0; archsize[arch] = max(union_type->size[arch], + field_type->size[arch]); + union_type->alignment[arch] = max(union_type->alignment[arch], + field_type->alignment[arch]); + union_type->aligned_size[arch] = ddt_aligned(union_type->size[arch], + union_type->alignment[arch]); + } +} + + +/** \brief Close an union description + * + * No new field can be added afterward, and it is mandatory to close the union before using it. + */ +void +gras_datadesc_union_close(gras_datadesc_type_t union_type) { + union_type->category.union_data.closed = 1; +} + +/** \brief Declare a new type being a reference to the one passed in arg */ +gras_datadesc_type_t + gras_datadesc_ref(const char *name, + gras_datadesc_type_t referenced_type) { + + gras_datadesc_type_t res; + gras_datadesc_type_t pointer_type = gras_datadesc_by_name("data pointer"); + int arch; + + XBT_IN1("(%s)",name); + res = gras_datadesc_by_name_or_null(name); + if (res) { + xbt_assert1(res->category_code == e_gras_datadesc_type_cat_ref, + "Redefinition of %s does not match",name); + xbt_assert1(res->category.ref_data.type == referenced_type, + "Redefinition of %s does not match",name); + xbt_assert1(res->category.ref_data.selector == NULL, + "Redefinition of %s does not match",name); + VERB1("Discarding redefinition of %s",name); + return res; + } + + res = gras_ddt_new(name); + + xbt_assert0(pointer_type, "Cannot get the description of data pointer"); + + for (arch=0; archsize[arch] = pointer_type->size[arch]; + res->alignment[arch] = pointer_type->alignment[arch]; + res->aligned_size[arch] = pointer_type->aligned_size[arch]; + } + + res->category_code = e_gras_datadesc_type_cat_ref; + res->category.ref_data.type = referenced_type; + res->category.ref_data.selector = NULL; + + return res; +} +/** \brief Declare a new type being a generic reference. + * + * The callback passed in argument is to be used to select which type is currently used. + * So, when GRAS wants to send a generic reference, it passes the current data to the selector + * callback and expects it to return the type description to use. + */ +gras_datadesc_type_t + gras_datadesc_ref_generic(const char *name, + gras_datadesc_selector_t selector) { + + gras_datadesc_type_t res; + gras_datadesc_type_t pointer_type = gras_datadesc_by_name("data pointer"); + int arch; + + XBT_IN1("(%s)",name); + res = gras_datadesc_by_name_or_null(name); + + if (res) { + xbt_assert1(res->category_code == e_gras_datadesc_type_cat_ref, + "Redefinition of type %s does not match", name); + xbt_assert1(res->category.ref_data.type == NULL, + "Redefinition of type %s does not match", name); + xbt_assert1(res->category.ref_data.selector == selector, + "Redefinition of type %s does not match", name); + VERB1("Discarding redefinition of %s",name); + return res; + } + res = gras_ddt_new(name); + + xbt_assert0(pointer_type, "Cannot get the description of data pointer"); + + for (arch=0; archsize[arch] = pointer_type->size[arch]; + res->alignment[arch] = pointer_type->alignment[arch]; + res->aligned_size[arch] = pointer_type->aligned_size[arch]; + } + + res->category_code = e_gras_datadesc_type_cat_ref; + + res->category.ref_data.type = NULL; + res->category.ref_data.selector = selector; + + return res; +} + +/** \brief Declare a new type being an array of fixed size and content */ +gras_datadesc_type_t + gras_datadesc_array_fixed(const char *name, + gras_datadesc_type_t element_type, + long int fixed_size) { + + gras_datadesc_type_t res; + int arch; + + XBT_IN1("(%s)",name); + res = gras_datadesc_by_name_or_null(name); + if (res) { + xbt_assert1(res->category_code == e_gras_datadesc_type_cat_array, + "Redefinition of type %s does not match", name); + + if (res->category.array_data.type != element_type) { + ERROR1("Redefinition of type %s does not match: array elements differ", name); + gras_datadesc_type_dump(res->category.array_data.type); + gras_datadesc_type_dump(element_type); + } + + xbt_assert1(res->category.array_data.fixed_size == fixed_size, + "Redefinition of type %s does not match", name); + xbt_assert1(res->category.array_data.dynamic_size == NULL, + "Redefinition of type %s does not match", name); + VERB1("Discarding redefinition of %s",name); + + return res; + } + res = gras_ddt_new(name); + + xbt_assert1(fixed_size > 0, "'%s' is a array of null fixed size",name); + for (arch=0; archsize[arch] = fixed_size * element_type->aligned_size[arch]; + res->alignment[arch] = element_type->alignment[arch]; + res->aligned_size[arch] = res->size[arch]; + } + + res->category_code = e_gras_datadesc_type_cat_array; + + res->category.array_data.type = element_type; + res->category.array_data.fixed_size = fixed_size; + res->category.array_data.dynamic_size = NULL; + + return res; +} + +/** \brief Declare a new type being an array of fixed size, but accepting several content types. */ +gras_datadesc_type_t gras_datadesc_array_dyn(const char *name, + gras_datadesc_type_t element_type, + gras_datadesc_type_cb_int_t dynamic_size) { + + gras_datadesc_type_t res; + int arch; + + XBT_IN1("(%s)",name); + xbt_assert1(dynamic_size, + "'%s' is a dynamic array without size discriminant", + name); + + res = gras_datadesc_by_name_or_null(name); + if (res) { + xbt_assert1(res->category_code == e_gras_datadesc_type_cat_array, + "Redefinition of type %s does not match", name); + xbt_assert1(res->category.array_data.type == element_type, + "Redefinition of type %s does not match", name); + xbt_assert1(res->category.array_data.fixed_size == 0, + "Redefinition of type %s does not match", name); + xbt_assert1(res->category.array_data.dynamic_size == dynamic_size, + "Redefinition of type %s does not match", name); + VERB1("Discarding redefinition of %s",name); + + return res; + } + + res = gras_ddt_new(name); + + for (arch=0; archsize[arch] = 0; /* make sure it indicates "dynamic" */ + res->alignment[arch] = element_type->alignment[arch]; + res->aligned_size[arch] = 0; /*FIXME: That was so in GS, but looks stupid*/ + } + + res->category_code = e_gras_datadesc_type_cat_array; + + res->category.array_data.type = element_type; + res->category.array_data.fixed_size = 0; + res->category.array_data.dynamic_size = dynamic_size; + + return res; +} + +/** \brief Declare a new type being an array which size can be found with \ref gras_cbps_i_pop + * + * Most of the time, you want to include a reference in your structure which + * is a pointer to a dynamic array whose size is fixed by another field of + * your structure. + * + * This case pops up so often that this function was created to take care of + * this case. It creates a dynamic array type whose size is poped from the + * current cbps, and then create a reference to it. + * + * The name of the created datatype will be the name of the element type, with + * '[]*' appended to it. + * + * Then to use it, you just have to make sure that your structure pre-callback + * does push the size of the array in the cbps (using #gras_cbps_i_push), and + * you are set. + * + * But be remember that this is a stack. If you have two different pop_arr, you + * should push the second one first, so that the first one is on the top of the + * list when the first field gets transfered. + * + */ +gras_datadesc_type_t + gras_datadesc_ref_pop_arr(gras_datadesc_type_t element_type) { + + gras_datadesc_type_t res; + char *name=(char*)xbt_malloc(strlen(element_type->name) + 4); + + sprintf(name,"%s[]",element_type->name); + + res = gras_datadesc_array_dyn(name,element_type, + gras_datadesc_cb_pop); + + sprintf(name,"%s[]*",element_type->name); + res = gras_datadesc_ref(name,res); + + free(name); + + return res; +} + +/* + *## + *## Constructor of container datatypes + *## + */ + +#include "xbt/dynar_private.h" +static void gras_datadesc_dynar_cb(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data) { + gras_datadesc_type_t subtype; + xbt_dynar_t dynar=(xbt_dynar_t)data; + + memcpy(&dynar->free_f, &typedesc->extra, sizeof(dynar->free_f)); + + /* search for the elemsize in what we have. If elements are "int", typedesc got is "int[]*" */ + subtype = gras_dd_find_field(typedesc,"data")->type; + + /* this is now a ref to array of what we're looking for */ + subtype = subtype->category.ref_data.type; + subtype = subtype->category.array_data.type; + + DEBUG1("subtype is %s",subtype->name); + + dynar->elmsize = subtype->size[GRAS_THISARCH]; + dynar->size = dynar->used; +} + +/** \brief Declare a new type being a dynar in which each elements are of the given type + * + * The type gets registered under the name "dynar(%s)_s", where %s is the name of the subtype. + * For example, a dynar of doubles will be called "dynar(double)_s" and a dynar of dynar of + * strings will be called "dynar(dynar(string)_s)_s". + * + * \param elm_t: the datadesc of the elements + * \param free_func: the function to use to free the elements when the dynar gets freed + */ +gras_datadesc_type_t +gras_datadesc_dynar(gras_datadesc_type_t elm_t, + void_f_pvoid_t *free_func) { + + char *buffname; + gras_datadesc_type_t res; + + asprintf(&buffname,"dynar(%s)_s",elm_t->name); + + res = gras_datadesc_struct(buffname); + + gras_datadesc_struct_append(res, "size", + gras_datadesc_by_name("unsigned long int")); + + gras_datadesc_struct_append(res, "used", + gras_datadesc_by_name("unsigned long int")); + + gras_datadesc_struct_append(res, "elmsize", + gras_datadesc_by_name("unsigned long int")); + + gras_datadesc_struct_append(res, "data", + gras_datadesc_ref_pop_arr (elm_t)); + + gras_datadesc_struct_append(res, "free_f", + gras_datadesc_by_name("function pointer")); + memcpy(res->extra,&free_func,sizeof(free_func)); + + gras_datadesc_struct_close(res); + + gras_datadesc_cb_field_push(res, "used"); + gras_datadesc_cb_recv(res, &gras_datadesc_dynar_cb); + + /* build a ref to it */ + free(buffname); + asprintf(&buffname,"dynar(%s)",elm_t->name); + res=gras_datadesc_ref(buffname,res); + free(buffname); + return res; +} + +#include "xbt/matrix.h" +static void gras_datadesc_matrix_cb(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data) { + gras_datadesc_type_t subtype; + xbt_matrix_t matrix=(xbt_matrix_t)data; + + memcpy(&matrix->free_f, &typedesc->extra, sizeof(matrix->free_f)); + + /* search for the elemsize in what we have. If elements are "int", typedesc got is "int[]*" */ + subtype = gras_dd_find_field(typedesc,"data")->type; + + /* this is now a ref to array of what we're looking for */ + subtype = subtype->category.ref_data.type; + subtype = subtype->category.array_data.type; + + DEBUG1("subtype is %s",subtype->name); + + matrix->elmsize = subtype->size[GRAS_THISARCH]; +} +gras_datadesc_type_t +gras_datadesc_matrix(gras_datadesc_type_t elm_t, + void_f_pvoid_t * const free_f) { + char *buffname; + gras_datadesc_type_t res; + + asprintf(&buffname,"s_xbt_matrix_t(%s)",elm_t->name); + res = gras_datadesc_struct(buffname); + + gras_datadesc_struct_append(res, "lines", + gras_datadesc_by_name("unsigned int")); + gras_datadesc_struct_append(res, "rows", + gras_datadesc_by_name("unsigned int")); + + gras_datadesc_struct_append(res, "elmsize", + gras_datadesc_by_name("unsigned long int")); + + gras_datadesc_struct_append(res, "data", + gras_datadesc_ref_pop_arr (elm_t)); + gras_datadesc_struct_append(res, "free_f", + gras_datadesc_by_name("function pointer")); + gras_datadesc_struct_close(res); + + gras_datadesc_cb_field_push(res, "lines"); + gras_datadesc_cb_field_push_multiplier(res, "rows"); + + gras_datadesc_cb_recv(res, &gras_datadesc_matrix_cb); + memcpy(res->extra,&free_f,sizeof(free_f)); + + /* build a ref to it */ + free(buffname); + asprintf(&buffname,"xbt_matrix_t(%s)",elm_t->name); + res=gras_datadesc_ref(buffname,res); + free(buffname); + return res; +} + +gras_datadesc_type_t +gras_datadesc_import_nws(const char *name, + const DataDescriptor *desc, + unsigned long howmany) { + THROW_UNIMPLEMENTED; +} + +/** + * (useful to push the sizes of the upcoming arrays, for example) + */ +void gras_datadesc_cb_send (gras_datadesc_type_t type, + gras_datadesc_type_cb_void_t send) { + type->send = send; +} +/** + * (useful to put the function pointers to the rigth value, for example) + */ +void gras_datadesc_cb_recv(gras_datadesc_type_t type, + gras_datadesc_type_cb_void_t recv) { + type->recv = recv; +} +/* + * gras_dd_find_field: + * + * Returns the type descriptor of the given field. Abort on error. + */ +static gras_dd_cat_field_t + gras_dd_find_field(gras_datadesc_type_t type, + const char *field_name) { + xbt_dynar_t field_array; + + gras_dd_cat_field_t field=NULL; + int field_num; + + if (type->category_code == e_gras_datadesc_type_cat_union) { + field_array = type->category.union_data.fields; + } else if (type->category_code == e_gras_datadesc_type_cat_struct) { + field_array = type->category.struct_data.fields; + } else { + ERROR2("%s (%p) is not a struct nor an union. There is no field.", type->name,(void*)type); + xbt_abort(); + } + xbt_dynar_foreach(field_array,field_num,field) { + if (!strcmp(field_name,field->name)) { + return field; + } + } + ERROR2("No field named '%s' in '%s'",field_name,type->name); + xbt_abort(); + +} + +/** + * The given datadesc must be a struct or union (abort if not). + * (useful to push the sizes of the upcoming arrays, for example) + */ +void gras_datadesc_cb_field_send (gras_datadesc_type_t type, + const char *field_name, + gras_datadesc_type_cb_void_t send) { + + gras_dd_cat_field_t field=gras_dd_find_field(type,field_name); + field->send = send; +} + + +/** + * The value, which must be an int, unsigned int, long int or unsigned long int + * is pushed to the stacks of sizes and can then be retrieved with + * \ref gras_datadesc_ref_pop_arr or directly with \ref gras_cbps_i_pop. + */ +void gras_datadesc_cb_field_push (gras_datadesc_type_t type, + const char *field_name) { + + gras_dd_cat_field_t field=gras_dd_find_field(type,field_name); + gras_datadesc_type_t sub_type=field->type; + + DEBUG3("add a PUSHy cb to '%s' field (type '%s') of '%s'", + field_name,sub_type->name,type->name); + if (!strcmp("int",sub_type->name)) { + field->send = gras_datadesc_cb_push_int; + } else if (!strcmp("unsigned int",sub_type->name)) { + field->send = gras_datadesc_cb_push_uint; + } else if (!strcmp("long int",sub_type->name)) { + field->send = gras_datadesc_cb_push_lint; + } else if (!strcmp("unsigned long int",sub_type->name)) { + field->send = gras_datadesc_cb_push_ulint; + } else { + ERROR1("Field %s is not an int, unsigned int, long int neither unsigned long int", + sub_type->name); + xbt_abort(); + } +} + +/** + * Any previously pushed value is poped and the field value is multiplied to + * it. The result is then pushed back into the stack of sizes. It can then be + * retrieved with \ref gras_datadesc_ref_pop_arr or directly with \ref + * gras_cbps_i_pop. + * + * The field must be an int, unsigned int, long int or unsigned long int. + */ +void gras_datadesc_cb_field_push_multiplier (gras_datadesc_type_t type, + const char *field_name) { + + gras_dd_cat_field_t field=gras_dd_find_field(type,field_name); + gras_datadesc_type_t sub_type=field->type; + + DEBUG3("add a MPUSHy cb to '%s' field (type '%s') of '%s'", + field_name,sub_type->name,type->name); + if (!strcmp("int",sub_type->name)) { + field->send = gras_datadesc_cb_push_int_mult; + } else if (!strcmp("unsigned int",sub_type->name)) { + field->send = gras_datadesc_cb_push_uint_mult; + } else if (!strcmp("long int",sub_type->name)) { + field->send = gras_datadesc_cb_push_lint_mult; + } else if (!strcmp("unsigned long int",sub_type->name)) { + field->send = gras_datadesc_cb_push_ulint_mult; + } else { + ERROR1("Field %s is not an int, unsigned int, long int neither unsigned long int", + sub_type->name); + xbt_abort(); + } +} + +/** + * The given datadesc must be a struct or union (abort if not). + * (useful to put the function pointers to the right value, for example) + */ +void gras_datadesc_cb_field_recv(gras_datadesc_type_t type, + const char *field_name, + gras_datadesc_type_cb_void_t recv) { + + gras_dd_cat_field_t field=gras_dd_find_field(type,field_name); + field->recv = recv; +} + +/* + * Free a datadesc. Should only be called at xbt_exit. + */ +void gras_datadesc_free(gras_datadesc_type_t *type) { + + DEBUG1("Let's free ddt %s",(*type)->name); + + switch ((*type)->category_code) { + case e_gras_datadesc_type_cat_scalar: + case e_gras_datadesc_type_cat_ref: + case e_gras_datadesc_type_cat_array: + /* nothing to free in there */ + break; + + case e_gras_datadesc_type_cat_struct: + xbt_dynar_free(&( (*type)->category.struct_data.fields )); + break; + + case e_gras_datadesc_type_cat_union: + xbt_dynar_free(&( (*type)->category.union_data.fields )); + break; + + default: + /* datadesc was invalid. Killing it is like euthanasy, I guess */ + break; + } + free((*type)->name); + free(*type); + type=NULL; +} + +/** + * gras_datadesc_type_cmp: + * + * Compares two datadesc types with the same semantic than strcmp. + * + * This comparison does not take the set headers into account (name and ID), + * but only the payload (actual type description). + */ +int gras_datadesc_type_cmp(const gras_datadesc_type_t d1, + const gras_datadesc_type_t d2) { + int ret,cpt; + gras_dd_cat_field_t field1,field2; + gras_datadesc_type_t field_desc_1,field_desc_2; + + if (d1 == d2) return 0; /* easy optimization */ + + if (!d1 && d2) { + DEBUG0("ddt_cmp: !d1 && d2 => 1"); + return 1; + } + if (!d1 && !d2) { + DEBUG0("ddt_cmp: !d1 && !d2 => 0"); + return 0; + } + if ( d1 && !d2) { + DEBUG0("ddt_cmp: d1 && !d2 => -1"); + return -1; + } + + for (cpt=0; cptsize[cpt] != d2->size[cpt]) { + DEBUG5("ddt_cmp: %s->size=%ld != %s->size=%ld (on %s)", + d1->name,d1->size[cpt],d2->name,d2->size[cpt], + gras_arches[cpt].name); + return d1->size[cpt] > d2->size[cpt] ? 1 : -1; + } + + if (d1->alignment[cpt] != d2->alignment[cpt]) { + DEBUG5("ddt_cmp: %s->alignment=%ld != %s->alignment=%ld (on %s)", + d1->name,d1->alignment[cpt],d2->name,d2->alignment[cpt], + gras_arches[cpt].name); + return d1->alignment[cpt] > d2->alignment[cpt] ? 1 : -1; + } + + if (d1->aligned_size[cpt] != d2->aligned_size[cpt]) { + DEBUG5("ddt_cmp: %s->aligned_size=%ld != %s->aligned_size=%ld (on %s)", + d1->name,d1->aligned_size[cpt],d2->name,d2->aligned_size[cpt], + gras_arches[cpt].name); + return d1->aligned_size[cpt] > d2->aligned_size[cpt] ? 1 : -1; + } + } + + if (d1->category_code != d2->category_code) { + DEBUG4("ddt_cmp: %s->cat=%s != %s->cat=%s", + d1->name,gras_datadesc_cat_names[d1->category_code], + d2->name,gras_datadesc_cat_names[d2->category_code]); + return d1->category_code > d2->category_code ? 1 : -1; + } + + if (d1->send != d2->send) { + DEBUG4("ddt_cmp: %s->send=%p != %s->send=%p", + d1->name,(void*)d1->send, d2->name,(void*)d2->send); + return 1; /* ISO C forbids ordered comparisons of pointers to functions */ + } + + if (d1->recv != d2->recv) { + DEBUG4("ddt_cmp: %s->recv=%p != %s->recv=%p", + d1->name,(void*)d1->recv, d2->name,(void*)d2->recv); + return 1; /* ISO C forbids ordered comparisons of pointers to functions */ + } + + switch (d1->category_code) { + case e_gras_datadesc_type_cat_scalar: + if (d1->category.scalar_data.encoding != d2->category.scalar_data.encoding) + return d1->category.scalar_data.encoding > d2->category.scalar_data.encoding ? 1 : -1 ; + break; + + case e_gras_datadesc_type_cat_struct: + if (xbt_dynar_length(d1->category.struct_data.fields) != + xbt_dynar_length(d2->category.struct_data.fields)) { + DEBUG4("ddt_cmp: %s (having %lu fields) != %s (having %lu fields)", + d1->name, xbt_dynar_length(d1->category.struct_data.fields), + d2->name, xbt_dynar_length(d2->category.struct_data.fields)); + + return xbt_dynar_length(d1->category.struct_data.fields) > + xbt_dynar_length(d2->category.struct_data.fields) ? + 1 : -1; + } + xbt_dynar_foreach(d1->category.struct_data.fields, cpt, field1) { + + field2 = xbt_dynar_get_as(d2->category.struct_data.fields, cpt, gras_dd_cat_field_t); + field_desc_1 = field1->type; + field_desc_2 = field2->type; + ret = gras_datadesc_type_cmp(field_desc_1,field_desc_2); + if (ret) { + DEBUG6("%s->field[%d]=%s != %s->field[%d]=%s", + d1->name,cpt,field1->name, + d2->name,cpt,field2->name); + return ret; + } + + } + break; + + case e_gras_datadesc_type_cat_union: + if (d1->category.union_data.selector != d2->category.union_data.selector) + return 1; /* ISO C forbids ordered comparisons of pointers to functions */ + + if (xbt_dynar_length(d1->category.union_data.fields) != + xbt_dynar_length(d2->category.union_data.fields)) + return xbt_dynar_length(d1->category.union_data.fields) > + xbt_dynar_length(d2->category.union_data.fields) ? + 1 : -1; + + xbt_dynar_foreach(d1->category.union_data.fields, cpt, field1) { + + field2 = xbt_dynar_get_as(d2->category.union_data.fields, cpt, gras_dd_cat_field_t); + field_desc_1 = field1->type; + field_desc_2 = field2->type; + ret = gras_datadesc_type_cmp(field_desc_1,field_desc_2); + if (ret) + return ret; + + } + break; + + + case e_gras_datadesc_type_cat_ref: + if (d1->category.ref_data.selector != d2->category.ref_data.selector) + return 1; /* ISO C forbids ordered comparisons of pointers to functions */ + + if (d1->category.ref_data.type != d2->category.ref_data.type) + return d1->category.ref_data.type > d2->category.ref_data.type ? 1 : -1; + break; + + case e_gras_datadesc_type_cat_array: + if (d1->category.array_data.type != d2->category.array_data.type) + return d1->category.array_data.type > d2->category.array_data.type ? 1 : -1; + + if (d1->category.array_data.fixed_size != d2->category.array_data.fixed_size) + return d1->category.array_data.fixed_size > d2->category.array_data.fixed_size ? 1 : -1; + + if (d1->category.array_data.dynamic_size != d2->category.array_data.dynamic_size) + return 1; /* ISO C forbids ordered comparisons of pointers to functions */ + + break; + + default: + /* two stupidly created ddt are equally stupid ;) */ + break; + } + return 0; + +} diff --git a/src/gras_simix/DataDesc/gras_simix_ddt_exchange.c b/src/gras_simix/DataDesc/gras_simix_ddt_exchange.c new file mode 100644 index 0000000000..8c15e20abc --- /dev/null +++ b/src/gras_simix/DataDesc/gras_simix_ddt_exchange.c @@ -0,0 +1,972 @@ +/* $Id$ */ + +/* ddt_exchange - send/recv data described */ + +/* Copyright (c) 2003 Olivier Aumage. */ +/* Copyright (c) 2003, 2004, 2005 Martin Quinson. */ +/* All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "xbt/ex.h" +#include "gras/DataDesc/datadesc_private.h" +#include "gras/Transport/transport_interface.h" /* gras_trp_send/recv */ + +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_ddt_exchange,gras_ddt, + "Sending data over the network"); +const char *gras_datadesc_cat_names[9] = { + "undefined", + "scalar", "struct", "union", "ref", "array", "ignored", + "invalid"}; + +static gras_datadesc_type_t int_type = NULL; +static gras_datadesc_type_t pointer_type = NULL; + +static XBT_INLINE void +gras_dd_send_int(gras_socket_t sock,int *i, int stable) { + + if (!int_type) { + int_type = gras_datadesc_by_name("int"); + xbt_assert(int_type); + } + + DEBUG1("send_int(%d)",*i); + gras_trp_send(sock, (char*)i, int_type->size[GRAS_THISARCH], stable); +} + +static XBT_INLINE void +gras_dd_recv_int(gras_socket_t sock, int r_arch, int *i) { + + if (!int_type) { + int_type = gras_datadesc_by_name("int"); + xbt_assert(int_type); + } + + if (int_type->size[GRAS_THISARCH] >= int_type->size[r_arch]) { + gras_trp_recv(sock, (char*)i, int_type->size[r_arch]); + if (r_arch != GRAS_THISARCH) + gras_dd_convert_elm(int_type,1,r_arch, i,i); + } else { + void *ptr = xbt_malloc(int_type->size[r_arch]); + + gras_trp_recv(sock, (char*)ptr, int_type->size[r_arch]); + if (r_arch != GRAS_THISARCH) + gras_dd_convert_elm(int_type,1,r_arch, ptr,i); + free(ptr); + } + DEBUG1("recv_int(%d)",*i); +} + +/* + * Note: here we suppose that the remote NULL is a sequence + * of 'length' bytes set to 0. + * FIXME: Check in configure? + */ +static XBT_INLINE int +gras_dd_is_r_null(char **r_ptr, long int length) { + int i; + + for (i=0; isize[r_arch] */ + char **l_ref, + int detect_cycle) { + char *l_data = NULL; + + xbt_assert1(size>0,"Cannot allocate %ld bytes!", size); + l_data = xbt_malloc((size_t)size); + + *l_ref = l_data; + DEBUG5("alloc_ref: l_data=%p, &l_data=%p; r_ref=%p; *r_ref=%p, r_len=%ld", + (void*)l_data,(void*)&l_data, + (void*)r_ref, (void*)(r_ref?*r_ref:NULL), r_len); + if (detect_cycle && r_ref && !gras_dd_is_r_null( r_ref, r_len)) { + void *ptr = xbt_malloc(sizeof(void *)); + + memcpy(ptr,l_ref, sizeof(void *)); + + DEBUG2("Insert l_ref=%p under r_ref=%p",*(void**)ptr, *(void**)r_ref); + + if (detect_cycle) + xbt_dict_set_ext(refs,(const char *) r_ref, r_len, ptr, xbt_free_f); + } +} + +static int +gras_datadesc_copy_rec(gras_cbps_t state, + xbt_dict_t refs, + gras_datadesc_type_t type, + char *src, + char *dst, + int subsize, + int detect_cycle) { + + + xbt_ex_t e; + int cpt; + gras_datadesc_type_t sub_type; /* type on which we recurse */ + int count = 0; + + VERB4("Copy a %s (%s) from %p to %p", + type->name, gras_datadesc_cat_names[type->category_code], + src,dst); + + if (type->send) { + type->send(type,state,src); + } + + switch (type->category_code) { + case e_gras_datadesc_type_cat_scalar: + memcpy(dst,src,type->size[GRAS_THISARCH]); + count += type->size[GRAS_THISARCH]; + break; + + case e_gras_datadesc_type_cat_struct: { + gras_dd_cat_struct_t struct_data; + gras_dd_cat_field_t field; + char *field_src; + char *field_dst; + + struct_data = type->category.struct_data; + xbt_assert1(struct_data.closed, + "Please call gras_datadesc_declare_struct_close on %s before copying it", + type->name); + VERB1(">> Copy all fields of the structure %s",type->name); + xbt_dynar_foreach(struct_data.fields, cpt, field) { + field_src = src + field->offset[GRAS_THISARCH]; + field_dst = dst + field->offset[GRAS_THISARCH]; + + sub_type = field->type; + + if (field->send) + field->send(type,state,field_src); + + DEBUG1("Copy field %s",field->name); + count += gras_datadesc_copy_rec(state,refs,sub_type, field_src, field_dst, 0, + detect_cycle || sub_type->cycle); + + if (XBT_LOG_ISENABLED(gras_ddt_exchange,xbt_log_priority_verbose)) { + if (sub_type == gras_datadesc_by_name("unsigned int")) { + VERB2("Copied value for field '%s': %d (type: unsigned int)",field->name, *(unsigned int*)field_dst); + } else if (sub_type == gras_datadesc_by_name("int")) { + VERB2("Copied value for field '%s': %d (type: int)",field->name, *(int*)field_dst); + + } else if (sub_type == gras_datadesc_by_name("unsigned long int")) { + VERB2("Copied value for field '%s': %ld (type: unsigned long int)",field->name, *(unsigned long int*)field_dst); + } else if (sub_type == gras_datadesc_by_name("long int")) { + VERB2("Copied value for field '%s': %ld (type: long int)",field->name, *(long int*)field_dst); + + } else if (sub_type == gras_datadesc_by_name("string")) { + VERB2("Copied value for field '%s': '%s' (type: string)", field->name, *(char**)field_dst); + } else { + VERB1("Copied a value for field '%s' (type not scalar?)", field->name); + } + } + + } + VERB1("<< Copied all fields of the structure %s", type->name); + + break; + } + + case e_gras_datadesc_type_cat_union: { + gras_dd_cat_union_t union_data; + gras_dd_cat_field_t field=NULL; + int field_num; + + union_data = type->category.union_data; + + xbt_assert1(union_data.closed, + "Please call gras_datadesc_declare_union_close on %s before copying it", + type->name); + /* retrieve the field number */ + field_num = union_data.selector(type, state, src); + + xbt_assert1(field_num > 0, + "union field selector of %s gave a negative value", + type->name); + + xbt_assert3(field_num < xbt_dynar_length(union_data.fields), + "union field selector of %s returned %d but there is only %lu fields", + type->name, field_num, xbt_dynar_length(union_data.fields)); + + /* Copy the content */ + field = xbt_dynar_get_as(union_data.fields, field_num, gras_dd_cat_field_t); + sub_type = field->type; + + if (field->send) + field->send(type,state,src); + + count += gras_datadesc_copy_rec(state,refs, sub_type, src, dst,0, + detect_cycle || sub_type->cycle); + + break; + } + + case e_gras_datadesc_type_cat_ref: { + gras_dd_cat_ref_t ref_data; + char **o_ref=NULL; + char **n_ref=NULL; + int reference_is_to_cpy; + + ref_data = type->category.ref_data; + + /* Detect the referenced type */ + sub_type = ref_data.type; + if (sub_type == NULL) { + sub_type = (*ref_data.selector)(type,state,src); + } + + /* Send the pointed data only if not already sent */ + if (*(void**)src == NULL) { + VERB0("Not copying NULL referenced data"); + *(void**)dst = NULL; + break; + } + o_ref=(char**)src; + + reference_is_to_cpy = 0; + TRY { + if (detect_cycle) { + /* return ignored. Just checking whether it's known or not */ + n_ref=xbt_dict_get_ext(refs,(char*)o_ref, sizeof(char*)); + } else { + reference_is_to_cpy = 1; + } + } CATCH(e) { + if (e.category != not_found_error) + RETHROW; + reference_is_to_cpy = 1; + xbt_ex_free(e); + } + + if (reference_is_to_cpy) { + int subsubcount = 0; + void *l_referenced=NULL; + VERB2("Copy a ref to '%s' referenced at %p",sub_type->name, (void*)*o_ref); + + if (!pointer_type) { + pointer_type = gras_datadesc_by_name("data pointer"); + xbt_assert(pointer_type); + } + + if (sub_type->category_code == e_gras_datadesc_type_cat_array) { + /* Damn. Reference to a dynamic array. Allocating the space for it + is more complicated */ + gras_dd_cat_array_t array_data = sub_type->category.array_data; + gras_datadesc_type_t subsub_type; + + subsub_type = array_data.type; + subsubcount = array_data.fixed_size; + if (subsubcount == 0) + subsubcount = array_data.dynamic_size(subsub_type,state,*o_ref); + + gras_dd_alloc_ref(refs, + subsub_type->size[GRAS_THISARCH] * subsubcount, + o_ref,pointer_type->size[GRAS_THISARCH], + (char**)&l_referenced, + detect_cycle); + } else { + gras_dd_alloc_ref(refs,sub_type->size[GRAS_THISARCH], + o_ref,pointer_type->size[GRAS_THISARCH], + (char**)&l_referenced, + detect_cycle); + } + + count += gras_datadesc_copy_rec(state,refs, sub_type, + *o_ref,(char*)l_referenced, subsubcount, + detect_cycle || sub_type->cycle); + + *(void**)dst=l_referenced; + VERB3("'%s' previously referenced at %p now at %p", + sub_type->name, *(void**)o_ref, l_referenced); + + } else { + VERB2("NOT copying data previously referenced @%p (already done, @%p now)", + *(void**)o_ref, *(void**)n_ref); + + *(void**)dst=*n_ref; + + } + break; + } + + case e_gras_datadesc_type_cat_array: { + gras_dd_cat_array_t array_data; + long int array_count; + char *src_ptr=src; + char *dst_ptr=dst; + long int elm_size; + + array_data = type->category.array_data; + + /* determine and send the element count */ + array_count = array_data.fixed_size; + if (array_count == 0) + array_count = subsize; + if (array_count == 0) { + array_count = array_data.dynamic_size(type,state,src); + xbt_assert1(array_count >=0, + "Invalid (negative) array size for type %s",type->name); + } + + /* send the content */ + sub_type = array_data.type; + elm_size = sub_type->aligned_size[GRAS_THISARCH]; + if (sub_type->category_code == e_gras_datadesc_type_cat_scalar) { + VERB1("Array of %ld scalars, copy it in one shot",array_count); + memcpy(dst, src, sub_type->aligned_size[GRAS_THISARCH] * array_count); + count += sub_type->aligned_size[GRAS_THISARCH] * array_count; + } else if (sub_type->category_code == e_gras_datadesc_type_cat_array && + sub_type->category.array_data.fixed_size > 0 && + sub_type->category.array_data.type->category_code == e_gras_datadesc_type_cat_scalar) { + + VERB1("Array of %ld fixed array of scalars, copy it in one shot", + array_count); + memcpy(dst,src,sub_type->category.array_data.type->aligned_size[GRAS_THISARCH] + * array_count * sub_type->category.array_data.fixed_size); + count += sub_type->category.array_data.type->aligned_size[GRAS_THISARCH] + * array_count * sub_type->category.array_data.fixed_size; + + } else { + VERB1("Array of %ld stuff, copy it in one after the other",array_count); + for (cpt=0; cptcycle); + src_ptr += elm_size; + dst_ptr += elm_size; + } + } + break; + } + + default: + xbt_assert0(0, "Invalid type"); + } + + return count; +} +/** + * gras_datadesc_copy: + * + * Copy the data pointed by src and described by type + * to a new location, and store a pointer to it in dst. + * + */ +int gras_datadesc_copy(gras_datadesc_type_t type, + void *src, void *dst) { + xbt_ex_t e; + static gras_cbps_t state=NULL; + static xbt_dict_t refs=NULL; /* all references already sent */ + int size=0; + + xbt_assert0(type,"called with NULL type descriptor"); + + if (!state) { + state = gras_cbps_new(); + refs = xbt_dict_new(); + } + + TRY { + size = gras_datadesc_copy_rec(state,refs,type,(char*)src,(char*)dst,0, + type->cycle); + } CLEANUP { + xbt_dict_reset(refs); + gras_cbps_reset(state); + } CATCH(e) { + RETHROW; + } + return size; +} + +/*** + *** Direct use functions + ***/ + +static void +gras_datadesc_send_rec(gras_socket_t sock, + gras_cbps_t state, + xbt_dict_t refs, + gras_datadesc_type_t type, + char *data, + int detect_cycle) { + + xbt_ex_t e; + int cpt; + gras_datadesc_type_t sub_type; /* type on which we recurse */ + + VERB2("Send a %s (%s)", + type->name, gras_datadesc_cat_names[type->category_code]); + + if (!strcmp(type->name,"string")) + VERB1("value: '%s'",*(char**)data); + + if (type->send) { + type->send(type,state,data); + DEBUG0("Run the emission callback"); + } + + switch (type->category_code) { + case e_gras_datadesc_type_cat_scalar: + gras_trp_send(sock, data, type->size[GRAS_THISARCH], 1); + break; + + case e_gras_datadesc_type_cat_struct: { + gras_dd_cat_struct_t struct_data; + gras_dd_cat_field_t field; + char *field_data; + + struct_data = type->category.struct_data; + xbt_assert1(struct_data.closed, + "Please call gras_datadesc_declare_struct_close on %s before sending it", + type->name); + VERB1(">> Send all fields of the structure %s",type->name); + xbt_dynar_foreach(struct_data.fields, cpt, field) { + field_data = data; + field_data += field->offset[GRAS_THISARCH]; + + sub_type = field->type; + + if (field->send) { + DEBUG1("Run the emission callback of field %s", field->name); + field->send(type,state,field_data); + } + + VERB1("Send field %s",field->name); + gras_datadesc_send_rec(sock,state,refs,sub_type, field_data, + detect_cycle || sub_type->cycle); + + } + VERB1("<< Sent all fields of the structure %s", type->name); + + break; + } + + case e_gras_datadesc_type_cat_union: { + gras_dd_cat_union_t union_data; + gras_dd_cat_field_t field=NULL; + int field_num; + + union_data = type->category.union_data; + + xbt_assert1(union_data.closed, + "Please call gras_datadesc_declare_union_close on %s before sending it", + type->name); + /* retrieve the field number */ + field_num = union_data.selector(type, state, data); + + xbt_assert1(field_num > 0, + "union field selector of %s gave a negative value", + type->name); + + xbt_assert3(field_num < xbt_dynar_length(union_data.fields), + "union field selector of %s returned %d but there is only %lu fields", + type->name, field_num, xbt_dynar_length(union_data.fields)); + + /* Send the field number */ + gras_dd_send_int(sock, &field_num, 0 /* not stable */); + + /* Send the content */ + field = xbt_dynar_get_as(union_data.fields, field_num, gras_dd_cat_field_t); + sub_type = field->type; + + if (field->send) + field->send(type,state,data); + + gras_datadesc_send_rec(sock,state,refs, sub_type, data, + detect_cycle || sub_type->cycle); + + break; + } + + case e_gras_datadesc_type_cat_ref: { + gras_dd_cat_ref_t ref_data; + void **ref=(void**)data; + int reference_is_to_send; + + ref_data = type->category.ref_data; + + /* Detect the referenced type and send it to peer if needed */ + sub_type = ref_data.type; + if (sub_type == NULL) { + sub_type = (*ref_data.selector)(type,state,data); + gras_dd_send_int(sock, (int*) &(sub_type->code),1 /*stable*/); + } + + /* Send the actual value of the pointer for cycle handling */ + if (!pointer_type) { + pointer_type = gras_datadesc_by_name("data pointer"); + xbt_assert(pointer_type); + } + + gras_trp_send(sock, (char*)data, + pointer_type->size[GRAS_THISARCH], 1 /*stable*/); + + /* Send the pointed data only if not already sent */ + if (*(void**)data == NULL) { + VERB0("Not sending NULL referenced data"); + break; + } + + reference_is_to_send = 0; + TRY { + if (detect_cycle) + /* return ignored. Just checking whether it's known or not */ + xbt_dict_get_ext(refs,(char*)ref, sizeof(char*)); + else + reference_is_to_send = 1; + } CATCH(e) { + if (e.category != not_found_error) + RETHROW; + reference_is_to_send = 1; + xbt_ex_free(e); + } + + if (reference_is_to_send) { + VERB1("Sending data referenced at %p", (void*)*ref); + if (detect_cycle) + xbt_dict_set_ext(refs, (char*)ref, sizeof(void*), ref, NULL); + gras_datadesc_send_rec(sock,state,refs, sub_type, *ref, + detect_cycle || sub_type->cycle); + + } else { + VERB1("Not sending data referenced at %p (already done)", (void*)*ref); + } + + break; + } + + case e_gras_datadesc_type_cat_array: { + gras_dd_cat_array_t array_data; + int count; + char *ptr=data; + long int elm_size; + + array_data = type->category.array_data; + + /* determine and send the element count */ + count = array_data.fixed_size; + if (count == 0) { + count = array_data.dynamic_size(type,state,data); + xbt_assert1(count >=0, + "Invalid (negative) array size for type %s",type->name); + gras_dd_send_int(sock, &count, 0/*non-stable*/); + } + + /* send the content */ + sub_type = array_data.type; + elm_size = sub_type->aligned_size[GRAS_THISARCH]; + if (sub_type->category_code == e_gras_datadesc_type_cat_scalar) { + VERB1("Array of %d scalars, send it in one shot",count); + gras_trp_send(sock, data, + sub_type->aligned_size[GRAS_THISARCH] * count, + 0 /* not stable */); + } else if (sub_type->category_code == e_gras_datadesc_type_cat_array && + sub_type->category.array_data.fixed_size > 0 && + sub_type->category.array_data.type->category_code == e_gras_datadesc_type_cat_scalar) { + + VERB1("Array of %d fixed array of scalars, send it in one shot",count); + gras_trp_send(sock, data, + sub_type->category.array_data.type->aligned_size[GRAS_THISARCH] + * count * sub_type->category.array_data.fixed_size, + 0 /* not stable */); + + } else { + for (cpt=0; cptcycle); + ptr += elm_size; + } + } + break; + } + + default: + xbt_assert0(0, "Invalid type"); + } +} + +/** + * gras_datadesc_send: + * + * Copy the data pointed by src and described by type to the socket + * + */ +void gras_datadesc_send(gras_socket_t sock, + gras_datadesc_type_t type, + void *src) { + + xbt_ex_t e; + static gras_cbps_t state=NULL; + static xbt_dict_t refs=NULL; /* all references already sent */ + + xbt_assert0(type,"called with NULL type descriptor"); + + if (!state) { + state = gras_cbps_new(); + refs = xbt_dict_new(); + } + + TRY { + gras_datadesc_send_rec(sock,state,refs,type,(char*)src, type->cycle); + } CLEANUP { + xbt_dict_reset(refs); + gras_cbps_reset(state); + } CATCH(e) { + RETHROW; + } +} + +/** + * gras_datadesc_recv_rec: + * + * Do the data reception job recursively. + * + * subsize used only to deal with vicious case of reference to dynamic array. + * This size is needed at the reference reception level (to allocate enough + * space) and at the array reception level (to fill enough room). + * + * Having this size passed as an argument of the recursive function is a crude + * hack, but I was told that working code is sometimes better than neat one ;) + */ +static void +gras_datadesc_recv_rec(gras_socket_t sock, + gras_cbps_t state, + xbt_dict_t refs, + gras_datadesc_type_t type, + int r_arch, + char **r_data, + long int r_lgr, + char *l_data, + int subsize, + int detect_cycle) { + + int cpt; + gras_datadesc_type_t sub_type; + xbt_ex_t e; + + VERB2("Recv a %s @%p", type->name, (void*)l_data); + xbt_assert(l_data); + + switch (type->category_code) { + case e_gras_datadesc_type_cat_scalar: + if (type->size[GRAS_THISARCH] == type->size[r_arch]) { + gras_trp_recv(sock, (char*)l_data, type->size[r_arch]); + if (r_arch != GRAS_THISARCH) + gras_dd_convert_elm(type,1,r_arch, l_data,l_data); + } else { + void *ptr = xbt_malloc(type->size[r_arch]); + + gras_trp_recv(sock, (char*)ptr, type->size[r_arch]); + if (r_arch != GRAS_THISARCH) + gras_dd_convert_elm(type,1,r_arch, ptr,l_data); + free(ptr); + } + break; + + case e_gras_datadesc_type_cat_struct: { + gras_dd_cat_struct_t struct_data; + gras_dd_cat_field_t field; + + struct_data = type->category.struct_data; + + xbt_assert1(struct_data.closed, + "Please call gras_datadesc_declare_struct_close on %s before receiving it", + type->name); + VERB1(">> Receive all fields of the structure %s",type->name); + xbt_dynar_foreach(struct_data.fields, cpt, field) { + char *field_data = l_data + field->offset[GRAS_THISARCH]; + + sub_type = field->type; + + gras_datadesc_recv_rec(sock,state,refs, sub_type, + r_arch,NULL,0, + field_data,-1, + detect_cycle || sub_type->cycle); + + if (field->recv) { + DEBUG1("Run the reception callback of field %s", field->name); + field->recv(type,state,(void*)l_data); + } + + } + VERB1("<< Received all fields of the structure %s", type->name); + + break; + } + + case e_gras_datadesc_type_cat_union: { + gras_dd_cat_union_t union_data; + gras_dd_cat_field_t field=NULL; + int field_num; + + union_data = type->category.union_data; + + xbt_assert1(union_data.closed, + "Please call gras_datadesc_declare_union_close on %s before receiving it", + type->name); + /* retrieve the field number */ + gras_dd_recv_int(sock, r_arch, &field_num); + if (field_num < 0) + THROW1(mismatch_error,0, + "Received union field for %s is negative", type->name); + if (field_num > xbt_dynar_length(union_data.fields)) + THROW3(mismatch_error,0, + "Received union field for %s is said to be #%d but there is only %lu fields", + type->name, field_num, xbt_dynar_length(union_data.fields)); + + /* Recv the content */ + field = xbt_dynar_get_as(union_data.fields, field_num, gras_dd_cat_field_t); + sub_type = field->type; + + gras_datadesc_recv_rec(sock,state,refs, sub_type, + r_arch,NULL,0, + l_data,-1, + detect_cycle || sub_type->cycle); + if (field->recv) + field->recv(type,state,l_data); + + break; + } + + case e_gras_datadesc_type_cat_ref: { + char **r_ref = NULL; + char **l_ref = NULL; + gras_dd_cat_ref_t ref_data; + int reference_is_to_recv = 0; + + ref_data = type->category.ref_data; + + /* Get the referenced type locally or from peer */ + sub_type = ref_data.type; + if (sub_type == NULL) { + int ref_code; + gras_dd_recv_int(sock, r_arch, &ref_code); + sub_type = gras_datadesc_by_id(ref_code); + } + + /* Get the actual value of the pointer for cycle handling */ + if (!pointer_type) { + pointer_type = gras_datadesc_by_name("data pointer"); + xbt_assert(pointer_type); + } + + r_ref = xbt_malloc(pointer_type->size[r_arch]); + + gras_trp_recv(sock, (char*)r_ref, + pointer_type->size[r_arch]); + + /* Receive the pointed data only if not already sent */ + if (gras_dd_is_r_null(r_ref, pointer_type->size[r_arch])) { + VERB1("Not receiving data remotely referenced @%p since it's NULL", + *(void **)r_ref); + *(void**)l_data = NULL; + free(r_ref); + break; + } + + reference_is_to_recv = 0; + TRY { + if (detect_cycle) { + l_ref = xbt_dict_get_ext(refs, (char*)r_ref, pointer_type->size[r_arch]); + } else { + reference_is_to_recv = 1; + } + } CATCH(e) { + if (e.category != not_found_error) + RETHROW; + reference_is_to_recv = 1; + xbt_ex_free(e); + } + + if (reference_is_to_recv) { + int subsubcount = 0; + void *l_referenced=NULL; + + VERB2("Receiving a ref to '%s', remotely @%p", + sub_type->name, *(void**)r_ref); + if (sub_type->category_code == e_gras_datadesc_type_cat_array) { + /* Damn. Reference to a dynamic array. Allocating the space for it + is more complicated */ + gras_dd_cat_array_t array_data = sub_type->category.array_data; + gras_datadesc_type_t subsub_type; + + subsubcount = array_data.fixed_size; + if (subsubcount == 0) + gras_dd_recv_int(sock, r_arch, &subsubcount); + + subsub_type = array_data.type; + + + gras_dd_alloc_ref(refs, + subsub_type->size[GRAS_THISARCH] * subsubcount, + r_ref,pointer_type->size[r_arch], + (char**)&l_referenced, + detect_cycle); + } else { + gras_dd_alloc_ref(refs,sub_type->size[GRAS_THISARCH], + r_ref,pointer_type->size[r_arch], + (char**)&l_referenced, + detect_cycle); + } + + gras_datadesc_recv_rec(sock,state,refs, sub_type, + r_arch,r_ref,pointer_type->size[r_arch], + (char*)l_referenced, subsubcount, + detect_cycle || sub_type->cycle); + + *(void**)l_data=l_referenced; + VERB3("'%s' remotely referenced at %p locally at %p", + sub_type->name, *(void**)r_ref, l_referenced); + + } else { + VERB2("NOT receiving data remotely referenced @%p (already done, @%p here)", + *(void**)r_ref, *(void**)l_ref); + + *(void**)l_data=*l_ref; + + } + free(r_ref); + break; + } + + case e_gras_datadesc_type_cat_array: { + gras_dd_cat_array_t array_data; + int count; + char *ptr; + long int elm_size; + + array_data = type->category.array_data; + /* determine element count locally, or from caller, or from peer */ + count = array_data.fixed_size; + if (count == 0) + count = subsize; + if (count == 0) + gras_dd_recv_int(sock, r_arch, &count); + if (count == 0) + THROW1(mismatch_error,0, + "Invalid (=0) array size for type %s",type->name); + + /* receive the content */ + sub_type = array_data.type; + if (sub_type->category_code == e_gras_datadesc_type_cat_scalar) { + VERB1("Array of %d scalars, get it in one shoot", count); + if (sub_type->aligned_size[GRAS_THISARCH] >= + sub_type->aligned_size[r_arch]) { + gras_trp_recv(sock, (char*)l_data, + sub_type->aligned_size[r_arch] * count); + if (r_arch != GRAS_THISARCH) + gras_dd_convert_elm(sub_type,count,r_arch, l_data,l_data); + } else { + ptr = xbt_malloc(sub_type->aligned_size[r_arch] * count); + + gras_trp_recv(sock, (char*)ptr, + sub_type->size[r_arch] * count); + if (r_arch != GRAS_THISARCH) + gras_dd_convert_elm(sub_type,count,r_arch, ptr,l_data); + free(ptr); + } + } else if (sub_type->category_code == e_gras_datadesc_type_cat_array && + sub_type->category.array_data.fixed_size > 0 && + sub_type->category.array_data.type->category_code == e_gras_datadesc_type_cat_scalar) { + gras_datadesc_type_t subsub_type; + array_data = sub_type->category.array_data; + subsub_type = array_data.type; + + VERB1("Array of %d fixed array of scalars, get it in one shot",count); + if (subsub_type->aligned_size[GRAS_THISARCH] >= + subsub_type->aligned_size[r_arch]) { + gras_trp_recv(sock, (char*)l_data, + subsub_type->aligned_size[r_arch] * count * + array_data.fixed_size); + if (r_arch != GRAS_THISARCH) + gras_dd_convert_elm(subsub_type,count*array_data.fixed_size,r_arch, l_data,l_data); + } else { + ptr = xbt_malloc(subsub_type->aligned_size[r_arch] * count*array_data.fixed_size); + + gras_trp_recv(sock, (char*)ptr, + subsub_type->size[r_arch] * count*array_data.fixed_size); + if (r_arch != GRAS_THISARCH) + gras_dd_convert_elm(subsub_type,count*array_data.fixed_size,r_arch, ptr,l_data); + free(ptr); + } + + + } else { + /* not scalar content, get it recursively (may contain pointers) */ + elm_size = sub_type->aligned_size[GRAS_THISARCH]; + VERB2("Receive a %d-long array of %s",count, sub_type->name); + + ptr = l_data; + for (cpt=0; cptcycle); + + ptr += elm_size; + } + } + break; + } + + default: + xbt_assert0(0, "Invalid type"); + } + + if (type->recv) + type->recv(type,state,l_data); + + if (!strcmp(type->name,"string")) + VERB1("value: '%s'",*(char**)l_data); + +} + +/** + * gras_datadesc_recv: + * + * Get an instance of the datatype described by @type from the @socket, + * and store a pointer to it in @dst + * + */ +void +gras_datadesc_recv(gras_socket_t sock, + gras_datadesc_type_t type, + int r_arch, + void *dst) { + + xbt_ex_t e; + static gras_cbps_t state=NULL; /* callback persistent state */ + static xbt_dict_t refs=NULL; /* all references already sent */ + + if (!state) { + state = gras_cbps_new(); + refs = xbt_dict_new(); + } + + xbt_assert0(type,"called with NULL type descriptor"); + TRY { + gras_datadesc_recv_rec(sock, state, refs, type, + r_arch, NULL, 0, + (char *) dst,-1, + type->cycle); + } CLEANUP { + xbt_dict_reset(refs); + gras_cbps_reset(state); + } CATCH(e) { + RETHROW; + } +} + diff --git a/src/gras_simix/DataDesc/gras_simix_ddt_parse.c b/src/gras_simix/DataDesc/gras_simix_ddt_parse.c new file mode 100644 index 0000000000..effc5f5a10 --- /dev/null +++ b/src/gras_simix/DataDesc/gras_simix_ddt_parse.c @@ -0,0 +1,752 @@ +/* $Id$ */ + +/* DataDesc/ddt_parse.c -- automatic parsing of data structures */ + +/* Copyright (c) 2003 Arnaud Legrand. */ +/* Copyright (c) 2003, 2004 Martin Quinson. */ +/* All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include /* isdigit */ + +#include "xbt/ex.h" +#include "gras/DataDesc/datadesc_private.h" +#include "gras/DataDesc/ddt_parse.yy.h" + +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_ddt_parse,gras_ddt, + "Parsing C data structures to build GRAS data description"); + +typedef struct s_type_modifier{ + short is_long; + int is_unsigned:1; + int is_short:1; + + int is_struct:1; + int is_union:1; + int is_enum:1; + + int is_ref:1; + + int is_dynar:2; + int is_matrix:2; +} s_type_modifier_t,*type_modifier_t; + +typedef struct s_field { + gras_datadesc_type_t type; + char *type_name; + char *name; + s_type_modifier_t tm; +} s_identifier_t; + +extern char *gras_ddt_parse_text; /* text being considered in the parser */ + +/* local functions */ +static void parse_type_modifier(type_modifier_t type_modifier) { + XBT_IN; + do { + if (gras_ddt_parse_tok_num == GRAS_DDT_PARSE_TOKEN_STAR) { + /* This only used when parsing 'short *' since this function returns when int, float, double,... is encountered */ + DEBUG0("This is a reference"); + type_modifier->is_ref++; + + } else if (!strcmp(gras_ddt_parse_text,"unsigned")) { + DEBUG0("This is an unsigned"); + type_modifier->is_unsigned = 1; + + } else if (!strcmp(gras_ddt_parse_text,"short")) { + DEBUG0("This is short"); + type_modifier->is_short = 1; + + } else if (!strcmp(gras_ddt_parse_text,"long")) { + DEBUG0("This is long"); + type_modifier->is_long++; /* handle "long long" */ + + } else if (!strcmp(gras_ddt_parse_text,"struct")) { + DEBUG0("This is a struct"); + type_modifier->is_struct = 1; + + } else if (!strcmp(gras_ddt_parse_text,"union")) { + DEBUG0("This is an union"); + type_modifier->is_union = 1; + + } else if (!strcmp(gras_ddt_parse_text,"enum")) { + DEBUG0("This is an enum"); + type_modifier->is_enum = 1; + + } else if (gras_ddt_parse_tok_num == GRAS_DDT_PARSE_TOKEN_EMPTY) { + DEBUG0("Pass space"); + + } else { + DEBUG1("Done with modifiers (got %s)",gras_ddt_parse_text); + break; + } + + gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump(); + if((gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_WORD) && + (gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_STAR)) { + DEBUG2("Done with modifiers (got %s,%d)",gras_ddt_parse_text,gras_ddt_parse_tok_num); + break; + } + } while(1); + XBT_OUT; +} + +static void print_type_modifier(s_type_modifier_t tm) { + int i; + + XBT_IN; + if (tm.is_unsigned) printf("(unsigned) "); + if (tm.is_short) printf("(short) "); + for (i=0 ; iname)+48); + DEBUG2("Array specification (size=%ld, elm='%s'), change pushed type", + size,former.type_name); + sprintf(array.type_name,"%s%s%s%s[%ld]", + (former.tm.is_unsigned?"u ":""), + (former.tm.is_short?"s ":""), + (former.tm.is_long?"l ":""), + former.type_name, + size); + free(former.type_name); + + array.type = gras_datadesc_array_fixed(array.type_name, former.type, size); /* redeclaration are ignored */ + array.name = former.name; + + xbt_dynar_push(dynar,&array); + XBT_OUT; +} +static void change_to_ref(xbt_dynar_t dynar) { + s_identifier_t former,ref; + memset(&ref,0,sizeof(ref)); + + XBT_IN; + xbt_dynar_pop(dynar,&former); + ref.type_name=(char*)xbt_malloc(strlen(former.type->name)+2); + DEBUG1("Ref specification (elm='%s'), change pushed type", former.type_name); + sprintf(ref.type_name,"%s*",former.type_name); + free(former.type_name); + + ref.type = gras_datadesc_ref(ref.type_name, former.type); /* redeclaration are ignored */ + ref.name = former.name; + + xbt_dynar_push(dynar,&ref); + XBT_OUT; +} + +static void change_to_ref_pop_array(xbt_dynar_t dynar) { + s_identifier_t former,ref; + memset(&ref,0,sizeof(ref)); + + XBT_IN; + xbt_dynar_pop(dynar,&former); + ref.type = gras_datadesc_ref_pop_arr(former.type); /* redeclaration are ignored */ + ref.type_name = (char*)strdup(ref.type->name); + ref.name = former.name; + + free(former.type_name); + + xbt_dynar_push(dynar,&ref); + XBT_OUT; +} + +static void change_to_dynar_of(xbt_dynar_t dynar,gras_datadesc_type_t subtype) { + s_identifier_t former,ref; + memset(&ref,0,sizeof(ref)); + + XBT_IN; + xbt_dynar_pop(dynar,&former); + ref.type = gras_datadesc_dynar(subtype,NULL); /* redeclaration are ignored */ + ref.type_name = (char*)strdup(ref.type->name); + ref.name = former.name; + + free(former.type_name); + + xbt_dynar_push(dynar,&ref); + XBT_OUT; +} + +static void change_to_matrix_of(xbt_dynar_t dynar,gras_datadesc_type_t subtype) { + s_identifier_t former,ref; + memset(&ref,0,sizeof(ref)); + + XBT_IN; + xbt_dynar_pop(dynar,&former); + ref.type = gras_datadesc_matrix(subtype,NULL); /* redeclaration are ignored */ + ref.type_name = (char*)strdup(ref.type->name); + ref.name = former.name; + + free(former.type_name); + + xbt_dynar_push(dynar,&ref); + XBT_OUT; +} + +static void add_free_f(xbt_dynar_t dynar,void_f_pvoid_t free_f) { + s_identifier_t former,ref; + memset(&ref,0,sizeof(ref)); + + XBT_IN; + xbt_dynar_pop(dynar,&former); + memcpy(former.type->extra,&free_f, sizeof(free_f)); + xbt_dynar_push(dynar,&former); + XBT_OUT; +} + +static void parse_statement(char *definition, + xbt_dynar_t identifiers, + xbt_dynar_t fields_to_push) { + char buffname[512]; + + s_identifier_t identifier; + + int expect_id_separator = 0; + + XBT_IN; + memset(&identifier,0,sizeof(identifier)); + + gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump(); + if(gras_ddt_parse_tok_num == GRAS_DDT_PARSE_TOKEN_RA) { + XBT_OUT; + THROW0(mismatch_error,0,"End of the englobing structure or union"); + } + + if (XBT_LOG_ISENABLED(gras_ddt_parse,xbt_log_priority_debug)) { + int colon_pos; + for (colon_pos = gras_ddt_parse_col_pos; + definition[colon_pos] != ';'; + colon_pos++); + definition[colon_pos] = '\0'; + DEBUG3("Parse the statement \"%s%s;\" (col_pos=%d)", + gras_ddt_parse_text, + definition+gras_ddt_parse_col_pos, + gras_ddt_parse_col_pos); + definition[colon_pos] = ';'; + } + + if(gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_WORD) + PARSE_ERROR1("Unparsable symbol: found a typeless statement (got '%s' instead)", + gras_ddt_parse_text); + + /**** get the type modifier of this statement ****/ + parse_type_modifier(&identifier.tm); + + /* FIXME: This does not detect recursive definitions at all? */ + if (identifier.tm.is_union || identifier.tm.is_enum || identifier.tm.is_struct) + PARSE_ERROR0("Unimplemented feature: GRAS_DEFINE_TYPE cannot handle recursive type definition yet"); + + /**** get the base type, giving "short a" the needed love ****/ + if (!identifier.tm.is_union && + !identifier.tm.is_enum && + !identifier.tm.is_struct && + + (identifier.tm.is_short || identifier.tm.is_long || identifier.tm.is_unsigned) && + + strcmp(gras_ddt_parse_text,"char") && + strcmp(gras_ddt_parse_text,"float") && + strcmp(gras_ddt_parse_text,"double") && + strcmp(gras_ddt_parse_text,"int") ) { + + /* bastard user, they omited "int" ! */ + identifier.type_name=(char*)strdup("int"); + DEBUG0("the base type is 'int', which were omited (you vicious user)"); + } else { + identifier.type_name=(char*)strdup(gras_ddt_parse_text); + DEBUG1("the base type is '%s'",identifier.type_name); + gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump(); + } + + /**** build the base type for latter use ****/ + if (identifier.tm.is_union) { + PARSE_ERROR0("Unimplemented feature: GRAS_DEFINE_TYPE cannot handle union yet (get callback from annotation?)"); + + } else if (identifier.tm.is_enum) { + PARSE_ERROR0("Unimplemented feature: GRAS_DEFINE_TYPE cannot handle enum yet"); + + } else if (identifier.tm.is_struct) { + sprintf(buffname,"struct %s",identifier.type_name); + identifier.type = gras_datadesc_struct(buffname); /* Get created when does not exist */ + + } else if (identifier.tm.is_unsigned) { + if (!strcmp(identifier.type_name,"int")) { + if (identifier.tm.is_long == 2) { + identifier.type = gras_datadesc_by_name("unsigned long long int"); + } else if (identifier.tm.is_long) { + identifier.type = gras_datadesc_by_name("unsigned long int"); + } else if (identifier.tm.is_short) { + identifier.type = gras_datadesc_by_name("unsigned short int"); + } else { + identifier.type = gras_datadesc_by_name("unsigned int"); + } + + } else if (!strcmp(identifier.type_name, "char")) { + identifier.type = gras_datadesc_by_name("unsigned char"); + + } else { /* impossible, gcc parses this shit before us */ + THROW_IMPOSSIBLE; + } + + } else if (!strcmp(identifier.type_name, "float")) { + /* no modificator allowed by gcc */ + identifier.type = gras_datadesc_by_name("float"); + + } else if (!strcmp(identifier.type_name, "double")) { + if (identifier.tm.is_long) + PARSE_ERROR0("long double not portable and thus not handled"); + + identifier.type = gras_datadesc_by_name("double"); + + } else { /* signed integer elemental */ + if (!strcmp(identifier.type_name,"int")) { + if (identifier.tm.is_long == 2) { + identifier.type = gras_datadesc_by_name("signed long long int"); + } else if (identifier.tm.is_long) { + identifier.type = gras_datadesc_by_name("signed long int"); + } else if (identifier.tm.is_short) { + identifier.type = gras_datadesc_by_name("signed short int"); + } else { + identifier.type = gras_datadesc_by_name("int"); + } + + } else if (!strcmp(identifier.type_name, "char")) { + identifier.type = gras_datadesc_by_name("char"); + + } else { + DEBUG1("Base type is a constructed one (%s)",identifier.type_name); + if (!strcmp(identifier.type_name,"xbt_matrix_t")) { + identifier.tm.is_matrix = 1; + } else if (!strcmp(identifier.type_name,"xbt_dynar_t")) { + identifier.tm.is_dynar = 1; + } else { + identifier.type = gras_datadesc_by_name(identifier.type_name); + if (!identifier.type) + PARSE_ERROR1("Unknown base type '%s'",identifier.type_name); + } + } + } + /* Now identifier.type and identifier.name speak about the base type. + Stars are not eaten unless 'int' was omitted. + We will have to enhance it if we are in fact asked for array or reference. + + Dynars and matrices also need some extra love (prodiged as annotations) + */ + + /**** look for the symbols of this type ****/ + for(expect_id_separator = 0; + + (/*(gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_EMPTY) && FIXME*/ + (gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_SEMI_COLON)) ; + + gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump() ) { + + if(expect_id_separator) { + if(gras_ddt_parse_tok_num == GRAS_DDT_PARSE_TOKEN_COLON) { + expect_id_separator = 0; + continue; + + } else if (gras_ddt_parse_tok_num == GRAS_DDT_PARSE_TOKEN_LB) { + /* Handle fixed size arrays */ + gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump(); + if (gras_ddt_parse_tok_num == GRAS_DDT_PARSE_TOKEN_RB) { + PARSE_ERROR0("Unimplemented feature: GRAS_DEFINE_TYPE cannot deal with [] constructs (yet)"); + + } else if (gras_ddt_parse_tok_num == GRAS_DDT_PARSE_TOKEN_WORD) { + char *end; + long int size=strtol(gras_ddt_parse_text, &end, 10); + + if (end == gras_ddt_parse_text || *end != '\0') { + /* Not a number. Get the constant value, if any */ + int *storage=xbt_dict_get_or_null(gras_dd_constants,gras_ddt_parse_text); + if (storage) { + size = *storage; + } else { + PARSE_ERROR1("Unparsable size of array. Found '%s', expected number or known constant. Need to use gras_datadesc_set_const(), huh?", + gras_ddt_parse_text); + } + } + + /* replace the previously pushed type to an array of it */ + change_to_fixed_array(identifiers,size); + + /* eat the closing bracket */ + gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump(); + if (gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_RB) + PARSE_ERROR0("Unparsable size of array"); + DEBUG1("Fixed size array, size=%ld",size); + continue; + } else { + PARSE_ERROR0("Unparsable size of array"); + } + /* End of fixed size arrays handling */ + + } else if (gras_ddt_parse_tok_num == GRAS_DDT_PARSE_TOKEN_WORD) { + /* Handle annotation */ + s_identifier_t array; + char *keyname = NULL; + char *keyval = NULL; + memset(&array,0,sizeof(array)); + if (strcmp(gras_ddt_parse_text,"GRAS_ANNOTE")) + PARSE_ERROR1("Unparsable symbol: Expected 'GRAS_ANNOTE', got '%s'",gras_ddt_parse_text); + + gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump(); + if (gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_LP) + PARSE_ERROR1("Unparsable annotation: Expected parenthesis, got '%s'",gras_ddt_parse_text); + + while ( (gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump()) == GRAS_DDT_PARSE_TOKEN_EMPTY ); + + if (gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_WORD) + PARSE_ERROR1("Unparsable annotation: Expected key name, got '%s'",gras_ddt_parse_text); + keyname = (char*)strdup(gras_ddt_parse_text); + + while ( (gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump()) == GRAS_DDT_PARSE_TOKEN_EMPTY ); + + if (gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_COLON) + PARSE_ERROR1("Unparsable annotation: expected ',' after the key name, got '%s'",gras_ddt_parse_text); + + while ( (gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump()) == GRAS_DDT_PARSE_TOKEN_EMPTY ); + + /* get the value */ + + if (gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_WORD) + PARSE_ERROR1("Unparsable annotation: Expected key value, got '%s'",gras_ddt_parse_text); + keyval = (char*)strdup(gras_ddt_parse_text); + + while ( (gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump()) == GRAS_DDT_PARSE_TOKEN_EMPTY ); + + /* Done with parsing the annotation. Now deal with it by replacing previously pushed type with the right one */ + + DEBUG2("Anotation: %s=%s",keyname,keyval); + if (!strcmp(keyname,"size")) { + free(keyname); + if (!identifier.tm.is_ref) + PARSE_ERROR0("Size annotation for a field not being a reference"); + identifier.tm.is_ref--; + + if (!strcmp(keyval,"1")) { + change_to_ref(identifiers); + free(keyval); + } else { + char *p; + int fixed = 1; + for (p = keyval; *p != '\0'; p++) + if (! isdigit(*p) ) + fixed = 0; + if (fixed) { + change_to_fixed_array(identifiers,atoi(keyval)); + change_to_ref(identifiers); + free(keyval); + + } else { + change_to_ref_pop_array(identifiers); + xbt_dynar_push(fields_to_push,&keyval); + } + } + } else if (!strcmp(keyname,"subtype")) { + gras_datadesc_type_t subtype = gras_datadesc_by_name(keyval); + if (identifier.tm.is_matrix) { + change_to_matrix_of(identifiers,subtype); + identifier.tm.is_matrix = -1; + } else if (identifier.tm.is_dynar) { + change_to_dynar_of(identifiers,subtype); + identifier.tm.is_dynar = -1; + } else { + PARSE_ERROR1("subtype annotation only accepted for dynars and matrices, but passed to '%s'",identifier.type_name); + } + free(keyval); + } else if (!strcmp(keyname,"free_f")) { + int *storage=xbt_dict_get_or_null(gras_dd_constants,keyval); + if (!storage) + PARSE_ERROR1("value for free_f annotation of field %s is not a known constant",identifier.name); + if (identifier.tm.is_matrix == -1) { + add_free_f(identifiers,*(void_f_pvoid_t**)storage); + identifier.tm.is_matrix = 0; + } else if (identifier.tm.is_dynar == -1) { + add_free_f(identifiers,*(void_f_pvoid_t**)storage); + identifier.tm.is_dynar = 0; + } else { + PARSE_ERROR1("free_f annotation only accepted for dynars and matrices which subtype is already declared (field %s)", + identifier.name); + } + free(keyval); + } else { + free(keyval); + PARSE_ERROR1("Unknown annotation type: '%s'",keyname); + } + + /* Get all the multipliers */ + while (gras_ddt_parse_tok_num == GRAS_DDT_PARSE_TOKEN_STAR) { + + gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump(); + + if (gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_WORD) + PARSE_ERROR1("Unparsable annotation: Expected field name after '*', got '%s'",gras_ddt_parse_text); + + keyval = xbt_malloc(strlen(gras_ddt_parse_text)+2); + sprintf(keyval,"*%s",gras_ddt_parse_text); + + /* ask caller to push field as a multiplier */ + xbt_dynar_push(fields_to_push,&keyval); + + /* skip blanks after this block*/ + while ( (gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump()) + == GRAS_DDT_PARSE_TOKEN_EMPTY ); + } + + if (gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_RP) + PARSE_ERROR1("Unparsable annotation: Expected parenthesis, got '%s'", + gras_ddt_parse_text); + + continue; + + /* End of annotation handling */ + } else { + PARSE_ERROR1("Unparsable symbol: Got '%s' instead of expected comma (',')",gras_ddt_parse_text); + } + } else if(gras_ddt_parse_tok_num == GRAS_DDT_PARSE_TOKEN_COLON) { + PARSE_ERROR0("Unparsable symbol: Unexpected comma (',')"); + } + + if (gras_ddt_parse_tok_num == GRAS_DDT_PARSE_TOKEN_STAR) { + identifier.tm.is_ref++; /* We indeed deal with multiple references with multiple annotations */ + continue; + } + + /* found a symbol name. Build the type and push it to dynar */ + if(gras_ddt_parse_tok_num == GRAS_DDT_PARSE_TOKEN_WORD) { + + identifier.name=(char*)strdup(gras_ddt_parse_text); + DEBUG1("Found the identifier \"%s\"",identifier.name); + + xbt_dynar_push(identifiers, &identifier); + DEBUG1("Dynar_len=%lu",xbt_dynar_length(identifiers)); + expect_id_separator = 1; + continue; + } + + PARSE_ERROR0("Unparasable symbol (maybe a def struct in a def struct or a parser bug ;)"); + } + + if (identifier.tm.is_matrix>0) + PARSE_ERROR0("xbt_matrix_t field without 'subtype' annotation"); + if (identifier.tm.is_dynar>0) + PARSE_ERROR0("xbt_dynar_t field without 'subtype' annotation"); + + XBT_OUT; +} + +static gras_datadesc_type_t parse_struct(char *definition) { + + xbt_ex_t e; + + char buffname[32]; + static int anonymous_struct=0; + + xbt_dynar_t identifiers; + s_identifier_t field; + int i; + int done; + + xbt_dynar_t fields_to_push; + char *name; + + gras_datadesc_type_t struct_type; + + XBT_IN; + identifiers = xbt_dynar_new(sizeof(s_identifier_t),NULL); + fields_to_push = xbt_dynar_new(sizeof(char*),NULL); + + /* Create the struct descriptor */ + if (gras_ddt_parse_tok_num == GRAS_DDT_PARSE_TOKEN_WORD) { + struct_type = gras_datadesc_struct(gras_ddt_parse_text); + VERB1("Parse the struct '%s'", gras_ddt_parse_text); + gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump(); + } else { + sprintf(buffname,"anonymous struct %d",anonymous_struct++); + VERB1("Parse the anonymous struct nb %d", anonymous_struct); + struct_type = gras_datadesc_struct(buffname); + } + + if (gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_LA) + PARSE_ERROR1("Unparasable symbol: Expecting struct definition, but got %s instead of '{'", + gras_ddt_parse_text); + + /* Parse the identifiers */ + done = 0; + do { + TRY { + parse_statement(definition,identifiers,fields_to_push); + } CATCH(e) { + if (e.category != mismatch_error) + RETHROW; + xbt_ex_free(e); + done = 1; + } + + DEBUG1("This statement contained %lu identifiers",xbt_dynar_length(identifiers)); + /* append the identifiers we've found */ + xbt_dynar_foreach(identifiers,i, field) { + if (field.tm.is_ref) + PARSE_ERROR2("Not enough GRAS_ANNOTATE to deal with all dereferencing levels of %s (%d '*' left)", + field.name,field.tm.is_ref); + + VERB2("Append field '%s' to %p",field.name, (void*)struct_type); + gras_datadesc_struct_append(struct_type, field.name, field.type); + free(field.name); + free(field.type_name); + + } + xbt_dynar_reset(identifiers); + DEBUG1("struct_type=%p",(void*)struct_type); + + /* Make sure that all fields declaring a size push it into the cbps */ + xbt_dynar_foreach(fields_to_push,i, name) { + DEBUG1("struct_type=%p",(void*)struct_type); + if (name[0] == '*') { + VERB2("Push field '%s' as a multiplier into size stack of %p", + name+1, (void*)struct_type); + gras_datadesc_cb_field_push_multiplier(struct_type, name+1); + } else { + VERB2("Push field '%s' into size stack of %p", + name, (void*)struct_type); + gras_datadesc_cb_field_push(struct_type, name); + } + free(name); + } + xbt_dynar_reset(fields_to_push); + } while (!done); + gras_datadesc_struct_close(struct_type); + + /* terminates */ + if (gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_RA) + PARSE_ERROR1("Unparasable symbol: Expected '}' at the end of struct definition, got '%s'", + gras_ddt_parse_text); + + gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump(); + + xbt_dynar_free(&identifiers); + xbt_dynar_free(&fields_to_push); + XBT_OUT; + return struct_type; +} + +static gras_datadesc_type_t parse_typedef(char *definition) { + + s_type_modifier_t tm; + + gras_datadesc_type_t struct_desc=NULL; + gras_datadesc_type_t typedef_desc=NULL; + + XBT_IN; + memset(&tm,0,sizeof(tm)); + + /* get the aliased type */ + parse_type_modifier(&tm); + + if (tm.is_struct) { + struct_desc = parse_struct(definition); + } + + parse_type_modifier(&tm); + + if (tm.is_ref) + PARSE_ERROR0("GRAS_DEFINE_TYPE cannot handle reference without annotation"); + + /* get the aliasing name */ + if (gras_ddt_parse_tok_num != GRAS_DDT_PARSE_TOKEN_WORD) + PARSE_ERROR1("Unparsable typedef: Expected the alias name, and got '%s'", + gras_ddt_parse_text); + + /* (FIXME: should) build the alias */ + PARSE_ERROR0("Unimplemented feature: GRAS_DEFINE_TYPE cannot handle typedef yet"); + + XBT_OUT; + return typedef_desc; +} + + +/** + * gras_datadesc_parse: + * + * Create a datadescription from the result of parsing the C type description + */ +gras_datadesc_type_t +gras_datadesc_parse(const char *name, + const char *C_statement) { + + gras_datadesc_type_t res=NULL; + char *definition; + int semicolon_count=0; + int def_count,C_count; + + XBT_IN; + /* reput the \n in place for debug */ + for (C_count=0; C_statement[C_count] != '\0'; C_count++) + if (C_statement[C_count] == ';' || C_statement[C_count] == '{') + semicolon_count++; + definition = (char*)xbt_malloc(C_count + semicolon_count + 1); + for (C_count=0,def_count=0; C_statement[C_count] != '\0'; C_count++) { + definition[def_count++] = C_statement[C_count]; + if (C_statement[C_count] == ';' || C_statement[C_count] == '{') { + definition[def_count++] = '\n'; + } + } + definition[def_count] = '\0'; + + /* init */ + VERB2("_gras_ddt_type_parse(%s) -> %d chars",definition, def_count); + gras_ddt_parse_pointer_string_init(definition); + + /* Do I have a typedef, or a raw struct ?*/ + gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump(); + + if ((gras_ddt_parse_tok_num == GRAS_DDT_PARSE_TOKEN_WORD) && (!strcmp(gras_ddt_parse_text,"struct"))) { + gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump(); + res = parse_struct(definition); + + } else if ((gras_ddt_parse_tok_num == GRAS_DDT_PARSE_TOKEN_WORD) && (!strcmp(gras_ddt_parse_text,"typedef"))) { + gras_ddt_parse_tok_num = gras_ddt_parse_lex_n_dump(); + res = parse_typedef(definition); + + } else { + ERROR1("Failed to parse the following symbol (not a struct neither a typedef) :\n%s",definition); + xbt_abort(); + } + + gras_ddt_parse_pointer_string_close(); + VERB0("end of _gras_ddt_type_parse()"); + free(definition); + /* register it under the name provided as symbol */ + if (strcmp(res->name,name)) { + ERROR2("In GRAS_DEFINE_TYPE, the provided symbol (here %s) must be the C type name (here %s)", + name,res->name); + xbt_abort(); + } + gras_ddt_parse_lex_destroy(); + XBT_OUT; + return res; +} + +xbt_dict_t gras_dd_constants; +/** \brief Declare a constant to the parsing mecanism. See the "\#define and fixed size array" section */ +void gras_datadesc_set_const(const char*name, int value) { + int *stored = xbt_new(int, 1); + *stored=value; + + xbt_dict_set(gras_dd_constants,name, stored, xbt_free_f); +} diff --git a/src/gras_simix/DataDesc/gras_simix_ddt_parse.yy.c b/src/gras_simix/DataDesc/gras_simix_ddt_parse.yy.c new file mode 100644 index 0000000000..3c9ea34c44 --- /dev/null +++ b/src/gras_simix/DataDesc/gras_simix_ddt_parse.yy.c @@ -0,0 +1,1985 @@ + +#line 3 "gras/DataDesc/ddt_parse.yy.c" + +#define YY_INT_ALIGNED short int + +/* A lexical scanner generated by flex */ + +#define FLEX_SCANNER +#define YY_FLEX_MAJOR_VERSION 2 +#define YY_FLEX_MINOR_VERSION 5 +#define YY_FLEX_SUBMINOR_VERSION 31 +#if YY_FLEX_SUBMINOR_VERSION > 0 +#define FLEX_BETA +#endif + +/* First, we deal with platform-specific or compiler-specific issues. */ + +/* begin standard C headers. */ +#include +#include +#include +#include + +/* end standard C headers. */ + +/* flex integer type definitions */ + +#ifndef FLEXINT_H +#define FLEXINT_H + +/* C99 systems have . Non-C99 systems may or may not. */ + +#if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L +#include +typedef int8_t flex_int8_t; +typedef uint8_t flex_uint8_t; +typedef int16_t flex_int16_t; +typedef uint16_t flex_uint16_t; +typedef int32_t flex_int32_t; +typedef uint32_t flex_uint32_t; +#else +typedef signed char flex_int8_t; +typedef short int flex_int16_t; +typedef int flex_int32_t; +typedef unsigned char flex_uint8_t; +typedef unsigned short int flex_uint16_t; +typedef unsigned int flex_uint32_t; +#endif /* ! C99 */ + +/* Limits of integral types. */ +#ifndef INT8_MIN +#define INT8_MIN (-128) +#endif +#ifndef INT16_MIN +#define INT16_MIN (-32767-1) +#endif +#ifndef INT32_MIN +#define INT32_MIN (-2147483647-1) +#endif +#ifndef INT8_MAX +#define INT8_MAX (127) +#endif +#ifndef INT16_MAX +#define INT16_MAX (32767) +#endif +#ifndef INT32_MAX +#define INT32_MAX (2147483647) +#endif +#ifndef UINT8_MAX +#define UINT8_MAX (255U) +#endif +#ifndef UINT16_MAX +#define UINT16_MAX (65535U) +#endif +#ifndef UINT32_MAX +#define UINT32_MAX (4294967295U) +#endif + +#endif /* ! FLEXINT_H */ + +#ifdef __cplusplus + +/* The "const" storage-class-modifier is valid. */ +#define YY_USE_CONST + +#else /* ! __cplusplus */ + +#if __STDC__ + +#define YY_USE_CONST + +#endif /* __STDC__ */ +#endif /* ! __cplusplus */ + +#ifdef YY_USE_CONST +#define yyconst const +#else +#define yyconst +#endif + +/* Returned upon end-of-file. */ +#define YY_NULL 0 + +/* Promotes a possibly negative, possibly signed char to an unsigned + * integer for use as an array index. If the signed char is negative, + * we want to instead treat it as an 8-bit unsigned char, hence the + * double cast. + */ +#define YY_SC_TO_UI(c) ((unsigned int) (unsigned char) c) + +/* Enter a start condition. This macro really ought to take a parameter, + * but we do it the disgusting crufty way forced on us by the ()-less + * definition of BEGIN. + */ +#define BEGIN (yy_start) = 1 + 2 * + +/* Translate the current start state into a value that can be later handed + * to BEGIN to return to the state. The YYSTATE alias is for lex + * compatibility. + */ +#define YY_START (((yy_start) - 1) / 2) +#define YYSTATE YY_START + +/* Action number for EOF rule of a given start state. */ +#define YY_STATE_EOF(state) (YY_END_OF_BUFFER + state + 1) + +/* Special action meaning "start processing a new file". */ +#define YY_NEW_FILE gras_ddt_parse_restart(gras_ddt_parse_in ) + +#define YY_END_OF_BUFFER_CHAR 0 + +/* Size of default input buffer. */ +#ifndef YY_BUF_SIZE +#define YY_BUF_SIZE 16384 +#endif + +#ifndef YY_TYPEDEF_YY_BUFFER_STATE +#define YY_TYPEDEF_YY_BUFFER_STATE +typedef struct yy_buffer_state *YY_BUFFER_STATE; +#endif + +extern int gras_ddt_parse_leng; + +extern FILE *gras_ddt_parse_in, *gras_ddt_parse_out; + +#define EOB_ACT_CONTINUE_SCAN 0 +#define EOB_ACT_END_OF_FILE 1 +#define EOB_ACT_LAST_MATCH 2 + + #define YY_LESS_LINENO(n) + +/* Return all but the first "n" matched characters back to the input stream. */ +#define yyless(n) \ + do \ + { \ + /* Undo effects of setting up gras_ddt_parse_text. */ \ + int yyless_macro_arg = (n); \ + YY_LESS_LINENO(yyless_macro_arg);\ + *yy_cp = (yy_hold_char); \ + YY_RESTORE_YY_MORE_OFFSET \ + (yy_c_buf_p) = yy_cp = yy_bp + yyless_macro_arg - YY_MORE_ADJ; \ + YY_DO_BEFORE_ACTION; /* set up gras_ddt_parse_text again */ \ + } \ + while ( 0 ) + +#define unput(c) yyunput( c, (yytext_ptr) ) + +/* The following is because we cannot portably get our hands on size_t + * (without autoconf's help, which isn't available because we want + * flex-generated scanners to compile on their own). + */ + +#ifndef YY_TYPEDEF_YY_SIZE_T +#define YY_TYPEDEF_YY_SIZE_T +typedef unsigned int yy_size_t; +#endif + +#ifndef YY_STRUCT_YY_BUFFER_STATE +#define YY_STRUCT_YY_BUFFER_STATE +struct yy_buffer_state + { + FILE *yy_input_file; + + char *yy_ch_buf; /* input buffer */ + char *yy_buf_pos; /* current position in input buffer */ + + /* Size of input buffer in bytes, not including room for EOB + * characters. + */ + yy_size_t yy_buf_size; + + /* Number of characters read into yy_ch_buf, not including EOB + * characters. + */ + int yy_n_chars; + + /* Whether we "own" the buffer - i.e., we know we created it, + * and can realloc() it to grow it, and should free() it to + * delete it. + */ + int yy_is_our_buffer; + + /* Whether this is an "interactive" input source; if so, and + * if we're using stdio for input, then we want to use getc() + * instead of fread(), to make sure we stop fetching input after + * each newline. + */ + int yy_is_interactive; + + /* Whether we're considered to be at the beginning of a line. + * If so, '^' rules will be active on the next match, otherwise + * not. + */ + int yy_at_bol; + + int yy_bs_lineno; /**< The line count. */ + int yy_bs_column; /**< The column count. */ + + /* Whether to try to fill the input buffer when we reach the + * end of it. + */ + int yy_fill_buffer; + + int yy_buffer_status; + +#define YY_BUFFER_NEW 0 +#define YY_BUFFER_NORMAL 1 + /* When an EOF's been seen but there's still some text to process + * then we mark the buffer as YY_EOF_PENDING, to indicate that we + * shouldn't try reading from the input source any more. We might + * still have a bunch of tokens to match, though, because of + * possible backing-up. + * + * When we actually see the EOF, we change the status to "new" + * (via gras_ddt_parse_restart()), so that the user can continue scanning by + * just pointing gras_ddt_parse_in at a new input file. + */ +#define YY_BUFFER_EOF_PENDING 2 + + }; +#endif /* !YY_STRUCT_YY_BUFFER_STATE */ + +/* Stack of input buffers. */ +static size_t yy_buffer_stack_top = 0; /**< index of top of stack. */ +static size_t yy_buffer_stack_max = 0; /**< capacity of stack. */ +static YY_BUFFER_STATE * yy_buffer_stack = 0; /**< Stack as an array. */ + +/* We provide macros for accessing buffer states in case in the + * future we want to put the buffer states in a more general + * "scanner state". + * + * Returns the top of the stack, or NULL. + */ +#define YY_CURRENT_BUFFER ( (yy_buffer_stack) \ + ? (yy_buffer_stack)[(yy_buffer_stack_top)] \ + : NULL) + +/* Same as previous macro, but useful when we know that the buffer stack is not + * NULL or when we need an lvalue. For internal use only. + */ +#define YY_CURRENT_BUFFER_LVALUE (yy_buffer_stack)[(yy_buffer_stack_top)] + +/* yy_hold_char holds the character lost when gras_ddt_parse_text is formed. */ +static char yy_hold_char; +static int yy_n_chars; /* number of characters read into yy_ch_buf */ +int gras_ddt_parse_leng; + +/* Points to current character in buffer. */ +static char *yy_c_buf_p = (char *) 0; +static int yy_init = 1; /* whether we need to initialize */ +static int yy_start = 0; /* start state number */ + +/* Flag which is used to allow gras_ddt_parse_wrap()'s to do buffer switches + * instead of setting up a fresh gras_ddt_parse_in. A bit of a hack ... + */ +static int yy_did_buffer_switch_on_eof; + +void gras_ddt_parse_restart (FILE *input_file ); +void gras_ddt_parse__switch_to_buffer (YY_BUFFER_STATE new_buffer ); +YY_BUFFER_STATE gras_ddt_parse__create_buffer (FILE *file,int size ); +void gras_ddt_parse__delete_buffer (YY_BUFFER_STATE b ); +void gras_ddt_parse__flush_buffer (YY_BUFFER_STATE b ); +void gras_ddt_parse_push_buffer_state (YY_BUFFER_STATE new_buffer ); +void gras_ddt_parse_pop_buffer_state (void ); + +static void gras_ddt_parse_ensure_buffer_stack (void ); +static void gras_ddt_parse__load_buffer_state (void ); +static void gras_ddt_parse__init_buffer (YY_BUFFER_STATE b,FILE *file ); + +#define YY_FLUSH_BUFFER gras_ddt_parse__flush_buffer(YY_CURRENT_BUFFER ) + +YY_BUFFER_STATE gras_ddt_parse__scan_buffer (char *base,yy_size_t size ); +YY_BUFFER_STATE gras_ddt_parse__scan_string (yyconst char *yy_str ); +YY_BUFFER_STATE gras_ddt_parse__scan_bytes (yyconst char *bytes,int len ); + +void *gras_ddt_parse_alloc (yy_size_t ); +void *gras_ddt_parse_realloc (void *,yy_size_t ); +void gras_ddt_parse_free (void * ); + +#define yy_new_buffer gras_ddt_parse__create_buffer + +#define yy_set_interactive(is_interactive) \ + { \ + if ( ! YY_CURRENT_BUFFER ){ \ + gras_ddt_parse_ensure_buffer_stack (); \ + YY_CURRENT_BUFFER_LVALUE = \ + gras_ddt_parse__create_buffer(gras_ddt_parse_in,YY_BUF_SIZE ); \ + } \ + YY_CURRENT_BUFFER_LVALUE->yy_is_interactive = is_interactive; \ + } + +#define yy_set_bol(at_bol) \ + { \ + if ( ! YY_CURRENT_BUFFER ){\ + gras_ddt_parse_ensure_buffer_stack (); \ + YY_CURRENT_BUFFER_LVALUE = \ + gras_ddt_parse__create_buffer(gras_ddt_parse_in,YY_BUF_SIZE ); \ + } \ + YY_CURRENT_BUFFER_LVALUE->yy_at_bol = at_bol; \ + } + +#define YY_AT_BOL() (YY_CURRENT_BUFFER_LVALUE->yy_at_bol) + +/* Begin user sect3 */ + +#define gras_ddt_parse_wrap() 1 +#define YY_SKIP_YYWRAP + +typedef unsigned char YY_CHAR; + +FILE *gras_ddt_parse_in = (FILE *) 0, *gras_ddt_parse_out = (FILE *) 0; + +typedef int yy_state_type; + +extern int gras_ddt_parse_lineno; + +int gras_ddt_parse_lineno = 1; + +extern char *gras_ddt_parse_text; +#define yytext_ptr gras_ddt_parse_text + +static yy_state_type yy_get_previous_state (void ); +static yy_state_type yy_try_NUL_trans (yy_state_type current_state ); +static int yy_get_next_buffer (void ); +static void yy_fatal_error (yyconst char msg[] ); + +/* Done after the current pattern has been matched and before the + * corresponding action - sets up gras_ddt_parse_text. + */ +#define YY_DO_BEFORE_ACTION \ + (yytext_ptr) = yy_bp; \ + gras_ddt_parse_leng = (size_t) (yy_cp - yy_bp); \ + (yy_hold_char) = *yy_cp; \ + *yy_cp = '\0'; \ + (yy_c_buf_p) = yy_cp; + +#define YY_NUM_RULES 26 +#define YY_END_OF_BUFFER 27 +/* This struct is not used in this scanner, + but its presence is necessary. */ +struct yy_trans_info + { + flex_int32_t yy_verify; + flex_int32_t yy_nxt; + }; +static yyconst flex_int16_t yy_accept[61] = + { 0, + 14, 14, 7, 7, 10, 10, 0, 0, 27, 25, + 24, 19, 20, 21, 23, 14, 25, 22, 17, 18, + 15, 16, 7, 7, 6, 7, 7, 10, 12, 11, + 26, 26, 14, 0, 1, 7, 7, 7, 5, 7, + 10, 11, 11, 13, 0, 0, 2, 1, 4, 0, + 3, 0, 2, 0, 3, 0, 0, 8, 9, 0 + } ; + +static yyconst flex_int32_t yy_ec[256] = + { 0, + 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 2, 1, 1, 1, 1, 1, 1, 1, 4, + 5, 6, 1, 7, 8, 8, 9, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 1, 11, 1, + 1, 1, 1, 1, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 12, 1, 13, 14, 8, 1, 8, 8, 8, 8, + + 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 16, 1, 17, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1 + } ; + +static yyconst flex_int32_t yy_meta[18] = + { 0, + 1, 1, 2, 1, 1, 3, 1, 4, 1, 4, + 1, 1, 1, 1, 4, 1, 1 + } ; + +static yyconst flex_int16_t yy_base[69] = + { 0, + 0, 0, 16, 30, 17, 18, 78, 77, 85, 88, + 88, 88, 88, 88, 88, 0, 19, 88, 88, 88, + 88, 88, 0, 24, 88, 75, 77, 0, 88, 21, + 88, 76, 0, 22, 0, 0, 27, 75, 0, 71, + 0, 29, 34, 88, 29, 65, 76, 0, 0, 63, + 74, 60, 72, 56, 65, 51, 50, 88, 88, 88, + 45, 49, 53, 55, 57, 61, 65, 69 + } ; + +static yyconst flex_int16_t yy_def[69] = + { 0, + 60, 1, 61, 61, 62, 62, 63, 63, 60, 60, + 60, 60, 60, 60, 60, 64, 60, 60, 60, 60, + 60, 60, 65, 65, 60, 65, 65, 66, 60, 67, + 60, 60, 64, 60, 68, 65, 65, 65, 65, 65, + 66, 67, 67, 60, 60, 60, 60, 68, 65, 60, + 60, 60, 60, 60, 60, 60, 60, 60, 60, 0, + 60, 60, 60, 60, 60, 60, 60, 60 + } ; + +static yyconst flex_int16_t yy_nxt[106] = + { 0, + 10, 10, 11, 12, 13, 14, 15, 16, 17, 16, + 18, 19, 20, 10, 16, 21, 22, 24, 25, 29, + 29, 26, 30, 30, 34, 37, 43, 35, 37, 44, + 27, 24, 25, 46, 60, 26, 47, 60, 38, 43, + 50, 38, 44, 51, 27, 23, 23, 23, 23, 28, + 28, 28, 28, 31, 31, 31, 31, 36, 33, 36, + 36, 41, 59, 58, 41, 42, 55, 42, 42, 48, + 57, 48, 48, 53, 56, 55, 54, 53, 52, 49, + 40, 45, 40, 39, 60, 32, 32, 9, 60, 60, + 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, + + 60, 60, 60, 60, 60 + } ; + +static yyconst flex_int16_t yy_chk[106] = + { 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 3, 3, 5, + 6, 3, 5, 6, 17, 24, 30, 17, 37, 30, + 3, 4, 4, 34, 42, 4, 34, 42, 24, 43, + 45, 37, 43, 45, 4, 61, 61, 61, 61, 62, + 62, 62, 62, 63, 63, 63, 63, 65, 64, 65, + 65, 66, 57, 56, 66, 67, 55, 67, 67, 68, + 54, 68, 68, 53, 52, 51, 50, 47, 46, 40, + 38, 32, 27, 26, 9, 8, 7, 60, 60, 60, + 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, + + 60, 60, 60, 60, 60 + } ; + +static yy_state_type yy_last_accepting_state; +static char *yy_last_accepting_cpos; + +extern int gras_ddt_parse__flex_debug; +int gras_ddt_parse__flex_debug = 0; + +/* The intent behind this definition is that it'll catch + * any uses of REJECT which flex missed. + */ +#define REJECT reject_used_but_not_detected +#define yymore() yymore_used_but_not_detected +#define YY_MORE_ADJ 0 +#define YY_RESTORE_YY_MORE_OFFSET +char *gras_ddt_parse_text; +/* $Id$ */ +/* DataDesc/ddt_parse -- automatic parsing of data structures */ +/* Copyright (c) 2004 Arnaud Legrand, Martin Quinson. All rights reserved. */ +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ +#include "gras/DataDesc/datadesc_private.h" +#include "gras/DataDesc/ddt_parse.yy.h" +#include + YY_BUFFER_STATE gras_ddt_input_buffer; + FILE *gras_ddt_file_to_parse; + + int gras_ddt_parse_line_pos = 1; + int gras_ddt_parse_col_pos = 0; + int gras_ddt_parse_char_pos = 0; + int gras_ddt_parse_tok_num = 0; + const char *definition; + XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_ddt_lexer,gras_ddt_parse,"The crude internals of the lexer used for type parsing"); +#define SHOW_WHERE DEBUG4("%d:%d (char #%d): seen '%s'", gras_ddt_parse_line_pos,gras_ddt_parse_col_pos,gras_ddt_parse_char_pos,gras_ddt_parse_text) + +#define INITIAL 0 +#define annotate 1 +#define comment 2 +#define foo 3 + +#ifndef YY_NO_UNISTD_H +/* Special case for "unistd.h", since it is non-ANSI. We include it way + * down here because we want the user's section 1 to have been scanned first. + * The user has a chance to override it with an option. + */ +#include +#endif + +#ifndef YY_EXTRA_TYPE +#define YY_EXTRA_TYPE void * +#endif + +/* Macros after this point can all be overridden by user definitions in + * section 1. + */ + +#ifndef YY_SKIP_YYWRAP +#ifdef __cplusplus +extern "C" int gras_ddt_parse_wrap (void ); +#else +extern int gras_ddt_parse_wrap (void ); +#endif +#endif + + static void yyunput (int c,char *buf_ptr ); + +#ifndef yytext_ptr +static void yy_flex_strncpy (char *,yyconst char *,int ); +#endif + +#ifdef YY_NEED_STRLEN +static int yy_flex_strlen (yyconst char * ); +#endif + +#ifndef YY_NO_INPUT + +#ifdef __cplusplus +static int yyinput (void ); +#else +static int input (void ); +#endif + +#endif + +/* Amount of stuff to slurp up with each read. */ +#ifndef YY_READ_BUF_SIZE +#define YY_READ_BUF_SIZE 8192 +#endif + +/* Copy whatever the last rule matched to the standard output. */ +#ifndef ECHO +/* This used to be an fputs(), but since the string might contain NUL's, + * we now use fwrite(). + */ +#define ECHO (void) fwrite( gras_ddt_parse_text, gras_ddt_parse_leng, 1, gras_ddt_parse_out ) +#endif + +/* Gets input and stuffs it into "buf". number of characters read, or YY_NULL, + * is returned in "result". + */ +#ifndef YY_INPUT +#define YY_INPUT(buf,result,max_size) \ + if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \ + { \ + int c = '*'; \ + size_t n; \ + for ( n = 0; n < max_size && \ + (c = getc( gras_ddt_parse_in )) != EOF && c != '\n'; ++n ) \ + buf[n] = (char) c; \ + if ( c == '\n' ) \ + buf[n++] = (char) c; \ + if ( c == EOF && ferror( gras_ddt_parse_in ) ) \ + YY_FATAL_ERROR( "input in flex scanner failed" ); \ + result = n; \ + } \ + else \ + { \ + errno=0; \ + while ( (result = fread(buf, 1, max_size, gras_ddt_parse_in))==0 && ferror(gras_ddt_parse_in)) \ + { \ + if( errno != EINTR) \ + { \ + YY_FATAL_ERROR( "input in flex scanner failed" ); \ + break; \ + } \ + errno=0; \ + clearerr(gras_ddt_parse_in); \ + } \ + }\ +\ + +#endif + +/* No semi-colon after return; correct usage is to write "yyterminate();" - + * we don't want an extra ';' after the "return" because that will cause + * some compilers to complain about unreachable statements. + */ +#ifndef yyterminate +#define yyterminate() return YY_NULL +#endif + +/* Number of entries by which start-condition stack grows. */ +#ifndef YY_START_STACK_INCR +#define YY_START_STACK_INCR 25 +#endif + +/* Report a fatal error. */ +#ifndef YY_FATAL_ERROR +#define YY_FATAL_ERROR(msg) yy_fatal_error( msg ) +#endif + +/* end tables serialization structures and prototypes */ + +/* Default declaration of generated scanner - a define so the user can + * easily add parameters. + */ +#ifndef YY_DECL +#define YY_DECL_IS_OURS 1 + +extern int gras_ddt_parse_lex (void); + +#define YY_DECL int gras_ddt_parse_lex (void) +#endif /* !YY_DECL */ + +/* Code executed at the beginning of each rule, after gras_ddt_parse_text and gras_ddt_parse_leng + * have been set up. + */ +#ifndef YY_USER_ACTION +#define YY_USER_ACTION +#endif + +/* Code executed at the end of each rule. */ +#ifndef YY_BREAK +#define YY_BREAK break; +#endif + +#define YY_RULE_SETUP \ + YY_USER_ACTION + +/** The main scanner function which does all the work. + */ +YY_DECL +{ + register yy_state_type yy_current_state; + register char *yy_cp, *yy_bp; + register int yy_act; + + int comment_caller=0; + int annotate_caller=0; + + if ( (yy_init) ) + { + (yy_init) = 0; + +#ifdef YY_USER_INIT + YY_USER_INIT; +#endif + + if ( ! (yy_start) ) + (yy_start) = 1; /* first start state */ + + if ( ! gras_ddt_parse_in ) + gras_ddt_parse_in = stdin; + + if ( ! gras_ddt_parse_out ) + gras_ddt_parse_out = stdout; + + if ( ! YY_CURRENT_BUFFER ) { + gras_ddt_parse_ensure_buffer_stack (); + YY_CURRENT_BUFFER_LVALUE = + gras_ddt_parse__create_buffer(gras_ddt_parse_in,YY_BUF_SIZE ); + } + + gras_ddt_parse__load_buffer_state( ); + } + + while ( 1 ) /* loops until end-of-file is reached */ + { + yy_cp = (yy_c_buf_p); + + /* Support of gras_ddt_parse_text. */ + *yy_cp = (yy_hold_char); + + /* yy_bp points to the position in yy_ch_buf of the start of + * the current run. + */ + yy_bp = yy_cp; + + yy_current_state = (yy_start); +yy_match: + do + { + register YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)]; + if ( yy_accept[yy_current_state] ) + { + (yy_last_accepting_state) = yy_current_state; + (yy_last_accepting_cpos) = yy_cp; + } + while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) + { + yy_current_state = (int) yy_def[yy_current_state]; + if ( yy_current_state >= 61 ) + yy_c = yy_meta[(unsigned int) yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; + ++yy_cp; + } + while ( yy_base[yy_current_state] != 88 ); + +yy_find_action: + yy_act = yy_accept[yy_current_state]; + if ( yy_act == 0 ) + { /* have to back up */ + yy_cp = (yy_last_accepting_cpos); + yy_current_state = (yy_last_accepting_state); + yy_act = yy_accept[yy_current_state]; + } + + YY_DO_BEFORE_ACTION; + +do_action: /* This label is used only to access EOF actions. */ + + switch ( yy_act ) + { /* beginning of action switch */ + case 0: /* must back up */ + /* undo the effects of YY_DO_BEFORE_ACTION */ + *yy_cp = (yy_hold_char); + yy_cp = (yy_last_accepting_cpos); + yy_current_state = (yy_last_accepting_state); + goto yy_find_action; + +case 1: +YY_RULE_SETUP + + YY_BREAK +case 2: +YY_RULE_SETUP +{ /****************** ANNOTATION ************************/ + DEBUG0("Begin annotation"); + annotate_caller = INITIAL; + gras_ddt_parse_char_pos+= strlen(gras_ddt_parse_text); + gras_ddt_parse_col_pos+= strlen(gras_ddt_parse_text); + BEGIN(annotate); +} + YY_BREAK +case 3: +YY_RULE_SETUP +{ /* trim annotation */ + DEBUG0("Begin annotation"); + annotate_caller = foo; + gras_ddt_parse_char_pos+= strlen(gras_ddt_parse_text); + gras_ddt_parse_col_pos+= strlen(gras_ddt_parse_text); + BEGIN(annotate); +} + YY_BREAK +case 4: +YY_RULE_SETUP +{ + DEBUG0("End annotation"); + gras_ddt_parse_char_pos+= strlen(gras_ddt_parse_text); + gras_ddt_parse_col_pos+= strlen(gras_ddt_parse_text); + BEGIN(annotate_caller); +} + YY_BREAK +case 5: +YY_RULE_SETUP +{ + PARSE_ERROR0("``/*g'' construct closed by a regular ``*/''"); +} + YY_BREAK +case 6: +/* rule 6 can match eol */ +YY_RULE_SETUP +{ + PARSE_ERROR0("Type annotation cannot spread over several lines"); +} + YY_BREAK +case 7: +YY_RULE_SETUP +{ /* eat the rest */ + gras_ddt_parse_char_pos+= strlen(gras_ddt_parse_text); + gras_ddt_parse_col_pos+= strlen(gras_ddt_parse_text); + return GRAS_DDT_PARSE_TOKEN_ANNOTATE; +} + YY_BREAK +case 8: +YY_RULE_SETUP +{ /****************** COMMENTS ************************/ + /* constructs like : */ + /*g [string] g*/ + /* are not comments but size annotations */ + comment_caller = INITIAL; + BEGIN(comment); +} + YY_BREAK +case 9: +YY_RULE_SETUP +{ + comment_caller = foo; + BEGIN(comment); +} + YY_BREAK +case 10: +YY_RULE_SETUP +{ /* eat anything that's not a '*' */ +} + YY_BREAK +case 11: +YY_RULE_SETUP +{ /* eat up '*'s not followed by '/'s */ +} + YY_BREAK +case 12: +/* rule 12 can match eol */ +YY_RULE_SETUP +{ + ++gras_ddt_parse_line_pos; + gras_ddt_parse_col_pos=0; + gras_ddt_parse_char_pos++; +} + YY_BREAK +case 13: +YY_RULE_SETUP +{ + gras_ddt_parse_char_pos+= strlen(gras_ddt_parse_text); + gras_ddt_parse_col_pos+= strlen(gras_ddt_parse_text); + BEGIN(comment_caller); +} + YY_BREAK +case 14: +YY_RULE_SETUP +{ /****************** STATEMENTS ************************/ + gras_ddt_parse_char_pos += strlen(gras_ddt_parse_text); + gras_ddt_parse_col_pos += strlen(gras_ddt_parse_text); + SHOW_WHERE; + return(GRAS_DDT_PARSE_TOKEN_WORD); +} + YY_BREAK +case 15: +YY_RULE_SETUP +{ + gras_ddt_parse_char_pos++; + gras_ddt_parse_col_pos++; + SHOW_WHERE; + return(GRAS_DDT_PARSE_TOKEN_LA); +} + YY_BREAK +case 16: +YY_RULE_SETUP +{ + gras_ddt_parse_char_pos++; + gras_ddt_parse_col_pos++; + SHOW_WHERE; + return(GRAS_DDT_PARSE_TOKEN_RA); +} + YY_BREAK +case 17: +YY_RULE_SETUP +{ + gras_ddt_parse_char_pos++; + gras_ddt_parse_col_pos++; + SHOW_WHERE; + return(GRAS_DDT_PARSE_TOKEN_LB); +} + YY_BREAK +case 18: +YY_RULE_SETUP +{ + gras_ddt_parse_char_pos++; + gras_ddt_parse_col_pos++; + SHOW_WHERE; + return(GRAS_DDT_PARSE_TOKEN_RB); +} + YY_BREAK +case 19: +YY_RULE_SETUP +{ + gras_ddt_parse_char_pos++; + gras_ddt_parse_col_pos++; + SHOW_WHERE; + return(GRAS_DDT_PARSE_TOKEN_LP); +} + YY_BREAK +case 20: +YY_RULE_SETUP +{ + gras_ddt_parse_char_pos++; + gras_ddt_parse_col_pos++; + SHOW_WHERE; + return(GRAS_DDT_PARSE_TOKEN_RP); +} + YY_BREAK +case 21: +YY_RULE_SETUP +{ + gras_ddt_parse_char_pos++; + gras_ddt_parse_col_pos++; + SHOW_WHERE; + return(GRAS_DDT_PARSE_TOKEN_STAR); +} + YY_BREAK +case 22: +YY_RULE_SETUP +{ + gras_ddt_parse_char_pos++; + gras_ddt_parse_col_pos++; + SHOW_WHERE; + return(GRAS_DDT_PARSE_TOKEN_SEMI_COLON); +} + YY_BREAK +case 23: +YY_RULE_SETUP +{ + gras_ddt_parse_char_pos++; + gras_ddt_parse_col_pos++; + SHOW_WHERE; + return(GRAS_DDT_PARSE_TOKEN_COLON); +} + YY_BREAK +case 24: +/* rule 24 can match eol */ +YY_RULE_SETUP +{ + gras_ddt_parse_line_pos++; + gras_ddt_parse_char_pos++; + gras_ddt_parse_col_pos=0; + SHOW_WHERE; +} + YY_BREAK +case 25: +YY_RULE_SETUP +{ + gras_ddt_parse_char_pos++; + gras_ddt_parse_col_pos++; + SHOW_WHERE; +} + YY_BREAK +case 26: +YY_RULE_SETUP +ECHO; + YY_BREAK +case YY_STATE_EOF(INITIAL): +case YY_STATE_EOF(annotate): +case YY_STATE_EOF(comment): +case YY_STATE_EOF(foo): + yyterminate(); + + case YY_END_OF_BUFFER: + { + /* Amount of text matched not including the EOB char. */ + int yy_amount_of_matched_text = (int) (yy_cp - (yytext_ptr)) - 1; + + /* Undo the effects of YY_DO_BEFORE_ACTION. */ + *yy_cp = (yy_hold_char); + YY_RESTORE_YY_MORE_OFFSET + + if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_NEW ) + { + /* We're scanning a new file or input source. It's + * possible that this happened because the user + * just pointed gras_ddt_parse_in at a new source and called + * gras_ddt_parse_lex(). If so, then we have to assure + * consistency between YY_CURRENT_BUFFER and our + * globals. Here is the right place to do so, because + * this is the first action (other than possibly a + * back-up) that will match for the new input source. + */ + (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; + YY_CURRENT_BUFFER_LVALUE->yy_input_file = gras_ddt_parse_in; + YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = YY_BUFFER_NORMAL; + } + + /* Note that here we test for yy_c_buf_p "<=" to the position + * of the first EOB in the buffer, since yy_c_buf_p will + * already have been incremented past the NUL character + * (since all states make transitions on EOB to the + * end-of-buffer state). Contrast this with the test + * in input(). + */ + if ( (yy_c_buf_p) <= &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] ) + { /* This was really a NUL. */ + yy_state_type yy_next_state; + + (yy_c_buf_p) = (yytext_ptr) + yy_amount_of_matched_text; + + yy_current_state = yy_get_previous_state( ); + + /* Okay, we're now positioned to make the NUL + * transition. We couldn't have + * yy_get_previous_state() go ahead and do it + * for us because it doesn't know how to deal + * with the possibility of jamming (and we don't + * want to build jamming into it because then it + * will run more slowly). + */ + + yy_next_state = yy_try_NUL_trans( yy_current_state ); + + yy_bp = (yytext_ptr) + YY_MORE_ADJ; + + if ( yy_next_state ) + { + /* Consume the NUL. */ + yy_cp = ++(yy_c_buf_p); + yy_current_state = yy_next_state; + goto yy_match; + } + + else + { + yy_cp = (yy_c_buf_p); + goto yy_find_action; + } + } + + else switch ( yy_get_next_buffer( ) ) + { + case EOB_ACT_END_OF_FILE: + { + (yy_did_buffer_switch_on_eof) = 0; + + if ( gras_ddt_parse_wrap( ) ) + { + /* Note: because we've taken care in + * yy_get_next_buffer() to have set up + * gras_ddt_parse_text, we can now set up + * yy_c_buf_p so that if some total + * hoser (like flex itself) wants to + * call the scanner after we return the + * YY_NULL, it'll still work - another + * YY_NULL will get returned. + */ + (yy_c_buf_p) = (yytext_ptr) + YY_MORE_ADJ; + + yy_act = YY_STATE_EOF(YY_START); + goto do_action; + } + + else + { + if ( ! (yy_did_buffer_switch_on_eof) ) + YY_NEW_FILE; + } + break; + } + + case EOB_ACT_CONTINUE_SCAN: + (yy_c_buf_p) = + (yytext_ptr) + yy_amount_of_matched_text; + + yy_current_state = yy_get_previous_state( ); + + yy_cp = (yy_c_buf_p); + yy_bp = (yytext_ptr) + YY_MORE_ADJ; + goto yy_match; + + case EOB_ACT_LAST_MATCH: + (yy_c_buf_p) = + &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)]; + + yy_current_state = yy_get_previous_state( ); + + yy_cp = (yy_c_buf_p); + yy_bp = (yytext_ptr) + YY_MORE_ADJ; + goto yy_find_action; + } + break; + } + + default: + YY_FATAL_ERROR( + "fatal flex scanner internal error--no action found" ); + } /* end of action switch */ + } /* end of scanning one token */ +} /* end of gras_ddt_parse_lex */ + +/* yy_get_next_buffer - try to read in a new buffer + * + * Returns a code representing an action: + * EOB_ACT_LAST_MATCH - + * EOB_ACT_CONTINUE_SCAN - continue scanning from current position + * EOB_ACT_END_OF_FILE - end of file + */ +static int yy_get_next_buffer (void) +{ + register char *dest = YY_CURRENT_BUFFER_LVALUE->yy_ch_buf; + register char *source = (yytext_ptr); + register int number_to_move, i; + int ret_val; + + if ( (yy_c_buf_p) > &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] ) + YY_FATAL_ERROR( + "fatal flex scanner internal error--end of buffer missed" ); + + if ( YY_CURRENT_BUFFER_LVALUE->yy_fill_buffer == 0 ) + { /* Don't try to fill the buffer, so this is an EOF. */ + if ( (yy_c_buf_p) - (yytext_ptr) - YY_MORE_ADJ == 1 ) + { + /* We matched a single character, the EOB, so + * treat this as a final EOF. + */ + return EOB_ACT_END_OF_FILE; + } + + else + { + /* We matched some text prior to the EOB, first + * process it. + */ + return EOB_ACT_LAST_MATCH; + } + } + + /* Try to read more data. */ + + /* First move last chars to start of buffer. */ + number_to_move = (int) ((yy_c_buf_p) - (yytext_ptr)) - 1; + + for ( i = 0; i < number_to_move; ++i ) + *(dest++) = *(source++); + + if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_EOF_PENDING ) + /* don't do the read, it's not guaranteed to return an EOF, + * just force an EOF + */ + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars) = 0; + + else + { + int num_to_read = + YY_CURRENT_BUFFER_LVALUE->yy_buf_size - number_to_move - 1; + + while ( num_to_read <= 0 ) + { /* Not enough room in the buffer - grow it. */ + + /* just a shorter name for the current buffer */ + YY_BUFFER_STATE b = YY_CURRENT_BUFFER; + + int yy_c_buf_p_offset = + (int) ((yy_c_buf_p) - b->yy_ch_buf); + + if ( b->yy_is_our_buffer ) + { + int new_size = b->yy_buf_size * 2; + + if ( new_size <= 0 ) + b->yy_buf_size += b->yy_buf_size / 8; + else + b->yy_buf_size *= 2; + + b->yy_ch_buf = (char *) + /* Include room in for 2 EOB chars. */ + gras_ddt_parse_realloc((void *) b->yy_ch_buf,b->yy_buf_size + 2 ); + } + else + /* Can't grow it, we don't own it. */ + b->yy_ch_buf = 0; + + if ( ! b->yy_ch_buf ) + YY_FATAL_ERROR( + "fatal error - scanner input buffer overflow" ); + + (yy_c_buf_p) = &b->yy_ch_buf[yy_c_buf_p_offset]; + + num_to_read = YY_CURRENT_BUFFER_LVALUE->yy_buf_size - + number_to_move - 1; + + } + + if ( num_to_read > YY_READ_BUF_SIZE ) + num_to_read = YY_READ_BUF_SIZE; + + /* Read in more data. */ + YY_INPUT( (&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]), + (yy_n_chars), num_to_read ); + + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); + } + + if ( (yy_n_chars) == 0 ) + { + if ( number_to_move == YY_MORE_ADJ ) + { + ret_val = EOB_ACT_END_OF_FILE; + gras_ddt_parse_restart(gras_ddt_parse_in ); + } + + else + { + ret_val = EOB_ACT_LAST_MATCH; + YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = + YY_BUFFER_EOF_PENDING; + } + } + + else + ret_val = EOB_ACT_CONTINUE_SCAN; + + (yy_n_chars) += number_to_move; + YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] = YY_END_OF_BUFFER_CHAR; + YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] = YY_END_OF_BUFFER_CHAR; + + (yytext_ptr) = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[0]; + + return ret_val; +} + +/* yy_get_previous_state - get the state just before the EOB char was reached */ + + static yy_state_type yy_get_previous_state (void) +{ + register yy_state_type yy_current_state; + register char *yy_cp; + + yy_current_state = (yy_start); + + for ( yy_cp = (yytext_ptr) + YY_MORE_ADJ; yy_cp < (yy_c_buf_p); ++yy_cp ) + { + register YY_CHAR yy_c = (*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : 1); + if ( yy_accept[yy_current_state] ) + { + (yy_last_accepting_state) = yy_current_state; + (yy_last_accepting_cpos) = yy_cp; + } + while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) + { + yy_current_state = (int) yy_def[yy_current_state]; + if ( yy_current_state >= 61 ) + yy_c = yy_meta[(unsigned int) yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; + } + + return yy_current_state; +} + +/* yy_try_NUL_trans - try to make a transition on the NUL character + * + * synopsis + * next_state = yy_try_NUL_trans( current_state ); + */ + static yy_state_type yy_try_NUL_trans (yy_state_type yy_current_state ) +{ + register int yy_is_jam; + register char *yy_cp = (yy_c_buf_p); + + register YY_CHAR yy_c = 1; + if ( yy_accept[yy_current_state] ) + { + (yy_last_accepting_state) = yy_current_state; + (yy_last_accepting_cpos) = yy_cp; + } + while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) + { + yy_current_state = (int) yy_def[yy_current_state]; + if ( yy_current_state >= 61 ) + yy_c = yy_meta[(unsigned int) yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; + yy_is_jam = (yy_current_state == 60); + + return yy_is_jam ? 0 : yy_current_state; +} + + static void yyunput (int c, register char * yy_bp ) +{ + register char *yy_cp; + + yy_cp = (yy_c_buf_p); + + /* undo effects of setting up gras_ddt_parse_text */ + *yy_cp = (yy_hold_char); + + if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 ) + { /* need to shift things up to make room */ + /* +2 for EOB chars. */ + register int number_to_move = (yy_n_chars) + 2; + register char *dest = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[ + YY_CURRENT_BUFFER_LVALUE->yy_buf_size + 2]; + register char *source = + &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]; + + while ( source > YY_CURRENT_BUFFER_LVALUE->yy_ch_buf ) + *--dest = *--source; + + yy_cp += (int) (dest - source); + yy_bp += (int) (dest - source); + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = + (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_buf_size; + + if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 ) + YY_FATAL_ERROR( "flex scanner push-back overflow" ); + } + + *--yy_cp = (char) c; + + (yytext_ptr) = yy_bp; + (yy_hold_char) = *yy_cp; + (yy_c_buf_p) = yy_cp; +} + +#ifndef YY_NO_INPUT +#ifdef __cplusplus + static int yyinput (void) +#else + static int input (void) +#endif + +{ + int c; + + *(yy_c_buf_p) = (yy_hold_char); + + if ( *(yy_c_buf_p) == YY_END_OF_BUFFER_CHAR ) + { + /* yy_c_buf_p now points to the character we want to return. + * If this occurs *before* the EOB characters, then it's a + * valid NUL; if not, then we've hit the end of the buffer. + */ + if ( (yy_c_buf_p) < &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] ) + /* This was really a NUL. */ + *(yy_c_buf_p) = '\0'; + + else + { /* need more input */ + int offset = (yy_c_buf_p) - (yytext_ptr); + ++(yy_c_buf_p); + + switch ( yy_get_next_buffer( ) ) + { + case EOB_ACT_LAST_MATCH: + /* This happens because yy_g_n_b() + * sees that we've accumulated a + * token and flags that we need to + * try matching the token before + * proceeding. But for input(), + * there's no matching to consider. + * So convert the EOB_ACT_LAST_MATCH + * to EOB_ACT_END_OF_FILE. + */ + + /* Reset buffer status. */ + gras_ddt_parse_restart(gras_ddt_parse_in ); + + /*FALLTHROUGH*/ + + case EOB_ACT_END_OF_FILE: + { + if ( gras_ddt_parse_wrap( ) ) + return EOF; + + if ( ! (yy_did_buffer_switch_on_eof) ) + YY_NEW_FILE; +#ifdef __cplusplus + return yyinput(); +#else + return input(); +#endif + } + + case EOB_ACT_CONTINUE_SCAN: + (yy_c_buf_p) = (yytext_ptr) + offset; + break; + } + } + } + + c = *(unsigned char *) (yy_c_buf_p); /* cast for 8-bit char's */ + *(yy_c_buf_p) = '\0'; /* preserve gras_ddt_parse_text */ + (yy_hold_char) = *++(yy_c_buf_p); + + return c; +} +#endif /* ifndef YY_NO_INPUT */ + +/** Immediately switch to a different input stream. + * @param input_file A readable stream. + * + * @note This function does not reset the start condition to @c INITIAL . + */ + void gras_ddt_parse_restart (FILE * input_file ) +{ + + if ( ! YY_CURRENT_BUFFER ){ + gras_ddt_parse_ensure_buffer_stack (); + YY_CURRENT_BUFFER_LVALUE = + gras_ddt_parse__create_buffer(gras_ddt_parse_in,YY_BUF_SIZE ); + } + + gras_ddt_parse__init_buffer(YY_CURRENT_BUFFER,input_file ); + gras_ddt_parse__load_buffer_state( ); +} + +/** Switch to a different input buffer. + * @param new_buffer The new input buffer. + * + */ + void gras_ddt_parse__switch_to_buffer (YY_BUFFER_STATE new_buffer ) +{ + + /* TODO. We should be able to replace this entire function body + * with + * gras_ddt_parse_pop_buffer_state(); + * gras_ddt_parse_push_buffer_state(new_buffer); + */ + gras_ddt_parse_ensure_buffer_stack (); + if ( YY_CURRENT_BUFFER == new_buffer ) + return; + + if ( YY_CURRENT_BUFFER ) + { + /* Flush out information for old buffer. */ + *(yy_c_buf_p) = (yy_hold_char); + YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p); + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); + } + + YY_CURRENT_BUFFER_LVALUE = new_buffer; + gras_ddt_parse__load_buffer_state( ); + + /* We don't actually know whether we did this switch during + * EOF (gras_ddt_parse_wrap()) processing, but the only time this flag + * is looked at is after gras_ddt_parse_wrap() is called, so it's safe + * to go ahead and always set it. + */ + (yy_did_buffer_switch_on_eof) = 1; +} + +static void gras_ddt_parse__load_buffer_state (void) +{ + (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; + (yytext_ptr) = (yy_c_buf_p) = YY_CURRENT_BUFFER_LVALUE->yy_buf_pos; + gras_ddt_parse_in = YY_CURRENT_BUFFER_LVALUE->yy_input_file; + (yy_hold_char) = *(yy_c_buf_p); +} + +/** Allocate and initialize an input buffer state. + * @param file A readable stream. + * @param size The character buffer size in bytes. When in doubt, use @c YY_BUF_SIZE. + * + * @return the allocated buffer state. + */ + YY_BUFFER_STATE gras_ddt_parse__create_buffer (FILE * file, int size ) +{ + YY_BUFFER_STATE b; + + b = (YY_BUFFER_STATE) gras_ddt_parse_alloc(sizeof( struct yy_buffer_state ) ); + if ( ! b ) + YY_FATAL_ERROR( "out of dynamic memory in gras_ddt_parse__create_buffer()" ); + + b->yy_buf_size = size; + + /* yy_ch_buf has to be 2 characters longer than the size given because + * we need to put in 2 end-of-buffer characters. + */ + b->yy_ch_buf = (char *) gras_ddt_parse_alloc(b->yy_buf_size + 2 ); + if ( ! b->yy_ch_buf ) + YY_FATAL_ERROR( "out of dynamic memory in gras_ddt_parse__create_buffer()" ); + + b->yy_is_our_buffer = 1; + + gras_ddt_parse__init_buffer(b,file ); + + return b; +} + +/** Destroy the buffer. + * @param b a buffer created with gras_ddt_parse__create_buffer() + * + */ + void gras_ddt_parse__delete_buffer (YY_BUFFER_STATE b ) +{ + + if ( ! b ) + return; + + if ( b == YY_CURRENT_BUFFER ) /* Not sure if we should pop here. */ + YY_CURRENT_BUFFER_LVALUE = (YY_BUFFER_STATE) 0; + + if ( b->yy_is_our_buffer ) + gras_ddt_parse_free((void *) b->yy_ch_buf ); + + gras_ddt_parse_free((void *) b ); +} + +#ifndef __cplusplus +#ifndef _WIN32 +extern int isatty (int ); +#endif +#endif /* __cplusplus */ + +/* Initializes or reinitializes a buffer. + * This function is sometimes called more than once on the same buffer, + * such as during a gras_ddt_parse_restart() or at EOF. + */ + static void gras_ddt_parse__init_buffer (YY_BUFFER_STATE b, FILE * file ) + +{ + int oerrno = errno; + + gras_ddt_parse__flush_buffer(b ); + + b->yy_input_file = file; + b->yy_fill_buffer = 1; + + /* If b is the current buffer, then gras_ddt_parse__init_buffer was _probably_ + * called from gras_ddt_parse_restart() or through yy_get_next_buffer. + * In that case, we don't want to reset the lineno or column. + */ + if (b != YY_CURRENT_BUFFER){ + b->yy_bs_lineno = 1; + b->yy_bs_column = 0; + } + + b->yy_is_interactive = file ? (isatty( fileno(file) ) > 0) : 0; + + errno = oerrno; +} + +/** Discard all buffered characters. On the next scan, YY_INPUT will be called. + * @param b the buffer state to be flushed, usually @c YY_CURRENT_BUFFER. + * + */ + void gras_ddt_parse__flush_buffer (YY_BUFFER_STATE b ) +{ + if ( ! b ) + return; + + b->yy_n_chars = 0; + + /* We always need two end-of-buffer characters. The first causes + * a transition to the end-of-buffer state. The second causes + * a jam in that state. + */ + b->yy_ch_buf[0] = YY_END_OF_BUFFER_CHAR; + b->yy_ch_buf[1] = YY_END_OF_BUFFER_CHAR; + + b->yy_buf_pos = &b->yy_ch_buf[0]; + + b->yy_at_bol = 1; + b->yy_buffer_status = YY_BUFFER_NEW; + + if ( b == YY_CURRENT_BUFFER ) + gras_ddt_parse__load_buffer_state( ); +} + +/** Pushes the new state onto the stack. The new state becomes + * the current state. This function will allocate the stack + * if necessary. + * @param new_buffer The new state. + * + */ +void gras_ddt_parse_push_buffer_state (YY_BUFFER_STATE new_buffer ) +{ + if (new_buffer == NULL) + return; + + gras_ddt_parse_ensure_buffer_stack(); + + /* This block is copied from gras_ddt_parse__switch_to_buffer. */ + if ( YY_CURRENT_BUFFER ) + { + /* Flush out information for old buffer. */ + *(yy_c_buf_p) = (yy_hold_char); + YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p); + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); + } + + /* Only push if top exists. Otherwise, replace top. */ + if (YY_CURRENT_BUFFER) + (yy_buffer_stack_top)++; + YY_CURRENT_BUFFER_LVALUE = new_buffer; + + /* copied from gras_ddt_parse__switch_to_buffer. */ + gras_ddt_parse__load_buffer_state( ); + (yy_did_buffer_switch_on_eof) = 1; +} + +/** Removes and deletes the top of the stack, if present. + * The next element becomes the new top. + * + */ +void gras_ddt_parse_pop_buffer_state (void) +{ + if (!YY_CURRENT_BUFFER) + return; + + gras_ddt_parse__delete_buffer(YY_CURRENT_BUFFER ); + YY_CURRENT_BUFFER_LVALUE = NULL; + if ((yy_buffer_stack_top) > 0) + --(yy_buffer_stack_top); + + if (YY_CURRENT_BUFFER) { + gras_ddt_parse__load_buffer_state( ); + (yy_did_buffer_switch_on_eof) = 1; + } +} + +/* Allocates the stack if it does not exist. + * Guarantees space for at least one push. + */ +static void gras_ddt_parse_ensure_buffer_stack (void) +{ + int num_to_alloc; + + if (!(yy_buffer_stack)) { + + /* First allocation is just for 2 elements, since we don't know if this + * scanner will even need a stack. We use 2 instead of 1 to avoid an + * immediate realloc on the next call. + */ + num_to_alloc = 1; + (yy_buffer_stack) = (struct yy_buffer_state**)gras_ddt_parse_alloc + (num_to_alloc * sizeof(struct yy_buffer_state*) + ); + + memset((yy_buffer_stack), 0, num_to_alloc * sizeof(struct yy_buffer_state*)); + + (yy_buffer_stack_max) = num_to_alloc; + (yy_buffer_stack_top) = 0; + return; + } + + if ((yy_buffer_stack_top) >= ((yy_buffer_stack_max)) - 1){ + + /* Increase the buffer to prepare for a possible push. */ + int grow_size = 8 /* arbitrary grow size */; + + num_to_alloc = (yy_buffer_stack_max) + grow_size; + (yy_buffer_stack) = (struct yy_buffer_state**)gras_ddt_parse_realloc + ((yy_buffer_stack), + num_to_alloc * sizeof(struct yy_buffer_state*) + ); + + /* zero only the new slots.*/ + memset((yy_buffer_stack) + (yy_buffer_stack_max), 0, grow_size * sizeof(struct yy_buffer_state*)); + (yy_buffer_stack_max) = num_to_alloc; + } +} + +/** Setup the input buffer state to scan directly from a user-specified character buffer. + * @param base the character buffer + * @param size the size in bytes of the character buffer + * + * @return the newly allocated buffer state object. + */ +YY_BUFFER_STATE gras_ddt_parse__scan_buffer (char * base, yy_size_t size ) +{ + YY_BUFFER_STATE b; + + if ( size < 2 || + base[size-2] != YY_END_OF_BUFFER_CHAR || + base[size-1] != YY_END_OF_BUFFER_CHAR ) + /* They forgot to leave room for the EOB's. */ + return 0; + + b = (YY_BUFFER_STATE) gras_ddt_parse_alloc(sizeof( struct yy_buffer_state ) ); + if ( ! b ) + YY_FATAL_ERROR( "out of dynamic memory in gras_ddt_parse__scan_buffer()" ); + + b->yy_buf_size = size - 2; /* "- 2" to take care of EOB's */ + b->yy_buf_pos = b->yy_ch_buf = base; + b->yy_is_our_buffer = 0; + b->yy_input_file = 0; + b->yy_n_chars = b->yy_buf_size; + b->yy_is_interactive = 0; + b->yy_at_bol = 1; + b->yy_fill_buffer = 0; + b->yy_buffer_status = YY_BUFFER_NEW; + + gras_ddt_parse__switch_to_buffer(b ); + + return b; +} + +/** Setup the input buffer state to scan a string. The next call to gras_ddt_parse_lex() will + * scan from a @e copy of @a str. + * @param str a NUL-terminated string to scan + * + * @return the newly allocated buffer state object. + * @note If you want to scan bytes that may contain NUL values, then use + * gras_ddt_parse__scan_bytes() instead. + */ +YY_BUFFER_STATE gras_ddt_parse__scan_string (yyconst char * yy_str ) +{ + + return gras_ddt_parse__scan_bytes(yy_str,strlen(yy_str) ); +} + +/** Setup the input buffer state to scan the given bytes. The next call to gras_ddt_parse_lex() will + * scan from a @e copy of @a bytes. + * @param bytes the byte buffer to scan + * @param len the number of bytes in the buffer pointed to by @a bytes. + * + * @return the newly allocated buffer state object. + */ +YY_BUFFER_STATE gras_ddt_parse__scan_bytes (yyconst char * bytes, int len ) +{ + YY_BUFFER_STATE b; + char *buf; + yy_size_t n; + int i; + + /* Get memory for full buffer, including space for trailing EOB's. */ + n = len + 2; + buf = (char *) gras_ddt_parse_alloc(n ); + if ( ! buf ) + YY_FATAL_ERROR( "out of dynamic memory in gras_ddt_parse__scan_bytes()" ); + + for ( i = 0; i < len; ++i ) + buf[i] = bytes[i]; + + buf[len] = buf[len+1] = YY_END_OF_BUFFER_CHAR; + + b = gras_ddt_parse__scan_buffer(buf,n ); + if ( ! b ) + YY_FATAL_ERROR( "bad buffer in gras_ddt_parse__scan_bytes()" ); + + /* It's okay to grow etc. this buffer, and we should throw it + * away when we're done. + */ + b->yy_is_our_buffer = 1; + + return b; +} + +#ifndef YY_EXIT_FAILURE +#define YY_EXIT_FAILURE 2 +#endif + +static void yy_fatal_error (yyconst char* msg ) +{ + (void) fprintf( stderr, "%s\n", msg ); + exit( YY_EXIT_FAILURE ); +} + +/* Redefine yyless() so it works in section 3 code. */ + +#undef yyless +#define yyless(n) \ + do \ + { \ + /* Undo effects of setting up gras_ddt_parse_text. */ \ + int yyless_macro_arg = (n); \ + YY_LESS_LINENO(yyless_macro_arg);\ + gras_ddt_parse_text[gras_ddt_parse_leng] = (yy_hold_char); \ + (yy_c_buf_p) = gras_ddt_parse_text + yyless_macro_arg; \ + (yy_hold_char) = *(yy_c_buf_p); \ + *(yy_c_buf_p) = '\0'; \ + gras_ddt_parse_leng = yyless_macro_arg; \ + } \ + while ( 0 ) + +/* Accessor methods (get/set functions) to struct members. */ + +/** Get the current line number. + * + */ +int gras_ddt_parse_get_lineno (void) +{ + + return gras_ddt_parse_lineno; +} + +/** Get the input stream. + * + */ +FILE *gras_ddt_parse_get_in (void) +{ + return gras_ddt_parse_in; +} + +/** Get the output stream. + * + */ +FILE *gras_ddt_parse_get_out (void) +{ + return gras_ddt_parse_out; +} + +/** Get the length of the current token. + * + */ +int gras_ddt_parse_get_leng (void) +{ + return gras_ddt_parse_leng; +} + +/** Get the current token. + * + */ + +char *gras_ddt_parse_get_text (void) +{ + return gras_ddt_parse_text; +} + +/** Set the current line number. + * @param line_number + * + */ +void gras_ddt_parse_set_lineno (int line_number ) +{ + + gras_ddt_parse_lineno = line_number; +} + +/** Set the input stream. This does not discard the current + * input buffer. + * @param in_str A readable stream. + * + * @see gras_ddt_parse__switch_to_buffer + */ +void gras_ddt_parse_set_in (FILE * in_str ) +{ + gras_ddt_parse_in = in_str ; +} + +void gras_ddt_parse_set_out (FILE * out_str ) +{ + gras_ddt_parse_out = out_str ; +} + +int gras_ddt_parse_get_debug (void) +{ + return gras_ddt_parse__flex_debug; +} + +void gras_ddt_parse_set_debug (int bdebug ) +{ + gras_ddt_parse__flex_debug = bdebug ; +} + +/* gras_ddt_parse_lex_destroy is for both reentrant and non-reentrant scanners. */ +int gras_ddt_parse_lex_destroy (void) +{ + + /* Pop the buffer stack, destroying each element. */ + while(YY_CURRENT_BUFFER){ + gras_ddt_parse__delete_buffer(YY_CURRENT_BUFFER ); + YY_CURRENT_BUFFER_LVALUE = NULL; + gras_ddt_parse_pop_buffer_state(); + } + + /* Destroy the stack itself. */ + gras_ddt_parse_free((yy_buffer_stack) ); + (yy_buffer_stack) = NULL; + + return 0; +} + +/* + * Internal utility routines. + */ + +#ifndef yytext_ptr +static void yy_flex_strncpy (char* s1, yyconst char * s2, int n ) +{ + register int i; + for ( i = 0; i < n; ++i ) + s1[i] = s2[i]; +} +#endif + +#ifdef YY_NEED_STRLEN +static int yy_flex_strlen (yyconst char * s ) +{ + register int n; + for ( n = 0; s[n]; ++n ) + ; + + return n; +} +#endif + +void *gras_ddt_parse_alloc (yy_size_t size ) +{ + return (void *) malloc( size ); +} + +void *gras_ddt_parse_realloc (void * ptr, yy_size_t size ) +{ + /* The cast to (char *) in the following accommodates both + * implementations that use char* generic pointers, and those + * that use void* generic pointers. It works with the latter + * because both ANSI C and C++ allow castless assignment from + * any pointer type to void*, and deal with argument conversions + * as though doing an assignment. + */ + return (void *) realloc( (char *) ptr, size ); +} + +void gras_ddt_parse_free (void * ptr ) +{ + free( (char *) ptr ); /* see gras_ddt_parse_realloc() for (char *) cast */ +} + +#define YYTABLES_NAME "yytables" + +#undef YY_NEW_FILE +#undef YY_FLUSH_BUFFER +#undef yy_set_bol +#undef yy_new_buffer +#undef yy_set_interactive +#undef yytext_ptr +#undef YY_DO_BEFORE_ACTION + +#ifdef YY_DECL_IS_OURS +#undef YY_DECL_IS_OURS +#undef YY_DECL +#endif + +/* {space}+ { return(TOKEN_SPACE);} */ + +void gras_ddt_parse_dump(void) { + switch(gras_ddt_parse_tok_num) { + case GRAS_DDT_PARSE_TOKEN_LA : {printf("TOKEN_LA ");break;} + case GRAS_DDT_PARSE_TOKEN_RA : {printf("TOKEN_RA ");break;} + case GRAS_DDT_PARSE_TOKEN_WORD : {printf("TOKEN_WORD ");break;} + /* case GRAS_DDT_PARSE_TOKEN_SPACE : {printf("TOKEN_SPACE ");break;}*/ + /* case GRAS_DDT_PARSE_TOKEN_COMMENT : {printf("TOKEN_COMMENT ");break;}*/ + case GRAS_DDT_PARSE_TOKEN_NEWLINE : {printf("TOKEN_NEWLINE\n");return;} + case GRAS_DDT_PARSE_TOKEN_EMPTY : {printf("TOKEN_EMPTY\n");return;} + default : {printf("Unknown token %d\n", gras_ddt_parse_tok_num);return;} + } + printf("-->%s<-- [line %d, pos %d]\n",gras_ddt_parse_text,gras_ddt_parse_line_pos,gras_ddt_parse_char_pos); + return; +} + +int gras_ddt_parse_lex_n_dump(void) { + gras_ddt_parse_tok_num = gras_ddt_parse_lex(); + /* gras_ddt_parse_char_pos += strlen(gras_ddt_parse_text);*/ + return(gras_ddt_parse_tok_num); +} + +void gras_ddt_parse_pointer_init(const char *file) { + gras_ddt_file_to_parse = fopen(file,"r"); + gras_ddt_input_buffer = gras_ddt_parse__create_buffer(gras_ddt_file_to_parse,10 ); + gras_ddt_parse__switch_to_buffer(gras_ddt_input_buffer); + + gras_ddt_parse_line_pos = 1; + gras_ddt_parse_char_pos = 0; + gras_ddt_parse_col_pos = 0; + gras_ddt_parse_tok_num = 0; +} + +void gras_ddt_parse_pointer_close(void) { + gras_ddt_parse__delete_buffer(gras_ddt_input_buffer); + fclose(gras_ddt_file_to_parse); + + gras_ddt_parse_line_pos = 1; + gras_ddt_parse_char_pos = 0; + gras_ddt_parse_tok_num = 0; +} + +void gras_ddt_parse_pointer_string_init(const char *string_to_parse) { + gras_ddt_input_buffer = gras_ddt_parse__scan_string (string_to_parse); + definition = string_to_parse; + gras_ddt_parse__switch_to_buffer(gras_ddt_input_buffer); + + gras_ddt_parse_line_pos = 1; + gras_ddt_parse_char_pos = 0; + gras_ddt_parse_tok_num = 0; +} + +void gras_ddt_parse_pointer_string_close(void) { + gras_ddt_parse__delete_buffer(gras_ddt_input_buffer); + + gras_ddt_parse_line_pos = 1; + gras_ddt_parse_char_pos = 0; + gras_ddt_parse_tok_num = 0; + + if (0) + yyunput('\0',NULL); /* fake a use of this function to calm gcc down */ +} + +/* Local variables:*/ +/* mode: c */ +/* End: */ + diff --git a/src/gras_simix/DataDesc/gras_simix_ddt_parse.yy.h b/src/gras_simix/DataDesc/gras_simix_ddt_parse.yy.h new file mode 100644 index 0000000000..cfedb4f644 --- /dev/null +++ b/src/gras_simix/DataDesc/gras_simix_ddt_parse.yy.h @@ -0,0 +1,71 @@ +/* $Id$ */ + +/* ddt_parse.h -- automatic parsing of data structures */ + +/* Copyright (c) 2003 Arnaud Legrand. */ +/* Copyright (c) 2003, 2004 Martin Quinson. */ +/* All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +typedef enum { + GRAS_DDT_PARSE_TOKEN_EMPTY = 0, + GRAS_DDT_PARSE_TOKEN_LA = 512, /* { 'A' for the french "accolade" since there is a name conflict in english (braket/brace) */ + GRAS_DDT_PARSE_TOKEN_RA, /* } */ + GRAS_DDT_PARSE_TOKEN_LB, /* [ */ + GRAS_DDT_PARSE_TOKEN_RB, /* ] */ + GRAS_DDT_PARSE_TOKEN_LP, /* ( */ + GRAS_DDT_PARSE_TOKEN_RP, /* ) */ + GRAS_DDT_PARSE_TOKEN_WORD, + GRAS_DDT_PARSE_TOKEN_SPACE, + GRAS_DDT_PARSE_TOKEN_COMMENT, + GRAS_DDT_PARSE_TOKEN_ANNOTATE, + GRAS_DDT_PARSE_TOKEN_NEWLINE, + GRAS_DDT_PARSE_TOKEN_STAR, + GRAS_DDT_PARSE_TOKEN_SEMI_COLON, + GRAS_DDT_PARSE_TOKEN_COLON, /* impossible since the macro think that it's a arg separator. + But handle anyway for the *vicious* calling gras_ddt_parse manually */ + GRAS_DDT_PARSE_TOKEN_ERROR +} gras_ddt_parse_token_t; + +#define GRAS_DDT_PARSE_MAX_STR_CONST 4048 + +extern int gras_ddt_parse_line_pos; +extern int gras_ddt_parse_col_pos; +extern int gras_ddt_parse_char_pos; +extern int gras_ddt_parse_tok_num; + +void gras_ddt_parse_dump(void); +int gras_ddt_parse_lex_n_dump(void); +void gras_ddt_parse_pointer_init(const char *file); +void gras_ddt_parse_pointer_close(void); +void gras_ddt_parse_pointer_string_init(const char *string_to_parse); +void gras_ddt_parse_pointer_string_close(void); + +/* prototypes of the functions offered by flex */ +int gras_ddt_parse_lex(void); +int gras_ddt_parse_get_lineno (void); +FILE *gras_ddt_parse_get_in (void); +FILE *gras_ddt_parse_get_out (void); +int gras_ddt_parse_get_leng (void); +char *gras_ddt_parse_get_text (void); +void gras_ddt_parse_set_lineno (int line_number ); +void gras_ddt_parse_set_in (FILE * in_str ); +void gras_ddt_parse_set_out (FILE * out_str ); +int gras_ddt_parse_get_debug (void); +void gras_ddt_parse_set_debug (int bdebug ); +int gras_ddt_parse_lex_destroy (void); + +#define PARSE_ERROR_PRE do { +#define PARSE_ERROR_POST xbt_abort();} while (0) + +#define PARSE_ERROR0(fmt) PARSE_ERROR_PRE \ + ERROR3(fmt " at %d:%d of :\n%s", gras_ddt_parse_line_pos,gras_ddt_parse_col_pos,definition);\ + PARSE_ERROR_POST +#define PARSE_ERROR1(fmt,a) PARSE_ERROR_PRE \ + ERROR4(fmt " at %d:%d of :\n%s",a, gras_ddt_parse_line_pos,gras_ddt_parse_col_pos,definition);\ + PARSE_ERROR_POST +#define PARSE_ERROR2(fmt,a,b) PARSE_ERROR_PRE \ + ERROR5(fmt " at %d:%d of :\n%s",a,b, gras_ddt_parse_line_pos,gras_ddt_parse_col_pos,definition);\ + PARSE_ERROR_POST diff --git a/src/gras_simix/DataDesc/gras_simix_ddt_parse.yy.l b/src/gras_simix/DataDesc/gras_simix_ddt_parse.yy.l new file mode 100644 index 0000000000..f3b62cb252 --- /dev/null +++ b/src/gras_simix/DataDesc/gras_simix_ddt_parse.yy.l @@ -0,0 +1,240 @@ +/* $Id$ */ + +/* DataDesc/ddt_parse -- automatic parsing of data structures */ + +/* Copyright (c) 2004 Arnaud Legrand, Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +%option noyywrap +%{ +#include "gras/DataDesc/datadesc_private.h" +#include "gras/DataDesc/ddt_parse.yy.h" +#include + YY_BUFFER_STATE gras_ddt_input_buffer; + FILE *gras_ddt_file_to_parse; + + int gras_ddt_parse_line_pos = 1; + int gras_ddt_parse_col_pos = 0; + int gras_ddt_parse_char_pos = 0; + int gras_ddt_parse_tok_num = 0; + const char *definition; + XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_ddt_lexer,gras_ddt_parse,"The crude internals of the lexer used for type parsing"); +#define SHOW_WHERE DEBUG4("%d:%d (char #%d): seen '%s'", gras_ddt_parse_line_pos,gras_ddt_parse_col_pos,gras_ddt_parse_char_pos,yytext) +%} + +%x annotate comment foo +space [ \t] +letter [A-Za-z._-] +digit [0-9] + +%% + int comment_caller=0; + int annotate_caller=0; + +"//"[^\n]* + +"/*g"{space}* { /****************** ANNOTATION ************************/ + DEBUG0("Begin annotation"); + annotate_caller = INITIAL; + gras_ddt_parse_char_pos+= strlen(yytext); + gras_ddt_parse_col_pos+= strlen(yytext); + BEGIN(annotate); +} +"/*g"{space}* { /* trim annotation */ + DEBUG0("Begin annotation"); + annotate_caller = foo; + gras_ddt_parse_char_pos+= strlen(yytext); + gras_ddt_parse_col_pos+= strlen(yytext); + BEGIN(annotate); +} + +{space}*"g*/" { + DEBUG0("End annotation"); + gras_ddt_parse_char_pos+= strlen(yytext); + gras_ddt_parse_col_pos+= strlen(yytext); + BEGIN(annotate_caller); +} + +"*/" { + PARSE_ERROR0("``/*g'' construct closed by a regular ``*/''"); +} +\n { + PARSE_ERROR0("Type annotation cannot spread over several lines"); +} + +.* { /* eat the rest */ + gras_ddt_parse_char_pos+= strlen(yytext); + gras_ddt_parse_col_pos+= strlen(yytext); + return GRAS_DDT_PARSE_TOKEN_ANNOTATE; +} + +"/*[^g]" { /****************** COMMENTS ************************/ + /* constructs like : */ + /*g [string] g*/ + /* are not comments but size annotations */ + comment_caller = INITIAL; + BEGIN(comment); +} + +"/*[^g]" { + comment_caller = foo; + BEGIN(comment); +} + +[^*\n]* { /* eat anything that's not a '*' */ +} +"*"+[^*/\n]* { /* eat up '*'s not followed by '/'s */ +} +\n { + ++gras_ddt_parse_line_pos; + gras_ddt_parse_col_pos=0; + gras_ddt_parse_char_pos++; +} +"*"+"/" { + gras_ddt_parse_char_pos+= strlen(yytext); + gras_ddt_parse_col_pos+= strlen(yytext); + BEGIN(comment_caller); +} + +({letter}|{digit})* { /****************** STATEMENTS ************************/ + gras_ddt_parse_char_pos += strlen(yytext); + gras_ddt_parse_col_pos += strlen(yytext); + SHOW_WHERE; + return(GRAS_DDT_PARSE_TOKEN_WORD); +} +"{" { + gras_ddt_parse_char_pos++; + gras_ddt_parse_col_pos++; + SHOW_WHERE; + return(GRAS_DDT_PARSE_TOKEN_LA); +} +"}" { + gras_ddt_parse_char_pos++; + gras_ddt_parse_col_pos++; + SHOW_WHERE; + return(GRAS_DDT_PARSE_TOKEN_RA); +} +"[" { + gras_ddt_parse_char_pos++; + gras_ddt_parse_col_pos++; + SHOW_WHERE; + return(GRAS_DDT_PARSE_TOKEN_LB); +} +"]" { + gras_ddt_parse_char_pos++; + gras_ddt_parse_col_pos++; + SHOW_WHERE; + return(GRAS_DDT_PARSE_TOKEN_RB); +} +"(" { + gras_ddt_parse_char_pos++; + gras_ddt_parse_col_pos++; + SHOW_WHERE; + return(GRAS_DDT_PARSE_TOKEN_LP); +} +")" { + gras_ddt_parse_char_pos++; + gras_ddt_parse_col_pos++; + SHOW_WHERE; + return(GRAS_DDT_PARSE_TOKEN_RP); +} +"*" { + gras_ddt_parse_char_pos++; + gras_ddt_parse_col_pos++; + SHOW_WHERE; + return(GRAS_DDT_PARSE_TOKEN_STAR); +} +";" { + gras_ddt_parse_char_pos++; + gras_ddt_parse_col_pos++; + SHOW_WHERE; + return(GRAS_DDT_PARSE_TOKEN_SEMI_COLON); +} +"," { + gras_ddt_parse_char_pos++; + gras_ddt_parse_col_pos++; + SHOW_WHERE; + return(GRAS_DDT_PARSE_TOKEN_COLON); +} +"\n" { + gras_ddt_parse_line_pos++; + gras_ddt_parse_char_pos++; + gras_ddt_parse_col_pos=0; + SHOW_WHERE; +} +. { + gras_ddt_parse_char_pos++; + gras_ddt_parse_col_pos++; + SHOW_WHERE; +} +%% +/* {space}+ { return(TOKEN_SPACE);} */ + +void gras_ddt_parse_dump(void) { + switch(gras_ddt_parse_tok_num) { + case GRAS_DDT_PARSE_TOKEN_LA : {printf("TOKEN_LA ");break;} + case GRAS_DDT_PARSE_TOKEN_RA : {printf("TOKEN_RA ");break;} + case GRAS_DDT_PARSE_TOKEN_WORD : {printf("TOKEN_WORD ");break;} + /* case GRAS_DDT_PARSE_TOKEN_SPACE : {printf("TOKEN_SPACE ");break;}*/ + /* case GRAS_DDT_PARSE_TOKEN_COMMENT : {printf("TOKEN_COMMENT ");break;}*/ + case GRAS_DDT_PARSE_TOKEN_NEWLINE : {printf("TOKEN_NEWLINE\n");return;} + case GRAS_DDT_PARSE_TOKEN_EMPTY : {printf("TOKEN_EMPTY\n");return;} + default : {printf("Unknown token %d\n", gras_ddt_parse_tok_num);return;} + } + printf("-->%s<-- [line %d, pos %d]\n",yytext,gras_ddt_parse_line_pos,gras_ddt_parse_char_pos); + return; +} + +int gras_ddt_parse_lex_n_dump(void) { + gras_ddt_parse_tok_num = gras_ddt_parse_lex(); + /* gras_ddt_parse_char_pos += strlen(yytext);*/ + return(gras_ddt_parse_tok_num); +} + +void gras_ddt_parse_pointer_init(const char *file) { + gras_ddt_file_to_parse = fopen(file,"r"); + gras_ddt_input_buffer = yy_create_buffer( gras_ddt_file_to_parse, 10 ); + yy_switch_to_buffer(gras_ddt_input_buffer); + + gras_ddt_parse_line_pos = 1; + gras_ddt_parse_char_pos = 0; + gras_ddt_parse_col_pos = 0; + gras_ddt_parse_tok_num = 0; +} + +void gras_ddt_parse_pointer_close(void) { + yy_delete_buffer(gras_ddt_input_buffer); + fclose(gras_ddt_file_to_parse); + + gras_ddt_parse_line_pos = 1; + gras_ddt_parse_char_pos = 0; + gras_ddt_parse_tok_num = 0; +} + + +void gras_ddt_parse_pointer_string_init(const char *string_to_parse) { + gras_ddt_input_buffer = yy_scan_string (string_to_parse); + definition = string_to_parse; + yy_switch_to_buffer(gras_ddt_input_buffer); + + gras_ddt_parse_line_pos = 1; + gras_ddt_parse_char_pos = 0; + gras_ddt_parse_tok_num = 0; +} + +void gras_ddt_parse_pointer_string_close(void) { + yy_delete_buffer(gras_ddt_input_buffer); + + gras_ddt_parse_line_pos = 1; + gras_ddt_parse_char_pos = 0; + gras_ddt_parse_tok_num = 0; + + if (0) + yyunput('\0',NULL); /* fake a use of this function to calm gcc down */ +} + +/* Local variables:*/ +/* mode: c */ +/* End: */ diff --git a/src/gras_simix/Msg/gras_simix_msg.c b/src/gras_simix/Msg/gras_simix_msg.c new file mode 100644 index 0000000000..6fcf0ad434 --- /dev/null +++ b/src/gras_simix/Msg/gras_simix_msg.c @@ -0,0 +1,835 @@ +/* $Id$ */ + +/* messaging - Function related to messaging (code shared between RL and SG)*/ + +/* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "xbt/ex.h" +#include "xbt/ex_interface.h" +#include "gras/Msg/msg_private.h" +#include "gras/Virtu/virtu_interface.h" +#include "gras/DataDesc/datadesc_interface.h" +#include "gras/Transport/transport_interface.h" /* gras_select */ +#include "portable.h" /* execinfo when available to propagate exceptions */ + +#ifndef MIN +#define MIN(a,b) ((a) < (b) ? (a) : (b)) +#endif + +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_msg,gras,"High level messaging"); + +xbt_set_t _gras_msgtype_set = NULL; +static char *make_namev(const char *name, short int ver); +char _GRAS_header[6]; +const char *e_gras_msg_kind_names[e_gras_msg_kind_count]= + {"UNKNOWN","ONEWAY","RPC call","RPC answer","RPC error"}; + +/* + * Creating procdata for this module + */ +static void *gras_msg_procdata_new() { + gras_msg_procdata_t res = xbt_new(s_gras_msg_procdata_t,1); + + res->name = xbt_strdup("gras_msg"); + res->name_len = 0; + res->msg_queue = xbt_dynar_new(sizeof(s_gras_msg_t), NULL); + res->msg_waitqueue = xbt_dynar_new(sizeof(s_gras_msg_t), NULL); + res->cbl_list = xbt_dynar_new(sizeof(gras_cblist_t *),gras_cbl_free); + res->timers = xbt_dynar_new(sizeof(s_gras_timer_t), NULL); + + return (void*)res; +} + +/* + * Freeing procdata for this module + */ +static void gras_msg_procdata_free(void *data) { + gras_msg_procdata_t res = (gras_msg_procdata_t)data; + + xbt_dynar_free(&( res->msg_queue )); + xbt_dynar_free(&( res->msg_waitqueue )); + xbt_dynar_free(&( res->cbl_list )); + xbt_dynar_free(&( res->timers )); + + free(res->name); + free(res); +} + +/* + * Module registration + */ +int gras_msg_libdata_id; +void gras_msg_register() { + gras_msg_libdata_id = gras_procdata_add("gras_msg",gras_msg_procdata_new, gras_msg_procdata_free); +} + +/* + * Initialize this submodule. + */ +void gras_msg_init(void) { + /* only initialize once */ + if (_gras_msgtype_set != NULL) + return; + + VERB0("Initializing Msg"); + + _gras_msgtype_set = xbt_set_new(); + + memcpy(_GRAS_header,"GRAS", 4); + _GRAS_header[4]=GRAS_PROTOCOL_VERSION; + _GRAS_header[5]=(char)GRAS_THISARCH; + + gras_msg_ctx_mallocator = + xbt_mallocator_new(1000, + gras_msg_ctx_mallocator_new_f, + gras_msg_ctx_mallocator_free_f, + gras_msg_ctx_mallocator_reset_f); +} + +/* + * Finalize the msg module + */ +void +gras_msg_exit(void) { + VERB0("Exiting Msg"); + xbt_set_free(&_gras_msgtype_set); + + xbt_mallocator_free(gras_msg_ctx_mallocator); +} + +/* + * Reclamed memory + */ +void gras_msgtype_free(void *t) { + gras_msgtype_t msgtype=(gras_msgtype_t)t; + if (msgtype) { + free(msgtype->name); + free(msgtype); + } +} +/** + * Dump all declared message types (debugging purpose) + */ +void gras_msgtype_dumpall(void) { + xbt_set_cursor_t cursor; + gras_msgtype_t msgtype=NULL; + + INFO0("Dump of all registered messages:"); + xbt_set_foreach(_gras_msgtype_set, cursor, msgtype) { + INFO6(" Message name: %s (v%d) %s; %s%s%s", + msgtype->name, msgtype->version, e_gras_msg_kind_names[msgtype->kind], + gras_datadesc_get_name(msgtype->ctn_type), + (msgtype->kind==e_gras_msg_kind_rpccall ? " -> ":""), + (msgtype->kind==e_gras_msg_kind_rpccall ? gras_datadesc_get_name(msgtype->answer_type) : "")); + } +} + + +/** + * make_namev: + * + * Returns the versionned name of the message. If the version is 0, that's + * the name unchanged. Pay attention to this before free'ing the result. + */ +static char *make_namev(const char *name, short int ver) { + char *namev; + + if (!ver) + return (char *)name; + + namev = (char*)xbt_malloc(strlen(name)+2+3+1); + + if (namev) + sprintf(namev,"%s_v%d",name,ver); + + return namev; +} + +/* Internal function doing the crude work of registering messages */ +void +gras_msgtype_declare_ext(const char *name, + short int version, + e_gras_msg_kind_t kind, + gras_datadesc_type_t payload_request, + gras_datadesc_type_t payload_answer) { + + gras_msgtype_t msgtype=NULL; + char *namev=make_namev(name,version); + volatile int found = 0; + xbt_ex_t e; + + TRY { + msgtype = (gras_msgtype_t)xbt_set_get_by_name(_gras_msgtype_set,namev); + found = 1; + } CATCH(e) { + if (e.category != not_found_error) + RETHROW; + xbt_ex_free(e); + } + + if (found) { + VERB2("Re-register version %d of message '%s' (same kind & payload, ignored).", + version, name); + xbt_assert3(msgtype->kind == kind, + "Message %s re-registered as a %s (it was known as a %s)", + namev,e_gras_msg_kind_names[kind],e_gras_msg_kind_names[msgtype->kind]); + xbt_assert3(!gras_datadesc_type_cmp(msgtype->ctn_type, payload_request), + "Message %s re-registred with another payload (%s was %s)", + namev,gras_datadesc_get_name(payload_request), + gras_datadesc_get_name(msgtype->ctn_type)); + + xbt_assert3(!gras_datadesc_type_cmp(msgtype->answer_type, payload_answer), + "Message %s re-registred with another answer payload (%s was %s)", + namev,gras_datadesc_get_name(payload_answer), + gras_datadesc_get_name(msgtype->answer_type)); + + return ; /* do really ignore it */ + + } + + VERB4("Register version %d of message '%s' " + "(payload: %s; answer payload: %s).", + version, name, gras_datadesc_get_name(payload_request), + gras_datadesc_get_name(payload_answer)); + + msgtype = xbt_new(s_gras_msgtype_t,1); + msgtype->name = (namev == name ? strdup(name) : namev); + msgtype->name_len = strlen(namev); + msgtype->version = version; + msgtype->kind = kind; + msgtype->ctn_type = payload_request; + msgtype->answer_type = payload_answer; + + xbt_set_add(_gras_msgtype_set, (xbt_set_elm_t)msgtype, + &gras_msgtype_free); +} + + +/** @brief declare a new message type of the given name. It only accepts the given datadesc as payload + * + * @param name: name as it should be used for logging messages (must be uniq) + * @param payload: datadescription of the payload + */ +void gras_msgtype_declare(const char *name, + gras_datadesc_type_t payload) { + gras_msgtype_declare_ext(name, 0, e_gras_msg_kind_oneway, payload, NULL); +} + + + +/** @brief declare a new versionned message type of the given name and payload + * + * @param name: name as it should be used for logging messages (must be uniq) + * @param version: something like versionning symbol + * @param payload: datadescription of the payload + * + * Registers a message to the GRAS mechanism. Use this version instead of + * gras_msgtype_declare when you change the semantic or syntax of a message and + * want your programs to be able to deal with both versions. Internally, each + * will be handled as an independent message type, so you can register + * differents for each of them. + */ +void +gras_msgtype_declare_v(const char *name, + short int version, + gras_datadesc_type_t payload) { + + gras_msgtype_declare_ext(name, version, + e_gras_msg_kind_oneway, payload, NULL); +} + +/** @brief retrieve an existing message type from its name (raises an exception if it does not exist). */ +gras_msgtype_t gras_msgtype_by_name (const char *name) { + return gras_msgtype_by_namev(name,0); +} +/** @brief retrieve an existing message type from its name (or NULL if it does not exist). */ +gras_msgtype_t gras_msgtype_by_name_or_null (const char *name) { + xbt_ex_t e; + gras_msgtype_t res = NULL; + + TRY { + res = gras_msgtype_by_namev(name,0); + } CATCH(e) { + res = NULL; + xbt_ex_free(e); + } + return res; +} + +/** @brief retrieve an existing message type from its name and version. */ +gras_msgtype_t gras_msgtype_by_namev(const char *name, + short int version) { + gras_msgtype_t res = NULL; + char *namev = make_namev(name,version); + volatile int found=0; + xbt_ex_t e; + + TRY { + res = (gras_msgtype_t)xbt_set_get_by_name(_gras_msgtype_set, namev); + found=1; + } CATCH(e) { + xbt_ex_free(e); + } + if (!found) + THROW1(not_found_error,0,"No registred message of that name: %s",name); + + if (name != namev) + free(namev); + + return res; +} +/** @brief retrieve an existing message type from its name and version. */ +gras_msgtype_t gras_msgtype_by_id(int id) { + return (gras_msgtype_t)xbt_set_get_by_id(_gras_msgtype_set, id); +} + +/** \brief Waits for a message to come in over a given socket. + * + * @param timeout: How long should we wait for this message. + * @param msgt_want: type of awaited msg (or NULL if I'm enclined to accept any message) + * @param expe_want: awaited expeditot (match on hostname, not port; NULL if not relevant) + * @param filter: function returning true or false when passed a payload. Messages for which it returns false are not selected. (NULL if not relevant) + * @param filter_ctx: context passed as second argument of the filter (a pattern to match?) + * @param[out] msg_got: where to write the message we got + * + * Every message of another type received before the one waited will be queued + * and used by subsequent call to this function or gras_msg_handle(). + */ + +void +gras_msg_wait_ext_(double timeout, + + gras_msgtype_t msgt_want, + gras_socket_t expe_want, + gras_msg_filter_t filter, + void *filter_ctx, + + gras_msg_t msg_got) { + + s_gras_msg_t msg; + double start, now; + gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id); + int cpt; + + xbt_assert0(msg_got,"msg_got is an output parameter"); + + start = gras_os_time(); + VERB1("Waiting for message '%s'",msgt_want?msgt_want->name:"(any)"); + + xbt_dynar_foreach(pd->msg_waitqueue,cpt,msg){ + if ( ( !msgt_want || (msg.type->code == msgt_want->code)) + && (!expe_want || (!strcmp( gras_socket_peer_name(msg.expe), + gras_socket_peer_name(expe_want)))) + && (!filter || filter(&msg,filter_ctx))) { + + memcpy(msg_got,&msg,sizeof(s_gras_msg_t)); + xbt_dynar_cursor_rm(pd->msg_waitqueue, &cpt); + VERB0("The waited message was queued"); + return; + } + } + + xbt_dynar_foreach(pd->msg_queue,cpt,msg){ + if ( ( !msgt_want || (msg.type->code == msgt_want->code)) + && (!expe_want || (!strcmp( gras_socket_peer_name(msg.expe), + gras_socket_peer_name(expe_want)))) + && (!filter || filter(&msg,filter_ctx))) { + + memcpy(msg_got,&msg,sizeof(s_gras_msg_t)); + xbt_dynar_cursor_rm(pd->msg_queue, &cpt); + VERB0("The waited message was queued"); + return; + } + } + + while (1) { + int need_restart; + xbt_ex_t e; + + restart_receive: /* Goto here when the receive of a message failed */ + need_restart=0; + now=gras_os_time(); + memset(&msg,sizeof(msg),0); + + TRY { + msg.expe = gras_trp_select(timeout ? timeout - now + start : 0); + gras_msg_recv(msg.expe, &msg); + } CATCH(e) { + if (e.category == system_error && + !strncmp("Socket closed by remote side",e.msg, + strlen("Socket closed by remote side"))) { + xbt_ex_free(e); + need_restart=1; + } else { + RETHROW; + } + } + if (need_restart) + goto restart_receive; + + DEBUG0("Got a message from the socket"); + + if ( ( !msgt_want || (msg.type->code == msgt_want->code)) + && (!expe_want || (!strcmp( gras_socket_peer_name(msg.expe), + gras_socket_peer_name(expe_want)))) + && (!filter || filter(&msg,filter_ctx))) { + + memcpy(msg_got,&msg,sizeof(s_gras_msg_t)); + DEBUG0("Message matches expectations. Use it."); + return; + } + DEBUG0("Message does not match expectations. Queue it."); + + /* not expected msg type. Queue it for later */ + xbt_dynar_push(pd->msg_queue,&msg); + + now=gras_os_time(); + if (now - start + 0.001 > timeout) { + THROW1(timeout_error, now-start+0.001-timeout, + "Timeout while waiting for msg '%s'", + msgt_want?msgt_want->name:"(any)"); + } + } + + THROW_IMPOSSIBLE; +} +/** \brief Waits for a message to come in over a given socket. + * + * @param timeout: How long should we wait for this message. + * @param msgt_want: type of awaited msg + * @param[out] expeditor: where to create a socket to answer the incomming message + * @param[out] payload: where to write the payload of the incomming message + * @return the error code (or no_error). + * + * Every message of another type received before the one waited will be queued + * and used by subsequent call to this function or gras_msg_handle(). + */ +void +gras_msg_wait_(double timeout, + gras_msgtype_t msgt_want, + gras_socket_t *expeditor, + void *payload) { + s_gras_msg_t msg; + + gras_msg_wait_ext_(timeout, + msgt_want, NULL, NULL, NULL, + &msg); + + if (msgt_want->ctn_type) { + xbt_assert1(payload, + "Message type '%s' convey a payload you must accept", + msgt_want->name); + } else { + xbt_assert1(!payload, + "No payload was declared for message type '%s'", + msgt_want->name); + } + + if (payload) { + memcpy(payload,msg.payl,msg.payl_size); + free(msg.payl); + } + + if (expeditor) + *expeditor = msg.expe; +} + +static int gras_msg_wait_or_filter(gras_msg_t msg, void *ctx) { + xbt_dynar_t dyn=(xbt_dynar_t)ctx; + int res = xbt_dynar_member(dyn,msg->type); + if (res) + VERB1("Got matching message (type=%s)",msg->type->name); + else + VERB0("Got message not matching our expectations"); + return res; +} +/** \brief Waits for a message to come in over a given socket. + * + * @param timeout: How long should we wait for this message. + * @param msgt_want: a dynar containing all accepted message type + * @param[out] ctx: the context of received message (in case it's a RPC call we want to answer to) + * @param[out] msgt_got: indice in the dynar of the type of the received message + * @param[out] payload: where to write the payload of the incomming message + * @return the error code (or no_error). + * + * Every message of a type not in the accepted list received before the one + * waited will be queued and used by subsequent call to this function or + * gras_msg_handle(). + * + * If you are interested in the context, pass the address of a s_gras_msg_cb_ctx_t variable. + */ +void gras_msg_wait_or(double timeout, + xbt_dynar_t msgt_want, + gras_msg_cb_ctx_t *ctx, + int *msgt_got, + void *payload) { + s_gras_msg_t msg; + + VERB1("Wait %f seconds for several message types",timeout); + gras_msg_wait_ext_(timeout, + NULL, NULL, + &gras_msg_wait_or_filter, (void*)msgt_want, + &msg); + + if (msg.type->ctn_type) { + xbt_assert1(payload, + "Message type '%s' convey a payload you must accept", + msg.type->name); + } /* don't check the other side since some of the types may have a payload */ + + if (payload && msg.type->ctn_type) { + memcpy(payload,msg.payl,msg.payl_size); + free(msg.payl); + } + + if (ctx) + *ctx=gras_msg_cb_ctx_new(msg.expe, msg.type, msg.ID, + (msg.kind == e_gras_msg_kind_rpccall), 60); + + if (msgt_got) + *msgt_got = xbt_dynar_search(msgt_want,msg.type); +} + + +/** \brief Send the data pointed by \a payload as a message of type + * \a msgtype to the peer \a sock */ +void +gras_msg_send_(gras_socket_t sock, + gras_msgtype_t msgtype, + void *payload) { + + if (msgtype->ctn_type) { + xbt_assert1(payload, + "Message type '%s' convey a payload you must provide", + msgtype->name); + } else { + xbt_assert1(!payload, + "No payload was declared for message type '%s'", + msgtype->name); + } + + DEBUG2("Send a oneway message of type '%s'. Payload=%p", + msgtype->name,payload); + gras_msg_send_ext(sock, e_gras_msg_kind_oneway,0, msgtype, payload); + VERB2("Sent a oneway message of type '%s'. Payload=%p", + msgtype->name,payload); +} + +/** @brief Handle all messages arriving within the given period + * + * @param period: How long to wait for incoming messages (in seconds) + * + * Messages are dealed with just like gras_msg_handle() would do. The + * difference is that gras_msg_handle() handles at most one message (or wait up + * to timeout second when no message arrives) while this function handles any + * amount of messages, and lasts the given period in any case. + */ +void +gras_msg_handleall(double period) { + xbt_ex_t e; + double begin=gras_os_time(); + double now; + + do { + now=gras_os_time(); + TRY{ + if (period - now + begin > 0) + gras_msg_handle(period - now + begin); + } CATCH(e) { + if (e.category != timeout_error) + RETHROW0("Error while waiting for messages: %s"); + xbt_ex_free(e); + } + } while (now - begin < period); +} + +/** @brief Handle an incomming message or timer (or wait up to \a timeOut seconds) + * + * @param timeOut: How long to wait for incoming messages (in seconds) + * @return the error code (or no_error). + * + * Messages are passed to the callbacks. See also gras_msg_handleall(). + */ +void +gras_msg_handle(double timeOut) { + + double untiltimer; + + int cpt; + int volatile ran_ok; + + s_gras_msg_t msg; + + gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id); + gras_cblist_t *list=NULL; + gras_msg_cb_t cb; + s_gras_msg_cb_ctx_t ctx; + + int timerexpected, timeouted; + xbt_ex_t e; + + VERB1("Handling message within the next %.2fs",timeOut); + + untiltimer = gras_msg_timer_handle(); + DEBUG1("Next timer in %f sec", untiltimer); + if (untiltimer == 0.0) { + /* A timer was already elapsed and handled */ + return; + } + if (untiltimer != -1.0) { + timerexpected = 1; + timeOut = MIN(timeOut, untiltimer); + } else { + timerexpected = 0; + } + + /* get a message (from the queue or from the net) */ + timeouted = 0; + if (xbt_dynar_length(pd->msg_queue)) { + DEBUG0("Get a message from the queue"); + xbt_dynar_shift(pd->msg_queue,&msg); + } else { + TRY { + msg.expe = gras_trp_select(timeOut); + } CATCH(e) { + if (e.category != timeout_error) + RETHROW; + xbt_ex_free(e); + timeouted = 1; + } + + if (!timeouted) { + TRY { + /* FIXME: if not the right kind, queue it and recall ourself or goto >:-) */ + gras_msg_recv(msg.expe, &msg); + DEBUG1("Received a msg from the socket kind:%s", + e_gras_msg_kind_names[msg.kind]); + + } CATCH(e) { + RETHROW4("Error while receiving a message on select()ed socket %p to [%s]%s:%d: %s", + msg.expe, + gras_socket_peer_proc(msg.expe),gras_socket_peer_name(msg.expe), + gras_socket_peer_port(msg.expe)); + } + } + } + + if (timeouted) { + if (timerexpected) { + + /* A timer elapsed before the arrival of any message even if we select()ed a bit */ + untiltimer = gras_msg_timer_handle(); + if (untiltimer == 0.0) { + /* we served a timer, we're done */ + return; + } else { + xbt_assert1(untiltimer>0, "Negative timer (%f). I'm 'puzzeled'", untiltimer); + WARN1("No timer elapsed, in contrary to expectations (next in %f sec)", + untiltimer); + THROW1(timeout_error,0, + "No timer elapsed, in contrary to expectations (next in %f sec)", + untiltimer); + } + + } else { + /* select timeouted, and no timer elapsed. Nothing to do */ + THROW1(timeout_error, 0, "No new message or timer (delay was %f)", + timeOut); + } + + } + + /* A message was already there or arrived in the meanwhile. handle it */ + xbt_dynar_foreach(pd->cbl_list,cpt,list) { + if (list->id == msg.type->code) { + break; + } else { + list=NULL; + } + } + if (!list) { + INFO3("No callback for message '%s' from %s:%d. Queue it for later gras_msg_wait() use.", + msg.type->name, + gras_socket_peer_name(msg.expe),gras_socket_peer_port(msg.expe)); + xbt_dynar_push(pd->msg_waitqueue,&msg); + return; /* FIXME: maybe we should call ourselves again until the end of the timer or a proper msg is got */ + } + + ctx.expeditor = msg.expe; + ctx.ID = msg.ID; + ctx.msgtype = msg.type; + ctx.answer_due = (msg.kind == e_gras_msg_kind_rpccall); + + switch (msg.kind) { + case e_gras_msg_kind_oneway: + case e_gras_msg_kind_rpccall: + ran_ok=0; + TRY { + xbt_dynar_foreach(list->cbs,cpt,cb) { + if (!ran_ok) { + DEBUG4("Use the callback #%d (@%p) for incomming msg %s (payload_size=%d)", + cpt+1,cb,msg.type->name,msg.payl_size); + if (!(*cb)(&ctx,msg.payl)) { + /* cb handled the message */ + free(msg.payl); + ran_ok = 1; + } + } + } + } CATCH(e) { + free(msg.payl); + if (msg.type->kind == e_gras_msg_kind_rpccall) { + /* The callback raised an exception, propagate it on the network */ + if (!e.remote) { /* the exception is born on this machine */ + e.host = (char*)gras_os_myname(); + xbt_ex_setup_backtrace(&e); + } + VERB5("Propagate %s exception ('%s') from '%s' RPC cb back to %s:%d", + (e.remote ? "remote" : "local"), + e.msg, + msg.type->name, + gras_socket_peer_name(msg.expe), + gras_socket_peer_port(msg.expe)); + gras_msg_send_ext(msg.expe, e_gras_msg_kind_rpcerror, + msg.ID, msg.type, &e); + xbt_ex_free(e); + ctx.answer_due = 0; + ran_ok=1; + } else { + RETHROW0("Callback raised an exception: %s"); + } + } + xbt_assert0(!(ctx.answer_due), + "RPC callback didn't call gras_msg_rpcreturn"); + + if (!ran_ok) + THROW1(mismatch_error,0, + "Message '%s' refused by all registered callbacks", msg.type->name); + /* FIXME: gras_datadesc_free not implemented => leaking the payload */ + break; + + + case e_gras_msg_kind_rpcanswer: + INFO1("Unexpected RPC answer discarded (type: %s)", msg.type->name); + WARN0("FIXME: gras_datadesc_free not implemented => leaking the payload"); + return; + + case e_gras_msg_kind_rpcerror: + INFO1("Unexpected RPC error discarded (type: %s)", msg.type->name); + WARN0("FIXME: gras_datadesc_free not implemented => leaking the payload"); + return; + + default: + THROW1(unknown_error,0, + "Cannot handle messages of kind %d yet",msg.type->kind); + } + +} + +void +gras_cbl_free(void *data){ + gras_cblist_t *list=*(void**)data; + if (list) { + xbt_dynar_free(&( list->cbs )); + free(list); + } +} + +/** \brief Bind the given callback to the given message type + * + * Several callbacks can be attached to a given message type. The lastly added one will get the message first, and + * if it returns a non-null value, the message will be passed to the second one. + * And so on until one of the callbacks accepts the message. + */ +void +gras_cb_register_(gras_msgtype_t msgtype, + gras_msg_cb_t cb) { + gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id); + gras_cblist_t *list=NULL; + int cpt; + + DEBUG2("Register %p as callback to '%s'",cb,msgtype->name); + + /* search the list of cb for this message on this host (creating if NULL) */ + xbt_dynar_foreach(pd->cbl_list,cpt,list) { + if (list->id == msgtype->code) { + break; + } else { + list=NULL; + } + } + if (!list) { + /* First cb? Create room */ + list = xbt_new(gras_cblist_t,1); + list->id = msgtype->code; + list->cbs = xbt_dynar_new(sizeof(gras_msg_cb_t), NULL); + xbt_dynar_push(pd->cbl_list,&list); + } + + /* Insert the new one into the set */ + xbt_dynar_insert_at(list->cbs,0,&cb); +} + +/** \brief Unbind the given callback from the given message type */ +void +gras_cb_unregister_(gras_msgtype_t msgtype, + gras_msg_cb_t cb) { + + gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id); + gras_cblist_t *list; + gras_msg_cb_t cb_cpt; + int cpt; + int found = 0; + + /* search the list of cb for this message on this host */ + xbt_dynar_foreach(pd->cbl_list,cpt,list) { + if (list->id == msgtype->code) { + break; + } else { + list=NULL; + } + } + + /* Remove it from the set */ + if (list) { + xbt_dynar_foreach(list->cbs,cpt,cb_cpt) { + if (cb == cb_cpt) { + xbt_dynar_cursor_rm(list->cbs, &cpt); + found = 1; + } + } + } + if (!found) + VERB1("Ignoring removal of unexisting callback to msg id %d", + msgtype->code); +} + +/** \brief Retrieve the expeditor of the message */ +gras_socket_t gras_msg_cb_ctx_from(gras_msg_cb_ctx_t ctx) { + return ctx->expeditor; +} +/* \brief Creates a new message exchange context (user should never have to) */ +gras_msg_cb_ctx_t gras_msg_cb_ctx_new(gras_socket_t expe, + gras_msgtype_t msgtype, + unsigned long int ID, + int answer_due, + double timeout) { + gras_msg_cb_ctx_t res=xbt_new(s_gras_msg_cb_ctx_t,1); + res->expeditor = expe; + res->msgtype = msgtype; + res->ID = ID; + res->timeout = timeout; + res->answer_due = answer_due; + + return res; +} +/* \brief Frees a message exchange context + * + * This function is mainly useful with \ref gras_msg_wait_or, ie seldom. + */ +void gras_msg_cb_ctx_free(gras_msg_cb_ctx_t ctx) { + free(ctx); +} diff --git a/src/gras_simix/Msg/gras_simix_msg_interface.h b/src/gras_simix/Msg/gras_simix_msg_interface.h new file mode 100644 index 0000000000..3595ead608 --- /dev/null +++ b/src/gras_simix/Msg/gras_simix_msg_interface.h @@ -0,0 +1,50 @@ +/* $Id$ */ + +/* messaging - high level communication (send/receive messages) */ + +/* module's public interface exported within GRAS, but not to end user. */ + +/* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#ifndef GRAS_MSG_INTERFACE_H +#define GRAS_MSG_INTERFACE_H + +#include "gras/transport.h" + +/* + * Data of this module specific to each process + * (used by sg_process.c to check some usual errors at the end of the simulation) + * FIXME: it could be cleaned up ? + */ +typedef struct { + /* set headers */ + unsigned int ID; + char *name; + unsigned int name_len; + + /* queue storing the msgs got while msg_wait'ing for something else. Reuse them ASAP. */ + xbt_dynar_t msg_queue; /* elm type: s_gras_msg_t */ + + /* queue storing the msgs without callback got when handling. Feed them to wait() */ + xbt_dynar_t msg_waitqueue; /* elm type: s_gras_msg_t */ + + /* registered callbacks for each message */ + xbt_dynar_t cbl_list; /* elm type: gras_cblist_t */ + + /* registered timers */ + xbt_dynar_t timers; /* elm type: s_gras_timer_t */ + +} s_gras_msg_procdata_t,*gras_msg_procdata_t; + + +void gras_msg_send_namev(gras_socket_t sock, + const char *namev, + void *payload); + +#define GRAS_PROTOCOL_VERSION '\0'; + + +#endif /* GRAS_MSG_INTERFACE_H */ diff --git a/src/gras_simix/Msg/gras_simix_msg_private.h b/src/gras_simix/Msg/gras_simix_msg_private.h new file mode 100644 index 0000000000..540e000a64 --- /dev/null +++ b/src/gras_simix/Msg/gras_simix_msg_private.h @@ -0,0 +1,132 @@ +/* $Id$ */ + +/* messaging - high level communication (send/receive messages) */ + +/* module's private interface masked even to other parts of GRAS. */ + +/* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#ifndef GRAS_MESSAGE_PRIVATE_H +#define GRAS_MESSAGE_PRIVATE_H + +#include "portable.h" + +#include "xbt/sysdep.h" +#include "xbt/log.h" +#include "xbt/dynar.h" +#include "xbt/set.h" +#include "gras/transport.h" +#include "gras/datadesc.h" +#include "gras/virtu.h" + +#include "gras/messages.h" +#include "gras/timer.h" +#include "gras_modinter.h" + +#include "gras/Msg/msg_interface.h" + +extern char _GRAS_header[6]; + +extern int gras_msg_libdata_id; /* The identifier of our libdata */ + +extern const char *e_gras_msg_kind_names[e_gras_msg_kind_count]; + +/* declare either regular messages or RPC or whatever */ +void +gras_msgtype_declare_ext(const char *name, + short int version, + e_gras_msg_kind_t kind, + gras_datadesc_type_t payload_request, + gras_datadesc_type_t payload_answer); + +/** + * gras_msgtype_t: + * + * Message type descriptor. There one of these for each registered version. + */ +typedef struct s_gras_msgtype { + /* headers for the data set */ + unsigned int code; + char *name; + unsigned int name_len; + + /* payload */ + short int version; + e_gras_msg_kind_t kind; + gras_datadesc_type_t ctn_type; + gras_datadesc_type_t answer_type; /* only used for RPC */ +} s_gras_msgtype_t; + +extern xbt_set_t _gras_msgtype_set; /* of gras_msgtype_t */ +void gras_msgtype_free(void *msgtype); + + +/* functions to extract msg from socket or put it on wire (depend RL vs SG) */ +void gras_msg_recv(gras_socket_t sock, + gras_msg_t msg/*OUT*/); +void gras_msg_send_ext(gras_socket_t sock, + e_gras_msg_kind_t kind, + unsigned long int ID, + gras_msgtype_t msgtype, + void *payload); + +/** + * gras_cblist_t: + * + * association between msg ID and cb list for a given process + */ +struct s_gras_cblist { + long int id; + xbt_dynar_t cbs; /* of gras_msg_cb_t */ +}; + +typedef struct s_gras_cblist gras_cblist_t; +void gras_cbl_free(void *); /* used to free the memory at the end */ +void gras_cblist_free(void *cbl); + +/** + * gras_msg_cb_ctx_t: + * + * Context associated to a given callback (to either regular message or RPC) + */ +struct s_gras_msg_cb_ctx { + gras_socket_t expeditor; + gras_msgtype_t msgtype; + unsigned long int ID; + double timeout; + int answer_due; /* Whether the callback is expected to return a result (for sanity checks) */ +}; +typedef struct s_gras_msg_cb_ctx s_gras_msg_cb_ctx_t; + +/* ********* * + * * TIMER * * + * ********* */ +typedef struct { + double expiry; + double period; + void_f_void_t *action; + int repeat; +} s_gras_timer_t, *gras_timer_t; + +/* returns 0 if it handled a timer, or the delay until next timer, or -1 if no armed timer */ +double gras_msg_timer_handle(void); + +gras_msg_cb_ctx_t gras_msg_cb_ctx_new(gras_socket_t expe, + gras_msgtype_t msgtype, + unsigned long int ID, + int answer_due, + double timeout); + + +/* We deploy a mallocator on the RPC contextes */ +#include "xbt/mallocator.h" +extern xbt_mallocator_t gras_msg_ctx_mallocator; +void* gras_msg_ctx_mallocator_new_f(void); +void gras_msg_ctx_mallocator_free_f(void* dict); +void gras_msg_ctx_mallocator_reset_f(void* dict); + + +#endif /* GRAS_MESSAGE_PRIVATE_H */ diff --git a/src/gras_simix/Msg/gras_simix_rl_msg.c b/src/gras_simix/Msg/gras_simix_rl_msg.c new file mode 100644 index 0000000000..a4c4e160d5 --- /dev/null +++ b/src/gras_simix/Msg/gras_simix_rl_msg.c @@ -0,0 +1,181 @@ +/* $Id$ */ + +/* messaging - Function related to messaging code specific to RL */ + +/* Copyright (c) 2003-2005 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "xbt/ex.h" +#include "gras/Msg/msg_private.h" + +#include "gras/DataDesc/datadesc_interface.h" +#include "gras/Transport/transport_interface.h" /* gras_trp_send/recv */ + +XBT_LOG_EXTERNAL_CATEGORY(gras_msg); +XBT_LOG_DEFAULT_CATEGORY(gras_msg); + +void gras_msg_send_ext(gras_socket_t sock, + e_gras_msg_kind_t kind, + unsigned long int ID, + gras_msgtype_t msgtype, + void *payload) { + + static gras_datadesc_type_t string_type=NULL; + static gras_datadesc_type_t ulong_type=NULL; + char c_kind=(char)kind; + + xbt_assert0(msgtype,"Cannot send the NULL message"); + + if (!string_type) { + string_type = gras_datadesc_by_name("string"); + xbt_assert(string_type); + } + if (!ulong_type) { + ulong_type = gras_datadesc_by_name("unsigned long int"); + xbt_assert(ulong_type); + } + + DEBUG3("send '%s' to %s:%d", msgtype->name, + gras_socket_peer_name(sock),gras_socket_peer_port(sock)); + gras_trp_send(sock, _GRAS_header, 6, 1 /* stable */); + gras_trp_send(sock, &c_kind, 1, 1 /* stable */); + switch (kind) { + case e_gras_msg_kind_oneway: + break; + + case e_gras_msg_kind_rpccall: + case e_gras_msg_kind_rpcanswer: + case e_gras_msg_kind_rpcerror: + gras_datadesc_send(sock,ulong_type,&ID); + break; + + default: + THROW1(unknown_error,0,"Unknown msg kind %d",kind); + } + + gras_datadesc_send(sock, string_type, &msgtype->name); + if (kind == e_gras_msg_kind_rpcerror) { + /* error on remote host, carfull, payload is an exception */ + gras_datadesc_send(sock, gras_datadesc_by_name("ex_t"),payload); + } else if (kind == e_gras_msg_kind_rpcanswer) { + if (msgtype->answer_type) + gras_datadesc_send(sock, msgtype->answer_type, payload); + }else { + /* regular message */ + if (msgtype->ctn_type) + gras_datadesc_send(sock, msgtype->ctn_type, payload); + } + + gras_trp_flush(sock); +} + +/* + * receive the next message on the given socket. + */ +void +gras_msg_recv(gras_socket_t sock, + gras_msg_t msg) { + + xbt_ex_t e; + static gras_datadesc_type_t string_type=NULL; + static gras_datadesc_type_t ulong_type=NULL; + char header[6]; + int cpt; + int r_arch; + char *msg_name=NULL; + char c_kind; + + xbt_assert1(!gras_socket_is_meas(sock), + "Asked to receive a message on the measurement socket %p", sock); + if (!string_type) { + string_type=gras_datadesc_by_name("string"); + xbt_assert(string_type); + } + if (!ulong_type) { + ulong_type = gras_datadesc_by_name("unsigned long int"); + xbt_assert(ulong_type); + } + + + TRY { + gras_trp_recv(sock, header, 6); + gras_trp_recv(sock, &c_kind, 1); + msg->kind=(e_gras_msg_kind_t)c_kind; + } CATCH(e) { + RETHROW0("Exception caught while trying to get the mesage header: %s"); + } + + for (cpt=0; cpt<4; cpt++) + if (header[cpt] != _GRAS_header[cpt]) + THROW2(mismatch_error,0, + "Incoming bytes do not look like a GRAS message (header='%.4s' not '%.4s')", + header,_GRAS_header); + if (header[4] != _GRAS_header[4]) + THROW2(mismatch_error,0,"GRAS protocol mismatch (got %d, use %d)", + (int)header[4], (int)_GRAS_header[4]); + r_arch = (int)header[5]; + DEBUG2("Handle an incoming message using protocol %d (remote is %s)", + (int)header[4],gras_datadesc_arch_name(r_arch)); + + switch (msg->kind) { + case e_gras_msg_kind_oneway: + break; + + case e_gras_msg_kind_rpccall: + case e_gras_msg_kind_rpcanswer: + case e_gras_msg_kind_rpcerror: + gras_datadesc_recv(sock,ulong_type,r_arch, &msg->ID); + break; + + default: + THROW_IMPOSSIBLE; + } + + gras_datadesc_recv(sock, string_type, r_arch, &msg_name); + TRY { + msg->type = (gras_msgtype_t)xbt_set_get_by_name(_gras_msgtype_set,msg_name); + } CATCH(e) { + /* FIXME: Survive unknown messages */ + RETHROW1("Exception caught while retrieving the type associated to messages '%s' : %s", + msg_name); + } + free(msg_name); + + if (msg->kind == e_gras_msg_kind_rpcerror) { + /* error on remote host. Carfull with that exception, eugene */ + msg->payl_size=gras_datadesc_size(gras_datadesc_by_name("ex_t")); + msg->payl=xbt_malloc(msg->payl_size); + gras_datadesc_recv(sock, gras_datadesc_by_name("ex_t"), r_arch, msg->payl); + + } else if (msg->kind == e_gras_msg_kind_rpcanswer) { + /* answer to RPC */ + if (msg->type->answer_type) { + msg->payl_size=gras_datadesc_size(msg->type->answer_type); + xbt_assert2(msg->payl_size > 0, + "%s %s", + "Dynamic array as payload is forbided for now (FIXME?).", + "Reference to dynamic array is allowed."); + msg->payl = xbt_malloc(msg->payl_size); + gras_datadesc_recv(sock, msg->type->answer_type, r_arch, msg->payl); + } else { + msg->payl = NULL; + msg->payl_size = 0; + } + } else { + /* regular message */ + if (msg->type->ctn_type) { + msg->payl_size=gras_datadesc_size(msg->type->ctn_type); + xbt_assert2(msg->payl_size > 0, + "%s %s", + "Dynamic array as payload is forbided for now (FIXME?).", + "Reference to dynamic array is allowed."); + msg->payl = xbt_malloc(msg->payl_size); + gras_datadesc_recv(sock, msg->type->ctn_type, r_arch, msg->payl); + } else { + msg->payl = NULL; + msg->payl_size = 0; + } + } +} diff --git a/src/gras_simix/Msg/gras_simix_rpc.c b/src/gras_simix/Msg/gras_simix_rpc.c new file mode 100644 index 0000000000..89374859ad --- /dev/null +++ b/src/gras_simix/Msg/gras_simix_rpc.c @@ -0,0 +1,231 @@ +/* $Id$ */ + +/* rpc - RPC implementation on top of GRAS messages */ + +/* Copyright (c) 2005 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "gras/Msg/msg_private.h" + +xbt_set_t _gras_rpctype_set = NULL; +xbt_dynar_t _gras_rpc_cancelled = NULL; + +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_msg_rpc,gras_msg,"RPC mecanism"); + + +/** @brief declare a new versionned RPC type of the given name and payloads + * + * @param name: name as it should be used for logging messages (must be uniq) + * @param payload_request: datatype of request + * @param payload_answer: datatype of answer + * + * Registers a new RPC message to the GRAS mechanism. RPC are constituted of a pair + * of messages. + */ +void +gras_msgtype_declare_rpc(const char *name, + gras_datadesc_type_t payload_request, + gras_datadesc_type_t payload_answer) { + + gras_msgtype_declare_ext(name, 0, + e_gras_msg_kind_rpccall, + payload_request, payload_answer); + +} + +/** @brief declare a new versionned RPC type of the given name and payloads + * + * @param name: name as it should be used for logging messages (must be uniq) + * @param version: something like versionning symbol + * @param payload_request: datatype of request + * @param payload_answer: datatype of answer + * + * Registers a new RPC message to the GRAS mechanism. RPC are constituted of a pair + * of messages. + * + * Use this version instead of gras_rpctype_declare when you change the + * semantic or syntax of a message and want your programs to be able to deal + * with both versions. Internally, each will be handled as an independent + * message type, so you can register differents for each of them. + */ +void +gras_msgtype_declare_rpc_v(const char *name, + short int version, + gras_datadesc_type_t payload_request, + gras_datadesc_type_t payload_answer) { + + gras_msgtype_declare_ext(name, version, + e_gras_msg_kind_rpccall, + payload_request, payload_answer); + +} + +static unsigned long int last_msg_ID = 0; + +static int msgfilter_rpcID(gras_msg_t msg, void* ctx) { + unsigned long int ID= *(unsigned long int*)ctx; + int res = msg->ID == ID && + (msg->kind == e_gras_msg_kind_rpcanswer || msg->kind == e_gras_msg_kind_rpcerror); + int cursor; + gras_msg_cb_ctx_t rpc_ctx; + + + DEBUG5("Filter a message of ID %lu, type '%s' and kind '%s'. Waiting for ID=%lu. %s", + msg->ID,msg->type->name,e_gras_msg_kind_names[msg->kind],ID, + res?"take it": "reject"); + + if (res && !_gras_rpc_cancelled) + return res; + + /* Check whether it is an old answer to a message we already canceled */ + xbt_dynar_foreach(_gras_rpc_cancelled,cursor,rpc_ctx) { + if (msg->ID == rpc_ctx->ID && msg->kind==e_gras_msg_kind_rpcanswer) { + VERB1("Got an answer to the already canceled (timeouted?) RPC %ld. Ignore it (leaking the payload!).",msg->ID); + xbt_dynar_cursor_rm (_gras_rpc_cancelled, &cursor); + return 1; + } + } + + return res; +} + +/* Mallocator cruft */ +xbt_mallocator_t gras_msg_ctx_mallocator = NULL; +void* gras_msg_ctx_mallocator_new_f(void) { + return xbt_new0(s_gras_msg_cb_ctx_t,1); +} +void gras_msg_ctx_mallocator_free_f(void* ctx) { + xbt_free(ctx); +} +void gras_msg_ctx_mallocator_reset_f(void* ctx) { + memset(ctx, sizeof(s_gras_msg_cb_ctx_t),0); +} + +/** @brief Launch a RPC call, but do not block for the answer */ +gras_msg_cb_ctx_t +gras_msg_rpc_async_call_(gras_socket_t server, + double timeOut, + gras_msgtype_t msgtype, + void *request) { + gras_msg_cb_ctx_t ctx = xbt_mallocator_get(gras_msg_ctx_mallocator); + + if (msgtype->ctn_type) { + xbt_assert1(request, + "RPC type '%s' convey a payload you must provide", + msgtype->name); + } else { + xbt_assert1(!request, + "No payload was declared for RPC type '%s'", + msgtype->name); + } + + ctx->ID = last_msg_ID++; + ctx->expeditor = server; + ctx->msgtype=msgtype; + ctx->timeout=timeOut; + + VERB4("Send to %s:%d a RPC of type '%s' (ID=%lu)", + gras_socket_peer_name(server), + gras_socket_peer_port(server), + msgtype->name,ctx->ID); + + gras_msg_send_ext(server, e_gras_msg_kind_rpccall, ctx->ID, msgtype, request); + + return ctx; +} + +/** @brief Wait the answer of a RPC call previously launched asynchronously */ +void gras_msg_rpc_async_wait(gras_msg_cb_ctx_t ctx, + void *answer) { + xbt_ex_t e; + s_gras_msg_t received; + + if (ctx->msgtype->answer_type) { + xbt_assert1(answer, + "Answers to RPC '%s' convey a payload you must accept", + ctx->msgtype->name); + } else { + xbt_assert1(!answer, + "No payload was declared for answers to RPC '%s'", + ctx->msgtype->name); + } + + TRY { + /* The filter returns 1 when we eat an old RPC answer to something canceled */ + do { + gras_msg_wait_ext_(ctx->timeout, + ctx->msgtype, NULL, msgfilter_rpcID, &ctx->ID, + &received); + } while (received.ID != ctx->ID); + + } CATCH(e) { + if (!_gras_rpc_cancelled) + _gras_rpc_cancelled = xbt_dynar_new(sizeof(ctx),NULL); + xbt_dynar_push(_gras_rpc_cancelled,&ctx); + INFO5("canceled RPC %ld pushed onto the stack (%s from %s:%d) Reason: %s", + ctx->ID,ctx->msgtype->name, + gras_socket_peer_name(ctx->expeditor),gras_socket_peer_port(ctx->expeditor), + e.msg); + RETHROW; + } + + xbt_mallocator_release(gras_msg_ctx_mallocator, ctx); + if (received.kind == e_gras_msg_kind_rpcerror) { + xbt_ex_t e; + memcpy(&e,received.payl,received.payl_size); + free(received.payl); + VERB3("Raise a remote exception cat:%d comming from %s (%s)", + e.category, e.host, e.msg); + __xbt_ex_ctx()->ctx_ex.msg = e.msg; + __xbt_ex_ctx()->ctx_ex.category = e.category; + __xbt_ex_ctx()->ctx_ex.value = e.value; + __xbt_ex_ctx()->ctx_ex.remote = 1; + __xbt_ex_ctx()->ctx_ex.host = e.host; + __xbt_ex_ctx()->ctx_ex.procname = e.procname; + __xbt_ex_ctx()->ctx_ex.pid = e.pid; + __xbt_ex_ctx()->ctx_ex.file = e.file; + __xbt_ex_ctx()->ctx_ex.line = e.line; + __xbt_ex_ctx()->ctx_ex.func = e.func; + __xbt_ex_ctx()->ctx_ex.used = e.used; + __xbt_ex_ctx()->ctx_ex.bt_strings = e.bt_strings; + memset(&__xbt_ex_ctx()->ctx_ex.bt,0, + sizeof(__xbt_ex_ctx()->ctx_ex.bt)); + DO_THROW(__xbt_ex_ctx()->ctx_ex); + } + memcpy(answer,received.payl,received.payl_size); + free(received.payl); +} + +/** @brief Conduct a RPC call */ +void gras_msg_rpccall_(gras_socket_t server, + double timeout, + gras_msgtype_t msgtype, + void *request, void *answer) { + + gras_msg_cb_ctx_t ctx; + + ctx = gras_msg_rpc_async_call_(server, timeout,msgtype,request); + gras_msg_rpc_async_wait(ctx, answer); +} + + +/** @brief Return the result of a RPC call + * + * It done before the actual return of the callback so that the callback can do + * some cleanups before leaving. + */ + +void gras_msg_rpcreturn(double timeOut,gras_msg_cb_ctx_t ctx,void *answer) { + xbt_assert0(ctx->answer_due, + "RPC return not allowed here. Either not a RPC message or already returned a result"); + ctx->answer_due = 0; + DEBUG5("Return to RPC '%s' from %s:%d (tOut=%f, payl=%p)", + ctx->msgtype->name, + gras_socket_peer_name(ctx->expeditor),gras_socket_peer_port(ctx->expeditor), + timeOut,answer); + gras_msg_send_ext(ctx->expeditor, e_gras_msg_kind_rpcanswer, + ctx->ID, ctx->msgtype, answer); +} + diff --git a/src/gras_simix/Msg/gras_simix_sg_msg.c b/src/gras_simix/Msg/gras_simix_sg_msg.c new file mode 100644 index 0000000000..444950f4e9 --- /dev/null +++ b/src/gras_simix/Msg/gras_simix_sg_msg.c @@ -0,0 +1,135 @@ +/* $Id$ */ + +/* messaging - Function related to messaging code specific to SG */ + +/* Copyright (c) 2003-2005 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "xbt/ex.h" + +#include "gras_simix/Virtu/gras_simix_virtu_sg.h" + +#include "gras_simix/Msg/gras_simix_msg_private.h" + +#include "gras_simix/DataDesc/gras_simix_datadesc_interface.h" +#include "gras_simix/Transport/gras_simix_transport_interface.h" /* gras_trp_chunk_send/recv */ +#include "gras_simix/Transport/gras_simix_transport_private.h" /* sock->data */ + +XBT_LOG_EXTERNAL_CATEGORY(gras_msg); +XBT_LOG_DEFAULT_CATEGORY(gras_msg); + +typedef void* gras_trp_bufdata_; + +void gras_msg_send_ext(gras_socket_t sock, + e_gras_msg_kind_t kind, + unsigned long int ID, + gras_msgtype_t msgtype, + void *payload) { +/* + + m_task_t task=NULL; + gras_trp_sg_sock_data_t *sock_data = (gras_trp_sg_sock_data_t *)sock->data; + gras_msg_t msg; + int whole_payload_size=0;*/ /* msg->payload_size is used to memcpy the payload. + This is used to report the load onto the simulator. It also counts the size of pointed stuff */ + /* + char *name; + + xbt_assert1(!gras_socket_is_meas(sock), + "Asked to send a message on the measurement socket %p", sock); + + msg=xbt_new0(s_gras_msg_t,1); + msg->type=msgtype; + msg->ID = ID; + + if (kind == e_gras_msg_kind_rpcerror) { + */ /* error on remote host, carfull, payload is an exception */ + /*msg->payl_size=gras_datadesc_size(gras_datadesc_by_name("ex_t")); + msg->payl=xbt_malloc(msg->payl_size); + whole_payload_size = gras_datadesc_copy(gras_datadesc_by_name("ex_t"), + payload,msg->payl); + } else if (kind == e_gras_msg_kind_rpcanswer) { + msg->payl_size=gras_datadesc_size(msgtype->answer_type); + msg->payl=xbt_malloc(msg->payl_size); + if (msgtype->answer_type) + whole_payload_size = gras_datadesc_copy(msgtype->answer_type, + payload, msg->payl); + } else { + msg->payl_size=gras_datadesc_size(msgtype->ctn_type); + msg->payl=msg->payl_size?xbt_malloc(msg->payl_size):NULL; + if (msgtype->ctn_type) + whole_payload_size = gras_datadesc_copy(msgtype->ctn_type, + payload, msg->payl); + } + + msg->kind = kind; + + if (XBT_LOG_ISENABLED(gras_msg,xbt_log_priority_verbose)) { + asprintf(&name,"type:'%s';kind:'%s';ID %lu from %s:%d to %s:%d", + msg->type->name, e_gras_msg_kind_names[msg->kind], msg->ID, + gras_os_myname(),gras_os_myport(), + gras_socket_peer_name(sock), gras_socket_peer_port(sock)); + task=MSG_task_create(name,0, + ((double)whole_payload_size),msg); + free(name); + } else { + task=MSG_task_create(msg->type->name,0, + ((double)whole_payload_size),msg); + } + + sock->bufdata = msg; + SIMIX_cond_signal(gras_libdata_by_name_from_remote("trp", sock->data->to_process)->cond); + DEBUG1("Prepare to send a message to %s", + MSG_host_get_name (sock_data->to_host)); + if (MSG_task_put_with_timeout(task, sock_data->to_host,sock_data->to_chan,60.0) != MSG_OK) + THROW0(system_error,0,"Problem during the MSG_task_put with timeout 60"); + + VERB5("Sent to %s(%d) a message type '%s' kind '%s' ID %lu", + MSG_host_get_name(sock_data->to_host),sock_data->to_PID, + msg->type->name, + e_gras_msg_kind_names[msg->kind], + msg->ID); +*/ +} +/* + * receive the next message on the given socket. + */ +void +gras_msg_recv(gras_socket_t sock, + gras_msg_t msg) { +/* + m_task_t task=NULL; + s_gras_msg_t msg_got; + gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_by_name("gras_trp"); + gras_msg_procdata_t msg = (gras_msg_procdata_t)gras_libdata_by_name("gras_msg"); + xbt_assert1(!gras_socket_is_meas(sock), + "Asked to receive a message on the measurement socket %p", sock); + + xbt_assert0(msg,"msg is an out parameter of gras_msg_recv..."); + + if (xbt_dynar_length(msg->msg_queue) == 0 ) { + THROW_IMPOSSIBLE; + } + xbt_dynar_shift(msg->msg_queue,&msg_got); + +// if (MSG_task_get(&task, pd->chan) != MSG_OK) +// THROW0(system_error,0,"Error in MSG_task_get()"); + + msg_got=MSG_task_get_data(task); + + + msg_got->expe= msg->expe; + memcpy(msg,msg_got,sizeof(s_gras_msg_t)); + + free(msg_got); + if (MSG_task_destroy(task) != MSG_OK) + THROW0(system_error,0,"Error in MSG_task_destroy()"); + + VERB3("Received a message type '%s' kind '%s' ID %lu",// from %s", + msg->type->name, + e_gras_msg_kind_names[msg->kind], + msg->ID); + */ +} diff --git a/src/gras_simix/Msg/gras_simix_timer.c b/src/gras_simix/Msg/gras_simix_timer.c new file mode 100644 index 0000000000..616250284a --- /dev/null +++ b/src/gras_simix/Msg/gras_simix_timer.c @@ -0,0 +1,175 @@ +/* $Id$ */ + +/* timer - Delayed and repetitive actions */ + +/* Copyright (c) 2005 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "xbt/ex.h" +#include "gras/Msg/msg_private.h" +#include "gras/timer.h" +#include "gras/Virtu/virtu_interface.h" + + +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_timer,gras, + "Delayed and repetitive actions"); + +/** @brief Request \a action to be called once in \a delay seconds */ +void gras_timer_delay(double delay, void_f_void_t action) { + gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id); + + gras_timer_t timer = xbt_dynar_push_ptr(pd->timers); + + VERB1("Register delayed action %p", action); + timer->period = delay; + timer->expiry = delay+gras_os_time(); + timer->action = action; + timer->repeat = FALSE; +} + +/** @brief Request \a action to be called every \a interval seconds */ +void gras_timer_repeat(double interval, void_f_void_t action) { + gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id); + + gras_timer_t timer = xbt_dynar_push_ptr(pd->timers); + + VERB1("Register repetitive action %p", action); + timer->period = interval; + timer->expiry = interval+gras_os_time(); + timer->action = action; + timer->repeat = TRUE; +} + +/** @brief Cancel a delayed task */ +void gras_timer_cancel_delay(double interval, void_f_void_t action) { + gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id); + int cursor,found; + s_gras_timer_t timer; + + found = FALSE; + xbt_dynar_foreach(pd->timers,cursor,timer){ + if (timer.repeat == FALSE && + timer.period == interval && + timer.action == action) { + + found = TRUE; + xbt_dynar_cursor_rm(pd->timers, &cursor); + } + } + + if (!found) + THROW2(mismatch_error,0,"Cannot remove the action %p delayed of %f second: not found", + action,interval); + +} + +/** @brief Cancel a repetitive task */ +void gras_timer_cancel_repeat(double interval, void_f_void_t action) { + gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id); + int cursor,found; + s_gras_timer_t timer; + + found = FALSE; + xbt_dynar_foreach(pd->timers,cursor,timer){ + if (timer.repeat == TRUE && + timer.period == interval && + timer.action == action) { + + found = TRUE; + xbt_dynar_cursor_rm(pd->timers, &cursor); + } + } + + if (!found) + THROW2(mismatch_error,0,"Cannot remove the action %p delayed of %f second: not found", + action,interval); +} + +/** @brief Cancel all delayed tasks */ +void gras_timer_cancel_delay_all(void) { + gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id); + int cursor, found; + s_gras_timer_t timer; + + found = FALSE; + xbt_dynar_foreach(pd->timers,cursor,timer){ + if (timer.repeat == FALSE) { + + found = TRUE; + xbt_dynar_cursor_rm(pd->timers, &cursor); + } + } + + if (!found) + THROW0(mismatch_error,0,"No delayed action to remove"); + +} + +/** @brief Cancel all repetitive tasks */ +void gras_timer_cancel_repeat_all(void){ + gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id); + int cursor, found; + s_gras_timer_t timer; + + found = FALSE; + xbt_dynar_foreach(pd->timers,cursor,timer){ + if (timer.repeat == FALSE) { + + found = TRUE; + xbt_dynar_cursor_rm(pd->timers, &cursor); + } + } + + if (!found) + THROW0(mismatch_error,0,"No repetitive action to remove"); +} + +/** @brief Cancel all delayed and repetitive tasks */ +void gras_timer_cancel_all(void) { + gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id); + xbt_dynar_reset( pd->timers ); +} + + +/* returns 0 if it handled a timer, or the delay until next timer, or -1 if no armed timer */ +double gras_msg_timer_handle(void) { + gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id); + int cursor; + gras_timer_t timer; + double now=gras_os_time(); + double untilnext = -1.0; + + for (cursor=0; cursor < xbt_dynar_length(pd->timers); cursor++) { + double untilthis; + + timer = xbt_dynar_get_ptr (pd->timers, cursor); + untilthis = timer->expiry - now; + + DEBUG2("Action %p expires in %f", timer->action, untilthis); + + if (untilthis <= 0.0) { + void_f_void_t *action = timer->action; + + DEBUG5("[%.0f] Serve %s action %p (%f<%f)",gras_os_time(), + timer->repeat ? "repetitive" : "delayed", timer->action, + timer->expiry, now); + + if (timer->repeat) { + timer->expiry = now + timer->period; + DEBUG4("[%.0f] Re-arm repetitive action %p for %f (period=%f)", + gras_os_time(), + timer->action, timer->expiry, timer->period); + } else { + DEBUG2("[%.0f] Remove %p now that it's done", gras_os_time(), timer->action); + xbt_dynar_cursor_rm(pd->timers, &cursor); + } + action(); + return 0.0; + } else if (untilthis < untilnext || untilnext == -1) { + untilnext = untilthis; + } + } + return untilnext; +} diff --git a/src/gras_simix/Transport/gras_simix_rl_transport.c b/src/gras_simix/Transport/gras_simix_rl_transport.c new file mode 100644 index 0000000000..7d7c7a413c --- /dev/null +++ b/src/gras_simix/Transport/gras_simix_rl_transport.c @@ -0,0 +1,226 @@ +/* $Id$ */ + +/* rl_transport - RL specific functions for transport */ + +/* Copyright (c) 2004 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "xbt/ex.h" +#include "portable.h" +#include "gras/Transport/transport_private.h" +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_trp); + +/* check transport_private.h for an explanation of this variable */ +gras_socket_t _gras_lastly_selected_socket = NULL; + +/** + * gras_trp_select: + * + * Returns the next socket to service because it receives a message. + * + * if timeout<0, we ought to implement the adaptative timeout (FIXME) + * + * if timeout=0, do not wait for new message, only handle the ones already there. + * + * if timeout>0 and no message there, wait at most that amount of time before giving up. + */ +gras_socket_t gras_trp_select(double timeout) { + xbt_dynar_t sockets= ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->sockets; + int done = -1; + double wakeup = gras_os_time() + timeout; + double now = 0; + /* nextToService used to make sure socket with high number do not starve */ + /* static int nextToService = 0; */ + struct timeval tout, *p_tout; + + int max_fds=0; /* first arg of select: number of existing sockets */ + /* but accept() of winsock returns sockets bigger than the limit, so don't bother + with this tiny optimisation on BillWare */ + fd_set FDS; + int ready; /* return of select: number of socket ready to be serviced */ + static int fd_setsize=-1; /* FD_SETSIZE not always defined. Get this portably */ + + gras_socket_t sock_iter; /* iterating over all sockets */ + int cursor; /* iterating over all sockets */ + + /* Check whether there is more data to read from the socket we selected last time. + This can happen with tcp buffered sockets since we try to get as much data as we can for them */ + if (_gras_lastly_selected_socket && _gras_lastly_selected_socket->moredata) { + VERB0("Returning _gras_lastly_selected_socket since there is more data on it"); + return _gras_lastly_selected_socket; + } + + /* Compute FD_SETSIZE on need */ + if (fd_setsize < 0) { +#ifdef HAVE_SYSCONF + fd_setsize = sysconf( _SC_OPEN_MAX ); +#else +# ifdef HAVE_GETDTABLESIZE + fd_setsize = getdtablesize(); +# else + fd_setsize = FD_SETSIZE; +# endif /* !USE_SYSCONF */ +#endif + } + + while (done == -1) { + if (timeout > 0) { /* did we timeout already? */ + now = gras_os_time(); + DEBUG2("wakeup=%f now=%f",wakeup, now); + if (now == -1 || now >= wakeup) { + /* didn't find anything; no need to update _gras_lastly_selected_socket since its moredata is 0 (or we would have returned it directly) */ + THROW1(timeout_error,0, + "Timeout (%f) elapsed with selecting for incomming connexions", + timeout); + } + } + + /* construct the set of socket to ear from */ + FD_ZERO(&FDS); + max_fds = -1; + xbt_dynar_foreach(sockets,cursor,sock_iter) { + if (!sock_iter->valid) + continue; + + if (sock_iter->incoming) { + DEBUG1("Considering socket %d for select",sock_iter->sd); +#ifndef HAVE_WINSOCK_H + if (max_fds < sock_iter->sd) + max_fds = sock_iter->sd; +#else + max_fds = 0; + +#endif + FD_SET(sock_iter->sd, &FDS); + } else { + DEBUG1("Not considering socket %d for select",sock_iter->sd); + } + } + + if (max_fds == -1) { + if (timeout > 0) { + DEBUG1("No socket to select onto. Sleep %f sec instead.",timeout); + gras_os_sleep(timeout); + THROW1(timeout_error,0,"No socket to select onto. Sleep %f sec instead",timeout); + } else { + DEBUG0("No socket to select onto. Return directly."); + THROW0(timeout_error,0, "No socket to select onto. Return directly."); + } + } + +#ifndef HAVE_WINSOCK_H + /* we cannot have more than FD_SETSIZE sockets + ... but with WINSOCK which returns sockets higher than the limit (killing this optim) */ + if (++max_fds > fd_setsize && fd_setsize > 0) { + WARN1("too many open sockets (%d).",max_fds); + done = 0; + break; + } +#else + max_fds = fd_setsize; +#endif + + if (timeout > 0) { + /* set the timeout */ + tout.tv_sec = (unsigned long)(wakeup - now); + tout.tv_usec = ((wakeup -now) - ((unsigned long)(wakeup - now))) * 1000000; + p_tout = &tout; + } else if (timeout == 0) { + /* polling only */ + tout.tv_sec = 0; + tout.tv_usec = 0; + p_tout = &tout; + /* we just do one loop around */ + done = 0; + } else { + /* no timeout: good luck! */ + p_tout = NULL; + } + + DEBUG2("Selecting over %d socket(s); timeout=%f", max_fds-1,timeout); + ready = select(max_fds, &FDS, NULL, NULL, p_tout); + DEBUG1("select returned %d", ready); + if (ready == -1) { + switch (errno) { + case EINTR: /* a signal we don't care about occured. we don't care */ + /* if we cared, we would have set an handler */ + continue; + case EINVAL: /* invalid value */ + THROW3(system_error,EINVAL,"invalid select: nb fds: %d, timeout: %d.%d", + max_fds, (int)tout.tv_sec,(int) tout.tv_usec); + case ENOMEM: + xbt_assert0(0,"Malloc error during the select"); + default: + THROW2(system_error,errno,"Error during select: %s (%d)", + strerror(errno),errno); + } + THROW_IMPOSSIBLE; + } else if (ready == 0) { + continue; /* this was a timeout */ + } + + xbt_dynar_foreach(sockets,cursor,sock_iter) { + if(!FD_ISSET(sock_iter->sd, &FDS)) { /* this socket is not ready */ + continue; + } + + /* Got a socket to serve */ + ready--; + + if ( sock_iter->accepting + && sock_iter->plugin->socket_accept) { + /* not a socket but an ear. accept on it and serve next socket */ + gras_socket_t accepted=NULL; + + accepted = (sock_iter->plugin->socket_accept)(sock_iter); + DEBUG2("accepted=%p,&accepted=%p",accepted,&accepted); + accepted->meas = sock_iter->meas; + + } else if (sock_iter->recv_ok) { + /* Make sure the socket is still alive by reading the first byte */ + char lookahead; + int recvd; + + recvd = recv(sock_iter->sd, &lookahead, 1, MSG_PEEK); + if (recvd < 0) { + WARN2("socket %d failed: %s", sock_iter->sd, strerror(errno)); + /* done with this socket */ + gras_socket_close(sock_iter); + cursor--; + } else if (recvd == 0) { + /* Connection reset (=closed) by peer. */ + DEBUG1("Connection %d reset by peer", sock_iter->sd); + sock_iter->valid=0; /* don't close it. User may keep references to it */ + } else { + /* Got a suited socket ! */ + XBT_OUT; + _gras_lastly_selected_socket = sock_iter; + return sock_iter; + } + + } else { + /* This is a file socket. Cannot recv() on it, but it must be alive */ + XBT_OUT; + _gras_lastly_selected_socket = sock_iter; + return sock_iter; + } + + + /* if we're here, the socket we found wasn't really ready to be served */ + if (ready == 0) /* exausted all sockets given by select. Request new ones */ + break; + } + + } + + /* No socket found. Maybe we had timeout=0 and nothing to do */ + DEBUG0("TIMEOUT"); + THROW0(timeout_error,0,"Timeout"); +} + +void gras_trp_sg_setup(gras_trp_plugin_t plug) { + THROW0(mismatch_error,0,"No SG transport on live platforms"); +} + diff --git a/src/gras_simix/Transport/gras_simix_sg_transport.c b/src/gras_simix/Transport/gras_simix_sg_transport.c new file mode 100644 index 0000000000..b316d32f09 --- /dev/null +++ b/src/gras_simix/Transport/gras_simix_sg_transport.c @@ -0,0 +1,152 @@ +/* $Id$ */ + +/* sg_transport - SG specific functions for transport */ + +/* Copyright (c) 2004 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "xbt/ex.h" +#include "gras_simix/Transport/gras_simix_transport_private.h" +//#include "msg/msg.h" +#include "gras_simix/Virtu/gras_simix_virtu_sg.h" + +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_trp); + +/* check transport_private.h for an explanation of this variable; this just need to be defined to NULL in SG */ +gras_socket_t _gras_lastly_selected_socket = NULL; + +/** + * gras_trp_select: + * + * Returns the next socket to service having a message awaiting. + * + * if timeout<0, we ought to implement the adaptative timeout (FIXME) + * + * if timeout=0, do not wait for new message, only handle the ones already there. + * + * if timeout>0 and no message there, wait at most that amount of time before giving up. + */ +gras_socket_t gras_trp_select(double timeout) { + gras_socket_t res; + res = xbt_new(s_gras_socket_t,1); + gras_trp_procdata_t pd = (gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id); + gras_trp_sg_sock_data_t *sockdata; + gras_trp_plugin_t trp; + gras_socket_t sock_iter; /* iterating over all sockets */ + int cursor; + + //gras_sg_portrec_t pr; /* iterating to find the chanel of expeditor */ +// int r_pid; + gras_hostdata_t *remote_hd; + + SIMIX_mutex_lock(pd->mutex); + DEBUG3("select on %s@%s with timeout=%f", + SIMIX_process_get_name(SIMIX_process_self()), + SIMIX_host_get_name(SIMIX_host_self()), + timeout); + //MSG_channel_select_from((m_channel_t) pd->chan, timeout, &r_pid); + SIMIX_cond_wait_timeout(pd->cond,pd->mutex,timeout); + + if (pd->active_socket == NULL) { + DEBUG0("TIMEOUT"); + THROW0(timeout_error,0,"Timeout"); + } +/* if (r_pid < 0) { + DEBUG0("TIMEOUT"); + THROW0(timeout_error,0,"Timeout"); + }*/ + + /* Ok, got something. Open a socket back to the expeditor */ + + /* Try to reuse an already openned socket to that expeditor */ + xbt_dynar_foreach(pd->sockets,cursor,sock_iter) { + DEBUG1("Consider %p as outgoing socket to expeditor",sock_iter); + + if (sock_iter->meas || !sock_iter->outgoing) + continue; + if (sock_iter->peer_port == pd->active_socket->port) + return sock_iter; + + } + + /* Socket to expeditor not created yet */ + DEBUG0("Create a socket to the expeditor"); + + trp = gras_trp_plugin_get_by_name("sg"); + + gras_trp_socket_new(1,&res); + res->plugin = trp; + + res->incoming = 1; + res->outgoing = 1; + res->accepting = 0; + res->sd = -1; + + res->port = -1; + + sockdata = xbt_new(gras_trp_sg_sock_data_t,1); + //sockdata->from_PID = MSG_process_self_PID(); + //sockdata->to_PID = r_pid; + sockdata->from_process = SIMIX_process_self(); + sockdata->to_process = ((gras_trp_sg_sock_data_t*)(pd->active_socket->data))->from_process; + + /* complicated*/ + sockdata->to_host = SIMIX_process_get_host(((gras_trp_sg_sock_data_t*)(pd->active_socket->data))->from_process); + res->data = sockdata; + gras_trp_buf_init_sock(res); + + res->peer_name = strdup(SIMIX_host_get_name(sockdata->to_host)); + + remote_hd=(gras_hostdata_t *)SIMIX_host_get_data(sockdata->to_host); + xbt_assert0(remote_hd,"Run gras_process_init!!"); + + //sockdata->to_chan = -1; + res->peer_port = pd->active_socket->port; + /* + for (cursor=0; cursorproc[cursor] == r_pid) { + sockdata->to_chan = cursor; + DEBUG2("Chan %d on %s is for my pal", + cursor,res->peer_name); + + xbt_dynar_foreach(remote_hd->ports, cpt, pr) { + if (sockdata->to_chan == pr.tochan) { + if (pr.meas) { + DEBUG0("Damn, it's for measurement"); + continue; + } + + res->peer_port = pr.port; + DEBUG1("Cool, it points to port %d", pr.port); + break; + } else { + DEBUG2("Wrong port (tochan=%d, looking for %d)\n", + pr.tochan,sockdata->to_chan); + } + } + if (res->peer_port == -10) { + sockdata->to_chan = -1; + } else { + break; + } + } + } + */ + + DEBUG1("New socket: Peer port %d", res->peer_port); + pd->active_socket=NULL; + SIMIX_mutex_unlock(pd->mutex); + return res; +} + + +/* dummy implementations of the functions used in RL mode */ + +void gras_trp_tcp_setup(gras_trp_plugin_t plug) { THROW0(mismatch_error,0,NULL); } +void gras_trp_file_setup(gras_trp_plugin_t plug){ THROW0(mismatch_error,0,NULL); } +void gras_trp_iov_setup(gras_trp_plugin_t plug) { THROW0(mismatch_error,0,NULL); } + +gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock) { return sock;} + diff --git a/src/gras_simix/Transport/gras_simix_transport.c b/src/gras_simix/Transport/gras_simix_transport.c new file mode 100644 index 0000000000..134b418420 --- /dev/null +++ b/src/gras_simix/Transport/gras_simix_transport.c @@ -0,0 +1,620 @@ +/* $Id$ */ + +/* transport - low level communication */ + +/* Copyright (c) 2004 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +/*** + *** Options + ***/ +int gras_opt_trp_nomoredata_on_close=0; + +#include "xbt/ex.h" +#include "xbt/peer.h" +#include "portable.h" +#include "gras/Transport/transport_private.h" + +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_trp,gras,"Conveying bytes over the network"); +XBT_LOG_NEW_SUBCATEGORY(gras_trp_meas,gras_trp,"Conveying bytes over the network without formating for perf measurements"); +static short int _gras_trp_started = 0; + +static xbt_dict_t _gras_trp_plugins; /* All registered plugins */ +static void gras_trp_plugin_free(void *p); /* free one of the plugins */ + +static void +gras_trp_plugin_new(const char *name, gras_trp_setup_t setup) { + xbt_ex_t e; + + gras_trp_plugin_t plug = xbt_new0(s_gras_trp_plugin_t, 1); + + DEBUG1("Create plugin %s",name); + + plug->name=xbt_strdup(name); + + TRY { + setup(plug); + } CATCH(e) { + if (e.category == mismatch_error) { + /* SG plugin raise mismatch when in RL mode (and vice versa) */ + free(plug->name); + free(plug); + plug=NULL; + xbt_ex_free(e); + } else { + RETHROW; + } + } + + if (plug) + xbt_dict_set(_gras_trp_plugins, name, plug, gras_trp_plugin_free); +} + +void gras_trp_init(void){ + if (!_gras_trp_started) { + /* make room for all plugins */ + _gras_trp_plugins=xbt_dict_new(); + +#ifdef HAVE_WINSOCK2_H + /* initialize the windows mechanism */ + { + WORD wVersionRequested; + WSADATA wsaData; + + wVersionRequested = MAKEWORD( 2, 0 ); + xbt_assert0(WSAStartup( wVersionRequested, &wsaData ) == 0, + "Cannot find a usable WinSock DLL"); + + /* Confirm that the WinSock DLL supports 2.0.*/ + /* Note that if the DLL supports versions greater */ + /* than 2.0 in addition to 2.0, it will still return */ + /* 2.0 in wVersion since that is the version we */ + /* requested. */ + + xbt_assert0(LOBYTE( wsaData.wVersion ) == 2 && + HIBYTE( wsaData.wVersion ) == 0, + "Cannot find a usable WinSock DLL"); + INFO0("Found and initialized winsock2"); + } /* The WinSock DLL is acceptable. Proceed. */ +#elif HAVE_WINSOCK_H + { WSADATA wsaData; + xbt_assert0(WSAStartup( 0x0101, &wsaData ) == 0, + "Cannot find a usable WinSock DLL"); + INFO0("Found and initialized winsock"); + } +#endif + + /* Add plugins */ + gras_trp_plugin_new("file",gras_trp_file_setup); + gras_trp_plugin_new("sg",gras_trp_sg_setup); + gras_trp_plugin_new("tcp", gras_trp_tcp_setup); + } + + _gras_trp_started++; +} + +void +gras_trp_exit(void){ + xbt_dynar_t sockets = ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->sockets; + gras_socket_t sock_iter; + int cursor; + + if (_gras_trp_started == 0) { + return; + } + + if ( --_gras_trp_started == 0 ) { +#ifdef HAVE_WINSOCK_H + if ( WSACleanup() == SOCKET_ERROR ) { + if ( WSAGetLastError() == WSAEINPROGRESS ) { + WSACancelBlockingCall(); + WSACleanup(); + } + } +#endif + + /* Close all the sockets */ + xbt_dynar_foreach(sockets,cursor,sock_iter) { + VERB1("Closing the socket %p left open on exit. Maybe a socket leak?", + sock_iter); + gras_socket_close(sock_iter); + } + + /* Delete the plugins */ + xbt_dict_free(&_gras_trp_plugins); + } +} + + +void gras_trp_plugin_free(void *p) { + gras_trp_plugin_t plug = p; + + if (plug) { + if (plug->exit) { + plug->exit(plug); + } else if (plug->data) { + DEBUG1("Plugin %s lacks exit(). Free data anyway.",plug->name); + free(plug->data); + } + + free(plug->name); + free(plug); + } +} + + +/** + * gras_trp_socket_new: + * + * Malloc a new socket, and initialize it with defaults + */ +void gras_trp_socket_new(int incoming, + gras_socket_t *dst) { + + gras_socket_t sock=xbt_new0(s_gras_socket_t,1); + + VERB1("Create a new socket (%p)", (void*)sock); + + sock->plugin = NULL; + + sock->incoming = incoming ? 1:0; + sock->outgoing = incoming ? 0:1; + sock->accepting = incoming ? 1:0; + sock->meas = 0; + sock->recv_ok = 1; + sock->valid = 1; + sock->moredata = 0; + + sock->sd = -1; + sock->port = -1; + sock->peer_port = -1; + sock->peer_name = NULL; + sock->peer_proc = NULL; + + sock->data = NULL; + sock->bufdata = NULL; + + *dst = sock; + + xbt_dynar_push(((gras_trp_procdata_t) + gras_libdata_by_id(gras_trp_libdata_id))->sockets,dst); + XBT_OUT; +} + +/** + * @brief Opens a server socket and makes it ready to be listened to. + * @param port: port on which you want to listen + * @param buf_size: size of the buffer (in byte) on the socket (for TCP sockets only). If 0, a sain default is used (32k, but may change) + * @param measurement: whether this socket is meant to convey measurement (if you don't know, use 0 to exchange regular messages) + * + * In real life, you'll get a TCP socket. + */ +gras_socket_t +gras_socket_server_ext(unsigned short port, + + unsigned long int buf_size, + int measurement) { + + xbt_ex_t e; + gras_trp_plugin_t trp; + gras_socket_t sock; + + DEBUG2("Create a server socket from plugin %s on port %d", + gras_if_RL() ? "tcp" : "sg", + port); + trp = gras_trp_plugin_get_by_name(gras_if_SG() ? "sg":"tcp"); + + /* defaults settings */ + gras_trp_socket_new(1,&sock); + sock->plugin= trp; + sock->port=port; + sock->buf_size = buf_size>0 ? buf_size : 32*1024; + sock->meas = measurement; + + /* Call plugin socket creation function */ + DEBUG1("Prepare socket with plugin (fct=%p)",trp->socket_server); + TRY { + trp->socket_server(trp, sock); + DEBUG3("in=%c out=%c accept=%c", + sock->incoming?'y':'n', + sock->outgoing?'y':'n', + sock->accepting?'y':'n'); + } CATCH(e) { + int cursor; + gras_socket_t sock_iter; + xbt_dynar_t socks = ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->sockets; + xbt_dynar_foreach(socks, cursor, sock_iter) { + if (sock_iter==sock) + xbt_dynar_cursor_rm(socks,&cursor); + } + free(sock); + RETHROW; + } + + if (!measurement) + ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->myport = port; + return sock; +} +/** + * @brief Opens a server socket on any port in the given range + * + * @param minport: first port we will try + * @param maxport: last port we will try + * @param buf_size: size of the buffer (in byte) on the socket (for TCP sockets only). If 0, a sain default is used (32k, but may change) + * @param measurement: whether this socket is meant to convey measurement (if you don't know, use 0 to exchange regular messages) + * + * If none of the provided ports works, raises the exception got when trying the last possibility + */ +gras_socket_t +gras_socket_server_range(unsigned short minport, unsigned short maxport, + unsigned long int buf_size, int measurement) { + + int port; + gras_socket_t res=NULL; + xbt_ex_t e; + + for (port=minport; portplugin= trp; + sock->peer_port = port; + sock->peer_name = (char*)strdup(host?host:"localhost"); + sock->buf_size = buf_size>0 ? buf_size: 32*1024; + sock->meas = measurement; + + /* plugin-specific */ + TRY { + (*trp->socket_client)(trp, sock); + DEBUG3("in=%c out=%c accept=%c", + sock->incoming?'y':'n', + sock->outgoing?'y':'n', + sock->accepting?'y':'n'); + } CATCH(e) { + free(sock); + RETHROW; + } + + return sock; +} + +/** + * gras_socket_server: + * + * Opens a server socket and make it ready to be listened to. + * In real life, you'll get a TCP socket. + */ +gras_socket_t +gras_socket_server(unsigned short port) { + return gras_socket_server_ext(port,32*1024,0); +} + +/** @brief Opens a client socket to a remote host */ +gras_socket_t +gras_socket_client(const char *host, + unsigned short port) { + return gras_socket_client_ext(host,port,0,0); +} + +/** @brief Opens a client socket to a remote host specified as '\a host:\a port' */ +gras_socket_t +gras_socket_client_from_string(const char *host) { + xbt_peer_t p = xbt_peer_from_string(host); + gras_socket_t res = gras_socket_client_ext(p->name,p->port,0,0); + xbt_peer_free(p); + return res; +} + +/** \brief Close socket */ +void gras_socket_close(gras_socket_t sock) { + xbt_dynar_t sockets = ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->sockets; + gras_socket_t sock_iter; + int cursor; + + XBT_IN; + VERB1("Close %p",sock); + if (sock == _gras_lastly_selected_socket) { + xbt_assert0(!gras_opt_trp_nomoredata_on_close || !sock->moredata, + "Closing a socket having more data in buffer while the nomoredata_on_close option is activated"); + + if (sock->moredata) + CRITICAL0("Closing a socket having more data in buffer. Option nomoredata_on_close disabled, so continuing."); + _gras_lastly_selected_socket=NULL; + } + + /* FIXME: Issue an event when the socket is closed */ + if (sock) { + xbt_dynar_foreach(sockets,cursor,sock_iter) { + if (sock == sock_iter) { + xbt_dynar_cursor_rm(sockets,&cursor); + if (sock->plugin->socket_close) + (* sock->plugin->socket_close)(sock); + + /* free the memory */ + if (sock->peer_name) + free(sock->peer_name); + free(sock); + XBT_OUT; + return; + } + } + WARN1("Ignoring request to free an unknown socket (%p). Execution stack:",sock); + xbt_backtrace_display(); + } + XBT_OUT; +} + +/** + * gras_trp_send: + * + * Send a bunch of bytes from on socket + * (stable if we know the storage will keep as is until the next trp_flush) + */ +void +gras_trp_send(gras_socket_t sd, char *data, long int size, int stable) { + xbt_assert0(sd->outgoing,"Socket not suited for data send"); + (*sd->plugin->send)(sd,data,size,stable); +} +/** + * gras_trp_chunk_recv: + * + * Receive a bunch of bytes from a socket + */ +void +gras_trp_recv(gras_socket_t sd, char *data, long int size) { + xbt_assert0(sd->incoming,"Socket not suited for data receive"); + (sd->plugin->recv)(sd,data,size); +} + +/** + * gras_trp_flush: + * + * Make sure all pending communications are done + */ +void +gras_trp_flush(gras_socket_t sd) { + if (sd->plugin->flush) + (sd->plugin->flush)(sd); +} + +gras_trp_plugin_t +gras_trp_plugin_get_by_name(const char *name){ + return xbt_dict_get(_gras_trp_plugins,name); +} + +int gras_socket_my_port (gras_socket_t sock) { + return sock->port; +} +int gras_socket_peer_port(gras_socket_t sock) { + return sock->peer_port; +} +char *gras_socket_peer_name(gras_socket_t sock) { + return sock->peer_name; +} +char *gras_socket_peer_proc(gras_socket_t sock) { + return sock->peer_proc; +} + +void gras_socket_peer_proc_set(gras_socket_t sock,char*peer_proc) { + sock->peer_proc = peer_proc; +} + +/** \brief Check if the provided socket is a measurement one (or a regular one) */ +int gras_socket_is_meas(gras_socket_t sock) { + return sock->meas; +} + +/** \brief Send a chunk of (random) data over a measurement socket + * + * @param peer measurement socket to use for the experiment + * @param timeout timeout (in seconds) + * @param msg_size size of each chunk sent over the socket (in bytes). + * @param msg_amount how many of these packets you want to send. + * + * Calls to gras_socket_meas_send() and gras_socket_meas_recv() on + * each side of the socket should be paired. + * + * The exchanged data is zeroed to make sure it's initialized, but + * there is no way to control what is sent (ie, you cannot use these + * functions to exchange data out of band). + * + * @warning: in SimGrid version 3.1 and previous, the numerical arguments + * were the total amount of data to send and the msg_size. This + * was changed for the fool wanting to send more than MAXINT + * bytes in a fat pipe. + */ +void gras_socket_meas_send(gras_socket_t peer, + unsigned int timeout, + unsigned long int msg_size, + unsigned long int msg_amount) { + char *chunk=NULL; + unsigned long int sent_sofar; + + XBT_IN; + + if (gras_if_RL()) + chunk=xbt_malloc0(msg_size); + + xbt_assert0(peer->meas,"Asked to send measurement data on a regular socket"); + xbt_assert0(peer->outgoing,"Socket not suited for data send (was created with gras_socket_server(), not gras_socket_client())"); + + for (sent_sofar=0; sent_sofar < msg_amount; sent_sofar++) { + CDEBUG5(gras_trp_meas,"Sent %lu msgs of %lu (size of each: %ld) to %s:%d", + sent_sofar,msg_amount,msg_size, + gras_socket_peer_name(peer), gras_socket_peer_port(peer)); + (*peer->plugin->raw_send)(peer,chunk,msg_size); + } + CDEBUG5(gras_trp_meas,"Sent %lu msgs of %lu (size of each: %ld) to %s:%d", + sent_sofar,msg_amount,msg_size, + gras_socket_peer_name(peer), gras_socket_peer_port(peer)); + + if (gras_if_RL()) + free(chunk); + + XBT_OUT; +} + +/** \brief Receive a chunk of data over a measurement socket + * + * Calls to gras_socket_meas_send() and gras_socket_meas_recv() on + * each side of the socket should be paired. + * + * @warning: in SimGrid version 3.1 and previous, the numerical arguments + * were the total amount of data to send and the msg_size. This + * was changed for the fool wanting to send more than MAXINT + * bytes in a fat pipe. + */ +void gras_socket_meas_recv(gras_socket_t peer, + unsigned int timeout, + unsigned long int msg_size, + unsigned long int msg_amount){ + + char *chunk=NULL; + unsigned long int got_sofar; + + XBT_IN; + + if (gras_if_RL()) + chunk = xbt_malloc(msg_size); + + xbt_assert0(peer->meas, + "Asked to receive measurement data on a regular socket"); + xbt_assert0(peer->incoming,"Socket not suited for data receive"); + + for (got_sofar=0; got_sofar < msg_amount; got_sofar ++) { + CDEBUG5(gras_trp_meas,"Recvd %ld msgs of %lu (size of each: %ld) from %s:%d", + got_sofar,msg_amount,msg_size, + gras_socket_peer_name(peer), gras_socket_peer_port(peer)); + (peer->plugin->raw_recv)(peer,chunk,msg_size); + } + CDEBUG5(gras_trp_meas,"Recvd %ld msgs of %lu (size of each: %ld) from %s:%d", + got_sofar,msg_amount,msg_size, + gras_socket_peer_name(peer), gras_socket_peer_port(peer)); + + if (gras_if_RL()) + free(chunk); + XBT_OUT; +} + +/** + * \brief Something similar to the good old accept system call. + * + * Make sure that there is someone speaking to the provided server socket. + * In RL, it does an accept(2) and return the result as last argument. + * In SG, as accepts are useless, it returns the provided argument as result. + * You should thus test whether (peer != accepted) before closing both of them. + * + * You should only call this on measurement sockets. It is automatically + * done for regular sockets, but you usually want more control about + * what's going on with measurement sockets. + */ +gras_socket_t gras_socket_meas_accept(gras_socket_t peer){ + gras_socket_t res; + + xbt_assert0(peer->meas, + "No need to accept on non-measurement sockets (it's automatic)"); + + if (!peer->accepting) { + /* nothing to accept here (must be in SG) */ + /* BUG: FIXME: this is BAD! it makes tricky to free the accepted socket*/ + return peer; + } + + res = (peer->plugin->socket_accept)(peer); + res->meas = peer->meas; + CDEBUG1(gras_trp_meas,"meas_accepted onto %d",res->sd); + + return res; +} + + +/* + * Creating procdata for this module + */ +static void *gras_trp_procdata_new() { + gras_trp_procdata_t res = xbt_new(s_gras_trp_procdata_t,1); + + res->name = xbt_strdup("gras_trp"); + res->name_len = 0; + res->sockets = xbt_dynar_new(sizeof(gras_socket_t*), NULL); + res->myport = 0; + + return (void*)res; +} + +/* + * Freeing procdata for this module + */ +static void gras_trp_procdata_free(void *data) { + gras_trp_procdata_t res = (gras_trp_procdata_t)data; + + xbt_dynar_free(&( res->sockets )); + free(res->name); + free(res); +} + +void gras_trp_socketset_dump(const char *name) { + gras_trp_procdata_t procdata = + (gras_trp_procdata_t)gras_libdata_by_id(gras_trp_libdata_id); + + int it; + gras_socket_t s; + + INFO1("** Dump the socket set %s",name); + xbt_dynar_foreach(procdata->sockets, it, s) { + INFO4(" %p -> %s:%d %s", + s,gras_socket_peer_name(s),gras_socket_peer_port(s), + s->valid?"(valid)":"(peer dead)"); + } + INFO1("** End of socket set %s",name); +} + +/* + * Module registration + */ +int gras_trp_libdata_id; +void gras_trp_register() { + gras_trp_libdata_id = gras_procdata_add("gras_trp",gras_trp_procdata_new, gras_trp_procdata_free); +} + +int gras_os_myport(void) { + return ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->myport; +} + diff --git a/src/gras_simix/Transport/gras_simix_transport_interface.h b/src/gras_simix/Transport/gras_simix_transport_interface.h new file mode 100644 index 0000000000..189f605871 --- /dev/null +++ b/src/gras_simix/Transport/gras_simix_transport_interface.h @@ -0,0 +1,119 @@ +/* $Id$ */ + +/* transport - low level communication (send/receive bunches of bytes) */ + +/* module's public interface exported within GRAS, but not to end user. */ + +/* Copyright (c) 2004 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#ifndef GRAS_TRP_INTERFACE_H +#define GRAS_TRP_INTERFACE_H + +#include "portable.h" /* sometimes needed for fd_set */ +#include "simix/simix.h" + +/*** + *** Options + ***/ +extern int gras_opt_trp_nomoredata_on_close; + +/*** + *** Main user functions + ***/ +/* stable if we know the storage will keep as is until the next trp_flush */ +XBT_PUBLIC(void) gras_trp_send(gras_socket_t sd, char *data, long int size, int stable); +XBT_PUBLIC(void) gras_trp_recv(gras_socket_t sd, char *data, long int size); +XBT_PUBLIC(void) gras_trp_flush(gras_socket_t sd); + +/* Find which socket needs to be read next */ +XBT_PUBLIC(gras_socket_t) gras_trp_select(double timeout); + +/* Set the peer process name (used by messaging layer) */ +XBT_PUBLIC(void) gras_socket_peer_proc_set(gras_socket_t sock,char*peer_proc); + +/*** + *** Plugin mechanism + ***/ + +/* A plugin type */ +typedef struct gras_trp_plugin_ s_gras_trp_plugin_t, *gras_trp_plugin_t; + +/* A plugin type */ +struct gras_trp_plugin_ { + char *name; + + /* dst pointers are created and initialized with default values + before call to socket_client/server. + Retrive the info you need from there. */ + void (*socket_client)(gras_trp_plugin_t self, + gras_socket_t dst); + void (*socket_server)(gras_trp_plugin_t self, + gras_socket_t dst); + + gras_socket_t (*socket_accept)(gras_socket_t from); + + + /* socket_close() is responsible of telling the OS that the socket is over, + but should not free the socket itself (beside the specific part) */ + void (*socket_close)(gras_socket_t sd); + + /* send/recv may be buffered */ + void (*send)(gras_socket_t sd, + const char *data, + unsigned long int size, + int stable /* storage will survive until flush*/); + int (*recv)(gras_socket_t sd, + char *data, + unsigned long int size); + /* raw_send/raw_recv is never buffered (use it for measurement stuff) */ + void (*raw_send)(gras_socket_t sd, + const char *data, + unsigned long int size); + int (*raw_recv)(gras_socket_t sd, + char *data, + unsigned long int size); + + /* flush has to make sure that the pending communications are achieved */ + void (*flush)(gras_socket_t sd); + + void *data; /* plugin-specific data */ + + /* exit is responsible for freeing data and telling the OS this plugin goes */ + /* exit=NULL, data gets freed. (ie exit function needed only when data contains pointers) */ + void (*exit)(gras_trp_plugin_t); +}; + +XBT_PUBLIC(gras_trp_plugin_t) +gras_trp_plugin_get_by_name(const char *name); + +/* Data of this module specific to each process + * (used by sg_process.c to cleanup the SG channel cruft) + */ +typedef struct { + /* set headers */ + long int pid; + long int ppid; + unsigned int ID; + char *name; + unsigned int name_len; + + xbt_dynar_t sockets; /* all sockets known to this process */ + int myport; /* Port on which I listen myself */ + fd_set *fdset; + + /* SG only elements. In RL, they are part of the OS ;) */ + smx_cond_t cond; + smx_mutex_t mutex; + gras_socket_t active_socket; + //int chan; /* Formated messages channel */ + //int measChan; /* Unformated echange channel for performance measurement*/ + +} s_gras_trp_procdata_t,*gras_trp_procdata_t; + +/* Display the content of our socket set (debugging purpose) */ +XBT_PUBLIC(void) gras_trp_socketset_dump(const char *name); + +#endif /* GRAS_TRP_INTERFACE_H */ diff --git a/src/gras_simix/Transport/gras_simix_transport_plugin_file.c b/src/gras_simix/Transport/gras_simix_transport_plugin_file.c new file mode 100644 index 0000000000..1b559697a1 --- /dev/null +++ b/src/gras_simix/Transport/gras_simix_transport_plugin_file.c @@ -0,0 +1,250 @@ +/* $Id$ */ + +/* File transport - send/receive a bunch of bytes from a file */ + +/* Copyright (c) 2004 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "portable.h" +#include "transport_private.h" +#include "xbt/ex.h" + +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_trp_file,gras_trp, + "Pseudo-transport to write to/read from a file"); + +/*** + *** Prototypes + ***/ +void gras_trp_file_close(gras_socket_t sd); + +void gras_trp_file_chunk_send_raw(gras_socket_t sd, + const char *data, + unsigned long int size); +void gras_trp_file_chunk_send(gras_socket_t sd, + const char *data, + unsigned long int size, + int stable_ignored); + +int gras_trp_file_chunk_recv(gras_socket_t sd, + char *data, + unsigned long int size); + +/*** + *** Specific plugin part + ***/ + +typedef struct { + fd_set incoming_socks; +} gras_trp_file_plug_data_t; + +/*** + *** Specific socket part + ***/ + + + +/*** + *** Code + ***/ +void +gras_trp_file_setup(gras_trp_plugin_t plug) { + + gras_trp_file_plug_data_t *file = xbt_new(gras_trp_file_plug_data_t,1); + + FD_ZERO(&(file->incoming_socks)); + + plug->socket_close = gras_trp_file_close; + + plug->raw_send = gras_trp_file_chunk_send_raw; + plug->send = gras_trp_file_chunk_send; + + plug->raw_recv = plug->recv = gras_trp_file_chunk_recv; + + plug->data = (void*)file; +} + +/** + * gras_socket_client_from_file: + * + * Create a client socket from a file path. + * + * This only possible in RL, and is mainly for debugging. + */ +gras_socket_t +gras_socket_client_from_file(const char*path) { + gras_socket_t res; + + xbt_assert0(gras_if_RL(), + "Cannot use file as socket in the simulator"); + + gras_trp_socket_new(0,&res); + + res->plugin=gras_trp_plugin_get_by_name("file"); + + if (strcmp("-", path)) { + res->sd = open(path, O_WRONLY|O_CREAT | O_BINARY, S_IRUSR|S_IWUSR|S_IRGRP ); + + if ( res->sd < 0) { + THROW2(system_error,0, + "Cannot create a client socket from file %s: %s", + path, strerror(errno)); + } + } else { + res->sd = 1; /* stdout */ + } + + res->recv_ok=0; + DEBUG5("sock_client_from_file(%s): sd=%d in=%c out=%c accept=%c", + path, + res->sd, + res->incoming?'y':'n', + res->outgoing?'y':'n', + res->accepting?'y':'n'); + + return res; +} + +/** + * gras_socket_server_from_file: + * + * Create a server socket from a file path. + * + * This only possible in RL, and is mainly for debugging. + */ +gras_socket_t gras_socket_server_from_file(const char*path) { + gras_socket_t res; + + xbt_assert0(gras_if_RL(), + "Cannot use file as socket in the simulator"); + + gras_trp_socket_new(1,&res); + + res->plugin=gras_trp_plugin_get_by_name("file"); + + + if (strcmp("-", path)) { + res->sd = open(path, O_RDONLY | O_BINARY); + + if ( res->sd < 0) { + THROW2(system_error,0, + "Cannot create a server socket from file %s: %s", + path, strerror(errno)); + } + } else { + res->sd = 0; /* stdin */ + } + + DEBUG4("sd=%d in=%c out=%c accept=%c", + res->sd, + res->incoming?'y':'n', + res->outgoing?'y':'n', + res->accepting?'y':'n'); + + res->recv_ok=0; + return res; +} + +void gras_trp_file_close(gras_socket_t sock){ + gras_trp_file_plug_data_t *data; + + if (!sock) return; /* close only once */ + data=sock->plugin->data; + + if (sock->sd == 0) { + DEBUG0("Do not close stdin"); + } else if (sock->sd == 1) { + DEBUG0("Do not close stdout"); + } else { + DEBUG1("close file connection %d", sock->sd); + + /* forget about the socket */ + FD_CLR(sock->sd, &(data->incoming_socks)); + + /* close the socket */ + if(close(sock->sd) < 0) { + WARN2("error while closing file %d: %s", + sock->sd, strerror(errno)); + } + } +} + +/** + * gras_trp_file_chunk_send: + * + * Send data on a file pseudo-socket + */ +void +gras_trp_file_chunk_send(gras_socket_t sock, + const char *data, + unsigned long int size, + int stable_ignored) { + gras_trp_file_chunk_send_raw(sock,data,size); +} +void +gras_trp_file_chunk_send_raw(gras_socket_t sock, + const char *data, + unsigned long int size) { + + xbt_assert0(sock->outgoing, "Cannot write on client file socket"); + xbt_assert0(size >= 0, "Cannot send a negative amount of data"); + + while (size) { + int status = 0; + + DEBUG3("write(%d, %p, %ld);", sock->sd, data, (long int)size); + status = write(sock->sd, data, (long int)size); + + if (status == -1) { + THROW4(system_error,0,"write(%d,%p,%d) failed: %s", + sock->sd, data, (int)size, + strerror(errno)); + } + + if (status) { + size -= status; + data += status; + } else { + THROW0(system_error,0,"file descriptor closed"); + } + } +} +/** + * gras_trp_file_chunk_recv: + * + * Receive data on a file pseudo-socket. + */ +int +gras_trp_file_chunk_recv(gras_socket_t sock, + char *data, + unsigned long int size) { + + int got = 0; + + xbt_assert0(sock, "Cannot recv on an NULL socket"); + xbt_assert0(sock->incoming, "Cannot recv on client file socket"); + xbt_assert0(size >= 0, "Cannot receive a negative amount of data"); + + while (size) { + int status = 0; + + status = read(sock->sd, data+got, (long int)size); + DEBUG3("read(%d, %p, %ld);", sock->sd, data+got, size); + + if (status == -1) { + THROW4(system_error,0,"read(%d,%p,%d) failed: %s", + sock->sd, data+got, (int)size, + strerror(errno)); + } + + if (status) { + size -= status; + got += status; + } else { + THROW0(system_error,0,"file descriptor closed"); + } + } + return got; +} + diff --git a/src/gras_simix/Transport/gras_simix_transport_plugin_sg.c b/src/gras_simix/Transport/gras_simix_transport_plugin_sg.c new file mode 100644 index 0000000000..23c384e34b --- /dev/null +++ b/src/gras_simix/Transport/gras_simix_transport_plugin_sg.c @@ -0,0 +1,331 @@ +/* $Id$ */ + +/* file trp (transport) - send/receive a bunch of bytes in SG realm */ + +/* Note that this is only used to debug other parts of GRAS since message */ +/* exchange in SG realm is implemented directly without mimicing real life */ +/* This would be terribly unefficient. */ + +/* Copyright (c) 2004 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "xbt/ex.h" + +//#include "msg/msg.h" + +#include "gras_simix_transport_private.h" +#include "gras_simix/Virtu/gras_simix_virtu_sg.h" + +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_trp_sg,gras_trp,"SimGrid pseudo-transport"); + +/*** + *** Prototypes + ***/ + +/* retrieve the port record associated to a numerical port on an host */ +static void find_port(gras_hostdata_t *hd, int port, gras_sg_portrec_t *hpd); + + +void gras_trp_sg_socket_client(gras_trp_plugin_t self, + /* OUT */ gras_socket_t sock); +void gras_trp_sg_socket_server(gras_trp_plugin_t self, + /* OUT */ gras_socket_t sock); +void gras_trp_sg_socket_close(gras_socket_t sd); + +void gras_trp_sg_chunk_send_raw(gras_socket_t sd, + const char *data, + unsigned long int size); +void gras_trp_sg_chunk_send(gras_socket_t sd, + const char *data, + unsigned long int size, + int stable_ignored); + +int gras_trp_sg_chunk_recv(gras_socket_t sd, + char *data, + unsigned long int size); + +/*** + *** Specific plugin part + ***/ +typedef struct { + int placeholder; /* nothing plugin specific so far */ +} gras_trp_sg_plug_data_t; + + +/*** + *** Code + ***/ +static void find_port(gras_hostdata_t *hd, int port, + gras_sg_portrec_t *hpd) { + int cpt; + gras_sg_portrec_t pr; + + xbt_assert0(hd,"Please run gras_process_init on each process"); + + xbt_dynar_foreach(hd->ports, cpt, pr) { + if (pr.port == port) { + memcpy(hpd,&pr,sizeof(gras_sg_portrec_t)); + return; + } + } + THROW1(mismatch_error,0,"Unable to find any portrec for port #%d",port); +} + + +void +gras_trp_sg_setup(gras_trp_plugin_t plug) { + + gras_trp_sg_plug_data_t *data=xbt_new(gras_trp_sg_plug_data_t,1); + + plug->data = data; + + plug->socket_client = gras_trp_sg_socket_client; + plug->socket_server = gras_trp_sg_socket_server; + plug->socket_close = gras_trp_sg_socket_close; + + plug->raw_send = gras_trp_sg_chunk_send_raw; + plug->send = gras_trp_sg_chunk_send; + plug->raw_recv = plug->recv = gras_trp_sg_chunk_recv; + + plug->flush = NULL; /* nothing cached */ +} + +void gras_trp_sg_socket_client(gras_trp_plugin_t self, + /* OUT */ gras_socket_t sock){ + /* + xbt_ex_t e; + + smx_host_t peer; + gras_hostdata_t *hd; + gras_trp_sg_sock_data_t *data; + gras_sg_portrec_t pr; +*/ + /* make sure this socket will reach someone */ + /*if (!(peer=SIMIX_get_host_by_name(sock->peer_name))) + THROW1(mismatch_error,0,"Can't connect to %s: no such host.\n",sock->peer_name); + + if (!(hd=(gras_hostdata_t *)SIMIX_host_get_data(peer))) + THROW1(mismatch_error,0, + "can't connect to %s: no process on this host", + sock->peer_name); + + TRY { + find_port(hd,sock->peer_port,&pr); + } CATCH(e) { + if (e.category == mismatch_error) { + xbt_ex_free(e); + THROW2(mismatch_error,0, + "can't connect to %s:%d, no process listen on this port", + sock->peer_name,sock->peer_port); + } + RETHROW; + } + + if (pr.meas && !sock->meas) { + THROW2(mismatch_error,0, + "can't connect to %s:%d in regular mode, the process listen " + "in meas mode on this port",sock->peer_name,sock->peer_port); + } + if (!pr.meas && sock->meas) { + THROW2(mismatch_error,0, + "can't connect to %s:%d in meas mode, the process listen " + "in regular mode on this port",sock->peer_name,sock->peer_port); + } +*/ + /* create the socket */ + /*data = xbt_new(gras_trp_sg_sock_data_t,1); + //data->from_PID = gras_os_getpid(); + data->from_process = SIMIX_process_self(); + data->to_process = pr.process; + //data->to_PID = hd->proc[ pr.tochan ]; + data->to_port = pr; + data->to_host = peer; + //data->to_chan = pr.tochan; + + sock->data = data; + sock->incoming = 1; + + DEBUG6("%s (PID %d) connects in %s mode to %s:%d (to_PID=%d)", + SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid, + sock->meas?"meas":"regular", + sock->peer_name,sock->peer_port,data->to_PID); +*/ +} + +void gras_trp_sg_socket_server(gras_trp_plugin_t self, + gras_socket_t sock){ +/* + gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self()); + gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_by_id(gras_trp_libdata_id); + gras_sg_portrec_t pr; + gras_trp_sg_sock_data_t *data; + volatile int found; + + const char *host=SIMIX_host_get_name(SIMIX_host_self()); + + xbt_ex_t e; + + xbt_assert0(hd,"Please run gras_process_init on each process"); + + sock->accepting = 0;*/ /* no such nuisance in SG */ +/* + found = 0; + TRY { + find_port(hd,sock->port,&pr); + found = 1; + } CATCH(e) { + if (e.category == mismatch_error) + xbt_ex_free(e); + else + RETHROW; + } + + if (found) + THROW2(mismatch_error,0, + "can't listen on address %s:%d: port already in use.", + host,sock->port); + + //pr.tochan = sock->meas ? pd->measChan : pd->chan; + pr.port = sock->port; + pr.meas = sock->meas; + pr.process = SIMIX_process_self(); + xbt_dynar_push(hd->ports,&pr); + */ + /* Create the socket */ + /*data = xbt_new(gras_trp_sg_sock_data_t,1); + data->from_process = NULL; + data->to_PID = SIMIX_process_self();; + //data->to_process = gras_os_getpid(); + data->to_host = SIMIX_host_self(); + data->to_port = pr; + //data->to_chan = pd->chan; + + sock->data = data; + + VERB6("'%s' (%d) ears on %s:%d%s (%p)", + SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid(), + host,sock->port,sock->meas? " (mode meas)":"",sock); +*/ +} + +void gras_trp_sg_socket_close(gras_socket_t sock){ +/* + gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self()); + int cpt; + + gras_sg_portrec_t pr; + + XBT_IN1(" (sock=%p)",sock); + + if (!sock) return; + xbt_assert0(hd,"Please run gras_process_init on each process"); + + if (sock->data) + free(sock->data); + + if (sock->incoming && sock->port >= 0) {*/ + /* server mode socket. Unregister it from 'OS' tables */ +/* xbt_dynar_foreach(hd->ports, cpt, pr) { + DEBUG2("Check pr %d of %lu", cpt, xbt_dynar_length(hd->ports)); + if (pr.port == sock->port) { + xbt_dynar_cursor_rm(hd->ports, &cpt); + XBT_OUT; + return; + } + } + WARN2("socket_close called on the unknown incoming socket %p (port=%d)", + sock,sock->port); + } + XBT_OUT; +*/ +} + +typedef struct { + int size; + void *data; +} sg_task_data_t; + +void gras_trp_sg_chunk_send(gras_socket_t sock, + const char *data, + unsigned long int size, + int stable_ignored) { + gras_trp_sg_chunk_send_raw(sock,data,size); +} + +void gras_trp_sg_chunk_send_raw(gras_socket_t sock, + const char *data, + unsigned long int size) { + /* + m_task_t task=NULL; + static unsigned int count=0; + char name[256]; + gras_trp_sg_sock_data_t *sock_data = (gras_trp_sg_sock_data_t *)sock->data; + sg_task_data_t *task_data; + + xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets"); + + sprintf(name,"Chunk[%d]",count++); + + task_data=xbt_new(sg_task_data_t,1); + task_data->size = size; + if (data) { + task_data->data=(void*)xbt_malloc(size); + memcpy(task_data->data,data,size); + } else { + task_data->data = NULL; + } + + task=MSG_task_create(name,0,((double)size),task_data); + + DEBUG5("send chunk %s from %s to %s:%d (size=%ld)", + name, MSG_host_get_name(MSG_host_self()), + MSG_host_get_name(sock_data->to_host), sock_data->to_chan,size); + if (MSG_task_put_with_timeout(task, sock_data->to_host,sock_data->to_chan,60.0) != MSG_OK) { + THROW0(system_error,0,"Problem during the MSG_task_put with timeout 60"); + }*/ +} + +int gras_trp_sg_chunk_recv(gras_socket_t sock, + char *data, + unsigned long int size){ + /* + gras_trp_procdata_t pd=(gras_trp_procdata_t)gras_libdata_by_id(gras_trp_libdata_id); + + m_task_t task=NULL; + sg_task_data_t *task_data; + gras_trp_sg_sock_data_t *sock_data = sock->data; + + xbt_assert0(sock->meas, "SG chunk exchange shouldn't be used on non-measurement sockets"); + XBT_IN; + DEBUG4("recv chunk on %s -> %s:%d (size=%ld)", + MSG_host_get_name(sock_data->to_host), + MSG_host_get_name(MSG_host_self()), sock_data->to_chan, size); + if (MSG_task_get_with_time_out(&task, + (sock->meas ? pd->measChan : pd->chan), + 60) != MSG_OK) + THROW0(system_error,0,"Error in MSG_task_get()"); + DEBUG1("Got chuck %s",MSG_task_get_name(task)); + + task_data = MSG_task_get_data(task); + if (task_data->size != size) + THROW5(mismatch_error,0, + "Got %d bytes when %ld where expected (in %s->%s:%d)", + task_data->size, size, + MSG_host_get_name(sock_data->to_host), + MSG_host_get_name(MSG_host_self()), sock_data->to_chan); + if (data) + memcpy(data,task_data->data,size); + if (task_data->data) + free(task_data->data); + free(task_data); + + if (MSG_task_destroy(task) != MSG_OK) + THROW0(system_error,0,"Error in MSG_task_destroy()"); + + XBT_OUT; + return size;*/ + return 0; +} + diff --git a/src/gras_simix/Transport/gras_simix_transport_plugin_tcp.c b/src/gras_simix/Transport/gras_simix_transport_plugin_tcp.c new file mode 100644 index 0000000000..e5b5d4c466 --- /dev/null +++ b/src/gras_simix/Transport/gras_simix_transport_plugin_tcp.c @@ -0,0 +1,783 @@ +/* $Id$ */ + +/* buf trp (transport) - buffered transport using the TCP one */ + +/* Copyright (c) 2004 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include +#include /* memset */ + +#include "portable.h" +#include "xbt/misc.h" +#include "xbt/sysdep.h" +#include "xbt/ex.h" +#include "transport_private.h" + +/* FIXME maybe READV is sometime a good thing? */ +#undef HAVE_READV + +#ifdef HAVE_READV +#include +#endif + +#ifndef MIN +#define MIN(a,b) ((a)<(b)?(a):(b)) +#endif + +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_trp_tcp,gras_trp, + "TCP buffered transport"); + +/*** + *** Specific socket part + ***/ + +typedef enum { buffering_buf, buffering_iov } buffering_kind; + +typedef struct { + int size; + char *data; + int pos; /* for receive; not exchanged over the net */ +} gras_trp_buf_t; + + +struct gras_trp_bufdata_{ + int buffsize; + gras_trp_buf_t in_buf; + gras_trp_buf_t out_buf; + +#ifdef HAVE_READV + xbt_dynar_t in_buf_v; + xbt_dynar_t out_buf_v; +#endif + + buffering_kind in; + buffering_kind out; +}; + + +/*****************************/ +/****[ SOCKET MANAGEMENT ]****/ +/*****************************/ +/* we exchange port number on client side on socket creation, + so we need to be able to talk right now. */ +static XBT_INLINE void gras_trp_tcp_send(gras_socket_t sock, const char *data, + unsigned long int size); +static int gras_trp_tcp_recv(gras_socket_t sock, char *data, + unsigned long int size); + + +static int _gras_tcp_proto_number(void); + +static XBT_INLINE void gras_trp_sock_socket_client(gras_trp_plugin_t ignored, + gras_socket_t sock){ + + struct sockaddr_in addr; + struct hostent *he; + struct in_addr *haddr; + int size = sock->buf_size; + uint32_t myport = htonl(((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->myport); + + sock->incoming = 1; /* TCP sockets are duplex'ed */ + + sock->sd = socket (AF_INET, SOCK_STREAM, 0); + + if (sock->sd < 0) { + THROW1(system_error,0, "Failed to create socket: %s", sock_errstr(sock_errno)); + } + + if (setsockopt(sock->sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size)) || + setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size))) { + WARN1("setsockopt failed, cannot set buffer size: %s",sock_errstr(sock_errno)); + } + + he = gethostbyname (sock->peer_name); + if (he == NULL) { + THROW2(system_error,0, "Failed to lookup hostname %s: %s", + sock->peer_name, sock_errstr(sock_errno)); + } + + haddr = ((struct in_addr *) (he->h_addr_list)[0]); + + memset(&addr, 0, sizeof(struct sockaddr_in)); + memcpy (&addr.sin_addr, haddr, sizeof(struct in_addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons (sock->peer_port); + + if (connect (sock->sd, (struct sockaddr*) &addr, sizeof (addr)) < 0) { + tcp_close(sock->sd); + THROW3(system_error,0, + "Failed to connect socket to %s:%d (%s)", + sock->peer_name, sock->peer_port, sock_errstr(sock_errno)); + } + + gras_trp_tcp_send(sock,(char*)&myport,sizeof(uint32_t)); + DEBUG1("peerport sent to %d", sock->peer_port); + + VERB4("Connect to %s:%d (sd=%d, port %d here)", + sock->peer_name, sock->peer_port, sock->sd, sock->port); +} + +/** + * gras_trp_sock_socket_server: + * + * Open a socket used to receive messages. + */ +static XBT_INLINE void gras_trp_sock_socket_server(gras_trp_plugin_t ignored, + gras_socket_t sock){ + int size = sock->buf_size; + int on = 1; + struct sockaddr_in server; + + sock->outgoing = 1; /* TCP => duplex mode */ + + server.sin_port = htons((u_short)sock->port); + server.sin_addr.s_addr = INADDR_ANY; + server.sin_family = AF_INET; + if((sock->sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) + THROW1(system_error,0,"Socket allocation failed: %s", sock_errstr(sock_errno)); + + if (setsockopt(sock->sd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on))) + THROW1(system_error,0,"setsockopt failed, cannot condition the socket: %s", + sock_errstr(sock_errno)); + + if (setsockopt(sock->sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size)) || + setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size))) { + WARN1("setsockopt failed, cannot set buffer size: %s", + sock_errstr(sock_errno)); + } + + if (bind(sock->sd, (struct sockaddr *)&server, sizeof(server)) == -1) { + tcp_close(sock->sd); + THROW2(system_error,0,"Cannot bind to port %d: %s",sock->port, sock_errstr(sock_errno)); + } + + DEBUG2("Listen on port %d (sd=%d)",sock->port, sock->sd); + if (listen(sock->sd, 5) < 0) { + tcp_close(sock->sd); + THROW2(system_error,0,"Cannot listen on port %d: %s",sock->port,sock_errstr(sock_errno)); + } + + VERB2("Openned a server socket on port %d (sd=%d)",sock->port,sock->sd); +} + +static gras_socket_t gras_trp_sock_socket_accept(gras_socket_t sock) { + gras_socket_t res; + + struct sockaddr_in peer_in; + socklen_t peer_in_len = sizeof(peer_in); + + int sd; + int tmp_errno; + int size; + + int i = 1; + socklen_t s = sizeof(int); + + uint32_t hisport; + + XBT_IN; + gras_trp_socket_new(1,&res); + + sd = accept(sock->sd, (struct sockaddr *)&peer_in, &peer_in_len); + tmp_errno = sock_errno; + + if (sd == -1) { + gras_socket_close(sock); + THROW1(system_error,0, + "Accept failed (%s). Droping server socket.", sock_errstr(tmp_errno)); + } + + if (setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *)&i, s) + || setsockopt(sd, _gras_tcp_proto_number(), TCP_NODELAY, (char *)&i, s)) + THROW1(system_error,0,"setsockopt failed, cannot condition the socket: %s", + sock_errstr(tmp_errno)); + + res->buf_size = sock->buf_size; + size = sock->buf_size; + if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size)) + || setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size))) + WARN1("setsockopt failed, cannot set buffer size: %s", sock_errstr(tmp_errno)); + + res->plugin = sock->plugin; + res->incoming = sock->incoming; + res->outgoing = sock->outgoing; + res->accepting = 0; + res->sd = sd; + res->port = -1; + + gras_trp_tcp_recv(res,(char*)&hisport,sizeof(hisport)); + res->peer_port = ntohl(hisport); + DEBUG1("peerport %d received",res->peer_port); + + /* FIXME: Lock to protect inet_ntoa */ + if (((struct sockaddr *)&peer_in)->sa_family != AF_INET) { + res->peer_name = (char*)strdup("unknown"); + } else { + struct in_addr addrAsInAddr; + char *tmp; + + addrAsInAddr.s_addr = peer_in.sin_addr.s_addr; + + tmp = inet_ntoa(addrAsInAddr); + if (tmp != NULL) { + res->peer_name = (char*)strdup(tmp); + } else { + res->peer_name = (char*)strdup("unknown"); + } + } + + VERB3("Accepted from %s:%d (sd=%d)", res->peer_name,res->peer_port,sd); + + XBT_OUT; + return res; +} + +static void gras_trp_sock_socket_close(gras_socket_t sock){ + + if (!sock) return; /* close only once */ + + VERB1("close tcp connection %d", sock->sd); + + /* FIXME: no pipe in GRAS so far + if(!FD_ISSET(sd, &connectedPipes)) { + if(shutdown(sd, 2) < 0) { + GetNWSLock(&lock); + tmp_errno = errno; + ReleaseNWSLock(&lock); + + / * The other side may have beaten us to the reset. * / + if ((tmp_errno!=ENOTCONN) && (tmp_errno!=ECONNRESET)) { + WARN1("CloseSocket: shutdown error %d\n", tmp_errno); + } + } + } */ + + + /* close the socket */ + if(tcp_close(sock->sd) < 0) { + WARN3("error while closing tcp socket %d: %d (%s)\n", + sock->sd, sock_errno, sock_errstr(sock_errno)); + } + +} +/************************************/ +/****[ end of SOCKET MANAGEMENT ]****/ +/************************************/ + + +/************************************/ +/****[ UNBUFFERED DATA EXCHANGE ]****/ +/************************************/ +/* Temptation to merge this with file data exchange is great, + but doesn't work on BillWare (see tcp_write() in portable.h) */ +static XBT_INLINE void gras_trp_tcp_send(gras_socket_t sock, + const char *data, + unsigned long int size) { + + while (size) { + int status = 0; + + status = tcp_write(sock->sd, data, (size_t)size); + DEBUG3("write(%d, %p, %ld);", sock->sd, data, size); + + if (status < 0) { +#ifdef EWOULDBLOCK + if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) +#else + if (errno == EINTR || errno == EAGAIN) +#endif + continue; + + THROW4(system_error,0,"write(%d,%p,%ld) failed: %s", + sock->sd, data, size, + sock_errstr(sock_errno)); + } + + if (status) { + size -= status; + data += status; + } else { + THROW1(system_error,0,"file descriptor closed (%s)", + sock_errstr(sock_errno)); + } + } +} +static XBT_INLINE int +gras_trp_tcp_recv_withbuffer(gras_socket_t sock, + char *data, + unsigned long int size, + unsigned long int bufsize) { + + int got = 0; + + while (size>got) { + int status = 0; + + DEBUG5("read(%d, %p, %ld) got %d so far (%s)", + sock->sd, data+got, bufsize, got, + hexa_str((unsigned char*)data,got,0)); + status = tcp_read(sock->sd, data+got, (size_t)bufsize); + + if (status < 0) { + THROW7(system_error,0,"read(%d,%p,%d) from %s:%d failed: %s; got %d so far", + sock->sd, data+got, (int)size, + gras_socket_peer_name(sock),gras_socket_peer_port(sock), + sock_errstr(sock_errno), + got); + } + DEBUG2("Got %d more bytes (%s)",status,hexa_str((unsigned char*)data+got,status,0)); + + if (status) { + bufsize -= status; + got += status; + } else { + THROW1(system_error,0,"Socket closed by remote side (got %d bytes before this)", + got); + } + } + + return got; +} + +static int gras_trp_tcp_recv(gras_socket_t sock, + char *data, + unsigned long int size) { + return gras_trp_tcp_recv_withbuffer(sock,data,size,size); + +} +/*******************************************/ +/****[ end of UNBUFFERED DATA EXCHANGE ]****/ +/*******************************************/ + +/**********************************/ +/****[ BUFFERED DATA EXCHANGE ]****/ +/**********************************/ + +/* Make sure the data is sent */ +static void +gras_trp_bufiov_flush(gras_socket_t sock) { +#ifdef HAVE_READV + xbt_dynar_t vect; + int size; +#endif + gras_trp_bufdata_t *data=sock->bufdata; + XBT_IN; + + DEBUG0("Flush"); + if (data->out == buffering_buf) { + if (XBT_LOG_ISENABLED(gras_trp_tcp,xbt_log_priority_debug)) + hexa_print("chunk to send ", + (unsigned char *) data->out_buf.data,data->out_buf.size); + if ((data->out_buf.size - data->out_buf.pos) != 0) { + DEBUG3("Send the chunk (size=%d) to %s:%d",data->out_buf.size, + gras_socket_peer_name(sock),gras_socket_peer_port(sock)); + gras_trp_tcp_send(sock, data->out_buf.data, data->out_buf.size); + VERB1("Chunk sent (size=%d)",data->out_buf.size); + data->out_buf.size = 0; + } + } + +#ifdef HAVE_READV + if (data->out == buffering_iov) { + DEBUG0("Flush out iov"); + vect = sock->bufdata->out_buf_v; + if ((size = xbt_dynar_length(vect))) { + DEBUG1("Flush %d chunks out of this socket",size); + writev(sock->sd,xbt_dynar_get_ptr(vect,0),size); + xbt_dynar_reset(vect); + } + data->out_buf.size = 0; /* reset the buffer containing non-stable data */ + } + + if (data->in == buffering_iov) { + DEBUG0("Flush in iov"); + vect = sock->bufdata->in_buf_v; + if ((size = xbt_dynar_length(vect))) { + DEBUG1("Get %d chunks from of this socket",size); + readv(sock->sd,xbt_dynar_get_ptr(vect,0),size); + xbt_dynar_reset(vect); + } + } +#endif +} +static void +gras_trp_buf_send(gras_socket_t sock, + const char *chunk, + unsigned long int size, + int stable_ignored) { + + gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata; + int chunk_pos=0; + + XBT_IN; + + while (chunk_pos < size) { + /* size of the chunk to receive in that shot */ + long int thissize = min(size-chunk_pos,data->buffsize-data->out_buf.size); + DEBUG4("Set the chars %d..%ld into the buffer; size=%ld, ctn=(%s)", + (int)data->out_buf.size, + ((int)data->out_buf.size) + thissize -1, + size, + hexa_str((unsigned char*)chunk,thissize,0)); + + memcpy(data->out_buf.data + data->out_buf.size, chunk + chunk_pos, thissize); + + data->out_buf.size += thissize; + chunk_pos += thissize; + DEBUG4("New pos = %d; Still to send = %ld of %ld; ctn sofar=(%s)", + data->out_buf.size,size-chunk_pos,size,hexa_str((unsigned char*)chunk,chunk_pos,0)); + + if (data->out_buf.size == data->buffsize) /* out of space. Flush it */ + gras_trp_bufiov_flush(sock); + } + + XBT_OUT; +} + +static int +gras_trp_buf_recv(gras_socket_t sock, + char *chunk, + unsigned long int size) { + + gras_trp_bufdata_t *data=sock->bufdata; + long int chunk_pos = 0; + + XBT_IN; + + while (chunk_pos < size) { + /* size of the chunk to receive in that shot */ + long int thissize; + + if (data->in_buf.size == data->in_buf.pos) { /* out of data. Get more */ + + DEBUG2("Get more data (size=%d,bufsize=%d)", + (int)MIN(size-chunk_pos,data->buffsize), + (int)data->buffsize); + + + data->in_buf.size = + gras_trp_tcp_recv_withbuffer(sock, data->in_buf.data, + MIN(size-chunk_pos,data->buffsize), + data->buffsize); + + data->in_buf.pos=0; + } + + thissize = min(size-chunk_pos , data->in_buf.size - data->in_buf.pos); + memcpy(chunk+chunk_pos, data->in_buf.data + data->in_buf.pos, thissize); + + data->in_buf.pos += thissize; + chunk_pos += thissize; + DEBUG4("New pos = %d; Still to receive = %ld of %ld. Ctn so far=(%s)", + data->in_buf.pos,size - chunk_pos,size,hexa_str((unsigned char*)chunk,chunk_pos,0)); + } + /* indicate on need to the gras_select function that there is more to read on this socket so that it does not actually select */ + sock->moredata = (data->in_buf.size > data->in_buf.pos); + DEBUG1("There is %smore data",(sock->moredata?"":"no ")); + + XBT_OUT; + return chunk_pos; +} + +/*****************************************/ +/****[ end of BUFFERED DATA EXCHANGE ]****/ +/*****************************************/ + +/********************************/ +/****[ VECTOR DATA EXCHANGE ]****/ +/********************************/ +#ifdef HAVE_READV +static void +gras_trp_iov_send(gras_socket_t sock, + const char *chunk, + unsigned long int size, + int stable) { + struct iovec elm; + gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata; + + + DEBUG1("Buffer one chunk to be sent later (%s)", + hexa_str((char*)chunk,size,0)); + + elm.iov_len = (size_t)size; + + if (!stable) { + /* data storage won't last until flush. Save it in a buffer if we can */ + + if (size > data->buffsize-data->out_buf.size) { + /* buffer too small: + flush the socket, using data in its actual storage */ + elm.iov_base = (void*)chunk; + xbt_dynar_push(data->out_buf_v,&elm); + + gras_trp_bufiov_flush(sock); + return; + } else { + /* buffer big enough: + copy data into it, and chain it for upcoming writev */ + memcpy(data->out_buf.data + data->out_buf.size, chunk, size); + elm.iov_base = (void*)(data->out_buf.data + data->out_buf.size); + data->out_buf.size += size; + + xbt_dynar_push(data->out_buf_v,&elm); + } + + } else { + /* data storage stable. Chain it */ + + elm.iov_base = (void*)chunk; + xbt_dynar_push(data->out_buf_v,&elm); + } +} +static int +gras_trp_iov_recv(gras_socket_t sock, + char *chunk, + unsigned long int size) { + struct iovec elm; + + DEBUG0("Buffer one chunk to be received later"); + elm.iov_base = (void*)chunk; + elm.iov_len = (size_t)size; + xbt_dynar_push(sock->bufdata->in_buf_v,&elm); + + return size; +} + +#endif +/***************************************/ +/****[ end of VECTOR DATA EXCHANGE ]****/ +/***************************************/ + + +/*** + *** Prototypes of BUFFERED + ***/ + +void gras_trp_buf_socket_client(gras_trp_plugin_t self, + gras_socket_t sock); +void gras_trp_buf_socket_server(gras_trp_plugin_t self, + gras_socket_t sock); +gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock); + +void gras_trp_buf_socket_close(gras_socket_t sd); + + +gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock) { + gras_trp_bufdata_t *data=xbt_new(gras_trp_bufdata_t,1); + + data->buffsize = 100 * 1024 ; /* 100k */ + + data->in_buf.size = 0; + data->in_buf.data = xbt_malloc(data->buffsize); + data->in_buf.pos = 0; /* useless, indeed, since size==pos */ + + data->out_buf.size = 0; + data->out_buf.data = xbt_malloc(data->buffsize); + data->out_buf.pos = data->out_buf.size; + +#ifdef HAVE_READV + data->in_buf_v = data->out_buf_v = NULL; + data->in_buf_v=xbt_dynar_new(sizeof(struct iovec),NULL); + data->out_buf_v=xbt_dynar_new(sizeof(struct iovec),NULL); + data->out = buffering_iov; +#else + data->out = buffering_buf; +#endif + + data->in = buffering_buf; + + sock->bufdata = data; + return sock; +} + +/*** + *** Code + ***/ +void +gras_trp_tcp_setup(gras_trp_plugin_t plug) { + + plug->socket_client = gras_trp_buf_socket_client; + plug->socket_server = gras_trp_buf_socket_server; + plug->socket_accept = gras_trp_buf_socket_accept; + plug->socket_close = gras_trp_buf_socket_close; + +#ifdef HAVE_READV + plug->send = gras_trp_iov_send; +#else + plug->send = gras_trp_buf_send; +#endif + plug->recv = gras_trp_buf_recv; + + plug->raw_send = gras_trp_tcp_send; + plug->raw_recv = gras_trp_tcp_recv; + + plug->flush = gras_trp_bufiov_flush; + + plug->data = NULL; + plug->exit = NULL; +} + +void gras_trp_buf_socket_client(gras_trp_plugin_t self, + /* OUT */ gras_socket_t sock){ + + gras_trp_sock_socket_client(NULL,sock); + gras_trp_buf_init_sock(sock); +} + +/** + * gras_trp_buf_socket_server: + * + * Open a socket used to receive messages. + */ +void gras_trp_buf_socket_server(gras_trp_plugin_t self, + /* OUT */ gras_socket_t sock){ + + gras_trp_sock_socket_server(NULL,sock); + gras_trp_buf_init_sock(sock); +} + +gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock) { + return gras_trp_buf_init_sock(gras_trp_sock_socket_accept(sock)); +} + +void gras_trp_buf_socket_close(gras_socket_t sock){ + gras_trp_bufdata_t *data=sock->bufdata; + + if (data->in_buf.size!=data->in_buf.pos) { + WARN3("Socket closed, but %d bytes were unread (size=%d,pos=%d)", + data->in_buf.size - data->in_buf.pos, + data->in_buf.size, data->in_buf.pos); + } + if (data->in_buf.data) + free(data->in_buf.data); + + if (data->out_buf.size!=data->out_buf.pos) { + DEBUG2("Flush the socket before closing (in=%d,out=%d)", + data->in_buf.size, data->out_buf.size); + gras_trp_bufiov_flush(sock); + } + if (data->out_buf.data) + free(data->out_buf.data); + +#ifdef HAVE_READV + if (data->in_buf_v) { + if (xbt_dynar_length(data->in_buf_v)) + WARN0("Socket closed, but some bytes were unread"); + xbt_dynar_free(&data->in_buf_v); + } + if (data->out_buf_v) { + if (xbt_dynar_length(data->out_buf_v)) { + DEBUG0("Flush the socket before closing"); + gras_trp_bufiov_flush(sock); + } + xbt_dynar_free(&data->out_buf_v); + } +#endif + + free(data); + gras_trp_sock_socket_close(sock); +} + +/****************************/ +/****[ HELPER FUNCTIONS ]****/ +/****************************/ + +/* + * Returns the tcp protocol number from the network protocol data base. + * + * getprotobyname() is not thread safe. We need to lock it. + */ +static int _gras_tcp_proto_number(void) { + struct protoent *fetchedEntry; + static int returnValue = 0; + + if(returnValue == 0) { + fetchedEntry = getprotobyname("tcp"); + xbt_assert0(fetchedEntry, "getprotobyname(tcp) gave NULL"); + returnValue = fetchedEntry->p_proto; + } + + return returnValue; +} + +#ifdef HAVE_WINSOCK_H +#define RETSTR( x ) case x: return #x + +const char *gras_wsa_err2string( int err ) { + switch( err ) { + RETSTR( WSAEINTR ); + RETSTR( WSAEBADF ); + RETSTR( WSAEACCES ); + RETSTR( WSAEFAULT ); + RETSTR( WSAEINVAL ); + RETSTR( WSAEMFILE ); + RETSTR( WSAEWOULDBLOCK ); + RETSTR( WSAEINPROGRESS ); + RETSTR( WSAEALREADY ); + RETSTR( WSAENOTSOCK ); + RETSTR( WSAEDESTADDRREQ ); + RETSTR( WSAEMSGSIZE ); + RETSTR( WSAEPROTOTYPE ); + RETSTR( WSAENOPROTOOPT ); + RETSTR( WSAEPROTONOSUPPORT ); + RETSTR( WSAESOCKTNOSUPPORT ); + RETSTR( WSAEOPNOTSUPP ); + RETSTR( WSAEPFNOSUPPORT ); + RETSTR( WSAEAFNOSUPPORT ); + RETSTR( WSAEADDRINUSE ); + RETSTR( WSAEADDRNOTAVAIL ); + RETSTR( WSAENETDOWN ); + RETSTR( WSAENETUNREACH ); + RETSTR( WSAENETRESET ); + RETSTR( WSAECONNABORTED ); + RETSTR( WSAECONNRESET ); + RETSTR( WSAENOBUFS ); + RETSTR( WSAEISCONN ); + RETSTR( WSAENOTCONN ); + RETSTR( WSAESHUTDOWN ); + RETSTR( WSAETOOMANYREFS ); + RETSTR( WSAETIMEDOUT ); + RETSTR( WSAECONNREFUSED ); + RETSTR( WSAELOOP ); + RETSTR( WSAENAMETOOLONG ); + RETSTR( WSAEHOSTDOWN ); + RETSTR( WSAEHOSTUNREACH ); + RETSTR( WSAENOTEMPTY ); + RETSTR( WSAEPROCLIM ); + RETSTR( WSAEUSERS ); + RETSTR( WSAEDQUOT ); + RETSTR( WSAESTALE ); + RETSTR( WSAEREMOTE ); + RETSTR( WSASYSNOTREADY ); + RETSTR( WSAVERNOTSUPPORTED ); + RETSTR( WSANOTINITIALISED ); + RETSTR( WSAEDISCON ); + +#ifdef HAVE_WINSOCK2 + RETSTR( WSAENOMORE ); + RETSTR( WSAECANCELLED ); + RETSTR( WSAEINVALIDPROCTABLE ); + RETSTR( WSAEINVALIDPROVIDER ); + RETSTR( WSASYSCALLFAILURE ); + RETSTR( WSASERVICE_NOT_FOUND ); + RETSTR( WSATYPE_NOT_FOUND ); + RETSTR( WSA_E_NO_MORE ); + RETSTR( WSA_E_CANCELLED ); + RETSTR( WSAEREFUSED ); +#endif /* HAVE_WINSOCK2 */ + + RETSTR( WSAHOST_NOT_FOUND ); + RETSTR( WSATRY_AGAIN ); + RETSTR( WSANO_RECOVERY ); + RETSTR( WSANO_DATA ); + } + return "unknown WSA error"; +} +#endif /* HAVE_WINSOCK_H */ + +/***********************************/ +/****[ end of HELPER FUNCTIONS ]****/ +/***********************************/ diff --git a/src/gras_simix/Transport/gras_simix_transport_private.h b/src/gras_simix/Transport/gras_simix_transport_private.h new file mode 100644 index 0000000000..788077c269 --- /dev/null +++ b/src/gras_simix/Transport/gras_simix_transport_private.h @@ -0,0 +1,109 @@ +/* $Id$ */ + +/* transport - low level communication (send/receive bunches of bytes) */ + +/* module's private interface masked even to other parts of GRAS. */ + +/* Copyright (c) 2004 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#ifndef GRAS_TRP_PRIVATE_H +#define GRAS_TRP_PRIVATE_H + +#include "xbt/sysdep.h" +#include "xbt/log.h" +#include "xbt/dynar.h" +#include "xbt/dict.h" + +#include "gras/emul.h" /* gras_if_RL() */ + +#include "gras_modinter.h" /* module init/exit */ +#include "gras/transport.h" /* rest of module interface */ + +#include "gras_simix/Transport/gras_simix_transport_interface.h" /* semi-public API */ +#include "gras_simix/Virtu/gras_simix_virtu_interface.h" /* libdata management */ + +extern int gras_trp_libdata_id; /* our libdata identifier */ + +/* The function that select returned the last time we asked. We need this because the TCP read + are greedy and try to get as much data in their buffer as possible (to avoid subsequent syscalls). + (measurement sockets are not buffered and thus not concerned). + + So, we can get more than one message in one shoot. And when this happens, we have to handle + the same socket again afterward without select()ing at all. + + Then, this data is not a static of the TCP driver because we want to zero it when + it gets closed by the user. If not, we use an already freed pointer, which is bad. + + It gets tricky since gras_socket_close is part of the common API, not only the RL one. */ +extern gras_socket_t _gras_lastly_selected_socket; + +/** + * s_gras_socket: + * + * Description of a socket. + */ +typedef struct gras_trp_bufdata_ gras_trp_bufdata_t; + +typedef struct s_gras_socket { + gras_trp_plugin_t plugin; + + int incoming :1; /* true if we can read from this sock */ + int outgoing :1; /* true if we can write on this sock */ + int accepting :1; /* true if master incoming sock in tcp */ + int meas :1; /* true if this is an experiment socket instead of messaging */ + int recv_ok :1; /* true if it is valid to recv() on the socket (false if it is a file) */ + int valid :1; /* false if a select returned that the peer quitted, forcing us to "close" the socket */ + int moredata :1; /* TCP socket use a buffer and read operation get as much data as possible. + It is possible that several messages are received in one shoot, and select won't catch them afterward again. + This boolean indicates that this is the case, so that we don't call select in that case. + Note that measurement sockets are not concerned since they use the TCP interface directly, with no buffer. */ + + unsigned long int buf_size; /* what to say to the OS. field here to remember it when accepting */ + + int sd; + int port; /* port on this side */ + int peer_port; /* port on the other side */ + char *peer_name; /* hostname of the other side */ + char *peer_proc; /* process on the other side */ + + void *data; /* plugin specific data */ + + /* buffer plugin specific data. Yeah, C is not OO, so I got to trick */ + gras_trp_bufdata_t *bufdata; +}s_gras_socket_t; + +void gras_trp_socket_new(int incomming, + gras_socket_t *dst); + +/* The drivers */ +typedef void (*gras_trp_setup_t)(gras_trp_plugin_t dst); + +void gras_trp_tcp_setup(gras_trp_plugin_t plug); +void gras_trp_iov_setup(gras_trp_plugin_t plug); +void gras_trp_file_setup(gras_trp_plugin_t plug); +void gras_trp_sg_setup(gras_trp_plugin_t plug); + +/* + + I'm tired of that shit. the select in SG has to create a socket to expeditor + manually do deal with the weirdness of the hostdata, themselves here to deal + with the weird channel concept of SG and convert them back to ports. + + When introducing buffered transport (which I want to get used in SG to debug + the buffering itself), we should not make the rest of the code aware of the + change and not specify code for this. This is bad design. + + But there is bad design all over the place, so fuck off for now, when we can + get rid of MSG and rely directly on SG, this crude hack can go away. But in + the meanwhile, I want to sleep this night (FIXME). + + Hu! You evil problem! Taste my axe! + +*/ + +gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock); + +#endif /* GRAS_TRP_PRIVATE_H */ diff --git a/src/gras_simix/Virtu/gras_simix_gras_module.c b/src/gras_simix/Virtu/gras_simix_gras_module.c new file mode 100644 index 0000000000..e0ad2b7682 --- /dev/null +++ b/src/gras_simix/Virtu/gras_simix_gras_module.c @@ -0,0 +1,249 @@ +/* $Id$ */ +/* module - module handling, along with module dependencies and local state */ + +/* Copyright (C) 2006 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "xbt.h" +#include "gras/module.h" +#include "gras_simix_virtu_private.h" + +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_modules,gras,"Module and moddata handling"); + +/* IMPLEMENTATION NOTE + +The goal of this module is quite difficult. We want to have several modules, +each of them having globals which have to be initialized in any unix +process. Let's call them world-wide globals. An example may be the messages +dictionnary, which is world-wide to detect definition discrepencies when +running in SG. + +And then, the module may want to have some globals specific to a gras process, +just like userdata stuff allows to have per-process globals. Let's call them +process-wide globals. An example may be the list of attached callbacks. + +That is why we have 4 functions per module: the join function is called by any +process, and is in charge of creating the process-wide globals. If it is the +first time a process uses the module, it also calls the init function, in +charge of initializing the world-wide globals. We have the symetric functions +leave, called by any process not using the module anymore, and exit, in charge +of freeing the world-wide globals when no process use it anymore. + +One can see this as a factory of factories. the init function is a factory +creating the module, which contains a factory (the join function) able to +create process-wide globals. The fact that indeed the calling sequence goes +from join to init and not the other side is just an implementation bias ;) + +Then again, we want these functionnalities to be quick. We want to access +the process-wide globals by providing their rank in a dynar, we don't want to +search them in a dictionnary. This is especially true in the module +implementation, where each functions is likely to require them to work. The +position could be a stored in a global variable only visible from the module +implementation. + +The need for an array in which to store the globals does not hold for +world-wide globals: only one instance of them can exist in the same unix naming +space. Thus, there is no need to put them in an array, we can actually declare +them as regular C globals. + +The next trick comes from the fact that the user may (and will) mess up and not +get all modules initialized in the same order in every process. So, the rank of +module data cannot be the order in which a process called the join +function. This has to be a world-wide global instead. + +And finally, if we want to get the ability to destroy modules (one day, they +will load their code in as plugin), we want to give the module handling library +(ie, this file) to modify the variable containing the rank of module in the +tables. This is why we have the ID field in the module structure: it points +exactly to the implementation side global. + +Yeah, I know. All this is not that clear. But at least, writing this helped me +to design that crap ;) + +*/ + +typedef struct s_gras_module { + XBT_SET_HEADERS; + + unsigned int datasize; + int refcount; /* Number of processes using this module */ + /* FIXME: we should keep a count of references within a given process to + allow modules initializing other modules while tracking dependencies + properly and leave() only when needed. This would allow dynamic module + loading/unloading */ + + int *p_id; /* where the module stores the libdata ID (a global somewhere), to tweak it on need */ + void_f_void_t *init_f; /* First time the module is referenced. */ + void_f_void_t *exit_f; /* When last process referencing it stops doing so. */ + void_f_pvoid_t *join_f; /* Called by each process in initialization phase (init_f called once for all processes) */ + void_f_pvoid_t *leave_f; /* Called by each process in finalization phase. Should free moddata passed */ +} s_gras_module_t, *gras_module_t; + +static xbt_set_t _gras_modules = NULL; /* content: s_gras_module_t */ + +static void gras_module_freep(void *p) { + free( ((gras_module_t)p) ->name); +} + + +/** + * @brief Declaring a new GRAS module + * @param name: name of the module, of course (beware of dupplicates!) + * @param datasize: the size of your data, ie of the state this module has on each process + * @param ID: address of a global you use as parameter to gras_module_data_by_id + * @param init_f: function called the first time a module gets by a process of the naming space. + * A classical use is to declare some messages the module uses, as well as the initialization + * of module constants (accross processes boundaries in SG). + * @param exit_f: function called when the last process of this naming space unref this module. + * @param join_f: function called each time a process references the module. + * It is passed the moddata already malloced, and should initialize the fields as it wants. + * It can also attach some callbacks to the module messages. + * @param leave_f: function called each time a process unrefs the module. + * It is passed the moddata, and should free any memory allocated by init_f. + * It should alse disconnect any attached callbacks. + */ + +void gras_module_add(const char *name, unsigned int datasize, int *ID, + void_f_void_t *init_f, void_f_void_t *exit_f, + void_f_pvoid_t *join_f, void_f_pvoid_t *leave_f) { + gras_module_t mod=NULL; + xbt_ex_t e; + volatile int found = 0; + + if (!_gras_modules) + _gras_modules = xbt_set_new(); + + TRY { + mod=(gras_module_t)xbt_set_get_by_name (_gras_modules,name); + found = 1; + } CATCH(e) { + if (e.category != not_found_error) + RETHROW; + xbt_ex_free(e); + } + + if (found) { + xbt_assert1(mod->init_f == init_f, + "Module %s reregistered with a different init_f!", name); + xbt_assert1(mod->exit_f == exit_f, + "Module %s reregistered with a different exit_f!", name); + xbt_assert1(mod->join_f == join_f, + "Module %s reregistered with a different join_f!", name); + xbt_assert1(mod->leave_f == leave_f, + "Module %s reregistered with a different leave_f!", name); + xbt_assert1(mod->datasize == datasize, + "Module %s reregistered with a different datasize!", name); + xbt_assert1(mod->p_id == ID, + "Module %s reregistered with a different p_id field!", name); + + DEBUG1("Module %s already registered. Ignoring re-registration",name); + return; + } + + VERB1("Register module %s",name); + mod = xbt_new(s_gras_module_t, 1); + mod->name = xbt_strdup(name); + mod->name_len = strlen(name); + + mod->datasize = datasize; + mod->p_id = ID; + mod->init_f = init_f; + mod->exit_f = exit_f; + mod->join_f = join_f; + mod->leave_f = leave_f; + mod->refcount = 0; + + *mod->p_id = xbt_set_length(_gras_modules); + + xbt_set_add(_gras_modules,(void*)mod,gras_module_freep); +} + +/* shutdown the module mechanism (world-wide cleanups) */ +void gras_moddata_exit(void) { + xbt_set_free(&_gras_modules); +} + +/* frees the moddata on this host (process-wide cleanups) */ +void gras_moddata_leave(void) { + gras_procdata_t *pd=gras_procdata_get(); + + xbt_dynar_free(&pd->moddata); +} + +/* Removes & frees a given moddata from the current host */ +static void moddata_freep(void *p) { + gras_procdata_t *pd=gras_procdata_get(); + int id = xbt_dynar_search (pd->moddata, p); + gras_module_t mod = (gras_module_t)xbt_set_get_by_id(_gras_modules, id); + + (*mod->leave_f)(gras_moddata_by_id(id)); +} + +void gras_module_join(const char *name) { + gras_procdata_t *pd; + void *moddata; + gras_module_t mod = (gras_module_t)xbt_set_get_by_name(_gras_modules, name); + + VERB2("Join to module %s (%p)",name,mod); + + /* NEW */ + if (mod->refcount == 0) { + VERB1("Init module %s",name); + mod->name = xbt_strdup(name); + + (*mod->init_f)(); + } else { + DEBUG3("Module %s already inited. Refcount=%d ID=%d", + mod->name, mod->refcount,*(mod->p_id)); + } + mod->refcount++; + + /* JOIN */ + pd=gras_procdata_get(); + + if (!pd->moddata) /* Damn. I must be the first module on this process. Scary ;)*/ + pd->moddata = xbt_dynar_new(sizeof(gras_module_t),&moddata_freep); + + moddata = xbt_malloc(mod->datasize); + + xbt_dynar_set(pd->moddata, *(mod->p_id), &moddata); + + (*mod->join_f)(moddata); + + DEBUG2("Module %s joined successfully (ID=%d)", name,*(mod->p_id)); +} +void gras_module_leave(const char *name) { + void *moddata; + gras_module_t mod = (gras_module_t)xbt_set_get_by_name(_gras_modules, name); + + VERB1("Leave module %s",name); + + /* LEAVE */ + moddata = gras_moddata_by_id( *(mod->p_id) ); + (*mod->leave_f)(moddata); + + /* EXIT */ + mod->refcount--; + if (!mod->refcount) { + VERB1("Exit module %s",name); + + (*mod->exit_f)(); + + /* Don't remove the module for real, sets don't allow to + + free(mod->name); + free(mod); + */ + } +} + + +void *gras_moddata_by_id(unsigned int ID) { + gras_procdata_t *pd=gras_procdata_get(); + void *p; + + xbt_dynar_get_cpy(pd->moddata, ID, &p); + return p; +} diff --git a/src/gras_simix/Virtu/gras_simix_process.c b/src/gras_simix/Virtu/gras_simix_process.c new file mode 100644 index 0000000000..28ab76ca70 --- /dev/null +++ b/src/gras_simix/Virtu/gras_simix_process.c @@ -0,0 +1,172 @@ +/* $Id$ */ + +/* process - GRAS process handling (common code for RL and SG) */ + +/* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "xbt/ex.h" +#include "xbt/sysdep.h" +#include "xbt/log.h" +#include "gras/transport.h" +#include "gras/datadesc.h" +#include "gras/messages.h" +#include "gras_modinter.h" + +#include "gras_simix/Virtu/gras_simix_virtu_private.h" + +XBT_LOG_NEW_SUBCATEGORY(gras_virtu,gras,"Virtualization code"); +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_virtu_process,gras_virtu,"Process manipulation code"); + +/* Functions to handle gras_procdata_t->libdata cells*/ +typedef struct { + char *name; + pvoid_f_void_t *constructor; + void_f_pvoid_t *destructor; +} s_gras_procdata_fabric_t, *gras_procdata_fabric_t; + +static xbt_dynar_t _gras_procdata_fabrics = NULL; /* content: s_gras_procdata_fabric_t */ + +static void gras_procdata_fabric_free(void *fab) { + free( ((gras_procdata_fabric_t)fab)->name ); +} + +/** @brief declare the functions in charge of creating/destructing the procdata of a module + * + * This is intended to be called from the gras__register function. + * This returns the module ID you can use for gras_libdata_by_id() + */ +int gras_procdata_add(const char *name, pvoid_f_void_t constructor,void_f_pvoid_t destructor) { + + gras_procdata_fabric_t fab; + + if (!_gras_procdata_fabrics) { + /* create the dynar if needed */ + _gras_procdata_fabrics = xbt_dynar_new(sizeof(s_gras_procdata_fabric_t), + gras_procdata_fabric_free); + } + + fab=xbt_dynar_push_ptr(_gras_procdata_fabrics); + + fab->name = xbt_strdup(name); + fab->constructor = constructor; + fab->destructor = destructor; + + return xbt_dynar_length(_gras_procdata_fabrics)-1; +} + +/* ************************************************************************** + * Process data + * **************************************************************************/ +void *gras_userdata_get(void) { + gras_procdata_t *pd=gras_procdata_get(); + return pd->userdata; +} + +void gras_userdata_set(void *ud) { + gras_procdata_t *pd=gras_procdata_get(); + + pd->userdata = ud; +} + +void *gras_libdata_by_name(const char *name) { + gras_procdata_t *pd=gras_procdata_get(); + return gras_libdata_by_name_from_procdata(name,pd); +} + +/* this function is splitted from previous to allow SG to get libdata from remote hosts */ +void *gras_libdata_by_name_from_procdata(const char*name, gras_procdata_t* pd) { + void *res=NULL; + xbt_ex_t e; + + if (xbt_set_length(pd->libdata) < xbt_dynar_length(_gras_procdata_fabrics)) { + /* Damn, some new modules were added since procdata_init(). Amok? */ + /* Get 'em all */ + gras_procdata_init(); + } + + TRY { + res = xbt_set_get_by_name(pd->libdata, name); + } CATCH(e) { + RETHROW1("Cannot retrieve the libdata associated to %s: %s",name); + } + return res; +} + +void *gras_libdata_by_id(int id) { + gras_procdata_t *pd=gras_procdata_get(); + if (xbt_set_length(pd->libdata) < xbt_dynar_length(_gras_procdata_fabrics)) { + /* Damn, some new modules were added since procdata_init(). Amok? */ + /* Get 'em all */ + gras_procdata_init(); + } + return xbt_set_get_by_id(pd->libdata, id); +} + + +void +gras_procdata_init() { + gras_procdata_t *pd=gras_procdata_get(); + s_gras_procdata_fabric_t fab; + + int cursor; + + xbt_ex_t e; + xbt_set_elm_t elem; + + if (!pd->libdata) { + pd->userdata = NULL; + pd->libdata = xbt_set_new(); + } + + xbt_dynar_foreach(_gras_procdata_fabrics,cursor,fab){ + volatile int found = 0; + + if (cursor+1 <= xbt_set_length(pd->libdata)) { + DEBUG2("Skip fabric %d: there is already %ld libdata", + cursor, xbt_set_length(pd->libdata)); + continue; /* allow to recall this function to get recently added fabrics */ + } + DEBUG2("Go ahead for cursor %d, there is %ld libdata", + cursor,xbt_set_length(pd->libdata)); + + xbt_assert1(fab.name,"Name of fabric #%d is NULL!",cursor); + DEBUG1("Create the procdata for %s",fab.name); + /* Check for our own errors */ + TRY { + xbt_set_get_by_name(pd->libdata, fab.name); + found = 1; + } CATCH(e) { + xbt_ex_free(e); + found = 0; + } + if (found) + THROW1(unknown_error,0,"MayDay: two modules use '%s' as libdata name", fab.name); + + /* Add the data in place, after some more sanity checking */ + elem = (fab.constructor)(); + if (elem->name_len && elem->name_len != strlen(elem->name)) { + elem->name_len = strlen(elem->name); + WARN1("Module '%s' constructor is borken: it does not set elem->name_len", + fab.name); + } + + xbt_set_add(pd->libdata, elem, fab.destructor); + } +} + +void +gras_procdata_exit() { + int len; + gras_procdata_t *pd=gras_procdata_get(); + + xbt_set_free(&( pd->libdata )); + + /* Remove procdata in reverse order wrt creation */ + while ((len=xbt_dynar_length(_gras_procdata_fabrics))) { + xbt_dynar_remove_at(_gras_procdata_fabrics,len-1,NULL); + } + xbt_dynar_free( & _gras_procdata_fabrics ); +} diff --git a/src/gras_simix/Virtu/gras_simix_rl_dns.c b/src/gras_simix/Virtu/gras_simix_rl_dns.c new file mode 100644 index 0000000000..04e2f5408b --- /dev/null +++ b/src/gras_simix/Virtu/gras_simix_rl_dns.c @@ -0,0 +1,49 @@ +/* $Id$ */ + +/* rl_dns - name resolution (real life) */ + +/* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "gras/Virtu/virtu_rl.h" +#include "portable.h" + +/* A portable DNS resolver is a nightmare to do in a portable manner. + keep it simple/stupid for now. */ + +const char *gras_os_myname(void) { + static char *myname = NULL; + + if (myname) + return (const char*) myname; + + myname = xbt_new(char, 255); + + if (gethostname(myname, 255) == -1) { +#ifdef HAVE_SYS_SOCKET_H + /* gethostname() failed! Trying with localhost instead. + We first need to query the DNS to make sure localhost is resolved + See the note in nws/Portability/dnsutil.c about {end,set}hostent() */ + struct hostent *tmp; + sethostent(0); + tmp = gethostbyname("localhost"); + endhostent(); + + if (tmp) { + strncat(myname, tmp->h_name, 255); + } else { + /* Erm. localhost cannot be resolved. There's something wrong in the user DNS setting */ + sprintf(myname, "(misconfigured host)"); + } +#else + sprintf(myname, "(misconfigured windows host)"); +#endif + } + + myname[254] = '\0'; + + return myname; +} + diff --git a/src/gras_simix/Virtu/gras_simix_rl_emul.c b/src/gras_simix/Virtu/gras_simix_rl_emul.c new file mode 100644 index 0000000000..908268d4fa --- /dev/null +++ b/src/gras_simix/Virtu/gras_simix_rl_emul.c @@ -0,0 +1,45 @@ +/* $Id$ */ + +/* rl_emul - Emulation support (real life) */ + +/* Copyright (c) 2003-5 Arnaud Legrand, Martin Quinson. All rights reserved.*/ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "gras/emul.h" +#include "gras/Virtu/virtu_rl.h" +#include "gras_modinter.h" + +XBT_LOG_NEW_SUBCATEGORY(gras_virtu_emul,gras_virtu,"Emulation support"); + +/*** Timing macros: nothing to do in RL. Actually do the job and shutup ***/ + +void gras_emul_init(void) {} +void gras_emul_exit(void) {} + +int gras_bench_always_begin(const char *location,int line) { + return 0; +} + +int gras_bench_always_end(void) { + return 0; +} + +int gras_bench_once_begin(const char *location,int line) { + return 1; +} + +int gras_bench_once_end(void) { + return 0; +} + +/*** Conditional execution support ***/ + +int gras_if_RL(void) { + return 1; +} + +int gras_if_SG(void) { + return 0; +} diff --git a/src/gras_simix/Virtu/gras_simix_rl_process.c b/src/gras_simix/Virtu/gras_simix_rl_process.c new file mode 100644 index 0000000000..7351f8aff4 --- /dev/null +++ b/src/gras_simix/Virtu/gras_simix_rl_process.c @@ -0,0 +1,48 @@ +/* $Id$ */ + +/* process_rl - GRAS process handling on real life */ + +/* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "gras_modinter.h" /* module initialization interface */ +#include "gras/Virtu/virtu_rl.h" +#include "portable.h" + +/* globals */ +static gras_procdata_t *_gras_procdata = NULL; +char const * XBT_PUBLIC_DATA _gras_procname = NULL; + +void gras_process_init() { + _gras_procdata=xbt_new0(gras_procdata_t,1); + gras_procdata_init(); +} +void gras_process_exit() { + gras_procdata_exit(); + free(_gras_procdata); +} + +const char *xbt_procname(void) { + if(_gras_procname) return _gras_procname; + else return ""; +} + +long int gras_os_getpid(void) { + #ifdef _WIN32 + return (long int) GetCurrentProcess(); + #else + return (long int) getpid(); + #endif +} + +/* ************************************************************************** + * Process data + * **************************************************************************/ + +gras_procdata_t *gras_procdata_get(void) { + xbt_assert0(_gras_procdata,"Run gras_process_init (ie, gras_init)!"); + + return _gras_procdata; +} diff --git a/src/gras_simix/Virtu/gras_simix_rl_time.c b/src/gras_simix/Virtu/gras_simix_rl_time.c new file mode 100644 index 0000000000..24e1fd6f4e --- /dev/null +++ b/src/gras_simix/Virtu/gras_simix_rl_time.c @@ -0,0 +1,50 @@ +/* $Id$ */ + +/* time - time related syscal wrappers */ + +/* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include /* floor */ + +#include "portable.h" + +#include "xbt/sysdep.h" +#include "xbt/log.h" +#include "gras/virtu.h" +#include "xbt/xbt_portability.h" /* private */ + +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_virtu); + +double gras_os_time() { + return xbt_os_time(); +} + +void gras_os_sleep(double sec) { +#ifdef HAVE_USLEEP + DEBUG1("Do sleep %f sec", sec); + sleep(sec); + (void)usleep( (sec - floor(sec)) * 1000000); + +#elif _WIN32 + DEBUG1("Do sleep %f sec", sec); + + Sleep((floor(sec) * 1000) +((sec - floor(sec)) * 1000)); + + +#else /* don't have usleep. Use select to sleep less than one second */ + struct timeval timeout; + + DEBUG1("Do sleep %f sec", sec); + + timeout.tv_sec = (unsigned long)(sec); + timeout.tv_usec = (sec - floor(sec)) * 1000000; + + select(0, NULL, NULL, NULL, &timeout); +#endif +} + + + diff --git a/src/gras_simix/Virtu/gras_simix_sg_dns.c b/src/gras_simix/Virtu/gras_simix_sg_dns.c new file mode 100644 index 0000000000..ae09723e92 --- /dev/null +++ b/src/gras_simix/Virtu/gras_simix_sg_dns.c @@ -0,0 +1,19 @@ +/* $Id$ */ + +/* sg_dns - name resolution (simulator) */ + +/* Copyright (c) 2005 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + + +#include "gras_simix/Virtu/gras_simix_virtu_sg.h" + +const char *gras_os_myname(void) { + smx_process_t process = SIMIX_process_self(); + if ((process != NULL) && (process->simdata)) + return SIMIX_host_get_name(SIMIX_host_self()); + else + return ""; +} diff --git a/src/gras_simix/Virtu/gras_simix_sg_emul.c b/src/gras_simix/Virtu/gras_simix_sg_emul.c new file mode 100644 index 0000000000..d07083f6dd --- /dev/null +++ b/src/gras_simix/Virtu/gras_simix_sg_emul.c @@ -0,0 +1,172 @@ +/* $Id$ */ + +/* sg_emul - Emulation support (simulation) */ + +/* Copyright (c) 2003-5 Arnaud Legrand, Martin Quinson. All rights reserved.*/ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include /* sprintf */ +#include "gras/emul.h" +#include "gras_simix/Virtu/gras_simix_virtu_sg.h" +#include "gras_modinter.h" + +#include "xbt/xbt_portability.h" /* timers */ +#include "xbt/dict.h" +#include "xbt/ex.h" + +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_virtu_emul,gras_virtu,"Emulation support"); + +/*** Timing macros ***/ +static xbt_os_timer_t timer; +static int benchmarking = 0; +static xbt_dict_t benchmark_set = NULL; +static double reference = .00000000523066250047108838; /* FIXME: we should benchmark host machine to set this; unit=s/flop */ +static double duration = 0.0; + +static char* locbuf = NULL; +static int locbufsize; + +void gras_emul_init(void) +{ + if(!benchmark_set) { + benchmark_set = xbt_dict_new(); + timer = xbt_os_timer_new(); + } +} + +void gras_emul_exit(void) { + if (locbuf) free(locbuf); + xbt_dict_free(&benchmark_set); + xbt_os_timer_free(timer); +} + + +static void store_in_dict(xbt_dict_t dict, const char *key, double value) +{ + double *ir; + + ir = xbt_dict_get_or_null(dict, key); + if (!ir) { + ir = xbt_new0(double,1); + xbt_dict_set(dict, key, ir, xbt_free_f); + } + *ir = value; +} + +static double get_from_dict(xbt_dict_t dict, const char *key) { + double *ir = xbt_dict_get(dict, key); + + return *ir; +} + +int gras_bench_always_begin(const char *location,int line) +{ + xbt_assert0(!benchmarking,"Already benchmarking"); + benchmarking = 1; + + if (!timer) + xbt_os_timer_start(timer); + return 0; +} + +int gras_bench_always_end(void) +{ + smx_action_t act; + smx_cond_t cond; + smx_mutex_t mutex; + + xbt_assert0(benchmarking,"Not benchmarking yet"); + benchmarking = 0; + xbt_os_timer_stop(timer); + duration = xbt_os_timer_elapsed(timer); + + cond = SIMIX_cond_init(); + mutex = SIMIX_mutex_init(); + + SIMIX_mutex_lock(mutex); + act = SIMIX_action_execute(SIMIX_host_self(), (char*) "task", (duration)/reference); + + SIMIX_register_action_to_condition(act,cond); + SIMIX_register_condition_to_action(act,cond); + SIMIX_cond_wait(cond, mutex); + + SIMIX_action_destroy(act); + SIMIX_mutex_unlock(mutex); + + SIMIX_cond_destroy(cond); + SIMIX_mutex_destroy(mutex); + + return 0; +} + +int gras_bench_once_begin(const char *location,int line) { + double *ir = NULL; + xbt_assert0(!benchmarking,"Already benchmarking"); + benchmarking = 1; + + if (!locbuf || locbufsize < strlen(location) + 64) { + locbufsize = strlen(location) + 64; + locbuf = xbt_realloc(locbuf,locbufsize); + } + sprintf(locbuf,"%s:%d",location, line); + + ir = xbt_dict_get_or_null(benchmark_set, locbuf); + if(!ir) { + DEBUG1("%s",locbuf); + duration = 1; + xbt_os_timer_start(timer); + return 1; + } else { + duration = -1.0; + return 0; + } +} + +int gras_bench_once_end(void) +{ + smx_action_t act; + smx_cond_t cond; + smx_mutex_t mutex; + + xbt_assert0(benchmarking,"Not benchmarking yet"); + benchmarking = 0; + if(duration>0) { + xbt_os_timer_stop(timer); + duration = xbt_os_timer_elapsed(timer); + store_in_dict(benchmark_set, locbuf, duration); + } else { + duration = get_from_dict(benchmark_set,locbuf); + } + DEBUG2("Simulate the run of a task of %f sec for %s",duration,locbuf); + cond = SIMIX_cond_init(); + mutex = SIMIX_mutex_init(); + + SIMIX_mutex_lock(mutex); + act = SIMIX_action_execute(SIMIX_host_self(), (char*)"task", (duration)/reference); + + SIMIX_register_action_to_condition(act,cond); + SIMIX_register_condition_to_action(act,cond); + SIMIX_cond_wait(cond, mutex); + + SIMIX_action_destroy(act); + SIMIX_mutex_unlock(mutex); + + SIMIX_cond_destroy(cond); + SIMIX_mutex_destroy(mutex); + return 0; +} + + +/*** Conditional execution support ***/ + +int gras_if_RL(void) { + return 0; +} + +int gras_if_SG(void) { + return 1; +} + + diff --git a/src/gras_simix/Virtu/gras_simix_sg_process.c b/src/gras_simix/Virtu/gras_simix_sg_process.c new file mode 100644 index 0000000000..7d8b43b9ab --- /dev/null +++ b/src/gras_simix/Virtu/gras_simix_sg_process.c @@ -0,0 +1,176 @@ +/* $Id$ */ + +/* process_sg - GRAS process handling on simulator */ + +/* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "xbt/ex.h" +#include "gras_modinter.h" /* module initialization interface */ +#include "gras_simix/Virtu/gras_simix_virtu_sg.h" +#include "gras_simix/Msg/gras_simix_msg_interface.h" /* For some checks at simulation end */ +#include "gras_simix/Transport/gras_simix_transport_interface.h" /* For some checks at simulation end */ + +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_virtu_process); + +static long int PID = 1; + +void +gras_process_init() { + gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self()); + gras_procdata_t *pd=xbt_new0(gras_procdata_t,1); + gras_trp_procdata_t trp_pd; + //gras_sg_portrec_t prmeas,pr; + //int i; + + SIMIX_process_set_data(SIMIX_process_self(),(void*)pd); + + trp_pd = (gras_trp_procdata_t)gras_libdata_by_name("gras_trp"); + trp_pd->pid = PID++; + if (SIMIX_process_self() != NULL ) { + trp_pd->ppid = gras_os_getpid(); + } + else trp_pd->ppid = -1; + + gras_procdata_init(); + + if (!hd) { + /* First process on this host */ + hd=xbt_new(gras_hostdata_t,1); + hd->refcount = 1; + hd->ports = xbt_dynar_new(sizeof(gras_sg_portrec_t),NULL); + + // memset(hd->proc, 0, sizeof(hd->proc[0]) * XBT_MAX_CHANNEL); + + SIMIX_host_set_data(SIMIX_host_self(),(void*)hd); + } else { + hd->refcount++; + } + + /* take a free channel for this process */ + /* + trp_pd = (gras_trp_procdata_t)gras_libdata_by_name("gras_trp"); + for (i=0; iproc[i]; i++); + if (i == XBT_MAX_CHANNEL) + THROW2(system_error,0, + "Can't add a new process on %s, because all channels are already in use. Please increase MAX CHANNEL (which is %d for now) and recompile GRAS.", + MSG_host_get_name(MSG_host_self()),XBT_MAX_CHANNEL); + + trp_pd->chan = i; + hd->proc[ i ] = MSG_process_self_PID(); +*/ + /* regiter it to the ports structure */ + // pr.port = -1; + //pr.tochan = i; + //pr.meas = 0; + //xbt_dynar_push(hd->ports,&pr); + + /* take a free meas channel for this process */ + /* + for (i=0; iproc[i]; i++); + if (i == XBT_MAX_CHANNEL) { + THROW2(system_error,0, + "Can't add a new process on %s, because all channels are already in use. Please increase MAX CHANNEL (which is %d for now) and recompile GRAS.", + MSG_host_get_name(MSG_host_self()),XBT_MAX_CHANNEL); + } + trp_pd->measChan = i; + + hd->proc[ i ] = MSG_process_self_PID(); +*/ + /* register it to the ports structure */ + //prmeas.port = -1; + //prmeas.tochan = i; + //prmeas.meas = 1; + //xbt_dynar_push(hd->ports,&prmeas); + + VERB2("Creating process '%s' (%ld)", + SIMIX_process_get_name(SIMIX_process_self()), + gras_os_getpid()); +} + +void +gras_process_exit() { + gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self()); + gras_procdata_t *pd=(gras_procdata_t*)SIMIX_process_get_data(SIMIX_process_self()); + + gras_msg_procdata_t msg_pd=(gras_msg_procdata_t)gras_libdata_by_name("gras_msg"); + //gras_trp_procdata_t trp_pd=(gras_trp_procdata_t)gras_libdata_by_name("gras_trp"); + //int myPID=gras_os_getpid(); + //int cpt; + //gras_sg_portrec_t pr; + + xbt_assert0(hd,"Run gras_process_init (ie, gras_init)!!"); + + VERB2("GRAS: Finalizing process '%s' (%ld)", + SIMIX_process_get_name(SIMIX_process_self()),gras_os_getpid()); + + if (xbt_dynar_length(msg_pd->msg_queue)) + WARN1("process %ld terminated, but some messages are still queued", + gras_os_getpid()); + +/* + for (cpt=0; cpt< XBT_MAX_CHANNEL; cpt++) + if (myPID == hd->proc[cpt]) + hd->proc[cpt] = 0; +*/ + +/* remove ports from host, maybe i can do it on the socket destroy function */ + /* + xbt_dynar_foreach(hd->ports, cpt, pr) { + if (pr.port == trp_pd->chan || pr.port == trp_pd->measChan) { + xbt_dynar_cursor_rm(hd->ports, &cpt); + } + }*/ + + if ( ! --(hd->refcount)) { + xbt_dynar_free(&hd->ports); + free(hd); + } + gras_procdata_exit(); + free(pd); +} + +/* ************************************************************************** + * Process data + * **************************************************************************/ + +gras_procdata_t *gras_procdata_get(void) { + gras_procdata_t *pd= + (gras_procdata_t *)SIMIX_process_get_data(SIMIX_process_self()); + + xbt_assert0(pd,"Run gras_process_init! (ie, gras_init)"); + + return pd; +} +void * +gras_libdata_by_name_from_remote(const char *name, smx_process_t p) { + gras_procdata_t *pd= + (gras_procdata_t *)SIMIX_process_get_data(p); + + xbt_assert2(pd,"process '%s' on '%s' didn't run gras_process_init! (ie, gras_init)", + SIMIX_process_get_name(p),SIMIX_host_get_name(SIMIX_process_get_host(p))); + + return gras_libdata_by_name_from_procdata(name, pd); +} + +const char* xbt_procname(void) { + const char *res = NULL; + smx_process_t process = SIMIX_process_self(); + if ((process != NULL) && (process->simdata)) + res = SIMIX_process_get_name(process); + if (res) + return res; + else + return ""; +} + +long int gras_os_getpid(void) { + + smx_process_t process = SIMIX_process_self(); + if ((process != NULL) && (process->data)) + return ((gras_procdata_t*)process->data)->pid; + else + return 0; +} diff --git a/src/gras_simix/Virtu/gras_simix_sg_time.c b/src/gras_simix/Virtu/gras_simix_sg_time.c new file mode 100644 index 0000000000..44d6b94a54 --- /dev/null +++ b/src/gras_simix/Virtu/gras_simix_sg_time.c @@ -0,0 +1,45 @@ +/* $Id$ */ + +/* time - time related syscal wrappers */ + +/* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "gras_simix/Virtu/gras_simix_virtu_sg.h" + +/* + * Time elapsed since the begining of the simulation. + */ +double gras_os_time() { + return SIMIX_get_clock(); +} + +/* + * Freeze the process for the specified amount of time + */ +void gras_os_sleep(double sec) { + smx_action_t act_sleep; + smx_process_t proc = SIMIX_process_self(); + smx_mutex_t mutex; + smx_cond_t cond; + /* create action to sleep */ + act_sleep = SIMIX_action_sleep(SIMIX_process_get_host(proc),sec); + + mutex = SIMIX_mutex_init(); + SIMIX_mutex_lock(mutex); + /* create conditional and register action to it */ + cond = SIMIX_cond_init(); + + SIMIX_register_condition_to_action(act_sleep, cond); + SIMIX_register_action_to_condition(act_sleep, cond); + SIMIX_cond_wait(cond,mutex); + SIMIX_mutex_unlock(mutex); + + /* remove variables */ + SIMIX_cond_destroy(cond); + SIMIX_mutex_destroy(mutex); + SIMIX_action_destroy(act_sleep); + +} diff --git a/src/gras_simix/Virtu/gras_simix_virtu_interface.h b/src/gras_simix/Virtu/gras_simix_virtu_interface.h new file mode 100644 index 0000000000..2d78570389 --- /dev/null +++ b/src/gras_simix/Virtu/gras_simix_virtu_interface.h @@ -0,0 +1,37 @@ +/* $Id$ */ + +/* virtu[alization] - speciafic parts for each OS and for SG */ + +/* module's public interface exported within GRAS, but not to end user. */ + +/* Copyright (c) 2004 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#ifndef GRAS_VIRTU_INTERFACE_H +#define GRAS_VIRTU_INTERFACE_H + +#include "xbt/function_types.h" +#include "xbt/sysdep.h" +#include "xbt/log.h" +#include "xbt/set.h" +#include "gras/virtu.h" +#include "gras/process.h" + +/* shutdown the module mechanism (world-wide cleanups) */ +void gras_moddata_exit(void); +/* shutdown this process wrt module mecanism (process-wide cleanups) */ +void gras_moddata_leave(void); + +/* This is the old interface (deprecated) */ + + +/* declare a new process specific data + (used by gras__register to make sure that gras_process_init will create it) */ +int gras_procdata_add(const char *name, pvoid_f_void_t creator,void_f_pvoid_t destructor); + +void* gras_libdata_by_name(const char *name); +void* gras_libdata_by_id(int id); + +#endif /* GRAS_VIRTU_INTERFACE_H */ diff --git a/src/gras_simix/Virtu/gras_simix_virtu_private.h b/src/gras_simix/Virtu/gras_simix_virtu_private.h new file mode 100644 index 0000000000..59444e4bdc --- /dev/null +++ b/src/gras_simix/Virtu/gras_simix_virtu_private.h @@ -0,0 +1,45 @@ +/* $Id$ */ + +/* virtu[alization] - speciafic parts for each OS and for SG */ + +/* module's private interface. */ + +/* Copyright (c) 2004 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + + +#ifndef GRAS_VIRTU_PRIVATE_H +#define GRAS_VIRTU_PRIVATE_H + +#include "xbt/dynar.h" +#include "gras_simix/Virtu/gras_simix_virtu_interface.h" +#include "simix/simix.h" + +/** @brief Data for each process */ +typedef struct { + /* globals of the process */ + void *userdata; + + long int pid; + long int ppid; + + /* data specific to each process for each module. + * Registered with gras_procdata_add(), retrieved with gras_libdata_get() + * This is the old interface, and will disapear before 3.2 + */ + xbt_set_t libdata; + + /* data specific to each process for each module. + * Registered with gras_module_add(), retrieved with gras_moddata_get() + * This is the new interface + */ + xbt_dynar_t moddata; +} gras_procdata_t; + +gras_procdata_t *gras_procdata_get(void); +void *gras_libdata_by_name_from_procdata(const char *name, gras_procdata_t* pd); + + +#endif /* GRAS_VIRTU_PRIVATE_H */ diff --git a/src/gras_simix/Virtu/gras_simix_virtu_rl.h b/src/gras_simix/Virtu/gras_simix_virtu_rl.h new file mode 100644 index 0000000000..0a30c4a19a --- /dev/null +++ b/src/gras_simix/Virtu/gras_simix_virtu_rl.h @@ -0,0 +1,15 @@ +/* $Id$ */ + +/* virtu_rl - specific GRAS implementation for real life */ + +/* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#ifndef VIRTU_RL_H +#define VIRTU_RL_H + +#include "gras/Virtu/virtu_private.h" + +#endif /* VIRTU_RL_H */ diff --git a/src/gras_simix/Virtu/gras_simix_virtu_sg.h b/src/gras_simix/Virtu/gras_simix_virtu_sg.h new file mode 100644 index 0000000000..6db30cad7b --- /dev/null +++ b/src/gras_simix/Virtu/gras_simix_virtu_sg.h @@ -0,0 +1,58 @@ +/* $Id$ */ + +/* virtu_sg - specific GRAS implementation for simulator */ + +/* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#ifndef VIRTU_SG_H +#define VIRTU_SG_H + +#include "gras_simix/Virtu/gras_simix_virtu_private.h" +#include "xbt/dynar.h" +//#include "msg/msg.h" /* SimGrid header */ +#include "simix/simix.h" /* SimGrid header */ + +typedef struct { + int port; /* list of ports used by a server socket */ +// int tochan; /* the channel it points to */ + int meas; /* (boolean) the channel is for measurements or for messages */ +} gras_sg_portrec_t; + +/* Data for each host */ +typedef struct { + int refcount; +// int proc[XBT_MAX_CHANNEL]; /* PID of who's connected to each channel */ + /* If =0, then free */ + + smx_cond_t cond_port[65536]; + + xbt_dynar_t ports; + +} gras_hostdata_t; + +/* data for each socket (FIXME: find a better location for that)*/ +typedef struct { + //int from_PID; /* process which sent this message */ + //int to_PID; /* process to which this message is destinated */ + smx_process_t from_process; + smx_process_t to_process; + + smx_host_t to_host; /* Who's on other side */ + gras_sg_portrec_t port; + //m_channel_t to_chan;/* Channel on which the other side is earing */ +} gras_trp_sg_sock_data_t; + + +void *gras_libdata_by_name_from_remote(const char *name, smx_process_t p); +/* The same function by id would be really dangerous. + * + * Indeed, it would rely on the fact that all process register libdatas in + * the same order, which is wrong if they init amok modules in different + * order. + */ + + +#endif /* VIRTU_SG_H */ diff --git a/src/gras_simix/gras_simix_gras.c b/src/gras_simix/gras_simix_gras.c new file mode 100644 index 0000000000..412fa511e2 --- /dev/null +++ b/src/gras_simix/gras_simix_gras.c @@ -0,0 +1,142 @@ +/* $Id$ */ + +/* gras.c -- generic functions not fitting anywhere else */ + +/* Copyright (c) 2003, 2004 Martin Quinson. */ +/* All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "xbt/log.h" +#include "xbt/module.h" /* xbt_init/exit */ + +#include "Virtu/gras_simix_virtu_interface.h" /* Module mechanism FIXME: deplace&rename */ +#include "gras_modinter.h" /* module init/exit */ +#include "amok/amok_modinter.h" /* module init/exit */ +#include "xbt_modinter.h" /* module init/exit */ + +#include "gras.h" +#include "gras/process.h" /* FIXME: killme and put process_init in modinter */ + +#include "portable.h" /* hexa_*(); signalling stuff */ + +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras,XBT_LOG_ROOT_CAT,"All GRAS categories (cf. section \ref GRAS_API)"); +static int gras_running_process = 0; +#if defined(HAVE_SIGNAL) && defined(HAVE_SIGNAL_H) +static void gras_sigusr_handler(int sig) { + INFO0("SIGUSR1 received. Display the backtrace"); + xbt_backtrace_display(); +} + +static void gras_sigint_handler(int sig) { + static double lastone = 0; + if (lastone == 0 || gras_os_time() - lastone > 5) { + lastone = gras_os_time(); + xbt_backtrace_display(); + fprintf(stderr,"\nBacktrace displayed because Ctrl-C was pressed. Press again (within 5 sec) to abort the process.\n"); + } else { + exit(1); + } +} +#endif + +void gras_init(int *argc,char **argv) { + + VERB0("Initialize GRAS"); + + /* First initialize the XBT */ + xbt_init(argc,argv); + + /* module registrations: + * - declare process specific data we need (without creating them) + */ + if (gras_running_process == 0) { + gras_trp_register(); + gras_msg_register(); + } + + /* + * Initialize the process specific stuff + */ + gras_process_init(); /* calls procdata_init, which creates process specific data for each module */ + + /* + * Initialize the global stuff if it's not the first process created + */ + if (gras_running_process++ == 0) { + gras_emul_init(); + gras_msg_init(); + gras_trp_init(); + gras_datadesc_init(); +#if defined(HAVE_SIGNAL) && defined(HAVE_SIGNAL_H) +# ifdef SIGUSR1 + signal(SIGUSR1,gras_sigusr_handler); +# endif + signal(SIGINT,gras_sigint_handler); +#endif + } + + /* and then init amok */ + amok_init(); +} + +void gras_exit(void) { + INFO0("Exiting GRAS"); + amok_exit(); + gras_moddata_leave(); + if (--gras_running_process == 0) { + gras_msg_exit(); + gras_trp_exit(); + gras_datadesc_exit(); + gras_emul_exit(); + gras_moddata_exit(); + } + gras_process_exit(); + xbt_exit(); +} + +const char *hexa_str(unsigned char *data, int size, int downside) { + static char*buff=NULL; + static int buffsize=0; + int i,pos=0; + int begin,increment; + + if (buffsize<5*(size+1)) { + if (buff) + free(buff); + buffsize=5*(size+1); + buff=xbt_malloc(buffsize); + } + + + if (downside) { + begin=size-1; + increment=-1; + } else { + begin=0; + increment=1; + } + + for (i=begin; 0<=i && i126) + sprintf(buff+pos,"."); + else + sprintf(buff+pos,"%c",data[i]); + while (buff[++pos]); + } + sprintf(buff+pos,"("); + while (buff[++pos]); + for (i=begin; 0<=i && i