tracker
resource_tracker.tracker
#
Track resource usage of a process or server.
Classes:
Name | Description |
---|---|
PidTracker |
Track resource usage of a process and optionally its children. |
SystemTracker |
Track system-wide resource usage. |
Functions:
Name | Description |
---|---|
is_partition |
Determine if a disk name represents a partition rather than a whole disk. |
get_pid_children |
Get all descendant processes recursively. |
get_pid_rss |
Get the current resident set size of a process. |
get_pid_pss_rollup |
Reads the total PSS from |
get_pid_proc_times |
Get the current user and system times of a process from |
get_pid_proc_io |
Get the total bytes read and written by a process from |
get_pid_stats |
Collect current/cumulative stats of a process from procfs. |
get_system_stats |
Collect current system-wide stats from procfs. |
is_partition
cached
#
Determine if a disk name represents a partition rather than a whole disk.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
disk_name
|
str
|
Name of the disk device (e.g., 'sda1', 'nvme0n1p1') |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the device is likely a partition, False otherwise |
Source code in resource_tracker/tracker.py
get_pid_children
#
Get all descendant processes recursively.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pid
|
int
|
The process ID to get descendant processes for. |
required |
Returns:
Type | Description |
---|---|
set[int]
|
All descendant process ids. |
Source code in resource_tracker/tracker.py
get_pid_rss
#
Get the current resident set size of a process.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pid
|
int
|
The process ID to get the resident set size for. |
required |
Returns:
Type | Description |
---|---|
int
|
The current resident set size of the process in kB. |
Source code in resource_tracker/tracker.py
get_pid_pss_rollup
#
Reads the total PSS from /proc/{pid}/smaps_rollup
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pid
|
int
|
The process ID to get the total PSS for. |
required |
Returns:
Type | Description |
---|---|
int
|
The total PSS in kB. |
Source code in resource_tracker/tracker.py
get_pid_proc_times
#
Get the current user and system times of a process from /proc/{pid}/stat
.
Note that cannot use cutime
/cstime
for real-time monitoring,
as they need to wait for the children to exit.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pid
|
int
|
Process ID to track |
required |
children
|
bool
|
Whether to include stats from exited child processes |
True
|
Returns:
Type | Description |
---|---|
dict[str, int]
|
A dictionary containing process time information:
- |
Source code in resource_tracker/tracker.py
get_pid_proc_io
#
Get the total bytes read and written by a process from /proc/{pid}/io
.
Note that it is not tracking reading from memory-mapped objects,
and is fairly limited in what it can track. E.g. the process might
not even have permissions to read its own /proc/self/io
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pid
|
int
|
The process ID to get the total bytes read and written for. |
required |
Returns:
Type | Description |
---|---|
dict[str, int]
|
A dictionary containing the total bytes read and written by the process. |
Source code in resource_tracker/tracker.py
get_pid_stats
#
Collect current/cumulative stats of a process from procfs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pid
|
int
|
The process ID to track. |
required |
children
|
bool
|
Whether to include child processes. |
True
|
Returns:
Type | Description |
---|---|
dict[str, int | float | None | set[int]]
|
A dictionary containing process stats:
|
Source code in resource_tracker/tracker.py
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 |
|
get_system_stats
#
Collect current system-wide stats from procfs.
Returns:
Type | Description |
---|---|
dict[str, int | float | dict]
|
A dictionary containing system stats:
|
Source code in resource_tracker/tracker.py
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 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 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
|
PidTracker
#
Track resource usage of a process and optionally its children.
This class monitors system resources like CPU times and usage, memory usage, GPU and VRAM utilization, I/O operations for a given process ID and optionally its child processes.
Data is collected every interval
seconds and written to the stdout or
output_file
(if provided) as CSV. Currently, the following columns are
tracked:
- timestamp (float): The current timestamp.
- pid (int): The monitored process ID.
- children (int | None): The current number of child processes.
- utime (int): The total user+nice mode CPU time in clock ticks.
- stime (int): The total system mode CPU time in clock ticks.
- cpu_usage (float): The current CPU usage between 0 and number of CPUs.
- pss (int): The current PSS (Proportional Set Size) in kB.
- read_bytes (int): The total number of bytes read from disk.
- write_bytes (int): The total number of bytes written to disk.
- gpu_usage (float): The current GPU utilization between 0 and GPU count.
- gpu_vram (float): The current GPU memory used in MiB.
- gpu_utilized (int): The number of GPUs with utilization > 0.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pid
|
int
|
Process ID to track. Defaults to current process ID. |
getpid()
|
interval
|
float
|
Sampling interval in seconds. Defaults to 1. |
1
|
children
|
bool
|
Whether to track child processes. Defaults to True. |
True
|
autostart
|
bool
|
Whether to start tracking immediately. Defaults to True. |
True
|
output_file
|
str
|
File to write the output to. Defaults to None, print to stdout. |
None
|
Methods:
Name | Description |
---|---|
__call__ |
Dummy method to make this class callable. |
diff_stats |
Calculate stats since last call. |
start_tracking |
Start an infinite loop tracking resource usage of the process until it exits. |
Source code in resource_tracker/tracker.py
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 |
|
__call__
#
diff_stats
#
Calculate stats since last call.
Source code in resource_tracker/tracker.py
start_tracking
#
Start an infinite loop tracking resource usage of the process until it exits.
A CSV line is written every interval
seconds.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_file
|
Optional[str]
|
File to write the output to. Defaults to None, printing to stdout. |
None
|
print_header
|
bool
|
Whether to print the header of the CSV. Defaults to True. |
True
|
Source code in resource_tracker/tracker.py
SystemTracker
#
Track system-wide resource usage.
This class monitors system resources like CPU times and usage, memory usage, GPU and VRAM utilization, disk I/O, and network traffic for the entire system.
Data is collected every interval
seconds and written to the stdout or
output_file
(if provided) as CSV. Currently, the following columns are
tracked:
- timestamp (float): The current timestamp.
- processes (int): The number of running processes.
- utime (int): The total user+nice mode CPU time in clock ticks.
- stime (int): The total system mode CPU time in clock ticks.
- cpu_usage (float): The current CPU usage between 0 and number of CPUs.
- memory_free (int): The amount of free memory in kB.
- memory_used (int): The amount of used memory in kB.
- memory_buffers (int): The amount of memory used for buffers in kB.
- memory_cached (int): The amount of memory used for caching in kB.
- memory_active_anon (int): The amount of memory used for anonymous pages in kB.
- memory_inactive_anon (int): The amount of memory used for inactive anonymous pages in kB.
- disk_read_bytes (int): The total number of bytes read from disk.
- disk_write_bytes (int): The total number of bytes written to disk.
- disk_space_total_gb (float): The total disk space in GB.
- disk_space_used_gb (float): The used disk space in GB.
- disk_space_free_gb (float): The free disk space in GB.
- net_recv_bytes (int): The total number of bytes received over network.
- net_sent_bytes (int): The total number of bytes sent over network.
- gpu_usage (float): The current GPU utilization between 0 and GPU count.
- gpu_vram (float): The current GPU memory used in MiB.
- gpu_utilized (int): The number of GPUs with utilization > 0.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interval
|
float
|
Sampling interval in seconds. Defaults to 1. |
1
|
autostart
|
bool
|
Whether to start tracking immediately. Defaults to True. |
True
|
output_file
|
str
|
File to write the output to. Defaults to None, print to stdout. |
None
|
Methods:
Name | Description |
---|---|
__call__ |
Dummy method to make this class callable. |
diff_stats |
Calculate stats since last call. |
start_tracking |
Start an infinite loop tracking system resource usage. |
Source code in resource_tracker/tracker.py
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 |
|
__call__
#
diff_stats
#
Calculate stats since last call.
Source code in resource_tracker/tracker.py
start_tracking
#
Start an infinite loop tracking system resource usage.
A CSV line is written every interval
seconds.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_file
|
Optional[str]
|
File to write the output to. Defaults to None, printing to stdout. |
None
|
print_header
|
bool
|
Whether to print the header of the CSV. Defaults to True. |
True
|