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

nasa / trick / 29045062286

09 Jul 2026 07:38PM UTC coverage: 56.149% (+0.004%) from 56.145%
29045062286

push

github

web-flow
Let user aware when itimer is deactivate because the frame time is less than 10ms. (#2155)

* Let user aware when itimer is disabled automatically due to frame time is less than minimum 10ms.

* clang-format

* Print the warning message when the itimer is changed from active to inactive due to the frame time < 10ms.

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

14693 of 26168 relevant lines covered (56.15%)

461683.32 hits per line

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

41.79
/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 ) {
×
NEW
102
            if (active)
×
103
            {
NEW
104
                message_publish(MSG_WARNING,
×
105
                                "ITimer: frame time (%g s or %g ms) is less than 10ms minimum; "
106
                                "itimer deactivated, falling back to clock_spin.\n",
107
                                in_frame_time, in_frame_time * 1000);
108
            }
109
            active = false ;
×
110
            return(0) ;
×
111
        }
112
        // Arm the itimer slightly early so clock_spin() can busy-wait to the
113
        // frame boundary.  On macOS, timer coalescing can delay SIGALRM delivery
114
        // by 3-15ms, so scale the value to 5% of the frame (with a range of 2-8ms).
115
        // On Linux a fixed 2ms is sufficient.
116
#if __APPLE__
117
        {
118
            // Frames < 10ms are already rejected above, so frame_usec >= 10000 thus >= 8000
119
            // When frame_sec == 0, meaning the borrow path only triggers for frames >= 1s.
120
            unsigned int scaled_usec = (unsigned int)(in_frame_time * 1000000) / 20; // 5% of frame
121
            if (scaled_usec < 2000)
122
                scaled_usec = 2000;
123
            if (scaled_usec > 8000)
124
                scaled_usec = 8000;
125
            if (frame_usec < scaled_usec)
126
            {
127
                frame_sec  -= 1;
128
                frame_usec += 1000000;
129
            }
130
            frame_usec -= scaled_usec;
131
        }
132
#else
133
        if (frame_usec < 2000 && frame_sec > 0)
×
134
        {
135
            frame_sec  -= 1 ;
×
136
            frame_usec += 1000000 ;
×
137
        }
138
        frame_usec -= 2000 ;
×
139
#endif
140

141
        if ( in_frame_time > 0 ) {
×
142
            /* Set timer interval in micro-seconds */
143
            ivalue.it_interval.tv_sec = 0;
×
144
            ivalue.it_interval.tv_usec = 0;
×
145
            ivalue.it_value.tv_sec = frame_sec;
×
146
            ivalue.it_value.tv_usec = (long) frame_usec;
×
147

148
            setitimer(ITIMER_REAL, &ivalue, NULL);
×
149
            active = true ;
×
150
        } else {
151
            char error_message[256];
152
            snprintf(error_message, sizeof(error_message), "itimer frame_time is not set\n" ) ;
×
153
            exec_terminate_with_return(-1, __FILE__, __LINE__ , error_message);
×
154
        }
155
    }
156
    return (0) ;
51✔
157

158
}
159

160
/*
161
@details
162
-# Call the start function
163
*/
164
int Trick::ITimer::reset(double in_frame_time) {
50✔
165
    start(in_frame_time) ;
50✔
166
    return (0) ;
50✔
167
}
168

169
/**
170
@details
171
-# Set the system itimer with the expiriation time of 0.
172
*/
173
int Trick::ITimer::stop() {
×
174

175
    static struct itimerval ivalue, ovalue;
176

177
    /* Turn off itimer */
178
    ivalue.it_interval.tv_usec = 0;
×
179
    ivalue.it_interval.tv_sec = 0;
×
180

181
    ivalue.it_value.tv_usec = 0;
×
182
    ivalue.it_value.tv_sec = 0;
×
183

184
    (void) setitimer(ITIMER_REAL, &ivalue, &ovalue);
×
185

186
    active = false ;
×
187

188
    return (0) ;
×
189

190
}
191

192
/**
193
@details
194
-# If the timer is enabled
195
   -# Call sem_wait to decrement the semaphore.  sem_wait will sleep and wait
196
      for the semaphore to be posted from the signal handler.
197
   -# If sem_wait returns an error, write an error message
198
*/
199
int Trick::ITimer::pause() {
50✔
200

201
    int sem_ret;
202

203
    if ( enabled and active ) {
50✔
204

205
        do {
206
            sem_ret = sem_wait(semaphore);
×
207
            //TODO: I'd like to use sem_timedwait
208
            //sem_ret = sem_timedwait(semaphore, &timeout);
209
        } while ((sem_ret < 0 && errno == EINTR) && (errno != EDEADLK));
×
210

211
        if (sem_ret != 0) {
×
212
            // Prepend info to the error message string (given by errno)
213
            perror( "ITimer call to sem_wait()" );
×
214
        }
215

216
    }
217

218
    return (0) ;
50✔
219

220
}
221

222
/**
223
@details
224
-# Unlink the semaphore
225
*/
226
int Trick::ITimer::shutdown() {
1✔
227
    sem_unlink(sem_name.c_str()) ;
1✔
228
    return(0) ;
1✔
229
}
230

231
Trick::ITimer * get_itimer() {
×
232
    return (the_itimer) ;
×
233
}
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