apply FSL(Flora Software License)
[apps/core/preloaded/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 #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
40         snprintf(buf, sizeof(buf), "/proc/%d/stat", (int)pid);
41
42         *ut = *st = 0;
43         cutime = cstime = 0;
44         fp = fopen(buf, "r");
45         if (fp) {
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);
49                 *ut += cutime;
50                 *st += cstime;
51                 fclose(fp);
52
53                 return 0;
54         }
55         return -1;
56 }
57
58 double _get_cpu_ratio(void *data, int ncpu, long tick)
59 {
60         struct _task_info *tinfo = (struct _task_info *)data;
61         unsigned int utime, stime;
62         struct timeval timev;
63         double usr, sys;
64         int r;
65         unsigned long long jiffy;
66
67         retvm_if(tinfo == NULL, -1, "Invalid argument: tinfo is NULL\n");
68
69         utime = 0;
70         stime = 0;
71         r = _get_stat_info(tinfo->pid, &utime, &stime);
72         if (r < 0) {
73                 _D("failed\n");
74                 return 0;
75         }
76         /* retvm_if(r < 0, -1, "Failed to get stat info\n"); */
77
78         gettimeofday(&timev, NULL);
79         jiffy = (timev.tv_sec - tinfo->oldtimev.tv_sec) * tick +
80                 ((timev.tv_usec - tinfo->oldtimev.tv_usec) * tick) / 1000000;
81
82         if(utime >= tinfo->oldutime) {
83                 usr = ((double)(utime - tinfo->oldutime) * 100 / jiffy) / ncpu;
84         } else {
85                 usr = 0.0;
86         }
87         if(stime >= tinfo->oldstime) {
88                 sys = ((double)(stime - tinfo->oldstime) * 100 / jiffy) / ncpu;
89         } else {
90                 sys = 0.0;
91         }
92
93         /* _D("per:%lf] %lf %lf/ %u %u/ %u %u/ %u\n",
94            usr+sys, usr, sys, utime, stime, tinfo->oldutime, tinfo->oldstime, jiffy);
95          */
96
97         tinfo->oldutime = utime;
98         tinfo->oldstime = stime;
99         tinfo->oldtimev = timev;
100
101         return usr + sys;
102 }
103
104