Re: Getting the CPU frequency in C

2007-09-09 Thread Erik Trulsson
On Sun, Sep 09, 2007 at 10:50:25PM +0200, Martin Tournoij wrote:
 I'm trying to get the CPU frequency in C:
 
 #include unistd.h
 #include time.h
 #include ctype.h
 #include sys/sysctl.h
 #include stdio.h
 #include sys/time.h
 
 int main()
 {
 int mib[2];
 size_t size;
 struct clockinfo clockrate;
 
 mib[0] = CTL_KERN;
 mib[1] = KERN_CLOCKRATE;
 size = sizeof clockrate;
 sysctl(mib, 2, clockrate, size, NULL, 0);
 
 fprintf(stdout, hz: %i\n, clockrate.hz);
 fprintf(stdout, tick: %i\n, clockrate.tick);
 fprintf(stdout, spare: %i\n, clockrate.spare);
 fprintf(stdout, stathz: %i\n, clockrate.stathz);
 fprintf(stdout, profhz: %i\n, clockrate.profhz);
 
 return 0;
 }
 
 I tried to run this on two machines (one machine with hw.clockrate: 1378 and
 the other 797) and it outputs the same on both:
 hz: 1000
 tick: 1000
 spare: 0
 stathz: 133
 profhz: 666
 
 The profhz value suggest the devil is at work :D although it's probably a some
 stupid mistake on my part :/ Can anyone help?

None of the kern.clockrate entries has any particular relationship with the
CPU clock frequency, so it is not unexpected that you would get the same
output from both machines.

I think looking at hw.clockrate is the most portable you can get.
If your CPU is using Cool'n'Quiet or the Intel equivalent you can also
look at dev.cpu.N.freq for the current frequency.




-- 
Insert your favourite quote here.
Erik Trulsson
[EMAIL PROTECTED]
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Getting the CPU frequency in C

2007-09-09 Thread Martin Tournoij
On Sun 09 Sep 2007 22:09, Erik Trulsson wrote:
 On Sun, Sep 09, 2007 at 10:50:25PM +0200, Martin Tournoij wrote:
  I'm trying to get the CPU frequency in C:
  
  #include unistd.h
  #include time.h
  #include ctype.h
  #include sys/sysctl.h
  #include stdio.h
  #include sys/time.h
  
  int main()
  {
  int mib[2];
  size_t size;
  struct clockinfo clockrate;
  
  mib[0] = CTL_KERN;
  mib[1] = KERN_CLOCKRATE;
  size = sizeof clockrate;
  sysctl(mib, 2, clockrate, size, NULL, 0);
  
  fprintf(stdout, hz: %i\n, clockrate.hz);
  fprintf(stdout, tick: %i\n, clockrate.tick);
  fprintf(stdout, spare: %i\n, clockrate.spare);
  fprintf(stdout, stathz: %i\n, clockrate.stathz);
  fprintf(stdout, profhz: %i\n, clockrate.profhz);
  
  return 0;
  }
  
  I tried to run this on two machines (one machine with hw.clockrate: 1378 and
  the other 797) and it outputs the same on both:
  hz: 1000
  tick: 1000
  spare: 0
  stathz: 133
  profhz: 666
  
  The profhz value suggest the devil is at work :D although it's probably a 
  some
  stupid mistake on my part :/ Can anyone help?
 
 None of the kern.clockrate entries has any particular relationship with the
 CPU clock frequency, so it is not unexpected that you would get the same
 output from both machines.
 
 I think looking at hw.clockrate is the most portable you can get.
 If your CPU is using Cool'n'Quiet or the Intel equivalent you can also
 look at dev.cpu.N.freq for the current frequency.

I got confused because they both have the same name ... do'h

hw.clockrate doesn't seem to available through C(?), exec-ing sysctl
hw.clockrate would work, but that's not very elegant...

dev.cpu.0.freq doesn't seem to exists on my (Athlon) CPU, it
does on my PIII CPU though.
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Getting the CPU frequency in C

2007-09-09 Thread Mel
On Monday 10 September 2007 00:04:45 Martin Tournoij wrote:
 On Sun 09 Sep 2007 22:09, Erik Trulsson wrote:
  On Sun, Sep 09, 2007 at 10:50:25PM +0200, Martin Tournoij wrote:
   I'm trying to get the CPU frequency in C:
  
   #include unistd.h
   #include time.h
   #include ctype.h
   #include sys/sysctl.h
   #include stdio.h
   #include sys/time.h
  
   int main()
   {
   int mib[2];
   size_t size;
   struct clockinfo clockrate;
  
   mib[0] = CTL_KERN;
   mib[1] = KERN_CLOCKRATE;
   size = sizeof clockrate;
   sysctl(mib, 2, clockrate, size, NULL, 0);
  
   fprintf(stdout, hz: %i\n, clockrate.hz);
   fprintf(stdout, tick: %i\n, clockrate.tick);
   fprintf(stdout, spare: %i\n, clockrate.spare);
   fprintf(stdout, stathz: %i\n, clockrate.stathz);
   fprintf(stdout, profhz: %i\n, clockrate.profhz);
  
   return 0;
   }
  
   I tried to run this on two machines (one machine with hw.clockrate:
   1378 and the other 797) and it outputs the same on both:
   hz: 1000
   tick: 1000
   spare: 0
   stathz: 133
   profhz: 666
  
   The profhz value suggest the devil is at work :D although it's probably
   a some stupid mistake on my part :/ Can anyone help?
 
  None of the kern.clockrate entries has any particular relationship with
  the CPU clock frequency, so it is not unexpected that you would get the
  same output from both machines.
 
  I think looking at hw.clockrate is the most portable you can get.
  If your CPU is using Cool'n'Quiet or the Intel equivalent you can also
  look at dev.cpu.N.freq for the current frequency.

 I got confused because they both have the same name ... do'h

 hw.clockrate doesn't seem to available through C(?), exec-ing sysctl
 hw.clockrate would work, but that's not very elegant...

int main()
{
const char mib[] = hw.clockrate;
size_t size = sizeof(int);
int clockrate;

sysctlbyname(mib, (void *)clockrate, size, NULL, 0);

fprintf(stdout, clockrate: %i\n, clockrate);

return 0;
}


-- 
Mel
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Getting the CPU frequency in C

2007-09-09 Thread Erik Trulsson
On Mon, Sep 10, 2007 at 12:04:45AM +0200, Martin Tournoij wrote:
 On Sun 09 Sep 2007 22:09, Erik Trulsson wrote:
  On Sun, Sep 09, 2007 at 10:50:25PM +0200, Martin Tournoij wrote:
   I'm trying to get the CPU frequency in C:
   
   #include unistd.h
   #include time.h
   #include ctype.h
   #include sys/sysctl.h
   #include stdio.h
   #include sys/time.h
   
   int main()
   {
   int mib[2];
   size_t size;
   struct clockinfo clockrate;
   
   mib[0] = CTL_KERN;
   mib[1] = KERN_CLOCKRATE;
   size = sizeof clockrate;
   sysctl(mib, 2, clockrate, size, NULL, 0);
   
   fprintf(stdout, hz: %i\n, clockrate.hz);
   fprintf(stdout, tick: %i\n, clockrate.tick);
   fprintf(stdout, spare: %i\n, clockrate.spare);
   fprintf(stdout, stathz: %i\n, clockrate.stathz);
   fprintf(stdout, profhz: %i\n, clockrate.profhz);
   
   return 0;
   }
   
   I tried to run this on two machines (one machine with hw.clockrate: 1378 
   and
   the other 797) and it outputs the same on both:
   hz: 1000
   tick: 1000
   spare: 0
   stathz: 133
   profhz: 666
   
   The profhz value suggest the devil is at work :D although it's probably a 
   some
   stupid mistake on my part :/ Can anyone help?
  
  None of the kern.clockrate entries has any particular relationship with the
  CPU clock frequency, so it is not unexpected that you would get the same
  output from both machines.
  
  I think looking at hw.clockrate is the most portable you can get.
  If your CPU is using Cool'n'Quiet or the Intel equivalent you can also
  look at dev.cpu.N.freq for the current frequency.
 
 I got confused because they both have the same name ... do'h
 
 hw.clockrate doesn't seem to available through C(?),

Of course it is.  Using sysctlbyname(3) to access it works fine:

#include unistd.h
#include sys/sysctl.h
#include stdio.h

int main()
{
size_t size;
int clockrate;

size = sizeof clockrate;
sysctlbyname(hw.clockrate, clockrate, size, NULL, 0);

fprintf(stdout, hz: %i\n, clockrate);
return 0;
}


hw.clockrate does however only seem to exist on i386 and amd64 architectures
so if you are running on something else you will have to find some
alternative solution. (Parsing the dmesg(8) output?)



 exec-ing sysctl
 hw.clockrate would work, but that's not very elegant...
 
 dev.cpu.0.freq doesn't seem to exists on my (Athlon) CPU, it
 does on my PIII CPU though.

It partly depends on the CPU, and mostly on the BIOS if the cpufreq(4)
kernel module will be activated (assuming it has been loaded in the first
place of course.)


-- 
Insert your favourite quote here.
Erik Trulsson
[EMAIL PROTECTED]
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Getting the CPU frequency in C

2007-09-09 Thread Martin Tournoij
On Mon 10 Sep 2007 00:09, Erik Trulsson wrote:
 On Mon, Sep 10, 2007 at 12:04:45AM +0200, Martin Tournoij wrote:
  On Sun 09 Sep 2007 22:09, Erik Trulsson wrote:
   On Sun, Sep 09, 2007 at 10:50:25PM +0200, Martin Tournoij wrote:
I'm trying to get the CPU frequency in C:

#include unistd.h
#include time.h
#include ctype.h
#include sys/sysctl.h
#include stdio.h
#include sys/time.h

int main()
{
int mib[2];
size_t size;
struct clockinfo clockrate;

mib[0] = CTL_KERN;
mib[1] = KERN_CLOCKRATE;
size = sizeof clockrate;
sysctl(mib, 2, clockrate, size, NULL, 0);

fprintf(stdout, hz: %i\n, clockrate.hz);
fprintf(stdout, tick: %i\n, clockrate.tick);
fprintf(stdout, spare: %i\n, clockrate.spare);
fprintf(stdout, stathz: %i\n, clockrate.stathz);
fprintf(stdout, profhz: %i\n, clockrate.profhz);

return 0;
}

I tried to run this on two machines (one machine with hw.clockrate: 
1378 and
the other 797) and it outputs the same on both:
hz: 1000
tick: 1000
spare: 0
stathz: 133
profhz: 666

The profhz value suggest the devil is at work :D although it's probably 
a some
stupid mistake on my part :/ Can anyone help?
   
   None of the kern.clockrate entries has any particular relationship with 
   the
   CPU clock frequency, so it is not unexpected that you would get the same
   output from both machines.
   
   I think looking at hw.clockrate is the most portable you can get.
   If your CPU is using Cool'n'Quiet or the Intel equivalent you can also
   look at dev.cpu.N.freq for the current frequency.
  
  I got confused because they both have the same name ... do'h
  
  hw.clockrate doesn't seem to available through C(?),
 
 Of course it is.  Using sysctlbyname(3) to access it works fine:
 
 #include unistd.h
 #include sys/sysctl.h
 #include stdio.h
 
 int main()
 {
 size_t size;
 int clockrate;
 
 size = sizeof clockrate;
 sysctlbyname(hw.clockrate, clockrate, size, NULL, 0);
 
 fprintf(stdout, hz: %i\n, clockrate);
 return 0;
 }
 
 
 hw.clockrate does however only seem to exist on i386 and amd64 architectures
 so if you are running on something else you will have to find some
 alternative solution. (Parsing the dmesg(8) output?)
 
 
 
  exec-ing sysctl
  hw.clockrate would work, but that's not very elegant...
  
  dev.cpu.0.freq doesn't seem to exists on my (Athlon) CPU, it
  does on my PIII CPU though.
 
 It partly depends on the CPU, and mostly on the BIOS if the cpufreq(4)
 kernel module will be activated (assuming it has been loaded in the first
 place of course.)

Thought you could only fetch the sysctl values defined in sys/sysctl.h
with sysctl()/sysctlbyname(), apparently not...

Thank you for the help Erik and Mel!
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Getting the CPU frequency in C

2007-09-09 Thread Mel
On Monday 10 September 2007 00:52:08 Martin Tournoij wrote:

 Thought you could only fetch the sysctl values defined in sys/sysctl.h
 with sysctl()/sysctlbyname(), apparently not...

No, I believe those are guaranteed to be available (more to the point: made 
available through /boot/kernel/kernel). Kernel modules (and as such drivers) 
can also expose their settings via sysctl and since they can be loaded 
dynamically and don't have to be part of the base src tree, sys/sysctl.h 
doesn't know about them. For example:
sysctl vfs.fuse.fuse4bsd_version
vfs.fuse.fuse4bsd_version: 0.3.9-pre1

which is /usr/ports/sysutils/fusefs-kmod.

-- 
Mel
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]