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.
24 #include "_util_log.h"
27 int _get_sysconf(int *ncpu, long *tick)
29 *ncpu = sysconf(_SC_NPROCESSORS_ONLN);
30 *ncpu = *ncpu < 1 ? 1 : *ncpu;
31 *tick = sysconf(_SC_CLK_TCK);
35 int _get_stat_info(pid_t pid, unsigned int *ut, unsigned int *st)
38 char buf[128] = {0, };
39 unsigned long cutime, cstime;
42 snprintf(buf, sizeof(buf), "/proc/%d/stat", (int)pid);
48 retvm_if(fp == NULL, -1, "Failed to open %s\n", buf);
49 fscanf(fp, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %lu %lu %ld %ld",
50 ut, st, &cutime, &cstime);
60 double _get_cpu_ratio(void *data, int ncpu, long tick)
62 struct _task_info *tinfo = (struct _task_info *)data;
63 unsigned int utime, stime;
67 unsigned long long jiffy;
69 retvm_if(tinfo == NULL, -1, "Invalid argument: tinfo is NULL\n");
73 r = _get_stat_info(tinfo->pid, &utime, &stime);
78 /* retvm_if(r < 0, -1, "Failed to get stat info\n"); */
80 gettimeofday(&timev, NULL);
81 jiffy = (timev.tv_sec - tinfo->oldtimev.tv_sec) * tick +
82 ((timev.tv_usec - tinfo->oldtimev.tv_usec) * tick) / 1000000;
84 if(utime >= tinfo->oldutime) {
85 usr = ((double)(utime - tinfo->oldutime) * 100 / jiffy) / ncpu;
89 if(stime >= tinfo->oldstime) {
90 sys = ((double)(stime - tinfo->oldstime) * 100 / jiffy) / ncpu;
95 /* _D("per:%lf] %lf %lf/ %u %u/ %u %u/ %u\n",
96 usr+sys, usr, sys, utime, stime, tinfo->oldutime, tinfo->oldstime, jiffy);
99 tinfo->oldutime = utime;
100 tinfo->oldstime = stime;
101 tinfo->oldtimev = timev;