aboutsummaryrefslogtreecommitdiffstats
path: root/mps/code/failover.c
blob: f6f213d213f8a7249bb753aa8dfb2299ceeb9a9f (plain) (blame)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* failover.c: FAILOVER IMPLEMENTATION
 *
 * $Id$
 * Copyright (c) 2014-2020 Ravenbrook Limited.  See end of file for license.
 *
 * .design: <design/failover>
 *
 * .critical: In manual-allocation-bound programs using MVFF, many of
 * these functions are on the critical paths via mps_alloc (and then
 * PoolAlloc, MVFFAlloc, failoverFind*) and mps_free (and then
 * MVFFFree, failoverInsert).
 */

#include "failover.h"
#include "mpm.h"
#include "range.h"

SRCID(failover, "$Id$");


ARG_DEFINE_KEY(failover_primary, Pointer);
ARG_DEFINE_KEY(failover_secondary, Pointer);


Bool FailoverCheck(Failover fo)
{
  CHECKS(Failover, fo);
  CHECKD(Land, &fo->landStruct);
  CHECKD(Land, fo->primary);
  CHECKD(Land, fo->secondary);
  return TRUE;
}


static Res failoverInit(Land land, Arena arena, Align alignment, ArgList args)
{
  Failover fo;
  ArgStruct arg;
  Res res;

  AVER(land != NULL);
  res = NextMethod(Land, Failover, init)(land, arena, alignment, args);
  if (res != ResOK)
    return res;
  fo = CouldBeA(Failover, land);

  ArgRequire(&arg, args, FailoverPrimary);
  fo->primary = arg.val.p;
  ArgRequire(&arg, args, FailoverSecondary);
  fo->secondary = arg.val.p;

  SetClassOfPoly(land, CLASS(Failover));
  fo->sig = FailoverSig;
  AVERC(Failover, fo);

  return ResOK;
}


static void failoverFinish(Inst inst)
{
  Land land = MustBeA(Land, inst);
  Failover fo = MustBeA(Failover, land);
  fo->sig = SigInvalid;
  NextMethod(Inst, Failover, finish)(inst);
}


static Size failoverSize(Land land)
{
  Failover fo = MustBeA_CRITICAL(Failover, land);
  return LandSize(fo->primary) + LandSize(fo->secondary);
}


static Res failoverInsert(Range rangeReturn, Land land, Range range)
{
  Failover fo = MustBeA_CRITICAL(Failover, land);
  Res res;

  AVER_CRITICAL(rangeReturn != NULL);
  AVERT_CRITICAL(Range, range);

  /* Provide more opportunities for coalescence. See
   * <design/failover#.impl.assume.flush>.
   */
  (void)LandFlush(fo->primary, fo->secondary);

  res = LandInsert(rangeReturn, fo->primary, range);
  if (res != ResOK && res != ResFAIL)
    res = LandInsert(rangeReturn, fo->secondary, range);

  return res;
}


static Res failoverInsertSteal(Range rangeReturn, Land land, Range rangeIO)
{
  Failover fo = MustBeA(Failover, land);
  Res res;

  AVER(rangeReturn != NULL);
  AVER(rangeReturn != rangeIO);
  AVERT(Range, rangeIO);

  /* Provide more opportunities for coalescence. See
   * <design/failover#.impl.assume.flush>.
   */
  (void)LandFlush(fo->primary, fo->secondary);

  res = LandInsertSteal(rangeReturn, fo->primary, rangeIO);
  AVER(res == ResOK || res == ResFAIL);
  return res;
}


static Res failoverDelete(Range rangeReturn, Land land, Range range)
{
  Failover fo = MustBeA(Failover, land);
  Res res;
  RangeStruct oldRange, dummyRange, left, right;

  AVER(rangeReturn != NULL);
  AVERT(Range, range);

  /* Prefer efficient search in the primary. See
   * <design/failover#.impl.assume.flush>.
   */
  (void)LandFlush(fo->primary, fo->secondary);

  res = LandDelete(&oldRange, fo->primary, range);

  if (res == ResFAIL) {
    /* Range not found in primary: try secondary. */
    return LandDelete(rangeReturn, fo->secondary, range);
  } else if (res != ResOK) {
    /* Range was found in primary, but couldn't be deleted. The only
     * case we expect to encounter here is the case where the primary
     * is out of memory. (In particular, we don't handle the case of a
     * CBS returning ResLIMIT because its block pool has been
     * configured not to automatically extend itself.)
     */
    AVER(ResIsAllocFailure(res));

    /* Delete the whole of oldRange, and re-insert the fragments
     * (which might end up in the secondary). See
     * <design/failover#.impl.assume.delete>.
     */
    res = LandDelete(&dummyRange, fo->primary, &oldRange);
    if (res != ResOK)
      return res;

    AVER(RangesEqual(&oldRange, &dummyRange));
    RangeInit(&left, RangeBase(&oldRange), RangeBase(range));
    if (!RangeIsEmpty(&left)) {
      /* Don't call LandInsert(..., land, ...) here: that would be
       * re-entrant and fail the landEnter check. */
      res = LandInsert(&dummyRange, fo->primary, &left);
      if (res != ResOK) {
        /* The range was successful deleted from the primary above. */
        AVER(res != ResFAIL);
        res = LandInsert(&dummyRange, fo->secondary, &left);
        AVER(res == ResOK);
      }
    }
    RangeInit(&right, RangeLimit(range), RangeLimit(&oldRange));
    if (!RangeIsEmpty(&right)) {
      res = LandInsert(&dummyRange, fo->primary, &right);
      if (res != ResOK) {
        /* The range was successful deleted from the primary above. */
        AVER(res != ResFAIL);
        res = LandInsert(&dummyRange, fo->secondary, &right);
        AVER(res == ResOK);
      }
    }
  }
  if (res == ResOK) {
    AVER_CRITICAL(RangesNest(&oldRange, range));
    RangeCopy(rangeReturn, &oldRange);
  }
  return res;
}


static Res failoverDeleteSteal(Range rangeReturn, Land land, Range range)
{
  Failover fo = MustBeA(Failover, land);
  Res res;

  AVER(rangeReturn != NULL);
  AVERT(Range, range);

  /* Prefer efficient search in the primary. See
   * <design/failover#.impl.assume.flush>.
   */
  (void)LandFlush(fo->primary, fo->secondary);

  res = LandDeleteSteal(rangeReturn, fo->primary, range);
  if (res == ResFAIL)
    /* Not found in primary: try secondary. */
    res = LandDeleteSteal(rangeReturn, fo->secondary, range);
  AVER(res == ResOK || res == ResFAIL);
  return res;
}


static Bool failoverIterate(Land land, LandVisitor visitor, void *closure)
{
  Failover fo = MustBeA(Failover, land);

  AVER(visitor != NULL);

  return LandIterate(fo->primary, visitor, closure)
    && LandIterate(fo->secondary, visitor, closure);
}


static Bool failoverFindFirst(Range rangeReturn, Range oldRangeReturn, Land land, Size size, FindDelete findDelete)
{
  Failover fo = MustBeA_CRITICAL(Failover, land);

  AVER_CRITICAL(rangeReturn != NULL);
  AVER_CRITICAL(oldRangeReturn != NULL);
  AVERT_CRITICAL(FindDelete, findDelete);

  /* <design/failover#.impl.assume.flush>. */
  (void)LandFlush(fo->primary, fo->secondary);

  return LandFindFirst(rangeReturn, oldRangeReturn, fo->primary, size, findDelete)
    || LandFindFirst(rangeReturn, oldRangeReturn, fo->secondary, size, findDelete);
}


static Bool failoverFindLast(Range rangeReturn, Range oldRangeReturn, Land land, Size size, FindDelete findDelete)
{
  Failover fo = MustBeA_CRITICAL(Failover, land);

  AVER_CRITICAL(rangeReturn != NULL);
  AVER_CRITICAL(oldRangeReturn != NULL);
  AVERT_CRITICAL(FindDelete, findDelete);

  /* <design/failover#.impl.assume.flush>. */
  (void)LandFlush(fo->primary, fo->secondary);

  return LandFindLast(rangeReturn, oldRangeReturn, fo->primary, size, findDelete)
    || LandFindLast(rangeReturn, oldRangeReturn, fo->secondary, size, findDelete);
}


static Bool failoverFindLargest(Range rangeReturn, Range oldRangeReturn, Land land, Size size, FindDelete findDelete)
{
  Failover fo = MustBeA_CRITICAL(Failover, land);

  AVER_CRITICAL(rangeReturn != NULL);
  AVER_CRITICAL(oldRangeReturn != NULL);
  AVERT_CRITICAL(FindDelete, findDelete);

  /* <design/failover#.impl.assume.flush>. */
  (void)LandFlush(fo->primary, fo->secondary);

  return LandFindLargest(rangeReturn, oldRangeReturn, fo->primary, size, findDelete)
    || LandFindLargest(rangeReturn, oldRangeReturn, fo->secondary, size, findDelete);
}


static Bool failoverFindInZones(Bool *foundReturn, Range rangeReturn, Range oldRangeReturn, Land land, Size size, ZoneSet zoneSet, Bool high)
{
  Failover fo = MustBeA_CRITICAL(Failover, land);
  Bool found = FALSE;
  Res res;

  AVER_CRITICAL(FALSE); /* TODO: this code is completely untested! */
  AVER_CRITICAL(foundReturn != NULL);
  AVER_CRITICAL(rangeReturn != NULL);
  AVER_CRITICAL(oldRangeReturn != NULL);
  /* AVERT_CRITICAL(ZoneSet, zoneSet); */
  AVERT_CRITICAL(Bool, high);

  /* <design/failover#.impl.assume.flush>. */
  (void)LandFlush(fo->primary, fo->secondary);

  res = LandFindInZones(&found, rangeReturn, oldRangeReturn, fo->primary, size, zoneSet, high);
  if (res != ResOK || !found)
    res = LandFindInZones(&found, rangeReturn, oldRangeReturn, fo->secondary, size, zoneSet, high);

  *foundReturn = found;
  return res;
}


static Res failoverDescribe(Inst inst, mps_lib_FILE *stream, Count depth)
{
  Land land = CouldBeA(Land, inst);
  Failover fo = CouldBeA(Failover, land);
  LandClass primaryClass, secondaryClass;
  Res res;

  if (!TESTC(Failover, fo))
    return ResPARAM;
  if (stream == NULL)
    return ResPARAM;

  res = NextMethod(Inst, Failover, describe)(inst, stream, depth);
  if (res != ResOK)
    return res;

  primaryClass = ClassOfPoly(Land, fo->primary);
  secondaryClass = ClassOfPoly(Land, fo->secondary);

  return WriteF(stream, depth + 2,
                "primary = $P ($S)\n",
                (WriteFP)fo->primary,
                (WriteFS)ClassName(primaryClass),
                "secondary = $P ($S)\n",
                (WriteFP)fo->secondary,
                (WriteFS)ClassName(secondaryClass),
                NULL);
}


DEFINE_CLASS(Land, Failover, klass)
{
  INHERIT_CLASS(klass, Failover, Land);
  klass->instClassStruct.describe = failoverDescribe;
  klass->instClassStruct.finish = failoverFinish;
  klass->size = sizeof(FailoverStruct);
  klass->init = failoverInit;
  klass->sizeMethod = failoverSize;
  klass->insert = failoverInsert;
  klass->insertSteal = failoverInsertSteal;
  klass->delete = failoverDelete;
  klass->deleteSteal = failoverDeleteSteal;
  klass->iterate = failoverIterate;
  klass->findFirst = failoverFindFirst;
  klass->findLast = failoverFindLast;
  klass->findLargest = failoverFindLargest;
  klass->findInZones = failoverFindInZones;
  AVERT(LandClass, klass);
}


/* C. COPYRIGHT AND LICENSE
 *
 * Copyright (C) 2014-2020 Ravenbrook Limited <https://www.ravenbrook.com/>.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */