481b8ad256eacb63f578f28437038c150d83ebc1
[profile/ivi/xorg-x11-drv-evdev-multitouch.git] / include / vsync_debug_info.h
1 /*
2  * xserver-xorg-input-evdev-multitouch
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Sung-Jin Park <sj76.park@samsung.com>
7  *          Sangjin LEE <lsj119@samsung.com>
8  *
9  * Permission to use, copy, modify, distribute, and sell this software
10  * and its documentation for any purpose is hereby granted without
11  * fee, provided that the above copyright notice appear in all copies
12  * and that both that copyright notice and this permission notice
13  * appear in supporting documentation, and that the name of Red Hat
14  * not be used in advertising or publicity pertaining to distribution
15  * of the software without specific, written prior permission.  Red
16  * Hat makes no representations about the suitability of this software
17  * for any purpose.  It is provided "as is" without express or implied
18  * warranty.
19  *
20  * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
21  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
22  * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
23  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
24  * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
25  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
26  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27  */
28
29 #ifndef _SYNC_DEBUG_H
30 #define _SYNC_DEBUG_H
31
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include <sys/shm.h>
35 #include <sys/ipc.h>
36 #include <sys/ioctl.h>
37 #include <time.h>
38 #include <sys/time.h>
39 #include <stdarg.h>
40 #include <semaphore.h>
41
42 #ifdef __GNUC__
43 #define MAY_NOT_BE_USED __attribute__ ((unused))
44 #else
45 #define MAY_NOT_BE_USED
46 #endif 
47
48
49 #define SYNC_DEBUG_SHM_KEY      0xDEAD2012
50 #define SHM_MESG_SIZE           ( 1024 * 1024 )
51
52 #ifndef CLOCK_MONOTONIC
53 #define CLOCK_MONOTONIC 1
54 #endif
55
56 typedef struct {
57         int initialized;
58
59         sem_t sem;
60
61         int count;
62
63         int cur_pos;
64
65         int isLogging;
66
67         int isPrintEnabled;
68
69         unsigned long tvStart;
70
71         int _shmid;
72
73         char debug_msg[0];
74 } VSYNC_DEBUG_INFO;
75
76 static volatile VSYNC_DEBUG_INFO * _vsync_debug = NULL;
77
78 static int
79 _vsync_debug_init(void)
80 {
81         int shmid = 0;
82
83         struct shmid_ds ds;
84
85         shmid = shmget(SYNC_DEBUG_SHM_KEY, SHM_MESG_SIZE, IPC_CREAT | IPC_RMID);
86         if (shmid < 0)
87         {
88                 fprintf(stderr, "\tError : shmget size:%d\n", SHM_MESG_SIZE);
89                 goto bail;
90         }
91
92         if (shmctl(shmid, IPC_STAT, &ds) < 0)
93         {
94                 fprintf(stderr, "\tError : shmctl\n");
95                 goto bail;
96         }
97
98         if (ds.shm_segsz < SHM_MESG_SIZE)
99         {
100                 fprintf(stderr, "\tError : size check\n");
101                 goto bail;
102         }
103
104         _vsync_debug = shmat(shmid, NULL, 0);
105         if (_vsync_debug == (void *) -1)
106         {
107                 fprintf(stderr, "\tError : shmat\n");
108                 goto bail;
109         }
110
111         return shmid;
112
113 bail:
114         fprintf(stderr, "VSYNC_DEBUG.... Error\n");
115
116         if (_vsync_debug != NULL && _vsync_debug != (void *) -1)
117                 shmdt((void*)_vsync_debug);
118
119         return -1;
120 }
121
122 static unsigned long
123 _vsync_debug_get_time(void)
124 {
125         struct timespec tp;
126
127         if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
128                 return (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000L);
129
130         return 0;               
131 }
132
133 static void
134 _vsync_debug_lock(void)
135 {
136         if (sem_wait((sem_t *) &_vsync_debug->sem) < 0)
137                 fprintf(stderr, "sem_wait error\n");
138 }
139
140
141 static void
142 _vsync_debug_unlock(void)
143 {
144         if (sem_post((sem_t *) &_vsync_debug->sem) < 0)
145                 fprintf(stderr, "sem_post error\n");
146 }
147
148 static void
149 _vsync_debug_set_slot(int isSync, const char * format, va_list args)
150 {
151         unsigned long cur_time;
152
153         if (_vsync_debug == NULL)
154                 _vsync_debug_init();
155
156         if (!_vsync_debug->initialized)
157                 return;
158
159         if (! _vsync_debug->isLogging)
160                 return;
161
162         if (!_vsync_debug->isPrintEnabled)
163                 return;
164
165         if (_vsync_debug->cur_pos > SHM_MESG_SIZE - 1024)
166                 return;
167
168         cur_time = _vsync_debug_get_time();
169
170         _vsync_debug_lock();
171
172         if (isSync)
173         {
174                 _vsync_debug->count++;
175                 _vsync_debug->cur_pos += sprintf((char *) _vsync_debug->debug_msg + _vsync_debug->cur_pos, "\e[32m");
176
177         }
178         _vsync_debug->cur_pos += sprintf((char *) _vsync_debug->debug_msg + _vsync_debug->cur_pos, "%6ld ", cur_time);
179         _vsync_debug->cur_pos += vsprintf((char *) _vsync_debug->debug_msg + _vsync_debug->cur_pos, format, args);
180         _vsync_debug->debug_msg[_vsync_debug->cur_pos++] = '\n';
181         _vsync_debug->debug_msg[_vsync_debug->cur_pos] = '\0';
182
183         if (isSync)
184                 _vsync_debug->cur_pos += sprintf((char *) _vsync_debug->debug_msg + _vsync_debug->cur_pos, "\e[0m");
185
186         _vsync_debug_unlock();
187 }
188
189 static void MAY_NOT_BE_USED
190 vsync_debug_start(void)
191 {
192         if (_vsync_debug == NULL)
193                 _vsync_debug_init();
194
195         if (!_vsync_debug->initialized)
196                 return;
197
198         _vsync_debug->isLogging = 1;
199         _vsync_debug->tvStart = _vsync_debug_get_time();
200 }
201
202 static void MAY_NOT_BE_USED
203 vsync_debug_stop(void)
204 {
205         unsigned long tvStop;
206
207         if (_vsync_debug == NULL)
208                 _vsync_debug_init();
209
210         if (!_vsync_debug->initialized)
211                 return;
212
213         _vsync_debug->isLogging = 0;
214         tvStop = _vsync_debug_get_time();
215         
216         _vsync_debug_lock();
217         if (_vsync_debug->isPrintEnabled)
218         {
219                 //Print Debug
220                 fprintf(stderr, "VSYNC DEBUG: count:%d, start:%6ld, end:%6ld\n", _vsync_debug->count, _vsync_debug->tvStart, tvStop);
221                 fprintf(stderr, "%s\n", _vsync_debug->debug_msg);
222         }
223         _vsync_debug->cur_pos = 0;
224         _vsync_debug->debug_msg[0] = '\0';
225         _vsync_debug_unlock();
226 }
227
228 static void MAY_NOT_BE_USED
229 vsync_debug_set_sync(const char * format, ...)
230 {
231         va_list args;
232
233         va_start(args, format);
234
235         _vsync_debug_set_slot(1, format, args);
236
237         va_end(args);
238 }
239
240 static void MAY_NOT_BE_USED
241 vsync_debug_set_slot(const char * format, ...)
242 {
243         va_list args;
244
245         va_start(args, format);
246
247         _vsync_debug_set_slot(0, format, args);
248
249         va_end(args);
250 }
251
252 static void MAY_NOT_BE_USED
253 vsync_debug_set_enable(int bEnable)
254 {
255         if (_vsync_debug == NULL)
256                 _vsync_debug_init();
257
258         if (!_vsync_debug->initialized)
259                 return;
260
261         _vsync_debug->isPrintEnabled = bEnable;
262 }
263
264 static void MAY_NOT_BE_USED
265 vsync_debug_master_init(void)
266 {
267         int shmid = _vsync_debug_init();
268
269         _vsync_debug->_shmid = shmid;
270
271         if (sem_init((sem_t *) &_vsync_debug->sem, 1, 1) < 0)
272         {
273                 fprintf(stderr, "\tError : sem_init\n");
274                 goto bail;
275         }
276
277         _vsync_debug->initialized = 1;
278
279         return;
280 bail:
281         fprintf(stderr, "VSYNC_DEBUG.... master Error\n");
282 }
283
284 static void MAY_NOT_BE_USED
285 vsync_debug_master_close(void)
286 {
287         if (_vsync_debug == NULL)
288                 return;
289
290         if (shmctl(_vsync_debug->_shmid, IPC_RMID, NULL) < 0)
291         {
292                 fprintf(stderr, "\tError : shmctl(IPC_RMID)\n");
293         }
294         if (shmdt((void*)_vsync_debug) < 0)
295         {
296                 fprintf(stderr, "\tError : shmdt\n");
297         }
298
299         _vsync_debug = NULL;
300 }
301
302 #endif /* _SYNC_DEBUG_H */
303