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 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 429 430 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 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 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
use std::time::{Duration, Instant};
use tokio::runtime;
#[cfg(any(docsrs, all(tokio_unstable, feature = "rt")))]
#[cfg_attr(docsrs, doc(cfg(all(tokio_unstable, feature = "rt"))))]
/// Monitors key metrics of the tokio runtime.
///
/// ### Usage
/// ```
/// use std::time::Duration;
/// use tokio_metrics::RuntimeMonitor;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
/// let handle = tokio::runtime::Handle::current();
///
/// // print runtime metrics every 500ms
/// {
/// let runtime_monitor = RuntimeMonitor::new(&handle);
/// tokio::spawn(async move {
/// for interval in runtime_monitor.intervals() {
/// // pretty-print the metric interval
/// println!("{:?}", interval);
/// // wait 500ms
/// tokio::time::sleep(Duration::from_millis(500)).await;
/// }
/// });
/// }
///
/// // await some tasks
/// tokio::join![
/// do_work(),
/// do_work(),
/// do_work(),
/// ];
///
/// Ok(())
/// }
///
/// async fn do_work() {
/// for _ in 0..25 {
/// tokio::task::yield_now().await;
/// tokio::time::sleep(Duration::from_millis(100)).await;
/// }
/// }
/// ```
#[derive(Debug)]
pub struct RuntimeMonitor {
/// Handle to the runtime
runtime: runtime::RuntimeMetrics,
}
#[cfg(any(docsrs, all(tokio_unstable, feature = "rt")))]
#[cfg_attr(docsrs, doc(cfg(all(tokio_unstable, feature = "rt"))))]
/// Key runtime metrics.
#[non_exhaustive]
#[derive(Default, Debug, Clone)]
pub struct RuntimeMetrics {
/// The number of worker threads used by the runtime.
///
/// This metric is static for a runtime.
///
/// This metric is always equal to [`tokio::runtime::RuntimeMetrics::num_workers`].
/// When using the `current_thread` runtime, the return value is always `1`.
///
/// The number of workers is set by configuring
/// [`worker_threads`][`tokio::runtime::Builder::worker_threads`] with
/// [`tokio::runtime::Builder`], or by parameterizing [`tokio::main`].
///
/// ##### Examples
/// In the below example, the number of workers is set by parameterizing [`tokio::main`]:
/// ```
/// use tokio::runtime::Handle;
///
/// #[tokio::main(flavor = "multi_thread", worker_threads = 10)]
/// async fn main() {
/// let handle = tokio::runtime::Handle::current();
/// let monitor = tokio_metrics::RuntimeMonitor::new(&handle);
/// let mut intervals = monitor.intervals();
/// let mut next_interval = || intervals.next().unwrap();
///
/// assert_eq!(next_interval().workers_count, 10);
/// }
/// ```
///
/// [`tokio::main`]: https://docs.rs/tokio/latest/tokio/attr.main.html
///
/// When using the `current_thread` runtime, the return value is always `1`; e.g.:
/// ```
/// use tokio::runtime::Handle;
///
/// #[tokio::main(flavor = "current_thread")]
/// async fn main() {
/// let handle = tokio::runtime::Handle::current();
/// let monitor = tokio_metrics::RuntimeMonitor::new(&handle);
/// let mut intervals = monitor.intervals();
/// let mut next_interval = || intervals.next().unwrap();
///
/// assert_eq!(next_interval().workers_count, 1);
/// }
/// ```
///
/// This metric is always equal to [`tokio::runtime::RuntimeMetrics::num_workers`]; e.g.:
/// ```
/// use tokio::runtime::Handle;
///
/// #[tokio::main]
/// async fn main() {
/// let handle = Handle::current();
/// let monitor = tokio_metrics::RuntimeMonitor::new(&handle);
/// let mut intervals = monitor.intervals();
/// let mut next_interval = || intervals.next().unwrap();
///
/// assert_eq!(next_interval().workers_count, handle.metrics().num_workers());
/// }
/// ```
pub workers_count: usize,
/// The number of times worker threads parked.
///
/// The worker park count increases by one each time the worker parks the thread waiting for
/// new inbound events to process. This usually means the worker has processed all pending work
/// and is currently idle.
///
/// ##### Definition
/// This metric is derived from the sum of [`tokio::runtime::RuntimeMetrics::worker_park_count`]
/// across all worker threads.
///
/// ##### See also
/// - [`RuntimeMetrics::max_park_count`]
/// - [`RuntimeMetrics::min_park_count`]
///
/// ##### Examples
/// ```
/// #[tokio::main(flavor = "multi_thread", worker_threads = 2)]
/// async fn main() {
/// let handle = tokio::runtime::Handle::current();
/// let monitor = tokio_metrics::RuntimeMonitor::new(&handle);
/// let mut intervals = monitor.intervals();
/// let mut next_interval = || intervals.next().unwrap();
///
/// let interval = next_interval(); // end of interval 1
/// assert_eq!(interval.total_park_count, 0);
///
/// induce_parks().await;
///
/// let interval = next_interval(); // end of interval 2
/// assert!(interval.total_park_count >= 1); // usually 1 or 2 parks
/// }
///
/// async fn induce_parks() {
/// let _ = tokio::time::timeout(std::time::Duration::ZERO, async {
/// loop { tokio::task::yield_now().await; }
/// }).await;
/// }
/// ```
pub total_park_count: u64,
/// The maximum number of times any worker thread parked.
///
/// ##### Definition
/// This metric is derived from the maximum of
/// [`tokio::runtime::RuntimeMetrics::worker_park_count`] across all worker threads.
///
/// ##### See also
/// - [`RuntimeMetrics::total_park_count`]
/// - [`RuntimeMetrics::min_park_count`]
pub max_park_count: u64,
/// The minimum number of times any worker thread parked.
///
/// ##### Definition
/// This metric is derived from the maximum of
/// [`tokio::runtime::RuntimeMetrics::worker_park_count`] across all worker threads.
///
/// ##### See also
/// - [`RuntimeMetrics::total_park_count`]
/// - [`RuntimeMetrics::max_park_count`]
pub min_park_count: u64,
/// The average duration of a single invocation of poll on a task.
///
/// This average is an exponentially-weighted moving average of the duration
/// of task polls on all runtime workers.
///
/// ##### Examples
/// ```
/// #[tokio::main(flavor = "multi_thread", worker_threads = 2)]
/// async fn main() {
/// let handle = tokio::runtime::Handle::current();
/// let monitor = tokio_metrics::RuntimeMonitor::new(&handle);
/// let mut intervals = monitor.intervals();
/// let mut next_interval = || intervals.next().unwrap();
///
/// let interval = next_interval();
/// println!("mean task poll duration is {:?}", interval.mean_poll_duration);
/// }
/// ```
pub mean_poll_duration: Duration,
/// The average duration of a single invocation of poll on a task on the
/// worker with the lowest value.
///
/// This average is an exponentially-weighted moving average of the duration
/// of task polls on the runtime worker with the lowest value.
///
/// ##### Examples
/// ```
/// #[tokio::main(flavor = "multi_thread", worker_threads = 2)]
/// async fn main() {
/// let handle = tokio::runtime::Handle::current();
/// let monitor = tokio_metrics::RuntimeMonitor::new(&handle);
/// let mut intervals = monitor.intervals();
/// let mut next_interval = || intervals.next().unwrap();
///
/// let interval = next_interval();
/// println!("min mean task poll duration is {:?}", interval.mean_poll_duration_worker_min);
/// }
/// ```
pub mean_poll_duration_worker_min: Duration,
/// The average duration of a single invocation of poll on a task on the
/// worker with the highest value.
///
/// This average is an exponentially-weighted moving average of the duration
/// of task polls on the runtime worker with the highest value.
///
/// ##### Examples
/// ```
/// #[tokio::main(flavor = "multi_thread", worker_threads = 2)]
/// async fn main() {
/// let handle = tokio::runtime::Handle::current();
/// let monitor = tokio_metrics::RuntimeMonitor::new(&handle);
/// let mut intervals = monitor.intervals();
/// let mut next_interval = || intervals.next().unwrap();
///
/// let interval = next_interval();
/// println!("max mean task poll duration is {:?}", interval.mean_poll_duration_worker_max);
/// }
/// ```
pub mean_poll_duration_worker_max: Duration,
/// A histogram of task polls since the previous probe grouped by poll
/// times.
///
/// This metric must be explicitly enabled when creating the runtime with
/// [`enable_metrics_poll_count_histogram`][tokio::runtime::Builder::enable_metrics_poll_count_histogram].
/// Bucket sizes are fixed and configured at the runtime level. See
/// configuration options on
/// [`runtime::Builder`][tokio::runtime::Builder::enable_metrics_poll_count_histogram].
///
/// ##### Examples
/// ```
/// use tokio::runtime::HistogramScale;
/// use std::time::Duration;
///
/// fn main() {
/// let rt = tokio::runtime::Builder::new_multi_thread()
/// .enable_metrics_poll_count_histogram()
/// .metrics_poll_count_histogram_scale(HistogramScale::Linear)
/// .metrics_poll_count_histogram_resolution(Duration::from_micros(50))
/// .metrics_poll_count_histogram_buckets(12)
/// .build()
/// .unwrap();
///
/// rt.block_on(async {
/// let handle = tokio::runtime::Handle::current();
/// let monitor = tokio_metrics::RuntimeMonitor::new(&handle);
/// let mut intervals = monitor.intervals();
/// let mut next_interval = || intervals.next().unwrap();
///
/// let interval = next_interval();
/// println!("poll count histogram {:?}", interval.poll_count_histogram);
/// });
/// }
/// ```
pub poll_count_histogram: Vec<u64>,
/// The number of times worker threads unparked but performed no work before parking again.
///
/// The worker no-op count increases by one each time the worker unparks the thread but finds
/// no new work and goes back to sleep. This indicates a false-positive wake up.
///
/// ##### Definition
/// This metric is derived from the sum of [`tokio::runtime::RuntimeMetrics::worker_noop_count`]
/// across all worker threads.
///
/// ##### Examples
/// Unfortunately, there isn't a great way to reliably induce no-op parks, as they occur as
/// false-positive events under concurrency.
///
/// The below example triggers fewer than two parks in the single-threaded runtime:
/// ```
/// #[tokio::main(flavor = "current_thread")]
/// async fn main() {
/// let handle = tokio::runtime::Handle::current();
/// let monitor = tokio_metrics::RuntimeMonitor::new(&handle);
/// let mut intervals = monitor.intervals();
/// let mut next_interval = || intervals.next().unwrap();
///
/// assert_eq!(next_interval().total_park_count, 0);
///
/// async {
/// tokio::time::sleep(std::time::Duration::from_millis(1)).await;
/// }.await;
///
/// assert!(next_interval().total_park_count > 0);
/// }
/// ```
///
/// The below example triggers fewer than two parks in the multi-threaded runtime:
/// ```
/// #[tokio::main(flavor = "multi_thread")]
/// async fn main() {
/// let handle = tokio::runtime::Handle::current();
/// let monitor = tokio_metrics::RuntimeMonitor::new(&handle);
/// let mut intervals = monitor.intervals();
/// let mut next_interval = || intervals.next().unwrap();
///
/// assert_eq!(next_interval().total_noop_count, 0);
///
/// async {
/// tokio::time::sleep(std::time::Duration::from_millis(1)).await;
/// }.await;
///
/// assert!(next_interval().total_noop_count > 0);
/// }
/// ```
pub total_noop_count: u64,
/// The maximum number of times any worker thread unparked but performed no work before parking
/// again.
///
/// ##### Definition
/// This metric is derived from the maximum of
/// [`tokio::runtime::RuntimeMetrics::worker_noop_count`] across all worker threads.
///
/// ##### See also
/// - [`RuntimeMetrics::total_noop_count`]
/// - [`RuntimeMetrics::min_noop_count`]
pub max_noop_count: u64,
/// The minimum number of times any worker thread unparked but performed no work before parking
/// again.
///
/// ##### Definition
/// This metric is derived from the minimum of
/// [`tokio::runtime::RuntimeMetrics::worker_noop_count`] across all worker threads.
///
/// ##### See also
/// - [`RuntimeMetrics::total_noop_count`]
/// - [`RuntimeMetrics::max_noop_count`]
pub min_noop_count: u64,
/// The number of tasks worker threads stole from another worker thread.
///
/// The worker steal count increases by the amount of stolen tasks each time the worker
/// has processed its scheduled queue and successfully steals more pending tasks from another
/// worker.
///
/// This metric only applies to the **multi-threaded** runtime and will always return `0` when
/// using the current thread runtime.
///
/// ##### Definition
/// This metric is derived from the sum of [`tokio::runtime::RuntimeMetrics::worker_steal_count`] for
/// all worker threads.
///
/// ##### See also
/// - [`RuntimeMetrics::min_steal_count`]
/// - [`RuntimeMetrics::max_steal_count`]
///
/// ##### Examples
/// In the below example, a blocking channel is used to backup one worker thread:
/// ```
/// #[tokio::main(flavor = "multi_thread", worker_threads = 2)]
/// async fn main() {
/// let handle = tokio::runtime::Handle::current();
/// let monitor = tokio_metrics::RuntimeMonitor::new(&handle);
/// let mut intervals = monitor.intervals();
/// let mut next_interval = || intervals.next().unwrap();
///
/// let interval = next_interval(); // end of first sampling interval
/// assert_eq!(interval.total_steal_count, 0);
/// assert_eq!(interval.min_steal_count, 0);
/// assert_eq!(interval.max_steal_count, 0);
///
/// // induce a steal
/// async {
/// let (tx, rx) = std::sync::mpsc::channel();
/// // Move to the runtime.
/// tokio::spawn(async move {
/// // Spawn the task that sends to the channel
/// tokio::spawn(async move {
/// tx.send(()).unwrap();
/// });
/// // Spawn a task that bumps the previous task out of the "next
/// // scheduled" slot.
/// tokio::spawn(async {});
/// // Blocking receive on the channel.
/// rx.recv().unwrap();
/// flush_metrics().await;
/// }).await.unwrap();
/// flush_metrics().await;
/// }.await;
///
/// let interval = { flush_metrics().await; next_interval() }; // end of interval 2
/// println!("total={}; min={}; max={}", interval.total_steal_count, interval.min_steal_count, interval.max_steal_count);
///
/// let interval = { flush_metrics().await; next_interval() }; // end of interval 3
/// println!("total={}; min={}; max={}", interval.total_steal_count, interval.min_steal_count, interval.max_steal_count);
/// }
///
/// async fn flush_metrics() {
/// let _ = tokio::time::sleep(std::time::Duration::ZERO).await;
/// }
/// ```
pub total_steal_count: u64,
/// The maximum number of tasks any worker thread stole from another worker thread.
///
/// ##### Definition
/// This metric is derived from the maximum of [`tokio::runtime::RuntimeMetrics::worker_steal_count`]
/// across all worker threads.
///
/// ##### See also
/// - [`RuntimeMetrics::total_steal_count`]
/// - [`RuntimeMetrics::min_steal_count`]
pub max_steal_count: u64,
/// The minimum number of tasks any worker thread stole from another worker thread.
///
/// ##### Definition
/// This metric is derived from the minimum of [`tokio::runtime::RuntimeMetrics::worker_steal_count`]
/// across all worker threads.
///
/// ##### See also
/// - [`RuntimeMetrics::total_steal_count`]
/// - [`RuntimeMetrics::max_steal_count`]
pub min_steal_count: u64,
/// The number of times worker threads stole tasks from another worker thread.
///
/// The worker steal operations increases by one each time the worker has processed its
/// scheduled queue and successfully steals more pending tasks from another worker.
///
/// This metric only applies to the **multi-threaded** runtime and will always return `0` when
/// using the current thread runtime.
///
/// ##### Definition
/// This metric is derived from the sum of [`tokio::runtime::RuntimeMetrics::worker_steal_operations`]
/// for all worker threads.
///
/// ##### See also
/// - [`RuntimeMetrics::min_steal_operations`]
/// - [`RuntimeMetrics::max_steal_operations`]
///
/// ##### Examples
/// In the below example, a blocking channel is used to backup one worker thread:
/// ```
/// #[tokio::main(flavor = "multi_thread", worker_threads = 2)]
/// async fn main() {
/// let handle = tokio::runtime::Handle::current();
/// let monitor = tokio_metrics::RuntimeMonitor::new(&handle);
/// let mut intervals = monitor.intervals();
/// let mut next_interval = || intervals.next().unwrap();
///
/// let interval = next_interval(); // end of first sampling interval
/// assert_eq!(interval.total_steal_operations, 0);
/// assert_eq!(interval.min_steal_operations, 0);
/// assert_eq!(interval.max_steal_operations, 0);
///
/// // induce a steal
/// async {
/// let (tx, rx) = std::sync::mpsc::channel();
/// // Move to the runtime.
/// tokio::spawn(async move {
/// // Spawn the task that sends to the channel
/// tokio::spawn(async move {
/// tx.send(()).unwrap();
/// });
/// // Spawn a task that bumps the previous task out of the "next
/// // scheduled" slot.
/// tokio::spawn(async {});
/// // Blocking receive on the channe.
/// rx.recv().unwrap();
/// flush_metrics().await;
/// }).await.unwrap();
/// flush_metrics().await;
/// }.await;
///
/// let interval = { flush_metrics().await; next_interval() }; // end of interval 2
/// println!("total={}; min={}; max={}", interval.total_steal_operations, interval.min_steal_operations, interval.max_steal_operations);
///
/// let interval = { flush_metrics().await; next_interval() }; // end of interval 3
/// println!("total={}; min={}; max={}", interval.total_steal_operations, interval.min_steal_operations, interval.max_steal_operations);
/// }
///
/// async fn flush_metrics() {
/// let _ = tokio::time::sleep(std::time::Duration::ZERO).await;
/// }
/// ```
pub total_steal_operations: u64,
/// The maximum number of times any worker thread stole tasks from another worker thread.
///
/// ##### Definition
/// This metric is derived from the maximum of [`tokio::runtime::RuntimeMetrics::worker_steal_operations`]
/// across all worker threads.
///
/// ##### See also
/// - [`RuntimeMetrics::total_steal_operations`]
/// - [`RuntimeMetrics::min_steal_operations`]
pub max_steal_operations: u64,
/// The minimum number of times any worker thread stole tasks from another worker thread.
///
/// ##### Definition
/// This metric is derived from the minimum of [`tokio::runtime::RuntimeMetrics::worker_steal_operations`]
/// across all worker threads.
///
/// ##### See also
/// - [`RuntimeMetrics::total_steal_operations`]
/// - [`RuntimeMetrics::max_steal_operations`]
pub min_steal_operations: u64,
/// The number of tasks scheduled from **outside** of the runtime.
///
/// The remote schedule count increases by one each time a task is woken from **outside** of
/// the runtime. This usually means that a task is spawned or notified from a non-runtime
/// thread and must be queued using the Runtime's injection queue, which tends to be slower.
///
/// ##### Definition
/// This metric is derived from [`tokio::runtime::RuntimeMetrics::remote_schedule_count`].
///
/// ##### Examples
/// In the below example, a remote schedule is induced by spawning a system thread, then
/// spawning a tokio task from that system thread:
/// ```
/// #[tokio::main(flavor = "multi_thread", worker_threads = 2)]
/// async fn main() {
/// let handle = tokio::runtime::Handle::current();
/// let monitor = tokio_metrics::RuntimeMonitor::new(&handle);
/// let mut intervals = monitor.intervals();
/// let mut next_interval = || intervals.next().unwrap();
///
/// let interval = next_interval(); // end of first sampling interval
/// assert_eq!(interval.num_remote_schedules, 0);
///
/// // spawn a non-runtime thread
/// std::thread::spawn(move || {
/// // spawn two tasks from this non-runtime thread
/// async move {
/// handle.spawn(async {}).await;
/// handle.spawn(async {}).await;
/// }
/// }).join().unwrap().await;
///
/// let interval = next_interval(); // end of second sampling interval
/// assert_eq!(interval.num_remote_schedules, 2);
///
/// let interval = next_interval(); // end of third sampling interval
/// assert_eq!(interval.num_remote_schedules, 0);
/// }
/// ```
pub num_remote_schedules: u64,
/// The number of tasks scheduled from worker threads.
///
/// The local schedule count increases by one each time a task is woken from **inside** of the
/// runtime. This usually means that a task is spawned or notified from within a runtime thread
/// and will be queued on the worker-local queue.
///
/// ##### Definition
/// This metric is derived from the sum of
/// [`tokio::runtime::RuntimeMetrics::worker_local_schedule_count`] across all worker threads.
///
/// ##### See also
/// - [`RuntimeMetrics::min_local_schedule_count`]
/// - [`RuntimeMetrics::max_local_schedule_count`]
///
/// ##### Examples
/// ###### With `current_thread` runtime
/// In the below example, two tasks are spawned from the context of a third tokio task:
/// ```
/// #[tokio::main(flavor = "current_thread")]
/// async fn main() {
/// let handle = tokio::runtime::Handle::current();
/// let monitor = tokio_metrics::RuntimeMonitor::new(&handle);
/// let mut intervals = monitor.intervals();
/// let mut next_interval = || intervals.next().unwrap();
///
/// let interval = { flush_metrics().await; next_interval() }; // end interval 2
/// assert_eq!(interval.total_local_schedule_count, 0);
///
/// let task = async {
/// tokio::spawn(async {}); // local schedule 1
/// tokio::spawn(async {}); // local schedule 2
/// };
///
/// let handle = tokio::spawn(task); // local schedule 3
///
/// let interval = { flush_metrics().await; next_interval() }; // end interval 2
/// assert_eq!(interval.total_local_schedule_count, 3);
///
/// let _ = handle.await;
///
/// let interval = { flush_metrics().await; next_interval() }; // end interval 3
/// assert_eq!(interval.total_local_schedule_count, 0);
/// }
///
/// async fn flush_metrics() {
/// tokio::task::yield_now().await;
/// }
/// ```
///
/// ###### With `multi_thread` runtime
/// In the below example, 100 tasks are spawned:
/// ```
/// #[tokio::main(flavor = "multi_thread", worker_threads = 2)]
/// async fn main() {
/// let handle = tokio::runtime::Handle::current();
/// let monitor = tokio_metrics::RuntimeMonitor::new(&handle);
/// let mut intervals = monitor.intervals();
/// let mut next_interval = || intervals.next().unwrap();
///
/// let interval = next_interval(); // end of interval 1
/// assert_eq!(interval.total_local_schedule_count, 0);
///
/// use std::sync::atomic::{AtomicBool, Ordering};
/// static SPINLOCK: AtomicBool = AtomicBool::new(true);
///
/// // block the other worker thread
/// tokio::spawn(async {
/// while SPINLOCK.load(Ordering::SeqCst) {}
/// });
///
/// // FIXME: why does this need to be in a `spawn`?
/// let _ = tokio::spawn(async {
/// // spawn 100 tasks
/// for _ in 0..100 {
/// tokio::spawn(async {});
/// }
/// // this spawns 1 more task
/// flush_metrics().await;
/// }).await;
///
/// // unblock the other worker thread
/// SPINLOCK.store(false, Ordering::SeqCst);
///
/// let interval = { flush_metrics().await; next_interval() }; // end of interval 2
/// assert_eq!(interval.total_local_schedule_count, 100 + 1);
/// }
///
/// async fn flush_metrics() {
/// let _ = tokio::time::sleep(std::time::Duration::ZERO).await;
/// }
/// ```
pub total_local_schedule_count: u64,
/// The maximum number of tasks scheduled from any one worker thread.
///
/// ##### Definition
/// This metric is derived from the maximum of
/// [`tokio::runtime::RuntimeMetrics::worker_local_schedule_count`] for all worker threads.
///
/// ##### See also
/// - [`RuntimeMetrics::total_local_schedule_count`]
/// - [`RuntimeMetrics::min_local_schedule_count`]
pub max_local_schedule_count: u64,
/// The minimum number of tasks scheduled from any one worker thread.
///
/// ##### Definition
/// This metric is derived from the minimum of
/// [`tokio::runtime::RuntimeMetrics::worker_local_schedule_count`] for all worker threads.
///
/// ##### See also
/// - [`RuntimeMetrics::total_local_schedule_count`]
/// - [`RuntimeMetrics::max_local_schedule_count`]
pub min_local_schedule_count: u64,
/// The number of times worker threads saturated their local queues.
///
/// The worker steal count increases by one each time the worker attempts to schedule a task
/// locally, but its local queue is full. When this happens, half of the
/// local queue is moved to the injection queue.
///
/// This metric only applies to the **multi-threaded** scheduler.
///
/// ##### Definition
/// This metric is derived from the sum of
/// [`tokio::runtime::RuntimeMetrics::worker_overflow_count`] across all worker threads.
///
/// ##### See also
/// - [`RuntimeMetrics::min_overflow_count`]
/// - [`RuntimeMetrics::max_overflow_count`]
///
/// ##### Examples
/// ```
/// #[tokio::main(flavor = "multi_thread", worker_threads = 1)]
/// async fn main() {
/// let handle = tokio::runtime::Handle::current();
/// let monitor = tokio_metrics::RuntimeMonitor::new(&handle);
/// let mut intervals = monitor.intervals();
/// let mut next_interval = || intervals.next().unwrap();
///
/// let interval = next_interval(); // end of interval 1
/// assert_eq!(interval.total_overflow_count, 0);
///
/// use std::sync::atomic::{AtomicBool, Ordering};
///
/// // spawn a ton of tasks
/// let _ = tokio::spawn(async {
/// // we do this in a `tokio::spawn` because it is impossible to
/// // overflow the main task
/// for _ in 0..300 {
/// tokio::spawn(async {});
/// }
/// }).await;
///
/// let interval = { flush_metrics().await; next_interval() }; // end of interval 2
/// assert_eq!(interval.total_overflow_count, 1);
/// }
///
/// async fn flush_metrics() {
/// let _ = tokio::time::sleep(std::time::Duration::from_millis(1)).await;
/// }
/// ```
pub total_overflow_count: u64,
/// The maximum number of times any one worker saturated its local queue.
///
/// ##### Definition
/// This metric is derived from the maximum of
/// [`tokio::runtime::RuntimeMetrics::worker_overflow_count`] across all worker threads.
///
/// ##### See also
/// - [`RuntimeMetrics::total_overflow_count`]
/// - [`RuntimeMetrics::min_overflow_count`]
pub max_overflow_count: u64,
/// The minimum number of times any one worker saturated its local queue.
///
/// ##### Definition
/// This metric is derived from the maximum of
/// [`tokio::runtime::RuntimeMetrics::worker_overflow_count`] across all worker threads.
///
/// ##### See also
/// - [`RuntimeMetrics::total_overflow_count`]
/// - [`RuntimeMetrics::max_overflow_count`]
pub min_overflow_count: u64,
/// The number of tasks that have been polled across all worker threads.
///
/// The worker poll count increases by one each time a worker polls a scheduled task.
///
/// ##### Definition
/// This metric is derived from the sum of
/// [`tokio::runtime::RuntimeMetrics::worker_poll_count`] across all worker threads.
///
/// ##### See also
/// - [`RuntimeMetrics::min_polls_count`]
/// - [`RuntimeMetrics::max_polls_count`]
///
/// ##### Examples
/// In the below example, 42 tasks are spawned and polled:
/// ```
/// #[tokio::main(flavor = "current_thread")]
/// async fn main() {
/// let handle = tokio::runtime::Handle::current();
/// let monitor = tokio_metrics::RuntimeMonitor::new(&handle);
/// let mut intervals = monitor.intervals();
/// let mut next_interval = || intervals.next().unwrap();
///
/// let interval = { flush_metrics().await; next_interval() }; // end of interval 1
/// assert_eq!(interval.total_polls_count, 0);
/// assert_eq!(interval.min_polls_count, 0);
/// assert_eq!(interval.max_polls_count, 0);
///
/// const N: u64 = 42;
///
/// for _ in 0..N {
/// let _ = tokio::spawn(async {}).await;
/// }
///
/// let interval = { flush_metrics().await; next_interval() }; // end of interval 2
/// assert_eq!(interval.total_polls_count, N);
/// assert_eq!(interval.min_polls_count, N);
/// assert_eq!(interval.max_polls_count, N);
/// }
///
/// async fn flush_metrics() {
/// let _ = tokio::task::yield_now().await;
/// }
/// ```
pub total_polls_count: u64,
/// The maximum number of tasks that have been polled in any worker thread.
///
/// ##### Definition
/// This metric is derived from the maximum of
/// [`tokio::runtime::RuntimeMetrics::worker_poll_count`] across all worker threads.
///
/// ##### See also
/// - [`RuntimeMetrics::total_polls_count`]
/// - [`RuntimeMetrics::min_polls_count`]
pub max_polls_count: u64,
/// The minimum number of tasks that have been polled in any worker thread.
///
/// ##### Definition
/// This metric is derived from the minimum of
/// [`tokio::runtime::RuntimeMetrics::worker_poll_count`] across all worker threads.
///
/// ##### See also
/// - [`RuntimeMetrics::total_polls_count`]
/// - [`RuntimeMetrics::max_polls_count`]
pub min_polls_count: u64,
/// The amount of time worker threads were busy.
///
/// The worker busy duration increases whenever the worker is spending time processing work.
/// Using this value can indicate the total load of workers.
///
/// ##### Definition
/// This metric is derived from the sum of
/// [`tokio::runtime::RuntimeMetrics::worker_total_busy_duration`] across all worker threads.
///
/// ##### See also
/// - [`RuntimeMetrics::min_busy_duration`]
/// - [`RuntimeMetrics::max_busy_duration`]
///
/// ##### Examples
/// In the below example, tasks spend a total of 3s busy:
/// ```
/// use tokio::time::Duration;
///
/// fn main() {
/// let start = tokio::time::Instant::now();
///
/// let rt = tokio::runtime::Builder::new_current_thread()
/// .enable_all()
/// .build()
/// .unwrap();
///
/// let handle = rt.handle();
/// let monitor = tokio_metrics::RuntimeMonitor::new(&handle);
/// let mut intervals = monitor.intervals();
/// let mut next_interval = || intervals.next().unwrap();
///
/// let delay_1s = Duration::from_secs(1);
/// let delay_3s = Duration::from_secs(3);
///
/// rt.block_on(async {
/// // keep the main task busy for 1s
/// spin_for(delay_1s);
///
/// // spawn a task and keep it busy for 2s
/// let _ = tokio::spawn(async move {
/// spin_for(delay_3s);
/// }).await;
/// });
///
/// // flush metrics
/// drop(rt);
///
/// let elapsed = start.elapsed();
///
/// let interval = next_interval(); // end of interval 2
/// assert!(interval.total_busy_duration >= delay_1s + delay_3s);
/// assert!(interval.total_busy_duration <= elapsed);
/// }
///
/// fn time<F>(task: F) -> Duration
/// where
/// F: Fn() -> ()
/// {
/// let start = tokio::time::Instant::now();
/// task();
/// start.elapsed()
/// }
///
/// /// Block the current thread for a given `duration`.
/// fn spin_for(duration: Duration) {
/// let start = tokio::time::Instant::now();
/// while start.elapsed() <= duration {}
/// }
/// ```
///
/// Busy times may not accumulate as the above example suggests (FIXME: Why?); e.g., if we
/// remove the three second delay, the time spent busy falls to mere microseconds:
/// ```should_panic
/// use tokio::time::Duration;
///
/// fn main() {
/// let rt = tokio::runtime::Builder::new_current_thread()
/// .enable_all()
/// .build()
/// .unwrap();
///
/// let handle = rt.handle();
/// let monitor = tokio_metrics::RuntimeMonitor::new(&handle);
/// let mut intervals = monitor.intervals();
/// let mut next_interval = || intervals.next().unwrap();
///
/// let delay_1s = Duration::from_secs(1);
///
/// let elapsed = time(|| rt.block_on(async {
/// // keep the main task busy for 1s
/// spin_for(delay_1s);
/// }));
///
/// // flush metrics
/// drop(rt);
///
/// let interval = next_interval(); // end of interval 2
/// assert!(interval.total_busy_duration >= delay_1s); // FAIL
/// assert!(interval.total_busy_duration <= elapsed);
/// }
///
/// fn time<F>(task: F) -> Duration
/// where
/// F: Fn() -> ()
/// {
/// let start = tokio::time::Instant::now();
/// task();
/// start.elapsed()
/// }
///
/// /// Block the current thread for a given `duration`.
/// fn spin_for(duration: Duration) {
/// let start = tokio::time::Instant::now();
/// while start.elapsed() <= duration {}
/// }
/// ```
pub total_busy_duration: Duration,
/// The maximum amount of time a worker thread was busy.
///
/// ##### Definition
/// This metric is derived from the maximum of
/// [`tokio::runtime::RuntimeMetrics::worker_total_busy_duration`] across all worker threads.
///
/// ##### See also
/// - [`RuntimeMetrics::total_busy_duration`]
/// - [`RuntimeMetrics::min_busy_duration`]
pub max_busy_duration: Duration,
/// The minimum amount of time a worker thread was busy.
///
/// ##### Definition
/// This metric is derived from the minimum of
/// [`tokio::runtime::RuntimeMetrics::worker_total_busy_duration`] across all worker threads.
///
/// ##### See also
/// - [`RuntimeMetrics::total_busy_duration`]
/// - [`RuntimeMetrics::max_busy_duration`]
pub min_busy_duration: Duration,
/// The number of tasks currently scheduled in the runtime's injection queue.
///
/// Tasks that are spawned or notified from a non-runtime thread are scheduled using the
/// runtime's injection queue. This metric returns the **current** number of tasks pending in
/// the injection queue. As such, the returned value may increase or decrease as new tasks are
/// scheduled and processed.
///
/// ##### Definition
/// This metric is derived from [`tokio::runtime::RuntimeMetrics::injection_queue_depth`].
///
/// ##### Example
/// ```
/// # let current_thread = tokio::runtime::Builder::new_current_thread()
/// # .enable_all()
/// # .build()
/// # .unwrap();
/// #
/// # let multi_thread = tokio::runtime::Builder::new_multi_thread()
/// # .worker_threads(2)
/// # .enable_all()
/// # .build()
/// # .unwrap();
/// #
/// # for runtime in [current_thread, multi_thread] {
/// let handle = runtime.handle().clone();
/// let monitor = tokio_metrics::RuntimeMonitor::new(&handle);
/// let mut intervals = monitor.intervals();
/// let mut next_interval = || intervals.next().unwrap();
///
/// let interval = next_interval(); // end of interval 1
/// assert_eq!(interval.num_remote_schedules, 0);
///
/// // spawn a system thread outside of the runtime
/// std::thread::spawn(move || {
/// // spawn two tasks from this non-runtime thread
/// handle.spawn(async {});
/// handle.spawn(async {});
/// }).join().unwrap();
///
/// // flush metrics
/// drop(runtime);
///
/// let interval = next_interval(); // end of interval 2
/// assert_eq!(interval.num_remote_schedules, 2);
/// # }
/// ```
pub injection_queue_depth: usize,
/// The total number of tasks currently scheduled in workers' local queues.
///
/// Tasks that are spawned or notified from within a runtime thread are scheduled using that
/// worker's local queue. This metric returns the **current** number of tasks pending in all
/// workers' local queues. As such, the returned value may increase or decrease as new tasks
/// are scheduled and processed.
///
/// ##### Definition
/// This metric is derived from [`tokio::runtime::RuntimeMetrics::worker_local_queue_depth`].
///
/// ##### See also
/// - [`RuntimeMetrics::min_local_queue_depth`]
/// - [`RuntimeMetrics::max_local_queue_depth`]
///
/// ##### Example
///
/// ###### With `current_thread` runtime
/// The below example spawns 100 tasks:
/// ```
/// #[tokio::main(flavor = "current_thread")]
/// async fn main() {
/// const N: usize = 100;
///
/// let handle = tokio::runtime::Handle::current();
/// let monitor = tokio_metrics::RuntimeMonitor::new(&handle);
/// let mut intervals = monitor.intervals();
/// let mut next_interval = || intervals.next().unwrap();
///
/// let interval = next_interval(); // end of interval 1
/// assert_eq!(interval.total_local_queue_depth, 0);
///
///
/// for _ in 0..N {
/// tokio::spawn(async {});
/// }
/// let interval = next_interval(); // end of interval 2
/// assert_eq!(interval.total_local_queue_depth, N);
/// }
/// ```
///
/// ###### With `multi_thread runtime
/// The below example spawns 100 tasks:
/// ```
/// #[tokio::main(flavor = "multi_thread", worker_threads = 2)]
/// async fn main() {
/// const N: usize = 100;
///
/// let handle = tokio::runtime::Handle::current();
/// let monitor = tokio_metrics::RuntimeMonitor::new(&handle);
/// let mut intervals = monitor.intervals();
/// let mut next_interval = || intervals.next().unwrap();
///
/// let interval = next_interval(); // end of interval 1
/// assert_eq!(interval.total_local_queue_depth, 0);
///
/// use std::sync::atomic::{AtomicBool, Ordering};
/// static SPINLOCK_A: AtomicBool = AtomicBool::new(true);
///
/// // block the other worker thread
/// tokio::spawn(async {
/// while SPINLOCK_A.load(Ordering::SeqCst) {}
/// });
///
/// static SPINLOCK_B: AtomicBool = AtomicBool::new(true);
///
/// let _ = tokio::spawn(async {
/// for _ in 0..N {
/// tokio::spawn(async {
/// while SPINLOCK_B.load(Ordering::SeqCst) {}
/// });
/// }
/// }).await;
///
/// // unblock the other worker thread
/// SPINLOCK_A.store(false, Ordering::SeqCst);
///
/// let interval = next_interval(); // end of interval 2
/// assert_eq!(interval.total_local_queue_depth, N - 1);
///
/// SPINLOCK_B.store(false, Ordering::SeqCst);
/// }
/// ```
pub total_local_queue_depth: usize,
/// The maximum number of tasks currently scheduled any worker's local queue.
///
/// ##### Definition
/// This metric is derived from the maximum of
/// [`tokio::runtime::RuntimeMetrics::worker_local_queue_depth`] across all worker threads.
///
/// ##### See also
/// - [`RuntimeMetrics::total_local_queue_depth`]
/// - [`RuntimeMetrics::min_local_queue_depth`]
pub max_local_queue_depth: usize,
/// The minimum number of tasks currently scheduled any worker's local queue.
///
/// ##### Definition
/// This metric is derived from the minimum of
/// [`tokio::runtime::RuntimeMetrics::worker_local_queue_depth`] across all worker threads.
///
/// ##### See also
/// - [`RuntimeMetrics::total_local_queue_depth`]
/// - [`RuntimeMetrics::max_local_queue_depth`]
pub min_local_queue_depth: usize,
/// Total amount of time elapsed since observing runtime metrics.
pub elapsed: Duration,
/// Returns the number of times that tasks have been forced to yield back to the scheduler after exhausting their task budgets.
///
/// This count starts at zero when the runtime is created and increases by one each time a task yields due to exhausting its budget.
///
/// The counter is monotonically increasing. It is never decremented or reset to zero.
///
/// ##### Definition
/// This metric is derived from [`tokio::runtime::RuntimeMetrics::budget_forced_yield_count`].
pub budget_forced_yield_count: u64,
/// Returns the number of ready events processed by the runtime’s I/O driver.
///
/// ##### Definition
/// This metric is derived from [`tokio::runtime::RuntimeMetrics::io_driver_ready_count`].
pub io_driver_ready_count: u64,
}
/// Snapshot of per-worker metrics
#[derive(Debug)]
struct Worker {
worker: usize,
total_park_count: u64,
total_noop_count: u64,
total_steal_count: u64,
total_steal_operations: u64,
total_local_schedule_count: u64,
total_overflow_count: u64,
total_polls_count: u64,
total_busy_duration: Duration,
poll_count_histogram: Vec<u64>,
}
/// Iterator returned by [`RuntimeMonitor::intervals`].
///
/// See that method's documentation for more details.
#[derive(Debug)]
pub struct RuntimeIntervals {
runtime: runtime::RuntimeMetrics,
started_at: Instant,
workers: Vec<Worker>,
// Number of tasks scheduled from *outside* of the runtime
num_remote_schedules: u64,
budget_forced_yield_count: u64,
io_driver_ready_count: u64,
}
impl RuntimeIntervals {
fn probe(&mut self) -> RuntimeMetrics {
let now = Instant::now();
let num_remote_schedules = self.runtime.remote_schedule_count();
let budget_forced_yields = self.runtime.budget_forced_yield_count();
let io_driver_ready_events = self.runtime.io_driver_ready_count();
let mut metrics = RuntimeMetrics {
workers_count: self.runtime.num_workers(),
elapsed: now - self.started_at,
injection_queue_depth: self.runtime.injection_queue_depth(),
num_remote_schedules: num_remote_schedules - self.num_remote_schedules,
min_park_count: u64::MAX,
min_noop_count: u64::MAX,
min_steal_count: u64::MAX,
min_local_schedule_count: u64::MAX,
min_overflow_count: u64::MAX,
min_polls_count: u64::MAX,
min_busy_duration: Duration::from_secs(1000000000),
min_local_queue_depth: usize::MAX,
mean_poll_duration_worker_min: Duration::MAX,
poll_count_histogram: vec![0; self.runtime.poll_count_histogram_num_buckets()],
budget_forced_yield_count: budget_forced_yields - self.budget_forced_yield_count,
io_driver_ready_count: io_driver_ready_events - self.io_driver_ready_count,
..Default::default()
};
self.num_remote_schedules = num_remote_schedules;
self.started_at = now;
self.budget_forced_yield_count = budget_forced_yields;
self.io_driver_ready_count = io_driver_ready_events;
for worker in &mut self.workers {
worker.probe(&self.runtime, &mut metrics);
}
if metrics.total_polls_count == 0 {
debug_assert_eq!(metrics.mean_poll_duration, Duration::default());
metrics.mean_poll_duration_worker_max = Duration::default();
metrics.mean_poll_duration_worker_min = Duration::default();
}
metrics
}
}
impl Iterator for RuntimeIntervals {
type Item = RuntimeMetrics;
fn next(&mut self) -> Option<RuntimeMetrics> {
Some(self.probe())
}
}
impl RuntimeMonitor {
pub fn new(runtime: &runtime::Handle) -> RuntimeMonitor {
let runtime = runtime.metrics();
RuntimeMonitor { runtime }
}
/// Produces an unending iterator of [`RuntimeMetrics`].
///
/// Each sampling interval is defined by the time elapsed between advancements of the iterator
/// produced by [`RuntimeMonitor::intervals`]. The item type of this iterator is [`RuntimeMetrics`],
/// which is a bundle of runtime metrics that describe *only* changes occurring within that sampling
/// interval.
///
/// # Example
///
/// ```
/// use std::time::Duration;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
/// let handle = tokio::runtime::Handle::current();
/// // construct the runtime metrics monitor
/// let runtime_monitor = tokio_metrics::RuntimeMonitor::new(&handle);
///
/// // print runtime metrics every 500ms
/// {
/// tokio::spawn(async move {
/// for interval in runtime_monitor.intervals() {
/// // pretty-print the metric interval
/// println!("{:?}", interval);
/// // wait 500ms
/// tokio::time::sleep(Duration::from_millis(500)).await;
/// }
/// });
/// }
///
/// // await some tasks
/// tokio::join![
/// do_work(),
/// do_work(),
/// do_work(),
/// ];
///
/// Ok(())
/// }
///
/// async fn do_work() {
/// for _ in 0..25 {
/// tokio::task::yield_now().await;
/// tokio::time::sleep(Duration::from_millis(100)).await;
/// }
/// }
/// ```
pub fn intervals(&self) -> RuntimeIntervals {
let started_at = Instant::now();
let workers = (0..self.runtime.num_workers())
.map(|worker| Worker::new(worker, &self.runtime))
.collect();
RuntimeIntervals {
runtime: self.runtime.clone(),
started_at,
workers,
num_remote_schedules: self.runtime.remote_schedule_count(),
budget_forced_yield_count: self.runtime.budget_forced_yield_count(),
io_driver_ready_count: self.runtime.io_driver_ready_count(),
}
}
}
impl Worker {
fn new(worker: usize, rt: &runtime::RuntimeMetrics) -> Worker {
let poll_count_histogram = if rt.poll_count_histogram_enabled() {
vec![0; rt.poll_count_histogram_num_buckets()]
} else {
vec![]
};
Worker {
worker,
total_park_count: rt.worker_park_count(worker),
total_noop_count: rt.worker_noop_count(worker),
total_steal_count: rt.worker_steal_count(worker),
total_steal_operations: rt.worker_steal_operations(worker),
total_local_schedule_count: rt.worker_local_schedule_count(worker),
total_overflow_count: rt.worker_overflow_count(worker),
total_polls_count: rt.worker_poll_count(worker),
total_busy_duration: rt.worker_total_busy_duration(worker),
poll_count_histogram,
}
}
fn probe(&mut self, rt: &runtime::RuntimeMetrics, metrics: &mut RuntimeMetrics) {
macro_rules! metric {
( $sum:ident, $max:ident, $min:ident, $probe:ident ) => {{
let val = rt.$probe(self.worker);
let delta = val - self.$sum;
self.$sum = val;
metrics.$sum += delta;
if delta > metrics.$max {
metrics.$max = delta;
}
if delta < metrics.$min {
metrics.$min = delta;
}
}};
}
let mut worker_polls_count = self.total_polls_count;
let total_polls_count = metrics.total_polls_count;
metric!(
total_park_count,
max_park_count,
min_park_count,
worker_park_count
);
metric!(
total_noop_count,
max_noop_count,
min_noop_count,
worker_noop_count
);
metric!(
total_steal_count,
max_steal_count,
min_steal_count,
worker_steal_count
);
metric!(
total_steal_operations,
max_steal_operations,
min_steal_operations,
worker_steal_operations
);
metric!(
total_local_schedule_count,
max_local_schedule_count,
min_local_schedule_count,
worker_local_schedule_count
);
metric!(
total_overflow_count,
max_overflow_count,
min_overflow_count,
worker_overflow_count
);
metric!(
total_polls_count,
max_polls_count,
min_polls_count,
worker_poll_count
);
metric!(
total_busy_duration,
max_busy_duration,
min_busy_duration,
worker_total_busy_duration
);
// Get the number of polls since last probe
worker_polls_count = self.total_polls_count - worker_polls_count;
// Update the mean task poll duration if there were polls
if worker_polls_count > 0 {
let val = rt.worker_mean_poll_time(self.worker);
if val > metrics.mean_poll_duration_worker_max {
metrics.mean_poll_duration_worker_max = val;
}
if val < metrics.mean_poll_duration_worker_min {
metrics.mean_poll_duration_worker_min = val;
}
// First, scale the current value down
let ratio = total_polls_count as f64 / metrics.total_polls_count as f64;
let mut mean = metrics.mean_poll_duration.as_nanos() as f64 * ratio;
// Add the scaled current worker's mean poll duration
let ratio = worker_polls_count as f64 / metrics.total_polls_count as f64;
mean += val.as_nanos() as f64 * ratio;
metrics.mean_poll_duration = Duration::from_nanos(mean as u64);
}
// Update the histogram counts if there were polls since last count
if worker_polls_count > 0 {
for (bucket, cell) in metrics.poll_count_histogram.iter_mut().enumerate() {
let new = rt.poll_count_histogram_bucket_count(self.worker, bucket);
let delta = new - self.poll_count_histogram[bucket];
self.poll_count_histogram[bucket] = new;
*cell += delta;
}
}
// Local scheduled tasks is an absolute value
let local_scheduled_tasks = rt.worker_local_queue_depth(self.worker);
metrics.total_local_queue_depth += local_scheduled_tasks;
if local_scheduled_tasks > metrics.max_local_queue_depth {
metrics.max_local_queue_depth = local_scheduled_tasks;
}
if local_scheduled_tasks < metrics.min_local_queue_depth {
metrics.min_local_queue_depth = local_scheduled_tasks;
}
}
}
impl RuntimeMetrics {
pub fn mean_polls_per_park(&self) -> f64 {
let total_park_count = self.total_park_count - self.total_noop_count;
if total_park_count == 0 {
0.0
} else {
self.total_polls_count as f64 / total_park_count as f64
}
}
pub fn busy_ratio(&self) -> f64 {
self.total_busy_duration.as_nanos() as f64 / self.elapsed.as_nanos() as f64
}
}