3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include "utility/fw_list.h"
19 #include "utility/fw_timer.h"
20 #include "utility/sync_util.h"
23 #define EXPORT_API __attribute__ ((visibility("default")))
26 #ifndef SYNC_AGENT_LOG
28 #define LOG_TAG "AF_TIMER"
34 static util_timer_element_s *_timer_element_alloc(char *label);
35 static void _timer_element_free(util_timer_element_s * element);
36 static double _calculate_diff_time(util_timer_element_s * element);
38 static util_timer_element_s *_timer_element_alloc(char *label)
42 util_timer_element_s *element = NULL;
43 char *dest_label = NULL;
48 element = (util_timer_element_s *) calloc(1, sizeof(util_timer_element_s));
49 if (element == NULL) {
50 _DEBUG_ERROR("fw_timer_element alloc failed because of out of memory");
53 dest_label = element->label;
56 strncpy(dest_label, label, UTIL_TIMER_MAX_LABEL_LEN);
63 static void _timer_element_free(util_timer_element_s * element)
67 if (element != NULL) {
74 util_timer_s *util_timer_alloc()
78 util_timer_s *timer = (util_timer_s *) malloc(sizeof(util_timer_s));
81 util_list_init(&(timer->element_head));
83 _DEBUG_ERROR("fw_timer_alloc failed because of out of memory");
91 static double _calculate_diff_time(util_timer_element_s * element)
95 double diff_time = 0.0;
96 double sTime = element->start_time.tv_sec + (element->start_time.tv_usec / 1000000.0);
97 double fTime = element->finish_time.tv_sec + (element->finish_time.tv_usec / 1000000.0);
98 diff_time = fTime - sTime;
105 util_timer_error_e util_timer_start(util_timer_s * timer, char *label)
109 util_timer_error_e err = UTIL_TIMER_OK;
111 err = UTIL_TIMER_INVALID_TIMER;
115 util_timer_element_s *element = _timer_element_alloc(label);
116 if (element == NULL) {
117 err = UTIL_TIMER_OUT_OF_MEMORY;
121 /* set timer start time */
122 if (-1 == gettimeofday(&(element->start_time), NULL)) {
123 err = UTIL_TIMER_GETTIME_ERROR;
124 _timer_element_free(element);
129 util_list_add_node((util_list_node_s *) element, &(timer->element_head));
136 util_timer_error_e util_timer_finish(util_timer_s * timer, char *label, double *passed_sec)
140 util_timer_error_e err = UTIL_TIMER_OK;
141 util_list_node_s *iter = NULL;
142 util_list_node_s *head = &(timer->element_head);
143 util_timer_element_s *element = NULL;
146 UTIL_LIST_ITER(iter, head) {
147 element = (util_timer_element_s *) iter;
149 if (strncmp(label, element->label, UTIL_TIMER_MAX_LABEL_LEN) != 0) {
153 if (-1 == gettimeofday(&(element->finish_time), NULL)) {
154 err = UTIL_TIMER_GETTIME_ERROR;
157 /* calcuate passed_sec */
158 if (passed_sec != NULL) {
159 *passed_sec = _calculate_diff_time(element);
172 util_timer_error_e util_timer_print(util_timer_s * timer)
176 util_timer_error_e err = UTIL_TIMER_OK;
184 util_timer_error_e util_timer_free(util_timer_s * timer)
188 util_timer_error_e err = UTIL_TIMER_OK;