0aaa89d7dc642d503b271a7d6076e4fccf3f0fd6
[apps/core/preloaded/taskmanager.git] / src / _cpu.c
1 /*
2  * org.tizen.taskmgr
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
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
8  *
9  *     http://floralicense.org/license/
10  *
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.
16  */
17
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <sys/time.h>
21
22 #include "_util_log.h"
23 #include "_cpu.h"
24
25 int _get_sysconf(int *ncpu, long *tick)
26 {
27         *ncpu = sysconf(_SC_NPROCESSORS_ONLN);
28         *ncpu = *ncpu < 1 ? 1 : *ncpu;
29         *tick = sysconf(_SC_CLK_TCK);
30         return 0;
31 }
32
33 int _get_stat_info(pid_t pid, unsigned int *ut, unsigned int *st)
34 {
35         FILE *fp;
36         char buf[128] = {0, };
37         unsigned long cutime, cstime;
38         int i;
39         int ret = -1;
40
41         snprintf(buf, sizeof(buf), "/proc/%d/stat", (int)pid);
42
43         *ut = *st = 0;
44         cutime = cstime = 0;
45         fp = fopen(buf, "r");
46         if (fp) {
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);
50                 if(ret < 0)
51                 {
52                         _E("Failed to fscanf() \n");
53                         fclose(fp);
54                         return -1;
55                 }
56                 *ut += cutime;
57                 *st += cstime;
58                 fclose(fp);
59
60                 return 0;
61         }
62         return -1;
63 }
64
65 double _get_cpu_ratio(void *data, int ncpu, long tick)
66 {
67         struct _task_info *tinfo = (struct _task_info *)data;
68         unsigned int utime, stime;
69         struct timeval timev;
70         double usr, sys;
71         int r;
72         unsigned long long jiffy;
73
74         retvm_if(tinfo == NULL, -1, "Invalid argument: tinfo is NULL\n");
75
76         utime = 0;
77         stime = 0;
78         r = _get_stat_info(tinfo->pid, &utime, &stime);
79         if (r < 0) {
80                 _D("failed\n");
81                 return 0;
82         }
83         /* retvm_if(r < 0, -1, "Failed to get stat info\n"); */
84
85         gettimeofday(&timev, NULL);
86         jiffy = (timev.tv_sec - tinfo->oldtimev.tv_sec) * tick +
87                 ((timev.tv_usec - tinfo->oldtimev.tv_usec) * tick) / 1000000;
88
89         if(utime >= tinfo->oldutime) {
90                 usr = ((double)(utime - tinfo->oldutime) * 100 / jiffy) / ncpu;
91         } else {
92                 usr = 0.0;
93         }
94         if(stime >= tinfo->oldstime) {
95                 sys = ((double)(stime - tinfo->oldstime) * 100 / jiffy) / ncpu;
96         } else {
97                 sys = 0.0;
98         }
99
100         /* _D("per:%lf] %lf %lf/ %u %u/ %u %u/ %u\n",
101            usr+sys, usr, sys, utime, stime, tinfo->oldutime, tinfo->oldstime, jiffy);
102          */
103
104         tinfo->oldutime = utime;
105         tinfo->oldstime = stime;
106         tinfo->oldtimev = timev;
107
108         return usr + sys;
109 }
110
111