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

nasa / trick / 27377772775

11 Jun 2026 09:12PM UTC coverage: 56.123% (-0.003%) from 56.126%
27377772775

Pull #2146

github

web-flow
Merge 614dfdae5 into 0fbae67ec
Pull Request #2146: Make itimer wake up earlier when necessary.

0 of 3 new or added lines in 1 file covered. (0.0%)

13 existing lines in 2 files now uncovered.

14680 of 26157 relevant lines covered (56.12%)

453656.92 hits per line

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

43.08
/trick_source/sim_services/Timer/ITimer.cpp
1
/*
2
PURPOSE:
3
    ( gettimeofday clock )
4
PROGRAMMERS:
5
    (((Robert W. Bailey) (LinCom) (April 1992) (--) (Realtime.))
6
     ((Robert W. Bailey) (LinCom) (7/22/92) (--) (--))
7
     ((Eddie J. Paddock) (MDSSC) (Oct 1992) (--) (Realtime.)))
8
     ((Alex Lin) (NASA) (April 2009) (--) (c++ port)))
9
*/
10

11
#include <iostream>
12
#include <sstream>
13

14
#include <fcntl.h>
15
#include <stdio.h>
16
#include <string.h>
17
#include <errno.h>
18
#include <math.h>
19
#include <signal.h>
20
#include <sys/types.h>
21
#include <unistd.h>
22

23
#include "trick/ITimer.hh"
24
#include "trick/exec_proto.h"
25
#include "trick/message_proto.h"
26

27
Trick::ITimer * the_itimer ;
28

29
Trick::ITimer::ITimer() : Timer() {
187✔
30

31
    the_itimer = this ;
187✔
32

33
#if ! ( __APPLE__ | __INTERIX )
34
    clock_getres(CLOCK_REALTIME, &res);
187✔
35
#else
36
    res.tv_nsec = 10000000;
37
    res.tv_sec = 0;
38
#endif
39

40
    return ;
187✔
41
}
42

43
Trick::ITimer::~ITimer() {
187✔
44
    return ;
187✔
45
}
187✔
46

47
void Trick::ITimer::semaphore_post() {
×
48
    sem_post(semaphore) ;
×
49
}
×
50

51
/**
52
@details
53
-# Set the SIGALRM signal handler to the ITimer signal handler routine.
54
-# Open a unique named semaphore
55
*/
56
int Trick::ITimer::init() {
2✔
57

58
    static struct sigaction sigact;
59
    std::stringstream sem_name_stream ;
2✔
60

61
    sigact.sa_handler = (void (*)(int)) it_handler;
2✔
62
    if (sigaction(SIGALRM, &sigact, NULL) < 0) {
2✔
63
        perror("sigaction() failed for SIGALRM");
×
64
    }
65

66
    // Initialize the itimer semaphore used by SIGALRM handler to
67
    // wake up the sim_exec_loop from its underrun sleepy time.
68

69
    sem_name_stream << "itimersepmaphore_" << getpid() ;
2✔
70
    sem_name = sem_name_stream.str() ;
2✔
71

72
    semaphore = sem_open(sem_name.c_str(), O_CREAT, S_IRWXU , 0);
2✔
73

74
    return (0) ;
2✔
75
}
2✔
76

77
/**
78
@details
79
-# If the timer is enabled
80
   -# If the frame time is valid
81
      -# Set the system itimer to expire one time at the desired frame period.
82
   -# Else termiate the simulation with the error message stating that the
83
      itimer frame period is invalid.
84
*/
85
int Trick::ITimer::start(double in_frame_time) {
51✔
86

87
    struct itimerval ivalue;
88
    int ret ;
89
    unsigned long long frame_sec ;
90
    unsigned int frame_usec ;
91

92
    if ( enabled ) {
51✔
93

94
        // clear out any built up signals.
95
        while ( (ret = sem_trywait(semaphore)) == 0 ) ;
×
96

97
        frame_sec = ( unsigned long long )in_frame_time ;
×
98
        frame_usec = (unsigned int)((in_frame_time - frame_sec) * 1000000) ;
×
99

100
        // check minimum time > 10ms
101
        if ( frame_sec == 0 && frame_usec < 10000 ) {
×
102
            active = false ;
×
103
            return(0) ;
×
104
        }
105
        // Arm the itimer slightly early so clock_spin() can busy-wait to the
106
        // frame boundary.  On macOS, timer coalescing can delay SIGALRM delivery
107
        // by 3-15ms, so scale the value to 5% of the frame (with a range of 2-8ms). 
108
        // On Linux a fixed 2ms is sufficient.
109
#if __APPLE__
110
        {
111
            // Frames < 10ms are already rejected above, so frame_usec >= 10000 thus >= 8000
112
            // When frame_sec == 0, meaning the borrow path only triggers for frames >= 1s.
113
            unsigned int scaled_usec = (unsigned int)(in_frame_time * 1000000) / 20 ; // 5% of frame
114
            if ( scaled_usec < 2000 ) scaled_usec = 2000 ;
115
            if ( scaled_usec > 8000 ) scaled_usec = 8000 ;
116
            if ( frame_usec < scaled_usec ) 
117
            {
118
                frame_sec  -= 1 ;
119
                frame_usec += 1000000 ;
120
            }
121
            frame_usec -= scaled_usec ;
122
        }
123
#else
NEW
124
        if ( frame_usec < 2000 && frame_sec > 0 ) {
×
NEW
125
            frame_sec  -= 1 ;
×
NEW
126
            frame_usec += 1000000 ;
×
127
        }
128
        frame_usec -= 2000 ;
×
129
#endif
130

131
        if ( in_frame_time > 0 ) {
×
132
            /* Set timer interval in micro-seconds */
133
            ivalue.it_interval.tv_sec = 0;
×
134
            ivalue.it_interval.tv_usec = 0;
×
UNCOV
135
            ivalue.it_value.tv_sec = frame_sec;
×
136
            ivalue.it_value.tv_usec = (long) frame_usec;
×
137

138
            setitimer(ITIMER_REAL, &ivalue, NULL);
×
139
            active = true ;
×
140
        } else {
141
            char error_message[256];
142
            snprintf(error_message, sizeof(error_message), "itimer frame_time is not set\n" ) ;
×
UNCOV
143
            exec_terminate_with_return(-1, __FILE__, __LINE__ , error_message);
×
144
        }
145
    }
146
    return (0) ;
51✔
147

148
}
149

150
/*
151
@details
152
-# Call the start function
153
*/
154
int Trick::ITimer::reset(double in_frame_time) {
50✔
155
    start(in_frame_time) ;
50✔
156
    return (0) ;
50✔
157
}
158

159
/**
160
@details
161
-# Set the system itimer with the expiriation time of 0.
162
*/
UNCOV
163
int Trick::ITimer::stop() {
×
164

165
    static struct itimerval ivalue, ovalue;
166

167
    /* Turn off itimer */
UNCOV
168
    ivalue.it_interval.tv_usec = 0;
×
UNCOV
169
    ivalue.it_interval.tv_sec = 0;
×
170

171
    ivalue.it_value.tv_usec = 0;
×
172
    ivalue.it_value.tv_sec = 0;
×
173

174
    (void) setitimer(ITIMER_REAL, &ivalue, &ovalue);
×
175

UNCOV
176
    active = false ;
×
177

UNCOV
178
    return (0) ;
×
179

180
}
181

182
/**
183
@details
184
-# If the timer is enabled
185
   -# Call sem_wait to decrement the semaphore.  sem_wait will sleep and wait
186
      for the semaphore to be posted from the signal handler.
187
   -# If sem_wait returns an error, write an error message
188
*/
189
int Trick::ITimer::pause() {
50✔
190

191
    int sem_ret;
192

193
    if ( enabled and active ) {
50✔
194

195
        do {
UNCOV
196
            sem_ret = sem_wait(semaphore);
×
197
            //TODO: I'd like to use sem_timedwait
198
            //sem_ret = sem_timedwait(semaphore, &timeout);
199
        } while ((sem_ret < 0 && errno == EINTR) && (errno != EDEADLK));
×
200

UNCOV
201
        if (sem_ret != 0) {
×
202
            // Prepend info to the error message string (given by errno)
UNCOV
203
            perror( "ITimer call to sem_wait()" );
×
204
        }
205

206
    }
207

208
    return (0) ;
50✔
209

210
}
211

212
/**
213
@details
214
-# Unlink the semaphore
215
*/
216
int Trick::ITimer::shutdown() {
1✔
217
    sem_unlink(sem_name.c_str()) ;
1✔
218
    return(0) ;
1✔
219
}
220

UNCOV
221
Trick::ITimer * get_itimer() {
×
UNCOV
222
    return (the_itimer) ;
×
223
}
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