4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
8 * Jaewon Lim <jaewon81.lim@samsung.com>
9 * Woojin Jung <woojin2.jung@samsung.com>
10 * Juyoung Kim <j0.kim@samsung.com>
12 * This library is free software; you can redistribute it and/or modify it under
13 * the terms of the GNU Lesser General Public License as published by the
14 * Free Software Foundation; either version 2.1 of the License, or (at your option)
17 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
18 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
20 * License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this library; if not, write to the Free Software Foundation, Inc., 51
24 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <signal.h> // for signal
33 #include <unistd.h> // for ualarm, sleep
34 #include <sys/timerfd.h> // for timerfd
36 #include <errno.h> // for errno
37 #include <string.h> // for memset
38 #include <stdint.h> // for uint64_t
42 #include "probeinfo.h"
48 #define ERR_THREAD_CREATE_FAIL -2001 // thread creation fail
50 #define MAX_TITLE_LENGTH 16
51 #define MAX_CHART_HANDLE 10 // cannot be exceeded 10
52 #define MAX_SERIES_PER_CHART 4 // cannot be exceeded 10
53 // chart handle is from 1 to 9
54 // series handle is from 11 to 99
55 // series handle 32 means that the series is 2nd series in 3rd chart.
56 // zero value handle means error
58 // =====================================================================
60 // =====================================================================
62 typedef struct _chart_interval_callback
64 da_handle chart_handle;
65 da_handle series_handle;
66 da_user_data_2_chart_data callback;
68 struct _chart_interval_callback* next;
69 } chart_interval_callback;
74 pthread_t thread_handle;
75 chart_interval_callback* callback_list;
76 pthread_mutex_t list_mutex;
81 int chart_handle_index;
82 int series_handle_index[MAX_CHART_HANDLE];
83 int interval_for_series[MAX_CHART_HANDLE][MAX_SERIES_PER_CHART];
84 interval_manager interval_10ms;
85 interval_manager interval_100ms;
86 interval_manager interval_1s;
87 } chart_handle_maintainer;
89 chart_handle_maintainer chm;
91 __thread pid_t cur_thread = -1;
94 // =====================================================================
95 // internal macro definition
96 // =====================================================================
98 #define DECLARE_CHART_VARIABLE \
99 da_handle __attribute__((unused)) ret = 0; \
100 probeInfo_t probeInfo;
102 // =====================================================================
103 // timer thread routine for callback
104 // =====================================================================
106 void* _chart_timerThread(void* data)
108 DECLARE_CHART_VARIABLE;
112 chart_interval_callback* cur;
114 sigset_t profsigmask;
115 interval_manager* pmanager = (interval_manager*)data;
119 sigemptyset(&profsigmask);
120 sigaddset(&profsigmask, SIGPROF);
121 pthread_sigmask(SIG_BLOCK, &profsigmask, NULL);
123 while((readsize = read(pmanager->timerfd, &exp, sizeof(uint64_t))) > 0)
125 pthread_mutex_lock(&pmanager->list_mutex);
127 cur = pmanager->callback_list;
130 value = cur->callback(cur->user_data);
132 setProbePoint(&probeInfo);
135 PACK_COMMON_BEGIN(MSG_PROBE_CUSTOM, LC_CUSTOM, "", 0);
136 PACK_COMMON_END(0, 0, 2);
137 PACK_CUSTOM(cur->series_handle, 0, "", 0, value);
143 pthread_mutex_unlock(&pmanager->list_mutex);
153 static int start_callback_thread(chart_interval interval)
156 pthread_t* thread_handle = NULL;
157 interval_manager* pman = NULL;
158 struct itimerspec timevalue;
160 if(interval == CHART_INTERVAL_10MSEC)
162 timerfd = &(chm.interval_10ms.timerfd);
163 thread_handle = &(chm.interval_10ms.thread_handle);
164 pman = &(chm.interval_10ms);
165 timevalue.it_value.tv_sec = 0;
166 timevalue.it_value.tv_nsec = 10000000L;
167 timevalue.it_interval.tv_sec = 0;
168 timevalue.it_interval.tv_nsec = 10000000L;
170 else if(interval == CHART_INTERVAL_100MSEC)
172 timerfd = &(chm.interval_100ms.timerfd);
173 thread_handle = &(chm.interval_100ms.thread_handle);
174 pman = &(chm.interval_100ms);
175 timevalue.it_value.tv_sec = 0;
176 timevalue.it_value.tv_nsec = 100000000L;
177 timevalue.it_interval.tv_sec = 0;
178 timevalue.it_interval.tv_nsec = 100000000L;
180 else if(interval == CHART_INTERVAL_1SEC)
182 timerfd = &(chm.interval_1s.timerfd);
183 thread_handle = &(chm.interval_1s.thread_handle);
184 pman = &(chm.interval_1s);
185 timevalue.it_value.tv_sec = 1;
186 timevalue.it_value.tv_nsec = 0;
187 timevalue.it_interval.tv_sec = 1;
188 timevalue.it_interval.tv_nsec = 0;
192 return ERR_WRONG_PARAMETER;
195 if(*timerfd != -1 || *thread_handle != -1)
196 return 0; // already thread exist
198 *timerfd = timerfd_create(CLOCK_REALTIME, 0);
202 if(timerfd_settime(*timerfd, 0, &timevalue, NULL) == -1)
205 if(pthread_create(thread_handle, NULL, _chart_timerThread, pman) < 0)
206 return ERR_THREAD_CREATE_FAIL;
212 // =====================================================================
213 // internal utility function
214 // =====================================================================
216 static void add_to_callback_list(chart_interval interval, da_handle charthandle, da_handle series_handle,
217 da_user_data_2_chart_data callback, void* user_data)
219 chart_interval_callback* newelem;
221 newelem = (chart_interval_callback*)malloc(sizeof(chart_interval_callback));
222 newelem->chart_handle = charthandle;
223 newelem->series_handle = series_handle;
224 newelem->callback = callback;
225 newelem->user_data = user_data;
227 chm.interval_for_series[charthandle][series_handle % 10] = interval;
231 case CHART_INTERVAL_10MSEC:
232 pthread_mutex_lock(&chm.interval_10ms.list_mutex);
233 newelem->next = chm.interval_10ms.callback_list;
234 chm.interval_10ms.callback_list = newelem;
235 pthread_mutex_unlock(&chm.interval_10ms.list_mutex);
236 __sync_add_and_fetch(&(gTraceInfo.custom_chart_callback_count), 1);
238 case CHART_INTERVAL_100MSEC:
239 pthread_mutex_lock(&chm.interval_100ms.list_mutex);
240 newelem->next = chm.interval_100ms.callback_list;
241 chm.interval_100ms.callback_list = newelem;
242 pthread_mutex_unlock(&chm.interval_100ms.list_mutex);
243 __sync_add_and_fetch(&(gTraceInfo.custom_chart_callback_count), 1);
245 case CHART_INTERVAL_1SEC:
246 pthread_mutex_lock(&chm.interval_1s.list_mutex);
247 newelem->next = chm.interval_1s.callback_list;
248 chm.interval_1s.callback_list = newelem;
249 pthread_mutex_unlock(&chm.interval_1s.list_mutex);
250 __sync_add_and_fetch(&(gTraceInfo.custom_chart_callback_count), 1);
258 static void remove_all_callback_list()
260 chart_interval_callback* cur;
262 pthread_mutex_lock(&chm.interval_10ms.list_mutex);
263 while(chm.interval_10ms.callback_list != NULL)
265 cur = chm.interval_10ms.callback_list;
266 chm.interval_10ms.callback_list = cur->next;
269 pthread_mutex_unlock(&chm.interval_10ms.list_mutex);
271 pthread_mutex_lock(&chm.interval_100ms.list_mutex);
272 while(chm.interval_100ms.callback_list != NULL)
274 cur = chm.interval_100ms.callback_list;
275 chm.interval_100ms.callback_list = cur->next;
278 pthread_mutex_unlock(&chm.interval_100ms.list_mutex);
280 pthread_mutex_lock(&chm.interval_1s.list_mutex);
281 while(chm.interval_1s.callback_list != NULL)
283 cur = chm.interval_1s.callback_list;
284 chm.interval_1s.callback_list = cur->next;
287 pthread_mutex_unlock(&chm.interval_1s.list_mutex);
289 memset(&chm.interval_for_series, 0, sizeof(chm.interval_for_series));
290 __sync_and_and_fetch(&(gTraceInfo.custom_chart_callback_count), 0);
293 static void remove_from_callback_list(da_handle charthandle, da_handle series_handle)
295 chart_interval_callback *prev, *cur;
296 chart_interval interval;
298 interval = chm.interval_for_series[charthandle][series_handle % 10];
299 chm.interval_for_series[charthandle][series_handle % 10] = 0;
303 case CHART_INTERVAL_10MSEC:
304 pthread_mutex_lock(&chm.interval_10ms.list_mutex);
307 cur = chm.interval_10ms.callback_list;
310 if(cur->chart_handle == charthandle && cur->series_handle == series_handle)
313 prev->next = cur->next;
315 chm.interval_10ms.callback_list = cur->next;
317 __sync_sub_and_fetch(&(gTraceInfo.custom_chart_callback_count), 1);
326 pthread_mutex_unlock(&chm.interval_10ms.list_mutex);
328 case CHART_INTERVAL_100MSEC:
329 pthread_mutex_lock(&chm.interval_100ms.list_mutex);
332 cur = chm.interval_100ms.callback_list;
335 if(cur->chart_handle == charthandle && cur->series_handle == series_handle)
338 prev->next = cur->next;
340 chm.interval_100ms.callback_list = cur->next;
342 __sync_sub_and_fetch(&(gTraceInfo.custom_chart_callback_count), 1);
351 pthread_mutex_unlock(&chm.interval_100ms.list_mutex);
353 case CHART_INTERVAL_1SEC:
354 pthread_mutex_lock(&chm.interval_1s.list_mutex);
357 cur = chm.interval_1s.callback_list;
360 if(cur->chart_handle == charthandle && cur->series_handle == series_handle)
363 prev->next = cur->next;
365 chm.interval_1s.callback_list = cur->next;
367 __sync_sub_and_fetch(&(gTraceInfo.custom_chart_callback_count), 1);
376 pthread_mutex_unlock(&chm.interval_1s.list_mutex);
384 // =====================================================================
385 // constructor and destructor functions
386 // =====================================================================
388 void __attribute__((constructor)) _init_lib()
392 memset(&chm, 0, sizeof(chm));
393 chm.interval_10ms.timerfd = -1;
394 chm.interval_100ms.timerfd = -1;
395 chm.interval_1s.timerfd = -1;
396 chm.interval_10ms.thread_handle = -1;
397 chm.interval_100ms.thread_handle = -1;
398 chm.interval_1s.thread_handle = -1;
399 pthread_mutex_init(&chm.interval_10ms.list_mutex, NULL);
400 pthread_mutex_init(&chm.interval_100ms.list_mutex, NULL);
401 pthread_mutex_init(&chm.interval_1s.list_mutex, NULL);
406 void __attribute__((destructor)) _fini_lib()
410 remove_all_callback_list();
411 if(chm.interval_10ms.timerfd != -1)
412 close(chm.interval_10ms.timerfd);
413 if(chm.interval_100ms.timerfd != -1)
414 close(chm.interval_100ms.timerfd);
415 if(chm.interval_1s.timerfd != -1)
416 close(chm.interval_1s.timerfd);
418 if(chm.interval_10ms.thread_handle != -1)
420 pthread_join(chm.interval_10ms.thread_handle, NULL);
422 if(chm.interval_100ms.thread_handle != -1)
424 pthread_join(chm.interval_100ms.thread_handle, NULL);
426 if(chm.interval_1s.thread_handle != -1)
428 pthread_join(chm.interval_1s.thread_handle, NULL);
435 // =====================================================================
437 // =====================================================================
439 void da_mark(chart_color color, char* mark_text)
441 DECLARE_CHART_VARIABLE;
445 setProbePoint(&probeInfo);
448 PACK_COMMON_BEGIN(MSG_PROBE_CUSTOM, LC_CUSTOM, "dp", color, mark_text);
449 PACK_COMMON_END(0, 0, 2);
450 PACK_CUSTOM(0, 0, mark_text, color, 0.0f);
456 da_handle da_create_chart(char* chart_name)
458 DECLARE_CHART_VARIABLE;
460 // check if there is available chart handle slot
461 if(chm.chart_handle_index + 1 >= MAX_CHART_HANDLE)
462 return ERR_MAX_CHART_NUMBER;
464 // check if chart_name is null
465 if(chart_name == NULL)
466 return ERR_WRONG_PARAMETER;
469 ret = ++(chm.chart_handle_index);
471 setProbePoint(&probeInfo);
474 PACK_COMMON_BEGIN(MSG_PROBE_CUSTOM, LC_CUSTOM, "p", chart_name);
475 PACK_COMMON_END(ret, 0, 2);
476 PACK_CUSTOM(0, 0, chart_name, 0, 0.0f);
484 da_handle da_create_series(da_handle charthandle, char* seriesname,
485 series_type type, chart_color color)
487 DECLARE_CHART_VARIABLE;
489 // check if charthandle is valid handle or not
490 if(charthandle <= 0 || charthandle > chm.chart_handle_index)
491 return ERR_WRONG_HANDLE;
493 // chech if extra parameter is valid
494 if(seriesname == NULL)
495 return ERR_WRONG_PARAMETER;
497 // check if there is available series spot
498 if(chm.series_handle_index[(int)charthandle] + 1 >= MAX_SERIES_PER_CHART)
499 return ERR_MAX_CHART_NUMBER;
502 ret = ++(chm.series_handle_index[charthandle]);
503 ret += (10 * charthandle);
505 setProbePoint(&probeInfo);
508 PACK_COMMON_BEGIN(MSG_PROBE_CUSTOM, LC_CUSTOM, "dpdd", charthandle, seriesname, type, color);
509 PACK_COMMON_END(ret, 0, 2);
510 PACK_CUSTOM(charthandle, type, seriesname, color, 0.0f);
518 da_handle da_create_default_series(da_handle charthandle, char* seriesname)
520 return da_create_series(charthandle, seriesname,
521 CHART_TYPE_AUTO, CHART_COLOR_AUTO);
524 int da_set_callback(da_handle series_handle, da_user_data_2_chart_data callback,
525 void* data_addr, chart_interval interval)
528 cindex = (int)(series_handle / 10);
529 sindex = series_handle % 10;
531 // check series handle
532 if(cindex <= 0 || cindex > chm.chart_handle_index)
533 return ERR_WRONG_HANDLE;
535 if(sindex > chm.series_handle_index[(int)cindex])
536 return ERR_WRONG_HANDLE;
538 // check rest parameters
539 if(interval == CHART_NO_CYCLE && callback != NULL)
540 return ERR_WRONG_PARAMETER;
544 // remove previously registered callback
545 remove_from_callback_list(cindex, series_handle);
547 // register new callback
550 add_to_callback_list(interval, cindex, series_handle, callback, data_addr);
551 start_callback_thread(interval);
558 void da_log(da_handle series_handle, float uservalue)
560 DECLARE_CHART_VARIABLE;
562 // chech if series handle is valid
564 cindex = (int)(series_handle / 10);
565 sindex = series_handle % 10;
567 if(cindex <= 0 || cindex > chm.chart_handle_index)
570 if(sindex > chm.series_handle_index[(int)cindex])
575 setProbePoint(&probeInfo);
578 PACK_COMMON_BEGIN(MSG_PROBE_CUSTOM, LC_CUSTOM, "dw", series_handle, uservalue);
579 PACK_COMMON_END(0, 0, 2);
580 PACK_CUSTOM(series_handle, 0, "", 0, uservalue);