Home | History | Annotate | Download | only in libutil
      1 /**
      2  * @file op_cpufreq.c
      3  * get cpu frequency definition
      4  *
      5  * @remark Copyright 2003 OProfile authors
      6  * @remark Read the file COPYING
      7  *
      8  * @author John Levon
      9  * @author Philippe Elie
     10  */
     11 
     12 #include <stdio.h>
     13 #include <stdlib.h>
     14 
     15 #include "op_fileio.h"
     16 
     17 double op_cpu_frequency(void)
     18 {
     19 	double fval = 0.0;
     20 	unsigned long uval;
     21 	char * line = NULL;
     22 
     23 	FILE * fp = op_try_open_file("/proc/cpuinfo", "r");
     24 	if (!fp)
     25 		return 0.0;
     26 
     27 	while (1) {
     28 		line = op_get_line(fp);
     29 
     30 		if (!line)
     31 			break;
     32 
     33 		if (line[0] == '\0') {
     34 			free(line);
     35 			continue;
     36 		}
     37 
     38 		/* x86/parisc/ia64/x86_64 */
     39 		if (sscanf(line, "cpu MHz : %lf", &fval) == 1)
     40 			break;
     41 		/* ppc/ppc64 */
     42 		if (sscanf(line, "clock : %lfMHz", &fval) == 1)
     43 			break;
     44 		/* alpha */
     45 		if (sscanf(line, "cycle frequency [Hz] : %lu", &uval) == 1) {
     46 			fval = uval / 1E6;
     47 			break;
     48 		}
     49 		/* sparc64 if CONFIG_SMP only */
     50 		if (sscanf(line, "Cpu0ClkTck : %lx", &uval) == 1) {
     51 			fval = uval / 1E6;
     52 			break;
     53 		}
     54 		/* mips including loongson2 */
     55 		if (sscanf(line, "BogoMIPS		: %lu", &uval) == 1) {
     56 			fval = uval * 3 / 2;
     57 			break;
     58 		}
     59 		/* s390 doesn't provide cpu freq, checked up to 2.6-test4 */
     60 
     61 		free(line);
     62 	}
     63 
     64 	if (line)
     65 		free(line);
     66 	op_close_file(fp);
     67 
     68 	return fval;
     69 }
     70