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 <stdarg.h>
29 #include <stdbool.h>
30 #include <stdint.h>
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include "virglrenderer.h"
37
38 #define TRACE_WITH_PERFETTO 1
39 #define TRACE_WITH_STDERR 2
40 #define TRACE_WITH_PERCETTO 3
41
42 #define BIT(n) (UINT32_C(1) << (n))
43
has_bit(uint32_t mask,uint32_t bit)44 static inline bool has_bit(uint32_t mask, uint32_t bit)
45 {
46 return !!(mask & bit);
47 }
48
has_bits(uint32_t mask,uint32_t bits)49 static inline bool has_bits(uint32_t mask, uint32_t bits)
50 {
51 return !!((mask & bits) == bits);
52 }
53
is_only_bit(uint32_t mask,uint32_t bit)54 static inline bool is_only_bit(uint32_t mask, uint32_t bit)
55 {
56 return (mask == bit);
57 }
58
59 unsigned hash_func_u32(void *key);
60
61 int compare_func(void *key1, void *key2);
62
63 bool has_eventfd(void);
64 int create_eventfd(unsigned int initval);
65 int write_eventfd(int fd, uint64_t val);
66 void flush_eventfd(int fd);
67
68 virgl_debug_callback_type virgl_log_set_logger(virgl_debug_callback_type logger);
69 void virgl_logv(const char *fmt, va_list va);
70
virgl_log(const char * fmt,...)71 static inline void virgl_log(const char *fmt, ...)
72 {
73 va_list va;
74 va_start(va, fmt);
75 virgl_logv(fmt, va);
76 va_end(va);
77 }
78
79 #ifdef ENABLE_TRACING
80 void trace_init(void);
81
82 #define TRACE_INIT() trace_init()
83 #define TRACE_FUNC() TRACE_SCOPE(__func__)
84
85 #if ENABLE_TRACING == TRACE_WITH_PERCETTO
86
87 #include <percetto.h>
88
89 #define VIRGL_PERCETTO_CATEGORIES(C, G) \
90 C(virgl, "virglrenderer") \
91 C(virgls, "virglrenderer detailed events", "slow")
92
93 PERCETTO_CATEGORY_DECLARE(VIRGL_PERCETTO_CATEGORIES)
94
95 #define TRACE_SCOPE(SCOPE) TRACE_EVENT(virgl, SCOPE)
96 /* Trace high frequency events (tracing may impact performance). */
97 #define TRACE_SCOPE_SLOW(SCOPE) TRACE_EVENT(virgls, SCOPE)
98
99 #define TRACE_SCOPE_BEGIN(SCOPE) TRACE_EVENT_BEGIN(virgl, SCOPE)
100 #define TRACE_SCOPE_END(SCOPE) do { TRACE_EVENT_END(virgl); (void)SCOPE; } while (0)
101
102 #else
103
104 const char *trace_begin(const char *scope);
105 void trace_end(const char **scope);
106
107 #define TRACE_SCOPE(SCOPE) \
108 const char *trace_dummy __attribute__((cleanup (trace_end), unused)) = \
109 trace_begin(SCOPE)
110
111 #define TRACE_SCOPE_SLOW(SCOPE) TRACE_SCOPE(SCOPE)
112
113 #define TRACE_SCOPE_BEGIN(SCOPE) trace_begin(SCOPE);
114 #define TRACE_SCOPE_END(SCOPE) trace_end(&SCOPE);
115
116 #endif /* ENABLE_TRACING == TRACE_WITH_PERCETTO */
117
118 #else
119 #define TRACE_INIT()
120 #define TRACE_FUNC()
121 #define TRACE_SCOPE(SCOPE)
122 #define TRACE_SCOPE_SLOW(SCOPE)
123 #define TRACE_SCOPE_BEGIN(SCOPE, VAR)
124 #define TRACE_SCOPE_END(VAR)
125 #endif /* ENABLE_TRACING */
126
127 #endif /* VIRGL_UTIL_H */
128