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