Release version 1.4.8
[platform/core/appfw/app-core.git] / src / legacy / appcore-measure.c
1 /*
2  * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <sys/time.h>
20
21 #include "appcore-internal.h"
22
23 static struct timeval tv_s;     /* measure start */
24
25 static inline int __get_msec(struct timeval *s, struct timeval *e)
26 {
27         return (e->tv_sec - s->tv_sec) * 1000 +
28             (e->tv_usec - s->tv_usec + 500) / 1000;
29 }
30
31 static int __get_time(struct timeval *s)
32 {
33         struct timeval t;
34
35         _retv_if(s == NULL || (s->tv_sec == 0 && s->tv_usec == 0), 0);
36
37         gettimeofday(&t, NULL);
38
39         return __get_msec(s, &t);
40 }
41
42 static int __get_envtime(const char *name, struct timeval *t)
43 {
44         int r;
45         char *s;
46
47         s = getenv(name ? : ENV_START);
48         /*_retvm_if(s == NULL, -1, "%s is not set", name);*/
49         _retv_if(s == NULL, -1);
50
51         r = sscanf(s, "%u/%u", (int *)&t->tv_sec, (int *)&t->tv_usec);
52         if (r != 2)
53                 r = sscanf(s, "%u %u", (int *)&t->tv_sec, (int *)&t->tv_usec);
54
55         _retv_if(r != 2, -1);
56
57         return 0;
58 }
59
60 static int __get_time_from(const char *name)
61 {
62         int r;
63         struct timeval s;
64         struct timeval t;
65
66         gettimeofday(&t, NULL);
67
68         r = __get_envtime(name, &s);
69         _retv_if(r == -1, 0);
70
71         return __get_msec(&s, &t);
72 }
73
74 EXPORT_API int appcore_measure_time_from(const char *envnm)
75 {
76         return __get_time_from(envnm);
77 }
78
79 EXPORT_API int appcore_measure_time(void)
80 {
81         return __get_time(&tv_s);
82 }
83
84 EXPORT_API void appcore_measure_start(void)
85 {
86         gettimeofday(&tv_s, NULL);
87 }