resource_tracker
resource_tracker
#
Resource Tracker package for monitoring resource usage, detecting cloud environments, and more.
Modules:
Name | Description |
---|---|
cloud_info |
Detect cloud environment (provider, region, instance type) via VM metadata services. |
dummy_workloads |
|
helpers |
General helpers. |
keeper |
|
nvidia |
Helpers to monitor NVIDIA GPUs. |
report |
|
server_info |
Detect server hardware (CPU count, memory amount, disk space, GPU count and VRAM amount) via |
tiny_bars |
A tiny, partial, and opinionated implementation of the Handlebars template engine. |
tiny_data_frame |
A very inefficient data-frame implementation for manipulating resource usage data. |
tracker |
Track resource usage of a process and/or the system. |
tracker_procfs |
Helpers to track resource usage via |
tracker_psutil |
Helpers to track resource usage via |
Classes:
Name | Description |
---|---|
ProcessTracker |
Track resource usage of a process and optionally its children. |
ResourceTracker |
Track resource usage of processes and the system in a non-blocking way. |
SystemTracker |
Track system-wide resource usage. |
Functions:
Name | Description |
---|---|
get_cloud_info |
Detect cloud environment and return standardized information on provider, region, and instance type. |
get_server_info |
Collects important information about the Linux server. |
get_cloud_info
cached
#
Detect cloud environment and return standardized information on provider, region, and instance type.
Returns:
Type | Description |
---|---|
dict
|
A dictionary containing standardized cloud information:
|
Source code in resource_tracker/cloud_info.py
get_server_info
#
Collects important information about the Linux server.
Returns:
Type | Description |
---|---|
dict
|
A dictionary containing server information:
|
Source code in resource_tracker/server_info.py
ProcessTracker
#
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 seconds.
- stime (int): The total system mode CPU time in seconds.
- cpu_usage (float): The current CPU usage between 0 and number of CPUs.
- memory (int): The current memory usage in kB. Implementation depends on the operating system, and it is preferably PSS (Proportional Set Size) on Linux, USS (Unique Set Size) on macOS and Windows, and RSS (Resident Set Size) on Windows.
- 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()
|
start_time
|
float
|
Time when to start tracking. Defaults to current time. |
time()
|
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
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 |
|
__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
ResourceTracker
#
Track resource usage of processes and the system in a non-blocking way.
Start a resource_tracker.ProcessTracker and/or a resource_tracker.SystemTracker in the background as spawned
or forked process(es), and make the collected data available easily in the
main process via the process_metrics
and system_metrics
properties.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pid
|
int
|
Process ID to track. Defaults to current process ID. |
getpid()
|
children
|
bool
|
Whether to track child processes. Defaults to True. |
True
|
interval
|
float
|
Sampling interval in seconds. Defaults to 1. |
1
|
method
|
Optional[str]
|
Multiprocessing method. Defaults to None, which tries to fork on Linux and macOS, and spawn on Windows. |
None
|
autostart
|
bool
|
Whether to start tracking immediately. Defaults to True. |
True
|
track_processes
|
bool
|
Whether to track resource usage at the process level. Defaults to True. |
True
|
track_system
|
bool
|
Whether to track system-wide resource usage. Defaults to True. |
True
|
discover_server
|
bool
|
Whether to discover the server specs in the background at startup. Defaults to True. |
True
|
discover_cloud
|
bool
|
Whether to discover the cloud environment in the background at startup. Defaults to True. |
True
|
Example:
>>> from resource_tracker.dummy_workloads import cpu_single, cpu_multi
>>> tracker = ResourceTracker()
>>> cpu_single()
>>> tracker.recommend_resources() # doctest: +SKIP
{'cpu': 1, 'memory': 128, 'gpu': 0, 'vram': 0}
>>> tracker = ResourceTracker()
>>> while tracker.n_samples == 0:
... cpu_multi(duration=0.25, ncores=2)
>>> tracker.recommend_resources() # multiprocessing is not enough efficient on Windows/macOS # doctest: +SKIP
{'cpu': 2, 'memory': 128, 'gpu': 0, 'vram': 0}
Methods:
Name | Description |
---|---|
start |
Start the selected resource trackers in the background as subprocess(es). |
stop |
Stop the previously started resource trackers' background processes. |
snapshot |
Collect the current state of the resource tracker. |
from_snapshot |
Create a ResourceTracker from a snapshot. |
dumps |
Serialize the resource tracker to a JSON string. |
loads |
Deserialize the resource tracker from a JSON string. |
dump |
Serialize the resource tracker to a gzipped JSON file. |
load |
Deserialize the resource tracker from a gzipped JSON file. |
get_combined_metrics |
Collected data both from the resource_tracker.ProcessTracker and resource_tracker.SystemTracker. |
stats |
Collect statistics from the resource tracker. |
wait_for_samples |
Wait for at least one sample to be collected. |
recommend_resources |
Recommend optimal resource allocation based on the measured resource tracker data. |
recommend_server |
Recommend the cheapest cloud server matching the recommended resources. |
Attributes:
Name | Type | Description |
---|---|---|
n_samples |
int
|
Number of samples collected by the resource tracker. |
server_info |
dict
|
High-level server info. |
cloud_info |
dict
|
High-level cloud info. |
process_metrics |
TinyDataFrame
|
Collected data from resource_tracker.ProcessTracker. |
system_metrics |
TinyDataFrame
|
Collected data from resource_tracker.SystemTracker. |
running |
bool
|
Check if the resource tracker is running. |
Source code in resource_tracker/tracker.py
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 550 551 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 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 |
|
start
#
Start the selected resource trackers in the background as subprocess(es).
Source code in resource_tracker/tracker.py
stop
#
Stop the previously started resource trackers' background processes.
Source code in resource_tracker/tracker.py
server_info
property
#
High-level server info.
Collected data from resource_tracker.get_server_info plus a guess for the allocation type of the server: if it's dedicated to the tracked process(es) or shared with other processes. The guess is based on the [resource_tracker.column_maps.SERVER_ALLOCATION_CHECKS][] checks.
cloud_info
property
#
High-level cloud info.
Collected data from resource_tracker.get_cloud_info.
process_metrics
property
#
Collected data from resource_tracker.ProcessTracker.
Returns:
Type | Description |
---|---|
TinyDataFrame
|
A resource_tracker.tiny_data_frame.TinyDataFrame object containing the collected data or an empty list if the resource_tracker.ProcessTracker is not running. |
system_metrics
property
#
Collected data from resource_tracker.SystemTracker.
Returns:
Type | Description |
---|---|
TinyDataFrame
|
A resource_tracker.tiny_data_frame.TinyDataFrame object containing the collected data or an empty list if the resource_tracker.SystemTracker is not running. |
snapshot
#
Collect the current state of the resource tracker.
Returns:
Type | Description |
---|---|
dict
|
A dictionary containing the current state of the resource tracker. |
Source code in resource_tracker/tracker.py
from_snapshot
classmethod
#
Create a ResourceTracker from a snapshot.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
snapshot
|
dict
|
A dictionary containing the current state of the resource tracker, created by resource_tracker.ResourceTracker.snapshot. |
required |
Source code in resource_tracker/tracker.py
dumps
#
Serialize the resource tracker to a JSON string.
Returns:
Type | Description |
---|---|
str
|
A JSON string containing the current state of the resource tracker. |
loads
classmethod
#
Deserialize the resource tracker from a JSON string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s
|
str
|
The JSON string to deserialize the resource tracker from. |
required |
dump
#
Serialize the resource tracker to a gzipped JSON file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
str
|
The path to the file to write the serialized resource tracker to. |
required |
Source code in resource_tracker/tracker.py
load
classmethod
#
Deserialize the resource tracker from a gzipped JSON file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
str
|
The path to the file to read the serialized resource tracker from. |
required |
Source code in resource_tracker/tracker.py
get_combined_metrics
#
Collected data both from the resource_tracker.ProcessTracker and resource_tracker.SystemTracker.
This is effectively binding the two dataframes together by timestamp, and adding a prefix to the column names to distinguish between the system and process metrics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bytes
|
bool
|
Whether to convert all metrics (e.g. memory, VRAM, disk usage) to bytes. Defaults to False, reporting as documented at resource_tracker.ProcessTracker and resource_tracker.SystemTracker (kB, MiB, or GiB). |
False
|
human_names
|
bool
|
Whether to rename the columns to use human-friendly names. Defaults to False, reporting as documented at resource_tracker.ProcessTracker and resource_tracker.SystemTracker with prefixes. |
False
|
system_prefix
|
Optional[str]
|
Prefix to add to the system-level column names. Defaults to "system_" or "System " based on the value of |
None
|
process_prefix
|
Optional[str]
|
Prefix to add to the process-level column names. Defaults to "process_" or "Process " based on the value of |
None
|
Returns:
Type | Description |
---|---|
TinyDataFrame
|
A resource_tracker.tiny_data_frame.TinyDataFrame object containing the combined data or an empty list if tracker(s) not running. |
Source code in resource_tracker/tracker.py
stats
#
stats(specs=[StatSpec(column='process_cpu_usage', agg=mean, round=2), StatSpec(column='process_cpu_usage', agg=max, round=2), StatSpec(column='process_memory', agg=mean, round=2), StatSpec(column='process_memory', agg=max, round=2), StatSpec(column='process_gpu_usage', agg=mean, round=2), StatSpec(column='process_gpu_usage', agg=max, round=2), StatSpec(column='process_gpu_vram', agg=mean, round=2), StatSpec(column='process_gpu_vram', agg=max, round=2), StatSpec(column='process_gpu_utilized', agg=mean, round=2), StatSpec(column='process_gpu_utilized', agg=max, round=2), StatSpec(column='system_disk_space_used_gb', agg=max, round=2), StatSpec(column='system_net_recv_bytes', agg=sum), StatSpec(column='system_net_sent_bytes', agg=sum), StatSpec(column='timestamp', agg=lambda x: max(x) - min(x), agg_name='duration')])
Collect statistics from the resource tracker.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
specs
|
List[StatSpec]
|
A list of resource_tracker.tiny_data_frame.StatSpec objects specifying the statistics to collect. |
[StatSpec(column='process_cpu_usage', agg=mean, round=2), StatSpec(column='process_cpu_usage', agg=max, round=2), StatSpec(column='process_memory', agg=mean, round=2), StatSpec(column='process_memory', agg=max, round=2), StatSpec(column='process_gpu_usage', agg=mean, round=2), StatSpec(column='process_gpu_usage', agg=max, round=2), StatSpec(column='process_gpu_vram', agg=mean, round=2), StatSpec(column='process_gpu_vram', agg=max, round=2), StatSpec(column='process_gpu_utilized', agg=mean, round=2), StatSpec(column='process_gpu_utilized', agg=max, round=2), StatSpec(column='system_disk_space_used_gb', agg=max, round=2), StatSpec(column='system_net_recv_bytes', agg=sum), StatSpec(column='system_net_sent_bytes', agg=sum), StatSpec(column='timestamp', agg=lambda x: max(x) - min(x), agg_name='duration')]
|
Returns:
Type | Description |
---|---|
dict
|
A dictionary containing the collected statistics. |
Source code in resource_tracker/tracker.py
running
property
#
Check if the resource tracker is running.
Returns:
Type | Description |
---|---|
bool
|
True if the resource tracker is running, False if already stopped. |
wait_for_samples
#
Wait for at least one sample to be collected.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n
|
int
|
The minimum number of samples to collect. Defaults to 1. |
1
|
timeout
|
float
|
The maximum time to wait for a sample. Defaults to 5 seconds. |
5
|
Source code in resource_tracker/tracker.py
recommend_resources
#
Recommend optimal resource allocation based on the measured resource tracker data.
The recommended resources are based on the following rules:
- target average CPU usage of the process(es)
- target maximum memory usage of the process(es) with a 20% buffer
- target maximum number of GPUs used by the process(es)
- target maximum VRAM usage of the process(es) with a 20% buffer
Parameters:
Name | Type | Description | Default |
---|---|---|---|
historical_stats
|
List[dict]
|
Optional list of historical statistics (as returned by resource_tracker.ResourceTracker.stats) to consider when making recommendations. These will be combined with the current stats. |
[]
|
Returns:
Type | Description |
---|---|
dict
|
A dictionary containing the recommended resources (cpu, memory, gpu, vram). |
Source code in resource_tracker/tracker.py
recommend_server
#
Recommend the cheapest cloud server matching the recommended resources.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
Additional filtering arguments (e.g. vendor_id or compliance_framework_id) to pass to the Spare Cores Keeper API. |
{}
|
Returns:
Type | Description |
---|---|
dict
|
A dictionary containing the recommended cloud server. Response format is described at https://keeper.sparecores.net/redoc#tag/Query-Resources/operation/search_servers_servers_get. |
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 seconds.
- stime (int): The total system mode CPU time in seconds.
- 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 (int): The amount of memory used for active pages in kB.
- memory_inactive (int): The amount of memory used for inactive 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 |
---|---|---|---|
start_time
|
float
|
Time when to start tracking. Defaults to current time. |
time()
|
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
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 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
|
__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
|