1 /*
2 * cpu_test.c -- cpu test application
3 *
4 * Copyright (c) 2017 Rockchip Electronics Co. Ltd.
5 * Author: Panzhenzhuan Wang <randy.wang@rock-chips.com>
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20 /* Get CPU frequency */
21 #include <stdio.h>
22
23 /* error ����������ͷ�ļ�*/
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include "cpu_test.h"
32
33 #define LOG_TAG "cpu_test"
34 #include "common.h"
35 #define CPU_PROC_ERR -55
36
37 #define _CPU_FREQ_TABLE_PATH "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_available_frequencies"
38 #define _CPU_FREQ_TXT "/data/cfg/rk_pcba_test/cpu%d_freq_table.txt"
39 #define _CPU_FREQ_GET "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq"
40 #define _CPU_FREQ_SET "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_setspeed"
41
42 int cpu_num0 = 0;
43 CPU_FREQ cpu_0;
44
45 int cpu_num1 = 0;
46 CPU_FREQ cpu_1;
47
48 int cpu_num2 = 0;
49 CPU_FREQ cpu_2;
50
51 int cpu_num3 = 0;
52 CPU_FREQ cpu_3;
53
54 int cpu_space = 0;
55
56 //��һ�CPU frequency test=========================
57 //===================================================
58 /* ��CPU����Ƶ�ʼ���Ƶ������*/
Insert_list(CPU_FREQ * head,CPU_FREQ * item)59 void Insert_list(CPU_FREQ *head, CPU_FREQ *item)
60 {
61 CPU_FREQ *p1 = NULL, *p0 = NULL;
62
63 p1 = head;
64 p0 = item;
65 p0->next = NULL;
66 if (head == NULL) {
67 head = p0;
68 } else {
69 while (p1 && p1->next)
70 p1 = p1->next;
71
72 if (p1->freq == 0){
73 p1->freq = p0->freq;
74 free(p0); //��Ҫ�ͷ�p0����Ȼitem�ڴ�й©
75 }
76 else
77 p1->next = p0;
78 }
79 }
80
81 /* 1����ȡCPU֧�ֵ�frequency table, �����ؿ���CPUƵ�ʸ���*/
get_cpu_freq_table(int cpu_num,CPU_FREQ * cpu_freq_head)82 int get_cpu_freq_table(int cpu_num, CPU_FREQ *cpu_freq_head)
83 {
84
85 char freq_table_path[512];
86 char freq_table_file[128];
87
88 printf("\n=================function :%s start \tcpu number: %d==================\n",__func__,cpu_num);
89 /* ��ȡCPU number ��Ӧ��frequency table ·��*/
90 memset(freq_table_path, 0, sizeof(freq_table_path));
91 sprintf(freq_table_path, _CPU_FREQ_TABLE_PATH, cpu_num);
92 printf("cpu: %d; freq_table_path is: %s\n",cpu_num,freq_table_path);
93
94 /* ��CPU frequency table���뵽�ļ��� */
95 char cmd[1024];
96 memset(cmd, 0, sizeof(cmd));
97 memset(freq_table_file, 0,sizeof(freq_table_file));
98 sprintf(freq_table_file, _CPU_FREQ_TXT, cpu_num);
99 printf("cpu: %d; freq_table_file is: %s\n",cpu_num,freq_table_file);
100 sprintf(cmd, "busybox cat %s > %s", freq_table_path, freq_table_file);
101 if(system(cmd)<0)
102 {
103 perror("system(cmd)");
104 return -1;
105 }
106 /* �����п��ܵ�CPUƵ�ʼ��뵽������, ����ӡ�����õ�Ƶ�ʸ���*/
107 FILE *fp_read = NULL;
108 char buf[64];
109 CPU_FREQ *new_freq;
110
111 fp_read = fopen(freq_table_file, "r");
112 if (NULL==fp_read) {
113 printf("%s open err:%s\r\n", freq_table_file,strerror(errno));
114 return -1;
115 }
116 //��ȡ�ļ��и�Ƶ��ֵ�������뵽��cpu_freq_headΪͷ��������
117 int freq_num = 0; //����Ƶ�ʸ���
118 while(!feof(fp_read)&&(fp_read!=NULL))
119 {
120 fscanf(fp_read,"%s",buf);
121 new_freq = calloc(1,sizeof(CPU_FREQ));
122 if (new_freq == NULL)
123 {
124 printf("%s:calloc err\r\n", __func__);
125 return -1;
126 break;
127 }
128 new_freq->freq = atoi(buf);
129 printf("cpu frequency num:%d,frequency is %d MHz\r\n",freq_num,new_freq->freq/1000);
130 Insert_list(cpu_freq_head, new_freq);
131 freq_num++;
132 }
133 fclose(fp_read);
134 remove(freq_table_file);
135 printf("cpu %d has %d availble frequencies\n",cpu_num,freq_num);
136 printf("\n=================function :%s finish \tcpu number: %d==================\n",__func__,cpu_num);
137 return freq_num;
138 }
139
140 /* 2����ȡ��ǰǰCPU����Ƶ�ʣ���ӡ���������� */
get_curl_freq(int cpu_num)141 int get_curl_freq(int cpu_num)
142 {
143 FILE *pp;
144 char cmd[512];
145 char cpufreq[64];
146 int cpu_curl_freq =0;
147
148 printf("\n=================function :%s start \tcpu number: %d==================\n",__func__,cpu_num);
149 /* ��ȡCPU 0/1/2/3 ��Ӧ�ĵ�ǰ����frequency·��*/
150 memset(cmd, 0, sizeof(cmd));
151 sprintf(cmd, "busybox cat %s", _CPU_FREQ_GET);
152 sprintf(cmd, cmd, cpu_num);
153 printf("cpu: %d; get current frequency cmd is: %s\n",cpu_num,cmd);
154 pp = popen(cmd, "r");
155
156 //����ļ���ʧ�ܣ������������Ϣ
157 if (!pp)
158 {
159 printf("%s open err:%s\r\n", __func__,strerror(errno)); //ʹ��strerror��������������
160 return -1;
161 }
162 if (fgets(cpufreq, sizeof(cpufreq), pp) == NULL) {
163 printf("popen read from %s is NULL!\n",cmd);
164 pclose(pp);
165 return -1;
166 }
167 pclose(pp);
168 cpu_curl_freq = atoi(cpufreq);
169 printf("cpu��%d ; current frequency is: %d MHz\n",cpu_num,cpu_curl_freq/1000);
170
171 printf("\n=================function :%s finish \tcpu number: %d==================\n",__func__,cpu_num);
172 return cpu_curl_freq;
173 }
174
175 /* 3������CPUһ��֧�ֵ�CPU frequency */
176 //����CPU ģʽ
cpu_set_mode(char * mode)177 int cpu_set_mode(char *mode)
178 {
179 char cmd[512];
180 printf("\n=================== function :%s start======================\n",__func__);
181
182 if (cpu_num0 > 0) {
183 memset(cmd, 0, sizeof(cmd));
184 sprintf(cmd, "echo " "%s" " > %s", mode,_CPU_0_FREQ_GOVERNOR);
185 system(cmd);
186 }
187
188 if (cpu_num1 > 0) {
189 memset(cmd, 0, sizeof(cmd));
190 sprintf(cmd, "echo " "%s" " > %s", mode,_CPU_1_FREQ_GOVERNOR);
191 system(cmd);
192 }
193
194 if (cpu_num2 > 0) {
195 memset(cmd, 0, sizeof(cmd));
196 sprintf(cmd, "echo " "%s" " > %s", mode,_CPU_2_FREQ_GOVERNOR);
197 system(cmd);
198 }
199
200 if (cpu_num3 > 0) {
201 memset(cmd, 0, sizeof(cmd));
202 sprintf(cmd, "echo " "%s" " > %s", mode,_CPU_3_FREQ_GOVERNOR);
203 system(cmd);
204 }
205 printf("\n=================== function :%s finish======================\n",__func__);
206 return 0;
207 }
208
209 //����CPU �б������Ƶ�ʣ��������õ�Ƶ��
set_curl_freq(int cpu_num,int freq_num)210 int set_curl_freq(int cpu_num, int freq_num)
211 {
212 char cmd[512];
213 char CPU_FREQ_SET_PATH[128];
214 CPU_FREQ *cpu_freq;
215 CPU_FREQ *p;
216 int freq_set = 0;
217 printf("\n=================== function :%s start======================\n",__func__);
218
219 //ѡ��CPU_num��Ӧ��Ƶ������
220 switch(cpu_num){
221 case 0:
222 cpu_freq = &cpu_0;break;
223 case 1:
224 cpu_freq = &cpu_1;break;
225 case 2:
226 cpu_freq = &cpu_2;break;
227 case 3:
228 cpu_freq = &cpu_3;break;
229 }
230
231 p = cpu_freq;
232 while(p) {
233 if ((--freq_num) <= 0)
234 break;
235 if (p->next)
236 p = p->next;
237 }
238
239 /* ����CPU Ƶ��*/
240 freq_set = p->freq;
241 memset(cmd, 0, sizeof(cmd));
242 sprintf(CPU_FREQ_SET_PATH,_CPU_FREQ_SET,cpu_num);
243 sprintf(cmd, "echo %d > %s", freq_set, CPU_FREQ_SET_PATH);
244 printf("cpu: %d; set current frequency cmd is: %s\n",cpu_num,cmd);
245 system(cmd);
246
247 printf("\n=================== function :%s finish======================\n",__func__);
248 return freq_set;
249 }
250
251
cpu_test(void * argv)252 void *cpu_test(void *argv)
253 {
254 char cmd[128];
255 int test_flag=0;
256 printf("======= cpu frequency test starting ========\n\n");
257
258 /* 1�����ȴ�ӡ��ÿ��CPU֧�ֵ�CPU frequency table */
259 /* get all freq */
260 printf("********************get cpu frequency table start ***************\n");
261 cpu_num0 = get_cpu_freq_table(0,&cpu_0);
262 cpu_num1 = get_cpu_freq_table(1,&cpu_1);
263 // cpu_num2 = get_cpu_freq_table(2,&cpu_2);
264 // cpu_num3 = get_cpu_freq_table(3,&cpu_3);
265 if(-1 == cpu_num0|| -1== cpu_num1||-1 ==cpu_num2 || -1 ==cpu_num3)
266 {
267 test_flag = -1;
268 goto fail;
269 }
270
271 printf("********************get cpu frequency table finish***************\n\n");
272
273 /* 2��Ȼ���ӡ��ĿǰCPU���е�frequency */
274 printf("********************get current cpu frequency start ***************\n");
275 int cpu_0_freq,cpu_1_freq;
276 cpu_0_freq = get_curl_freq(0);
277 cpu_1_freq = get_curl_freq(1);
278 if(cpu_0_freq == -1 || cpu_1_freq==-1)
279 {
280 test_flag = -1;
281 goto fail;
282 }
283 printf("********************get current cpu frequency finish ***************\n\n");
284
285 /* 3������CPUһ��֧�ֵ�CPU frequency */
286 printf("********************set cpu frequency start ***************\n");
287 /* set mode to userspace */
288 test_flag = cpu_set_mode(_CPU_MODE_USER);
289 int freq_get,freq_set =-1;
290 int cpu_num = 0; //ѡ��CPU 0
291 int freq_num = rand()%cpu_num0; //���ѡ��CPU֧��Ƶ���б��е�һ����ܳ���
292 freq_set = set_curl_freq(cpu_num, freq_num);
293 printf("********************set cpu frequency finish ***************\n");
294
295 /* 4����ȡCPU��frequency�� �鿴�Ƿ������õ�һ��*/
296 freq_get = get_curl_freq(cpu_num);
297 if(freq_set == freq_get)
298 {
299 printf("Cpu frequency set and get is equal; is %d MHz\n",freq_set/1000);
300 }
301 else{
302 printf("Cpu frequency set and get is not equal; set failed\n");
303 printf("Cpu frequency set is :%d MHz\n",freq_set);
304 printf("Cpu frequency get is :%d MHz\n",freq_get);
305 test_flag = -1;
306 goto fail;
307 }
308 printf("======= cpu frequency test success ========\n");
309
310 return (void*)test_flag;
311 fail:
312 printf("======= cpu frequency test failed ========\n");
313
314 return (void*)test_flag;
315 }
316
317 //����������cpu_test
main(int argc,char * argv[])318 int main(int argc, char *argv[])
319 {
320 int test_flag = 0,err_code = 0;
321 char buf[COMMAND_VALUESIZE] = "cpu_test";
322 char result[COMMAND_VALUESIZE] = RESULT_PASS;
323 test_flag = (int)cpu_test(argv[0]);
324 if(test_flag < 0)
325 {
326 strcpy(result,RESULT_FAIL);
327 err_code = CPU_PROC_ERR;
328 }
329 send_msg_to_server(buf, result, err_code);
330 }
331
332
333