Cgminer API python client for bash and shell script for profiling when system is idle

From Urandom
Jump to: navigation, search

I use my computer for scrypt mining with cgminer. I also want to use it as a work and media station. Wrote this script for monitoring system idle (X minutes, Y minutes) and also running programs (vlc, clementine...). During this time i wanted to slow down cgminer(lower intencity and cpu clocks), so that the computer would be quiet and responsive. First I needed to control cgminer from the console.

Contents

Commanding cgminer from shell / bash

Found this post useful http://thomassileo.com/blog/2013/09/17/playing-with-python-and-cgminer-rpc-api/ So i built my own version of it, so that i could use it in simple bash or shell scripts.

* For this to work start cgminer with these extra options --api-listen --api-allow W:127.0.0.1 --api-port 28080


cgminer_pipe.py

Download from http://www.loovsys.eu/redmine/projects/cgminerapi/files

wget http://www.loovsys.eu/redmine/attachments/download/9/cgminer_pipe.py

Also add executable flag to script.

$ chmod +x cgminer_pipe.py

Copy to bin folder or add it to your enviroment $PATH

cp -p cgminer_pipe.py /usr/bin/
#or env
export PATH="./:$PATH"

Example usage: summary

$ echo summary | cgminer_pipe.py
---------------    
STATUS 
---------------    
STATUS: S    
Msg: Summary 
Code: 11     
When: 1389430665   
Description: cgminer 3.7.2     
---------------    
id     
1      
---------------    
SUMMARY      
---------------    
Difficulty Accepted: 429568.0  
Pool Rejected%: 5.0905   
Found Blocks: 0    
Difficulty Rejected: 23040.0   
Device Rejected%: 5.0393 
Pool Stale%: 0.0   
Work Utility: 503.38     
Rejected: 45 
Elapsed: 54497
Hardware Errors: 0
Accepted: 839
Network Blocks: 1498
Local Work: 9051
Get Failures: 0
Difficulty Stale: 0.0
Total MH: 28545.5012
Device Hardware%: 0.0
Discarded: 4438
Stale: 0
MHS av: 0.52
Getworks: 2223
MHS 5s: 0.1
Best Share: 447544
Remote Failures: 0
Utility: 0.92

Example usage: summary, but with reduced output

$ echo summary | cgminer_pipe.py | grep "Accepted\|Rejected\|Errors\|MHS av" 
Difficulty Accepted: 429568.0
Pool Rejected%: 5.0905
Difficulty Rejected: 23040.0
Device Rejected%: 5.0393
Rejected: 45
Hardware Errors: 0
Accepted: 839
MHS av: 0.52

Example usage: change intensity

$ echo gpuintensity:0,10 | cgminer_pipe.py
---------------
STATUS
---------------
STATUS: I
Msg: GPU 0 set new intensity to 10
Code: 32
When: 1389430714
Description: cgminer 3.7.2
---------------
id
1

Example usage: change gpu clock (MAKE SURE IT IS NOT TOO HIGH FOR YOUR GPU!)

$ echo gpuengine:0,1000 | cgminer_pipe.py
---------------
STATUS
---------------
STATUS: S
Msg: Setting GPU 0 clock to (1000) reported success
Code: 37
When: 1389430750
Description: cgminer 3.7.2
---------------
id
1

Example usage: List pools

 echo pools | cgminer_pipe.py

Example usage: Add pool

echo addpool:stratum+tcp://eu.middlecoin.com:3333,17MoKZfJYb89FfAKsJnbrhZJUkeU1tPbfa,x  | cgminer_pipe.py


Example usage: Rotate 2 pools every X hours

$ HOURS=9;
$ SECONDS=`expr  60 \* 60 \* $HOURS`
$ while [ 1 -gt 0 ] #run forever
 do
 if [ "$l" == "" ]
 then
  echo switchpool:4 | cgminer_pipe.py #pool number 4, starts first
  unset l
 else
  echo switchpool:0 | cgminer_pipe.py #pool number 0
  l=1
 fi
 sleep $SECONDS
 done


Shellscript to profile/change cgminer options when system is idle or certain programs run

* NB!: 
* Make sure you change the GPU core and memory clocks before running this script!
* The cgminer_pipe.py file has to be in the same folder and executable.
* xprintidle is required for this to work (apt-get install xprintidle)

activity_monitor.sh

#!/bin/bash
#This is the first stage, when system is idle for 2 minutes the profile average will be used
idletime=5 #minutes
#This is the second stage, when system is idle for 60 minutes the profile fast will be used
sleepy=60 #minutes

#list of programs that will override to use average profile
silentprogs='vlc xbmc clementine'
#quiet profile will be used then system is less time idle than $idletime 

#convert minutes to milliseconds
idletime=`expr  60 \* 1000 \* $idletime`
sleepy=`expr  60 \* 1000 \* $sleepy`

last='none' #last profile (startup value)

while [ 1 -gt 0 ]
do

if [ `xprintidle` -gt $sleepy ] #check how long x has been idle        
then
   in='fast'
elif [ `xprintidle` -gt $idletime ] #check how long x has been idle        
then
   in='average'
else
   in='quiet'
fi

for pr in $silentprogs
do

pgrep $pr > /dev/null
if [ $? -eq 0 ]
then
  in='average'
  break
fi

done

#place a file called activity_monitor.override that contains the profile name.
#will cause switch to that profile until there is some activity again on the computer
#I use this so that when i press power button, it will swtich to fast profile immediately ( and back when there is activity again )
if [ -f ~/activity_monitor.override ]
then
  
  if [ `xprintidle` -lt 300 ] #check how long x has been idle        
  then
    rm -rf ~/activity_monitor.override
  else
    in=`cat ~/activity_monitor.override | xargs echo -n`
  fi
fi


if [ "$in" != "$last"  ] #if the profile has changed
then
    last=$in
    echo "`date +%Y-%m-%d_%H:%M:%S` Selected new profile $in -----------------------"
    echo summary | cgminer_pipe.py | grep "Accepted\|Rejected\|Errors\|MHS av"
    if [ "$in" == "quiet" ]
    then
      echo gpumem:0,1300 | cgminer_pipe.py > /dev/null
      echo gpuengine:0,940 | cgminer_pipe.py > /dev/null
      echo gpuintensity:0,14 | cgminer_pipe.py > /dev/null 
    elif [ "$in" == "average" ]
    then
      echo gpumem:0,1450 | cgminer_pipe.py > /dev/null
      echo gpuengine:0,950 | cgminer_pipe.py > /dev/null
      echo gpuintensity:0,14 | cgminer_pipe.py  > /dev/null
    elif [ "$in" == "fast" ]
    then
      echo gpumem:0,1500 | cgminer_pipe.py > /dev/null
      echo gpuengine:0,1014 | cgminer_pipe.py > /dev/null  
      echo gpuintensity:0,20 | cgminer_pipe.py > /dev/null
    fi
fi

if [ "$in" == "fast" ] #we might want to repeatedly execute this, since sometimes screens just wake up, or the mouse gets moved just a little.
then
echo ""  > /dev/null
 xset dpms force off
fi

sleep 5 #rescan every 5 seconds
done

Example for force profile

echo "fast" > ~/activity_monitor.override


If you have any further questions feel free to e-mail me. A donation is also welcome, my bitcoin wallet: 12Nfc9LANAd4HNoVUxSw23kyKvpaLkKYGG

Personal tools