4 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <sys/types.h>
27 #include <sys/sysinfo.h>
32 #include "simple_util.h"
35 #define _MAX_STATUS_BUF_SIZE 64
36 #define _MAX_STAT_BUF_SIZE 1024
38 static const char PROC_PROCESS_STATUS_INFO[] = "/proc/self/status";
39 static const char PROC_KEY_PROCESS_MEMORY[] = "VmSize";
41 long long __get_process_running_time(pid_t pid)
43 char proc_path[_MAX_STAT_BUF_SIZE] = { 0, };
50 long long start_time = 0;
51 long long running_time = 0;
56 snprintf(proc_path, sizeof(proc_path), "%sproc/self/stat", _get_root_path());
60 SECURE_LOGD("pid: %d", pid);
61 snprintf(proc_path, sizeof(proc_path), "%sproc/%u/task", _get_root_path(), pid);
67 _E("PID is invalid.");
71 int fd = open(proc_path, O_RDONLY);
74 SECURE_LOGE("Openning %s is failed.", proc_path);
78 line = (char *)calloc(_MAX_STAT_BUF_SIZE, sizeof(char));
81 _E("The memory is insufficient.");
85 res = read(fd, line, _MAX_STAT_BUF_SIZE - 1);
88 SECURE_LOGE("Reading %s is failed.", proc_path);
94 for (i = 0, str = line; ; ++i, str = NULL)
96 token = strtok_r(str, " ", &saveptr);
99 _E("There is no start time.");
103 if (i == 21) //starttime
105 start_time = atoll(token);
106 SECURE_LOGD("Start time: %lld (ticks)", start_time);
116 long long sec_since_boot = (long long)info.uptime;
118 start_time /= (long long)sysconf(_SC_CLK_TCK);
119 running_time = sec_since_boot - start_time;
121 unsigned long mm = (unsigned long)running_time;
122 unsigned ss = mm % 60;
124 SECURE_LOGD("Running time: %lu:%02u", mm, ss);
142 int __get_info_from_proc(const char* path, const char* key)
146 char line[_MAX_STATUS_BUF_SIZE] = {0, };
147 char field[_MAX_STATUS_BUF_SIZE] = {0, };
149 FILE* fp = fopen(path, "r");
152 while (fgets(line, _MAX_STATUS_BUF_SIZE, fp))
154 if (sscanf(line, "%s %d", field, &value) != EOF)
156 if (strncmp(field, key, strlen(key)) == 0)
158 if (value > (INT_MAX / 1024)) {
159 value = INT_MAX / 1024;
162 SECURE_LOGD("PROC %s VALUE: %d\n", field, value * 1024);
166 return value * 1024;;
177 SLPAPI int aul_get_app_allocated_memory(void)
179 char buf[_MAX_STAT_BUF_SIZE] = {0, };
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);
186 SLPAPI long long aul_get_app_running_time(void)
188 return __get_process_running_time(-1);