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