int ci_get_cpu_khz(unsigned* cpu_khz_out) { FILE* f; char buf[80]; if( ! ci_cpu_khz ) { /* We only go get the khz if we need to. Obviously it's sensible for * performance, but also we need to do this because we can't call fclose * once the system is fully initialized, since our overridden version of * fclose needs to get the fdtable-lock. (Note: we would ideally just * ensure we always call the 'real' libc fclose from here, but since this * gets linked into the ciapp library, finding real libc is not so easy) * Therefore, it is important that this function get called early, to * ensure that we can't deadlock on ourselves by calling fclose when the * fdtable lock is held. */ f = fopen("/proc/cpuinfo", "r"); if( !f ) return -errno; while( 1 ) { if( !fgets(buf, sizeof(buf), f) ) { fclose (f); return -EIO; } if( try_get_hz(buf, &ci_cpu_khz) ) break; } fclose (f); } if( cpu_khz_out ) *cpu_khz_out = ci_cpu_khz; return 0; }
int ci_get_cpu_khz(unsigned* cpu_khz_out) { FILE* f; char buf[80]; if( ! ci_cpu_khz ) { /* On powerpc /proc/cpuinfo gives reliable information, hence no need to * measure. * On x86 cpuinfo readings might be ugely inaccurate when cpu_scaling * is enabled */ #ifndef __powerpc__ if( ci_measure_cpu_khz(&ci_cpu_khz) ) goto end; else ci_log("Warning measured cpu_khz not stable, querying /proc/cpuinfo"); #endif /* We only go get the khz if we need to. Obviously it's sensible for * performance, but also we need to do this because we can't call fclose * once the system is fully initialized, since our overridden version of * fclose needs to get the fdtable-lock. (Note: we would ideally just * ensure we always call the 'real' libc fclose from here, but since this * gets linked into the ciapp library, finding real libc is not so easy) * Therefore, it is important that this function get called early, to * ensure that we can't deadlock on ourselves by calling fclose when the * fdtable lock is held. */ f = fopen("/proc/cpuinfo", "r"); if( !f ) return -errno; while( 1 ) { if( !fgets(buf, sizeof(buf), f) ) { fclose (f); return -EIO; } if( try_get_hz(buf, &ci_cpu_khz) ) break; } fclose (f); } #ifndef __powerpc__ end: #endif if( cpu_khz_out ) *cpu_khz_out = ci_cpu_khz; return 0; }