Skip to content

Commit

Permalink
usd-util: FreeBSD && MacOSX port of get_mhz
Browse files Browse the repository at this point in the history
Use: "sysctl -n hw.cpufrequency" in MacOSX
and: "sysctl -n dev.cpu.0.freq" in FreeBSD
to retrieve the mhz of the running system

Signed-off-by: John Chandy <john.chandy@uconn.edu>
Signed-off-by: Boaz Harrosh <bharrosh@panasas.com>
  • Loading branch information
joc02012 authored and Boaz Harrosh committed Mar 15, 2010
1 parent 3fc608c commit 4039042
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions osd-util/osd-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -636,9 +636,29 @@ double median(double *v, int N)
double get_mhz(void)
{
FILE *fp;
char s[1024], *cp;
int found = 0;
char s[1024];
double mhz;
#if defined(__FreeBSD__) || defined(__APPLE__)
#ifdef __APPLE__
#define hw_cpufrequency "hw.cpufrequency"
const long div_by = 1000000L;
#else
#define hw_cpufrequency "dev.cpu.0.freq"
const long div_by = 1L;
#endif

if (!(fp = popen("sysctl -n " hw_cpufrequency, "r")))
osd_error_fatal("Cannot call sysctl");

if (fgets(s, sizeof(s), fp) == NULL ||
sscanf(s, "%lf", &mhz) != 1) {
osd_error_fatal("got no hw.cpufrequency sysctl value");
}
mhz /= div_by;
pclose(fp);
#else /* defined(__FreeBSD__) || defined(__APPLE__) .e.g Linux */
char *cp;
int found = 0;

if (!(fp = fopen("/proc/cpuinfo", "r")))
osd_error_fatal("Cannot open /proc/cpuinfo");
Expand All @@ -658,6 +678,7 @@ double get_mhz(void)
osd_error_fatal("\"cpu MHz\" line not found\n");

fclose(fp);
#endif /* else defined(__FreeBSD__) || defined(__APPLE__) */

return mhz;
}
Expand Down

0 comments on commit 4039042

Please sign in to comment.