سؤال

Do all of the BSDs have a common way to get CPU stats (time spend in kernel, idle, user, etc) like Linux has in the first line of /proc/stats?

I'm trying to detect a system under load and not perform computations when it is active (above a certain threshold). I've figured out how to do it on Windows/Linux.

I think I'll need to use sysctl somehow. I've found this:

http://osdir.com/ml/darwin-kernel/2009-06/msg00036.html

Is this cross-platform? I'm trying to target these BSDs in particular:

  • Mac OS 10.6+
  • FreeBSD 8+
  • OpenBSD 5+

I'm only marginally familiar with the BSDs (I played with FreeBSD for a bit as a teenager), so any help would be appreciated.

هل كانت مفيدة؟

المحلول

Nope - those things are not quite that portable. Though

      sysctl vm.loadavg 
      vm.loadavg: { 0.98 1.39 1.50 }

happens to work for the above set and is somewhat comparable.

If you need something simple yet broad - I'd consider the parsing of the command uptime (that behind the scenes relies on sysctls which are subtly different between platforms) - and extracting the load numbers:

      uptime
      11:38AM  up 18 days, 15:50, 2 users, load averages: 0.04, 0.01, 0.00

That is pretty cross platform - and has very similar semantics.

If that is not good enough - what you could do is ensure that the system runs an SNMP agent which exposes the host resources MIB. That gets you an easy platform independent way of getting load. If you really want to do this from your code - consider taking this bit of code from net-snmp - as it has the various flavours of /proc and sysctl for a lot of platforms.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top