|
2 | 2 | // SPDX-License-Identifier: BSD-3-Clause-Clear |
3 | 3 |
|
4 | 4 | #include <unistd.h> |
| 5 | +#include <sched.h> |
5 | 6 |
|
6 | 7 | #include "Utils.h" |
7 | 8 | #include "Logger.h" |
@@ -566,6 +567,45 @@ static void limitCpuTime(void* context) { |
566 | 567 | } |
567 | 568 | } |
568 | 569 |
|
| 570 | +static void setTaskAffinity(void* context) { |
| 571 | + if(context == nullptr) return; |
| 572 | + Resource* resource = static_cast<Resource*>(context); |
| 573 | + if(resource->getValuesCount() < 2) return; |
| 574 | + |
| 575 | + pid_t pid = resource->getValueAt(0); |
| 576 | + |
| 577 | + // Get current mask |
| 578 | + cpu_set_t mask; |
| 579 | + if(sched_getaffinity(pid, sizeof(cpu_set_t), &mask) == -1) { |
| 580 | + TYPELOGV(ERRNO_LOG, "sched_getaffinity", strerror(errno)); |
| 581 | + } else { |
| 582 | + std::string procDesc = "task_affinity_" + std::to_string(pid); |
| 583 | + std::string maskStr = ""; |
| 584 | + |
| 585 | + // Serialize the mask as a string |
| 586 | + for(int32_t i = 0; i < CPU_SETSIZE; i++) { |
| 587 | + if(CPU_ISSET(i, &mask)) { |
| 588 | + if(maskStr.length() > 0) { |
| 589 | + maskStr.append(","); |
| 590 | + } |
| 591 | + maskStr.append(std::to_string(i)); |
| 592 | + } |
| 593 | + } |
| 594 | + |
| 595 | + ResourceRegistry::getInstance()->addDefaultValue(procDesc, maskStr); |
| 596 | + } |
| 597 | + |
| 598 | + CPU_ZERO(&mask); |
| 599 | + for(int32_t i = 1; i < resource->getValuesCount(); i++) { |
| 600 | + int32_t cpuId = resource->getValueAt(i); |
| 601 | + CPU_SET(cpuId, &mask); |
| 602 | + } |
| 603 | + |
| 604 | + if(sched_setaffinity(pid, sizeof(mask), &mask) == -1) { |
| 605 | + TYPELOGV(ERRNO_LOG, "sched_setaffinity", strerror(errno)); |
| 606 | + } |
| 607 | +} |
| 608 | + |
569 | 609 | static void removeProcessFromCGroup(void* context) { |
570 | 610 | if(context == nullptr) return; |
571 | 611 | Resource* resource = static_cast<Resource*>(context); |
@@ -682,12 +722,49 @@ static void resetRunOnCoresExclusively(void* context) { |
682 | 722 | } |
683 | 723 | } |
684 | 724 |
|
| 725 | +static void resetTaskAffinity(void* context) { |
| 726 | + if(context == nullptr) return; |
| 727 | + Resource* resource = static_cast<Resource*>(context); |
| 728 | + if(resource->getValuesCount() < 2) return; |
| 729 | + |
| 730 | + pid_t pid = resource->getValueAt(0); |
| 731 | + |
| 732 | + std::string procDesc = "task_affinity_" + std::to_string(pid); |
| 733 | + std::string defVal = ResourceRegistry::getInstance()->getDefaultValue(procDesc); |
| 734 | + |
| 735 | + if(defVal.length() == 0) { |
| 736 | + return; |
| 737 | + } |
| 738 | + |
| 739 | + // Reconstruct Original Mask |
| 740 | + cpu_set_t originalMask; |
| 741 | + CPU_ZERO(&originalMask); |
| 742 | + |
| 743 | + std::string value; |
| 744 | + std::istringstream iss(defVal); |
| 745 | + while(std::getline(iss, value, ',')) { |
| 746 | + try { |
| 747 | + int32_t cpuId = std::stoi(value); |
| 748 | + CPU_SET(cpuId, &originalMask); |
| 749 | + |
| 750 | + } catch (const std::exception&) { |
| 751 | + return; |
| 752 | + } |
| 753 | + } |
| 754 | + |
| 755 | + if(sched_setaffinity(pid, sizeof(originalMask), &originalMask) == -1) { |
| 756 | + TYPELOGV(ERRNO_LOG, "sched_setaffinity", strerror(errno)); |
| 757 | + } |
| 758 | +} |
| 759 | + |
685 | 760 | // Register the specific Callbacks |
686 | 761 | URM_REGISTER_RES_APPLIER_CB(0x00090000, moveProcessToCGroup); |
687 | 762 | URM_REGISTER_RES_APPLIER_CB(0x00090001, moveThreadToCGroup); |
688 | 763 | URM_REGISTER_RES_APPLIER_CB(0x00090002, setRunOnCores); |
689 | 764 | URM_REGISTER_RES_APPLIER_CB(0x00090003, setRunOnCoresExclusively); |
690 | 765 | URM_REGISTER_RES_APPLIER_CB(0x00090005, limitCpuTime); |
| 766 | +URM_REGISTER_RES_APPLIER_CB(0x000c0000, setTaskAffinity); |
691 | 767 | URM_REGISTER_RES_TEAR_CB(0x00090000, removeProcessFromCGroup); |
692 | 768 | URM_REGISTER_RES_TEAR_CB(0x00090001, removeThreadFromCGroup); |
693 | 769 | URM_REGISTER_RES_TEAR_CB(0x00090003, resetRunOnCoresExclusively); |
| 770 | +URM_REGISTER_RES_TEAR_CB(0x000c0000, resetTaskAffinity) |
0 commit comments