hholtmann / smcFanControl

Control the fans of every Intel Mac to make it run cooler
GNU General Public License v2.0
2.41k stars 303 forks source link

smc: support writing FP values like fan speed (tested on MBP 16,1) #108

Open Blaisorblade opened 4 years ago

Blaisorblade commented 4 years ago

Needed to help with https://github.com/hholtmann/smcFanControl/issues/77 — to set fan speeds by hand. TODO:

I've only tested this on my machine and I haven't programmed in C for a long while, but I've tried to be careful.

Example usage:

$ sudo ./smc -k F0Md -w 01
$ sudo ./smc -k F1Md -w 01
$ sudo ./smc -k F0Tg -p 3000
$ sudo ./smc -k F1Tg -p 3000
$ ./smc -f
Total fans in system: 2

Fan #0:
    Actual speed : 2980
    Minimum speed: 1836
    Maximum speed: 5297
    Safe speed   : 0
    Target speed : 3000
    Mode         : forced

Fan #1:
    Actual speed : 3007
    Minimum speed: 1700
    Maximum speed: 4905
    Safe speed   : 0
    Target speed : 3000
    Mode         : forced

$ ./smc -l|grep F.Tg
  F0Tg  [flt ]  3000 (bytes 00 80 3b 45)
  F1Tg  [flt ]  3000 (bytes 00 80 3b 45)
Blaisorblade commented 4 years ago

(For some minimal testing code for this, see https://github.com/Blaisorblade/smcFanControl/pull/1).

jlehrer1 commented 4 years ago

Wow, it works! I'm amazed. Thank you very much! Does the mode have to be set back to automatic to stop the fan control?

Blaisorblade commented 4 years ago

@jlehrer1 I'm not sure as I've only studied the parsing code, but it seems so, as before. I did disable force mode via sudo ./smc -k F0Md -w 00; sudo ./smc -k F1Md -w 00. Sample output:

$ ./smc -f
Total fans in system: 2

Fan #0:
    Actual speed : 5012
    Minimum speed: 1836
    Maximum speed: 5297
    Safe speed   : 0
    Target speed : 5000
    Mode         : forced

Fan #1:
    Actual speed : 4900
    Minimum speed: 1700
    Maximum speed: 4905
    Safe speed   : 0
    Target speed : 4900
    Mode         : forced
$ sudo ./smc -k F1Md -w 00
$ sudo ./smc -k F0Md -w 00
$ ./smc -f
Total fans in system: 2

Fan #0:
    Actual speed : 2562
    Minimum speed: 1836
    Maximum speed: 5297
    Safe speed   : 0
    Target speed : 1836
    Mode         : auto

Fan #1:
    Actual speed : 1812
    Minimum speed: 1700
    Maximum speed: 4905
    Safe speed   : 0
    Target speed : 1700
    Mode         : auto
jlehrer1 commented 4 years ago

@Blaisorblade Interesting. Resetting the values back to 00 keeps the mode in forced for me, but it could be because I have Macs Fan Control running as well. Also, I'm testing this on a Macbook Pro 13" and it works.

Edit: Yes its Macs Fan Control. Closing the app / deleting the smc write resets the fans to auto. Thanks again for this commit, I'm excited to get working.

Blaisorblade commented 4 years ago

@jlehrer1 which MBP13''? Do you have production year or model number? And remember combining fan control apps is not really supported; make sure to not trigger bugs that melt your computer :-)

jlehrer1 commented 4 years ago

@Blaisorblade heh yeah I won't, although I'm not too worried. Neither program can override the fans hardware speed limits. It's from 2020.

jlehrer1 commented 4 years ago

@Blaisorblade The only thing I'm not sure of is why setting the fan in forced mode is a different key than the fan key? Per your example, F0Tg vs F0Md. How did you know to set F0Md? Thanks again for this.

Blaisorblade commented 4 years ago

How did you know to set F0Md?

That's from https://github.com/hholtmann/smcFanControl/issues/77#issuecomment-660781379 and https://github.com/hholtmann/smcFanControl/issues/77#issuecomment-661623897.

The harder part was F0Tg; I took that from the README, but the instructions were contradicted by the code; basically, SMCPrintFans knows how to turn the raw data from SMC into actual values, and I just wrote the inverse, also looking at #105 and the surrounding code. It seems smcFanControl itself is fixed, but the smc command wasn't extended — but I can't tell since I haven't tried to build smcFanControl.

The only thing I'm not sure of is why setting the fan in forced mode is a different key than the fan key?

No clue, that's what other contributors figured out :-)

quackduck commented 3 years ago

@Blaisorblade would this work with setting minimum values too? My friend with a 2020 Intel MacBook Air tried it out and it seems the minimum value didn't change.

Blaisorblade commented 3 years ago

@quackduck Unfortunately I forgot the details, I've been using other (paid) tools, and I lack the time to try to help further; this patch fixes just one piece and wasn't tested on other laptops.

quackduck commented 3 years ago

Alright, but would this work on F0Mn on the laptop you tested this on?

Blaisorblade commented 3 years ago

@quackduck But IIRC the output of smc -l is helpful; if the relevant "fan speed" field is in flt mode (floating point), you need this patch to encode the values correctly.

Blaisorblade commented 3 years ago

okay, I tried a moment... but the minimum speed doesn't seem settable. Indeed, even the README implies the minimum is read-only. Concrete test:

$ ./smc -l|grep F0Mn
  F0Mn  [flt ]  1836 (bytes 00 80 e5 44)
$ sudo ./smc -k F0Mn -p 3000
Writing:         [flt ]  3000 (bytes 00 80 3b 45)

$ ./smc -l|grep F0Mn
  F0Mn  [flt ]  1836 (bytes 00 80 e5 44)
$ ./smc -f
Total fans in system: 2

Fan #0:
    Actual speed : 1838
    Minimum speed: 1836
    Maximum speed: 5297
    Safe speed   : 0
    Target speed : 1836
    Mode         : auto

Fan #1:
    Actual speed : 1707
    Minimum speed: 1700
    Maximum speed: 4905
    Safe speed   : 0
    Target speed : 1700
    Mode         : auto
quackduck commented 3 years ago

That's strange because the min speed is settable on older macs. The smcFanControl GUI works by setting min fan speeds.

Blaisorblade commented 3 years ago

IIRC the smc readme already acknowledges this interface change, from minimum to target...

quackduck commented 3 years ago

Hmm, is there a way for me to set a speed that can still be overridden by smc (as per default thermal management)?

Say, for example, I set the fan speed to 1200 when the CPU is at 105 C. Of course, the smc would by default set the fan speed to something higher, so I want it such that if the speed I set it to is less than the default, the fans will run at the default speed.

quackduck commented 3 years ago

Also, doesn't the GUI smcFanControl work by setting min speeds? Right, @hholtmann?

ruipatcheco commented 1 year ago

I've just tested this on an M1 13" macbook pro and it works brilliantly, thanks @Blaisorblade !

OleksandrKucherenko commented 1 year ago

Why its not merged yet?