tizen 2.4 release
[framework/appfw/app-core.git] / src / appcore-measure.c
1 /*
2  *  app-core
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <sys/time.h>
26
27 #include "appcore-internal.h"
28
29 static struct timeval tv_s;     /* measure start */
30
31 static inline int __get_msec(struct timeval *s, struct timeval *e)
32 {
33         return (e->tv_sec - s->tv_sec) * 1000 +
34             (e->tv_usec - s->tv_usec + 500) / 1000;
35 }
36
37 static int __get_time(struct timeval *s)
38 {
39         struct timeval t;
40
41         _retv_if(s == NULL || (s->tv_sec == 0 && s->tv_usec == 0), 0);
42
43         gettimeofday(&t, NULL);
44
45         return __get_msec(s, &t);
46 }
47
48 static int __get_envtime(const char *name, struct timeval *t)
49 {
50         int r;
51         char *s;
52
53         s = getenv(name ? name : ENV_START);
54         if (s == NULL) {
55                 return -1;
56         }
57
58         r = sscanf((const char *)s, "%u/%u", (int *)&t->tv_sec, (int *)&t->tv_usec);
59         if (r != 2)
60                 r = sscanf((const char *)s, "%u %u", (int *)&t->tv_sec, (int *)&t->tv_usec);
61
62         if (r != 2) {
63                 return -1;
64         }
65
66         return 0;
67 }
68
69 static int __get_time_from(const char *name)
70 {
71         int r;
72         struct timeval s;
73         struct timeval t;
74
75         gettimeofday(&t, NULL);
76
77         r = __get_envtime(name, &s);
78         _retv_if(r == -1, 0);
79
80         return __get_msec(&s, &t);
81 }
82
83 EXPORT_API int appcore_measure_time_from(const char *envnm)
84 {
85         return __get_time_from(envnm);
86 }
87
88 EXPORT_API int appcore_measure_time(void)
89 {
90         return __get_time(&tv_s);
91 }
92
93 EXPORT_API void appcore_measure_start(void)
94 {
95         gettimeofday(&tv_s, NULL);
96 }