| 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 |
import os |
| 48 |
import sys |
| 49 |
import time |
| 50 |
import socket |
| 51 |
import struct |
| 52 |
import logging |
| 53 |
import logging.handlers as loghand |
| 54 |
import threading |
| 55 |
|
| 56 |
SOCKPATH = b"\0atopgpud_nvidia" |
| 57 |
|
| 58 |
COMPUTE = 1 |
| 59 |
GRAPHIC = 2 |
| 60 |
|
| 61 |
HIGHSTAMP = 1000 |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
gpulist = [] |
| 67 |
cliterm = {} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
gpulock = None |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
class Stats(object): pass |
| 80 |
|
| 81 |
class GpuProp(object): |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
def __init__(self, num): |
| 87 |
gpuhandle = pynvml.nvmlDeviceGetHandleByIndex(num) |
| 88 |
pciInfo = pynvml.nvmlDeviceGetPciInfo(gpuhandle) |
| 89 |
|
| 90 |
self.gpuhandle = gpuhandle |
| 91 |
self.stats = Stats() |
| 92 |
|
| 93 |
self.stats.busid = pciInfo.busId |
| 94 |
self.stats.devname = pynvml.nvmlDeviceGetName(gpuhandle) |
| 95 |
|
| 96 |
|
| 97 |
if type(self.stats.busid) == bytes: |
| 98 |
self.stats.busid = self.stats.busid.decode('ascii', errors='replace') |
| 99 |
if type(self.stats.devname) == bytes: |
| 100 |
self.stats.devname = self.stats.devname.decode('ascii', errors='replace') |
| 101 |
|
| 102 |
self.stats.devname = self.stats.devname.replace(' ', '_') |
| 103 |
|
| 104 |
self.stats.tasksupport = 0 |
| 105 |
|
| 106 |
try: |
| 107 |
procinfo = pynvml.nvmlDeviceGetComputeRunningProcesses(gpuhandle) |
| 108 |
self.stats.tasksupport |= COMPUTE |
| 109 |
except Exception: |
| 110 |
pass |
| 111 |
|
| 112 |
try: |
| 113 |
procinfo = pynvml.nvmlDeviceGetGraphicsRunningProcesses(gpuhandle) |
| 114 |
self.stats.tasksupport |= GRAPHIC |
| 115 |
except Exception: |
| 116 |
pass |
| 117 |
|
| 118 |
self.stats.gpupercnow = 0 |
| 119 |
self.stats.mempercnow = 0 |
| 120 |
|
| 121 |
self.stats.memtotalnow = 0 |
| 122 |
self.stats.memusednow = 0 |
| 123 |
|
| 124 |
self.stats.gpusamples = 0 |
| 125 |
self.stats.gpuperccum = 0 |
| 126 |
self.stats.memperccum = 0 |
| 127 |
self.stats.memusedcum = 0 |
| 128 |
|
| 129 |
self.stats.procstats = {} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
def readstats(self): |
| 135 |
global HIGHSTAMP |
| 136 |
|
| 137 |
self.stats.gpusamples += 1 |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
try: |
| 143 |
rates = pynvml.nvmlDeviceGetUtilizationRates(self.gpuhandle) |
| 144 |
|
| 145 |
self.stats.gpupercnow = rates.gpu |
| 146 |
self.stats.mempercnow = rates.memory |
| 147 |
self.stats.gpuperccum += rates.gpu |
| 148 |
self.stats.memperccum += rates.memory |
| 149 |
except pynvml.NVMLError as err: |
| 150 |
self.stats.gpupercnow = -1 |
| 151 |
self.stats.mempercnow = -1 |
| 152 |
self.stats.gpuperccum = -1 |
| 153 |
self.stats.memperccum = -1 |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
try: |
| 159 |
meminfo = pynvml.nvmlDeviceGetMemoryInfo(self.gpuhandle) |
| 160 |
|
| 161 |
self.stats.memtotalnow = meminfo.total // 1024 |
| 162 |
self.stats.memusednow = meminfo.used // 1024 |
| 163 |
self.stats.memusedcum += meminfo.used // 1024 |
| 164 |
except pynvml.NVMLError as err: |
| 165 |
pass |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
try: |
| 171 |
if self.stats.tasksupport & COMPUTE: |
| 172 |
cprocinfo = pynvml.nvmlDeviceGetComputeRunningProcesses(self.gpuhandle) |
| 173 |
|
| 174 |
for proc in cprocinfo: |
| 175 |
proc.type = 'C' |
| 176 |
|
| 177 |
if self.stats.tasksupport & GRAPHIC: |
| 178 |
gprocinfo = pynvml.nvmlDeviceGetGraphicsRunningProcesses(self.gpuhandle) |
| 179 |
|
| 180 |
for proc in gprocinfo: |
| 181 |
proc.type = 'G' |
| 182 |
|
| 183 |
procinfo = cprocinfo + gprocinfo |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
actprocs = list(self.stats.procstats.keys()) |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
for proc in procinfo: |
| 196 |
pid = proc.pid |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
if pid not in actprocs: |
| 203 |
self.stats.procstats[pid] = Stats() |
| 204 |
|
| 205 |
self.stats.procstats[pid].samples = 0 |
| 206 |
self.stats.procstats[pid].type = 'U' |
| 207 |
|
| 208 |
self.stats.procstats[pid].memnow = 0 |
| 209 |
self.stats.procstats[pid].memcum = 0 |
| 210 |
|
| 211 |
self.stats.procstats[pid].gpubusy = -1 |
| 212 |
self.stats.procstats[pid].membusy = -1 |
| 213 |
self.stats.procstats[pid].gpubusycum = -1 |
| 214 |
self.stats.procstats[pid].membusycum = -1 |
| 215 |
else: |
| 216 |
actprocs.remove(pid) |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
self.stats.procstats[pid].samples += 1 |
| 222 |
self.stats.procstats[pid].type = proc.type |
| 223 |
|
| 224 |
if proc.usedGpuMemory: |
| 225 |
self.stats.procstats[pid].memnow = proc.usedGpuMemory // 1024 |
| 226 |
self.stats.procstats[pid].memcum += proc.usedGpuMemory // 1024 |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
try: |
| 234 |
procutils = pynvml.nvmlDeviceGetProcessUtilization(self.gpuhandle, HIGHSTAMP) |
| 235 |
|
| 236 |
if procutils and len(procutils) > 0: |
| 237 |
for proc in procutils: |
| 238 |
if HIGHSTAMP < proc.timeStamp: |
| 239 |
HIGHSTAMP = proc.timeStamp |
| 240 |
|
| 241 |
if proc.pid in self.stats.procstats: |
| 242 |
self.stats.procstats[proc.pid].gpubusy = proc.smUtil |
| 243 |
self.stats.procstats[proc.pid].membusy = proc.memUtil |
| 244 |
|
| 245 |
self.stats.procstats[proc.pid].gpubusycum += proc.smUtil |
| 246 |
self.stats.procstats[proc.pid].membusycum += proc.memUtil |
| 247 |
except Exception as e: |
| 248 |
pass |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
for pid in actprocs: |
| 256 |
for client in cliterm: |
| 257 |
cliterm[client][pid] = self.stats.procstats[pid] |
| 258 |
|
| 259 |
del self.stats.procstats[pid] |
| 260 |
|
| 261 |
except pynvml.NVMLError as err: |
| 262 |
pass |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
def getstats(self): |
| 269 |
return self.stats |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
def main(): |
| 276 |
global gpulock |
| 277 |
gpulock = threading.Lock() |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
try: |
| 285 |
pynvml.nvmlInit() |
| 286 |
except Exception: |
| 287 |
logging.error("Shared lib 'libnvidia-ml' probably not installed!") |
| 288 |
sys.exit() |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
try: |
| 294 |
mysock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM, 0) |
| 295 |
except Exception as sockex: |
| 296 |
logging.error("Socket creation fails") |
| 297 |
sys.exit(1) |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
try: |
| 304 |
mysock.bind(SOCKPATH) |
| 305 |
mysock.listen(32) |
| 306 |
except Exception as sockex: |
| 307 |
logging.error("Socket binding to path %s fails", SOCKPATH) |
| 308 |
sys.exit(1) |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
try: |
| 315 |
pynvml.nvmlInit() |
| 316 |
except Exception: |
| 317 |
pass |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
gpunum = pynvml.nvmlDeviceGetCount() |
| 324 |
logging.info("Number of GPUs: %d", gpunum) |
| 325 |
|
| 326 |
if gpunum == 0: |
| 327 |
logging.info("Terminated (no GPUs available)") |
| 328 |
sys.exit() |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
for i in range(gpunum): |
| 334 |
gpulist.append( GpuProp(i) ) |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
t = threading.Thread(target=gpuscanner, args=(1,)) |
| 341 |
t.daemon = True |
| 342 |
t.start() |
| 343 |
|
| 344 |
logging.info("Initialization succeeded") |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
while True: |
| 351 |
newsock, peeraddr = mysock.accept() |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
t = threading.Thread(target=serveclient, args=(newsock, peeraddr)) |
| 358 |
t.daemon = True |
| 359 |
t.start() |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
def serveclient(sock, peer): |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
with gpulock: |
| 399 |
cliterm[sock] = {} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
while True: |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
try: |
| 409 |
rcvbuf = sock.recv(20) |
| 410 |
except Exception as sockex: |
| 411 |
logging.error("Receive error: %s", sockex) |
| 412 |
sock.close() |
| 413 |
break |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
if not rcvbuf: |
| 419 |
sock.close() |
| 420 |
break |
| 421 |
|
| 422 |
logging.debug("Received: %s", rcvbuf) |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
if len(rcvbuf) != 2: |
| 428 |
logging.error('Wrong request length: %d', len(rcvbuf)) |
| 429 |
sock.close() |
| 430 |
break |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
command = chr(rcvbuf[0]) |
| 437 |
version = rcvbuf[1] |
| 438 |
|
| 439 |
if command == 'S': |
| 440 |
if version == 0 or version >= len(gpuformatters): |
| 441 |
version = len(gpuformatters)-1 |
| 442 |
|
| 443 |
xmitbuf = gpuformatters[version](sock).encode('ascii', |
| 444 |
errors='replace') |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
elif command == 'T': |
| 451 |
if version == 0 or version >= len(gpudevnames): |
| 452 |
version = len(gpudevnames)-1 |
| 453 |
|
| 454 |
xmitbuf = gpudevnames[version]().encode('ascii', errors='replace') |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
else: |
| 460 |
logging.error('Wrong request from client: %s', command) |
| 461 |
sock.close() |
| 462 |
break |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
prelude = struct.pack(">I", (version << 24) + len(xmitbuf)) |
| 469 |
|
| 470 |
try: |
| 471 |
sock.send(prelude) |
| 472 |
sock.send(xmitbuf) |
| 473 |
|
| 474 |
logging.debug("Send: %s", prelude + xmitbuf) |
| 475 |
except Exception as sockex: |
| 476 |
logging.error("Send error: %s", sockex) |
| 477 |
sock.close() |
| 478 |
break |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
with gpulock: |
| 485 |
del cliterm[sock] |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
def gpudevname_v1(): |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
strbuf = str( len(gpulist) ) |
| 504 |
|
| 505 |
with gpulock: |
| 506 |
for i, gpu in enumerate(gpulist): |
| 507 |
s = gpu.getstats() |
| 508 |
strbuf += "@{:s} {:s} {:d}".format(s.busid, s.devname, s.tasksupport) |
| 509 |
|
| 510 |
return strbuf |
| 511 |
|
| 512 |
gpudevnames = [None, gpudevname_v1, gpudevname_v1] |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
def gpuformatter_v1(clisock): |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
strbuf = "" |
| 528 |
|
| 529 |
with gpulock: |
| 530 |
for i, gpu in enumerate(gpulist): |
| 531 |
s = gpu.getstats() |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
strbuf += "@{:d} {:d} {:d} {:d} {:d} {:d} {:d} {:d}".format( |
| 537 |
s.gpupercnow, s.mempercnow, |
| 538 |
s.memtotalnow, s.memusednow, s.gpusamples, |
| 539 |
s.gpuperccum, s.memperccum, s.memusedcum); |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
for pid, stat in s.procstats.items(): |
| 546 |
strbuf += "#A {:d} {:d} {:d} {:d} {:d} {:d} {:d}".format(pid, |
| 547 |
stat.gpubusy, stat.membusy, 0, |
| 548 |
stat.memnow, stat.memcum, stat.samples) |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
for pid, stat in cliterm[clisock].items(): |
| 555 |
strbuf += "#E {:d} {:d} {:d} {:d} {:d} {:d} {:d}".format(pid, |
| 556 |
stat.gpubusy, stat.membusy, 0, |
| 557 |
stat.memnow, stat.memcum, stat.samples) |
| 558 |
|
| 559 |
cliterm[clisock].clear() |
| 560 |
|
| 561 |
|
| 562 |
return strbuf |
| 563 |
|
| 564 |
|
| 565 |
def gpuformatter_v2(clisock): |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
strbuf = "" |
| 574 |
|
| 575 |
with gpulock: |
| 576 |
for i, gpu in enumerate(gpulist): |
| 577 |
s = gpu.getstats() |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
strbuf += "@{:d} {:d} {:d} {:d} {:d} {:d} {:d} {:d}".format( |
| 583 |
s.gpupercnow, s.mempercnow, |
| 584 |
s.memtotalnow, s.memusednow, s.gpusamples, |
| 585 |
s.gpuperccum, s.memperccum, s.memusedcum); |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
for pid, stat in s.procstats.items(): |
| 592 |
strbuf += "#A {:d} {:s} {:d} {:d} {:d} {:d} {:d}".format(pid, |
| 593 |
stat.type, |
| 594 |
stat.gpubusycum, stat.membusycum, |
| 595 |
stat.memnow, stat.memcum, stat.samples) |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
for pid, stat in cliterm[clisock].items(): |
| 602 |
strbuf += "#E {:d} {:s} {:d} {:d} {:d} {:d} {:d}".format(pid, |
| 603 |
stat.type, |
| 604 |
stat.gpubusycum, stat.membusycum, |
| 605 |
stat.memnow, stat.memcum, stat.samples) |
| 606 |
|
| 607 |
cliterm[clisock].clear() |
| 608 |
|
| 609 |
return strbuf |
| 610 |
|
| 611 |
gpuformatters = [None, gpuformatter_v1, gpuformatter_v2] |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
def gpuscanner(scaninterval): |
| 620 |
realtimenext = time.time() + scaninterval |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
while True: |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
with gpulock: |
| 630 |
for gpu in gpulist: |
| 631 |
gpu.readstats() |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
sleeptime = realtimenext - time.time() |
| 640 |
|
| 641 |
if sleeptime > 0: |
| 642 |
time.sleep(sleeptime) |
| 643 |
|
| 644 |
realtimenext += scaninterval |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
if '-v' in sys.argv: |
| 653 |
loglevel = logging.DEBUG |
| 654 |
else: |
| 655 |
loglevel = logging.INFO |
| 656 |
|
| 657 |
fm = logging.Formatter('atopgpud %(levelname)s: %(message)s') |
| 658 |
|
| 659 |
fh = loghand.SysLogHandler('/dev/log', |
| 660 |
facility=loghand.SysLogHandler.LOG_DAEMON) |
| 661 |
fh.setFormatter(fm) |
| 662 |
fh.setLevel(loglevel) |
| 663 |
|
| 664 |
lg = logging.getLogger() |
| 665 |
lg.addHandler(fh) |
| 666 |
lg.setLevel(loglevel) |
| 667 |
|
| 668 |
if '-f' not in sys.argv: |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
try: |
| 674 |
if os.fork(): |
| 675 |
sys.exit(0) |
| 676 |
except Exception: |
| 677 |
logging.error("Failed to fork child") |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
try: |
| 683 |
import pynvml |
| 684 |
except Exception: |
| 685 |
logging.error("Python module 'pynvml' not installed!") |
| 686 |
sys.exit(1) |
| 687 |
|
| 688 |
|
| 689 |
try: |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
main() |
| 694 |
|
| 695 |
finally: |
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
try: |
| 700 |
pynvml.nvmlShutdown() |
| 701 |
except Exception: |
| 702 |
pass |