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 uint64_t
tv2us(const struct timeval * tv)79 tv2us(const struct timeval *tv)
80 {
81 return s2us(tv->tv_sec) + tv->tv_usec;
82 }
83
84 static inline struct timeval
us2tv(uint64_t time)85 us2tv(uint64_t time)
86 {
87 struct timeval tv;
88
89 tv.tv_sec = time / ms2us(1000);
90 tv.tv_usec = time % ms2us(1000);
91
92 return tv;
93 }
94
95 struct human_time {
96 unsigned int value;
97 const char *unit;
98 };
99
100 /**
101 * Converts a time delta in µs to a human-readable time like "2h" or "4d"
102 */
103 static inline struct human_time
to_human_time(uint64_t us)104 to_human_time(uint64_t us)
105 {
106 struct human_time t;
107 struct c {
108 const char *unit;
109 unsigned int change_from_previous;
110 uint64_t limit;
111 } conversion[] = {
112 {"us", 1, 5000},
113 {"ms", 1000, 5000},
114 {"s", 1000, 120},
115 {"min", 60, 120},
116 {"h", 60, 48},
117 {"d", 24, ~0},
118 };
119 struct c *c;
120 uint64_t value = us;
121
122 ARRAY_FOR_EACH(conversion, c) {
123 value = value/c->change_from_previous;
124 if (value < c->limit) {
125 t.unit = c->unit;
126 t.value = value;
127 return t;
128 }
129 }
130
131 assert(!"We should never get here");
132 }
133