Skip to content

Commit

Permalink
[lib] Add utilities for getting 64-bit time stamps. Contibutes to JB#…
Browse files Browse the repository at this point in the history
…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 a3e3626
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 193 deletions.
10 changes: 10 additions & 0 deletions .depend
Expand Up @@ -217,6 +217,7 @@ mce-hbtimer.o:\
datapipe.h\
libwakelock.h\
mce-hbtimer.h\
mce-lib.h\
mce-log.h\
mce.h\

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

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

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

Expand Down Expand Up @@ -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:\
Expand Down Expand Up @@ -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\
Expand All @@ -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\
Expand Down Expand Up @@ -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\
Expand All @@ -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\
Expand Down
28 changes: 4 additions & 24 deletions mce-hbtimer.c
Expand Up @@ -28,6 +28,7 @@

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

#ifdef ENABLE_WAKELOCKS
# include "libwakelock.h"
Expand Down Expand Up @@ -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);

/* ------------------------------------------------------------------------- *
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
41 changes: 3 additions & 38 deletions mce-io.c
Expand Up @@ -24,6 +24,7 @@

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

#ifdef ENABLE_WAKELOCKS
# include "libwakelock.h"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
48 changes: 48 additions & 0 deletions mce-lib.c
Expand Up @@ -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);
}
6 changes: 6 additions & 0 deletions mce-lib.h
Expand Up @@ -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 */
Expand Down Expand Up @@ -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_ */
11 changes: 2 additions & 9 deletions modules/cpu-keepalive.c
Expand Up @@ -26,6 +26,7 @@
*/

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

#ifdef ENABLE_WAKELOCKS
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a3e3626

Please sign in to comment.