274b7ec79ab3254f2c0cedb7048f9d21308d5fd7
[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 #define _GNU_SOURCE
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <fcntl.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/sysinfo.h>
28 #include <limits.h>
29
30 #include "aul.h"
31 #include "aul_api.h"
32 #include "simple_util.h"
33 #include "aul_zone.h"
34
35 #define _MAX_STATUS_BUF_SIZE    64
36 #define _MAX_STAT_BUF_SIZE              1024
37
38 static const char PROC_PROCESS_STATUS_INFO[] = "/proc/self/status";
39 static const char PROC_KEY_PROCESS_MEMORY[] = "VmSize";
40
41 long long __get_process_running_time(pid_t pid)
42 {
43         char proc_path[_MAX_STAT_BUF_SIZE] = { 0, };
44         char *line = NULL;
45         ssize_t res = -1;
46         int i = 0;
47         char *str = NULL;
48         char *token = NULL;
49         char *saveptr = NULL;
50         long long start_time = 0;
51         long long running_time = 0;
52
53         if (pid == -1) //self
54         {
55                 _D("self");
56                 snprintf(proc_path, sizeof(proc_path), "%sproc/self/stat", _get_root_path());
57         }
58         else if (pid > 0)
59         {
60                 SECURE_LOGD("pid: %d", pid);
61                 snprintf(proc_path, sizeof(proc_path), "%sproc/%u/task", _get_root_path(), pid);
62                 _E("Not supported");
63                 return -1;
64         }
65         else
66         {
67                 _E("PID is invalid.");
68                 return -1;
69         }
70
71         int fd = open(proc_path, O_RDONLY);
72         if (fd < 0)
73         {
74                 SECURE_LOGE("Openning %s is failed.", proc_path);
75                 goto CATCH;
76         }
77
78         line = (char *)calloc(_MAX_STAT_BUF_SIZE, sizeof(char));
79         if (line == NULL)
80         {
81                 _E("The memory is insufficient.");
82                 goto CATCH;
83         }
84
85         res = read(fd, line, _MAX_STAT_BUF_SIZE - 1);
86         if (res < 0)
87         {
88                 SECURE_LOGE("Reading %s is failed.", proc_path);
89                 goto CATCH;
90         }
91         close(fd);
92         fd = -1;
93
94         for (i = 0, str = line; ; ++i, str = NULL)
95         {
96                 token = strtok_r(str, " ", &saveptr);
97                 if (token == NULL)
98                 {
99                         _E("There is no start time.");
100                         goto CATCH;
101                 }
102
103                 if (i == 21) //starttime
104                 {
105                         start_time = atoll(token);
106                         SECURE_LOGD("Start time: %lld (ticks)", start_time);
107                         break;
108                 }
109         }
110         free(line);
111         line = NULL;
112
113         {
114                 struct sysinfo info;
115                 sysinfo(&info);
116                 long long sec_since_boot = (long long)info.uptime;
117
118                 start_time /= (long long)sysconf(_SC_CLK_TCK);
119                 running_time = sec_since_boot - start_time;
120
121                 unsigned long mm = (unsigned long)running_time;
122                 unsigned ss = mm % 60;
123                 mm /= 60;
124                 SECURE_LOGD("Running time: %lu:%02u", mm, ss);
125         }
126
127         return running_time;
128
129 CATCH:
130         if (fd >= 0)
131         {
132                 close(fd);
133         }
134         if (line != NULL)
135         {
136                 free(line);
137         }
138
139         return -1;
140 }
141
142 int __get_info_from_proc(const char* path, const char* key)
143 {
144         int value = 0;
145
146         char line[_MAX_STATUS_BUF_SIZE] = {0, };
147         char field[_MAX_STATUS_BUF_SIZE] = {0, };
148
149         FILE* fp = fopen(path, "r");
150         if (fp != NULL)
151         {
152                 while (fgets(line, _MAX_STATUS_BUF_SIZE, fp))
153                 {
154                         if (sscanf(line, "%s %d", field, &value) != EOF)
155                         {
156                                 if (strncmp(field, key, strlen(key)) == 0)
157                                 {
158                                         if (value > (INT_MAX / 1024)) {
159                                                 value = INT_MAX / 1024;
160                                         }
161
162                                         SECURE_LOGD("PROC %s VALUE: %d\n", field, value * 1024);
163
164                                         fclose(fp);
165
166                                         return value * 1024;;
167                                 }
168                         }
169                 }
170
171                 fclose(fp);
172         }
173
174         return -1;
175 }
176
177 SLPAPI int aul_get_app_allocated_memory(void)
178 {
179         char buf[_MAX_STAT_BUF_SIZE] = {0, };
180
181         snprintf(buf, _MAX_STAT_BUF_SIZE - 1, "%s%s", _get_root_path(),
182                  PROC_PROCESS_STATUS_INFO);
183         return __get_info_from_proc(buf, PROC_KEY_PROCESS_MEMORY);
184 }
185
186 SLPAPI long long aul_get_app_running_time(void)
187 {
188         return __get_process_running_time(-1);
189 }