Skip to content

Commit be42f23

Browse files
authored
Task Affinity Configuration through URM (#179)
--- Signed-off-by: Kartik Nema <kartnema@qti.qualcomm.com>
1 parent 0e97898 commit be42f23

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

configs/ResourcesConfig.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,12 @@ ResourceConfigs:
398398
Modes: ["display_on"]
399399
Policy: "pass_through"
400400
ApplyType: "global"
401+
402+
- ResType: "0x0c"
403+
ResID: "0x0000"
404+
Name: "RES_PER_TASK_AFFINITY"
405+
Supported: true
406+
Permissions: "third_party"
407+
Modes: ["display_on"]
408+
Policy: "pass_through"
409+
ApplyType: "global"

resource-tuner/core/ResourceHooks.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: BSD-3-Clause-Clear
33

44
#include <unistd.h>
5+
#include <sched.h>
56

67
#include "Utils.h"
78
#include "Logger.h"
@@ -566,6 +567,45 @@ static void limitCpuTime(void* context) {
566567
}
567568
}
568569

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+
569609
static void removeProcessFromCGroup(void* context) {
570610
if(context == nullptr) return;
571611
Resource* resource = static_cast<Resource*>(context);
@@ -682,12 +722,49 @@ static void resetRunOnCoresExclusively(void* context) {
682722
}
683723
}
684724

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+
685760
// Register the specific Callbacks
686761
URM_REGISTER_RES_APPLIER_CB(0x00090000, moveProcessToCGroup);
687762
URM_REGISTER_RES_APPLIER_CB(0x00090001, moveThreadToCGroup);
688763
URM_REGISTER_RES_APPLIER_CB(0x00090002, setRunOnCores);
689764
URM_REGISTER_RES_APPLIER_CB(0x00090003, setRunOnCoresExclusively);
690765
URM_REGISTER_RES_APPLIER_CB(0x00090005, limitCpuTime);
766+
URM_REGISTER_RES_APPLIER_CB(0x000c0000, setTaskAffinity);
691767
URM_REGISTER_RES_TEAR_CB(0x00090000, removeProcessFromCGroup);
692768
URM_REGISTER_RES_TEAR_CB(0x00090001, removeThreadFromCGroup);
693769
URM_REGISTER_RES_TEAR_CB(0x00090003, resetRunOnCoresExclusively);
770+
URM_REGISTER_RES_TEAR_CB(0x000c0000, resetTaskAffinity)

0 commit comments

Comments
 (0)