Loading...
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 | /* * Copyright (c) 2006-2021 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ * */ #include <kern/task.h> #include <libkern/libkern.h> #include <machine/atomic.h> #include <mach/coalition.h> #include <os/log.h> #include <sys/coalition.h> #include <sys/proc.h> #include <sys/proc_internal.h> #include <sys/sysctl.h> #include <sys/kdebug.h> #include <sys/kern_memorystatus.h> #include <vm/vm_protos.h> #include <vm/vm_compressor_xnu.h> #include <kern/kern_memorystatus_internal.h> /* * All memory pressure policy decisions should live here, and there should be * as little mechanism as possible. This file prioritizes readability. */ #pragma mark Policy Function Declarations #if CONFIG_JETSAM static bool memorystatus_check_aggressive_jetsam_needed(int *jld_idle_kills); #endif /* CONFIG_JETSAM */ #pragma mark Memorystatus Health Check /* * Each subsystem that relies on the memorystatus thread * for resource exhaustion should put a health check in this section. * The memorystatus thread runs all of the health checks * to determine if the system is healthy. If the system is unhealthy * it picks an action based on the system health status. See the * Memorystatus Thread Actions section below. */ extern uint64_t memstat_oldest_reapable_proc_prio_start; extern uint64_t memstat_reaper_min_age_secs; extern uint64_t memstat_oldest_reapable_proc_will_be_reapable_at_ts_matu; extern bool memstat_reaper_is_currently_sweeping; extern vm_pressure_level_t memorystatus_vm_pressure_level; static void memstat_evaluate_health_conditions(memorystatus_system_health_t status) { memset(status, 0, sizeof(memorystatus_system_health_t)); status->msh_compressor_low_on_space = vm_compressor_low_on_space() || os_atomic_load(&memorystatus_compressor_space_shortage, relaxed); status->msh_compressor_exhausted = vm_compressor_out_of_space(); status->msh_swap_low_on_space = vm_swap_low_on_space(); status->msh_swap_exhausted = vm_swap_out_of_space(); #if CONFIG_JETSAM memstat_evaluate_page_shortage( &status->msh_available_pages_below_soft, &status->msh_available_pages_below_idle, &status->msh_available_pages_below_critical, &status->msh_available_pages_below_reaper); status->msh_compressor_is_thrashing = !memorystatus_swap_all_apps && vm_compressor_is_thrashing(); #if CONFIG_PHANTOM_CACHE status->msh_phantom_cache_pressure = os_atomic_load(&memorystatus_phantom_cache_pressure, relaxed); #else status->msh_phantom_cache_pressure = false; #endif /* CONFIG_PHANTOM_CACHE */ if (!memorystatus_swap_all_apps && status->msh_phantom_cache_pressure && !(status->msh_compressor_is_thrashing && status->msh_compressor_exhausted)) { status->msh_filecache_is_thrashing = true; } status->msh_pageout_starved = os_atomic_load(&memorystatus_pageout_starved, relaxed); status->msh_swappable_compressor_segments_over_limit = memorystatus_swap_over_trigger(100); status->msh_swapin_queue_over_limit = memorystatus_swapin_over_trigger(); #else /* !CONFIG_JETSAM */ vm_pressure_level_t pressure_level = memorystatus_vm_pressure_level; status->msh_vm_pressure_critical = (pressure_level == kVMPressureCritical); status->msh_vm_pressure_warning = (pressure_level >= kVMPressureWarning); #endif /* CONFIG_JETSAM */ status->msh_zone_map_is_exhausted = os_atomic_load(&memorystatus_zone_map_is_exhausted, relaxed); } static bool memstat_is_system_healthy(const memorystatus_system_health_t status) { #if CONFIG_JETSAM return !(status->msh_available_pages_below_critical || status->msh_compressor_is_thrashing || status->msh_compressor_exhausted || status->msh_compressor_low_on_space || status->msh_filecache_is_thrashing || status->msh_zone_map_is_exhausted || status->msh_pageout_starved); #else /* CONFIG_JETSAM */ return !(status->msh_zone_map_is_exhausted || status->msh_compressor_exhausted || status->msh_compressor_low_on_space || status->msh_swap_exhausted || status->msh_swap_low_on_space || status->msh_vm_pressure_critical || status->msh_vm_pressure_warning); #endif /* CONFIG_JETSAM */ } static void memstat_log_system_health(const memorystatus_system_health_t status) { static struct memorystatus_system_health_s prev_status = {0}; bool healthy = memstat_is_system_healthy(status); /* * Avoid spamming logs by only logging when the system status has changed. */ if (prev_status.msh_zone_map_is_exhausted == status->msh_zone_map_is_exhausted && prev_status.msh_compressor_exhausted == status->msh_compressor_exhausted && prev_status.msh_swap_low_on_space == status->msh_swap_low_on_space && prev_status.msh_swap_exhausted == status->msh_swap_exhausted #if CONFIG_JETSAM && prev_status.msh_available_pages_below_idle == status->msh_available_pages_below_idle && prev_status.msh_available_pages_below_soft == status->msh_available_pages_below_soft && prev_status.msh_available_pages_below_critical == status->msh_available_pages_below_critical && prev_status.msh_available_pages_below_reaper == status->msh_available_pages_below_reaper && prev_status.msh_compressor_needs_to_swap == status->msh_compressor_needs_to_swap && prev_status.msh_compressor_is_thrashing == status->msh_compressor_is_thrashing && prev_status.msh_filecache_is_thrashing == status->msh_filecache_is_thrashing && prev_status.msh_phantom_cache_pressure == status->msh_phantom_cache_pressure && prev_status.msh_swapin_queue_over_limit == status->msh_swapin_queue_over_limit && prev_status.msh_pageout_starved == status->msh_pageout_starved #endif /* CONFIG_JETSAM */ ) { /* No change */ return; } #if CONFIG_JETSAM if (healthy) { if (status->msh_available_pages_below_soft) { memorystatus_log( "memorystatus: System will begin enforcing " "soft memory limits. " "memorystatus_available_pages: %llu compressor_size: %u\n", (uint64_t)MEMORYSTATUS_LOG_AVAILABLE_PAGES, vm_compressor_pool_size()); } else if (status->msh_available_pages_below_idle) { memorystatus_log( "memorystatus: System will begin enacting " "idle-exits. " "memorystatus_available_pages: %llu compressor_size: %u\n", (uint64_t)MEMORYSTATUS_LOG_AVAILABLE_PAGES, vm_compressor_pool_size()); } else if (status->msh_available_pages_below_reaper) { memorystatus_log( "memorystatus: System will begin reaping " "long-idle processes. " "memorystatus_available_pages: %llu compressor_size: %u\n", (uint64_t)MEMORYSTATUS_LOG_AVAILABLE_PAGES, vm_compressor_pool_size()); } else { memorystatus_log( "memorystatus: System is healthy. " "memorystatus_available_pages: %llu compressor_size:%u\n", (uint64_t)MEMORYSTATUS_LOG_AVAILABLE_PAGES, vm_compressor_pool_size()); } } else { /* Unhealthy */ memorystatus_log("memorystatus: System is unhealthy! memorystatus_available_pages: %llu compressor_size:%u\n", (uint64_t)MEMORYSTATUS_LOG_AVAILABLE_PAGES, vm_compressor_pool_size()); memorystatus_log( "memorystatus: {" "\"available_pages_below_critical\": %d, " "\"available_pages_below_idle\": %d, " "\"available_pages_below_soft\": %d, " "\"available_pages_below_reaper\": %d, " "\"compressor_needs_to_swap\": %d, " "\"compressor_exhausted\": %d, " "\"compressor_is_thrashing\": %d, " "\"filecache_is_thrashing\": %d, " "\"zone_map_is_exhausted\": %d, " "\"phantom_cache_pressure\": %d, " "\"swappable_compressor_segments_over_limit\": %d, " "\"swapin_queue_over_limit\": %d, " "\"swap_low\": %d, " "\"swap_exhausted\": %d" "}\n", status->msh_available_pages_below_critical, status->msh_available_pages_below_idle, status->msh_available_pages_below_soft, status->msh_available_pages_below_reaper, status->msh_compressor_needs_to_swap, status->msh_compressor_exhausted, status->msh_compressor_is_thrashing, status->msh_filecache_is_thrashing, status->msh_zone_map_is_exhausted, status->msh_phantom_cache_pressure, status->msh_swappable_compressor_segments_over_limit, status->msh_swapin_queue_over_limit, status->msh_swap_low_on_space, status->msh_swap_exhausted); } #else /* CONFIG_JETSAM */ memorystatus_log("memorystatus: System is %s. memorystatus_available_pages: %llu compressor_size:%u\n", healthy ? "healthy" : "unhealthy", (uint64_t)MEMORYSTATUS_LOG_AVAILABLE_PAGES, vm_compressor_pool_size()); if (!healthy) { memorystatus_log( "memorystatus: {" "\"compressor_exhausted\": %d, " "\"zone_map_is_exhausted\": %d, " "\"swap_low\": %d, " "\"swap_exhausted\": %d" "}\n", status->msh_compressor_exhausted, status->msh_zone_map_is_exhausted, status->msh_swap_low_on_space, status->msh_swap_exhausted); } #endif /* CONFIG_JETSAM */ prev_status = *status; } bool memstat_check_system_health(memorystatus_system_health_t status) { memstat_evaluate_health_conditions(status); memstat_log_system_health(status); return memstat_is_system_healthy(status); } #pragma mark Memorystatus Thread Actions /* * This section picks the appropriate memorystatus_action & deploys it. */ uint64_t memstat_last_cache_purge_ts; /* Purge caches under critical pressure up to every 1 min */ TUNABLE(uint64_t, memstat_cache_purge_backoff_ns, "memorystatus_cache_purge_backoff_ns", 1 * 60 * NSEC_PER_SEC); static uint32_t memorystatus_pick_kill_cause(const memorystatus_system_health_t status) { assert(!memstat_is_system_healthy(status)); #if CONFIG_JETSAM if (status->msh_available_pages_below_critical) { return kMemorystatusKilledVMPageShortage; } else if (status->msh_compressor_exhausted) { return kMemorystatusKilledVMCompressorSpaceShortage; } else if (status->msh_compressor_is_thrashing) { return kMemorystatusKilledVMCompressorThrashing; } else if (status->msh_filecache_is_thrashing) { return kMemorystatusKilledFCThrashing; } else if (status->msh_zone_map_is_exhausted) { return kMemorystatusKilledZoneMapExhaustion; } else if (status->msh_pageout_starved) { return kMemorystatusKilledVMPageoutStarvation; } else { panic("decided to kill-top-process for unknown cause"); } #else /* CONFIG_JETSAM */ if (status->msh_zone_map_is_exhausted) { return kMemorystatusKilledZoneMapExhaustion; } else if (status->msh_compressor_exhausted) { return kMemorystatusKilledVMCompressorSpaceShortage; } else if (status->msh_swap_exhausted) { return kMemorystatusKilledLowSwap; } else { return kMemorystatusKilled; } #endif /* CONFIG_JETSAM */ } /* * Inspects the state of various resources in the system to see if * the system is healthy. If the system is not healthy, picks a * memorystatus_action_t to recover the system. * * Every time the memorystatus thread wakes up it calls into here * to pick an action. It will continue performing memorystatus actions until this * function returns MEMORYSTATUS_KILL_NONE. At that point the thread will block. */ memorystatus_action_t memorystatus_pick_action(jetsam_state_t state, uint32_t *kill_cause, bool highwater_remaining, bool suspended_swappable_apps_remaining, bool swappable_apps_remaining, int *jld_idle_kills) { struct memorystatus_system_health_s status; bool is_system_healthy = memstat_check_system_health(&status); #if CONFIG_JETSAM if (status.msh_available_pages_below_soft || !is_system_healthy) { /* * If swap is enabled, first check if we're running low or are out of swap space. */ if (memorystatus_swap_all_apps && jetsam_kill_on_low_swap) { if (swappable_apps_remaining && status.msh_swap_exhausted) { *kill_cause = kMemorystatusKilledLowSwap; return MEMORYSTATUS_KILL_SWAPPABLE; } else if (suspended_swappable_apps_remaining && status.msh_swap_low_on_space) { *kill_cause = kMemorystatusKilledLowSwap; return MEMORYSTATUS_KILL_SUSPENDED_SWAPPABLE; } } /* * We're below the pressure level or the system is unhealthy, * regardless of the system health let's check if we should be swapping * and if there are high watermark kills left to do. */ if (memorystatus_swap_all_apps) { if (status.msh_swappable_compressor_segments_over_limit && !vm_swapout_is_running()) { /* * TODO: The swapper will keep running until it has drained the entire early swapout queue. * That might be overly aggressive & we should look into tuning it. * See rdar://84102304. */ return MEMORYSTATUS_WAKE_SWAPPER; } else if (status.msh_swapin_queue_over_limit) { return MEMORYSTATUS_PROCESS_SWAPIN_QUEUE; } else if (status.msh_swappable_compressor_segments_over_limit) { memorystatus_log_info( "memorystatus: Skipping swap wakeup because the swap thread is already running.\n"); } } if (status.msh_compressor_exhausted || status.msh_compressor_low_on_space) { *kill_cause = kMemorystatusKilledVMCompressorSpaceShortage; return MEMORYSTATUS_KILL_TOP_PROCESS; } if (highwater_remaining) { *kill_cause = kMemorystatusKilledHiwat; return MEMORYSTATUS_KILL_HIWATER; } } if (status.msh_available_pages_below_idle && memstat_get_idle_proccnt() > 0 && is_system_healthy) { /* * The system is below the idle threshold but otherwise healthy. */ *kill_cause = kMemorystatusKilledIdleExit; return MEMORYSTATUS_KILL_IDLE; } if (memstat_reaper_is_currently_sweeping && is_system_healthy) { /* * The system is healthy and we're in a reaper sweep. */ *kill_cause = kMemorystatusKilledLongIdleExit; return MEMORYSTATUS_KILL_LONG_IDLE; } if (is_system_healthy) { *kill_cause = 0; return MEMORYSTATUS_KILL_NONE; } /* * At this point the system is unhealthy and there are no * more highwatermark processes to kill. */ if (!state->limit_to_low_bands) { if (memorystatus_check_aggressive_jetsam_needed(jld_idle_kills)) { memorystatus_log("memorystatus: Starting aggressive jetsam.\n"); *kill_cause = kMemorystatusKilledProcThrashing; return MEMORYSTATUS_KILL_AGGRESSIVE; } } /* * The system is unhealthy and we either don't need aggressive jetsam * or are not allowed to deploy it. * Kill in priority order. We'll use LRU within every band except the * FG (which will be sorted by coalition role). */ *kill_cause = memorystatus_pick_kill_cause(&status); return MEMORYSTATUS_KILL_TOP_PROCESS; #else /* !CONFIG_JETSAM */ (void) state; (void) jld_idle_kills; (void) suspended_swappable_apps_remaining; (void) swappable_apps_remaining; (void) highwater_remaining; /* * Without CONFIG_JETSAM, we only kill if the system is unhealthy. * There is no aggressive jetsam and no * early highwatermark killing. */ if (is_system_healthy) { *kill_cause = 0; return MEMORYSTATUS_KILL_NONE; } *kill_cause = memorystatus_pick_kill_cause(&status); if (status.msh_zone_map_is_exhausted) { return MEMORYSTATUS_KILL_TOP_PROCESS; } if (status.msh_compressor_exhausted || status.msh_swap_exhausted) { if (kill_on_no_paging_space) { return MEMORYSTATUS_KILL_TOP_PROCESS; } } if (status.msh_compressor_low_on_space || status.msh_swap_low_on_space) { if (memstat_get_idle_proccnt() > 0) { /* Kill all idle processes before invoking the no paging space action */ return MEMORYSTATUS_KILL_IDLE; } /* * Throttle how often the no-paging-space action is performed. */ uint64_t now = mach_absolute_time(); uint64_t delta_since_last_no_space_ns; uint64_t last_action_ts = os_atomic_load(&last_no_space_action_ts, relaxed); assert3u(now, >=, last_action_ts); absolutetime_to_nanoseconds(now - last_action_ts, &delta_since_last_no_space_ns); if (delta_since_last_no_space_ns > no_paging_space_action_throttle_delay_ns) { return MEMORYSTATUS_NO_PAGING_SPACE; } else { return MEMORYSTATUS_KILL_NONE; } } if (status.msh_vm_pressure_critical) { /* * The system is under critical memory pressure. First terminate any low-risk * idle processes. When they are exhausted, purge system memory caches. */ if (memstat_pressure_config & MEMSTAT_WARNING_KILL_LONG_IDLE && memstat_get_long_idle_proccnt |