File: pthread_misc.c

package info (click to toggle)
blcr 0.8.5-2.2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 10,728 kB
  • ctags: 3,413
  • sloc: ansic: 31,999; sh: 12,633; makefile: 929; perl: 401; cpp: 79; java: 9
file content (229 lines) | stat: -rw-r--r-- 5,594 bytes parent folder | download | duplicates (4)
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
/*
 * Berkeley Lab Checkpoint/Restart (BLCR) for Linux is Copyright (c)
 * 2003, The Regents of the University of California, through Lawrence
 * Berkeley National Laboratory (subject to receipt of any required
 * approvals from the U.S. Dept. of Energy).  All rights reserved.
 *
 * Portions may be copyrighted by others, as may be noted in specific
 * copyright notices within specific files.
 *
 * This program 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 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: pthread_misc.c,v 1.13 2008/08/27 21:11:42 phargrov Exp $
 */

#define _LARGEFILE64_SOURCE 1   /* For O_LARGEFILE */

#include <time.h>
#include <sys/time.h>
#include <sys/timeb.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "libcr.h"


/* Take a blocking checkpoint of my own process */
int checkpoint_self(void)
{
    int ret;
    cr_checkpoint_args_t my_args;
    cr_checkpoint_handle_t my_handle;
    char filename[64];

    /* build the filename */
    snprintf(filename, sizeof(filename), "context.%d", (int)getpid());

    /* remove existing context file, if any */
    (void)unlink(filename);

    /* open the context file */
    ret = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, 0600);
    if (ret < 0) {
        perror("open");
        goto out;
    }

    cr_initialize_checkpoint_args_t(&my_args);
    my_args.cr_fd = ret; /* still holds the return from open() */
    my_args.cr_scope = CR_SCOPE_PROC;

    /* issue the request */
    ret = cr_request_checkpoint(&my_args, &my_handle);
    if (ret < 0) {
        perror("cr_request_checkpoint");
        goto out;
    }

    /* wait for the request to complete */
    do {
        ret = cr_poll_checkpoint(&my_handle, NULL);
        if (ret < 0) {
            if ((ret == CR_POLL_CHKPT_ERR_POST) && (errno == CR_ERESTARTED)) {
                /* restarting -- not an error */
                ret = 1; /* Signal RESTART to caller */
            } else if (errno == EINTR) {
                /* poll was interrupted by a signal -- retry */
            } else {
                perror("cr_poll_checkpoint");
                goto out;
            }
        } else if (ret == 0) {
            fprintf(stderr, "cr_poll_checkpoint returned unexpected 0\n");
            ret = -1;
            goto out;
        } else {
            ret = 0; /* Signal CONTINUE to caller */
	}
    } while (ret < 0);

    close(my_args.cr_fd);
out:
    return ret;
}

#define THREADS 2

static int count = 0;
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

static pthread_t main_tid = (pthread_t)-1;

#define DATELEN 80
static char date[DATELEN];

static char * now(void)
{
    struct timeb when;

    ftime(&when);
    return ctime(&when.time);
}

static int my_callback(void* arg)
{
    int * rc_ptr = (int *)arg;
    int rc;

    if (pthread_self() == main_tid) {
	strncpy(date, now(), DATELEN);
    }

    rc = cr_checkpoint(0);

    if (rc_ptr != NULL) {
	*rc_ptr = rc;
    }

    return 0;
}

static void advance(void)
{
    pthread_mutex_lock(&lock);
    count++;
    pthread_cond_broadcast(&cond);
    pthread_mutex_unlock(&lock);
}

static void wait_for(int want)
{
    pthread_mutex_lock(&lock);
    while (count < want) {
	pthread_cond_wait(&cond, &lock);
    }
    pthread_mutex_unlock(&lock);
}

static void * thread(void * arg)
{
    int tid = (int)pthread_self();
    int rc;

    printf("STARTING thread w/ tid %d\n", tid);
    rc = (int)cr_init();
    if (rc < 0) {
	exit(-1);
    }

    (void)cr_register_callback(my_callback, &rc, CR_SIGNAL_CONTEXT);
    advance();
    wait_for(THREADS + 1);
    if (rc) {
	printf("RESTARTING tid %d, from checkpoint taken %s", tid, date);
    } else {
	printf("CONTINUING tid %d, after taking checkpoint\n", tid);
    }
    printf("EXITING tid %d at %s", tid, now());

    return NULL;
}

int main(void)
{
    pthread_t th[THREADS];
    int rc;
    int i;
	
    printf("Starting main process w/ pid %d\n", (int)getpid());
    main_tid = pthread_self();

    rc = (int)cr_init();
    if (rc < 0) {
	exit(-1);
    }

    (void)cr_register_callback(my_callback, &rc, CR_SIGNAL_CONTEXT);

    // Start one or more threads
    for (i = 0; i < THREADS; ++i) {
	pthread_create(&th[i], NULL, thread, NULL);
    }

    // Wait for them to initialize
    wait_for(THREADS);
    
    rc = checkpoint_self(); // should cause a checkpoint NOW
    if (rc < 0) {
	exit(-1);
    }

    if (rc) {
	printf("RESTARTING main thread, from checkpoint taken %s", date);
    } else {
	printf("CONTINUING main thread, after taking checkpoint\n");
    }

    advance();

    for (i = 0; i < THREADS; ++i) {
	rc = pthread_join(th[i], NULL);
	if (rc < 0) {
	    perror("pthread_join()");
	    exit(-1);
	}
    }

    printf("EXITING main thread (pid %d) at %s", (int)getpid(), now());

    return 0;
}