1 /*
2 * Copyright © 2013-2019 Red Hat, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #pragma once
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <time.h>
30 #include <stdint.h>
31 #include <unistd.h>
32 #include <linux/input.h>
33
34 #include "util-macros.h"
35
36 static inline void
msleep(unsigned int ms)37 msleep(unsigned int ms)
38 {
39 usleep(ms * 1000);
40 }
41
42 static inline uint64_t
us(uint64_t us)43 us(uint64_t us)
44 {
45 return us;
46 }
47
48 static inline uint64_t
ns2us(uint64_t ns)49 ns2us(uint64_t ns)
50 {
51 return us(ns / 1000);
52 }
53
54 static inline uint64_t
ms2us(uint64_t ms)55 ms2us(uint64_t ms)
56 {
57 return us(ms * 1000);
58 }
59
60 static inline uint64_t
s2us(uint64_t s)61 s2us(uint64_t s)
62 {
63 return ms2us(s * 1000);
64 }
65
66 static inline uint64_t
h2us(uint64_t h)67 h2us(uint64_t h)
68 {
69 return s2us(h * 3600);
70 }
71
72 static inline uint32_t
us2ms(uint64_t us)73 us2ms(uint64_t us)
74 {
75 return (uint32_t)(us / 1000);
76 }
77
78 static inline double
us2ms_f(uint64_t us)79 us2ms_f(uint64_t us)
80 {
81 return (double)us / 1000.0;
82 }
83
84 static inline uint64_t
tv2us(const struct timeval * tv)85 tv2us(const struct timeval *tv)
86 {
87 return s2us(tv->tv_sec) + tv->tv_usec;
88 }
89
90 static inline struct timeval
us2tv(uint64_t time)91 us2tv(uint64_t time)
92 {
93 struct timeval tv;
94
95 tv.tv_sec = time / ms2us(1000);
96 tv.tv_usec = time % ms2us(1000);
97
98 return tv;
99 }
100
101 struct human_time {
102 unsigned int value;
103 const char *unit;
104 };
105
106 /**
107 * Converts a time delta in µs to a human-readable time like "2h" or "4d"
108 */
109 static inline struct human_time
to_human_time(uint64_t us)110 to_human_time(uint64_t us)
111 {
112 struct human_time t;
113 struct c {
114 const char *unit;
115 unsigned int change_from_previous;
116 uint64_t limit;
117 } conversion[] = {
118 {"us", 1, 5000},
119 {"ms", 1000, 5000},
120 {"s", 1000, 120},
121 {"min", 60, 120},
122 {"h", 60, 48},
123 {"d", 24, ~0},
124 };
125 uint64_t value = us;
126
127 ARRAY_FOR_EACH(conversion, c) {
128 value = value/c->change_from_previous;
129 if (value < c->limit) {
130 t.unit = c->unit;
131 t.value = value;
132 return t;
133 }
134 }
135
136 assert(!"We should never get here");
137 }
138