Skip to content
Permalink
Browse files
[lib] Add utilities for getting 64-bit time stamps. Contibutes to JB#…
…31310

The mce codebase is littered with very similar functions for turning
clock_gettime() results into 64-bit integer time stamps.

Add common utility functions for obtaining boot, real and monotonic time
stamps in to mce-lib module. Use these common utility functions instead
of locally defined versions.
  • Loading branch information
spiiroin committed Sep 22, 2015
1 parent 04b5056 commit a3e3626ea32a387d311467fa694922fa783e42cf
Showing with 107 additions and 193 deletions.
  1. +10 −0 .depend
  2. +4 −24 mce-hbtimer.c
  3. +3 −38 mce-io.c
  4. +48 −0 mce-lib.c
  5. +6 −0 mce-lib.h
  6. +2 −9 modules/cpu-keepalive.c
  7. +10 −27 modules/display.c
  8. +2 −20 powerkey.c
  9. +22 −75 tklock.c
10 .depend
@@ -217,6 +217,7 @@ mce-hbtimer.o:\
datapipe.h\
libwakelock.h\
mce-hbtimer.h\
mce-lib.h\
mce-log.h\
mce.h\

@@ -225,6 +226,7 @@ mce-hbtimer.pic.o:\
datapipe.h\
libwakelock.h\
mce-hbtimer.h\
mce-lib.h\
mce-log.h\
mce.h\

@@ -251,6 +253,7 @@ mce-io.o:\
datapipe.h\
libwakelock.h\
mce-io.h\
mce-lib.h\
mce-log.h\
mce.h\

@@ -259,6 +262,7 @@ mce-io.pic.o:\
datapipe.h\
libwakelock.h\
mce-io.h\
mce-lib.h\
mce-log.h\
mce.h\

@@ -511,13 +515,15 @@ modules/cpu-keepalive.o:\
builtin-gconf.h\
libwakelock.h\
mce-dbus.h\
mce-lib.h\
mce-log.h\

modules/cpu-keepalive.pic.o:\
modules/cpu-keepalive.c\
builtin-gconf.h\
libwakelock.h\
mce-dbus.h\
mce-lib.h\
mce-log.h\

modules/display.o:\
@@ -835,6 +841,7 @@ powerkey.o:\
mce-dbus.h\
mce-dsme.h\
mce-gconf.h\
mce-lib.h\
mce-log.h\
mce.h\
powerkey.h\
@@ -848,6 +855,7 @@ powerkey.pic.o:\
mce-dbus.h\
mce-dsme.h\
mce-gconf.h\
mce-lib.h\
mce-log.h\
mce.h\
powerkey.h\
@@ -1071,6 +1079,7 @@ tklock.o:\
mce-gconf.h\
mce-hbtimer.h\
mce-io.h\
mce-lib.h\
mce-log.h\
mce.h\
modules/display.h\
@@ -1090,6 +1099,7 @@ tklock.pic.o:\
mce-gconf.h\
mce-hbtimer.h\
mce-io.h\
mce-lib.h\
mce-log.h\
mce.h\
modules/display.h\
@@ -28,6 +28,7 @@

#include "mce.h"
#include "mce-log.h"
#include "mce-lib.h"

#ifdef ENABLE_WAKELOCKS
# include "libwakelock.h"
@@ -95,7 +96,6 @@ static void mce_hbtimer_set_trigger (mce_hbtimer_t *self, int64_t trigger
/** Monotonic tick value used to signify "not-set" */
#define NO_TICK INT64_MAX

static int64_t mht_get_monotick (void);
static guint mht_add_iowatch (int fd, bool close_on_unref, GIOCondition cnd, GIOFunc io_cb, gpointer aptr);

/* ------------------------------------------------------------------------- *
@@ -201,26 +201,6 @@ void mce_hbtimer_quit (void);
* GENERIC_UTILITIES
* ========================================================================= */

/** Get CLOCK_BOOTTIME time stamp in milliseconds
*
* @return current boottime in ms resolution
*/
static int64_t
mht_get_monotick(void)
{
int64_t res = 0;

struct timespec ts;

if( clock_gettime(CLOCK_BOOTTIME, &ts) == 0 ) {
res = ts.tv_sec;
res *= 1000;
res += ts.tv_nsec / 1000000;
}

return res;
}

/** Helper for creating I/O watch for file descriptor
*/
static guint
@@ -415,7 +395,7 @@ mce_hbtimer_start(mce_hbtimer_t *self)

mce_log(LL_DEBUG, "start %s %d", mce_hbtimer_get_name(self),
self->hbt_period);
int64_t now = mht_get_monotick();
int64_t now = mce_lib_get_boot_tick();
int64_t trigger = now + self->hbt_period;
mce_hbtimer_set_trigger(self, trigger);

@@ -557,7 +537,7 @@ mht_queue_schedule_wakeups(void)
if( compact )
mht_queue_garbage_collect();

int64_t now = mht_get_monotick();
int64_t now = mce_lib_get_boot_tick();

if( trigger < now )
trigger = now;
@@ -591,7 +571,7 @@ mht_queue_dispatch_timers(void)
wakelock_lock("mce_hbtimer_dispatch", -1);
#endif

int64_t now = mht_get_monotick();
int64_t now = mce_lib_get_boot_tick();

for( GSList *item = mht_queue_timer_list; item; item = item->next ) {
mce_hbtimer_t *timer = item->data;
@@ -24,6 +24,7 @@

#include "mce.h"
#include "mce-log.h"
#include "mce-lib.h"

#ifdef ENABLE_WAKELOCKS
# include "libwakelock.h"
@@ -91,8 +92,6 @@ static GSList *file_monitors = NULL;

// SUSPEND_DETECTION

static int64_t io_get_boot_tick (void);
static int64_t io_get_mono_tick (void);
static void io_detect_resume (void);

// GLIB_IO_HELPERS
@@ -150,48 +149,14 @@ gboolean mce_io_update_file_atomic (const char *path, const
* SUSPEND_DETECTION
* ========================================================================= */

/** Get CLOCK_BOOTTIME time stamp in milliseconds
*/
static int64_t io_get_boot_tick(void)
{
int64_t res = 0;

struct timespec ts;

if( clock_gettime(CLOCK_BOOTTIME, &ts) == 0 ) {
res = ts.tv_sec;
res *= 1000;
res += ts.tv_nsec / 1000000;
}

return res;
}

/** Get CLOCK_MONOTONIC time stamp in milliseconds
*/
static int64_t io_get_mono_tick(void)
{
int64_t res = 0;

struct timespec ts;

if( clock_gettime(CLOCK_MONOTONIC, &ts) == 0 ) {
res = ts.tv_sec;
res *= 1000;
res += ts.tv_nsec / 1000000;
}

return res;
}

/** Detect suspend/resume cycle from CLOCK_MONOTONIC vs CLOCK_BOOTTIME
*/
static void io_detect_resume(void)
{
static int64_t prev = 0;

int64_t boot = io_get_boot_tick();
int64_t mono = io_get_mono_tick();
int64_t boot = mce_lib_get_boot_tick();
int64_t mono = mce_lib_get_mono_tick();
int64_t diff = boot - mono;

int64_t skip = diff - prev;
@@ -358,3 +358,51 @@ gboolean strmemcmp(guint8 *mem, const gchar *str, gulong len)
EXIT:
return result;
}

/** Get clock id specific time stamp in milliseconds
*
* @param id Clock id such as CLOCK_REALTIME or CLOCK_MONOTONIC
*
* @return 64-bit timestamp
*/
static int64_t mce_lib_get_tick(clockid_t id)
{
int64_t res = 0;

struct timespec ts;

if( clock_gettime(id, &ts) == 0 ) {
res = ts.tv_sec;
res *= 1000;
res += ts.tv_nsec / 1000000;
}

return res;
}

/** Get CLOCK_BOOTTIME time stamp in milliseconds
*
* @return 64-bit timestamp
*/
int64_t mce_lib_get_boot_tick(void)
{
return mce_lib_get_tick(CLOCK_BOOTTIME);
}

/** Get CLOCK_MONOTONIC time stamp in milliseconds
*
* @return 64-bit timestamp
*/
int64_t mce_lib_get_mono_tick(void)
{
return mce_lib_get_tick(CLOCK_MONOTONIC);
}

/** Get CLOCK_REALTIME time stamp in milliseconds
*
* @return 64-bit timestamp
*/
int64_t mce_lib_get_real_tick(void)
{
return mce_lib_get_tick(CLOCK_REALTIME);
}
@@ -22,6 +22,8 @@
#ifndef _MCE_LIB_H_
#define _MCE_LIB_H_

#include <stdint.h>

#include <glib.h>

/** Find the number of bits of a type */
@@ -55,4 +57,8 @@ gchar *strstr_delim(const gchar *const haystack, const char *needle,
const char *const delimiter);
gboolean strmemcmp(guint8 *mem, const gchar *str, gulong len);

int64_t mce_lib_get_boot_tick(void);
int64_t mce_lib_get_mono_tick(void);
int64_t mce_lib_get_real_tick(void);

#endif /* _MCE_LIB_H_ */
@@ -26,6 +26,7 @@
*/

#include "../mce-log.h"
#include "../mce-lib.h"
#include "../mce-dbus.h"

#ifdef ENABLE_WAKELOCKS
@@ -285,15 +286,7 @@ static
tick_t
cka_tick_get_current(void)
{
tick_t ms;
struct timespec ts;
clock_gettime(CLOCK_BOOTTIME, &ts);

ms = ts.tv_sec;
ms *= 1000;
ms += ts.tv_nsec / 1000000;

return ms;
return mce_lib_get_boot_tick();
}

/** Helper for calculating timeout values from ms base + seconds offset

0 comments on commit a3e3626

Please sign in to comment.