Hprofile

From Gentoo Wiki
Jump to: navigation, search
External resources

sys-apps/hprofile is a little application that can be used to manage multiple profiles be it hardware or software. The following subsection are just examples on what could be done with hprofile.

To be able to to go further, one should, of course, merge the package.

root # emerge --ask sys-apps/hprofile

Contents

Hardware profiles

Preparation

Important
The following sections assume that you have a patched hprofile init service. So grab the following patch if you want to make any use of the following hardware profiles.
Filehprofile.initd.patch

--- /etc/init.d/hprofile	2012-07-04 02:01:29.861618809 +0200
+++ /etc/init.d/hprofile	2012-07-04 02:03:49.713870416 +0200
@@ -2,24 +2,26 @@
 # Copyright 1999-2002 Gentoo Technologies, Inc.
 # Distributed under the terms of the GNU General Public License, v2 or later
 
-
 depend() {
-	before modules
+	after modules
 }
 
 start() {
-	profile=$(/usr/local/sbin/hpdet boot)
-	ebegin "Selecting hardware profile ${profile}"
-	/usr/local/sbin/hprofile boot
+	local vgap=$(/usr/sbin/hpdet vga)
+	local pwrp=$(/usr/sbin/hpdet disk)
+	ebegin "Selecting hardware profile vga.$vgap and power.$pwrp"
+	/usr/sbin/hprofile vga 
+	/usr/sbin/hprofile power
 	eend $?
 }
 
 stop() {
-	profile=$(/usr/local/sbin/hprofile -c boot)
-	ebegin "Stopping hardware profile ${profile}"
-	/usr/local/sbin/hprofile -s boot
+	local vgap=$(/usr/sbin/hprofile -c vga)
+	local pwrp=$(/usr/sbin/hprofile -c disk)
+	ebegin "Stopwrping hardware profile vga.$vgap and power.$pwrp"
+	/usr/sbin/hprofile -s vga
+	/usr/sbin/hprofile -s power
 	eend $?
 }
 
-
 # vim:ts=4
Note
Every script file `/etc/hprofile/profiles/$profile', `ptest', `stop' and `post-start' should have the executable bit set.

Power

When thinking a better to have different dynamic power profiles that could dynamically switched depending on hardware of software state is not straightforward especially for power management which could depend on many software e.g. sys-power/powertop, sys-power/cpufrequtils, sys-apps/hdparm to name a few. So how could you get hardware profiles and switch between them dynamically? And how to get everything together? How many deamons to start depending on what hardware or software state?

That sum up quite a few considerations to what a user can face when trying to build a nice setup with power management in mind. [S]he can remember of Windows(tm) days power management profiles if [s]he ever used it. Or else, [s]he did not any thought about it because [s]he used to run a GNU Linux or BSD based distribution with everything putted together and does not know how to begin with if [s]he is not satisfied with what [s]he gets.

Now sys-power/powertop-2 grew up to be quite a good piece of software. If you're runnng an Intel based platform, you can get access to pretty much everything one would expect if trying to build power manager profiles. But the package does not ship with a daemon nor with a configuration or profile file which PowerTOP could pick up when launched in the next restart. One has to launch it again and again in a terminal and enable power management for every power manageable hardware.

Now, it will a bit long to go pick enough power management tuning that a software like PowerTOP use to be able to manage almost everything. This is just a beginning to that. One can use acpid to switch between different hardware profiles depending on hardware state.

This basic profile will try to implement Hprofile#Disk, CPU (see Hprofile#Extra_resources, network interfaces and ALSA drivers power saving capabilities.

One would need the following files to set up basic power management profiles.

File/etc/hprofile/profiles/power/profiles

adp
bat
dyn
med
quiet
Note
adp stand for AC adapter, bat for battery, dyn for dynamic (performance profile), med for medium and quiet for quiet of courses.
File/etc/hprofile/profiles/power/default

adp
File/etc/hprofile/profiles/power/ptest

#!/bin/sh
if [ -e "/sys/class/power_supply/ADP1/online" ]; then
	status=$(cat /sys/class/power_supply/ADP1/online)
	case "$status" in
		1) profile="adp";;
		0) profile="bat";;
	esac
fi

echo "${profile:-adp}"
File/etc/hprofile/profiles/power/scripts/adp.start

#!/bin/sh
hprofile disk.adp
for c in $(ls -d /sys/devices/system/cpu/cpu[0-9]*); do
	echo ondemand >$c/cpufreq/scaling_governor
done
echo 200 >/sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor
echo 0   >/sys/devices/system/cpu/sched_mc_power_savings
echo default >/sys/module/pcie_aspm/parameters/policy
for h in $(ls -d /sys/class/scsi_host/host[0-9]*); do
	[ "$(cat $h/proc_name)" = "ahci" ] &&
		echo min_power >$h/link_power_management_policy
done
[ -e /run/openrc/started/alsasound ] &&
	echo 0 >/sys/module/snd_hda_intel/parameters/power_save
#iwpriv wlan0 set_power 2
xbacklight -set 40
xset dpms 600 900 1200
File/etc/hprofile/profiles/power/scripts/bat.start

#!/bin/sh
hprofile disk.adp
for c in $(ls -d /sys/devices/system/cpu/cpu[0-9]*); do
	echo ondemand >$c/cpufreq/scaling_governor
done
echo 200 >/sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor
echo 1   >/sys/devices/system/cpu/sched_mc_power_savings
echo powersave >/sys/module/pcie_aspm/parameters/policy
for h in $(ls -d /sys/class/scsi_host/host[0-9]*); do
	[ "$(cat $h/proc_name)" = "ahci" ] &&
		echo min_power >$h/link_power_management_policy
done
[ -e /run/openrc/started/alsasound ] &&
	echo 0 >/sys/module/snd_hda_intel/parameters/power_save
#iwpriv wlan0 set_power 4
xbacklight -set 30
xset dpms 300 400 600
File/etc/hprofile/profiles/power/scripts/dyn.start

#!/bin/sh
hprofile disk.adp
for c in $(ls -d /sys/devices/system/cpu/cpu[0-9]*); do
	echo performance >$c/cpufreq/scaling_governor
done
echo 200 >/sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor
echo 0   >/sys/devices/system/cpu/sched_mc_power_savings
echo performance >/sys/module/pcie_aspm/parameters/policy
for h in $(ls -d /sys/class/scsi_host/host[0-9]*); do
	[ "$(cat $h/proc_name)" = "ahci" ] &&
		echo max_performance >$h/link_power_management_policy
done
[ -e /run/openrc/started/alsasound ] &&
	echo 0 >/sys/module/snd_hda_intel/parameters/power_save
#iwpriv wlan0 set_power 1
xbacklight -set 40
xset dpms 0 0 2400
File/etc/hprofile/profiles/power/scripts/med.start

#!/bin/sh
hprofile disk.adp
for c in $(ls -d /sys/devices/system/cpu/cpu[0-9]*); do
	echo ondemand >$c/cpufreq/scaling_governor
done
echo 200 >/sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor
echo 1   >/sys/devices/system/cpu/sched_mc_power_savings
echo performance >/sys/module/pcie_aspm/parameters/policy
for h in $(ls -d /sys/class/scsi_host/host[0-9]*); do
	[ "$(cat $h/proc_name)" = "ahci" ] &&
		echo min_power >$h/link_power_management_policy
done
[ -e /run/openrc/started/alsasound ] &&
	echo 0 >/sys/module/snd_hda_intel/parameters/power_save
#iwpriv wlan0 set_power 6
xbacklight -set 30
xset dpms 500 700 900
File/etc/hprofile/profiles/power/scripts/quiet.start

#!/bin/sh
hprofile disk.adp
for c in $(ls -d /sys/devices/system/cpu/cpu[0-9]*); do
	echo ondemand >$c/cpufreq/scaling_governor
done
echo 200 >/sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor
echo 1   >/sys/devices/system/cpu/sched_mc_power_savings
echo powersave >/sys/module/pcie_aspm/parameters/policy
for h in $(ls -d /sys/class/scsi_host/host[0-9]*); do
	[ "$(cat $h/proc_name)" = "ahci" ] &&
		echo min_power >$h/link_power_management_policy
done
[ -e /run/openrc/started/alsasound ] &&
	echo 0 >/sys/module/snd_hda_intel/parameters/power_save
#iwpriv wlan0 set_power 6
xbacklight -set 20
xset dpms 0 0 300
Note
xset dpms argument unit is second, first for standby, second for suspend and third for off mode; I do not remember the effect of increasing/decreasing sampling_down_factor anymore, and I could not found the source (that setting impact on how often CPU should scale down/up obviously); the other lines are pretty obvious.

I have a battery runlevel, a copycat of default runlevel, that is dynamically switched if AC power is unplugged. See ACPI article for more info. That's not enough? Do you want more tunings? Expand the article then! I wish PowerTOP implement a daemon-ish profile-able application which is the right sport for Intel based hardware.

RFKill

This hardware profile subsection could be beneficial to the previous section, but killing radio of devices can be quite troublesome which require hard reboot and removing battery/AC power for a few second to be able to get back usable wireless interfaces again. This happen on my Intel GM45 based laptop. Luckily there is a physical switch to kill bluetooth and wifi radio.

There's no much consideration for this section, if your hardware support RFKill, add it to your hardware profile!

Note
b prefix stand for bluetooth, k for kill, w for wan and sw for switch.
File/etc/hprofile/profiles/rfkill/ptest

 
File/etc/hprofile/profiles/rfkill/default

 
Note
The previous file are intentionally left empty!
File/etc/hprofile/profiles/rfkill/profiles

bsw
ksw
wsw
File/etc/hprofile/profiles/rfkill/scripts/bsw.start

#!/bin/sh
echo 1 >/sys/class/rfkill/rfkill1/state
echo 1 >/sys/class/rfkill/rfkill7/state
File/etc/hprofile/profiles/rfkill/scripts/bsw.stop

#!/bin/sh
echo 0 >/sys/class/rfkill/rfkill1/state
echo 0 >/sys/class/rfkill/rfkill7/state
File/etc/hprofile/profiles/rfkill/scripts/wsw.start

#!/bin/sh
echo 1 >/sys/class/rfkill/rfkill0/state
echo 1 >/sys/class/rfkill/rfkill2/state
File/etc/hprofile/profiles/rfkill/scripts/wsw.stop

#!/bin/sh
echo 0 >/sys/class/rfkill/rfkill0/state
echo 0 >/sys/class/rfkill/rfkill2/state
File/etc/hprofile/profiles/rfkill/scripts/ksw.start

#!/bin/sh
hprofile rfkill.wsw
hprofile rfkill.bsw
Note
To determine what RFKill switch is for what, you will have to `cat /sys/class/rfkill/rfkill*/type' to know that. Of course, there are several manner to make this profile as each $profile.st* is just runned to apply a state so each $profile.start and $profile.stop is a profile in its own. So, feel free to expand those to your needs.

VGA

I use sys-apps/hprofile to manage VGA profile to my laptop and my desktop. On my desktop, I use hprofile to switch between nvidia-drivers, nouveau and nv (x11-drivers/xf86-video-nv) when the previous profiles do no work; and to switch radeon and intel on my laptop which has switchable graphics via VGASwitcheroo.

Just be sure to built kernel modules about everything — at least i9{1,6}5, nouveau, radeon, ttm, drm, and optionally (?) ac, button, video, i2c-algo-bit — to avoid useless hassles.

Create the necessaries folder /etc/hprofile/profiles/vga/{scripts,files/etc/X11/xorg.conf.d} and add the following files.

Note
Of course, the necessary files should be added for the following profiles, you do not need to copy every single profile related file if you do not have any use of it. Just do not switch to that particular profile with missing files later when using vga profile. Or even better, do not add, or rather remove, those particular profiles from the profile list file.
File/etc/hprofile/profiles/vga/profiles

intel
radeon
nvidia
nv
nouveau
fglrx
Note
This is necessary because the console font will be messed whenever a new profile is switched.
File/etc/hprofile/profiles/vga/post-start

#!/bin/sh
echo "Finished starting profile ${1}"
/etc/init.d/consolefont restart
File/etc/hprofile/profiles/vga/stop

#!/bin/sh
[ "${1}" == "intel" ] || [ "${1}" == "radeon" ] &&
    echo ON >/sys/kernel/debug/vgaswitcheroo/switch
echo "Finished Stopping profile ${1} and moving back xorg.conf"
Note
If a specific kernel module is loaded with an initramfs or modules init services, the appropriate profile will be selected with the following script.
File/etc/hprofile/profiles/vga/ptest

if [  ! -e /sys/kernel/debug/vgaswitcheroo/switch ]; then
    [ -z "$(lsmod | grep -i radeon)" ] && modprobe radeon && sleep 3
    [ -z "$(lsmod | grep -i i915)" ] && modprobe i915 && sleep 3
fi
[ $(grep DIS:+ /sys/kernel/debug/vgaswitcheroo/switch) ] && echo "radeon" && exit
[ $(grep IGD:+ /sys/kernel/debug/vgaswitcheroo/switch) ] && echo "intel" && exit
for drv in nouveau nvidia radeon fglrx; do
    [ -n "$(lspci -k | grep "Kernel driver in use: $drv")" ] && echo "$drv" && exit
done
for drv in i9{6,1}5; do
    [ -n "$(lspci -k | grep "Kernel driver in use: $drv")" ] && echo intel && exit 
done
echo "invalid" && exit 1
File/etc/hprofile/profiles/vga/scripts/fglrx.start

#!/bin/sh
echo "Starting fglrx profile."
modprobe fglrx || echo "failed to load fglrx module." && exit 1
[ "$(eselect opengl show)" != "ati" ] && eselect opengl set ati
[ "$(eselect xvmc show)" != "ati" ] && eselect xvmc set ati
File/etc/hprofile/profiles/vga/scripts/fglrx.stop

#!/bin/sh
echo "Stopping nvidia profile."
rmmod fglrx || echo "failed to remove fglrx module." && exit 1
File/etc/hprofile/profiles/vga/scripts/intel.start

#!/bin/sh
echo "Starting intel VGA profile."
echo IGD >/sys/kernel/debug/vgaswitcheroo/switch
echo OFF >/sys/kernel/debug/vgaswitcheroo/switch
[ "$(eselect opengl show)" != "xorg-x11" ] && eselect opengl set xorg-x11
[ "$(eselect xvmc show)" != "intel-i815" ] && eselect xvmc set intel-i815
File/etc/hprofile/profiles/vga/scripts/nouveau.start

#!/bin/sh
echo "Starting nouveau profile."
modprobe nouveau || echo "failed to load nouveau module."
[ "$(eselect opengl show)" != "xorg-x11" ] && eselect opengl set xorg-x11
exit
File/etc/hprofile/profiles/vga/scripts/nouveau.stop

#!/bin/sh
echo "Stopping nouveau profile."
echo 0 >/sys/class/vtconsole/vtcon1/bind
rmmod nouveau || echo "failed to remove nouveau module." && exit 1
/etc/init.d/consolefont restart
rmmod ttm
rmmod drm_kms_helper
rmmod dri
File/etc/hprofile/profiles/vga/scripts/radeon.start

#!/bin/sh
echo "Starting radeon profile."
echo DIS >/sys/kernel/debug/vgaswitcheroo/switch
echo OFF >/sys/kernel/debug/vgaswitcheroo/switch
echo low >/sys/class/drm/card0/device/power_profile
[ "$(eselect opengl show)" != "xorg-x11" ] && eselect opengl set xorg-x11
[ "$(eselect xvmc show)" != "xorg-x11" ] && eselect xvmc set xorg-x11
echo dynpm >/sys/class/drm/card0/device/power_method
# or use the following because "dynpm" can cause screen flickering
# echo profile >/sys/class/drm/card0/device/power_method
# echo auto[|low|mid] >/sys/class/drm/card0/device/power_profile
File/etc/hprofile/profiles/vga/scripts/nvidia.start

#!/bin/sh
echo "Starting nvidia profile."
modprobe nvidia || echo "failed to load nvidia module." && exit 1
[ "$(eselect opengl show)" != "nvidia" ] && eselect opengl set nvidia
File/etc/hprofile/profiles/vga/scripts/nvidia.stop

#!/bin/sh
echo "Stopping nvidia profile."
rmmod nvidia || echo "failed to remove nvidia module." && exit 1
File/etc/hprofile/profiles/vga/files/etc/X11/xorg.conf.d/40-monitor.conf.fglrx

Section "Monitor"
	Identifier  "Default"
EndSection

Section "Device"
	Identifier  "ATI"
	Driver      "fglrx"
EndSection

Section "Screen"
	Identifier "FGLRX"
	Device     "ATI"
	Monitor    "Default"
EndSection
File/etc/hprofile/profiles/vga/files/etc/X11/xorg.conf.d/40-monitor.conf.intel

Section "Monitor"
	Identifier  "Default"
EndSection

Section "Device"
	Option      "DRI" "True"
	Option      "XvMCSurfaces" "6"
	Identifier  "GMAX"
	Driver      "intel"
EndSection

Section "Screen"
	Identifier "INTEL"
	Device     "GMAX"
	Monitor    "Default"
EndSection
File/etc/hprofile/profiles/vga/files/etc/X11/xorg.conf.d/40-monitor.conf.nouveau

Section "Monitor"
    Identifier  "Default"
EndSection

Section "Device"
    Option      "EXAVsync"  "True"
    Option      "GLXVBlank" "True"
    Identifier  "nVidia"
    Driver      "nouveau"
EndSection

Section "Screen"
    Identifier "NOUVEAU"
    Device     "nVidia"
    Monitor    "Default"
EndSection
File/etc/hprofile/profiles/vga/files/etc/X11/xorg.conf.d/40-monitor.conf.nvidia

Section "Monitor"
    Identifier "Default"
EndSection

Section "Device"
    Option     "AddARGBGLXVisuals"  "on"
    Option     "NoLogo"
    Identifier "nVidia"
    Driver     "nvidia"
EndSection

Section "Screen"
    Identifier "NVIDIA"
    Device     "nVidia"
    Monitor    "Default"
EndSection
File/etc/hprofile/profiles/vga/files/etc/X11/xorg.conf.d/40-monitor.conf.radeon

Section "Monitor"
	Identifier   "Default"
EndSection

Section "Device"
	Option      "AccelMethod" "EXA"
	Option      "ClockGating" "On"
	Option      "ForceLowPowerMode" "On"
	Option      "VGAAccess" "On"
	Identifier  "RadeonHD"
	Driver      "radeon"
	Option	    "MonitorLayout" "LVDS, AUTO"
EndSection

Section "Screen"
	Identifier "RADEON"
	Device     "RadeonHD"
	Monitor    "Default"
EndSection

And with that, you should be able to switch vga profile as you like without needing an extra kernel and setup.

The ptest script will just look if VGASwitcheroo is available and which driver is loaded and then start the appropriate profile. One can black list a module and leave the other un-black listed to be able to chose ...a default VGA profile. Or else, use of `/etc/hprofile/profiles/vga/default' file.

File/etc/hprofile/profiles/vga/default

intel
Warning
This hardware profile shall be used out of X server by killing, if necessary, X server or display manager or desktop environment or windows manager session or even all of them.

Hard disk

A disk profile is even straightforward because there fewer issue to keep in mind. So this section will be short and straightforward. Additionally, one need sys-apps/hdparm.

File/etc/hprofile/profiles/disk/default

adp
File/etc/hprofile/profiles/disk/post-start

#!/bin/sh
for dev in $(ls -d /sys/devices/pci*/*/ata*/host*/target*/*/block/sd[a-z] 2>/dev/null); do
    echo cfq  >/sys/block/${dev##*/}/queue/scheduler
    echo 0    >/sys/block/${dev##*/}/queue/iosched/slice_idle
    echo 64   >/sys/block/${dev##*/}/queue/iosched/quantum
    # more opitmizations with ncq
    echo 1024 >/sys/block/${dev##*/}/queue/nr_requests
    echo 2    >/sys/block/${dev##*/}/device/queue_depth
done
for dev in $(ls -d /sys/devices/pci*/*/usb*/*/*/host*/target*/*/block/sd[a-z] 2>/dev/null)
do echo noop  >/sys/block/${dev##*/}/queue/scheduler; done
File/etc/hprofile/profiles/disk/ptest

#!/bin/sh
[ -e /etc/runlevels/battery ] &&
    [ "rc-status -r" != "default" ] && echo "bat" || echo "adp"
exit
File/etc/hprofil/profiles/disk/profiles

bat
adp
fast
quiet
File/etc/hprofile/profiles/disk/stop

#!/bin/sh
hdparm -q -S120 -B254 -M254 /dev/sd[a-z]
File/etc/hprofile/profiles/disk/scripts/adp.start

#!/bin/sh
hdparm -q -S120 -B230 -M254 /dev/sd[a-z]
File/etc/hprofile/profiles/disk/scripts/bat.start

#!/bin/sh
hdparm -q -S120 -B210 -M230 /dev/sd[a-z]
File/etc/hprofile/profiles/disk/scripts/fast.start

#!/bin/sh
hdparm -q -S180 -B254 -M254 /dev/sd[a-z]
File/etc/hprofile/profiles/disk/scripts/quiet.start

#!/bin/sh
hdparm -q -S120 -B200 -M200 /dev/sd[a-z]
Important
When using this script, you do not obviously need any extra hdparm init service, setting up dynamically hard disk profiles is more than sufficient. Or else, edit it to your needs.

External resources

Personal tools
Namespaces

Variants
Actions
Gentoo Websites logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Navigation
Toolbox
Categories