aboutsummaryrefslogtreecommitdiffstats
path: root/mps/code/locusss.c
blob: 8c8e3a25a26a5a25dd376c1d8bf14fe556521dfe (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
/* locusss.c: LOCUS STRESS TEST
 *
 * $Id$
 * Copyright (c) 2001-2020 Ravenbrook Limited.  See end of file for license.
 */

#include "mpscmvff.h"
#include "mpslib.h"
#include "mpsavm.h"
#include "testlib.h"
#include "mpslib.h"
#include "mps.h"

#include <stdio.h> /* printf */


/* some constants */

#define TRUE  1
#define FALSE 0

#define iterationCount 30          /* number of iterations */
#define contigAllocs 8             /* number of allocs each iteration */
#define chunkSize ((size_t)65536)  /* our allocation chunk size */

#define smallArenaSize \
  ((size_t)(chunkSize * iterationCount * contigAllocs * 2))


#define AddressOffset(b, l) \
  ((size_t)((char *)(l) - (char *)(b)))


/* PoolStat -- maintain data about contiguous allocations */

typedef struct PoolStatStruct *PoolStat;

typedef struct PoolStatStruct {
  mps_pool_t pool;  /* the pool being measured */
  size_t objSize;   /* size of each allocation */
  mps_addr_t min;   /* lowest address lock allocated to the pool */
  mps_addr_t max;   /* highest address lock allocated to the pool */
  int ncCount;      /* count of non-contiguous allocations */
  int aCount;       /* count of allocations */
  int fCount;       /* count of frees */
} PoolStatStruct;



static mps_addr_t allocObject(mps_pool_t pool, size_t size)
{
  mps_addr_t addr;
  die(mps_alloc(&addr, pool, size),
      "Allocate Object");
  return addr;
}

static void recordNewObjectStat(PoolStat stat, mps_addr_t obj)
{
  stat->aCount++;
  if (obj < stat->min) {
    if (AddressOffset(obj, stat->min) > stat->objSize) {
      stat->ncCount++;
    }
    stat->min = obj;
  } else if (obj > stat->max) {
    if (AddressOffset(stat->max, obj) > stat->objSize) {
      stat->ncCount++;
    }
    stat->max = obj;
  }
}

static void recordFreedObjectStat(PoolStat stat)
{
  stat->fCount++;
}


static void poolStatInit(PoolStat stat, mps_pool_t pool, size_t objSize)
{
  mps_addr_t s1, s2, s3;

  stat->pool = pool;
  stat->objSize = objSize;
  stat->ncCount = 0;
  stat->aCount = 0;
  stat->fCount = 0;

  /* allocate 3 half-size sentinel objects, freeing the middle one */
  /* to leave a bit of space for the control pool */
  s1 = allocObject(pool, objSize / 2);
  stat->min = s1;
  stat->max = s1;
  stat->aCount++;

  s2 = allocObject(pool, objSize / 2);
  recordNewObjectStat(stat, s2);
  s3 = allocObject(pool, objSize / 2);
  recordNewObjectStat(stat, s3);

  mps_free(pool, s2, objSize / 2);
  recordFreedObjectStat(stat);

}


static mps_res_t allocMultiple(PoolStat stat)
{
  mps_addr_t objects[contigAllocs];
  size_t i;

  /* allocate a few objects, and record stats for them */
  for (i = 0; i < contigAllocs; i++) {
    mps_addr_t obj;
    mps_res_t res = mps_alloc(&obj, stat->pool, stat->objSize);
    if (res != MPS_RES_OK)
      return res;
    recordNewObjectStat(stat, obj);
    objects[i] = obj;
  }

  /* free one of the objects, to make the test more interesting */
  i = rnd() % contigAllocs;
  mps_free(stat->pool, objects[i], stat->objSize);
  recordFreedObjectStat(stat);

  return MPS_RES_OK;
}


/* reportResults - print a report on a PoolStat */

static void reportResults(PoolStat stat, const char *name)
{
  printf("\nResults for ");
  printf("%s", name);
  printf("\n");
  printf("   Allocated  %"PRIuLONGEST" objects\n", (ulongest_t)stat->aCount);
  printf("   Freed      %"PRIuLONGEST" objects\n", (ulongest_t)stat->fCount);
  printf("   There were %lu non-contiguous allocations\n",
         (unsigned long)stat->ncCount);
  printf("   Address range from %p to %p\n", stat->min, stat->max);
  printf("\n");
}


static void testInArena(mps_arena_t arena,
                        mps_bool_t failcase,
                        mps_bool_t usefulFailcase)
{
  mps_pool_t lopool, hipool, temppool;
  PoolStatStruct lostruct;  /* stats about lopool */
  PoolStatStruct histruct;  /* stats about lopool */
  PoolStatStruct tempstruct;  /* stats about temppool */
  PoolStat lostat = &lostruct;
  PoolStat histat = &histruct;
  PoolStat tempstat = &tempstruct;
  int i;

  die(mps_pool_create(&hipool, arena, mps_class_mvff(),
                      chunkSize, chunkSize, (size_t)1024,
                      TRUE, TRUE, TRUE),
      "Create HI MFFV");

  die(mps_pool_create(&lopool, arena, mps_class_mvff(),
                      chunkSize, chunkSize, (size_t)1024,
                      FALSE, FALSE, TRUE),
      "Create LO MFFV");

  die(mps_pool_create_k(&temppool, arena, mps_class_mvff(),
                        mps_args_none),
      "Create TEMP");

  if(failcase) {
    if(usefulFailcase) {
      /* describe a useful failure case */
    } else {
      /* describe a misleading failure case */
    }
  }

  poolStatInit(lostat, lopool, chunkSize);
  poolStatInit(histat, hipool, chunkSize);
  poolStatInit(tempstat, temppool, chunkSize);

  /* iterate, allocating objects */
  for (i=0; i<iterationCount; ++i) {
    mps_res_t res;
    res = allocMultiple(lostat);
    if (res != MPS_RES_OK)
      break;
    res = allocMultiple(histat);
    if (res != MPS_RES_OK)
      break;
    res = allocMultiple(tempstat);
    if (res != MPS_RES_OK)
      break;
  }

  /* report results */
  reportResults(lostat, "the low MVFF pool");
  reportResults(histat, "the high MVFF pool");
  reportResults(tempstat, "the temp pool");

  mps_pool_destroy(hipool);
  mps_pool_destroy(lopool);
  mps_pool_destroy(temppool);

}

static void runArenaTest(size_t size,
                         mps_bool_t failcase,
                         mps_bool_t usefulFailcase)
{
  mps_arena_t arena;

  MPS_ARGS_BEGIN(args) {
    MPS_ARGS_ADD(args, MPS_KEY_ARENA_SIZE, size);
    MPS_ARGS_ADD(args, MPS_KEY_ARENA_ZONED, FALSE);
    MPS_ARGS_ADD(args, MPS_KEY_COMMIT_LIMIT, size - chunkSize);
    die(mps_arena_create_k(&arena, mps_arena_class_vm(), args),
        "mps_arena_create");
  } MPS_ARGS_END(args);

  testInArena(arena, failcase, usefulFailcase);

  mps_arena_destroy(arena);

}


int main(int argc, char *argv[])
{
  testlib_init(argc, argv);

  printf("\nRunning test with no information about peak usage.\n");
  runArenaTest(smallArenaSize, FALSE, FALSE);
  /*
  printf("\nRunning test with useful information about peak usage.\n");
  runArenaTest(smallArenaSize, TRUE, TRUE);
  printf("\nRunning test with misleading information about peak usage.\n");
  runArenaTest(smallArenaSize, TRUE, FALSE);
  */

  return 0;
}


/* C. COPYRIGHT AND LICENSE
 *
 * Copyright (C) 2001-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.
 */