3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #define PROC_STAT "/proc/stat"
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"
33 int device_cpu_get_count(int *cpu_cnt)
40 return DEVICE_ERROR_INVALID_PARAMETER;
42 fp = fopen(PROC_CPU_PRESENT, "r");
44 return DEVICE_ERROR_OPERATION_FAILED;
46 ret = fscanf(fp, "%u-%u", &st, &ed);
49 return DEVICE_ERROR_OPERATION_FAILED;
52 return DEVICE_ERROR_NONE;
55 static int _get_systime(device_system_time_s *st)
63 fp = fopen(PROC_STAT, "r");
67 s = fgets(buf, sizeof(buf), fp);
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);
88 int device_cpu_get_system_time(device_system_time_s *time)
91 device_system_time_s st;
94 return DEVICE_ERROR_INVALID_PARAMETER;
96 ret = _get_systime(&st);
98 return DEVICE_ERROR_OPERATION_FAILED;
100 st.total = st.user+st.nice+st.system+st.idle+st.iowait+st.irq+st.softirq;
102 return DEVICE_ERROR_NONE;
105 static int _get_uint(const char *path, unsigned int *val)
114 fp = fopen(path, "r");
118 ret = fscanf(fp, "%u", &num);
127 int device_cpu_get_current_freq(int cpu, unsigned int *cur_freq)
129 char path[FILENAME_MAX];
134 if (cur_freq == NULL)
135 return DEVICE_ERROR_INVALID_PARAMETER;
137 ret = device_cpu_get_count(&count);
138 if (ret != DEVICE_ERROR_NONE || cpu < 0 || cpu >= count)
139 return DEVICE_ERROR_INVALID_PARAMETER;
141 snprintf(path, sizeof(path), PROC_SCALING_CUR_FREQ, cpu);
142 ret = _get_uint(path, &cur);
147 return DEVICE_ERROR_NONE;
150 int device_cpu_get_max_freq(int cpu, unsigned int *max_freq)
152 char path[FILENAME_MAX];
157 if (max_freq == NULL)
158 return DEVICE_ERROR_INVALID_PARAMETER;
160 ret = device_cpu_get_count(&count);
161 if (ret != DEVICE_ERROR_NONE || cpu < 0 || cpu >= count)
162 return DEVICE_ERROR_INVALID_PARAMETER;
164 snprintf(path, sizeof(path), PROC_SCALING_MAX_FREQ, cpu);
165 ret = _get_uint(path, &max);
170 return DEVICE_ERROR_NONE;