-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpumon.py
174 lines (153 loc) · 5.28 KB
/
gpumon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# or in the "license" file accompanying this file. This file 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.
# pip3 install requests nvidia-ml-py3 boto3
import boto3
import requests
from pynvml import *
from datetime import datetime
from time import sleep
### CHOOSE REGION ####
EC2_REGION = 'us-east-1'
###CHOOSE NAMESPACE PARMETERS HERE###
my_NameSpace = 'OmnisciGPU'
### CHOOSE PUSH INTERVAL ####
sleep_interval = 10
### CHOOSE STORAGE RESOLUTION (BETWEEN 1-60) ####
store_reso = 60
#Instance information
BASE_URL = 'http://169.254.169.254/latest/meta-data/'
INSTANCE_ID = requests.get(BASE_URL + 'instance-id').text
IMAGE_ID = requests.get(BASE_URL + 'ami-id').text
INSTANCE_TYPE = requests.get(BASE_URL + 'instance-type').text
INSTANCE_AZ = requests.get(BASE_URL + 'placement/availability-zone').text
EC2_REGION = INSTANCE_AZ[:-1]
TIMESTAMP = datetime.now().strftime('%Y-%m-%dT%H')
TMP_FILE = '/tmp/GPU_TEMP'
TMP_FILE_SAVED = TMP_FILE + TIMESTAMP
# Create CloudWatch client
cloudwatch = boto3.client('cloudwatch', region_name=EC2_REGION)
# Flag to push to CloudWatch
PUSH_TO_CW = True
def getPowerDraw(handle):
try:
powDraw = nvmlDeviceGetPowerUsage(handle) / 1000.0
powDrawStr = '%.2f' % powDraw
except NVMLError as err:
powDrawStr = handleError(err)
PUSH_TO_CW = False
return powDrawStr
def getTemp(handle):
try:
temp = str(nvmlDeviceGetTemperature(handle, NVML_TEMPERATURE_GPU))
except NVMLError as err:
temp = handleError(err)
PUSH_TO_CW = False
return temp
def getMemoryUtilization(handle):
try:
info = nvmlDeviceGetMemoryInfo(handle)
free = info.free
total = info.total
used = info.used
mem_util = info.used/info.total * 100
except NVMLError as err:
error = handleError(err)
gpu_util = error
mem_util = error
PUSH_TO_CW = False
return free, total, used, mem_util
def logResults(i, free, total, used, mem_util, powDrawStr, temp):
if (PUSH_TO_CW):
print("push to cw")
MY_DIMENSIONS=[
{
'Name': 'InstanceId',
'Value': INSTANCE_ID
},
{
'Name': 'ImageId',
'Value': IMAGE_ID
},
{
'Name': 'InstanceType',
'Value': INSTANCE_TYPE
},
{
'Name': 'GPUNumber',
'Value': str(i)
}
]
cloudwatch.put_metric_data(
MetricData=[
{
'MetricName': 'Total Memory',
'Dimensions': MY_DIMENSIONS,
'Unit': 'Bytes',
'StorageResolution': store_reso,
'Value': total
},
{
'MetricName': 'Used Memory',
'Dimensions': MY_DIMENSIONS,
'Unit': 'Bytes',
'StorageResolution': store_reso,
'Value': used
},
{
'MetricName': 'Free Memory',
'Dimensions': MY_DIMENSIONS,
'Unit': 'Bytes',
'StorageResolution': store_reso,
'Value': free
},
{
'MetricName': 'Memory Usage',
'Dimensions': MY_DIMENSIONS,
'Unit': 'Percent',
'StorageResolution': store_reso,
'Value': mem_util
},
{
'MetricName': 'Power Usage (Watts)',
'Dimensions': MY_DIMENSIONS,
'Unit': 'None',
'StorageResolution': store_reso,
'Value': float(powDrawStr)
},
{
'MetricName': 'Temperature (C)',
'Dimensions': MY_DIMENSIONS,
'Unit': 'None',
'StorageResolution': store_reso,
'Value': int(temp)
},
],
Namespace=my_NameSpace
)
nvmlInit()
deviceCount = nvmlDeviceGetCount()
def main():
try:
PUSH_TO_CW = True
# Find the metrics for each GPU on instance
for i in range(deviceCount):
handle = nvmlDeviceGetHandleByIndex(i)
powDrawStr = getPowerDraw(handle)
temp = getTemp(handle)
free, total, used, mem_util = getMemoryUtilization(handle)
logResults(i, free, total, used, mem_util, powDrawStr, temp)
print(free,total,used,mem_util)
finally:
nvmlShutdown()
if __name__=='__main__':
main()