+static size_t shm_size(int fd) {
+ struct stat st;
+
+ if(fstat(fd, &st) < 0) {
+ xbt_die("Could not stat fd %d: %s", fd, strerror(errno));
+ }
+ return (size_t)st.st_size;
+}
+
+#ifndef WIN32
+static void* shm_map(int fd, size_t size, shared_data_t* data) {
+ void* mem;
+ char loc[PTR_STRLEN];
+ shared_metadata_t* meta;
+
+ if(size > shm_size(fd)) {
+ if(ftruncate(fd, (off_t)size) < 0) {
+ xbt_die("Could not truncate fd %d to %zu: %s", fd, size, strerror(errno));
+ }
+ }
+
+ mem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ if(mem == MAP_FAILED) {
+ xbt_die("Could not map fd %d: %s", fd, strerror(errno));
+ }
+ if(!allocs_metadata) {
+ allocs_metadata = xbt_dict_new();
+ }
+ snprintf(loc, PTR_STRLEN, "%p", mem);
+ meta = xbt_new(shared_metadata_t, 1);
+ meta->size = size;
+ meta->data = data;
+ xbt_dict_set(allocs_metadata, loc, meta, &free);
+ XBT_DEBUG("MMAP %zu to %p", size, mem);
+ return mem;
+}
+#endif
+
+void smpi_bench_destroy(void)
+{
+ xbt_dict_free(&allocs);
+ xbt_dict_free(&samples);
+ xbt_dict_free(&calls);
+}
+
+void smpi_execute_flops(double flops) {
+ smx_action_t action;
+ smx_host_t host;
+ host = SIMIX_host_self();
+
+ XBT_DEBUG("Handle real computation time: %f flops", flops);
+ action = simcall_host_execute("computation", host, flops, 1);
+#ifdef HAVE_TRACING
+ simcall_set_category (action, TRACE_internal_smpi_get_category());
+#endif
+ simcall_host_execution_wait(action);
+}
+
+static void smpi_execute(double duration)
+{
+ /* FIXME: a global variable would be less expensive to consult than a call to xbt_cfg_get_double() right on the critical path */
+ if (duration >= sg_cfg_get_double("smpi/cpu_threshold")) {
+ XBT_DEBUG("Sleep for %f to handle real computation time", duration);
+ smpi_execute_flops(duration *
+ sg_cfg_get_double("smpi/running_power"));
+ } else {
+ XBT_DEBUG("Real computation took %f while option smpi/cpu_threshold is set to %f => ignore it",
+ duration, sg_cfg_get_double("smpi/cpu_threshold"));
+ }
+}
+
+void smpi_bench_begin(void)
+{
+ xbt_os_timer_start(smpi_process_timer());
+ smpi_current_rank = smpi_process_index();
+}
+
+void smpi_bench_end(void)
+{
+ xbt_os_timer_t timer = smpi_process_timer();
+
+ xbt_os_timer_stop(timer);
+ smpi_execute(xbt_os_timer_elapsed(timer));
+}
+
+unsigned int smpi_sleep(unsigned int secs)
+{
+ smpi_bench_end();
+ smpi_execute((double) secs);
+ smpi_bench_begin();
+ return secs;
+}