How to use AMD P-State in Linux

well … I gave the task of writing the script to ChatGPT free model (chatGPT 3):

User
Write a Shell script that applies a configurable value, for example 'balance_power' to all threads of a system. The path is /sys/devices/system/cpu/cpufreq/policy6/energy_performance_preference



ChatGPT

Certainly! Here's a Shell script that applies a configurable value, such as 'balance_power', to all threads of a system by modifying the energy_performance_preference file for each CPU thread:

bash

#!/bin/bash

# Configurable value
CONFIG_VALUE="balance_power"

# Get the number of CPU threads
NUM_THREADS=$(grep -c ^processor /proc/cpuinfo)

# Loop through each CPU thread and apply the configurable value
for ((i=0; i<NUM_THREADS; i++))
do
    THREAD_PATH="/sys/devices/system/cpu/cpufreq/policy$i/energy_performance_preference"
    
    # Check if the energy_performance_preference file exists
    if [ -f "$THREAD_PATH" ]; then
        echo "$CONFIG_VALUE" > "$THREAD_PATH"
        echo "Applied $CONFIG_VALUE to $THREAD_PATH"
    else
        echo "Error: $THREAD_PATH does not exist"
    fi
done

Here's how the script works:

    The configurable value is set to "balance_power" in the CONFIG_VALUE variable.
    The script retrieves the number of CPU threads by counting the lines containing "processor" in the /proc/cpuinfo file.
    It then loops through each CPU thread from 0 to NUM_THREADS-1.
    For each thread, it constructs the path to the energy_performance_preference file using the thread number.
    The script checks if the energy_performance_preference file exists for the current thread.
    If the file exists, it writes the configurable value to the file using the echo command.
    Finally, it prints a success message if the value was applied, or an error message if the file doesn't exist.

Make sure to run this script with appropriate permissions (e.g., sudo) since modifying system files typically requires elevated privileges.


User
rewrite the script to be silent


ChatGPT

Certainly! Here's a modified version of the script that runs silently, without printing any messages:

bash

#!/bin/bash

# Configurable value
CONFIG_VALUE="balance_power"

# Get the number of CPU threads
NUM_THREADS=$(grep -c ^processor /proc/cpuinfo)

# Loop through each CPU thread and apply the configurable value
for ((i=0; i<NUM_THREADS; i++))
do
    THREAD_PATH="/sys/devices/system/cpu/cpufreq/policy$i/energy_performance_preference"
    
    # Check if the energy_performance_preference file exists
    if [ -f "$THREAD_PATH" ]; then
        echo "$CONFIG_VALUE" > "$THREAD_PATH" 2>/dev/null
    fi
done

In this version of the script, the echo command that writes the CONFIG_VALUE to the file is appended with 2>/dev/null. This redirects the standard error stream to /dev/null, discarding any error messages that may occur during the execution of the script.

With this modification, the script will run silently, without any output or error messages.

I am now executing that script with the systemd process that I set up above and it works

Edit:
Here is the silent script without having to scroll through the chat-log and minor modifications for race condition fixes.

#!/bin/bash

# Configurable value
CONFIG_VALUE='balance_power'
sleep 3
# Get the number of CPU threads
NUM_THREADS=$(grep -c ^processor /proc/cpuinfo)

# Loop through each CPU thread and apply the configurable value
for ((i=0; i<NUM_THREADS; i++))
do
    THREAD_PATH="/sys/devices/system/cpu/cpufreq/policy$i/energy_performance_preference"
    
    # Check if the energy_performance_preference file exists
    if [ -f "$THREAD_PATH" ]; then
        echo "$CONFIG_VALUE" > "$THREAD_PATH" 2>/dev/null
    fi
done
1 Like