-
Notifications
You must be signed in to change notification settings - Fork 2
/
PopulateDatabase
executable file
·364 lines (274 loc) · 10.4 KB
/
PopulateDatabase
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/env python3
"""
Desc: Script to populate the database for the last N days of use.
Notes:
- This script is built to be run on all users, en-masse
Authors:
- Chance Nelson <chance-nelson@nau.edu>
"""
import subprocess
import sys
import time
import re
from datetime import datetime
from datetime import timedelta
import mysql.connector as mariadb
import configparser
import os
import json
def timeStringToSeconds(timeString):
"""
Desc: Convert a time string to seconds total elapsed.
Args:
timeString (string): Timestamp of the form 'D-HH:MM:SS.SSS'
Returns:
Integer or floating point representation of seconds elapsed.
"""
rightDecimal = 0
timeInSeconds = 0
timeIndex = 0
timeParsed = timeString.split('.')
if len(timeParsed) > 1:
timeInSeconds += float('0.' + timeParsed[1])
timeParsed = timeParsed[0]
timeParsed = re.split('-|:', timeParsed)
# Does elapsed contain days? (*1-*00:00:00)
if len(timeParsed) == 4:
timeInSeconds += int(timeParsed[0]) * 24 * 60 * 60
# Does time contain hours?
if len(timeParsed) >= 3:
timeInSeconds += int(timeParsed[-3]) * 60 * 60 # Hours
# Does time contain minutes?
if len(timeParsed) >= 2:
timeInSeconds += int(timeParsed[-2]) * 60 # Minutes
# Does time contain seconds?
if len(timeParsed) >= 1:
timeInSeconds += int(timeParsed[-1]) # Seconds
return timeInSeconds
def getSlurmAccountUsers(acc):
"""
Desc: Get a list of slurm users who belong to a given account.
Args:
acc (string): name of slurm account to search
Returns:
List of account user names. None of account doesn't exist/no users
belong to it.
"""
out = subprocess.getstatusoutput('sshare -a -P')[1].split('\n')
users = []
for i in out:
users.append(i.split('|'))
users = [i for i in users if acc in i[0]]
users = [i[1] for i in users if not i[1] == 'parent' and not i[1] == '']
return users
def getSlurmAccounts():
"""
Desc: Retrieve a list of all available slurm accounts in the cluster
"""
out = subprocess.getstatusoutput('sshare -l -P')[1].split('\n')[1::]
accounts = []
for i in out:
accounts.append(i.split('|'))
account_names = []
for i in accounts :
if i[0].strip() not in account_names:
account_names.append(i[0].strip())
return account_names
def timeStringToSeconds(time_string):
"""
Desc: Convert a time string to seconds total elapsed.
Args:
time_string (string): Timestamp of the form 'D-HH:MM:SS.SSS'
Returns:
Integer or floating point representation of seconds elapsed.
"""
right_decimal = 0
time_in_seconds = 0
time_index = 0
time_parsed = time_string.split('.')
if len(time_parsed) > 1:
time_in_seconds += float('0.' + time_parsed[1])
time_parsed = time_parsed[0]
time_parsed = re.split('-|:', time_parsed)
# Does elapsed contain days? (*1-*00:00:00)
if len(time_parsed) == 4:
time_in_seconds += int(time_parsed[0]) * 24 * 60 * 60
# Does time contain hours?
if len(time_parsed) >= 3:
time_in_seconds += int(time_parsed[-3]) * 60 * 60 # Hours
# Does time contain minutes?
if len(time_parsed) >= 2:
time_in_seconds += int(time_parsed[-2]) * 60 # Minutes
# Does time contain seconds?
if len(time_parsed) >= 1:
time_in_seconds += float(time_parsed[-1]) # Seconds
return time_in_seconds
def getJobStats(cursor, user, account, start_date_time, track_single_core_jobs):
"""
Desc: Retrieve the job stats for a list of users, and populate the gpuinfo
table
Args:
user (string): User to search.
account (string): Slurm account of user
hoursBack (int) (optional): How many hours in the past to search back
Returns:
[float, float, float, float]
List containing the stats for memory, cores, timelimit, and total,
respectively.
"""
cmd = 'jobstats -o Comment,End,AllocGRES -p' # Add in username and make parsable
cmd += " -u {0} -A {1}".format(user, account)
if start_date_time:
cmd += ' -S {}'.format(start_date_time.strftime('%m/%d/%y'))
cmd += ' -E {}'.format((start_date_time + timedelta(1)).strftime('%m/%d/%y'))
cmd += ' --state=COMPLETED' # filter for only completed jobs on that day
# to prevent duplicates
res = subprocess.getstatusoutput(cmd)[1].split('\n')
job_count = len(res)
user_stats = {
'memory-req': None,
'ideal-cpu-time': None,
'tlimit-req': None,
'memory-use': None,
'cpu-time': None,
'tlimit-use': None,
'job-count': job_count
}
# Get the resources required/used per job
for job in res:
job = job.split('|')
# Skip failed jobs
if len(job) != 13 or job[0] == 'JobID':
user_stats['job-count'] -= 1
continue
mem_req = None
mem_use = None
core_req = None
cpu_time = None
tlimit_req = None
tlimit_use = None
comment = None # contains GPU usage info
try:
mem_req = int(job[2].replace('M', ''))
mem_use = int(float(job[3].replace('M', '')))
if mem_req > 0:
if not user_stats['memory-req']:
user_stats['memory-req'] = 0
if not user_stats['memory-use']:
user_stats['memory-use'] = 0
user_stats['memory-req'] += mem_req
user_stats['memory-use'] += mem_use
except:
pass
try:
tlimit_req = int(timeStringToSeconds(job[6]))
tlimit_use = int(timeStringToSeconds(job[7]))
if tlimit_req > 0:
if not user_stats['tlimit-req']:
user_stats['tlimit-req'] = 0
if not user_stats['tlimit-use']:
user_stats['tlimit-use'] = 0
user_stats['tlimit-req'] += tlimit_req
user_stats['tlimit-use'] += tlimit_use
except:
pass
try:
core_req = int(job[4])
cpu_time = float(timeStringToSeconds(job[5]))
if core_req > 1 or track_single_core_jobs:
if not user_stats['ideal-cpu-time']:
user_stats['ideal-cpu-time'] = 0
if not user_stats['cpu-time']:
user_stats['cpu-time'] = 0
user_stats['ideal-cpu-time'] += (core_req * tlimit_use)
user_stats['cpu-time'] += cpu_time
except:
pass
# add row to gpuinfo (1 row per job)
gpustats = dict()
gpustats['username'] = user
gpustats['account'] = account
gpustats['date'] = job[10][0:job[10].index('T')]
gpustats['jobid'] = job[0]
comment = job[9]
allocGRES = job[11]
gpuinfo = None
try:
gpuinfo = json.loads(comment)
except:
pass
if gpuinfo is not None and allocGRES.find('gpu') != -1:
gpustats['ngpu'] = gpuinfo['ngpu']
gpustats['gputime'] = 60.0 * gpuinfo['step'] * gpuinfo['ngpu'] * gpuinfo['gpu_util'] / 100.0
try:
gpustats['idealgpu'] = gpuinfo['ngpu'] * tlimit_use
except:
# tlimit_use must be None
gpustats['idealgpu'] = gpuinfo['ngpu'] * 60.0 * gpuinfo['step']
# insert query
statement = 'INSERT IGNORE INTO gpuinfo ' + \
'(username,account,date,jobid,ngpu,gputime,idealgpu) ' + \
'VALUES (%s, %s, %s, %s, %s, %s, %s)'
args = (gpustats['username'], gpustats['account'], gpustats['date'],
gpustats['jobid'], gpustats['ngpu'], gpustats['gputime'],
gpustats['idealgpu'])
cursor.execute(statement, args)
return user_stats
def updateDatabase(user, account, stats, date, cursor):
"""
Desc: Insert some data into the database
Args:
user (string): Username associated with account
account (string): Slurm account of user
stats (dict): Dictionary of stats values
date (datetime): Datetime object for stats
cursor (cursor): SQL cursor
"""
statement = 'INSERT IGNORE INTO jobs (username, account, date, ' + \
'idealcpu, memoryreq, tlimitreq, cputime, tlimituse, ' + \
'memoryuse, jobsum) VALUES (%s, %s, %s, %s, %s, ' + \
'%s, %s, %s, %s, %s)'
args = (user, account, date, stats['ideal-cpu-time'], stats['memory-req'],
stats['tlimit-req'], stats['cpu-time'], stats['tlimit-use'],
stats['memory-use'], stats['job-count'])
cursor.execute(statement, args)
if __name__ == '__main__':
config = configparser.ConfigParser()
config.read(os.path.dirname(__file__) + str('/config.ini'))
username = None
password = None
host = None
TRACK_SINGLE_CORE_JOBS = False
try:
username = config['jobstats-db']['username']
host = config['jobstats-db']['host']
password = config['jobstats-db']['password']
TRACK_SINGLE_CORE_JOBS = config['jobstats-db']['TRACK_SINGLE_CORE_JOBS'] == 'True'
except:
print("Error: configuration values not found")
try:
from_date = datetime.strptime(sys.argv[1], '%m/%d/%y')
except:
print("{0} date")
exit()
print("Processing jobs for", from_date)
accounts = getSlurmAccounts()
conn = mariadb.connect(user=username, password=password, host=host, autocommit=True)
cursor = conn.cursor()
cursor.execute('USE jobstats')
account_total = len(accounts)
for account in accounts:
if account == 'orphan':
continue
user_total = len(getSlurmAccountUsers(account))
for user in getSlurmAccountUsers(account):
stats = getJobStats(cursor,
user,
account,
from_date,
TRACK_SINGLE_CORE_JOBS)
if stats['job-count'] != 0:
updateDatabase(user, account, stats, from_date, cursor)
conn.commit()
cursor.close()
conn.close()