device: Merge public api on Tizen 2.3 into tizen branch
[platform/core/api/device.git] / src / cpu.c
1 /*
2  * capi-system-device
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <assert.h>
23 #include "device.h"
24 #include "common.h"
25
26 #define PROC_STAT               "/proc/stat"
27
28 #define PROC_CPU_PRESENT                                "/sys/devices/system/cpu/present"
29 #define PROC_SCALING_CUR_FREQ                   "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq"
30 #define PROC_SCALING_MAX_FREQ                   "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq"
31
32
33 int device_cpu_get_count(int *cpu_cnt)
34 {
35         FILE *fp;
36         int ret;
37         int st, ed;
38
39         if (cpu_cnt == NULL)
40                 return DEVICE_ERROR_INVALID_PARAMETER;
41
42         fp = fopen(PROC_CPU_PRESENT, "r");
43         if (!fp)
44                 return DEVICE_ERROR_OPERATION_FAILED;
45
46         ret = fscanf(fp, "%u-%u", &st, &ed);
47         fclose(fp);
48         if (ret != 2)
49                 return DEVICE_ERROR_OPERATION_FAILED;
50
51         *cpu_cnt = ed+1;
52         return DEVICE_ERROR_NONE;
53 }
54
55 static int _get_systime(device_system_time_s *st)
56 {
57         FILE *fp;
58         char buf[4096];
59         char *s;
60
61         assert(st);
62
63         fp = fopen(PROC_STAT, "r");
64         if (!fp)
65                 return -1;
66
67         s = fgets(buf, sizeof(buf), fp);
68         fclose(fp);
69         if (!s)
70                 return -1;
71
72         s = strchr(buf, ' ');
73         if (!s)
74                 return -1;
75
76         s++;
77         st->user = strtol(s, &s, 10);
78         st->nice = strtol(s, &s, 10);
79         st->system = strtol(s, &s, 10);
80         st->idle = strtol(s, &s, 10);
81         st->iowait = strtol(s, &s, 10);
82         st->irq = strtol(s, &s, 10);
83         st->softirq = strtol(s, &s, 10);
84
85         return 0;
86 }
87
88 int device_cpu_get_system_time(device_system_time_s *time)
89 {
90         int ret;
91         device_system_time_s st;
92
93         if (time == NULL)
94                 return DEVICE_ERROR_INVALID_PARAMETER;
95
96         ret = _get_systime(&st);
97         if (ret < 0)
98                 return DEVICE_ERROR_OPERATION_FAILED;
99
100         st.total = st.user+st.nice+st.system+st.idle+st.iowait+st.irq+st.softirq;
101         *time = st;
102         return DEVICE_ERROR_NONE;
103 }
104
105 static int _get_uint(const char *path, unsigned int *val)
106 {
107         FILE *fp;
108         unsigned int num;
109         int ret;
110
111         assert(path);
112         assert(val);
113
114         fp = fopen(path, "r");
115         if (!fp)
116                 return -1;
117
118         ret = fscanf(fp, "%u", &num);
119         fclose(fp);
120         if (ret != 1)
121                 return -1;
122
123         *val = num;
124         return 0;
125 }
126
127 int device_cpu_get_current_freq(int cpu, unsigned int *cur_freq)
128 {
129         char path[FILENAME_MAX];
130         int ret;
131         int count;
132         unsigned int cur;
133
134         if (cur_freq == NULL)
135                 return DEVICE_ERROR_INVALID_PARAMETER;
136
137         ret = device_cpu_get_count(&count);
138         if (ret != DEVICE_ERROR_NONE || cpu < 0 || cpu >= count)
139                 return DEVICE_ERROR_INVALID_PARAMETER;
140
141         snprintf(path, sizeof(path), PROC_SCALING_CUR_FREQ, cpu);
142         ret = _get_uint(path, &cur);
143         if (ret < 0)
144                 cur = 0;
145
146         *cur_freq = cur;
147         return DEVICE_ERROR_NONE;
148 }
149
150 int device_cpu_get_max_freq(int cpu, unsigned int *max_freq)
151 {
152         char path[FILENAME_MAX];
153         int ret;
154         int count;
155         unsigned int max;
156
157         if (max_freq == NULL)
158                 return DEVICE_ERROR_INVALID_PARAMETER;
159
160         ret = device_cpu_get_count(&count);
161         if (ret != DEVICE_ERROR_NONE || cpu < 0 || cpu >= count)
162                 return DEVICE_ERROR_INVALID_PARAMETER;
163
164         snprintf(path, sizeof(path), PROC_SCALING_MAX_FREQ, cpu);
165         ret = _get_uint(path, &max);
166         if (ret < 0)
167                 max = 0;
168
169         *max_freq = max;
170         return DEVICE_ERROR_NONE;
171 }