summaryrefslogtreecommitdiff
path: root/utils/vmallocate.c
blob: 628cfd12d1df7e4243d937d4ef4d12c8bbf8ae66 (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
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
/* vmallocate -- a utility to allocate memory.

   Copyright (C) 2015,2016 Free Software Foundation, Inc.

   This file is part of the GNU Hurd.

   The GNU Hurd 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 2, or (at
   your option) any later version.

   The GNU Hurd 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.

   You should have received a copy of the GNU General Public License
   along with the GNU Hurd.  If not, see <http://www.gnu.org/licenses/>.  */

#include <argp.h>
#include <assert.h>
#include <error.h>
#include <hurd.h>
#include <inttypes.h>
#include <mach.h>
#include <mach/vm_param.h>
#include <unistd.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <version.h>



const char *argp_program_version = STANDARD_HURD_VERSION (vmallocate);

int verbose;
int do_pause;
int do_read;
int do_write;

uint64_t size;
uint64_t chunk_size = 1U << 30;	/* 1 gigabyte */
uint64_t allocated;

static const struct argp_option options[] =
{
  {NULL, 0, NULL, OPTION_DOC, "Mapping options", 1},
  {"read", 'r', NULL, 0, "read from mapping", 1},
  {"write", 'w', NULL, 0, "write to mapping", 1},

  {NULL, 0, NULL, OPTION_DOC, "Options", 2},
  {"verbose", 'v', NULL, 0, "be more verbose", 2},
  {"pause", 'p', NULL, 0, "read newline from stdin before exiting", 2},
  {0}
};

#define UNITS	"gGmMkKpP"
static const char args_doc[] = "SIZE["UNITS"]";
static const char doc[] = "Allocates memory.\v"
  "This is a stress-test for the vm system.";

/* Parse our options...	 */
error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
  char *end;
  switch (key)
    {
    case 'r':
      do_read = 1;
      break;

    case 'w':
      do_write = 1;
      break;

    case 'v':
      verbose += 1;
      break;

    case 'p':
      do_pause = 1;
      break;

    case ARGP_KEY_ARG:
      if (size > 0)
        argp_error (state, "Extra argument after size: %s", arg);

      size = strtoull (arg, &end, 10);
      if (arg == end || (end[0] != 0 && end[1] != 0))
        argp_error (state, "Could not parse size `%s'.", arg);

      switch (end[0]) {
      case 0:
	break;

      case 'g':
      case 'G':
	size <<= 30;
	break;

      case 'm':
      case 'M':
	size <<= 20;
	break;

      case 'k':
      case 'K':
	size <<= 10;
	break;

      case 'p':
      case 'P':
	size <<= PAGE_SHIFT;
	break;

      default:
        argp_error (state,
		    "Unknown unit `%c', expected one of "UNITS".",
		    end[0]);
      }
      break;

    case ARGP_KEY_NO_ARGS:
      argp_usage (state);
      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

const struct argp argp =
  {
  options: options,
  parser: parse_opt,
  args_doc: args_doc,
  doc: doc,
  };



#define min(a, b)	((a) < (b) ? (a) : (b))

struct child
{
  task_t task;
  struct child *next;
};

int
main (int argc, char **argv)
{
  error_t err;
  int nchildren = 0;
  struct child *c, *children = NULL;
  process_t proc = getproc ();

  /* We must make sure that chunk_size fits into vm_size_t.  */
  assert (chunk_size <= 1U << (sizeof (vm_size_t) * 8 - 1));

  /* Parse our arguments.  */
  argp_parse (&argp, argc, argv, 0, 0, 0);

  if (verbose)
    fprintf (stderr, "About to allocate %"PRIu64" bytes in chunks of "
             "%"PRIu64" bytes.\n", size, chunk_size);

  while (allocated < size)
    {
      task_t task;
      uint64_t s = min (chunk_size, size - allocated);
      vm_address_t address = 0;
      volatile char *p;
      char **argv = NULL;

      err = vm_allocate (mach_task_self (), &address, (vm_size_t) s,
                         1 /* anywhere */);
      if (err)
	error (1, err, "vm_allocate");

      /* Write an argument and environment vector.  */
      if (s > 128)
        {
          int written;
          char *arg0;

          arg0 = (char *) address;
          written = sprintf (arg0, "[vmallocate %d]", nchildren);

          argv = (char **) (address + (unsigned) written + 1);
          argv[0] = arg0;
          argv[1] = NULL;
        }

      for (p = (char *) address + PAGE_SIZE;
           p < (char *) (address + (size_t) s);
           p += PAGE_SIZE)
        {
          if (do_read)
            (void) *p;
          if (do_write)
            *p = 1;

          if (verbose > 1
              && ((unsigned int) (p - address) & ((1U<<20) - 1)) == 0)
            fprintf (stderr, "\r%"PRIu64,
                     allocated
                     + ((uint64_t) (uintptr_t) p - (uint64_t) address));
        }

      err = vm_inherit (mach_task_self (), address, (vm_size_t) s,
			VM_INHERIT_COPY);
      if (err)
	error (1, err, "vm_inherit");

      err = task_create (mach_task_self (), 1, &task);
      if (err)
	error (1, err, "task_create");

      err = proc_child (proc, task);
      if (err)
	error (1, err, "proc_child");

      if (argv != NULL)
        {
          process_t childp;

          err = proc_task2proc (proc, task, &childp);
          if (err)
            error (1, err, "proc_task2proc");

          err = proc_set_arg_locations (childp,
                                        (vm_offset_t) &argv[0],
                                        (vm_offset_t) &argv[1]);
          if (err)
            error (1, err, "proc_set_arg_locations");
        }

      c = malloc (sizeof *c);
      if (c == NULL)
	error (1, errno, "malloc");
      c->task = task;
      c->next = children;
      children = c;

      err = vm_deallocate (mach_task_self (), address, (vm_size_t) s);
      if (err)
	error (1, err, "vm_deallocate");

      allocated += s;
      nchildren += 1;

      if (verbose)
        fprintf (stderr, "\rAllocated %"PRIu64" bytes.\n", allocated);
    }

  if (do_pause)
    {
      fprintf (stderr, "Press enter to exit and release the memory.");
      getchar ();
    }

  for (c = children; c; c = c->next)
    task_terminate (c->task);

  return EXIT_SUCCESS;
}