• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nasa / trick / 22071109586

16 Feb 2026 04:49PM UTC coverage: 55.753% (+0.1%) from 55.624%
22071109586

Pull #2016

github

web-flow
Merge 12e8524d4 into 764b4b429
Pull Request #2016: better support set_cycle in drgroup

100 of 124 new or added lines in 6 files covered. (80.65%)

48 existing lines in 4 files now uncovered.

12593 of 22587 relevant lines covered (55.75%)

316136.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.04
/trick_source/sim_services/CheckPointRestart/CheckPointRestart.cpp
1

2
#include <iostream>
3
#include <iomanip>
4
#include <sstream>
5
#include <stdlib.h>
6
#include <sys/types.h>
7
#include <unistd.h>
8
#include <sys/syscall.h>
9
#include <sys/stat.h>
10
#include <string.h>
11

12
#include "trick/CheckPointRestart.hh"
13
#include "trick/MemoryManager.hh"
14
#include "trick/SimObject.hh"
15
#include "trick/Executive.hh"
16
#include "trick/exec_proto.hh"
17
#include "trick/exec_proto.h"
18
#include "trick/command_line_protos.h"
19
#include "trick/message_proto.h"
20
#include "trick/message_type.h"
21
#include "trick/TrickConstant.hh"
22

23
Trick::CheckPointRestart * the_cpr ;
24

25
Trick::CheckPointRestart::CheckPointRestart() {
186✔
26

27
    int num_classes = 0 ;
186✔
28

29
    pre_init_checkpoint = false ;
186✔
30
    post_init_checkpoint = false ;
186✔
31
    end_checkpoint = false ;
186✔
32
    safestore_enabled = false ;
186✔
33
    cpu_num = -1 ;
186✔
34
    safestore_time = TRICK_MAX_LONG_LONG ;
186✔
35
    load_checkpoint_file_name.clear() ;
186✔
36

37
    write_checkpoint_job = NULL ;
186✔
38
    safestore_checkpoint_job = NULL ;
186✔
39

40
    class_map["checkpoint"] = num_classes ;
186✔
41
    class_to_queue[num_classes++] = &checkpoint_queue ;
186✔
42

43
    class_map["post_checkpoint"] = num_classes ;
186✔
44
    class_to_queue[num_classes++] = &post_checkpoint_queue ;
186✔
45

46
    class_map["preload_checkpoint"] = num_classes ;
186✔
47
    class_to_queue[num_classes++] = &preload_checkpoint_queue ;
186✔
48

49
    class_map["restart"] = num_classes ;
186✔
50
    class_to_queue[num_classes++] = &restart_queue ;
186✔
51

52
    the_cpr = this ;
186✔
53
}
186✔
54

55
int Trick::CheckPointRestart::set_pre_init_checkpoint(bool yes_no) {
1✔
56
    pre_init_checkpoint = yes_no ;
1✔
57
    return(0) ;
1✔
58
}
59

60
int Trick::CheckPointRestart::set_post_init_checkpoint(bool yes_no) {
1✔
61
    post_init_checkpoint = yes_no ;
1✔
62
    return(0) ;
1✔
63
}
64

65
int Trick::CheckPointRestart::set_end_checkpoint(bool yes_no) {
2✔
66
    end_checkpoint = yes_no ;
2✔
67
    return(0) ;
2✔
68
}
69

70
int Trick::CheckPointRestart::set_safestore_enabled(bool yes_no) {
×
71
    safestore_enabled = yes_no ;
×
72
    return(0) ;
×
73
}
74

75
int Trick::CheckPointRestart::set_cpu_num(int in_cpu_num) {
×
76
    if ( in_cpu_num <= 0 ) {
×
77
        cpu_num = -1 ;
×
78
    } else {
79
        cpu_num = in_cpu_num ;
×
80
    }
81
    return(0) ;
×
82
}
83

84

85
const char * Trick::CheckPointRestart::get_output_file() {
×
86
    return output_file.c_str() ;
×
87
}
88

89
const char * Trick::CheckPointRestart::get_load_file() {
×
90
    return load_checkpoint_file_name.c_str() ;
×
91
}
92

93
int Trick::CheckPointRestart::find_write_checkpoint_jobs(std::string sim_object_name) {
186✔
94

95
    write_checkpoint_job = exec_get_job(std::string(sim_object_name + ".write_checkpoint").c_str()) ;
186✔
96
    if ( write_checkpoint_job == NULL ) {
186✔
97
        exec_terminate_with_return(-1 , __FILE__ , __LINE__ , "CheckPointRestart could not find write_checkpoint job" ) ;
×
98
    } else {
99
        write_checkpoint_job->next_tics = TRICK_MAX_LONG_LONG ;
186✔
100
    }
101

102
    safestore_checkpoint_job = exec_get_job(std::string(sim_object_name + ".safestore_checkpoint").c_str()) ;
186✔
103
    if ( safestore_checkpoint_job == NULL ) {
186✔
104
        exec_terminate_with_return(-1 , __FILE__ , __LINE__ , "CheckPointRestart could not find safestore_checkpoint job" ) ;
×
105
    } else {
106
        safestore_checkpoint_job->next_tics = TRICK_MAX_LONG_LONG ;
186✔
107
    }
108

109
    return(0) ;
186✔
110
}
111

112
int Trick::CheckPointRestart::checkpoint(double in_time) {
11✔
113

114
    long long curr_time = exec_get_time_tics() ;
11✔
115
    long long new_time ;
116

117
    new_time = (long long)(in_time * exec_get_time_tic_value()) ;
11✔
118

119
    if (new_time >= curr_time ) {
11✔
120
        checkpoint_times.push(new_time) ;
11✔
121
        if ( new_time < write_checkpoint_job->next_tics ) {
11✔
122
            write_checkpoint_job->next_tics = new_time ;
11✔
123
        }
124
        //std::cout << "\033[33mSET CHECKPOINT TIME " << in_time << " " << new_time << "\033[0m" << std::endl ;
125
    } else {
126
        message_publish(MSG_ERROR, "Checkpoint time specified in the past. specified %f, current_time %f\n",
×
127
         in_time , exec_get_sim_time()) ;
128
    }
129

130
    return(0) ;
11✔
131
}
132

133
int Trick::CheckPointRestart::set_safestore_time(double in_time) {
×
134

135
    long long software_frame_tics ;
136

137
    safestore_period = (long long)(in_time * exec_get_time_tic_value()) ;
×
138
    software_frame_tics = exec_get_software_frame_tics() ;
×
139

140
    if ( safestore_period % software_frame_tics ) {
×
141
        safestore_period = ((safestore_time / software_frame_tics) + 1 ) * software_frame_tics ;
×
142
    }
143
    safestore_time = safestore_period ;
×
144

145
    if ( safestore_checkpoint_job != NULL ) {
×
146
        safestore_checkpoint_job->next_tics = safestore_time ;
×
147
    }
148

149
    return(0) ;
×
150
}
151

152
int Trick::CheckPointRestart::checkpoint(std::string file_name, bool print_status, std::string obj_list_str ) {
17✔
153

154
    // first, empty the sim obj list to make sure there is nothing left from last time
155
    obj_list.clear();
17✔
156

157
    if (obj_list_str.compare("")) {
17✔
158
        const char * tok = strtok((char*)obj_list_str.c_str(), ",");
×
159
        while (tok != NULL) {
×
160
            obj_list.push_back(tok);
×
161
            tok = strtok(NULL, ",");
×
162
        }
163
    }
164

165
    do_checkpoint(file_name, print_status) ;
17✔
166

167
    return(0) ;
17✔
168
}
169

170
int Trick::CheckPointRestart::do_checkpoint(std::string file_name, bool print_status) {
17✔
171

172
    JobData * curr_job ;
173
    pid_t pid;
174

175
    if ( ! file_name.compare("") ) {
17✔
176
        std::stringstream file_name_stream ;
×
177
        file_name_stream << "chkpnt_" << std::fixed << std::setprecision(6) << exec_get_sim_time() ;
×
178
        file_name = file_name_stream.str() ;
×
179
    }
180
    output_file = std::string(command_line_args_get_output_dir()) + "/" + file_name ;
17✔
181

182
    checkpoint_queue.reset_curr_index() ;
17✔
183
    while ( (curr_job = checkpoint_queue.get_next_job()) != NULL ) {
64✔
184
        curr_job->parent_object->call_function(curr_job) ;
47✔
185
    }
186

187
    if ( cpu_num != -1 ) {
17✔
188
    // if the user specified a cpu number for the checkpoint, fork a process to write the checkpoint
189
        if ((pid = fork()) == 0) {
×
190
#if __linux__
191
            if ( cpu_num >= 0 ) {
×
192
                unsigned long mask;
193
                mask = 1 << cpu_num ;
×
194
                syscall((long) __NR_sched_setaffinity, 0, sizeof(mask), &mask);
×
195
            }
196
#endif
197
#if __APPLE__
198
            if ( cpu_num >= 0 ) {
199
            }
200
#endif
201
            if (obj_list.empty()) {
×
202
                trick_MM->write_checkpoint(output_file.c_str()) ;
×
203
            } else {
204
                trick_MM->write_checkpoint(output_file.c_str(), obj_list);
×
205
            }
206
            _Exit(0) ;
×
207
        }
208
    }
209
    else {
210
    // no fork
211
        if (obj_list.empty()) {
17✔
212
            trick_MM->write_checkpoint(output_file.c_str()) ;
17✔
213
        } else {
214
            trick_MM->write_checkpoint(output_file.c_str(), obj_list);
×
215
        }
216
    }
217

218
    post_checkpoint_queue.reset_curr_index() ;
17✔
219
    while ( (curr_job = post_checkpoint_queue.get_next_job()) != NULL ) {
47✔
220
        curr_job->parent_object->call_function(curr_job) ;
30✔
221
    }
222

223
    if ( print_status )
17✔
224
    {
225
        if (trick_MM->is_hexfloat_checkpoint())
17✔
226
        {
UNCOV
227
            message_publish(MSG_INFO, "Dumped Checkpoint (floating point values in Hexadecimal) %s.\n", file_name.c_str()) ;
×
228
        }
229
        else
230
        {
231
            message_publish(MSG_INFO, "Dumped ASCII Checkpoint %s.\n", file_name.c_str()) ;
17✔
232
        }
233
    }
234

235
    return 0 ;
17✔
236
}
237

238
int Trick::CheckPointRestart::write_checkpoint() {
11✔
239

240
    long long curr_time = exec_get_time_tics() ;
11✔
241

242
    // checkpoint time is set in a read event that occurs at top of frame
243
    if ( curr_time == checkpoint_times.top() ) {
11✔
244

245
        // remove all times at the top of the queue that match the current time.
246
        while ( !checkpoint_times.empty() and (checkpoint_times.top() == curr_time) ) {
22✔
247
            checkpoint_times.pop() ;
11✔
248
        }
249

250
        if ( !checkpoint_times.empty() ) {
11✔
UNCOV
251
            write_checkpoint_job->next_tics = checkpoint_times.top() ;
×
252
        } else {
253
            write_checkpoint_job->next_tics = TRICK_MAX_LONG_LONG ;
11✔
254
        }
255

256
        double sim_time = exec_get_sim_time() ;
11✔
257
        std::stringstream chk_name_stream ;
11✔
258

259
        chk_name_stream << "chkpnt_" << std::fixed << std::setprecision(6) << sim_time ;
11✔
260

261
        checkpoint( chk_name_stream.str() );
11✔
262

263
    }
264

265
    return(0) ;
11✔
266
}
267

268
int Trick::CheckPointRestart::write_pre_init_checkpoint() {
154✔
269
    if ( pre_init_checkpoint ) {
154✔
UNCOV
270
        checkpoint(std::string("chkpnt_pre_init")) ;
×
271
    }
272
    return 0  ;
154✔
273
}
274

275
int Trick::CheckPointRestart::write_post_init_checkpoint() {
153✔
276
    if ( post_init_checkpoint ) {
153✔
UNCOV
277
        checkpoint(std::string("chkpnt_post_init")) ;
×
278
    }
279
    return 0  ;
153✔
280
}
281

282
int Trick::CheckPointRestart::write_end_checkpoint() {
154✔
283
    if ( end_checkpoint ) {
154✔
284
        checkpoint(std::string("chkpnt_end")) ;
1✔
285
    }
286
    return 0  ;
154✔
287
}
288

289
int Trick::CheckPointRestart::safestore_checkpoint() {
×
290

UNCOV
291
    if ( safestore_enabled) {
×
292
        checkpoint(std::string("chkpnt_safestore"), false) ;
×
UNCOV
293
        safestore_time += safestore_period ;
×
294
    }
295

UNCOV
296
    if ( safestore_checkpoint_job != NULL ) {
×
UNCOV
297
        safestore_checkpoint_job->next_tics = safestore_time ;
×
298
    }
299

300
    return(0) ;
×
301
}
302

303
void Trick::CheckPointRestart::load_checkpoint(std::string file_name) {
11✔
304
    load_checkpoint_file_name = file_name ;
11✔
305
}
11✔
306

UNCOV
307
void Trick::CheckPointRestart::load_checkpoint(std::string file_name, bool stls_on) {
×
UNCOV
308
    trick_MM->set_restore_stls_default(stls_on);
×
UNCOV
309
    load_checkpoint(file_name);
×
UNCOV
310
}
×
311

312
int Trick::CheckPointRestart::load_checkpoint_job() {
113,493✔
313

314
    JobData * curr_job ;
315
    struct stat temp_buf ;
316

317
    if ( ! load_checkpoint_file_name.empty() ) {
113,493✔
318

319
        if ( stat( load_checkpoint_file_name.c_str() , &temp_buf) == 0 ) {
11✔
320
            preload_checkpoint_queue.reset_curr_index() ;
11✔
321
            while ( (curr_job = preload_checkpoint_queue.get_next_job()) != NULL ) {
66✔
322
                curr_job->call() ;
55✔
323
            }
324

325
            // clear the queues!  They will be rebuilt when the executive calls its restart job.
326
            checkpoint_queue.clear() ;
11✔
327
            post_checkpoint_queue.clear() ;
11✔
328
            preload_checkpoint_queue.clear() ;
11✔
329
            restart_queue.clear() ;
11✔
330

331
            message_publish(MSG_INFO, "Load checkpoint file %s.\n", load_checkpoint_file_name.c_str()) ;
11✔
332
            trick_MM->init_from_checkpoint(load_checkpoint_file_name.c_str()) ;
11✔
333

334
            message_publish(MSG_INFO, "Finished loading checkpoint file.  Calling restart jobs.\n") ;
11✔
335

336
            // bootstrap the sim_objects back into the executive!
337
            // TODO: MAKE AN exec_restart() CALL THAT DOES NOT REQUIRE WE USE the_exec.
338
            the_exec->restart() ;
11✔
339

340
            // the restart queue will be rebuilt by the executive.
341
            restart_queue.reset_curr_index() ;
11✔
342
            while ( (curr_job = restart_queue.get_next_job()) != NULL ) {
152✔
343
                curr_job->call() ;
141✔
344
            }
345
        } else {
UNCOV
346
            message_publish(MSG_INFO, "Could not find checkpoint file %s.\n", load_checkpoint_file_name.c_str()) ;
×
347
        }
348
        load_checkpoint_file_name.clear() ;
11✔
349
    }
350

351
    return(0) ;
113,493✔
352
}
353

354
int Trick::CheckPointRestart::write_s_job_execution(FILE *fp) {
153✔
355

356
    if ( fp == NULL ) {
153✔
UNCOV
357
        return(0) ;
×
358
    }
359

360
    fprintf(fp, "\n===================================================================================================\n") ;
153✔
361
    fprintf(fp, "CheckPointRestart :\n\n") ;
153✔
362
    checkpoint_queue.write_non_sched_queue(fp) ;
153✔
363
    post_checkpoint_queue.write_non_sched_queue(fp) ;
153✔
364
    preload_checkpoint_queue.write_non_sched_queue(fp) ;
153✔
365
    restart_queue.write_non_sched_queue(fp) ;
153✔
366
    return 0 ;
153✔
367
}
368

369
int Trick::CheckPointRestart::instrument_job_before( Trick::JobData * instrument_job ) {
1✔
370

371
    int count = 0 ;
1✔
372

373
    /** @par Detailed Design: */
374

375
    /** @li if target_job specified, instrument job will only be inserted before it
376
        note: when more than one target job with same name, this will insert instrument job for 1st occurrence of target_job in each queue
377
     */
378
    count += checkpoint_queue.instrument_before(instrument_job) ;
1✔
379
    count += preload_checkpoint_queue.instrument_before(instrument_job) ;
1✔
380
    count += restart_queue.instrument_before(instrument_job) ;
1✔
381

382
    /** @li Return how many insertions were done. */
383
    return(count) ;
1✔
384

385
}
386

387
int Trick::CheckPointRestart::instrument_job_after( Trick::JobData * instrument_job ) {
1✔
388

389
    int count = 0 ;
1✔
390

391
    /** @par Detailed Design */
392
    // if target_job specified, instrument job will only be inserted after it
393
    // note: when more than one target job with same name, this will insert instrument job for 1st occurrence of target_job in each queue
394

395
    count += checkpoint_queue.instrument_after(instrument_job) ;
1✔
396
    count += preload_checkpoint_queue.instrument_after(instrument_job) ;
1✔
397
    count += restart_queue.instrument_after(instrument_job) ;
1✔
398

399
    /** @li Return how many insertions were done. */
400
    return(count) ;
1✔
401

402
}
403

UNCOV
404
int Trick::CheckPointRestart::instrument_job_remove(std::string in_job) {
×
405

UNCOV
406
    checkpoint_queue.instrument_remove(in_job) ;
×
UNCOV
407
    preload_checkpoint_queue.instrument_remove(in_job) ;
×
UNCOV
408
    restart_queue.instrument_remove(in_job) ;
×
409

UNCOV
410
    return(0) ;
×
411
}
412

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc