From 09f3418c1a7a77bb8ffb215c0f905587f579cc95 Mon Sep 17 00:00:00 2001 From: Matthew Kent Date: Wed, 12 Aug 2009 22:51:48 -0700 Subject: [PATCH 1/2] Add linux collection of cpu min/max. Make cpu mhz default the current mhz. --- bindings/SigarWrapper.pm | 12 +++++++++++- bindings/ruby/examples/cpu_info.rb | 4 +++- include/sigar.h | 2 ++ src/os/linux/linux_sigar.c | 19 ++++++++++++++++++- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/bindings/SigarWrapper.pm b/bindings/SigarWrapper.pm index 5801f70e..6f79a5c3 100644 --- a/bindings/SigarWrapper.pm +++ b/bindings/SigarWrapper.pm @@ -471,9 +471,19 @@ use vars qw(%classes %cmds); }, { name => 'mhz', type => 'Int', - desc => 'CPU speed', + desc => 'Current CPU speed', plat => 'AFHLSW' }, + { + name => 'mhz_max', type => 'Int', + desc => 'Maximum CPU speed', + plat => 'L' + }, + { + name => 'mhz_min', type => 'Int', + desc => 'Maximum CPU speed', + plat => 'L' + }, { name => 'cache_size', type => 'Long', desc => 'CPU cache size', diff --git a/bindings/ruby/examples/cpu_info.rb b/bindings/ruby/examples/cpu_info.rb index f68a5a80..a38dec8d 100644 --- a/bindings/ruby/examples/cpu_info.rb +++ b/bindings/ruby/examples/cpu_info.rb @@ -11,6 +11,8 @@ puts num.to_s + " total CPUs.." infos.each do |info| puts "Vendor........" + info.vendor puts "Model........." + info.model - puts "Mhz..........." + info.mhz.to_s + puts "Current Mhz..." + info.mhz.to_s + puts "Maximum Mhz..." + info.mhz_max.to_s + puts "Minimum Mhz..." + info.mhz_min.to_s puts "Cache size...." + info.cache_size.to_s end diff --git a/include/sigar.h b/include/sigar.h index d5f5d6a2..003ede19 100644 --- a/include/sigar.h +++ b/include/sigar.h @@ -188,6 +188,8 @@ typedef struct { char vendor[128]; char model[128]; int mhz; + int mhz_max; + int mhz_min; sigar_uint64_t cache_size; int total_sockets; int total_cores; diff --git a/src/os/linux/linux_sigar.c b/src/os/linux/linux_sigar.c index 1d3a9e1e..008c9bc3 100644 --- a/src/os/linux/linux_sigar.c +++ b/src/os/linux/linux_sigar.c @@ -1598,7 +1598,23 @@ static void get_cpuinfo_max_freq(sigar_cpu_info_t *cpu_info, int num) sigar_file2str(max_freq, max_freq, sizeof(max_freq)-1); if (status == SIGAR_OK) { - cpu_info->mhz = atoi(max_freq) / 1000; + cpu_info->mhz_max = atoi(max_freq) / 1000; + } +} + +static void get_cpuinfo_min_freq(sigar_cpu_info_t *cpu_info, int num) +{ + int status; + char min_freq[PATH_MAX]; + snprintf(min_freq, sizeof(min_freq), + "/sys/devices/system/cpu/cpu%d" + "/cpufreq/cpuinfo_min_freq", num); + + status = + sigar_file2str(min_freq, min_freq, sizeof(min_freq)-1); + + if (status == SIGAR_OK) { + cpu_info->mhz_min = atoi(min_freq) / 1000; } } @@ -1624,6 +1640,7 @@ int sigar_cpu_info_list_get(sigar_t *sigar, info = &cpu_infos->data[cpu_infos->number]; get_cpuinfo_max_freq(info, cpu_infos->number); + get_cpuinfo_min_freq(info, cpu_infos->number); info->total_cores = sigar->ncpu; info->cores_per_socket = sigar->lcpu; From dcddb5f642983138c51bd2703d0852142e08ab08 Mon Sep 17 00:00:00 2001 From: Matthew Kent Date: Wed, 12 Aug 2009 23:41:06 -0700 Subject: [PATCH 2/2] Untested stab at cpu mhz min/max support for Darwin. --- bindings/SigarWrapper.pm | 28 ++++++++++++------------- src/os/darwin/darwin_sigar.c | 40 ++++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 16 deletions(-) diff --git a/bindings/SigarWrapper.pm b/bindings/SigarWrapper.pm index 6f79a5c3..f1895157 100644 --- a/bindings/SigarWrapper.pm +++ b/bindings/SigarWrapper.pm @@ -462,44 +462,44 @@ use vars qw(%classes %cmds); { name => 'vendor', type => 'String', desc => 'CPU vendor id', - plat => 'AFLHSW' + plat => '*' }, { name => 'model', type => 'String', desc => 'CPU model', - plat => 'AFLHSW' + plat => '*' }, { name => 'mhz', type => 'Int', desc => 'Current CPU speed', - plat => 'AFHLSW' + plat => '*' }, { name => 'mhz_max', type => 'Int', desc => 'Maximum CPU speed', - plat => 'L' + plat => 'DL' }, { name => 'mhz_min', type => 'Int', desc => 'Maximum CPU speed', - plat => 'L' + plat => 'DL' }, { name => 'cache_size', type => 'Long', desc => 'CPU cache size', - plat => 'AL' + plat => 'ADL' }, { - name => 'total_cores', type => 'Int', - desc => 'Total CPU cores (logical)', + name => 'total_cores', type => 'Int', + desc => 'Total CPU cores (logical)', }, { - name => 'total_sockets', type => 'Int', - desc => 'Total CPU sockets (physical)', + name => 'total_sockets', type => 'Int', + desc => 'Total CPU sockets (physical)', }, { - name => 'cores_per_socket', type => 'Int', - desc => 'Number of CPU cores per CPU socket', + name => 'cores_per_socket', type => 'Int', + desc => 'Number of CPU cores per CPU socket', }, ], Uptime => [ @@ -2070,8 +2070,8 @@ EOF my $member = $field->{member} || $name; my $desc = $field->{desc} || $name; (my $jname = $name) =~ s/_(\w)/\u$1/g; - my $getter = "get\u$jname"; - $jname = jname($jname); + my $getter = "get\u$jname"; + $jname = jname($jname); my $sig = qq("$field_types{$type}"); my $set = "JENV->Set${type}Field"; my $get = "JENV->Get${type}Field"; diff --git a/src/os/darwin/darwin_sigar.c b/src/os/darwin/darwin_sigar.c index 38c80958..43baaa3b 100644 --- a/src/os/darwin/darwin_sigar.c +++ b/src/os/darwin/darwin_sigar.c @@ -2237,6 +2237,8 @@ int sigar_file_system_usage_get(sigar_t *sigar, #ifdef DARWIN #define CTL_HW_FREQ "hw.cpufrequency" +#define CTL_HW_FREQ_MAX "hw.cpufrequency_max" +#define CTL_HW_FREQ_MIN "hw.cpufrequency_min" #else /* XXX FreeBSD 5.x+ only? */ #define CTL_HW_FREQ "machdep.tsc_freq" @@ -2246,7 +2248,7 @@ int sigar_cpu_info_list_get(sigar_t *sigar, sigar_cpu_info_list_t *cpu_infos) { int i; - unsigned int mhz; + unsigned int mhz, mhz_min, mhz_max; int cache_size=SIGAR_FIELD_NOTIMPL; size_t size; char model[128], vendor[128], *ptr; @@ -2257,24 +2259,48 @@ int sigar_cpu_info_list_get(sigar_t *sigar, #if defined(DARWIN) { - int mib[] = { CTL_HW, HW_CPU_FREQ }; + int mib[2]; + mib[0] = CTL_HW; + + mib[1] = HW_CPU_FREQ; size = sizeof(mhz); if (sysctl(mib, NMIB(mib), &mhz, &size, NULL, 0) < 0) { mhz = SIGAR_FIELD_NOTIMPL; } + mib[1] = HW_CPU_FREQ_MAX; + size = sizeof(mhz_max); + if (sysctl(mib, NMIB(mib), &mhz_max, &size, NULL, 0) < 0) { + mhz_max = SIGAR_FIELD_NOTIMPL; + } + mib[1] = HW_CPU_FREQ_MIN; + size = sizeof(mhz_min); + if (sysctl(mib, NMIB(mib), &mhz_max, &size, NULL, 0) < 0) { + mhz_min = SIGAR_FIELD_NOTIMPL; + } } #elif defined(__FreeBSD__) if (sysctlbyname(CTL_HW_FREQ, &mhz, &size, NULL, 0) < 0) { mhz = SIGAR_FIELD_NOTIMPL; } + /* TODO */ + mhz_max = SIGAR_FIELD_NOTIMPL; + mhz_min = SIGAR_FIELD_NOTIMPL; #else /*XXX OpenBSD*/ mhz = SIGAR_FIELD_NOTIMPL; + mhz_max = SIGAR_FIELD_NOTIMPL; + mhz_min = SIGAR_FIELD_NOTIMPL; #endif if (mhz != SIGAR_FIELD_NOTIMPL) { mhz /= 1000000; } + if (mhz_max != SIGAR_FIELD_NOTIMPL) { + mhz_max /= 1000000; + } + if (mhz_min != SIGAR_FIELD_NOTIMPL) { + mhz_min /= 1000000; + } size = sizeof(model); #ifdef __OpenBSD__ @@ -2297,6 +2323,14 @@ int sigar_cpu_info_list_get(sigar_t *sigar, /* freebsd4 */ mhz = sigar_cpu_mhz_from_model(model); } + /* XXX not sure */ + if (mhz_max == SIGAR_FIELD_NOTIMPL) { + mhz_max = 0; + } + if (mhz_min == SIGAR_FIELD_NOTIMPL) { + mhz_min = 0; + } + #ifdef DARWIN size = sizeof(vendor); @@ -2351,6 +2385,8 @@ int sigar_cpu_info_list_get(sigar_t *sigar, sigar_cpu_model_adjust(sigar, info); info->mhz = mhz; + info->mhz_max = mhz_max; + info->mhz_min = mhz_min; info->cache_size = cache_size; info->total_cores = sigar->ncpu; info->cores_per_socket = sigar->lcpu;