Git init
[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 ? : ENV_START);
54         /*_retvm_if(s == NULL, -1, "%s is not set", name);*/
55         _retv_if(s == NULL, -1);
56
57         r = sscanf(s, "%u/%u", (int *)&t->tv_sec, (int *)&t->tv_usec);
58         if (r != 2)
59                 r = sscanf(s, "%u %u", (int *)&t->tv_sec, (int *)&t->tv_usec);
60
61         _retv_if(r != 2, -1);
62
63         return 0;
64 }
65
66 static int __get_time_from(const char *name)
67 {
68         int r;
69         struct timeval s;
70         struct timeval t;
71
72         gettimeofday(&t, NULL);
73
74         r = __get_envtime(name, &s);
75         _retv_if(r == -1, 0);
76
77         return __get_msec(&s, &t);
78 }
79
80 EXPORT_API int appcore_measure_time_from(const char *envnm)
81 {
82         return __get_time_from(envnm);
83 }
84
85 EXPORT_API int appcore_measure_time(void)
86 {
87         return __get_time(&tv_s);
88 }
89
90 EXPORT_API void appcore_measure_start(void)
91 {
92         gettimeofday(&tv_s, NULL);
93 }