tizen 2.3 release
[framework/appfw/aul-1.git] / src / runtime_info.c
1 /*
2  *  aul
3  *
4  * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <sys/sysinfo.h>
27
28 #include "aul.h"
29 #include "aul_api.h"
30 #include "simple_util.h"
31
32 #define _MAX_STATUS_BUF_SIZE    64
33 #define _MAX_STAT_BUF_SIZE              1024
34
35 static const char PROC_PROCESS_STATUS_INFO[] = "/proc/self/status";
36 static const char PROC_KEY_PROCESS_MEMORY[] = "VmSize";
37
38 long long __get_process_running_time(pid_t pid)
39 {
40         char proc_path[sizeof("/proc//stat") + sizeof(int) * 3] = { 0, };
41         char *line = NULL;
42         ssize_t res = -1;
43         int i = 0;
44         char *str = NULL;
45         char *token = NULL;
46         char *saveptr = NULL;
47         long long start_time = 0;
48         long long running_time = 0;
49
50         if (pid == -1) //self
51         {
52                 _D("self");
53                 strcpy(proc_path, "/proc/self/stat");
54         }
55         else if (pid > 0)
56         {
57                 SECURE_LOGD("pid: %d", pid);
58                 sprintf(proc_path, "/proc/%u/task", pid);
59                 _E("Not supported");
60                 return -1;
61         }
62         else
63         {
64                 _E("PID is invalid.");
65                 return -1;
66         }
67
68         int fd = open(proc_path, O_RDONLY);
69         if (fd < 0)
70         {
71                 SECURE_LOGE("Openning %s is failed.", proc_path);
72                 goto CATCH;
73         }
74
75         line = (char *)calloc(_MAX_STAT_BUF_SIZE, sizeof(char));
76         if (line == NULL)
77         {
78                 _E("The memory is insufficient.");
79                 goto CATCH;
80         }
81
82         res = read(fd, line, _MAX_STAT_BUF_SIZE - 1);
83         if (res < 0)
84         {
85                 SECURE_LOGE("Reading %s is failed.", proc_path);
86                 goto CATCH;
87         }
88         close(fd);
89         fd = -1;
90
91         for (i = 0, str = line; ; ++i, str = NULL)
92         {
93                 token = strtok_r(str, " ", &saveptr);
94                 if (token == NULL)
95                 {
96                         _E("There is no start time.");
97                         goto CATCH;
98                 }
99
100                 if (i == 21) //starttime
101                 {
102                         start_time = atoll(token);
103                         SECURE_LOGD("Start time: %lld (ticks)", start_time);
104                         break;
105                 }
106         }
107         free(line);
108         line = NULL;
109
110         {
111                 struct sysinfo info;
112                 sysinfo(&info);
113                 long long sec_since_boot = (long long)info.uptime;
114
115                 start_time /= (long long)sysconf(_SC_CLK_TCK);
116                 running_time = sec_since_boot - start_time;
117
118                 unsigned long mm = (unsigned long)running_time;
119                 unsigned ss = mm % 60;
120                 mm /= 60;
121                 SECURE_LOGD("Running time: %lu:%02u", mm, ss);
122         }
123
124         return running_time;
125
126 CATCH:
127         if (fd >= 0)
128         {
129                 close(fd);
130         }
131         if (line != NULL)
132         {
133                 free(line);
134         }
135
136         return -1;
137 }
138
139 int __get_info_from_proc(const char* path, const char* key)
140 {
141         int value = 0;
142
143         char line[_MAX_STATUS_BUF_SIZE] = {0, };
144         char field[_MAX_STATUS_BUF_SIZE] = {0, };
145
146         FILE* fp = fopen(path, "r");
147         if (fp != NULL)
148         {
149                 while (fgets(line, _MAX_STATUS_BUF_SIZE, fp))
150                 {
151                         if (sscanf(line, "%s %d", field, &value) != EOF)
152                         {
153                                 if (strncmp(field, key, strlen(key)) == 0)
154                                 {
155                                         SECURE_LOGD("PROC %s VALUE: %d\n", field, value * 1024);
156
157                                         fclose(fp);
158
159                                         return value * 1024;;
160                                 }
161                         }
162                 }
163
164                 fclose(fp);
165         }
166
167         return -1;
168 }
169
170 SLPAPI int aul_get_app_allocated_memory(void)
171 {
172         return __get_info_from_proc(PROC_PROCESS_STATUS_INFO, PROC_KEY_PROCESS_MEMORY);
173 }
174
175 SLPAPI long long aul_get_app_running_time(void)
176 {
177         return __get_process_running_time(-1);
178 }