3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * Licensed under the Flora License, Version 1.1 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://floralicense.org/license/
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an AS IS BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
22 #include "_util_log.h"
25 int _get_sysconf(int *ncpu, long *tick)
27 *ncpu = sysconf(_SC_NPROCESSORS_ONLN);
28 *ncpu = *ncpu < 1 ? 1 : *ncpu;
29 *tick = sysconf(_SC_CLK_TCK);
33 int _get_stat_info(pid_t pid, unsigned int *ut, unsigned int *st)
36 char buf[128] = {0, };
37 unsigned long cutime, cstime;
41 snprintf(buf, sizeof(buf), "/proc/%d/stat", (int)pid);
47 retvm_if(fp == NULL, -1, "Failed to open %s\n", buf);
48 ret = fscanf(fp, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %lu %lu %ld %ld",
49 ut, st, &cutime, &cstime);
52 _E("Failed to fscanf() \n");
65 double _get_cpu_ratio(void *data, int ncpu, long tick)
67 struct _task_info *tinfo = (struct _task_info *)data;
68 unsigned int utime, stime;
72 unsigned long long jiffy;
74 retvm_if(tinfo == NULL, -1, "Invalid argument: tinfo is NULL\n");
78 r = _get_stat_info(tinfo->pid, &utime, &stime);
83 /* retvm_if(r < 0, -1, "Failed to get stat info\n"); */
85 gettimeofday(&timev, NULL);
86 jiffy = (timev.tv_sec - tinfo->oldtimev.tv_sec) * tick +
87 ((timev.tv_usec - tinfo->oldtimev.tv_usec) * tick) / 1000000;
89 if(utime >= tinfo->oldutime) {
90 usr = ((double)(utime - tinfo->oldutime) * 100 / jiffy) / ncpu;
94 if(stime >= tinfo->oldstime) {
95 sys = ((double)(stime - tinfo->oldstime) * 100 / jiffy) / ncpu;
100 /* _D("per:%lf] %lf %lf/ %u %u/ %u %u/ %u\n",
101 usr+sys, usr, sys, utime, stime, tinfo->oldutime, tinfo->oldstime, jiffy);
104 tinfo->oldutime = utime;
105 tinfo->oldstime = stime;
106 tinfo->oldtimev = timev;