🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: es.comp.os.linux.programacion
Article #7474

about simple checkpointing

#7474
From: karim bernardet
Date: Wed, 26 Feb 2003 20:20
317 lines
7933 bytes
Hello,

I m trying to code a program to do checkpointing (for a simple loop) on
a Linux Intel box (just to understand how it works). To do that, I (try
to) follow what is said for the condor's checkpointing
(www.cc.gatech.edu/classes/cs6761_98_spring/condor.ps).
So I save the data and the stack space (in 2 different files data.dat
and stack.dat).
The data space is from _etext to sbrk(0) (data+bss) and the stack space
from
STACKBOTTOM (which is well defined) to the stack pointer. I save the
stack context too.
The checkpointing is done when the program receive the SIGTSTP signal
and I restart it with the flag 1.
But I can t read the data segment when I restart the code :

$ ./a.out 1
Recover ...
Restoring Data Section
data start=134516798
data size=12226
HEAP END=134529024
Problem to read data segment nb=-1
read: Bad address

Below I put my code.
Thanks a lot if someone can help me



/*******************   THE CODE
**************************************/
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <setjmp.h>
#include <fcntl.h>
#include <errno.h>


extern long _etext;
jmp_buf pos;
static void (*SaveFunc)();
#define TmpStackSize 4096
static char TmpStack[TmpStackSize];

/* Note - on linux/intel, the stack grows down. */
#define STACKBOTTOM ((char *)0xc0000000)

void checkpointing()
{
  long data_start,HEAP_end,stack_end,data_size;
  int f;

  if (setjmp(pos)==0)
    {

      /////***************** STACK SECTION ****************///////

      data_size =(long)STACKBOTTOM -(long)(pos[0].__jmpbuf[JB_SP]);
      stack_end=(long)&pos[0].__jmpbuf[JB_SP];

      if ((f=open("stack.dat",O_CREAT|O_RDWR|O_TRUNC,0755))<=0)
 {
   fprintf(stderr,"Problem with file stack.dat\n");
   exit(0);
 }

      if (write(f,&stack_end,sizeof(long))<sizeof(long))
 {
   fprintf(stderr,"Problem to write stack_end\n");
   perror("write");
   exit(0);
 }

      if (write(f,&data_size,sizeof(long))<sizeof(long))
 {
   fprintf(stderr,"Problem to write data_size\n");
   perror("write");
   exit(0);
 }

      if (write(f,pos[0].__jmpbuf[JB_SP],data_size)<data_size)
 {
   fprintf(stderr,"Problem to write the stack space\n");
   perror("write");
   exit(0);
 }

      if ( write(f,pos,sizeof(jmp_buf))<sizeof(jmp_buf))
 {
   fprintf(stderr,"Problem to write jmp_buf\n");
   perror("write");
   exit(0);
 }

      close(f);
      ////*************  END STACK SECTION  *****************////

      ////*************  DATA SECTION  **********************////

      if ((f=open("data.dat",O_CREAT|O_RDWR|O_TRUNC,0755))<=0)
 {
   fprintf(stderr,"Problem with file data.dat\n");
   exit(0);
 }

      // start of data segment
      data_start=(long)&_etext;

      //find end address of heap
      HEAP_end=(long)sbrk(0);

      data_size=HEAP_end-data_start;

      if (write(f,&HEAP_end,sizeof(long))<sizeof(long))
 {
   fprintf(stderr,"Problem to write HEAP_end\n");
   perror("write");
   exit(0);
 }

      if (write(f,&data_start,sizeof(long))<sizeof(long))
 {
   fprintf(stderr,"Problem to write data_start\n");
   perror("write");
   exit(0);
 }

      if (write(f,&data_size,sizeof(long))<sizeof(long))
 {
   fprintf(stderr,"Problem to write data_size\n");
   perror("write");
   exit(0);
 }

      if (write(f,data_start,data_size)<data_size)
 {
   fprintf(stderr,"Problem to write the data space\n");
   perror("write");
   exit(0);
 }

      close(f);

      ////*************  END DATA SECTION  **********************////

      exit(0);
    }
  else
    {
      //when state is restored by longjmp , program continues from here.

      fprintf(stderr,"Resume OK\n");
    }
}

void restore_data()
{
  long data_size,HEAP_end,data_start,nb;
  int f;

  if ((f=open("data.dat",O_RDONLY))<=0)
    {
      fprintf(stderr,"Problem with file data.dat\n");
      exit(0);
    }

  if (read(f,&HEAP_end,sizeof(long))<sizeof(long))
    {
      fprintf(stderr,"Problem to read HEAP_end\n");
      perror("read");
      exit(0);
    }

  if (brk((long)HEAP_end)==-1)
    {
      fprintf(stderr,"Problem with brk()\n");
      exit(0);
    }

  // restore data segment

  if (read(f,&data_start,sizeof(long))<sizeof(long))
    {
      fprintf(stderr,"Problem to read data_start\n");
      perror("read");
      exit(0);
    }

  if (read(f,&data_size,sizeof(long))<sizeof(long))
    {
      fprintf(stderr,"Problem to read data_size\n");
      perror("read");
      exit(0);
    }

  fprintf(stdout,"data start=%ld\ndata size=%ld\nHEAP END=%ld
\n",data_start,data_size,HEAP_end);

  if ((nb=read(f,data_start,data_size))<data_size)
    {
      fprintf(stderr,"Problem to read data segment nb=%ld\n",nb);
      perror("read");
      exit(0);
    }

  close(f);

  fprintf(stderr," - Data Section Restored\n\n") ;
}

void restore_stack()
{
  long stack_end, data_size,nb;
  int f;

  if ((f=open("stack.dat",O_RDONLY))<=0)
    {
      fprintf(stderr,"Problem with file stack.dat\n");
      exit(0);
    }

  if (read(f,&stack_end,sizeof(long))<sizeof(long))
    {
      fprintf(stderr,"Problem to read stack_end\n");
      perror("read");
      exit(0);
    }

  if ((nb=read(f,&data_size,sizeof(long)))<sizeof(long))
    {
      fprintf(stderr,"Problem to read stack size nb=%d\n",nb);
      perror("read");
      exit(0);
    }

  fprintf(stdout,"stack end=%ld\nstack size=%ld\nHEAP END=%ld
\n",stack_end,data_size);

  if ((nb=read(f,stack_end,data_size))<data_size)
    {
      fprintf(stderr,"Problem to read stack space nb=%d\n",nb);
      perror("read");
      exit(0);
    }

  if ((nb=read(f,pos,sizeof(jmp_buf)))<sizeof(jmp_buf))
    {
      fprintf(stderr,"Problem to read jmp_buf nb=%d\n",nb);
      perror("read");
      exit(0);
    }

  close(f) ;

  fprintf(stderr," - Stack section Restored\n\n") ;

  longjmp(pos,1) ;

}

void ExecuteOnTmpStk(void (*func)())
{
  jmp_buf env;
  SaveFunc = func;
  if (setjmp(env)==0)
    {
      env[0].__jmpbuf[JB_SP]=(long)TmpStack+TmpStackSize;
      longjmp(env,1);
    }
  else
    {
      SaveFunc();
    }
}

void recover()
{
  //int save_fd=f;

  //Restore Data
  fprintf(stderr,"Restoring Data Section\n") ;
  restore_data() ;

  fprintf(stderr,"Restoring Stack Section\n") ;
  //f=save_fd;
   restore_stack() ;
  //ExecuteOnTmpStk(restore_stack);

  fprintf(stderr,"should never be here !\n");
  exit(1);
}

void mymain()
{
  int i=0;
  while(i<500000)
    {
      i++;
      printf("i=%d\n",i);
    }
}

main(int argc, char **argv)
{

  signal(SIGTSTP,checkpointing);

  if (argc==2 && atoi(argv[1])==1)
    {
      fprintf(stderr,"Recover ...\n");
      recover();
    }
  else
    mymain();
}
Technical Details
Message-ID: <b3j7li$813$1@news-reader11.wanadoo.fr>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!oleane.net!oleane!wanadoo.fr!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f86c8,1f98dfe213fdbb12,start
X-Google-Attributes: gidf86c8,public
X-Google-ArrivalTime: 2003-02-26 12:22:11 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!oleane.net!oleane!wanadoo.fr!not-for-mail
From: "karim bernardet" <bernardet.karim@wanadoo.fr>
Newsgroups: es.comp.os.linux.programacion
Subject: about simple checkpointing
Date: Wed, 26 Feb 2003 21:20:51 +0100
Organization: Wanadoo, l'internet avec France Telecom
Lines: 322
Message-ID: <b3j7li$813$1@news-reader11.wanadoo.fr>
NNTP-Posting-Host: amarseille-206-1-17-6.abo.wanadoo.fr
X-Trace: news-reader11.wanadoo.fr 1046290930 8227 81.50.82.6 (26 Feb 2003 20:22:10 GMT)
X-Complaints-To: abuse@wanadoo.fr
NNTP-Posting-Date: 26 Feb 2003 20:22:10 GMT
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000