Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
send test program
[simgrid.git] / src / smpi / smpi_bench.c
1 /* Copyright (c) 2007, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5   * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "private.h"
8 #include "xbt/dict.h"
9 #include "xbt/sysdep.h"
10 #include "xbt/ex.h"
11 #include "surf/surf.h"
12
13 #include <sys/mman.h>
14 #include <sys/stat.h>
15 #include <sys/types.h>
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <string.h>
20 #include <stdio.h>
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_bench, smpi,
23                                 "Logging specific to SMPI (benchmarking)");
24
25 /* Shared allocations are handled through shared memory segments.
26  * Associated data and metadata are used as follows:
27  *
28  *                                                                    mmap #1
29  *    `allocs' dict                                                     ---- -.
30  *    ----------      shared_data_t               shared_metadata_t   / |  |  |
31  * .->| <name> | ---> -------------------- <--.   -----------------   | |  |  |
32  * |  ----------      | fd of <name>     |    |   | size of mmap  | --| |  |  |
33  * |                  | count (2)        |    |-- | data          |   \ |  |  |
34  * `----------------- | <name>           |    |   -----------------     ----  |
35  *                    --------------------    |   ^                           |
36  *                                            |   |                           |
37  *                                            |   |   `allocs_metadata' dict  |
38  *                                            |   |   ----------------------  |
39  *                                            |   `-- | <addr of mmap #1>  |<-'
40  *                                            |   .-- | <addr of mmap #2>  |<-.
41  *                                            |   |   ----------------------  |
42  *                                            |   |                           |
43  *                                            |   |                           |
44  *                                            |   |                           |
45  *                                            |   |                   mmap #2 |
46  *                                            |   v                     ---- -'
47  *                                            |   shared_metadata_t   / |  |
48  *                                            |   -----------------   | |  |
49  *                                            |   | size of mmap  | --| |  |
50  *                                            `-- | data          |   | |  |
51  *                                                -----------------   | |  |
52  *                                                                    \ |  |
53  *                                                                      ----
54  */
55
56 #define PTR_STRLEN (2 + 2 * sizeof(void*) + 1)
57
58 xbt_dict_t allocs = NULL;          /* Allocated on first use */
59 xbt_dict_t allocs_metadata = NULL; /* Allocated on first use */
60 xbt_dict_t samples = NULL;         /* Allocated on first use */
61 xbt_dict_t calls = NULL;           /* Allocated on first use */
62 __thread int smpi_current_rank = 0;      /* Updated after each MPI call */
63
64 typedef struct {
65   int fd;
66   int count;
67   char* loc;
68 } shared_data_t;
69
70 typedef struct  {
71   size_t size;
72   shared_data_t* data;
73 } shared_metadata_t;
74
75 static size_t shm_size(int fd) {
76   struct stat st;
77
78   if(fstat(fd, &st) < 0) {
79     xbt_die("Could not stat fd %d: %s", fd, strerror(errno));
80   }
81   return (size_t)st.st_size;
82 }
83
84 static void* shm_map(int fd, size_t size, shared_data_t* data) {
85   void* mem;
86   char loc[PTR_STRLEN];
87   shared_metadata_t* meta;
88
89   if(size > shm_size(fd)) {
90     if(ftruncate(fd, (off_t)size) < 0) {
91       xbt_die("Could not truncate fd %d to %zu: %s", fd, size, strerror(errno));
92     }
93   }
94   mem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
95   if(mem == MAP_FAILED) {
96     xbt_die("Could not map fd %d: %s", fd, strerror(errno));
97   }
98   if(!allocs_metadata) {
99     allocs_metadata = xbt_dict_new();
100   }
101   snprintf(loc, PTR_STRLEN, "%p", mem);
102   meta = xbt_new(shared_metadata_t, 1);
103   meta->size = size;
104   meta->data = data;
105   xbt_dict_set(allocs_metadata, loc, meta, &free);
106   XBT_DEBUG("MMAP %zu to %p", size, mem);
107   return mem;
108 }
109
110 typedef struct {
111   int count;
112   double sum;
113   double sum_pow2;
114   double mean;
115   double relstderr;
116   int iters;
117   double threshold;
118   int started;
119 } local_data_t;
120
121 void smpi_bench_destroy(void)
122 {
123   if (allocs) {
124     xbt_dict_free(&allocs);
125   }
126   if (samples) {
127     xbt_dict_free(&samples);
128   }
129   if(calls) {
130     xbt_dict_free(&calls);
131   }
132 }
133
134 static void smpi_execute_flops(double flops)
135 {
136   smx_action_t action;
137   smx_host_t host;
138   host = SIMIX_host_self();
139
140   XBT_DEBUG("Handle real computation time: %f flops", flops);
141   action = SIMIX_req_host_execute("computation", host, flops, 1);
142 #ifdef HAVE_TRACING
143   SIMIX_req_set_category (action, TRACE_internal_smpi_get_category());
144 #endif
145   SIMIX_req_host_execution_wait(action);
146 }
147
148 static void smpi_execute(double duration)
149 {
150   if (duration >= xbt_cfg_get_double(_surf_cfg_set, "smpi/cpu_threshold")) {
151     XBT_DEBUG("Sleep for %f to handle real computation time", duration);
152     smpi_execute_flops(duration *
153                        xbt_cfg_get_double(_surf_cfg_set,
154                                           "smpi/running_power"));
155   }
156 }
157
158 void smpi_bench_begin(void)
159 {
160   xbt_os_timer_start(smpi_process_timer());
161   smpi_current_rank = smpi_process_index();
162 }
163
164 void smpi_bench_end(void)
165 {
166   xbt_os_timer_t timer = smpi_process_timer();
167
168   xbt_os_timer_stop(timer);
169   smpi_execute(xbt_os_timer_elapsed(timer));
170 }
171
172 unsigned int smpi_sleep(unsigned int secs)
173 {
174   smpi_bench_end();
175   smpi_execute((double) secs);
176   smpi_bench_begin();
177   return secs;
178 }
179
180 int smpi_gettimeofday(struct timeval *tv, struct timezone *tz)
181 {
182   double now;
183   smpi_bench_end();
184   now = SIMIX_get_clock();
185   if (tv) {
186     tv->tv_sec = (time_t)now;
187     tv->tv_usec = (suseconds_t)((now - tv->tv_sec) * 1e6);
188   }
189   smpi_bench_begin();
190   return 0;
191 }
192
193 static char *sample_location(int global, const char *file, int line)
194 {
195   if (global) {
196     return bprintf("%s:%d", file, line);
197   } else {
198     return bprintf("%s:%d:%d", file, line, smpi_process_index());
199   }
200 }
201
202 int smpi_sample_1(int global, const char *file, int line, int iters, double threshold)
203 {
204   char *loc = sample_location(global, file, line);
205   local_data_t *data;
206
207   smpi_bench_end();     /* Take time from previous MPI call into account */
208   if (!samples) {
209     samples = xbt_dict_new();
210   }
211   data = xbt_dict_get_or_null(samples, loc);
212   if (!data) {
213     data = (local_data_t *) xbt_new(local_data_t, 1);
214     data->count = 0;
215     data->sum = 0.0;
216     data->sum_pow2 = 0.0;
217     data->iters = iters;
218     data->threshold = threshold;
219     data->started = 0;
220     xbt_dict_set(samples, loc, data, &free);
221     return 0;
222   }
223   free(loc);
224   return 1;
225 }
226
227 int smpi_sample_2(int global, const char *file, int line)
228 {
229   char *loc = sample_location(global, file, line);
230   local_data_t *data;
231
232   xbt_assert(samples, "You did something very inconsistent, didn't you?");
233   data = xbt_dict_get_or_null(samples, loc);
234   if (!data) {
235     xbt_assert(data, "Please, do thing in order");
236   }
237   if (!data->started) {
238     if ((data->iters > 0 && data->count >= data->iters)
239         || (data->count > 1 && data->threshold > 0.0 && data->relstderr <= data->threshold)) {
240       XBT_DEBUG("Perform some wait of %f", data->mean);
241       smpi_execute(data->mean);
242     } else {
243       data->started = 1;
244       data->count++;
245     }
246   } else {
247     data->started = 0;
248   }
249   free(loc);
250   smpi_bench_begin();
251   smpi_process_simulated_start();
252   return data->started;
253 }
254
255 void smpi_sample_3(int global, const char *file, int line)
256 {
257   char *loc = sample_location(global, file, line);
258   local_data_t *data;
259   double sample, n;
260
261   xbt_assert(samples, "You did something very inconsistent, didn't you?");
262   data = xbt_dict_get_or_null(samples, loc);
263   smpi_bench_end();
264   if(data && data->started && data->count < data->iters) {
265     sample = smpi_process_simulated_elapsed();
266     data->sum += sample;
267     data->sum_pow2 += sample * sample;
268     n = (double)data->count;
269     data->mean = data->sum / n;
270     data->relstderr = sqrt((data->sum_pow2 / n - data->mean * data->mean) / n) / data->mean;
271     XBT_DEBUG("Average mean after %d steps is %f, relative standard error is %f (sample was %f)", data->count,
272            data->mean, data->relstderr, sample);
273   }
274   free(loc);
275 }
276
277 void smpi_sample_flops(double flops)
278 {
279   smpi_execute_flops(flops);
280 }
281
282 void *smpi_shared_malloc(size_t size, const char *file, int line)
283 {
284   char *loc = bprintf("%zu_%s_%d", (size_t)getpid(), file, line);
285   size_t len = strlen(loc);
286   size_t i;
287   int fd;
288   void* mem;
289   shared_data_t *data;
290
291   for(i = 0; i < len; i++) {
292     /* Make the 'loc' ID be a flat filename */
293     if(loc[i] == '/') {
294       loc[i] = '_';
295     }
296   }
297   if (!allocs) {
298     allocs = xbt_dict_new();
299   }
300   data = xbt_dict_get_or_null(allocs, loc);
301   if(!data) {
302     fd = shm_open(loc, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
303     if(fd < 0) {
304       switch(errno) {
305         case EEXIST:
306           xbt_die("Please cleanup /dev/shm/%s", loc);
307         default:
308           xbt_die("An unhandled error occured while opening %s: %s", loc, strerror(errno));
309       }
310     }
311     data = xbt_new(shared_data_t, 1);
312     data->fd = fd;
313     data->count = 1;
314     data->loc = loc;
315     mem = shm_map(fd, size, data);
316     if(shm_unlink(loc) < 0) {
317       XBT_WARN("Could not early unlink %s: %s", loc, strerror(errno));
318     }
319     xbt_dict_set(allocs, loc, data, &free);
320     XBT_DEBUG("Mapping %s at %p through %d", loc, mem, fd);
321   } else {
322     mem = shm_map(data->fd, size, data);
323     data->count++;
324   }
325   XBT_DEBUG("Malloc %zu in %p (metadata at %p)", size, mem, data);
326   return mem;
327 }
328
329 void smpi_shared_free(void *ptr)
330 {
331   char loc[PTR_STRLEN];
332   shared_metadata_t* meta;
333   shared_data_t* data;
334
335   if (!allocs) {
336     XBT_WARN("Cannot free: nothing was allocated");
337     return;
338   }
339   if(!allocs_metadata) {
340     XBT_WARN("Cannot free: no metadata was allocated");
341   }
342   snprintf(loc, PTR_STRLEN, "%p", ptr);
343   meta = (shared_metadata_t*)xbt_dict_get_or_null(allocs_metadata, loc);
344   if (!meta) {
345     XBT_WARN("Cannot free: %p was not shared-allocated by SMPI", ptr);
346     return;
347   }
348   data = meta->data;
349   if(!data) {
350     XBT_WARN("Cannot free: something is broken in the metadata link");
351     return;
352   }
353   if(munmap(ptr, meta->size) < 0) {
354     XBT_WARN("Unmapping of fd %d failed: %s", data->fd, strerror(errno));
355   }
356   data->count--;
357   if (data->count <= 0) {
358     close(data->fd);
359     xbt_dict_remove(allocs, data->loc);
360     free(data->loc);
361   }
362 }
363
364 int smpi_shared_known_call(const char* func, const char* input) {
365    char* loc = bprintf("%s:%s", func, input);
366    xbt_ex_t ex;
367    int known;
368
369    if(!calls) {
370       calls = xbt_dict_new();
371    }
372    TRY {
373       xbt_dict_get(calls, loc); /* Succeed or throw */
374       known = 1;
375    }
376    CATCH(ex) {
377       if(ex.category == not_found_error) {
378          known = 0;
379          xbt_ex_free(ex);
380       } else {
381          RETHROW;
382       }
383    }
384    free(loc);
385    return known;
386 }
387
388 void* smpi_shared_get_call(const char* func, const char* input) {
389    char* loc = bprintf("%s:%s", func, input);
390    void* data;
391
392    if(!calls) {
393       calls = xbt_dict_new();
394    }
395    data = xbt_dict_get(calls, loc);
396    free(loc);
397    return data;
398 }
399
400 void* smpi_shared_set_call(const char* func, const char* input, void* data) {
401    char* loc = bprintf("%s:%s", func, input);
402
403    if(!calls) {
404       calls = xbt_dict_new();
405    }
406    xbt_dict_set(calls, loc, data, NULL);
407    free(loc);
408    return data;
409 }