/** * \brief Schedules a task * * The task state must be #SD_NOT_SCHEDULED. * Once scheduled, a task is executed as soon as possible in \see SD_simulate, i.e. when its dependencies are satisfied. * * \param task the task you want to schedule * \param host_count number of hosts on which the task will be executed * \param workstation_list the hosts on which the task will be executed * \param flops_amount computation amount for each hosts (i.e., an array of host_count doubles) * \param bytes_amount communication amount between each pair of hosts (i.e., a matrix of host_count*host_count doubles) * \param rate task execution speed rate * \see SD_task_unschedule() */ void SD_task_schedule(SD_task_t task, int host_count, const sg_host_t * workstation_list, const double *flops_amount, const double *bytes_amount, double rate) { xbt_assert(host_count > 0, "workstation_nb must be positive"); task->host_count = host_count; task->rate = rate; if (flops_amount) { task->flops_amount = static_cast<double*>(xbt_realloc(task->flops_amount, sizeof(double) * host_count)); memcpy(task->flops_amount, flops_amount, sizeof(double) * host_count); } else { xbt_free(task->flops_amount); task->flops_amount = nullptr; } int communication_nb = host_count * host_count; if (bytes_amount) { task->bytes_amount = static_cast<double*>(xbt_realloc(task->bytes_amount, sizeof(double) * communication_nb)); memcpy(task->bytes_amount, bytes_amount, sizeof(double) * communication_nb); } else { xbt_free(task->bytes_amount); task->bytes_amount = nullptr; } task->host_list = static_cast<sg_host_t*>(xbt_realloc(task->host_list, sizeof(sg_host_t) * host_count)); memcpy(task->host_list, workstation_list, sizeof(sg_host_t) * host_count); SD_task_do_schedule(task); }
static inline void _xbt_dynar_resize(xbt_dynar_t dynar, unsigned long new_size) { if (new_size != dynar->size) { dynar->size = new_size; dynar->data = xbt_realloc(dynar->data, new_size * dynar->elmsize); } }
static void MC_snapshot_add_region(mc_snapshot_t snapshot, void *start_addr, size_t size) { mc_mem_region_t new_reg = MC_region_new(start_addr, size); snapshot->regions = xbt_realloc(snapshot->regions, (snapshot->num_reg + 1) * sizeof(mc_mem_region_t)); snapshot->regions[snapshot->num_reg] = new_reg; snapshot->num_reg++; return; }
//allocate a single buffer for all recv void* smpi_get_tmp_recvbuffer(int size){ if (!smpi_process_get_replaying()) return xbt_malloc(size); if (recvbuffer_size<size){ recvbuffer=xbt_realloc(recvbuffer,size); recvbuffer_size=size; } return sendbuffer; }
//allocate a single buffer for all recv void* smpi_get_tmp_recvbuffer(int size){ if (!smpi_process_get_replaying()) return xbt_malloc(size); if (recvbuffer_size<size){ recvbuffer=static_cast<char*>(xbt_realloc(recvbuffer,size)); recvbuffer_size=size; } return recvbuffer; }
void _xbt_log_event_log(xbt_log_event_t ev, const char *fmt, ...) { xbt_log_category_t cat = ev->cat; xbt_assert(ev->priority >= 0, "Negative logging priority naturally forbidden"); xbt_assert(ev->priority < sizeof(xbt_log_priority_names), "Priority %d is greater than the biggest allowed value", ev->priority); do { xbt_log_appender_t appender = cat->appender; if (!appender) continue; /* No appender, try next */ xbt_assert(cat->layout, "No valid layout for the appender of category %s", cat->name); /* First, try with a static buffer */ if (XBT_LOG_STATIC_BUFFER_SIZE) { char buff[XBT_LOG_STATIC_BUFFER_SIZE]; int done; ev->buffer = buff; ev->buffer_size = sizeof buff; va_start(ev->ap, fmt); done = cat->layout->do_layout(cat->layout, ev, fmt); va_end(ev->ap); if (done) { appender->do_append(appender, buff); continue; /* Ok, that worked: go next */ } } /* The static buffer was too small, use a dynamically expanded one */ ev->buffer_size = XBT_LOG_DYNAMIC_BUFFER_SIZE; ev->buffer = xbt_malloc(ev->buffer_size); while (1) { int done; va_start(ev->ap, fmt); done = cat->layout->do_layout(cat->layout, ev, fmt); va_end(ev->ap); if (done) break; /* Got it */ ev->buffer_size *= 2; ev->buffer = xbt_realloc(ev->buffer, ev->buffer_size); } appender->do_append(appender, ev->buffer); xbt_free(ev->buffer); } while (cat->additivity && (cat = cat->parent, 1)); }
/** * @brief Writes the specified data into a memory buffer. * * This function is a valid lua_Writer that writes into a memory buffer passed * as userdata. * * @param L a lua state * @param source some data * @param size number of bytes of data * @param userdata the memory buffer to write */ int sglua_memory_writer(lua_State* L, const void* source, size_t size, void* userdata) { sglua_buffer_t buffer = (sglua_buffer_t) userdata; while (buffer->capacity < buffer->size + size) { buffer->capacity *= 2; buffer->data = (char*)xbt_realloc(buffer->data, buffer->capacity); } memcpy(buffer->data + buffer->size, source, size); buffer->size += size; return 0; }
lmm_variable_t lmm_variable_new(lmm_system_t sys, void *id, double weight, double bound, int number_of_constraints) { lmm_variable_t var = NULL; int i; XBT_IN("(sys=%p, id=%p, weight=%f, bound=%f, num_cons =%d)", sys, id, weight, bound, number_of_constraints); var = (lmm_variable_t) xbt_mallocator_get(sys->variable_mallocator); var->id = id; var->id_int = Global_debug_id++; var->cnsts = (s_lmm_element_t *) xbt_realloc(var->cnsts, number_of_constraints * sizeof(s_lmm_element_t)); for (i = 0; i < number_of_constraints; i++) { var->cnsts[i].enabled_element_set_hookup.next = NULL; var->cnsts[i].enabled_element_set_hookup.prev = NULL; var->cnsts[i].disabled_element_set_hookup.next = NULL; var->cnsts[i].disabled_element_set_hookup.prev = NULL; var->cnsts[i].active_element_set_hookup.next = NULL; var->cnsts[i].active_element_set_hookup.prev = NULL; var->cnsts[i].constraint = NULL; var->cnsts[i].variable = NULL; var->cnsts[i].value = 0.0; } var->cnsts_size = number_of_constraints; var->cnsts_number = 0; var->weight = weight; var->staged_weight = 0.0; var->bound = bound; var->concurrency_share = 1; var->value = 0.0; var->visited = sys->visited_counter - 1; var->mu = 0.0; var->new_mu = 0.0; var->func_f = func_f_def; var->func_fp = func_fp_def; var->func_fpi = func_fpi_def; var->variable_set_hookup.next = NULL; var->variable_set_hookup.prev = NULL; var->saturated_variable_set_hookup.next = NULL; var->saturated_variable_set_hookup.prev = NULL; if (weight) xbt_swag_insert_at_head(var, &(sys->variable_set)); else xbt_swag_insert_at_tail(var, &(sys->variable_set)); XBT_OUT(" returns %p", var); return var; }
xbt_swag_foreach(cnst, cnst_list) { sum = 0.0; elem_list = &(cnst->element_set); sprintf(print_buf, "\t"); trace_buf = xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1); strcat(trace_buf, print_buf); sprintf(print_buf, "%s(",(cnst->shared)?"":"max"); trace_buf = xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1); strcat(trace_buf, print_buf); xbt_swag_foreach(elem, elem_list) { sprintf(print_buf, "%f.'%d'(%f) %s ", elem->value, elem->variable->id_int, elem->variable->value,(cnst->shared)?"+":","); trace_buf = xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1); strcat(trace_buf, print_buf); if(cnst->shared) sum += elem->value * elem->variable->value; else sum = MAX(sum,elem->value * elem->variable->value); }
void lmm_print(lmm_system_t sys) { lmm_constraint_t cnst = NULL; lmm_element_t elem = NULL; lmm_variable_t var = NULL; xbt_swag_t cnst_list = NULL; xbt_swag_t var_list = NULL; xbt_swag_t elem_list = NULL; char print_buf[1024]; char *trace_buf = xbt_malloc0(sizeof(char)); double sum = 0.0; /* Printing Objective */ var_list = &(sys->variable_set); sprintf(print_buf, "MAX-MIN ( "); trace_buf = xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1); strcat(trace_buf, print_buf); xbt_swag_foreach(var, var_list) { sprintf(print_buf, "'%d'(%f) ", var->id_int, var->weight); trace_buf = xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1); strcat(trace_buf, print_buf); }
void xbt_strbuff_append(xbt_strbuff_t b, const char *toadd) { int addlen; int needed_space; xbt_assert(b, "Asked to append stuff to NULL buffer"); addlen = strlen(toadd); needed_space = b->used + addlen + 1; if (needed_space > b->size) { b->size = MAX(minimal_increment + b->used, needed_space); b->data = xbt_realloc(b->data, b->size); } strcpy(b->data + b->used, toadd); b->used += addlen; }
/* Expend the size of the dict */ static void xbt_dict_rehash(xbt_dict_t dict) { const int oldsize = dict->table_size + 1; int newsize = oldsize * 2; int i; xbt_dictelm_t *currcell; xbt_dictelm_t *twincell; xbt_dictelm_t bucklet; xbt_dictelm_t *pprev; currcell = (xbt_dictelm_t *) xbt_realloc((char *) dict->table, newsize * sizeof(xbt_dictelm_t)); memset(&currcell[oldsize], 0, oldsize * sizeof(xbt_dictelm_t)); /* zero second half */ dict->table_size = --newsize; dict->table = currcell; XBT_DEBUG("REHASH (%d->%d)", oldsize, newsize); for (i = 0; i < oldsize; i++, currcell++) { if (!*currcell) /* empty cell */ continue; twincell = currcell + oldsize; for (pprev = currcell, bucklet = *currcell; bucklet; bucklet = *pprev) { /* Since we use "& size" instead of "%size" and since the size was doubled, each bucklet of this cell must either : - stay in cell i (ie, currcell) - go to the cell i+oldsize (ie, twincell) */ if ((bucklet->hash_code & newsize) != i) { /* Move to b */ *pprev = bucklet->next; bucklet->next = *twincell; if (!*twincell) dict->fill++; *twincell = bucklet; continue; } else { pprev = &bucklet->next; } } if (!*currcell) /* everything moved */ dict->fill--; } }
static inline void saturated_constraint_set_update(double usage, int cnst_light_num, dyn_light_t saturated_constraint_set, double *min_usage) { xbt_assert(usage > 0,"Impossible"); if (*min_usage < 0 || *min_usage > usage) { *min_usage = usage; XBT_HERE(" min_usage=%f (cnst->remaining / cnst->usage =%f)", *min_usage, usage); saturated_constraint_set->data[0] = cnst_light_num; saturated_constraint_set->pos = 1; } else if (*min_usage == usage) { if(saturated_constraint_set->pos == saturated_constraint_set->size) { // realloc the size saturated_constraint_set->size *= 2; saturated_constraint_set->data = (int*) xbt_realloc(saturated_constraint_set->data, (saturated_constraint_set->size) * sizeof(int)); } saturated_constraint_set->data[saturated_constraint_set->pos] = cnst_light_num; saturated_constraint_set->pos++; } }
int find_model_description(s_surf_model_description_t * table, const char *name) { int i; char *name_list = NULL; for (i = 0; table[i].name; i++) if (!strcmp(name, table[i].name)) { return i; } name_list = strdup(table[0].name); for (i = 1; table[i].name; i++) { name_list = xbt_realloc(name_list, strlen(name_list) + strlen(table[i].name) + 3); strcat(name_list, ", "); strcat(name_list, table[i].name); } xbt_die("Model '%s' is invalid! Valid models are: %s.", name, name_list); return -1; }
int find_coll_description(s_mpi_coll_description_t * table, char *name, const char *desc) { int i; char *name_list = NULL; int selector_on=0; if(name==NULL){//no argument provided, use active selector's algorithm name=(char*)sg_cfg_get_string("smpi/coll_selector"); selector_on=1; } for (i = 0; table[i].name; i++) if (!strcmp(name, table[i].name)) { if (strcmp(table[i].name,"default")) XBT_INFO("Switch to algorithm %s for collective %s",table[i].name,desc); return i; } if(selector_on){ // collective seems not handled by the active selector, try with default one name=(char*)"default"; for (i = 0; table[i].name; i++) if (!strcmp(name, table[i].name)) { return i; } } if (!table[0].name) xbt_die("No collective is valid for '%s'! This is a bug.",name); name_list = xbt_strdup(table[0].name); for (i = 1; table[i].name; i++) { name_list = xbt_realloc(name_list, strlen(name_list) + strlen(table[i].name) + 3); strcat(name_list, ", "); strcat(name_list, table[i].name); } xbt_die("Collective '%s' is invalid! Valid collectives are: %s.", name, name_list); return -1; }
memory_map_t get_memory_map(void) { FILE *fp; /* File pointer to process's proc maps file */ char *line = NULL; /* Temporal storage for each line that is readed */ ssize_t read; /* Number of bytes readed */ size_t n = 0; /* Amount of bytes to read by xbt_getline */ memory_map_t ret = NULL; /* The memory map to return */ /* The following variables are used during the parsing of the file "maps" */ s_map_region_t memreg; /* temporal map region used for creating the map */ char *lfields[6], *tok, *endptr; int i; /* Open the actual process's proc maps file and create the memory_map_t */ /* to be returned. */ fp = fopen("/proc/self/maps", "r"); if(fp == NULL) perror("fopen failed"); xbt_assert(fp, "Cannot open /proc/self/maps to investigate the memory map of the process. Please report this bug."); setbuf(fp, NULL); ret = xbt_new0(s_memory_map_t, 1); /* Read one line at the time, parse it and add it to the memory map to be returned */ while ((read = xbt_getline(&line, &n, fp)) != -1) { //fprintf(stderr,"%s", line); /* Wipeout the new line character */ line[read - 1] = '\0'; /* Tokenize the line using spaces as delimiters and store each token */ /* in lfields array. We expect 5 tokens/fields */ lfields[0] = strtok(line, " "); for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) { lfields[i] = strtok(NULL, " "); } /* Check to see if we got the expected amount of columns */ if (i < 6) xbt_abort(); /* Ok we are good enough to try to get the info we need */ /* First get the start and the end address of the map */ tok = strtok(lfields[0], "-"); if (tok == NULL) xbt_abort(); memreg.start_addr = (void *) strtoul(tok, &endptr, 16); /* Make sure that the entire string was an hex number */ if (*endptr != '\0') xbt_abort(); tok = strtok(NULL, "-"); if (tok == NULL) xbt_abort(); memreg.end_addr = (void *) strtoul(tok, &endptr, 16); /* Make sure that the entire string was an hex number */ if (*endptr != '\0') xbt_abort(); /* Get the permissions flags */ if (strlen(lfields[1]) < 4) xbt_abort(); memreg.prot = 0; for (i = 0; i < 3; i++){ switch(lfields[1][i]){ case 'r': memreg.prot |= PROT_READ; break; case 'w': memreg.prot |= PROT_WRITE; break; case 'x': memreg.prot |= PROT_EXEC; break; default: break; } } if (memreg.prot == 0) memreg.prot |= PROT_NONE; if (lfields[1][4] == 'p') memreg.flags |= MAP_PRIVATE; else if (lfields[1][4] == 's') memreg.flags |= MAP_SHARED; /* Get the offset value */ memreg.offset = (void *) strtoul(lfields[2], &endptr, 16); /* Make sure that the entire string was an hex number */ if (*endptr != '\0') xbt_abort(); /* Get the device major:minor bytes */ tok = strtok(lfields[3], ":"); if (tok == NULL) xbt_abort(); memreg.dev_major = (char) strtoul(tok, &endptr, 16); /* Make sure that the entire string was an hex number */ if (*endptr != '\0') xbt_abort(); tok = strtok(NULL, ":"); if (tok == NULL) xbt_abort(); memreg.dev_minor = (char) strtoul(tok, &endptr, 16); /* Make sure that the entire string was an hex number */ if (*endptr != '\0') xbt_abort(); /* Get the inode number and make sure that the entire string was a long int */ memreg.inode = strtoul(lfields[4], &endptr, 10); if (*endptr != '\0') xbt_abort(); /* And finally get the pathname */ memreg.pathname = xbt_strdup(lfields[5]); /* Create space for a new map region in the region's array and copy the */ /* parsed stuff from the temporal memreg variable */ ret->regions = xbt_realloc(ret->regions, sizeof(memreg) * (ret->mapsize + 1)); memcpy(ret->regions + ret->mapsize, &memreg, sizeof(memreg)); ret->mapsize++; } free(line); fclose(fp); return ret; }
/* Printing Objective */ var_list = &(sys->variable_set); sprintf(print_buf, "MAX-MIN ( "); trace_buf = xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1); strcat(trace_buf, print_buf); xbt_swag_foreach(var, var_list) { sprintf(print_buf, "'%d'(%f) ", var->id_int, var->weight); trace_buf = xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1); strcat(trace_buf, print_buf); } sprintf(print_buf, ")"); trace_buf = xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1); strcat(trace_buf, print_buf); XBT_DEBUG("%20s", trace_buf); trace_buf[0] = '\000'; XBT_DEBUG("Constraints"); /* Printing Constraints */ cnst_list = &(sys->active_constraint_set); xbt_swag_foreach(cnst, cnst_list) { sum = 0.0; elem_list = &(cnst->element_set); sprintf(print_buf, "\t"); trace_buf = xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1); strcat(trace_buf, print_buf); sprintf(print_buf, "%s(",(cnst->shared)?"":"max");
/** @brief Replaces a set of variables by their values * * @param b buffer to modify * @param patterns variables to substitute in the buffer * * Both '$toto' and '${toto}' are valid (and the two writing are equivalent). * * If the variable name contains spaces, use the brace version (ie, ${toto tutu}) * * You can provide a default value to use if the variable is not set in the dict by using * '${var:=default}' or '${var:-default}'. These two forms are equivalent, even if they * shouldn't to respect the shell standard (:= form should set the value in the dict, * but does not) (BUG). */ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns) { char *end; /* pointers around the parsed chunk */ int in_simple_quote = 0, in_double_quote = 0; int done = 0; if (b->data[0] == '\0') return; end = b->data; while (!done) { switch (*end) { case '\\': /* Protected char; pass the protection */ end++; xbt_assert(*end != '\0', "String ends with \\"); break; case '\'': if (!in_double_quote) { /* simple quote not protected by double ones, note it */ in_simple_quote = !in_simple_quote; } break; case '"': if (!in_simple_quote) { /* double quote protected by simple ones, note it */ in_double_quote = !in_double_quote; } break; case '$': if (!in_simple_quote) { /* Go for the substitution. First search the variable name */ char *beg_var, *end_var; /* variable name boundary */ char *beg_subst, *end_subst = NULL; /* where value should be written to */ char *value, *default_value = NULL; int val_len; beg_subst = end; if (*(++end) == '{') { /* the variable name is enclosed in braces. */ beg_var = end + 1; /* Search name's end */ end_var = beg_var; while (*end_var != '\0' && *end_var != '}') { /* TODO: we do not respect the standard for ":=", we should set this value in the dict */ if (*end_var == ':' && ((*(end_var + 1) == '=') || (*(end_var + 1) == '-'))) { /* damn, we have a default value */ char *p = end_var + 1; while (*p != '\0' && *p != '}') p++; xbt_assert (*p != '\0', "Variable default value not terminated ('}' missing)"); default_value = xbt_malloc(p - end_var - 1); memcpy(default_value, end_var + 2, p - end_var - 2); default_value[p - end_var - 2] = '\0'; end_subst = p + 1; /* eat '}' */ break; } end_var++; } xbt_assert(*end_var != '\0', "Variable name not terminated ('}' missing)"); if (!end_subst) /* already set if there's a default value */ end_subst = end_var + 1; /* also kill the } in the name */ xbt_assert(end_var != beg_var, "Variable name empty (${} is not valid)"); } else { /* name given directly */ beg_var = end; end_var = beg_var; while (*end_var != '\0' && *end_var != ' ' && *end_var != '\t' && *end_var != '\n') end_var++; end_subst = end_var; xbt_assert (end_var != beg_var, "Variable name empty ($ is not valid)"); } /* ok, we now have the variable name. Search the dictionary for the substituted value */ value = xbt_dict_get_or_null_ext(patterns, beg_var, end_var - beg_var); if (value) value = xbt_strdup(value); else if (default_value) value = xbt_strdup(default_value); else value = xbt_strdup(""); /* En route for the actual substitution */ val_len = strlen(value); if (val_len <= end_subst - beg_subst) { /* enough room to do the substitute in place */ memmove(beg_subst, value, val_len); /* substitute */ memmove(beg_subst + val_len, end_subst, b->used - (end_subst - b->data) + 1); /* move the end of the string closer */ // XBT_DEBUG("String is now: '%s'",b->data); end = beg_subst + val_len; /* update the currently explored char in the overall loop */ // XBT_DEBUG("end of substituted section is now '%s'",end); b->used -= end_subst - beg_subst - val_len; /* update string buffer used size */ // XBT_DEBUG("Used:%d end:%d ending char:%d",b->used,end-b->data,*end); } else { /* we have to extend the data area */ int tooshort = val_len - (end_subst - beg_subst) + 1 /* don't forget \0 */ ; int newused = b->used + tooshort; end += tooshort; /* update the pointer of the overall loop */ // XBT_DEBUG("Too short (by %d chars; %d chars left in area)",val_len- (end_subst-beg_subst), b->size - b->used); if (newused > b->size) { /* We have to realloc the data area before (because b->size is too small). We have to update our pointers, too */ char *newdata = xbt_realloc(b->data, b->used + MAX(minimal_increment, tooshort)); int offset = newdata - b->data; b->data = newdata; b->size = b->used + MAX(minimal_increment, tooshort); end += offset; beg_subst += offset; end_subst += offset; } memmove(beg_subst + val_len, end_subst, b->used - (end_subst - b->data) + 1); /* move the end of the string a bit further */ memmove(beg_subst, value, val_len); /* substitute */ b->used = newused; // XBT_DEBUG("String is now: %s",b->data); } free(value); free(default_value); end--; /* compensate the next end++ */ } break; case '\0': done = 1; break; } end++; } }