2026年2月7日 星期六

Linux的電源管理TLP(for tlp 1.9)

  我記得我幾個月前寫過關於TLP設定教學的文章,總共兩篇,我就不附上連結了,因為在我撰文的當下,最新的TLP 1.9版有了翻天覆地的改變,首先是增加了可以在桌面環境的選單選擇電源模式(例如效能模式、平衡模式、省電模式),這一舉跟上了power-profiles-deamon或是tuned(tlp>tuned>power-profiles-deamon,懶人就裝tuned,省心)。我在此統一寫一篇設定教學的文章,順便作爲記錄。

安裝(以debian爲例)

  • Debian Trixie

    #使用backports倉庫安裝最新版tlp
    sudo apt install -t tlp tlp-rdw tlp-pd


  • Debian Testing 

    sudo apt install tlp tlp-rdw tlp-pd

設定

  同樣地,我們先用tlp-stat -p來查看CPU的電源狀態、sudo tlp-stat -g查看GPU電源狀態、sudo tlp-stat -b查看電池狀態,這個步驟是要大致掌握電腦可用的電源模式。下圖給出輸出範例。

tlp-stat -p

sudo tlp-stat -g

sudo tlp-stat -b

  使用「sudo nano /etc/tlp.conf」進入nano編輯器界面,我將介紹新版的使用邏輯。原文我會複製貼上在說明下面,用斜體表示且每一行開頭會有「#」。同時,我也會附上我的設定值(裡面加底線就是我有取消注解或是有調整的)。我在此不會全部說明,僅挑我覺得、有玩過、有在用的設定。說實在的,在我花了很多時間看了下裡面的設定,我發現基本不用調整,預設的設定就很棒了。

  • 設定邏輯:根據下文說明,這是指我們可以在參數的名稱後面加上_AC、_BAT、_SAV,分別表示針對接上電源、使用電池、使用省電模式。換言之,我們可以等一下自定參數時,加上對應的後綴。若沒有後綴,則使用預設的設定。建議是三個情況都設定。

    我們先簡單講一下TLP三個檔次的運作邏輯。效能模式是指我們手動制定「效能模式」,或是套用文件上有修改的「_AC結尾的參數且接上電源」,舉個例子,我現在有接上電源,現在會自動使用效能模式,假如我有修改tlp.conf這個文件,則會套用_AC結尾的參數;同理,平衡模式
    是指我們手動制定「平衡模式」,或是套用文件上有修改的「_BAT結尾的參數且使用電池」,舉個例子,我筆電離電就會套用平衡模式,假如我有修改tlp.conf這個文件,則會套用_BAT結尾的參數;省電模式是只可以手動啓用,才會使用省電模式,假如我們有修改tlp.conf這個文件,則會套用_SAV結尾的參數。

    舉例來說,我們要設定DISK_IDLE_SECS這個參數,則「DISK_IDLE_SECS_AC=」是指針對電源的參數,以此類推。

    # Power Profiles: a part of TLP's parameters is divided into two or three
    # groups:
    # - performance: parameters ending in _AC are used when AC power is
    #   connected or when the command 'tlp performance' is run.
    # - balanced: parameters ending in _BAT are used when operating
    #   on battery power or when the command 'tlp balanced' is run.
    # - power-saver: parameters ending in _SAV are used when the command
    #   'tlp power-saver' is run. If there is no _SAV parameter available
    #   for a feature, the _BAT parameter will be used instead.
    # - Any remaining parameters not divided apply to all power profiles.

     
  • 編輯須知:如果後面出現一樣的參數,則以最後一個出現的參數爲主,簡單來說,第一行和地五行出現一樣的參數,則以第五行爲主;若要使得自己的參數設定生效,則需要取消注解(將該行的#刪掉);參數(「=」後面的值)最好是加上英文的雙引號「 " " 」把值給包住,若要完全禁用,則只要引號內留空即可。預設全部加上「#」即爲使用初始設定,不套用tlp.conf的自定義參數

    # Please note:
    # - If parameters are specified more than once, the last occurrence takes
    #   precedence. This also means that any parameters defined here will take
    #   precedence over any drop-ins.
    # - You can however, append values to a parameter already defined as intrinsic
    #   default or in a previously read file: use PARAMETER+="add values".
    # - Important: all parameters are disabled here. Remove the leading '#' if you
    #   want to enable a feature without a default or if you want to set a value
    #   other than the default.
    # - Parameters must always be specified for all power profiles, i.e. in the
    #   AC, BAT and SAV category (where applicable). If you omit one of them,
    #   the missing profile will receive its value from another profile, since
    #   a change will only occur if different values are defined.
    # - To completely disable a parameter, use PARAMETER="".

  • 預設值:裡面的「Default」代表預設值,如果沒有寫就表示使用Linux內核或是硬體的預設值。

    # Legend for defaults:
    # - Default *: intrinsic default that is effective when the parameter is
    #   missing or the line has a leading #'.
    # - Default <none>: do nothing or use kernel/hardware defaults.

  • 關於電池:受限於電池模組的驅動,目前僅特定型號的筆電或是Lenovo ThinkPads筆電支持設定充電限制百分比(我的筆電不支持)。

    # Battery Care -- Charge thresholds
    # Charging starts when the charger is connected and the charge level
    # is below the start threshold. Charging stops when the charge level
    # is above the stop threshold.
    # Required hardware: Lenovo ThinkPads and other laptop brands are driven
    #   via specific plugins:
    # - Use the tlp-stat -b command to see if a plugin for your hardware is
    #   active and to look up vendor-specific threshold values. Some
    #   laptops support only 1 (on)/0 (off) instead of a percentage level.
    # - If your hardware supports a start *and* a stop threshold, you must
    #   specify both, otherwise TLP will refuse to apply the single threshold.
    # - If your hardware supports only a stop threshold, set the start
    #   value to 0.
    # - The names of the batteries shown by tlp-stat -b don't have to match
    #   the _BAT0 or _BAT1 parameter qualifiers. Please refer to [2]
    #   to see which qualifier applies to which battery.
    # For further explanation and all vendor specific details refer to
    # [1] https://linrunner.de/tlp/settings/battery.html
    # [2] https://linrunner.de/tlp/settings/bc-vendors.html


  • TLP_ENABLE:決定是否要啓用TLP,這當然是設爲1(啓用)。

    # tlp - Parameters for power saving

    # Set to 0 to disable, 1 to enable TLP.
    # Default: 1

    #TLP_ENABLE=1


  • TLP_DISABLE_DEFAULTS:宣示預設策略,設爲1代表該文件(tlp.conf)裡面取消注解的參數會優先被引用

    # Set to 1 to deactivate all intrinsic defaults of TLP. This means that
    # TLP only applies settings that have been explicitly activated i.e.
    # parameters without a leading '#'.
    # Notes:
    # - Helpful if one wants to use only selected features of TLP
    # - After activation, use tlp-stat -c to display your effective configuration

    #TLP_DISABLE_DEFAULTS=1


  • TLP_WARN_LEVEL:這是指如果設定的參數無效可以怎麼做,這個我就不改,保持預設即可

    # Control how warnings about invalid settings are issued:
    #   0=disabled
    #   1=background tasks (boot, resume, change of power source) report to syslog
    #   2=shell commands report to the terminal (stderr)
    #   3=combination of 1 and 2
    # Default: 3

    #TLP_WARN_LEVEL=3


  • TLP_MSG_COLORS:這是示警的顏色,我保持預設。
  • TLP_AUTO_SWITCH:這是指要不要智慧/自動切換電源檔位,我保持預設

    # Control automatic switching of the power profile when connecting or removing
    # the charger, when booting the system or when executing 'tlp start':
    #   0=disabled - never switch, use TLP_DEFAULT_MODE if configured
    #   1=auto  - always switch, select performance on AC and
    #             balanced on battery power.
    #   2=smart - do not switch if the following profiles were active previously:
    #             power-saver or balanced on AC resp.
    #             power-saver or performance on battery power.
    # Note: the same applies if the charger was connected/removed during suspend.
    # Default: 2


    #TLP_AUTO_SWITCH=2

  • TLP_DEFAULT_MODE:這是指如果我們將TLP_AUTO_SWITCH設爲0,代表關閉自動切換電源檔位,此時我們得要手動指定預設的電源檔位,筆電會推薦是SAV或是BAL,但我有打開,所以就不動了。 

    # Power profile to use when automatic switching is disabled
    # (TLP_AUTO_SWITCH=0), profile is locked (TLP_PERSISTENT_DEFAULT=1)
    # or no power supply is detected:
    #   PRF=performance, BAL=balanced, SAV=power-saver.
    # Note: legacy values AC and BAT continue to work. They are mapped to
    # PRF and BAL, respectively.
    # Default: <none>

    #TLP_DEFAULT_MODE=BAL

  • TLP_PERSISTENT_DEFAULT:這是指要不要固定電源檔位,需要的話再手動切換,就相當於TLP_AUTO_SWITCH設爲0且TLP_DEFAULT_MODE有手動指定。如果有設定TLP_PERSISTENT_DEFAULT,則會忽略TLP_AUTO_SWITCH的設定值,並且依照TLP_DEFAULT_MODE來指定電源檔位。

    # Lock power profile:
    #   0=profile depends on automatic switching,
    #   1=profile is locked to TLP_DEFAULT_MODE (TLP_AUTO_SWITCH is ignored).
    # Default: 0

    #TLP_PERSISTENT_DEFAULT=0


  • TLP_PS_IGNORE:這是當電腦無法正確判斷現在筆電是用電池或接電源時,預設的電源模式,我推薦設定這個,我設爲電池模式。

    # Power supply classes to ignore when determining power profile:
    #  AC, USB, BAT.
    # Separate multiple classes with spaces.
    # Note: try on laptops where operation mode AC/BAT is incorrectly detected.
    # Default: <none>

    TLP_PS_IGNORE="BAT"


  • DISK_IDLE_SECS_ON:這是Linux對於硬碟的做法,系統會偏好把要寫入的資料放在記憶體,當硬碟需要讀取資料時(此時硬碟不得不轉動),會把記憶體的資料順便寫入。如果是使用固態硬碟(SSD),則建議設爲0(也可以設大於0,此時會因爲資料已經在記憶體堆成一個連續區塊而些微減少寫入放大);如果是機械硬碟(HDD),則推薦離電模式設爲2~5秒。如果這個值大於0(假定是n秒鐘),當電腦瞬間斷電,則至少會損失斷電前n秒鐘的資料。

    # Seconds laptop mode has to wait after the disk goes idle before doing a
    # sync. Non-zero value enables, zero disables laptop mode.
    # Default: 0 (AC), 2 (BAT)

    DISK_IDLE_SECS_ON_AC=0
    DISK_IDLE_SECS_ON_BAT=2
    DISK_IDLE_SECS_ON_SAV=2

  • MAX_LOST_WORK_SECS:資料在程式被關閉後最多可以留在記憶體多少時間,設的越大,越能夠減少啓動硬碟的頻率,進而達到省電的效果,同樣地,會減少因為斷電而丟失資料的風險。

    # Dirty page values (timeouts in secs).
    # Default: 15 (AC), 60 (BAT)


    MAX_LOST_WORK_SECS_ON_AC=15
    MAX_LOST_WORK_SECS_ON_BAT=60
    MAX_LOST_WORK_SECS_ON_SAV=120


  • CPU_DRIVER_OPMODE:這是設定CPU的策略調節的驅動,我是用intel_pstate,所以就都使用active。如果是使用passive模式,則是用AHCI這個通用的電源管理驅動來管理,可以參考:〈我的筆電的電源策略調整——TLP〉裡面「預備知識」一節。

    # Select a CPU scaling driver operation mode.
    # Intel CPU with intel_pstate driver:
    #   active, passive.
    # AMD Zen 2 or newer CPU with amd-pstate driver as of kernel 6.3/6.4(*):
    #   active, passive, guided(*).
    # Default: <none>
    #CPU_DRIVER_OPMODE_ON_AC=active
    #CPU_DRIVER_OPMODE_ON_BAT=active
    #CPU_DRIVER_OPMODE_ON_SAV=active


  • CPU_SCALING_GOVERNOR:設定CPU頻率的檔位,我是接上電池就用performance,其他就powersave

    # Select a CPU frequency scaling governor.
    # Intel CPU with intel_pstate driver or
    # AMD CPU with amd-pstate driver in active mode ('amd-pstate-epp'):
    #   performance, powersave(*).
    # Intel CPU with intel_pstate driver in passive mode ('intel_cpufreq') or
    # AMD CPU with amd-pstate driver in passive or guided mode ('amd-pstate') or
    # Intel, AMD and other CPU brands with acpi-cpufreq driver:
    #   conservative, ondemand(*), userspace, powersave, performance, schedutil(*)
    # Use tlp-stat -p to show the active driver and available governors.
    # Important:
    #   Governors marked (*) above are power efficient for *almost all* workloads
    #   and therefore kernel and most distributions have chosen them as defaults.
    #   You should have done your research about advantages/disadvantages *before*
    #   changing the governor.
    # Default: <none>


    CPU_SCALING_GOVERNOR_ON_AC=performance
    CPU_SCALING_GOVERNOR_ON_BAT=powersave
    CPU_SCALING_GOVERNOR_ON_SAV=powersave

  • CPU_SCALING_MAX_FREQ:可以在 tlp-stat -p查看可以調整的頻率,我的這個intel平臺是不能調整頻率的。下圖會給出AMD平臺可以調整的有效參數,如果有這行有效參數,則可以考慮調整,我就不調了。
    只要有紅色區塊那行,我們就可以設定有效頻率。

    # Set the min/max frequency available for the scaling governor.
    # Possible values depend on your CPU. For available frequencies see
    # the output of tlp-stat -p.
    # Notes:
    # - Min/max frequencies must always be specified for both AC *and* BAT
    # - Not recommended for use with the intel_pstate driver, use
    #   CPU_MIN/MAX_PERF_ON_AC/BAT below instead
    # Default: <none>


    #CPU_SCALING_MIN_FREQ_ON_AC=0   
    #CPU_SCALING_MAX_FREQ_ON_AC=0    
    #CPU_SCALING_MIN_FREQ_ON_BAT=0   
    #CPU_SCALING_MAX_FREQ_ON_BAT=0   
    #CPU_SCALING_MIN_FREQ_ON_SAV=0
    #CPU_SCALING_MAX_FREQ_ON_SAV=0


  • CPU_ENERGY_PERF:設定CPU的模式,接電、離電、省電,我依序是設平衡偏向性能、平衡偏向省電、省電。我這臺筆電的風扇管理策略不佳,容易拉高轉速,所以我就不使用performance。

    # Set CPU energy/performance policies EPP and EPB:
    #   performance, balance_performance, default, balance_power, power.
    # Values are given in order of increasing power saving.
    # Requires:
    # * Intel CPU
    #   EPP: Intel Core i 6th gen. or newer CPU with intel_pstate driver
    #   EPB: Intel Core i 2nd gen. or newer CPU with intel_pstate driver
    #   EPP and EPB are mutually exclusive: when EPP is available, Intel CPUs
    #   will not honor EPB. Only the matching feature will be applied by TLP.
    # * AMD Zen 2 or newer CPU
    #   EPP: amd-pstate driver in active mode ('amd-pstate-epp') as of kernel 6.3
    # Default: balance_performance (AC), balance_power (BAT), power (SAV)

    CPU_ENERGY_PERF_POLICY_ON_AC=balance_performance
    CPU_ENERGY_PERF_POLICY_ON_BAT=balance_power
    CPU_ENERGY_PERF_POLICY_ON_SAV=power


  • CPU_MIN_PERF、CPU_MAX_PERF:是設定CPU的佔用率,我覺得預設就可以了,依照電腦實際狀況設定,設的太低會容易卡頓。WIndows平臺也是推薦設定,可以參考〈新!TLP電源管理,適用於Intel平臺〉裡面的「彩蛋」一節

    # Set Intel CPU P-state performance: 0..100 (%).
    # Limit the max/min P-state to control the power dissipation of the CPU.
    # Values are stated as a percentage of the available performance.
    # Requires Intel Core i 2nd gen. or newer CPU with intel_pstate driver.
    # Default: <none>

    CPU_MIN_PERF_ON_AC=0
    CPU_MAX_PERF_ON_AC=100
    CPU_MIN_PERF_ON_BAT=0
    CPU_MAX_PERF_ON_BAT=70
    CPU_MIN_PERF_ON_SAV=0
    CPU_MAX_PERF_ON_SAV=60


  • CPU_BOOST_ON_AC:是否允許最大加速頻率,我一般只會在接電時許可

    # Set the CPU "turbo boost" (Intel) or "core performance boost" (AMD) feature:
    #   0=disable, 1=allow.
    # Allows to raise the maximum frequency/P-state of some cores if the
    # CPU chip is not fully utilized and below it's intended thermal budget.
    # Note: a value of 1 does *not* activate boosting, it just allows it.
    # Default: <none>

    CPU_BOOST_ON_AC=1
    CPU_BOOST_ON_BAT=0
    CPU_BOOST_ON_SAV=0


  • NMI_WATCHDOG:用於偵測是否有問題在瘋狂在內核態中循環,使得其他程序無法被分配、使用資源,這個就不設定,保持預設不啓用
  • PLATFORM_PROFILE:設定電腦整體的電源模式,我保持預設。

    # Select platform profile:
    #   performance, balanced, low-power.
    # Controls system operating characteristics around power/performance levels,
    # thermal and fan speed. Values are given in order of increasing power saving.
    # Note: check the output of tlp-stat -p to determine availability on your
    # hardware and additional profiles such as: balanced-performance, quiet, cool.
    # Default: performance (AC), balanced (BAT), low-power (SAV)

    #PLATFORM_PROFILE_ON_AC=performance
    #PLATFORM_PROFILE_ON_BAT=balanced
    #PLATFORM_PROFILE_ON_SAV=low-power

     
  •  MEM_SLEEP:設定電腦睡眠模式,deep就是電腦睡眠時,只有記憶體有電,其他硬體不過電,這是正統的睡眠模式;idle是指除了記憶體的周邊硬體也會抱有低量電源,這是微軟最近提倡的模式(有叫做S0),特點是喚醒快,缺點就是耗電。一般來說,我當離電就用deep模式,如果是最新的Ultra處理器或是AMD的Ryzen AI系列,用idle模式是可以考慮的。注意的是,睡眠的模式是以啓用睡眠的當下電源模式來處理,舉例來說,我接電時按下睡眠,然後拔掉電池,仍然是s2idle模式,不會切到deep,所以如果會需要長時間運送筆電,可先離電睡眠再放入背包帶着走,比較省電。

    # System suspend mode:
    #   s2idle: Idle standby - a pure software, light-weight, system sleep state,
    #   deep: Suspend to RAM - the whole system is put into a low-power state,
    #     except for memory, usually resulting in higher savings than s2idle.
    # CAUTION: changing suspend mode may lead to system instability and even
    # data loss. As for the availability of different modes on your system,
    # check the output of tlp-stat -s. If unsure, stick with the system default
    # by not enabling this.
    # Default: <none>

    MEM_SLEEP_ON_AC=s2idle
    MEM_SLEEP_ON_BAT=deep


  • RESTORE_DEVICE_STATE_ON_STARTUP:決定每次電腦開機是否要維持上次關機前的無線裝置的狀態。舉例來說,我現在只有開wifi,重新啓動過後會預設打開wifi和藍牙,如果我將此參數設爲1,關機前開(或關)哪些無線模組,下次開機就會保持開(或關)哪些無線模組。

    # Restore radio device state (Bluetooth, WiFi, WWAN) from previous shutdown
    # on system startup: 0=disable, 1=enable.
    # Note: the parameters DEVICES_TO_DISABLE/ENABLE_ON_STARTUP/SHUTDOWN below
    # are ignored when this is enabled.
    # Default: 0

    RESTORE_DEVICE_STATE_ON_STARTUP=1


  • DEVICES_TO_DISABLE_ON_STARTUP:指定開機後哪些無線模組要關閉。

    # Radio devices to disable on startup: bluetooth, nfc, wifi, wwan.
    # Separate multiple devices with spaces.
    # Default: <none>

    #DEVICES_TO_DISABLE_ON_STARTUP="bluetooth nfc wifi wwan"


  • DEVICES_TO_ENABLE_ON_STARTUP:指定開機後哪些無線模組要開啓。

    # Radio devices to enable on startup: bluetooth, nfc, wifi, wwan.
    # Separate multiple devices with spaces.
    # Default: <none>

    #DEVICES_TO_ENABLE_ON_STARTUP="wifi"


  • DEVICES_TO_DISABLE_ON_XX_CONNECT:假定連上wifi,就要停用哪些功能。如果你的筆電有有線網路口,是用無線網路上網、有線網路口接網路影印機,建議全部留空。

    # Radio devices to disable on connect.

    DEVICES_TO_DISABLE_ON_LAN_CONNECT=""
    DEVICES_TO_DISABLE_ON_WIFI_CONNECT=""
    DEVICES_TO_DISABLE_ON_WWAN_CONNECT=""

    # Radio devices to enable on disconnect.

    DEVICES_TO_ENABLE_ON_LAN_DISCONNECT=""
    DEVICES_TO_ENABLE_ON_WIFI_DISCONNECT=""
    DEVICES_TO_ENABLE_ON_WWAN_DISCONNECT=""


參考資料

沒有留言:

張貼留言