2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.tizenopensource.org/license
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * 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;
40 snprintf(buf, sizeof(buf), "/proc/%d/stat", (int)pid);
46 retvm_if(fp == NULL, -1, "Failed to open %s\n", buf);
47 fscanf(fp, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %lu %lu %ld %ld",
48 ut, st, &cutime, &cstime);
58 double _get_cpu_ratio(void *data, int ncpu, long tick)
60 struct _task_info *tinfo = (struct _task_info *)data;
61 unsigned int utime, stime;
65 unsigned long long jiffy;
67 retvm_if(tinfo == NULL, -1, "Invalid argument: tinfo is NULL\n");
71 r = _get_stat_info(tinfo->pid, &utime, &stime);
76 /* retvm_if(r < 0, -1, "Failed to get stat info\n"); */
78 gettimeofday(&timev, NULL);
79 jiffy = (timev.tv_sec - tinfo->oldtimev.tv_sec) * tick +
80 ((timev.tv_usec - tinfo->oldtimev.tv_usec) * tick) / 1000000;
82 if(utime >= tinfo->oldutime) {
83 usr = ((double)(utime - tinfo->oldutime) * 100 / jiffy) / ncpu;
87 if(stime >= tinfo->oldstime) {
88 sys = ((double)(stime - tinfo->oldstime) * 100 / jiffy) / ncpu;
93 /* _D("per:%lf] %lf %lf/ %u %u/ %u %u/ %u\n",
94 usr+sys, usr, sys, utime, stime, tinfo->oldutime, tinfo->oldstime, jiffy);
97 tinfo->oldutime = utime;
98 tinfo->oldstime = stime;
99 tinfo->oldtimev = timev;