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

nasa / trick / 28386878450

29 Jun 2026 04:23PM UTC coverage: 56.143% (-0.01%) from 56.153%
28386878450

Pull #2155

github

web-flow
Merge ccb1fe013 into b2ea6f12e
Pull Request #2155: Let user aware when itimer is disabled because the frame time is less than 10ms.

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

2 existing lines in 1 file now uncovered.

14691 of 26167 relevant lines covered (56.14%)

461667.58 hits per line

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

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

136
        if ( in_frame_time > 0 ) {
×
137
            /* Set timer interval in micro-seconds */
138
            ivalue.it_interval.tv_sec = 0;
×
139
            ivalue.it_interval.tv_usec = 0;
×
140
            ivalue.it_value.tv_sec = frame_sec;
×
141
            ivalue.it_value.tv_usec = (long) frame_usec;
×
142

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

153
}
154

155
/*
156
@details
157
-# Call the start function
158
*/
159
int Trick::ITimer::reset(double in_frame_time) {
50✔
160
    start(in_frame_time) ;
50✔
161
    return (0) ;
50✔
162
}
163

164
/**
165
@details
166
-# Set the system itimer with the expiriation time of 0.
167
*/
168
int Trick::ITimer::stop() {
×
169

170
    static struct itimerval ivalue, ovalue;
171

172
    /* Turn off itimer */
173
    ivalue.it_interval.tv_usec = 0;
×
174
    ivalue.it_interval.tv_sec = 0;
×
175

176
    ivalue.it_value.tv_usec = 0;
×
177
    ivalue.it_value.tv_sec = 0;
×
178

179
    (void) setitimer(ITIMER_REAL, &ivalue, &ovalue);
×
180

181
    active = false ;
×
182

183
    return (0) ;
×
184

185
}
186

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

196
    int sem_ret;
197

198
    if ( enabled and active ) {
50✔
199

200
        do {
201
            sem_ret = sem_wait(semaphore);
×
202
            //TODO: I'd like to use sem_timedwait
203
            //sem_ret = sem_timedwait(semaphore, &timeout);
204
        } while ((sem_ret < 0 && errno == EINTR) && (errno != EDEADLK));
×
205

206
        if (sem_ret != 0) {
×
207
            // Prepend info to the error message string (given by errno)
208
            perror( "ITimer call to sem_wait()" );
×
209
        }
210

211
    }
212

213
    return (0) ;
50✔
214

215
}
216

217
/**
218
@details
219
-# Unlink the semaphore
220
*/
221
int Trick::ITimer::shutdown() {
1✔
222
    sem_unlink(sem_name.c_str()) ;
1✔
223
    return(0) ;
1✔
224
}
225

226
Trick::ITimer * get_itimer() {
×
227
    return (the_itimer) ;
×
228
}
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