You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					74 lines
				
				1.5 KiB
			
		
		
			
		
	
	
					74 lines
				
				1.5 KiB
			| 
											6 years ago
										 | #include <sys/resource.h>
 | ||
|  | #include <sys/time.h>
 | ||
| 
											5 years ago
										 | #include <unistd.h>
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											4 years ago
										 | #include <cstdint>
 | ||
|  | #include <cstdio>
 | ||
|  | 
 | ||
| 
											5 years ago
										 | // Apple doesn't have timerfd
 | ||
|  | #ifdef __APPLE__
 | ||
|  | #include <thread>
 | ||
|  | #else
 | ||
|  | #include <sys/timerfd.h>
 | ||
|  | #endif
 | ||
|  | 
 | ||
| 
											5 years ago
										 | #include <cassert>
 | ||
| 
											5 years ago
										 | #include <chrono>
 | ||
|  | 
 | ||
| 
											5 years ago
										 | #include "cereal/messaging/messaging.h"
 | ||
| 
											4 years ago
										 | #include "common/timing.h"
 | ||
|  | #include "common/util.h"
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											5 years ago
										 | ExitHandler do_exit;
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											6 years ago
										 | int main() {
 | ||
|  |   setpriority(PRIO_PROCESS, 0, -13);
 | ||
| 
											6 years ago
										 |   PubMaster pm({"clocks"});
 | ||
| 
											6 years ago
										 | 
 | ||
| 
											5 years ago
										 | #ifndef __APPLE__
 | ||
| 
											6 years ago
										 |   int timerfd = timerfd_create(CLOCK_BOOTTIME, 0);
 | ||
|  |   assert(timerfd >= 0);
 | ||
|  | 
 | ||
|  |   struct itimerspec spec = {0};
 | ||
|  |   spec.it_interval.tv_sec = 1;
 | ||
|  |   spec.it_interval.tv_nsec = 0;
 | ||
|  |   spec.it_value.tv_sec = 1;
 | ||
|  |   spec.it_value.tv_nsec = 0;
 | ||
|  | 
 | ||
| 
											5 years ago
										 |   int err = timerfd_settime(timerfd, 0, &spec, 0);
 | ||
| 
											6 years ago
										 |   assert(err == 0);
 | ||
|  | 
 | ||
|  |   uint64_t expirations = 0;
 | ||
| 
											5 years ago
										 |   while (!do_exit && (err = read(timerfd, &expirations, sizeof(expirations)))) {
 | ||
| 
											4 years ago
										 |     if (err < 0) {
 | ||
|  |       if (errno == EINTR) continue;
 | ||
|  |       break;
 | ||
|  |     }
 | ||
| 
											5 years ago
										 | #else
 | ||
|  |   // Just run at 1Hz on apple
 | ||
| 
											4 years ago
										 |   while (!do_exit) {
 | ||
| 
											5 years ago
										 |     util::sleep_for(1000);
 | ||
| 
											5 years ago
										 | #endif
 | ||
| 
											6 years ago
										 | 
 | ||
|  |     uint64_t boottime = nanos_since_boot();
 | ||
|  |     uint64_t monotonic = nanos_monotonic();
 | ||
|  |     uint64_t monotonic_raw = nanos_monotonic_raw();
 | ||
|  |     uint64_t wall_time = nanos_since_epoch();
 | ||
|  | 
 | ||
| 
											5 years ago
										 |     MessageBuilder msg;
 | ||
|  |     auto clocks = msg.initEvent().initClocks();
 | ||
| 
											6 years ago
										 | 
 | ||
|  |     clocks.setBootTimeNanos(boottime);
 | ||
|  |     clocks.setMonotonicNanos(monotonic);
 | ||
|  |     clocks.setMonotonicRawNanos(monotonic_raw);
 | ||
|  |     clocks.setWallTimeNanos(wall_time);
 | ||
|  | 
 | ||
| 
											6 years ago
										 |     pm.send("clocks", msg);
 | ||
| 
											6 years ago
										 |   }
 | ||
|  | 
 | ||
| 
											5 years ago
										 | #ifndef __APPLE__
 | ||
| 
											6 years ago
										 |   close(timerfd);
 | ||
| 
											5 years ago
										 | #endif
 | ||
| 
											6 years ago
										 |   return 0;
 | ||
| 
											5 years ago
										 | }
 |