summaryrefslogtreecommitdiffstats
path: root/testrecovery.c
blob: e3a871aa0a040dbc2378292bbf579d98157de218 (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
/* ----------------------------------------------------------------------- *
 *   
 *   Copyright 2000-2008 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as
 *   published by the Free Software Foundation, Inc.,
 *   59 Temple Place Ste 330, Boston MA 02111-1307, USA, version 2.1,
 *   incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * testrecovery.c
 *
 * This test verifies that we can indeed recover from a failure gracefully.
 * It is basically the same as the allocation test, except it keeps data-
 * structures in persistent memory and periodically kills off the processes.
 */

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <limits.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "lpsm.h"

#define COUNT	262144
#define SLOTS   4096
#define MAXLOG  26

void **areas;
int *sizes;

void verify_no_overlap(int slot)
{
  char *start = areas[slot];
  char *end = start+sizes[slot];
  char *xs, *xe;
  int i;

  for ( i = 0 ; i < SLOTS ; i++ ) {
    if ( i != slot && areas[i] ) {
      xs = areas[i];
      xe = xs + sizes[i];

      if ( xs < end && xe > start ) {
	printf("OVERLAP ALERT: [%p,%p) -> [%p,%p)\n",
	       start, end, xs, xe);
	abort();
      }
    }
  }
}

struct misc_info {
  long occupied;
  int count;
};

int main(int argc, char *argv[])
{
  int i, slot, newsize;
  double rnd;
  void **root_ptr;
  struct misc_info *misc_ptr;
  void *nptr;
  pid_t pid;
  pid_t master_pid = getpid();

  /* Do initial setup */

  setlinebuf(stdout);
  setlinebuf(stderr);
  
  unlink("arena.dat");
  unlink("arena.log");

  if ( !(root_ptr = lpsm_init("arena.dat", "arena.log")) ) {
    fprintf(stderr, "%s: lpsm_arena_init() failed\n", argv[0]);
    return 1;
  }

  printf("Arena:   initialized, %d root pointers at %p\n",
	 LPSM_ROOT_POINTERS, root_ptr);

  root_ptr[0] = misc_ptr = lpsm_malloc(sizeof(struct misc_info));
  root_ptr[1] = areas = lpsm_malloc(sizeof(void *) * SLOTS);
  root_ptr[2] = sizes = lpsm_malloc(sizeof(int) * SLOTS);

  misc_ptr->count = misc_ptr->occupied = 0;

  memset(areas, 0, sizeof(areas));
  memset(sizes, 0, sizeof(sizes));

  lpsm_checkpoint(0.0, PSMSYNC_SYNC);
  lpsm_shutdown();

  /* Become our own process group and ignore SIGTERM to make it easier to
     "kill everything".  Become our own session to keep shell job control
     from interfering with stop signals. */

  setpgrp();

  while ( 1 ) {
    int master_waits = rand() & 1;

    pid = fork();
    if ( pid < 0 ) {
      perror("fork");
      return 1;
    } else if ( pid > 0 ) {
      int status;

      /* Give the child process a reasonable chance to finish log replay,
	 otherwise we'll never get through the test.  On the other hand,
	 we want to check for failures during log replay too! */
      if ( master_waits ) {
	waitpid(pid, &status, WUNTRACED);
	if ( !WIFSTOPPED(status) ) {
	  printf("*** CHILD ERROR, exit code = 0x%x\n", status);
	  exit(1);
	}
	kill(pid, SIGCONT);
      }
      sleep(rand() & 63);
      signal(SIGTERM, SIG_IGN);
      kill(-master_pid, SIGTERM);
      signal(SIGTERM, SIG_DFL);
      waitpid(pid, &status, 0);

      if ( WIFEXITED(status) )
	break;			/* Child exited, not killed, consider us done */
    } else {
      struct timeval start_time, init_time;

      pid = getpid();
      printf("*** Process starting: %d, master_waits = %d\n",
	     pid, master_waits);

      gettimeofday(&start_time, NULL);

      root_ptr = lpsm_init("arena.dat", "arena.log");
      
      gettimeofday(&init_time, NULL);

      misc_ptr = root_ptr[0];
      areas    = root_ptr[1];
      sizes    = root_ptr[2];

      printf("*** Recovering at count %d, occupied = %ld,\n"
	     "*** areas = %p, sizes = %p, replay time = %.6f s\n",
	     misc_ptr->count, misc_ptr->occupied, areas, sizes,
	     (init_time.tv_sec-start_time.tv_sec)+
	     (init_time.tv_usec-start_time.tv_usec)/1e+6);

      assert(misc_ptr->occupied >= 0);

      /* In case the master process decided to be kind and wait for replay */
      if ( master_waits )
	kill(pid, SIGTSTP);	/* The master will wait for this and SIGCONT */

      for ( i = misc_ptr->count+1 ; i <= COUNT ; i++ ) {
	misc_ptr->count = i;
	slot = rand() % SLOTS;

	printf("Count:   %d  Slot: %d\n", i, slot);
	
	if ( areas[slot] ) {
	  if ( rand() % 1 ) {
	    lpsm_free(areas[slot]);
	    printf("Free:    %d (0x%08x) bytes at %p\n",
		   sizes[slot], sizes[slot], areas[slot]);
	    areas[slot] = NULL;
	    misc_ptr->occupied -= sizes[slot];
	    assert(misc_ptr->occupied >= 0);
	  } else {
	    rnd = (double)rand()/RAND_MAX;
	    newsize = (int)pow(2.0, MAXLOG*pow(rnd,3.0));
	    nptr = lpsm_realloc(areas[slot], newsize);
	    if ( nptr ) {
	      printf("Realloc: %d bytes at %p -> %d bytes at %p\n",
		     sizes[slot], areas[slot], newsize, nptr);
	      misc_ptr->occupied += newsize;
	      misc_ptr->occupied -= sizes[slot];
	      assert(misc_ptr->occupied >= 0);
	      sizes[slot] = newsize;
	      areas[slot] = nptr;
	      verify_no_overlap(slot);
	    } else {
	      printf("Realloc: %d bytes at %p -> %d bytes FAILED\n",
		     sizes[slot], areas[slot], newsize);
	    }
	  }
	} else {
	  rnd = (double)rand()/RAND_MAX;
	  sizes[slot] = (int)pow(2.0, MAXLOG*pow(rnd,5.0));
	  areas[slot] = lpsm_malloc(sizes[slot]);
	  if (areas[slot]) { 
	    printf("Alloc:   %d (0x%08x) bytes at %p\n",
		   sizes[slot], sizes[slot], areas[slot]);
	    misc_ptr->occupied += sizes[slot];
	  } else {
	    printf("Alloc:   %d (0x%08x) bytes FAILED\n",
		   sizes[slot], sizes[slot]);
	  }
	  assert(misc_ptr->occupied >= 0);
	  verify_no_overlap(slot);
	}
	
	if ( (i & 255) == 0 ) {
	  printf("Arena:   checkpointing, count = %d\n", i);
	  lpsm_checkpoint(0.1, PSMSYNC_SKIP);
	}
	printf("Arena:   %ld bytes officially occupied\n", misc_ptr->occupied);
      }
      /* If the loop finishes, exit... */
      fprintf(stderr, "ready...\n");
      lpsm_checkpoint(0.0, PSMSYNC_SYNC); /* Synchronous checkpoint */
      exit(0);
    }
  }

  return 0;
}