Time-util: introduce usec to jiffies
authorSusant Sahani <ssahani@gmail.com>
Tue, 3 Nov 2015 16:28:42 +0000 (21:58 +0530)
committerSusant Sahani <ssahani@gmail.com>
Tue, 3 Nov 2015 16:28:42 +0000 (21:58 +0530)
src/basic/time-util.c
src/basic/time-util.h

index 9dc280e..e629d91 100644 (file)
@@ -1122,3 +1122,17 @@ time_t mktime_or_timegm(struct tm *tm, bool utc) {
 struct tm *localtime_or_gmtime_r(const time_t *t, struct tm *tm, bool utc) {
         return utc ? gmtime_r(t, tm) : localtime_r(t, tm);
 }
+
+unsigned long usec_to_jiffies(usec_t u) {
+        static thread_local unsigned long hz = 0;
+        long r;
+
+        if (hz == 0) {
+                r = sysconf(_SC_CLK_TCK);
+
+                assert(r > 0);
+                hz = (unsigned long) r;
+        }
+
+        return DIV_ROUND_UP(u , USEC_PER_SEC / hz);
+}
index 417376e..925bf18 100644 (file)
@@ -121,3 +121,5 @@ int get_timezone(char **timezone);
 
 time_t mktime_or_timegm(struct tm *tm, bool utc);
 struct tm *localtime_or_gmtime_r(const time_t *t, struct tm *tm, bool utc);
+
+unsigned long usec_to_jiffies(usec_t usec);