#!/bin/bash
#
# script to read out the current values which can be set by pm-profiler.
#

function pm_profile_name {
    Value=`cat /etc/pm-profiler.conf |grep PM_PROFILER_PROFILE| \
                 sed -e 's/PM_PROFILER_PROFILE=\"\(.*\)\"/\1/g'`
    echo "NAME=$Value"
}

function get_cpufreq_governor {
    Option="CPUFREQ_GOVERNOR"
    Governor=`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor`
    echo $Option"="$Governor
}

function get_cpufreq_up_threshold {
    Option="CPUFREQ_UP_THRESHOLD"

    if [ -f /sys/devices/system/cpu/cpu0/cpufreq/$Governor/up_threshold ]; then
      Value=`cat /sys/devices/system/cpu/cpu0/cpufreq/$Governor/up_threshold`
      for I in /sys/devices/system/cpu/cpu[[:digit:]]*; do
        if [ -d $I/cpufreq/$Governor ]; then
            Next_Value=`cat $I/cpufreq/$Governor/up_threshold`
            if [[ $Next_Value != $Value ]]; then
                echo "Warning: different values: "$Option >&2
            fi
        else
            echo "Warning: different governor on "$I >&2
        fi
      done
    else
      Value=`cat /sys/devices/system/cpu/cpufreq/$Governor/up_threshold`
    fi
    echo $Option"="$Value
}

function get_cpufreq_sampling_rate {
    Option="CPUFREQ_SAMPLING_RATE"

    if [ -f /sys/devices/system/cpu/cpu0/cpufreq/$Governor/sampling_rate ]; then
      Value=`cat /sys/devices/system/cpu/cpu0/cpufreq/$Governor/sampling_rate`
      for I in /sys/devices/system/cpu/cpu[[:digit:]]*; do
        if [ -d $I/cpufreq/$Governor ]; then
            if [ `cat $I/cpufreq/$Governor/sampling_rate` != $Value ]; then
                echo "Warning: different values: "$Option >&2
            fi
        else
            echo "Warning: different governor on "$I >&2
        fi
      done
    else
      Value=`cat /sys/devices/system/cpu/cpufreq/$Governor/sampling_rate`
    fi
    echo $Option"="$Value
}

function get_cpufreq_sampling_down_factor {
    Option="CPUFREQ_SAMPLING_DOWN_FACTOR"

    if [ -f /sys/devices/system/cpu/cpu0/cpufreq/$Governor/sampling_down_factor ]; then
      Value=`cat /sys/devices/system/cpu/cpu0/cpufreq/$Governor/sampling_down_factor`
      for I in /sys/devices/system/cpu/cpu[[:digit:]]*; do
        if [ -d $I/cpufreq/$Governor ]; then
            if [ `cat $I/cpufreq/$Governor/sampling_down_factor` != $Value ]; then
                echo "Warning: different values: "$Option >&2
            fi
        else
            echo "Warning: different governor on "$I >&2
        fi
      done
    else
      Value=`cat /sys/devices/system/cpu/cpufreq/$Governor/sampling_down_factor`
    fi
    echo $Option"="$Value
}

function get_cpufreq_ondemand_powersave_bias {
    Option="CPUFREQ_ONDEMAND_POWERSAVE_BIAS"

    if [ -f /sys/devices/system/cpu/cpu0/cpufreq/$Governor/powersave_bias ]; then
      Value=`cat /sys/devices/system/cpu/cpu0/cpufreq/$Governor/powersave_bias`
      for I in /sys/devices/system/cpu/cpu[[:digit:]]*; do
        if [ -d $I/cpufreq/$Governor ]; then
            if [ `cat $I/cpufreq/$Governor/powersave_bias` != $Value ]; then
                echo "Warning: different values: "$Option >&2
            fi
        else
            echo "Warning: different governor on "$I >&2
        fi
      done
    else
      Value=`cat /sys/devices/system/cpu/cpufreq/$Governor/powersave_bias`
    fi
    echo $Option"="$Value
}

function get_cpufreq_schedutil_rate_limit_us {
    Option="CPUFREQ_SCHEDUTIL_RATE_LIMIT_US"
    Value=`cat /sys/devices/system/cpu/cpufreq/$Governor/rate_limit_us`
    echo $Option"="$Value
}

function get_sata_alpm {
    Option="SATA_ALPM"
    Value=

    if [ -f /sys/class/scsi_host/host0/link_power_management_policy ]; then
        Value=`cat /sys/class/scsi_host/host0/link_power_management_policy`

        for I in /sys/class/scsi_host/host[[:digit:]]*/link_power_management_policy; do
            if [ `cat $I` != $Value ] ; then
                echo "Warning: different values: "$Option >&2
            fi
        done
    fi
    echo $Option"="$Value
}

function get_dirty_writeback_centisecs {
    Value=`cat /proc/sys/vm/dirty_writeback_centisecs`
    echo "DIRTY_WRITEBACK_CENTISECS="$Value >&2
}

function get_read_ahead_kb {
    Option="READ_AHEAD_KB"
    Value=`cat /sys/block/sda/queue/read_ahead_kb`
    for I in /sys/block/sd*/queue/read_ahead_kb; do
        if [[ `cat $I` != $Value ]]; then
            echo "Warning: different values: "$Option >&2
        fi
    done
    echo $Option"="$Value
}

get_cpufreq_governor
get_sata_alpm
#get_external_hook
get_dirty_writeback_centisecs
get_read_ahead_kb

if [ -d /sys/devices/system/cpu/cpu0/cpufreq ]; then
    case "$Governor" in
      ondemand)
        get_cpufreq_sampling_rate
        get_cpufreq_up_threshold
        get_cpufreq_sampling_down_factor
        get_cpufreq_ondemand_powersave_bias
        ;;
      conservative)
        get_cpufreq_sampling_rate
        get_cpufreq_up_threshold
        get_cpufreq_sampling_down_factor
        ;;
      schedutil)
        get_cpufreq_schedutil_rate_limit_us
        ;;
    esac
    exit 0
else
    echo "Error: unable to read settings." \
         "Please check if your system supports power management." >&2
    exit 126
fi

exit 0


