2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
25 #include <time.h> /* time, ctime */
26 #include <sys/time.h> /* gettimeofday */
27 #include <sys/utsname.h>
28 #include <sys/resource.h>
35 static void __free_cps(void);
36 static int __get_cp_index(char *name);
38 static void __free_accums(void);
39 static int __get_accum_index(char *name);
42 cam_ta_checkpoint **cam_g_cps = NULL;
43 static int cam_g_cp_index = 0;
45 cam_ta_accum_item **cam_g_accums = NULL;
46 static int cam_g_accum_index = 0;
47 static int cam_g_accum_longest_name = 0;
48 static unsigned long cam_g_accum_first_time = 0xFFFFFFFF; /* jmlee */
49 static int cam_g_enable = 0;
57 (cam_ta_checkpoint **) malloc(CAM_TA_MAX_CHECKPOINT *
58 sizeof(cam_ta_checkpoint *));
63 (cam_ta_accum_item **) malloc(CAM_TA_MAX_CHECKPOINT *
64 sizeof(cam_ta_accum_item *));
68 cam_g_accum_first_time = 0xFFFFFFFF;
73 int cam_ta_release(void)
81 cam_g_accum_first_time = 0xFFFFFFFF;
86 void cam_ta_set_enable(int enable)
88 printf("CAM_TA : setting enable to %d\n", enable);
89 cam_g_enable = enable;
92 int cam_ta_get_numof_checkpoints()
94 return cam_g_cp_index;
97 char *cam_ta_fmt(const char *fmt, ...)
99 static char ta_buf[512];
102 memset(ta_buf, '\0', 512);
105 vsnprintf(ta_buf, 512, fmt, args);
111 int cam_ta_add_checkpoint(char *name, int show, char *filename, int line)
113 cam_ta_checkpoint *cp = NULL;
122 if (cam_g_cp_index == CAM_TA_MAX_CHECKPOINT)
128 if (strlen(name) == 0)
131 cp = (cam_ta_checkpoint *) malloc(sizeof(cam_ta_checkpoint));
135 cp->name = (char *)malloc(strlen(name) + 1);
140 strncpy(cp->name, name, strlen(name));
143 printf("[CHECK-POINT] %s...(%s:%d)\n", name, filename, line);
145 gettimeofday(&t, NULL);
146 cp->timestamp = t.tv_sec * 1000000L + t.tv_usec;
147 #ifdef CAM_TA_UNIT_MSEC
148 cp->timestamp = (cp->timestamp >= 1000) ? cp->timestamp / 1000 : 0;
151 cam_g_cps[cam_g_cp_index] = cp;
158 void cam_ta_show_checkpoints(void)
165 printf("BEGIN RESULT ============================\n");
166 for (i = 0; i < cam_g_cp_index; i++) {
167 printf("[%d] %s : %ld us.\n", i, cam_g_cps[i]->name,
168 cam_g_cps[i]->timestamp);
170 printf("END RESULT ============================\n");
173 void cam_ta_show_diff(char *name1, char *name2)
178 printf("Time takes from [%s] to [%s] : %ld us.\n", name1, name2,
179 cam_ta_get_diff(name1, name2));
182 unsigned long cam_ta_get_diff(char *name1, char *name2)
189 /* fail if bad param. */
190 if (!name1 || !name2)
194 if (strcmp(name1, name2) == 0)
198 if ((cp1 = __get_cp_index(name1)) == -1)
201 if ((cp2 = __get_cp_index(name2)) == -1)
205 return value must be positive value.
206 bcz the value of higher index of cam_g_cps always higher than lower one.
208 return cam_g_cps[cp2]->timestamp - cam_g_cps[cp1]->timestamp;
212 static int __get_cp_index(char *name)
219 for (i = 0; i < cam_g_cp_index; i++) {
220 if (strcmp(name, cam_g_cps[i]->name) == 0)
227 static int __get_accum_index(char *name)
234 for (i = 0; i < cam_g_accum_index; i++) {
235 if (strcmp(name, cam_g_accums[i]->name) == 0)
242 static void __free_cps(void)
249 for (i = 0; i < cam_g_cp_index; i++) {
251 if (cam_g_cps[i]->name)
252 free(cam_g_cps[i]->name);
266 static void __free_accums(void)
273 for (i = 0; i < cam_g_accum_index; i++) {
274 if (cam_g_accums[i]) {
275 if (cam_g_accums[i]->name)
276 free(cam_g_accums[i]->name);
278 free(cam_g_accums[i]);
280 cam_g_accums[i] = NULL;
284 cam_g_accum_index = 0;
285 cam_g_accum_longest_name = 0;
291 int cam_ta_accum_item_begin(char *name, int show, char *filename, int line)
293 cam_ta_accum_item *accum = NULL;
304 if (cam_g_accum_index == CAM_TA_MAX_ACCUM)
310 name_len = strlen(name);
314 /* if 'name' is new one. create new item. */
315 if ((index = __get_accum_index(name)) == -1) {
316 accum = (cam_ta_accum_item *) malloc(sizeof(cam_ta_accum_item));
321 memset(accum, 0, sizeof(cam_ta_accum_item));
322 accum->elapsed_min = 0xFFFFFFFF;
324 accum->name = (char *)malloc(name_len + 1);
330 memset(accum->name, 0, name_len+1);
331 strncpy(accum->name, name, name_len);
333 /* add it to list. */
334 cam_g_accums[cam_g_accum_index] = accum;
337 if (cam_g_accum_longest_name < name_len)
338 cam_g_accum_longest_name = name_len;
341 accum = cam_g_accums[index];
344 /* verify pairs of begin, end. */
345 if (accum->on_estimate) {
346 printf("[%s] is not 'end'ed!\n", accum->name);
351 gettimeofday(&t, NULL);
352 accum->timestamp = t.tv_sec * 1000000L + t.tv_usec;
353 #ifdef CAM_TA_UNIT_MSEC
355 (accum->timestamp >= 1000) ? accum->timestamp / 1000 : 0;
357 accum->on_estimate = 1;
359 if (accum->first_start == 0) { /* assum that timestamp never could be zero. */
360 accum->first_start = accum->timestamp;
362 if (cam_g_accum_first_time > accum->first_start)
363 cam_g_accum_first_time = accum->first_start;
367 printf("[ACCUM BEGIN] %s : %ld ---(%s:%d)\n", name,
368 accum->timestamp, filename, line);
375 int cam_ta_accum_item_end(char *name, int show, char *filename, int line)
377 cam_ta_accum_item *accum = NULL;
388 /* get time first for more accuracy. */
389 gettimeofday(&t, NULL);
391 if (cam_g_accum_index == CAM_TA_MAX_ACCUM)
397 if (strlen(name) == 0)
400 /* varify the 'name' is already exist. */
401 if ((index = __get_accum_index(name)) == -1) {
402 /* printf("[%s] is not added before!\n", name); */
406 accum = cam_g_accums[index];
408 /* verify pairs of begin, end. */
409 if (!accum->on_estimate) {
410 printf("[%s] is not 'begin' yet!\n", accum->name);
414 /* get current timestamp. */
415 tval = t.tv_sec * 1000000L + t.tv_usec;
416 #ifdef CAM_TA_UNIT_MSEC
417 tval = (tval >= 1000) ? tval / 1000 : 0;
420 /* update last_end */
421 accum->last_end = tval;
423 /* make get elapsed time. */
424 tval = tval - accum->timestamp;
428 tval > accum->elapsed_max ? tval : accum->elapsed_max;
430 tval < accum->elapsed_min ? tval : accum->elapsed_min;
433 printf("[ACCUM END] %s : %ld + %ld ---(%s:%d)\n", name,
434 accum->elapsed_accum, tval, filename, line);
436 /* add elapsed time */
437 accum->elapsed_accum += tval;
438 accum->on_estimate = 0;
443 void __print_some_info(FILE *fp)
450 fprintf(fp, "\nb~ b~ b~\n\n");
453 /* General infomation */
456 char hostname[256] = { '\0', };
458 struct rusage r_usage;
460 fprintf(fp, "\n[[ General info ]]\n");
464 fprintf(fp, "Date : %s", ctime(&t_val));
467 if (gethostname(hostname, 255) == 0 && uname(&uts) >= 0) {
468 fprintf(fp, "Hostname : %s\n", hostname);
469 fprintf(fp, "System : %s\n", uts.sysname);
470 fprintf(fp, "Machine : %s\n", uts.machine);
471 fprintf(fp, "Nodename : %s\n", uts.nodename);
472 fprintf(fp, "Release : %s \n", uts.release);
473 fprintf(fp, "Version : %s \n", uts.version);
476 fprintf(fp, "Process priority : %d\n",
477 getpriority(PRIO_PROCESS, getpid()));
478 getrusage(RUSAGE_SELF, &r_usage);
480 "CPU usage : User = %ld.%06ld, System = %ld.%06ld\n",
481 r_usage.ru_utime.tv_sec, r_usage.ru_utime.tv_usec,
482 r_usage.ru_stime.tv_sec, r_usage.ru_stime.tv_usec);
486 /* host environment variables */
488 extern char **environ;
489 char **env = environ;
491 fprintf(fp, "\n[[ Host environment variables ]]\n");
493 fprintf(fp, "%s\n", *env);
498 /* applied samsung feature */
501 fprintf(fp, "\n[[ Applied samsung feature ]]\n");
502 #ifdef GST_EXT_TIME_ANALYSIS
503 fprintf(fp, "GST_EXT_TIME_ANALYSIS\n");
505 #ifdef GST_EXT_SKIP_RESCAN_REGISTRY
506 fprintf(fp, "GST_EXT_SKIP_RESCAN_REGISTRY\n");
508 #ifdef GST_EXT_USE_TINY_REGISTRY
510 fprintf(fp, "GST_EXT_USE_TINY_REGISTRY\n");
512 #ifdef GST_EXT_PAD_LINK_UNCHECKED
513 fprintf(fp, "GST_EXT_PAD_LINK_UNCHECKED\n");
515 #ifdef GST_EXT_DFBVIDEOSINK_IPP
516 fprintf(fp, "GST_EXT_DFBVIDEOSINK_IPP\n");
518 #ifdef GST_EXT_REDUCE_PLUGIN_NUM
519 fprintf(fp, "GST_EXT_REDUCE_PLUGIN_NUM\n");
521 #ifdef GST_EXT_USE_PDP_NETWORK
522 fprintf(fp, "GST_EXT_USE_PDP_NETWORK\n");
524 #ifdef GST_EXT_VOLUME_WITHOUT_LIBOIL
525 fprintf(fp, "GST_EXT_VOLUME_WITHOUT_LIBOIL\n");
527 #ifdef GST_EXT_DECODEBIN_QUEUE_ENLARGE
528 fprintf(fp, "GST_EXT_DECODEBIN_QUEUE_ENLARGE\n");
531 /* Camcorder releated feature */
533 #ifdef GST_EXT_NONBLOCKDQUE
534 fprintf(fp, "GST_EXT_NONBLOCKDQUE\n");
536 #ifdef GST_EXT_RENEGOTIATION
537 fprintf(fp, "GST_EXT_RENEGOTIATION\n");
539 #ifdef GST_EXT_MOBILECAMERA
540 fprintf(fp, "GST_EXT_MOBILECAMERA\n");
542 #ifdef GST_EXT_VIDEOSCALE_IPP
543 fprintf(fp, "GST_EXT_VIDEOSCALE_IPP\n");
545 #ifdef GST_EXT_ASYNC_DEV
546 fprintf(fp, "GST_EXT_ASYNC_DEV\n");
548 #ifdef GST_EXT_AV_RECORDING
549 fprintf(fp, "GST_EXT_AV_RECORDING\n");
551 #ifdef GST_EXT_SWITCH_CAMERA
552 fprintf(fp, "GST_EXT_SWITCH_CAMERA\n");
561 void cam_ta_accum_show_result(int direction)
571 case CAM_TA_SHOW_STDOUT:
574 case CAM_TA_SHOW_STDERR:
577 case CAM_TA_SHOW_FILE:
579 fp = fopen(CAM_TA_RESULT_FILE, "wt");
584 __print_some_info(fp);
586 #ifdef CAM_TA_UNIT_MSEC
587 snprintf(format, sizeof(format),
588 "[%%3d] %%-%ds | \ttotal : %%4ld\tcalls : %%3ld\tavg : %%4ld\tmin : %%4ld\tmax : %%4ld\tstart : %%4lu\tend : %%4lu\tunpair : %%3ld\n",
589 cam_g_accum_longest_name);
591 "BEGIN RESULT ACCUM============================ : NumOfItems : %d, unit(msec)\n",
594 snprintf(format, sizeof(format),
595 "[%%3d] %%-%ds | \ttotal : %%ld\tcalls : %%ld\tavg : %%ld\tmin : %%ld\tmax : %%ld\tstart : %%lu\tend : %%lu\tunpair : %%ld\n",
596 cam_g_accum_longest_name);
598 "BEGIN RESULT ACCUM============================ : NumOfItems : %d, unit(usec)\n",
602 for (i = 0; i < cam_g_accum_index; i++) {
603 /* prevent 'devide by zero' error */
604 if (cam_g_accums[i]->num_calls == 0)
605 cam_g_accums[i]->num_calls = 1;
607 fprintf(fp, format, i, cam_g_accums[i]->name, cam_g_accums[i]->elapsed_accum, cam_g_accums[i]->num_calls, (cam_g_accums[i]->elapsed_accum == 0) ? 0 : (int)(cam_g_accums[i]->elapsed_accum / cam_g_accums[i]->num_calls), /* Fix it! : devide by zero. */
608 cam_g_accums[i]->elapsed_min,
609 cam_g_accums[i]->elapsed_max,
610 cam_g_accums[i]->first_start - cam_g_accum_first_time,
611 cam_g_accums[i]->last_end - cam_g_accum_first_time,
612 cam_g_accums[i]->num_unpair);
614 fprintf(fp, "END RESULT ACCUM ============================\n");
616 if (direction == CAM_TA_SHOW_FILE)
620 #endif /* _CAM_TA_C_ */