1 /**************************************************************************
2  *
3  * Copyright (C) 2019 Chromium.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included
13  * in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16  * OR 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
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23     **************************************************************************/
24 
25 #ifndef VIRGL_UTIL_H
26 #define VIRGL_UTIL_H
27 
28 #include <stdint.h>
29 #include <stdbool.h>
30 
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34 
35 #define TRACE_WITH_PERFETTO 1
36 #define TRACE_WITH_STDERR 2
37 #define TRACE_WITH_PERCETTO 3
38 
39 #define BIT(n)                   (UINT32_C(1) << (n))
40 
has_bit(uint32_t mask,uint32_t bit)41 static inline bool has_bit(uint32_t mask, uint32_t bit)
42 {
43     return !!(mask & bit);
44 }
45 
has_bits(uint32_t mask,uint32_t bits)46 static inline bool has_bits(uint32_t mask, uint32_t bits)
47 {
48     return !!((mask & bits) == bits);
49 }
50 
is_only_bit(uint32_t mask,uint32_t bit)51 static inline bool is_only_bit(uint32_t mask, uint32_t bit)
52 {
53     return (mask == bit);
54 }
55 
56 unsigned hash_func_u32(void *key);
57 
58 int compare_func(void *key1, void *key2);
59 
60 bool has_eventfd(void);
61 int create_eventfd(unsigned int initval);
62 int write_eventfd(int fd, uint64_t val);
63 void flush_eventfd(int fd);
64 
65 #ifdef ENABLE_TRACING
66 void trace_init(void);
67 
68 #define TRACE_INIT() trace_init()
69 #define TRACE_FUNC() TRACE_SCOPE(__func__)
70 
71 #if ENABLE_TRACING == TRACE_WITH_PERCETTO
72 
73 #include <percetto.h>
74 
75 #define VIRGL_PERCETTO_CATEGORIES(C, G) \
76   C(virgl, "virglrenderer") \
77   C(virgls, "virglrenderer detailed events", "slow")
78 
79 PERCETTO_CATEGORY_DECLARE(VIRGL_PERCETTO_CATEGORIES)
80 
81 #define TRACE_SCOPE(SCOPE) TRACE_EVENT(virgl, SCOPE)
82 /* Trace high frequency events (tracing may impact performance). */
83 #define TRACE_SCOPE_SLOW(SCOPE) TRACE_EVENT(virgls, SCOPE)
84 
85 #else
86 
87 const char *trace_begin(const char *scope);
88 void trace_end(const char **scope);
89 
90 #define TRACE_SCOPE(SCOPE) \
91    const char *trace_dummy __attribute__((cleanup (trace_end), unused)) = \
92    trace_begin(SCOPE)
93 
94 #define TRACE_SCOPE_SLOW(SCOPE) TRACE_SCOPE(SCOPE)
95 
96 #endif /* ENABLE_TRACING == TRACE_WITH_PERCETTO */
97 
98 #else
99 #define TRACE_INIT()
100 #define TRACE_FUNC()
101 #define TRACE_SCOPE(SCOPE)
102 #define TRACE_SCOPE_SLOW(SCOPE)
103 #endif /* ENABLE_TRACING */
104 
105 #endif /* VIRGL_UTIL_H */
106