TODO: "dynamic" sysfs callbacks (plus 2D
Quoted 13 lines · Jean Delvare
Hi Jim,
Quoted 10 lines
once again with the email-harvesting / communication/coordination.
<quoting Jean, I think>
BTW, do you have any plan to convert the asb100 driver to use the "dynamic" sysfs callbacks? This would kill some more macros, and shrink the driver size even more.
Heres a shorter, more accurate list, it finds the macro-repeated functions. They're the biggest opportunity for code-shrink; N->1 for function bodies.
[jimc at harpo hwmon-stuff]$ grep -n 'static ssize_t' w83-1/drivers/hwmon/*.c |grep '##' | cut -d: -f1 | sort -u
w83-1/drivers/hwmon/adm1021.cw83-1/drivers/hwmon/adm1025.cw83-1/drivers/hwmon/adm1031.cw83-1/drivers/hwmon/asb100.cw83-1/drivers/hwmon/ds1621.cw83-1/drivers/hwmon/fscher.cw83-1/drivers/hwmon/fscpos.cw83-1/drivers/hwmon/gl518sm.cw83-1/drivers/hwmon/gl520sm.cw83-1/drivers/hwmon/lm75.cw83-1/drivers/hwmon/lm77.cw83-1/drivers/hwmon/lm78.cw83-1/drivers/hwmon/lm80.cw83-1/drivers/hwmon/lm85.cw83-1/drivers/hwmon/lm87.cw83-1/drivers/hwmon/lm92.cw83-1/drivers/hwmon/max1619.cw83-1/drivers/hwmon/sis5595.cw83-1/drivers/hwmon/smsc47b397.cw83-1/drivers/hwmon/smsc47m1.cw83-1/drivers/hwmon/via686a.cw83-1/drivers/hwmon/w83627ehf.cw83-1/drivers/hwmon/w83627hf.cw83-1/drivers/hwmon/w83781d.cw83-1/drivers/hwmon/w83791d.cw83-1/drivers/hwmon/w83792d.c
Quoted 7 lines
Quoted 2 lines
drivers/hwmon/adm1026.c- patch on hold, needs tester.I had to discard it from my stack, it no more applies after Mark M. Hoffman's patch fixing unchecked return values.
Ack.
Quoted 11 lines
I do not consider the dynamic sysfs callbacks conversion as "must be done". Of course drivers are nicer and smaller when done, but this is a significant work, and it needs complete testing, contrary to the unchecked return values fixes.
So I wouldn't bother converting a driver unless you can test it. See what happened with the adm1026 driver, you converted it long ago, nobody ever tested it, and now the patch is discarded as it conflicts with other changes.
Indeed - Nobody knew the patch was there, and I just discovered a potential tester
for it on-list just last week. Hence this email ;-) .. and happily, this conversation.
Im hoping (against experience) that by doing these:
- identify drivers using macro repeated function defns
- show how to find the lines to look at (the above grep)
- discuss the technique ( 'dynamic' doesnt explain it, at least for me)
.. that I/we can lower the barrier to participation.
The task is still somewhat harder than the average janitorial patch, (which may attract would-be hackers), and of course there are the testing / validation issues (must have hardware).
<aside> Testing by personal invitation has worked for me, at least once. Yuan Mu from Winbond took a compile-tested patch from me, and finished it. I aint too proud to beg ;-) .. (cues up some Rolling Stones ..)
Quoted 44 lines
Quoted 34 lines
*Deeper 'dynamic's are possible*
The above folds multiple callbacks on 1 dimension (index) to reduce code footprint, but more are possible.
following 2 steps above:
1. function now relys on being passed a struct
sensor_device_attribute_2which has 2 additional parameters; index, and nr. This example uses 2nd parameter to distinguish which attribute of the sensor is being requested (index still chooses 1 of N sensors)static
ssize_tshow_fan(struct device *dev, struct device_attribute *devattr, char *buf){ struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr); int idx = attr->index; int func = attr->nr; struct pc87360_data *data = pc87360_update_device(dev); unsigned res = -1; switch(func) { case FN_FAN_INPUT: res = FAN_FROM_REG(data->fan[idx], FAN_DIV_FROM_REG(data->fan_status[idx])); break; case FN_FAN_MIN: res = FAN_FROM_REG(data->fan_min[idx], FAN_DIV_FROM_REG(data->fan_status[idx])); break; case FN_FAN_STATUS: res = FAN_STATUS_FROM_REG(data->fan_status[idx]); break;Quoted 9 lines
case FN_FAN_DIV: res = FAN_DIV_FROM_REG(data->fan_status[idx]); break; default: printk(KERN_ERR "unknown attr fetch\n"); } return sprintf(buf, "%u\n", res); }
Quoted 7 lines
As I mentioned earlier during the vt1211 review, I'm no big fan of this approach. You end up aggregating many features which don't share much code. It's still reasonable in your example above, but I think it has gone to an extreme in one case in the vt1211 driver. Let's not jump from one extreme to the other.
As with everything else, its a judgement call - Juerg used it for show_in, set_in, show_temp, set_temp, show_fan, set_fan, show|set_pwm He did not for many others. In most places he did so, the combo-callback was still ~30 lines, a pretty good indicator of clarity.
Also, the 'assignment' of callbacks (by SENSOR_ATTR*) is hugely flexible; one could use a combo-callback to handle show-ops, but stick with dedicated callbacks
for store-ops. This is perhaps subtle at 1st, but a 2 line comment could address that.
Quoted 8 lines
I do agree that having a single callback for displaying
fanN_inputandfanN_minmakes full sense, and this suggests a second level of indexing. But beyond that point I think we have to be reasonable. I don't mind for new drivers, authors should stay relatively free, however I strongly object to converting all our perfectly working drivers to use that approach.
Uhm .. "strongly object" ?
Is it your intent to discourage patches of this nature ? ( and by inference, other minor improvements ?)
I know you've got bigger fish to fry ( conversion to platform, etc - todo list forthcoming ? ;-)
and that review of combo-callback conversions is a non-zero cost upon you, but surely you want to give objective incentives for folks to get involved.
For my part, your acceptance of my previous work is a big factor in my hanging around here, and Id hope you'd regard those reviews as a successful investment,
beyond the X kB trimmed from 1 driver. Even half-baked reviews (such as the ones
Ive offered) can result in brush-clearing (something our president is good at ;-)
Quoted 9 lines
Beyond the readability of the code, there are some performance issues to consider. For example I wonder how the code above interacts with the CPU cache, compared to 1-level-indexed callbacks, in the typical "sensors" scenario. I don't really have the time to investigate this, unfortunately. Switch/case is usually not recommended in performance terms, even though I'd expect gcc to optimize it relatively nicely if the "func" values are chosen wisely.
Quoted 7 lines
Quoted 2 lines
NB - technique saves ~ 9% object code on 1 driver.
It really depends on where you come from for a given driver. But yes there's definitely a size reduction.
Just to clarify, for anyone else reading this far..
That 9% number is using *2D combo-callbacks*, on top of a driver (pc87360) that already employed the *dynamic callbacks* technique (see list at top), which, iirc, saved 30-40%. Of course, thats enormously dependent upon the driver, but I feel compelled to point it out, esp as "strongly object" could be misconstrued to apply to both kinds of patches, not just 2D-combo-callbacks.
Quoted 10 lines
The 2-index variants may be a problem because not all callbacks need 2 parameters and mixing 0, 1 and 2-indexes attributes can become a mess. Using
sysfs_create_groupmay help in that respect, because it manipulates pointers rather than the attributes directly.I have been thinking of a "standard hwmon attribute" which would combine our current sensor and
sensor_2attributes. I'll think again about it after we are done with the unchecked return values.
interesting. I played briefly with enums, but couldnt figure out how to do it
as a specialization/derivation of struct sensor_device_attribute. maybe anonymous unions have a place here..
Thanks Jean
jimc
Hi Jim,
Quoted 5 lines
Im hoping (against experience) that by doing these:
- identify drivers using macro repeated function defns
- show how to find the lines to look at (the above grep)
- discuss the technique ( 'dynamic' doesnt explain it, at least for me)
Agreed, a better name would probably have been "parametrized" or similar, but for some reason we came up with "dynamic" originally.
Quoted 4 lines
.. that I/we can lower the barrier to participation.
The task is still somewhat harder than the average janitorial patch, (which may attract would-be hackers), and of course there are the testing / validation issues (must have hardware).
Sure, that'd be nice. Maybe you can really make it a kernel janitors project? You might be more successful that way than by assuming that people on the lm-sensors list will pick your plan and implement it. As you said, experience suggests this won't happen, as much as we'd both like it to change.
At any rate, this should only happen after we're done with the unchecked return status changes, and preferably after the individual alarm files are added, because these two changes have higher priority.
Quoted 3 lines
<aside> Testing by personal invitation has worked for me, at least once. Yuan Mu from Winbond took a compile-tested patch from me, and finished it. I aint too proud to beg ;-) .. (cues up some Rolling Stones ..)
Yep, sometimes it works, and that was nice.
Quoted 9 lines
Quoted 5 lines
As I mentioned earlier during the vt1211 review, I'm no big fan of this approach. You end up aggregating many features which don't share much code. It's still reasonable in your example above, but I think it has gone to an extreme in one case in the vt1211 driver. Let's not jump from one extreme to the other.
As with everything else, its a judgement call - Juerg used it for
show_in,set_in,show_temp,set_temp,show_fan,set_fan, show|set_pwm He did not for many others.
Well the list above covers almost all the callbacks, only missing are the automatic fan speed regulation and misc ones.
Quoted 2 lines
In most places he did so, the combo-callback was still ~30 lines, a pretty good indicator of clarity.
True, the only one I think was not so nice was set_pwm, as it reaches 90 lines.
Quoted 5 lines
Also, the 'assignment' of callbacks (by SENSOR_ATTR*) is hugely flexible; one could use a combo-callback to handle show-ops, but stick with dedicated callbacks
for store-ops. This is perhaps subtle at 1st, but a 2 line comment could address that.
I agree, that makes sense, as the show-ops are usually very simple, and the store-ops usually more complex.
Quoted 9 lines
Quoted 6 lines
I do agree that having a single callback for displaying
fanN_inputandfanN_minmakes full sense, and this suggests a second level of indexing. But beyond that point I think we have to be reasonable. I don't mind for new drivers, authors should stay relatively free, however I strongly object to converting all our perfectly working drivers to use that approach.Uhm .. "strongly object" ?
Is it your intent to discourage patches of this nature ?
Yes.
Quoted 1 line
(and by inference, other minor improvements ?)
No. The change proposed above is not a "minor improvement" by any mean, it is a complete rewrite of the callback functions, for no technical benefit that I can see.
To summarize my idea, I do encourage people to get rid of all the ugly macros the older hardware monitoring drivers have, assuming they have the hardware to test their change (or can find someone who does). I do encourage them to use 1-index "dynamic" callbacks because they are straightforward and do only require minimum changes. Going beyong that requires larger changes, design decisions, and is more risky; not something for newcomers.
Quoted 2 lines
I know you've got bigger fish to fry ( conversion to platform, etc - todo list forthcoming ? ;-)
Indeed. i2c core conversion to the driver model, unchecked return values, i2c-isa/platform conversion, individual alarm files, all these are big and have higher priority. People could help with some of these, by the way.
Quoted 2 lines
and that review of combo-callback conversions is a non-zero cost upon you, but surely you want to give objective incentives for folks to get involved.
If people really want to get involved, they will. If they are looking for easy little changes to start with and learn how the kernel development works, there are the kernel janitors and kernel mentors projects for that, and I don't plan to duplicate their effort. I couldn't afford it anyway.
Quoted 3 lines
For my part, your acceptance of my previous work is a big factor in my hanging around here, and Id hope you'd regard those reviews as a successful investment,
Sure I do, and I know that my reviews might sound harsh sometimes and might discourage the newcomers. I try to explain my reasons everytime so that it doesn't sound too arbitrary or personal. But well, we want high quality code in the kernel, and we want code that follows guidelines, some written, some unfortunately not.
I am also aware that my reviews are often delayed beyond what contributors would like (see the vt1211 driver for an extreme example), but my time and energy is limited. The only two ways around it are:
* I get paid for the reviews. Now that I am a Suse employee, it is partly true, but my main task there is L3 support, not kernel development, so I only have a few hours every month that I can use for kernel development. This month I used this time for the vt1211 review.
* Others help me with the reviews.
Ideally both would happen.
Quoted 3 lines
beyond the X kB trimmed from 1 driver. Even half-baked reviews (such as the ones Ive offered) can result in brush-clearing (something our president is good at ;-)
Absolutely. I encourage everyone here to review other's patches. You don't have to be me to do it, and every contribution in that area can help lower my workload, which I think can only help the project.
Quoted 7 lines
Quoted 3 lines
I have been thinking of a "standard hwmon attribute" which would combine our current sensor and
sensor_2attributes. I'll think again about it after we are done with the unchecked return values.interesting. I played briefly with enums, but couldnt figure out how to do it as a specialization/derivation of struct
sensor_device_attribute. maybe anonymous unions have a place here..
enums don't have anything to do here, unions could. But my original intent was just one simple structure which would fit all needs. The idea came to me as I realized that the struct sensor_device_attribute_2 is _smaller_ than the struct sensor_device_attribute.
Signature · 1 line
Jean Delvare