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

nasa / trick / 23871410048

01 Apr 2026 09:20PM UTC coverage: 55.811% (-0.1%) from 55.922%
23871410048

Pull #2011

github

web-flow
Merge 933021c3f into 5cf9d7558
Pull Request #2011: Standardize code style using clang-format

12577 of 22535 relevant lines covered (55.81%)

310562.05 hits per line

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

67.62
/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

54
    redirect_data_recording = false;
186✔
55
    redirected_data_recording_directory = "";
186✔
56
}
186✔
57

58
int Trick::CheckPointRestart::set_pre_init_checkpoint(bool yes_no) {
1✔
59
    pre_init_checkpoint = yes_no ;
1✔
60
    return(0) ;
1✔
61
}
62

63
int Trick::CheckPointRestart::set_post_init_checkpoint(bool yes_no) {
1✔
64
    post_init_checkpoint = yes_no ;
1✔
65
    return(0) ;
1✔
66
}
67

68
int Trick::CheckPointRestart::set_end_checkpoint(bool yes_no) {
2✔
69
    end_checkpoint = yes_no ;
2✔
70
    return(0) ;
2✔
71
}
72

73
int Trick::CheckPointRestart::set_safestore_enabled(bool yes_no) {
×
74
    safestore_enabled = yes_no ;
×
75
    return(0) ;
×
76
}
77

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

87

88
const char * Trick::CheckPointRestart::get_output_file() {
×
89
    return output_file.c_str() ;
×
90
}
91

92
const char * Trick::CheckPointRestart::get_load_file() {
×
93
    return load_checkpoint_file_name.c_str() ;
×
94
}
95

96
int Trick::CheckPointRestart::find_write_checkpoint_jobs(std::string sim_object_name) {
186✔
97

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

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

112
    return(0) ;
186✔
113
}
114

115
int Trick::CheckPointRestart::checkpoint(double in_time) {
11✔
116

117
    long long curr_time = exec_get_time_tics() ;
11✔
118
    long long new_time ;
119

120
    new_time = (long long)(in_time * exec_get_time_tic_value()) ;
11✔
121

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

133
    return(0) ;
11✔
134
}
135

136
int Trick::CheckPointRestart::set_safestore_time(double in_time) {
×
137

138
    long long software_frame_tics ;
139

140
    safestore_period = (long long)(in_time * exec_get_time_tic_value()) ;
×
141
    software_frame_tics = exec_get_software_frame_tics() ;
×
142

143
    if ( safestore_period % software_frame_tics ) {
×
144
        safestore_period = ((safestore_time / software_frame_tics) + 1 ) * software_frame_tics ;
×
145
    }
146
    safestore_time = safestore_period ;
×
147

148
    if ( safestore_checkpoint_job != NULL ) {
×
149
        safestore_checkpoint_job->next_tics = safestore_time ;
×
150
    }
151

152
    return(0) ;
×
153
}
154

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

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

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

168
    do_checkpoint(file_name, print_status) ;
17✔
169

170
    return(0) ;
17✔
171
}
172

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

175
    JobData * curr_job ;
176
    pid_t pid;
177

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

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

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

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

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

238
    return 0 ;
17✔
239
}
240

241
int Trick::CheckPointRestart::write_checkpoint() {
11✔
242

243
    long long curr_time = exec_get_time_tics() ;
11✔
244

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

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

253
        if ( !checkpoint_times.empty() ) {
11✔
254
            write_checkpoint_job->next_tics = checkpoint_times.top() ;
×
255
        } else {
256
            write_checkpoint_job->next_tics = TRICK_MAX_LONG_LONG ;
11✔
257
        }
258

259
        double sim_time = exec_get_sim_time() ;
11✔
260
        std::stringstream chk_name_stream ;
11✔
261

262
        chk_name_stream << "chkpnt_" << std::fixed << std::setprecision(6) << sim_time ;
11✔
263

264
        checkpoint( chk_name_stream.str() );
11✔
265

266
    }
267

268
    return(0) ;
11✔
269
}
270

271
int Trick::CheckPointRestart::write_pre_init_checkpoint() {
154✔
272
    if ( pre_init_checkpoint ) {
154✔
273
        checkpoint(std::string("chkpnt_pre_init")) ;
×
274
    }
275
    return 0  ;
154✔
276
}
277

278
int Trick::CheckPointRestart::write_post_init_checkpoint() {
153✔
279
    if ( post_init_checkpoint ) {
153✔
280
        checkpoint(std::string("chkpnt_post_init")) ;
×
281
    }
282
    return 0  ;
153✔
283
}
284

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

292
int Trick::CheckPointRestart::safestore_checkpoint() {
×
293

294
    if ( safestore_enabled) {
×
295
        checkpoint(std::string("chkpnt_safestore"), false) ;
×
296
        safestore_time += safestore_period ;
×
297
    }
298

299
    if ( safestore_checkpoint_job != NULL ) {
×
300
        safestore_checkpoint_job->next_tics = safestore_time ;
×
301
    }
302

303
    return(0) ;
×
304
}
305

306
void Trick::CheckPointRestart::load_checkpoint(std::string file_name) {
11✔
307
    load_checkpoint_file_name = file_name ;
11✔
308
}
11✔
309

310
void Trick::CheckPointRestart::load_checkpoint(std::string file_name, bool stls_on) {
×
311
    trick_MM->set_restore_stls_default(stls_on);
×
312
    load_checkpoint(file_name);
×
313
}
×
314

315
void Trick::CheckPointRestart::load_checkpoint(std::string file_name, std::string new_data_record_dir)
1✔
316
{
317

318
    redirect_data_recording = true;
1✔
319
    redirected_data_recording_directory = new_data_record_dir;
1✔
320
    load_checkpoint(file_name);
1✔
321
}
1✔
322

323
void Trick::CheckPointRestart::load_checkpoint(std::string file_name, std::string new_data_record_dir, bool stls_on)
×
324
{
325

326
    redirect_data_recording = true;
×
327
    redirected_data_recording_directory = new_data_record_dir;
×
328
    trick_MM->set_restore_stls_default(stls_on);
×
329
    load_checkpoint(file_name);
×
330
}
×
331

332

333
int Trick::CheckPointRestart::load_checkpoint_job() {
106,286✔
334

335
    JobData * curr_job ;
336
    struct stat temp_buf ;
337

338
    if ( ! load_checkpoint_file_name.empty() ) {
106,286✔
339

340
        if ( stat( load_checkpoint_file_name.c_str() , &temp_buf) == 0 ) {
11✔
341
            preload_checkpoint_queue.reset_curr_index() ;
11✔
342
            while ( (curr_job = preload_checkpoint_queue.get_next_job()) != NULL ) {
66✔
343
                curr_job->call() ;
55✔
344
            }
345

346
            // clear the queues!  They will be rebuilt when the executive calls its restart job.
347
            checkpoint_queue.clear() ;
11✔
348
            post_checkpoint_queue.clear() ;
11✔
349
            preload_checkpoint_queue.clear() ;
11✔
350
            restart_queue.clear() ;
11✔
351

352
            message_publish(MSG_INFO, "Load checkpoint file %s.\n", load_checkpoint_file_name.c_str()) ;
11✔
353
            trick_MM->init_from_checkpoint(load_checkpoint_file_name.c_str()) ;
11✔
354

355
            message_publish(MSG_INFO, "Finished loading checkpoint file.  Calling restart jobs.\n") ;
11✔
356
            if (redirect_data_recording)
11✔
357
            {
358
                redirect_data_recording = false;
1✔
359
                set_output_dir( redirected_data_recording_directory.c_str() );
1✔
360
            }
361

362

363
            // bootstrap the sim_objects back into the executive!
364
            // TODO: MAKE AN exec_restart() CALL THAT DOES NOT REQUIRE WE USE the_exec.
365
            the_exec->restart() ;
11✔
366

367
            // the restart queue will be rebuilt by the executive.
368
            restart_queue.reset_curr_index() ;
11✔
369
            while ( (curr_job = restart_queue.get_next_job()) != NULL ) {
152✔
370
                curr_job->call() ;
141✔
371
            }
372
        } else {
373
            message_publish(MSG_INFO, "Could not find checkpoint file %s.\n", load_checkpoint_file_name.c_str()) ;
×
374
        }
375
        load_checkpoint_file_name.clear() ;
11✔
376
    }
377

378
    return(0) ;
106,286✔
379
}
380

381
int Trick::CheckPointRestart::write_s_job_execution(FILE *fp) {
153✔
382

383
    if ( fp == NULL ) {
153✔
384
        return(0) ;
×
385
    }
386

387
    fprintf(fp, "\n===================================================================================================\n") ;
153✔
388
    fprintf(fp, "CheckPointRestart :\n\n") ;
153✔
389
    checkpoint_queue.write_non_sched_queue(fp) ;
153✔
390
    post_checkpoint_queue.write_non_sched_queue(fp) ;
153✔
391
    preload_checkpoint_queue.write_non_sched_queue(fp) ;
153✔
392
    restart_queue.write_non_sched_queue(fp) ;
153✔
393
    return 0 ;
153✔
394
}
395

396
int Trick::CheckPointRestart::instrument_job_before( Trick::JobData * instrument_job ) {
1✔
397

398
    int count = 0 ;
1✔
399

400
    /** @par Detailed Design: */
401

402
    /** @li if target_job specified, instrument job will only be inserted before it
403
        note: when more than one target job with same name, this will insert instrument job for 1st occurrence of target_job in each queue
404
     */
405
    count += checkpoint_queue.instrument_before(instrument_job) ;
1✔
406
    count += preload_checkpoint_queue.instrument_before(instrument_job) ;
1✔
407
    count += restart_queue.instrument_before(instrument_job) ;
1✔
408

409
    /** @li Return how many insertions were done. */
410
    return(count) ;
1✔
411

412
}
413

414
int Trick::CheckPointRestart::instrument_job_after( Trick::JobData * instrument_job ) {
1✔
415

416
    int count = 0 ;
1✔
417

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

422
    count += checkpoint_queue.instrument_after(instrument_job) ;
1✔
423
    count += preload_checkpoint_queue.instrument_after(instrument_job) ;
1✔
424
    count += restart_queue.instrument_after(instrument_job) ;
1✔
425

426
    /** @li Return how many insertions were done. */
427
    return(count) ;
1✔
428

429
}
430

431
int Trick::CheckPointRestart::instrument_job_remove(std::string in_job) {
×
432

433
    checkpoint_queue.instrument_remove(in_job) ;
×
434
    preload_checkpoint_queue.instrument_remove(in_job) ;
×
435
    restart_queue.instrument_remove(in_job) ;
×
436

437
    return(0) ;
×
438
}
439

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