aboutsummaryrefslogtreecommitdiff
path: root/libgfortran/caf_shared/shared_memory.c
blob: bc1a2e97170e71492d68ac71b3232c5c09abd614 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* Copyright (C) 2020 Free Software Foundation, Inc.
   Contributed by Nicolas Koenig

This file is part of the GNU Fortran Native Coarray Library (libnca).

Libnca is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

Libnca is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.

You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
<http://www.gnu.org/licenses/>.  */

#include "libgfortran.h"
#include "libcoarraynative.h"

#include <sys/mman.h>
#include <unistd.h>
#include <string.h>

/* This implements shared memory based on POSIX mmap.  We start with
   memory block of the size of the global shared memory data, rounded
   up to one pagesize, and enlarge as needed.

   We address the memory via a shared_memory_ptr, which is an offset into
   the shared memory block. The metadata is situated at offset 0.

   In order to be able to resize the memory and to keep pointers
   valid, we keep the old mapping around, so the memory is actually
   visible several times to the process.  Thus, pointers returned by
   shared_memory_get_mem_with_alignment remain valid even when
   resizing.  */

/* Global metadata for shared memory, always kept at offset 0.  */

typedef struct
{
  size_t size;
  size_t used;
  int fd;
} global_shared_memory_meta;

/* Type realization for opaque type shared_memory.  */

typedef struct shared_memory_act
{
  global_shared_memory_meta *meta;
  void *header;
  size_t last_seen_size;

  /* We don't need to free these. We probably also don't need to keep
     track of them, but it is much more future proof if we do.  */

  size_t num_local_allocs;

  struct local_alloc
  {
    void *base;
    size_t size;
  } allocs[];

} shared_memory_act;

/* Convenience wrapper for mmap.  */

static inline void *
map_memory (int fd, size_t size, off_t offset)
{
  void *ret
      = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
  if (ret == MAP_FAILED)
    {
      perror ("mmap failed");
      exit (1);
    }
  return ret;
}

/* Returns the size of shared_memory_act.  */

static inline size_t
get_shared_memory_act_size (int nallocs)
{
  return sizeof (shared_memory_act) + nallocs * sizeof (struct local_alloc);
}

/* When the shared memory block is enlarged, we need to map it into
   virtual memory again.  */

static inline shared_memory_act *
new_base_mapping (shared_memory_act *mem)
{
  shared_memory_act *newmem;
  /* We need another entry in the alloc table.  */
  mem->num_local_allocs++;
  newmem = realloc (mem, get_shared_memory_act_size (mem->num_local_allocs));
  newmem->allocs[newmem->num_local_allocs - 1] = ((struct local_alloc){
      .base = map_memory (newmem->meta->fd, newmem->meta->size, 0),
      .size = newmem->meta->size });
  newmem->last_seen_size = newmem->meta->size;
  return newmem;
}

/* Return the most recently allocated base pointer.  */

static inline void *
last_base (shared_memory_act *mem)
{
  return mem->allocs[mem->num_local_allocs - 1].base;
}

/* Get a pointer into the shared memory block with alignemnt
   (works similar to sbrk).  */

shared_mem_ptr
shared_memory_get_mem_with_alignment (shared_memory_act **pmem, size_t size,
				      size_t align)
{
  shared_memory_act *mem = *pmem;
  size_t new_size;
  size_t orig_used;

  /* Offset into memory block with alignment.  */
  size_t used_wa = alignto (mem->meta->used, align);

  if (used_wa + size <= mem->meta->size)
    {
      memset (last_base (mem) + mem->meta->used, 0xCA,
	      used_wa - mem->meta->used);
      memset (last_base (mem) + used_wa, 0x42, size);
      mem->meta->used = used_wa + size;

      return (shared_mem_ptr){ .offset = used_wa };
    }

  /* We need to enlarge the memory segment.  Double the size if that
     is big enough, otherwise get what's needed.  */

  if (mem->meta->size * 2 > used_wa + size)
    new_size = mem->meta->size * 2;
  else
    new_size = round_to_pagesize (used_wa + size);

  orig_used = mem->meta->used;
  mem->meta->size = new_size;
  mem->meta->used = used_wa + size;
  ftruncate (mem->meta->fd, mem->meta->size);
  /* This also sets the new base pointer where the shared memory
     can be found in the address space.  */

  mem = new_base_mapping (mem);

  *pmem = mem;
  assert (used_wa != 0);

  memset (last_base (mem) + orig_used, 0xCA, used_wa - orig_used);
  memset (last_base (mem) + used_wa, 0x42, size);

  return (shared_mem_ptr){ .offset = used_wa };
}

/* If another image changed the size, update the size accordingly.  */

void
shared_memory_prepare (shared_memory_act **pmem)
{
  shared_memory_act *mem = *pmem;
  if (mem->meta->size == mem->last_seen_size)
    return;
  mem = new_base_mapping (mem);
  *pmem = mem;
}

/* Initialize the memory with one page, the shared metadata of the
   shared memory is stored at the beginning.  */

void
shared_memory_init (shared_memory_act **pmem, size_t initial_size)
{
  shared_memory_act *mem;
  int fd;

  mem = malloc (get_shared_memory_act_size (1));
  fd = get_shmem_fd ();

  ftruncate (fd, initial_size);
  mem->meta = map_memory (fd, initial_size, 0);
  *mem->meta = ((global_shared_memory_meta){
      .size = initial_size,
      .used = sizeof (global_shared_memory_meta),
      .fd = fd });
  mem->last_seen_size = initial_size;
  mem->num_local_allocs = 1;
  mem->allocs[0]
      = ((struct local_alloc){ .base = mem->meta, .size = initial_size });

  *pmem = mem;
}

/* Convert a shared memory pointer (i.e. an offset into the shared
   memory block) to a pointer.  */

void *
shared_mem_ptr_to_void_ptr (shared_memory_act **pmem, shared_mem_ptr smp)
{
  return last_base (*pmem) + smp.offset;
}