Skip to content

Instantly share code, notes, and snippets.

@seqizz
Last active May 11, 2020 19:03
Show Gist options
  • Select an option

  • Save seqizz/ebe5bd5a1178c965d81dc09458d98a38 to your computer and use it in GitHub Desktop.

Select an option

Save seqizz/ebe5bd5a1178c965d81dc09458d98a38 to your computer and use it in GitHub Desktop.

Revisions

  1. seqizz renamed this gist Jan 5, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. seqizz created this gist Jan 5, 2020.
    63 changes: 63 additions & 0 deletions psitool.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    #!/usr/bin/env python3

    from hashlib import sha256
    from pickle import load, dump
    from socket import gethostname
    from time import time


    def current_psi_dict():
    with open('/proc/pressure/cpu', 'r') as psifile:
    total = psifile.read().split(' ')[4].split('=')[1]
    return {
    'value': int(total),
    'time': int(time())
    }


    def write_current_value(temp_file):
    current_value = current_psi_dict()
    with open(temp_file, 'wb') as valuefile:
    dump(current_value, valuefile)


    def get_last_value(name):
    result = False
    with open(name, 'rb') as valuefile:
    result = load(valuefile)

    return result


    def get_filename():
    hash = sha256(gethostname().encode('utf-8')).hexdigest()[:8]
    return '/tmp/{}_psi'.format(hash)


    def get_difference(temp_file):
    last_value = get_last_value(temp_file)
    if last_value is False:
    return "-1"
    current_value = current_psi_dict()
    time_difference = current_value['time'] - last_value['time']
    value_difference = current_value['value'] - last_value['value']
    if time_difference == 0:
    # Prevent division by zero, feed last one
    return current_value['value']
    return(int(value_difference / time_difference))


    def main():

    temp_file = get_filename()

    try:
    with open(temp_file) as f:
    print(get_difference(temp_file))
    except IOError:
    print('-1')
    finally:
    write_current_value(temp_file)


    main()