PadicoTM

Documentation

« back to PM2 home.
shm-sysdep.c
Go to the documentation of this file.
1/*
2 * Padico -- a High Performance Parallel and Distributed Computing Environment
3 * Copyright (c) 2002-2026 INRIA and the University of Rennes 1
4 * Alexandre DENIS <Alexandre.Denis@inria.fr>
5 * Christian PEREZ <Christian.Perez@inria.fr>
6 *
7 * The software has been registered at the Agency for the Protection of
8 * Programs (APP) under the number IDDN.FR.001.260013.000.S.P.2002.000.10000.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
28#include <Padico/Module.h>
29
30#if defined(MARCEL)
31# warning "Shm: Marcel is enabled- not building module 'Shm'"
32#else /* MARCEL */
33
35
36#include "Shm.h"
37#include <Padico/PadicoTM.h>
38#include <Padico/Topology.h>
39
40#include <unistd.h>
41#include <signal.h>
42#include <semaphore.h>
43#ifdef HAVE_SYS_VFS_H
44#include <sys/vfs.h>
45#endif /* HAVE_SYS_VFS_H */
46
47
48static void padico_shm_seg_init(struct padico_shm_s*shm);
52
53
54/* ********************************************************* */
55/* ** segment creation */
56
57#ifdef SHM_USE_POSIX
58
60{
61 static const int max_retry = 20;
62 int fd = -1;
63 int count = 0;
64 int err = 0;
65
66 struct padico_shm_s*shm = padico_malloc(sizeof(struct padico_shm_s));
67 shm->seg_size = (payload < 0) ? sizeof(struct padico_shm_seg_full_s) : (sizeof(struct padico_shm_seg_directory_s) + payload);
68 shm->rank = -1;
69 shm->self = NULL;
71 shm->seg_mbox = NULL;
72 shm->shm_path = NULL;
73 shm->sem_path = NULL;
74
76 padico_string_printf(p, "/dev/shm/shm-%s-%s.tmp", name, padico_getenv("USER"));
79
80#ifdef HAVE_SYS_VFS_H
81 {
82 struct statfs s;
83 int rc = statfs("/dev/shm", &s);
84 if(rc == -1)
85 padico_fatal("statfs() failed for /dev/shm.\n");
86 const long long free_size = s.f_bsize * s.f_bavail;
87 const long long total_size = s.f_bsize * s.f_blocks;
88 const long long need_size = shm->seg_size;
89 padico_print("/dev/shm total size = %lld; free size = %lld; needed size = %lld\n",
90 total_size, free_size, need_size);
92 {
93 padico_fatal("not enough free space on /dev/shm; need at least %lld bytes.\n", need_size);
94 }
95 }
96#endif /* HAVE_SYS_VFS_H */
97
98 padico_out(20, "opening shared segment.\n");
99 do
100 {
103 count++;
104 }
105 while(count < max_retry && fd == -1 && err == EINTR);
106 if(fd == -1)
107 padico_fatal("cannot create shared segment: %s (%d)- count=%d\n", strerror(err), err, count);
108 int rc = PUK_ABI_FSYS_WRAP(ftruncate)(fd, shm->seg_size);
110 if(rc == -1)
111 padico_fatal("%s (%d).\n", strerror(err), err);
112 padico_out(20, "mapping shared segment.\n");
115 if(seg == MAP_FAILED)
116 padico_fatal("error while mapping shared segment: %s (%d)- fd = %d.\n", strerror(err), err, fd);
117 PUK_ABI_FSYS_WRAP(close)(fd);
118 shm->seg_directory = &seg->seg_directory;
119 if(payload < 0)
120 {
121 shm->seg_mbox = &seg->seg_mbox;
122 }
123 padico_out(20, "shared segment ok.\n");
124
125 /* acquire semaphore */
127 padico_string_printf(s_sem_path, "/shm-%s-%s.sem", name, padico_getenv("USER"));
130 shm->sem = NULL;
132 errno = 0;
133 shm->sem = sem_open(shm->sem_path, O_CREAT, 0600, 1);
134 if(shm->sem == NULL)
135 {
136 padico_warning("error %d (%s) while opening semaphore.\n", errno, strerror(errno));
137 puk_sleep(2);
138 goto retry_open;
139 }
140 struct timespec t;
142 t.tv_sec += 3;
144 rc = sem_timedwait(shm->sem, &t);
145 if(rc == -1)
146 {
147 if(errno == EINTR)
148 goto retry_wait;
149 else if(errno == ETIMEDOUT)
150 {
151 padico_warning("timeout on semaphore wait. Recovering shared segment.\n");
152 sem_close(shm->sem);
154 puk_sleep(getpid() % 5);
155 goto retry_open;
156 }
157 else
158 {
159 padico_fatal("error %d while waiting for semaphore (%s).\n", errno, strerror(errno));
160 }
161 }
162 rc = sem_post(shm->sem);
163 assert(rc == 0);
164 rc = sem_wait(shm->sem);
165 if(rc != 0)
166 {
167 padico_fatal("failed to acquire semaphore; error %d (%s).\n", errno, strerror(errno));
168 }
169
170 int val = -1;
172 if(val > 0)
173 {
174 padico_fatal("semaphore %s in inconsistent state (val = %d).\n", shm->sem_path, val);
175 }
176
177 if( (strncmp(seg->seg_directory.build_string.date, __DATE__, strlen(__DATE__)) != 0 ) ||
178 (strncmp(seg->seg_directory.build_string.time, __TIME__, strlen(__TIME__)) != 0) )
179 {
181 }
182 else
183 {
185 }
186
187 /* register self node */
189
190 /* release semaphore */
191 sem_post(shm->sem);
192
193 padico_print("%d x %dKB blocks- %d slots; directory = %zuKB; buffer = %zuKB\n",
195 sizeof(shm->seg_directory->directory) / 1024,
196 shm->seg_mbox ? sizeof(shm->seg_mbox->buffer) / 1024 : 0);
197
198 return shm;
199}
200
202{
203 if(shm->shm_path)
204 {
205 int rc = sem_wait(shm->sem);
206 if(rc != 0)
207 {
208 padico_fatal("failed to acquire semaphore; error %d (%s).\n", errno, strerror(errno));
209 }
211 /* check whether we are last process */
212 int numnodes = 0;
213 int i;
214 for(i = 0; i < PADICO_SHM_NUMNODES; i++)
215 {
217 if(n->enabled)
218 {
219 numnodes++;
220 }
221 }
222 if(numnodes == 0)
223 {
224 padico_out(10, "last process; unlink shm path %s\n", shm->shm_path);
226 }
227 rc = sem_post(shm->sem);
228 assert(rc == 0);
231 }
232 sem_close(shm->sem);
233 if(shm->sem_path)
234 {
235 /* TODO: find a way to unlink the semaphore without creating race condition */
236 /*
237 sem_unlink(shm->sem_path);
238 */
240 }
242}
243
244#elif SHM_USE_IPC
245# error "Shm IPC not implemented yet"
246#else
247# error "No shm method defined"
248#endif
249
250
251/* ********************************************************* */
252/* ** segment and directory management */
253
289
294{
295 int numnodes = 0;
296 int i;
297 for(i = 0; i < PADICO_SHM_NUMNODES; i++)
298 {
300 if(n->enabled)
301 {
302 int pid = n->pid;
303 int rc = kill(pid, 0); /* check that the process is running */
304 if(rc)
305 {
306 padico_print("cleaning node %s (pid=%d)\n", n->node_uuid, n->pid);
307 n->enabled = 0;
308 n->pid = -1;
309 }
310 else
311 numnodes++;
312 }
313 }
314 if(numnodes == 0)
315 {
316 /* no nodes in directory- reinit for safety (recover all blocks, etc.) */
318 }
319}
320
322{
323 int retry_count = 100;
324 int i;
326 retry:
327 for(i = 0; i < PADICO_SHM_NUMNODES; i++)
328 {
330 if(n->enabled && memcmp(n->node_uuid, uuid, PADICO_TOPO_UUID_SIZE) == 0)
331 {
332 return n;
333 }
334 }
335 if(retry_count-- > 0)
336 {
338 puk_usleep(1000);
339 goto retry;
340 }
341 return NULL;
342}
343
348{
349 int i;
350 /* find slot */
351 retry:
352 for(i = 0; i < PADICO_SHM_NUMNODES; i++)
353 {
355 if(!node->enabled)
356 {
357 int e = __sync_fetch_and_add(&node->enabled, 1);
358 if(e != 0)
359 {
360 __sync_fetch_and_sub(&node->enabled, 1);
361 padico_warning("race detected while registering self; retrying...\n");
362 goto retry;
363 }
364 assert(node->enabled == 1);
365 node->pid = getpid();
366 node->rank = i;
367 if(shm->seg_mbox != NULL)
368 {
371 int j;
372 for(j = 0; j < PADICO_SHM_NUMNODES; j++)
373 {
376 }
377 }
378 shm->rank = i;
379 shm->self = node;
380 /* fill uuid last so that other nodes don't use the entry before it's complete */
382 node->node_uuid[PADICO_TOPO_UUID_SIZE] = '\0';
383 padico_out(40, "I am node #%d\n", i);
385 if(self == NULL)
386 {
387 padico_fatal("inconsistency detected in directory.\n");
388 }
389 return;
390 }
391 }
392 padico_fatal("cannot register self- directory full.\n");
393}
394
399{
400 struct padico_shm_node_s*node = shm->self;
401 assert(node->pid == getpid());
402 node->enabled = 0;
403 node->pid = -1;
404 shm->rank = -1;
405 shm->self = NULL;
406}
407
409{
410 int i;
411 for(i = 0; i < PADICO_SHM_NUMNODES; i++)
412 {
414 fprintf(stderr, "# local = %s; i = %d; enabled = %d; rank = %d; uuid = %s \n",
415 (const char*)padico_topo_node_getuuid(padico_topo_getlocalnode()), i, node->enabled, node->rank, node->node_uuid);
416 }
417}
418
419
420/* ********************************************************* */
421/* ** blocks */
422
432
433
434/* ********************************************************* */
435/* ** large messages */
436
437void padico_shm_large_send(struct padico_shm_s*shm, const void*bytes, size_t size, struct padico_shm_node_s*dest)
438{
439 padico_shm_queue_t queue = NULL;
440 while(shm->seg_mbox->mailboxes[dest->rank].large[shm->rank].pipeline == NULL)
441 {
442 /* wait the receiver to be ready to receive */
444 }
445 queue = shm->seg_mbox->mailboxes[dest->rank].large[shm->rank].pipeline;
446 shm->seg_mbox->mailboxes[dest->rank].large[shm->rank].pipeline = NULL; /* consume RTR before send to avoid races */
447 assert(queue != NULL);
449 int done = 0;
450 do
451 {
453 do
454 {
456 }
457 while(block == NULL);
459 padico_out(40, "copying block #%p\n", block);
460 const int todo = size - done;
461 const int b = (todo > PADICO_SHM_BLOCKSIZE) ? PADICO_SHM_BLOCKSIZE : todo;
462 memcpy(ptr, bytes + done, b);
463 done += b;
464 padico_out(40, "commit block #%p\n", block);
465 int rc = -1;
466 do
467 {
469 }
470 while(rc != 0);
471 }
472 while(done < size);
473 padico_out(40, "done.\n");
474}
475
477{
478 /* allocate queue and signal RTR */
479 padico_shm_queue_t queue = NULL;
480 while(queue == NULL)
482 shm->seg_mbox->mailboxes[shm->rank].large[from].pipeline = queue;
484 return queue;
485}
487{
489 int done = 0;
490 padico_out(40, "recv large- size=%d; done=%d\n", (int)size, done);
491 while(done < size)
492 {
494 padico_out(40, "waiting chunk- size=%d; done=%d\n", (int)size, done);
495 do
496 {
498 if((block == NULL) && (done == 0))
499 return -1;
500 }
501 while(block == NULL);
502 const int todo = size - done;
503 int b = (todo > PADICO_SHM_BLOCKSIZE) ? PADICO_SHM_BLOCKSIZE : todo;
505 memcpy(bytes + done, ptr, b);
506 done += b;
507 padico_out(40, "copy done- size=%d; done=%d\n", (int)size, done);
509 }
510 /* release queue */
512 return 0;
513}
514
515void padico_shm_large_recv(struct padico_shm_s*shm, void*bytes, size_t size, int from)
516{
518 int rc = -1;
519 do
520 {
521 rc = padico_shm_large_recv_poll(shm, bytes, size, queue);
522 }
523 while(rc != 0);
524}
525
526#endif /* MARCEL */
assert(slab_size > 0)
struct ns_rulebucket_hash_entry_s * e
ssize_t n
struct ns_rulebucket_hash_entry_s * b
Padico Shared memory common interface.
return NULL
Definition Shm.h:88
static struct padico_shm_large_lfqueue_s * padico_shm_queue_get_lfqueue(struct padico_shm_s *shm, padico_shm_queue_t queue)
Definition Shm.h:280
puk_tagged_ptr_t p
Definition Shm.h:88
goto retry
Definition Shm.h:88
struct padico_shm_block_s * padico_shm_block_t
offset of a block in the shared segment, stored in a pointer.
Definition Shm.h:80
#define PADICO_SHM_NUMBLOCKS
Number of blocks allocated in shm buffer.
Definition Shm.h:63
static void * padico_shm_block_get_ptr(struct padico_shm_s *shm, padico_shm_block_t block)
get the pointer to a block given its block number
Definition Shm.h:211
static void padico_shm_block_free(struct padico_shm_s *shm, padico_shm_block_t block)
return a free block back to the free blocks pool
Definition Shm.h:255
struct padico_shm_queue_s * padico_shm_queue_t
offset of a queue in the shared segment, stored in a pointer.
Definition Shm.h:85
#define PADICO_SHM_NUMNODES
Definition Shm.h:59
static void padico_shm_block_assert(struct padico_shm_s *shm, padico_shm_block_t block)
assert given offset is a valid block
Definition Shm.h:221
static padico_shm_queue_t padico_shm_queue_from_num(struct padico_shm_s *shm, int queue_num)
Definition Shm.h:271
static padico_shm_block_t padico_shm_block_from_num(struct padico_shm_s *shm, int block_num)
Definition Shm.h:243
#define PADICO_SHM_BLOCKSIZE
size of data block in segment
Definition Shm.h:65
uintptr_t s
Definition Shm.h:88
#define PADICO_SHM_NUMQUEUES
number of queues for large packets in segment
Definition Shm.h:69
padico_topo_host_t const char * uuid
struct padico_addrkey_hash_entry_block_lfstack_cell_s block[]
static struct seg_s * seg
static struct bench_node_s * self
static int done
Definition console-mod.c:49
static int size
Definition datacheck.c:34
static int fd
Definition datacheck.c:32
char * name
Definition PadicoTM.h:168
#define PUK_ABI_FSYS_WRAP(SYM)
Definition PadicoTM.h:88
padico_rc_t rc
Definition PadicoTM.h:146
#define __puk_abi_wrap__errno
Definition PadicoTM.h:101
padico_topo_node_t padico_topo_getlocalnode(void)
Definition topology.c:1891
#define PADICO_TOPO_UUID_SIZE
Definition Topology.h:73
padico_topo_uuid_t padico_topo_node_getuuid(padico_topo_node_t node)
Get the UUID of a node.
Definition topology.c:1912
padico_topo_node_t node
Definition na-control.c:65
struct padico_shm_s * shm
Definition psp-shm.c:95
int pid
Definition sf-srtun.c:112
padico_shm_block_t padico_shm_block_alloc(struct padico_shm_s *shm)
allocate a memory block in shm buffer
Definition shm-sysdep.c:423
static void padico_shm_directory_unregister_self(struct padico_shm_s *shm)
Unregister local node into directory Asserts semaphore is held.
Definition shm-sysdep.c:398
struct padico_shm_node_s * padico_shm_directory_node_lookup(const struct padico_shm_s *shm, padico_topo_node_t node)
Definition shm-sysdep.c:321
void padico_shm_large_recv(struct padico_shm_s *shm, void *bytes, size_t size, int from)
Definition shm-sysdep.c:515
static void padico_shm_directory_register_self(struct padico_shm_s *shm)
Register local node into directory Asserts semaphore is held.
Definition shm-sysdep.c:347
void padico_shm_close(struct padico_shm_s *shm)
Definition shm-sysdep.c:201
void padico_shm_large_send(struct padico_shm_s *shm, const void *bytes, size_t size, struct padico_shm_node_s *dest)
Definition shm-sysdep.c:437
struct padico_shm_s * padico_shm_init(const char *name, ssize_t payload)
create a shm segment; payload is the size allocated in addition to the directory.
Definition shm-sysdep.c:59
int padico_shm_large_recv_poll(struct padico_shm_s *shm, void *bytes, size_t size, padico_shm_queue_t queue)
Definition shm-sysdep.c:486
static void padico_shm_seg_init(struct padico_shm_s *shm)
Initializes a newly created segment.
Definition shm-sysdep.c:257
padico_shm_queue_t padico_shm_large_recv_post(struct padico_shm_s *shm, int from)
Definition shm-sysdep.c:476
static void padico_shm_directory_clean(struct padico_shm_s *shm)
Cleans directory.
Definition shm-sysdep.c:293
void padico_shm_directory_dump(struct padico_shm_s *shm)
dump content of shm directory, for debug
Definition shm-sysdep.c:408
struct padico_shm_mbox_s::@66 large[128]
large messages mbox; unlocked acces, valid when pipeline != -1
struct padico_shm_short_lfqueue_s shared_queue
shared queue for short messages sent eagerly; contains block index
Definition Shm.h:115
volatile padico_shm_queue_t pipeline
allocated queue for transfer
Definition Shm.h:120
struct padico_shm_blockcache_lfqueue_s block_cache
free blocks cache
Definition Shm.h:117
struct padico_shm_short_lfqueue_s short_queues[128]
queues for short messages sent eagerly; contains block index
Definition Shm.h:116
A node descriptor.
Definition Shm.h:103
int pid
process id
Definition Shm.h:105
int rank
index of the node in directory
Definition Shm.h:106
global state of the Shm segment
Definition Shm.h:173
char * sem_path
Definition Shm.h:179
int rank
Definition Shm.h:177
ssize_t seg_size
Definition Shm.h:181
struct padico_shm_seg_directory_s * seg_directory
Definition Shm.h:174
struct padico_shm_node_s * self
Definition Shm.h:176
sem_t * sem
Definition Shm.h:180
char * shm_path
Definition Shm.h:178
struct padico_shm_seg_mbox_s * seg_mbox
mbox part of the segment; may be NULL if the segment does not use mbox (payload != -1 at init)
Definition Shm.h:175
a segment containing only a directory
Definition Shm.h:126
struct padico_shm_node_s nodes[128]
directory of node connected to the shared segment
Definition Shm.h:134
struct padico_shm_seg_directory_s::@68 build_string
build string to ensure all nodes use the same build; this field must be first
struct padico_shm_seg_directory_s::@69 directory
a full segment, including buffers
Definition Shm.h:155
struct padico_shm_mbox_s mailboxes[128]
Definition Shm.h:141
struct padico_shm_seg_mbox_s::@70 buffer
struct padico_shm_block_lfqueue_s free_blocks
block allocator, i.e.
Definition Shm.h:145
struct padico_shm_block_lfqueue_s pending_blocks[128]
pending blocks to send (used only by nm_strat_shm)
Definition Shm.h:148
struct padico_shm_large_lfqueue_s queues[512]
pool of queues to pipeline large transfers
Definition Shm.h:146
struct padico_shm_queue_lfqueue_s free_queues
queue allocator, i.e.
Definition Shm.h:147
Node UUID, in plain text (hex encoding)
char * ptr
Definition vpipe.c:83