RFC PATCH sensors-detect (was Re: tickets needing help)
A couple months ago, I wrote...
Quoted 8 lines · Mark M. Hoffman
Quoted 8 lines
Quoted 8 lines
To really do it properly, it needs to be aware of 2.4 vs. 2.6, due to the fact that the ridiculous PCI device unhiding happens at driver load in the former, at bootup in the latter.
I.e. if we only had 2.6 to worry about, i2c-sis96x would have a single PCI device entry in the table and detection would be nice and generic like all the rest.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Instead, what I should do is yank all the sis96x and sis5595 entries (because that misdetects all 96x) out of that table and write a special- purpose function for detecting them properly.
I'm not a perl wizard (in fact I dislike perl) so I'm inclined to just list both devices and attack the rest later, if ever.
I still don't like perl much, but here's a patch/RFC. It's not at all
complete: I'd just like a second opinion on %pci_refs. The idea is that we'll be able to do this:
if (exists $pci_refs{"1039:0645"}) { ... }In fact, I think @pci_list should have been a hash of references instead of an array. Could it be that sensors-detect is old enough to predate perl5?
Oh, and there's a kernel version function too. Comments?
(I won't commit any of this before the next release.)
Regards,
Signature · 67 lines
Mark M. Hoffman [email protected] Index: prog/detect/sensors-detect =================================RCS file: /home/cvs/lm_sensors2/prog/detect/sensors-detect,v retrieving revision 1.280 diff -u -r1.280 sensors-detect --- prog/detect/sensors-detect 2 Mar 2004 21:05:44 -0000 1.280 +++ prog/detect/sensors-detect 3 Mar 2004 05:22:26 -0000 @@ -1784,7 +1784,7 @@ # PCI ACCESS # ############## -use vars qw(@pci_list); +use vars qw(@pci_list %pci_refs); # This function returns a list of hashes. Each hash has some PCI information # (more than we will ever need, probably). The most important @@ -1806,10 +1806,12 @@ $record->{func} = $dfn & 0x07; $record->{vendid} = $vend >> 16; $record->{devid} = $vend & 0xffff; - push @pci_list,$record; + push @pci_list,$record; + $pci_refs{ sprintf("%04x:%04x",$record->{vendid},$record->{devid}) } + \$record; } close INPUTFILE or return; - return @pci_list; + return (@pci_list, %pci_refs); } # This function returns a list of hashes. Each hash has some PCI @@ -1836,16 +1838,18 @@ $record->{desc} = $desc; } push @pci_list,$record; + $pci_refs{ sprintf("%04x:%04x",$record->{vendid},$record->{devid}) } + \$record; } } close INPUTFILE or return; - return @pci_list; + return (@pci_list, %pci_refs); } sub initialize_proc_pci { - @pci_list = read_proc_dev_pci; - @pci_list = read_proc_pci if not defined @pci_list; + (@pci_list, %pci_refs) = read_proc_dev_pci; + (@pci_list, %pci_refs) = read_proc_pci if not defined @pci_list; die "Can't access either /proc/bus/pci/ or /proc/pci!" if not defined @pci_list; } @@ -4149,6 +4153,12 @@ exit; } +sub kernel_version +{ + `uname -r` =~ /([0-9]+)\.([0-9]+)\.([0-9]+)(.*)/; + return ($1, $2, $3, $4); +} + sub main { my (@adapters,$res,$did_adapter_detection,$detect_others,$adapter);
Quoting "Mark M. Hoffman" <[email protected]>:
Quoted 1 line
A couple months ago, I wrote...
I think I remember I was supposed to help... :/
Quoted 9 lines
I still don't like perl much, but here's a patch/RFC. It's not at all complete: I'd just like a second opinion on %pci_refs. The idea is that we'll be able to do this:
if (exists $pci_refs{"1039:0645"}) { ... }In fact, I think @pci_list should have been a hash of references instead of an array. Could it be that sensors-detect is old enough to predate perl5?
I doubt it. Perl 5 was available for more than 4 full years as the sensors-detect script was created.
More liekly, Frodo was not used to hashes (little people are before they
really start feeling how powerful it is) and would not take care of algoritmic complexity in his choices. That said, it worked for 4 years, so it must not have been that bad.
Review
Quoted 4 lines
Oh, and there's a kernel version function too. Comments? (...)
(quoted diff) +1
-1-use vars qw(@pci_list); +use vars qw(@pci_list %pci_refs);
Why not simply "convert" the old list into the hash, instead of having two variables? You can always get a list from a hash using the "values" perl function.
Once done, we may find that in various places, we are walking the list when we could use "exists" instead, and we will be able to optimize this. I guess this was your intention anyway. Can you point us to the particular places where such changes could be done?
Review
Quoted 5 lines
(quoted diff) +5
-0+sub kernel_version +{ + `uname -r` =~ /([0-9]+)\.([0-9]+)\.([0-9]+)(.*)/; + return ($1, $2, $3, $4); +}
I'd prefer /^(\d+)\.(\d+)\.(\d+)(.*)/ as the regular expression. BTW, do
we really need the fourth field? I don't much see what we will be doing with it, since it's mostly distributor-dependant.
Also, I don't think this should be a function. Instead, I'd define a global variable (an array, obviously) and fill it at the beginning of the script.
We may need functions, on the other hand, for version comparisons. Something as "if (kernel_at_least(2,5,75)) { ... }". The function could look like:
sub kernel_at_least
{
my ($maj, $min, $rev) = @_;
return 1 if $kver[0] > $maj ||
($kver[0] = maj && ($kver[1] > $min ||
($kver[1] = $min && $kver[2] >= $rev))); return 0;
}
(where @kver is the global array containing the kernel version information).
Signature · 2 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/
Quoted 3 lines
Quoting "Mark M. Hoffman" <[email protected]>:
Quoted 2 lines
(quoted diff) +1
-1-use vars qw(@pci_list); +use vars qw(@pci_list %pci_refs);
Quoted 3 lines · Jean Delvare
Why not simply "convert" the old list into the hash, instead of having two variables? You can always get a list from a hash using the "values" perl function.
OK, I did the conversion (attached). I also noticed that I had an extra indirection in there. It's gone now.
Quoted 4 lines
Once done, we may find that in various places, we are walking the list when we could use "exists" instead, and we will be able to optimize this. I guess this was your intention anyway. Can you point us to the particular places where such changes could be done?
No place at all. The special SiS handling will need it though.
Quoted 7 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+sub kernel_version +{ + `uname -r` =~ /([0-9]+)\.([0-9]+)\.([0-9]+)(.*)/; + return ($1, $2, $3, $4); +}I'd prefer /^(\d+)\.(\d+)\.(\d+)(.*)/ as the regular expression. BTW, do
Heh, ok. You begin to see my un-familiarity with Perl...
Quoted 2 lines
we really need the fourth field? I don't much see what we will be doing with it, since it's mostly distributor-dependant.
Doesn't hurt.
Quoted 3 lines
Also, I don't think this should be a function. Instead, I'd define a global variable (an array, obviously) and fill it at the beginning of the script.
OK.
Quoted 8 lines
We may need functions, on the other hand, for version comparisons. Something as "if (
kernel_at_least(2,5,75)) { ... }". The function could look like:sub
kernel_at_least
{
my ($maj, $min, $rev) = @_;
return 1 if $kver[0] > $maj ||
Quoted 7 lines
($kver[0] = maj && ($kver[1] > $min || ($kver[1] = $min && $kver[2] >= $rev)));return 0;
}(where @kver is the global array containing the kernel version information).
Good idea, but I didn't put it in yet.
One other thing: I yanked out the alternate /proc/pci reading routine. Since CVS doesn't support kernel < 2.4 anymore, it seems unecessary.
Comments?
Signature · 174 lines
Mark M. Hoffman [email protected] Index: sensors-detect =================================RCS file: /home/cvs/lm_sensors2/prog/detect/sensors-detect,v retrieving revision 1.281 diff -u -r1.281 sensors-detect --- sensors-detect 3 Mar 2004 20:19:10 -0000 1.281 +++ sensors-detect 4 Mar 2004 06:24:32 -0000 @@ -1765,6 +1765,14 @@ } } +use vars qw(@kernel_version); + +sub initialize_kernel_version +{ + `uname -r` =~ /(\d+)\.(\d+)\.(\d+)(.*)/; + @kernel_version = ($1, $2, $3, $4); +} + ########### # MODULES # ########### @@ -1784,16 +1792,15 @@ # PCI ACCESS # ############## -use vars qw(@pci_list); +use vars qw(%pci_list); -# This function returns a list of hashes. Each hash has some PCI information +# This function returns a hash of hashes. Each hash has some PCI information # (more than we will ever need, probably). The most important # fields are 'bus', 'slot', 'func' (they uniquely identify a PCI device in # a computer) and 'vendid','devid' (they uniquely identify a type of device). -# /proc/bus/pci/devices is only available on late 2.1 and 2.2 kernels. sub read_proc_dev_pci { - my ($dfn,$vend,@pci_list); + my ($dfn,$vend,%pci_list); open INPUTFILE, "/proc/bus/pci/devices" or return; while (<INPUTFILE>) { my $record = {}; @@ -1806,48 +1813,18 @@ $record->{func} = $dfn & 0x07; $record->{vendid} = $vend >> 16; $record->{devid} = $vend & 0xffff; - push @pci_list,$record; - } - close INPUTFILE or return; - return @pci_list; -} - -# This function returns a list of hashes. Each hash has some PCI -# information. The important fields here are 'bus', 'slot', 'func' (they -# uniquely identify a PCI device in a computer) and 'desc' (a functional -# description of the PCI device). If this is an 'unknown device', the -# vendid and devid fields are set instead. -sub read_proc_pci -{ - my @pci_list; - open INPUTFILE, "/proc/pci" or return; - while (<INPUTFILE>) { - my $record = {}; - if (($record->{bus},$record->{slot},$record->{func}) = - /^\s*Bus\s*(\S)+\s*,\s*device\s*(\S+)\s*,\s*function\s*(\S+)\s*:\s*$/) { - my $desc = <INPUTFILE>; - $_ = <INPUTFILE>; - if (($desc =~ /Unknown device/) and - (($record->{vendid},$record->{devid}) = - /^\s*Vendor id=(\S+)\.\s*Device id=(\S+)\.$/)) { - $record->{vendid} = hex $record->{vendid}; - $record->{devid} = hex $record->{devid}; - } else { - $record->{desc} = $desc; - } - push @pci_list,$record; - } + + $pci_list{ sprintf("%04x:%04x",$record->{vendid},$record->{devid}) } + $record; } close INPUTFILE or return; - return @pci_list; + return %pci_list; } sub initialize_proc_pci { - @pci_list = read_proc_dev_pci; - @pci_list = read_proc_pci if not defined @pci_list; - die "Can't access either /proc/bus/pci/ or /proc/pci!" - if not defined @pci_list; + %pci_list = read_proc_dev_pci; + die "Can't access /proc/bus/pci/devices!" if not defined %pci_list; } ##################### @@ -1872,10 +1849,10 @@ sub adapter_pci_detection { - my ($device,$try,@res); + my ($key,$device,$try,@res); print "Probing for PCI bus adapters...\n"; - foreach $device (@pci_list) { + while ( ($key, $device) = each %pci_list) { foreach $try (@pci_adapters) { if ((defined($device->{vendid}) and $try->{vendid} = $device->{vendid} and @@ -3390,7 +3367,7 @@ sub sis5595_isa_detect { my ($addr) = @_; - my ($adapter,$try,$local_try); + my ($key,$adapter,$try,$local_try); my $found = 0; foreach $local_try (@pci_adapters) { if ($local_try->{procid} eq "Silicon Integrated Systems SIS5595") { @@ -3402,7 +3379,7 @@ return if not $found; $found = 0; - foreach $adapter (@pci_list) { + while ( ($key, $adapter) = each %pci_list) { if ((defined($adapter->{vendid}) and $try->{vendid} = $adapter->{vendid} and $try->{devid} = $adapter->{devid} and @@ -3426,7 +3403,7 @@ sub via686a_isa_detect { my ($addr) = @_; - my ($adapter,$try,$local_try); + my ($key,$adapter,$try,$local_try); my $found = 0; foreach $local_try (@pci_adapters) { if ($local_try->{procid} eq "VIA Technologies VT82C686 Apollo ACPI") { @@ -3438,7 +3415,7 @@ return if not $found; $found = 0; - foreach $adapter (@pci_list) { + while ( ($key, $adapter) = each %pci_list) { if ((defined($adapter->{vendid}) and $try->{vendid} = $adapter->{vendid} and $try->{devid} = $adapter->{devid} and @@ -3462,7 +3439,7 @@ sub via8231_isa_detect { my ($addr) = @_; - my ($adapter,$try,$local_try); + my ($key,$adapter,$try,$local_try); my $found = 0; foreach $local_try (@pci_adapters) { if ($local_try->{procid} eq "VIA Technologies VT8231 South Bridge") { @@ -3474,7 +3451,7 @@ return if not $found; $found = 0; - foreach $adapter (@pci_list) { + while ( ($key, $adapter) = each %pci_list) { if ((defined($adapter->{vendid}) and $try->{vendid} = $adapter->{vendid} and $try->{devid} = $adapter->{devid} and @@ -4155,6 +4132,7 @@ initialize_conf; initialize_proc_pci; initialize_modules_list; + initialize_kernel_version; print "\nThis program will help you determine which I2C/SMBus modules you need to\n", "load to use lm_sensors most effectively. You need to have i2c and\n",