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.
24 #include <sys/types.h>
26 #include <sys/sysinfo.h>
30 #include "simple_util.h"
32 #define _MAX_STATUS_BUF_SIZE 64
33 #define _MAX_STAT_BUF_SIZE 1024
35 static const char PROC_PROCESS_STATUS_INFO[] = "/proc/self/status";
36 static const char PROC_KEY_PROCESS_MEMORY[] = "VmSize";
38 long long __get_process_running_time(pid_t pid)
40 char proc_path[sizeof("/proc//stat") + sizeof(int) * 3] = { 0, };
47 long long start_time = 0;
48 long long running_time = 0;
53 strcpy(proc_path, "/proc/self/stat");
57 SECURE_LOGD("pid: %d", pid);
58 sprintf(proc_path, "/proc/%u/task", pid);
64 _E("PID is invalid.");
68 int fd = open(proc_path, O_RDONLY);
71 SECURE_LOGE("Openning %s is failed.", proc_path);
75 line = (char *)calloc(_MAX_STAT_BUF_SIZE, sizeof(char));
78 _E("The memory is insufficient.");
82 res = read(fd, line, _MAX_STAT_BUF_SIZE - 1);
85 SECURE_LOGE("Reading %s is failed.", proc_path);
91 for (i = 0, str = line; ; ++i, str = NULL)
93 token = strtok_r(str, " ", &saveptr);
96 _E("There is no start time.");
100 if (i == 21) //starttime
102 start_time = atoll(token);
103 SECURE_LOGD("Start time: %lld (ticks)", start_time);
113 long long sec_since_boot = (long long)info.uptime;
115 start_time /= (long long)sysconf(_SC_CLK_TCK);
116 running_time = sec_since_boot - start_time;
118 unsigned long mm = (unsigned long)running_time;
119 unsigned ss = mm % 60;
121 SECURE_LOGD("Running time: %lu:%02u", mm, ss);
139 int __get_info_from_proc(const char* path, const char* key)
143 char line[_MAX_STATUS_BUF_SIZE] = {0, };
144 char field[_MAX_STATUS_BUF_SIZE] = {0, };
146 FILE* fp = fopen(path, "r");
149 while (fgets(line, _MAX_STATUS_BUF_SIZE, fp))
151 if (sscanf(line, "%s %d", field, &value) != EOF)
153 if (strncmp(field, key, strlen(key)) == 0)
155 SECURE_LOGD("PROC %s VALUE: %d\n", field, value * 1024);
159 return value * 1024;;
170 SLPAPI int aul_get_app_allocated_memory(void)
172 return __get_info_from_proc(PROC_PROCESS_STATUS_INFO, PROC_KEY_PROCESS_MEMORY);
175 SLPAPI long long aul_get_app_running_time(void)
177 return __get_process_running_time(-1);