Skip to content

Commit

Permalink
Final changes needed before making pip installable
Browse files Browse the repository at this point in the history
  • Loading branch information
klottick committed Jul 22, 2019
1 parent 85fd783 commit 0da16ae
Show file tree
Hide file tree
Showing 22 changed files with 61 additions and 95 deletions.
3 changes: 2 additions & 1 deletion AUTHORS.txt
@@ -1,2 +1,3 @@
* Sorelle Friedler
* Kadan Lottick
* Silvia Susai
* Kadan Lottick
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

13 changes: 13 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,13 @@
Copyright 2019 by authors list (see AUTHORS.txt)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
12 changes: 12 additions & 0 deletions MANIFEST.in
@@ -0,0 +1,12 @@
recursive-include energy_usage *.py *.md *.txt

include requirements*.*
include README.*

graft energy_usage/data/json
graft energy_usage/data/csv

recursive-exclude ./dist *.py *.R *.Rmd *.md *.txt
recursive-exclude ./build *.py *.R *.Rmd *.md *.txt

prune venv
1 change: 0 additions & 1 deletion __init__.py

This file was deleted.

Empty file removed data/__init__.py
Empty file.
File renamed without changes.
1 change: 1 addition & 0 deletions energy_usage/__init__.py
@@ -0,0 +1 @@
name = "energy_usage"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions energy-meter/energy-meter.py → energy_usage/energy-meter.py
@@ -1,3 +1,5 @@
#import energy_usage.evaluate as evaluate
from . import evaluate
import evaluate
import time

Expand Down
34 changes: 9 additions & 25 deletions energy-meter/evaluate.py → energy_usage/evaluate.py
Expand Up @@ -6,18 +6,18 @@
import datetime
import subprocess

import utils
import convert
import locate
import energy_usage.utils as utils
import energy_usage.convert as convert
import energy_usage.locate as locate

DELAY = .1 # in seconds


def func(user_func, q, *args):
""" Runs the user's function and gets return value """

value = user_func(*args)
q.put(value)


def energy(user_func, *args):
""" Evaluates the kwh needed for your code to run
Expand All @@ -28,23 +28,8 @@ def energy(user_func, *args):

baseline_checks_in_seconds = 2
files, multiple_cpus = utils.get_files()

#packages = utils.get_packages()
# Get baseline wattage reading (FOR WHAT? PKG for now, DELAY? default .1 second)

# GPU handling if Nvidia
is_nvidia_gpu = True
try:
bash_command = "nvidia-smi > /dev/null 2>&1" #we must pipe to ignore error message
output = subprocess.check_call(['bash','-c', bash_command])
if "GPU" not in files:
files.append(RAPLFile("GPU", ""))
bash_command = "nvidia-smi --query-gpu=,power.draw --format=csv,noheader,nounits"

except:
is_nvidia_gpu = False


is_nvidia_gpu = utils.valid_gpu()

for i in range(int(baseline_checks_in_seconds / DELAY)):
if is_nvidia_gpu:
Expand Down Expand Up @@ -72,7 +57,6 @@ def energy(user_func, *args):

files = utils.measure_files(files, DELAY)
files = utils.update_files(files, True)

package = utils.get_total(files, multiple_cpus)
if package >=0:
utils.log("Process wattage", package)
Expand Down Expand Up @@ -115,7 +99,7 @@ def energy_mix(location):
if location == "Unknown":
location = "United States"

data = utils.get_data("../data/json/energy-mix-us.json")
data = utils.get_data("data/json/energy-mix-us.json")
s = data[location]['mix'] # get state
coal, oil, gas = s['coal'], s['oil'], s['gas']
nuclear, hydro, biomass, wind, solar, geo, = \
Expand All @@ -128,7 +112,7 @@ def energy_mix(location):
return breakdown # list of % of each

else:
data = utils.get_data('../data/json/energy-mix-intl.json')
data = utils.get_data('data/json/energy-mix-intl.json')
c = data[location] # get country
total, breakdown = c['total'], [c['coal'], c['naturalGas'], \
c['petroleum'], c['lowCarbon']]
Expand Down Expand Up @@ -164,7 +148,7 @@ def emissions(process_kwh, breakdown, location):
if location == "Unknown":
location = "United States"
# US Emissions data is in lbs/Mwh
data = utils.get_data("../data/json/us-emissions.json")
data = utils.get_data("data/json/us-emissions.json")
emission = convert.lbs_to_kgs(data[location]*convert.to_Mwh(process_kwh))

# Case 3: International location
Expand Down
1 change: 0 additions & 1 deletion energy-meter/locate.py → energy_usage/locate.py
Expand Up @@ -37,6 +37,5 @@ def get():
print("Location: {:>70}".format(location))
return location


def in_US(location):
return (location in STATES or location == "United States")
43 changes: 10 additions & 33 deletions energy-meter/utils.py → energy_usage/utils.py
Expand Up @@ -7,29 +7,17 @@
import re
import statistics

import convert
import locate
from RAPLFile import RAPLFile
import energy_usage.convert as convert
import energy_usage.locate as locate
from energy_usage.RAPLFile import RAPLFile


# TO DO: Function to convert seconds into more reasonable time
# TO DO: Having something to relate to

# MSR_*FILE*_ENERGY_STATUS
# Total amount of energy consumed since that last time this register was
# cleared
BASE = "/sys/class/powercap/"

PKG = "/sys/class/powercap/intel-rapl:0/energy_uj"
CORE = "/sys/class/powercap/intel-rapl:0:0/energy_uj"
UNCORE = "/sys/class/powercap/intel-rapl:0:1/energy_uj"
DRAM = "/sys/class/powercap/intel-rapl:1:0/energy_uj"# if has_multiple_cpus() \
#else "/sys/class/powercap/intel-rapl:0:2/energy_uj"

DELAY = .1 # in seconds
DIR_PATH = os.path.dirname(os.path.realpath(__file__))


""" MEASUREMENT UTILS """

def read(file):
Expand All @@ -55,6 +43,7 @@ def measure(file, delay=1):
end = read(file)

return end-start

def get_process_average(raplfiles, multiple_cpus):
total = 0
if multiple_cpus:
Expand All @@ -67,6 +56,7 @@ def get_process_average(raplfiles, multiple_cpus):
if file.name == "Package":
return file.process_average
return -1

def get_baseline_average(raplfiles, multiple_cpus):
total = 0
if multiple_cpus:
Expand All @@ -78,7 +68,7 @@ def get_baseline_average(raplfiles, multiple_cpus):
for file in raplfiles:
if file.name == "Package":
return file.baseline_average
return -1


def get_total(raplfiles, multiple_cpus):
total = 0
Expand All @@ -91,7 +81,7 @@ def get_total(raplfiles, multiple_cpus):
for file in raplfiles:
if file.name == "Package":
return file.recent
return -1


def update_files(raplfiles, process = False):
if process:
Expand All @@ -105,7 +95,6 @@ def update_files(raplfiles, process = False):
return raplfiles



def start(raplfile):
measurement = read(raplfile.path)
raplfile.recent = measurement
Expand All @@ -130,8 +119,7 @@ def measure_files(files, delay = 1):

files = list(map(start, files))
time.sleep(delay)
# need lambda to pass in delay
files = list(map(lambda x: end(x, delay), files))
files = list(map(lambda x: end(x, delay), files)) # need lambda to pass in delay
return files


Expand Down Expand Up @@ -165,7 +153,7 @@ def get_files():
for file in files:
if (re.fullmatch("intel-rapl:.", file)):
cpu_count += 1

if cpu_count > 1:
multiple_cpus = True

Expand All @@ -184,13 +172,6 @@ def get_files():
return filenames, multiple_cpus



def has_multiple_cpus():

packages = get_packages()
return len(packages) > 1


# from realpython.com/python-rounding
def round_up(n, decimals=4):
""" Rounds up if digit is >= 5 """
Expand Down Expand Up @@ -277,7 +258,6 @@ def log(*args):
sys.stdout.write("{:<14} {:>65}\n".format("Natural gas:", ".0885960 kg CO2/kwh"))



""" MISC UTILS """

def get_data(file):
Expand All @@ -290,10 +270,7 @@ def valid_cpu():
return os.path.exists(BASE) and bool(os.listdir(BASE))

def valid_gpu():

# checks that there is a valid gpu: either integrated graphics
# or nvidia

""" Checks that there is a valid Nvidia GPU """
try:
bash_command = "nvidia-smi > /dev/null 2>&1" #we must pipe to ignore error message
output = subprocess.check_call(['bash','-c', bash_command])
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
@@ -1,2 +1,2 @@
requests
reportlab

23 changes: 11 additions & 12 deletions setup.py
Expand Up @@ -3,16 +3,17 @@
with open("README.md", "r") as fh:
long_description = fh.read()

NAME = "energy-usage"
NAME = "energy_usage"
VERSION = "0.0.1"

DESCRIPTION = "Measuring the environmental impact of computation"
LONG_DESCRIPTION = long_description

URL = "https://github.com/responsibleproblemsolving/energy-usage"
AUTHOR = "Silvia Susai, Kadan Lottick"

AUTHOR_EMAIL = "<todo>@haverford.edu"
AUTHOR = "Sorelle Friedler, Kadan Lottick, Silvia Susai"
AUTHOR_EMAIL = "sorelle@cs.haverford.edu"

LICENSE = "Apache 2.0"

CLASSIFIERS = [
Expand All @@ -21,32 +22,30 @@
"Operating System :: OS Independent",
]

PACKAGES = find_packages()
PACKAGES = ['energy_usage']

PACKAGE_DATA = {
'energy.data.csv' : ['*.csv']
'energy.data.json' : ['*.json']
'energy_usage.data.csv' : ['*.csv'],
'energy_usage.data.json' : ['*.json']
}
INCLUDE_PACKAGE_DATA = True

PACKAGE_DIR = {
'energy.data' : 'energy/data'
'energy_usage.data' : 'data'
}

INSTALL_REQUIRES = [
'requests'
]




setup(
name= NAME,
version=VERSION,
description=DESCRIPTION,
long_description=LONG_DESCRIPTION,
long_description_content_type="text/markdown",
url=URL,
author=AUTHOR,
author_email=AUTHOR_EMAIL,
author_email = AUTHOR_EMAIL,
license = LICENSE,
classifiers=CLASSIFIERS,
packages = PACKAGES,
Expand Down

0 comments on commit 0da16ae

Please sign in to comment.