OSDN Git Service

Standardize timestamps on CLOCK_BOOTTIME
authorPatrick Porlan <patrick.porlan@intel.com>
Mon, 24 Nov 2014 10:52:50 +0000 (11:52 +0100)
committerPatrick Porlan <patrick.porlan@intel.com>
Mon, 24 Nov 2014 10:52:50 +0000 (11:52 +0100)
That's time since boot, including sleep, and should match the CDD
5.0 requirement to sync with elapsedRealtimeNanos (which is what
we were doing anyway).

The iio timestamp channel provides timestamps based on the output
of iio_get_time_ns however, which is based on ktime_get_real_ns,
so we have no choice bu periodically recompute the difference
between these.

Change-Id: I1db6c1cea89d8507e1557412267d67756eba5749
Signed-off-by: Patrick Porlan <patrick.porlan@intel.com>
common.h
control.c
utils.c
utils.h

index 0dfa098..f07d364 100644 (file)
--- a/common.h
+++ b/common.h
@@ -241,7 +241,4 @@ extern struct sensor_t      sensor_desc[MAX_SENSORS];
 extern struct sensor_info_t            sensor_info[MAX_SENSORS];
 extern struct sensor_catalog_entry_t   sensor_catalog[];
 
-/* We are required to be in sync with SystemClock.getNanos */
-extern int64_t ts_delta;
-
 #endif
index b16204d..9d8baa4 100644 (file)
--- a/control.c
+++ b/control.c
@@ -30,8 +30,6 @@ static int poll_fd; /* epoll instance covering all enabled sensors */
 
 static int active_poll_sensors; /* Number of enabled poll-mode sensors */
 
-int64_t ts_delta; /* delta between SystemClock.getNanos and our timestamp */
-
 static int64_t sys_to_rt_delta;        /* delta between system and realtime clocks */
 
 /* We use pthread condition variables to get worker threads out of sleep */
@@ -462,11 +460,11 @@ static void* acquisition_routine (void* param)
        pthread_mutex_lock(&thread_release_mutex[s]);
 
        /* Pinpoint the moment we start sampling */
-       timestamp = get_timestamp_monotonic();
+       timestamp = get_timestamp_boot();
 
        /* Check and honor termination requests */
        while (sensor_info[s].thread_data_fd[1] != -1) {
-               start = get_timestamp();
+               start = get_timestamp_boot();
                /* Read values through sysfs */
                for (c=0; c<num_fields; c++) {
                        data.data[c] = acquire_immediate_value(s, c);
@@ -474,7 +472,7 @@ static void* acquisition_routine (void* param)
                        if (sensor_info[s].thread_data_fd[1] == -1)
                                goto exit;
                }
-               stop = get_timestamp();
+               stop = get_timestamp_boot();
                data.timestamp = start/2 + stop/2;
 
                /* If the sample looks good */
@@ -598,8 +596,8 @@ int sensor_activate(int s, int enabled)
 
        /* Prepare the report timestamp field for the first event, see set_report_ts method */
        sensor_info[s].report_ts = 0;
-       ts_delta = load_timestamp_sys_clock() - get_timestamp_monotonic();
-       sys_to_rt_delta = get_timestamp_realtime() - load_timestamp_sys_clock();
+
+       sys_to_rt_delta = get_timestamp_realtime() - get_timestamp_boot();
 
 
        /* If we want to activate gyro calibrated and gyro uncalibrated is activated
@@ -915,7 +913,7 @@ static int integrate_device_report (int dev_num)
                for (s=0; s<MAX_SENSORS; s++)
                        if (sensor_info[s].dev_num == dev_num &&
                                sensor_info[s].enabled)
-                                       set_report_ts(s, get_timestamp());
+                                       set_report_ts(s, get_timestamp_boot());
                return 0;
        }
 
@@ -931,7 +929,7 @@ static int integrate_device_report (int dev_num)
                for (s=0; s<MAX_SENSORS; s++)
                        if (sensor_info[s].dev_num == dev_num &&
                                sensor_info[s].enabled)
-                                       set_report_ts(s, get_timestamp());
+                                       set_report_ts(s, get_timestamp_boot());
                return 0;
        }
 
@@ -1040,7 +1038,7 @@ static void synthetize_duplicate_samples (void)
 
                period = (int64_t) (1000000000.0/ sensor_info[s].sampling_rate);
 
-               current_ts = get_timestamp();
+               current_ts = get_timestamp_boot();
                target_ts = sensor_info[s].report_ts + period;
 
                if (target_ts <= current_ts) {
@@ -1112,7 +1110,7 @@ static int get_poll_wait_timeout (void)
        if (target_ts == INT64_MAX)
                return -1; /* Infinite wait */
 
-       ms_to_wait = (target_ts - get_timestamp()) / 1000000;
+       ms_to_wait = (target_ts - get_timestamp_boot()) / 1000000;
 
        /* If the target timestamp is already behind us, don't wait */
        if (ms_to_wait < 1)
diff --git a/utils.c b/utils.c
index dbfd48e..240b0c2 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -258,64 +258,25 @@ int decode_type_spec(     const char type_buf[MAX_TYPE_SPEC_LEN],
 }
 
 
-int64_t load_timestamp_monotonic(struct timespec *ts)
-{
-       clock_gettime(CLOCK_MONOTONIC, ts);
-
-       return (1000000000LL * ts->tv_sec + ts->tv_nsec);
-}
-
-
-int64_t load_timestamp_sys_clock(void)
-{
-       static int s_fd = -1;
-       int fd, result = 0;
-       struct timespec ts;
-
-       if (s_fd == -1) {
-               fd = open("/dev/alarm", O_RDONLY);
-               if (android_atomic_cmpxchg(-1, fd, &s_fd)) {
-                       close(fd);
-               }
-       }
-
-       result = ioctl(s_fd,
-               ANDROID_ALARM_GET_TIME(ANDROID_ALARM_ELAPSED_REALTIME), &ts);
-
-       if (result != 0) {
-               /** /dev/alarm doesn't exist, fallback to CLOCK_BOOTTIME */
-               result = clock_gettime(CLOCK_BOOTTIME, &ts);
-       }
-
-       return 1000000000LL * ts.tv_sec + ts.tv_nsec;
-}
-
-
 int64_t get_timestamp_realtime (void)
 {
        struct timespec ts = {0};
-
        clock_gettime(CLOCK_REALTIME, &ts);
 
        return 1000000000LL * ts.tv_sec + ts.tv_nsec;
 }
 
 
-int64_t get_timestamp_monotonic(void)
+int64_t get_timestamp_boot (void)
 {
        struct timespec ts = {0};
+       clock_gettime(CLOCK_BOOTTIME, &ts);
 
-       return load_timestamp_monotonic(&ts);
-}
-
-
-int64_t get_timestamp(void)
-{
-       return (get_timestamp_monotonic() + ts_delta);
+       return 1000000000LL * ts.tv_sec + ts.tv_nsec;
 }
 
 
-void set_timestamp(struct timespec *out, int64_t target_ns)
+void set_timestamp (struct timespec *out, int64_t target_ns)
 {
        out->tv_sec  = target_ns / 1000000000LL;
        out->tv_nsec = target_ns % 1000000000LL;
diff --git a/utils.h b/utils.h
index 4e29536..54410d5 100644 (file)
--- a/utils.h
+++ b/utils.h
@@ -19,14 +19,10 @@ int sysfs_write_float(const char path[PATH_MAX], float value);
 int    decode_type_spec(const char type_buf[MAX_TYPE_SPEC_LEN],
                         struct datum_info_t *type_info);
 
-int64_t        load_timestamp_monotonic        (struct timespec *ts);
-int64_t        get_timestamp_monotonic (void);
 void   set_timestamp   (struct timespec *out, int64_t target_ns);
 
-int64_t get_timestamp(void);
-int64_t load_timestamp_sys_clock(void);
-
-int64_t get_timestamp_realtime (void);
+int64_t get_timestamp_boot     (void);
+int64_t get_timestamp_realtime (void);
 
 #endif