Skip to content

Commit 2b986d3

Browse files
authored
Add signal untuning support for classifier (#164)
--- Signed-off-by: Kartik Nema <kartnema@qti.qualcomm.com>
1 parent 88a0104 commit 2b986d3

2 files changed

Lines changed: 79 additions & 44 deletions

File tree

contextual-classifier/ContextualClassifier.cpp

Lines changed: 76 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
#define CLASSIFIER_TAG "CONTEXTUAL_CLASSIFIER"
2727
#define CLASSIFIER_CONFIGS_DIR "/etc/urm/classifier/"
2828

29+
#define CURR_RESTUNE_CGRP_HNDL 0
30+
#define CURR_RESTUNE_SIG_HNDL 1
31+
2932
static const std::string FT_MODEL_PATH =
3033
CLASSIFIER_CONFIGS_DIR "fasttext_model_supervised.bin";
3134
static const std::string IGNORE_PROC_PATH =
@@ -52,15 +55,55 @@ Inference *ContextualClassifier::GetInferenceObject() {
5255
}
5356
#endif
5457

58+
static ContextualClassifier *gClassifier = nullptr;
59+
static const int32_t pendingQueueControlSize = 30;
60+
5561
ContextualClassifier::ContextualClassifier() {
56-
this->mRestuneHandle.mCurHandle = -1;
62+
this->mRestuneHandles[CURR_RESTUNE_CGRP_HNDL].mCurHandle = -1;
63+
this->mRestuneHandles[CURR_RESTUNE_SIG_HNDL].mCurHandle = -1;
5764
mInference = GetInferenceObject();
5865
}
5966

60-
static ContextualClassifier *gClassifier = nullptr;
61-
static const int32_t pendingQueueControlSize = 30;
67+
void ContextualClassifier::untuneRequestHelper(int32_t index) {
68+
try {
69+
Request* untuneRequest = MPLACED(Request);
70+
71+
untuneRequest->setRequestType(REQ_RESOURCE_UNTUNING);
72+
untuneRequest->setHandle(this->mRestuneHandles[index].mCurHandle);
73+
untuneRequest->setDuration(-1);
74+
// Passing priority as HIGH_TRANSFER_PRIORITY (= -1)
75+
// - Ensures untune requests are processed before even SERVER_HIGH priority tune requests
76+
// which helps in freeing memory
77+
// - Since this level of priority is only used internally, hence it has been customized to
78+
// not free up the underlying Request object, allowing for reuse.
79+
// Priority Level: -2 is used to force server termination and cleanup so should not be used otherwise.
80+
untuneRequest->setPriority(SYSTEM_HIGH);
81+
untuneRequest->setClientPID(this->mRestuneHandles[index].mCurReqPid);
82+
untuneRequest->setClientTID(this->mRestuneHandles[index].mCurReqTid);
83+
84+
// fast path to Request Queue
85+
// Mark verification status as true. Request still goes through RequestManager though.
86+
submitResProvisionRequest(untuneRequest, true);
87+
88+
} catch(const std::exception& e) {
89+
LOGE(CLASSIFIER_TAG,
90+
"Failed to move per-app threads to cgroup, Error: " + std::string(e.what()));
91+
}
92+
}
6293

63-
static Request* createResourceTuningRequest(uint32_t sigId,
94+
static ResIterable* createMovePidResource(int32_t cGroupdId, pid_t pid) {
95+
ResIterable* resIterable = MPLACED(ResIterable);
96+
Resource* resource = MPLACED(Resource);
97+
resource->setResCode(RES_CGRP_MOVE_PID);
98+
resource->setNumValues(2);
99+
resource->setValueAt(0, cGroupdId);
100+
resource->setValueAt(1, pid);
101+
102+
resIterable->mData = resource;
103+
return resIterable;
104+
}
105+
106+
static Request* createTuneRequestFromSignal(uint32_t sigId,
64107
uint32_t sigType,
65108
pid_t incomingPID,
66109
pid_t incomingTID) {
@@ -107,18 +150,6 @@ static Request* createResourceTuningRequest(uint32_t sigId,
107150
return nullptr;
108151
}
109152

110-
static ResIterable* createMovePidResource(int32_t cGroupdId, pid_t pid) {
111-
ResIterable* resIterable = MPLACED(ResIterable);
112-
Resource* resource = MPLACED(Resource);
113-
resource->setResCode(RES_CGRP_MOVE_PID);
114-
resource->setNumValues(2);
115-
resource->setValueAt(0, cGroupdId);
116-
resource->setValueAt(1, pid);
117-
118-
resIterable->mData = resource;
119-
return resIterable;
120-
}
121-
122153
ContextualClassifier::~ContextualClassifier() {
123154
this->Terminate();
124155
if(this->mInference) {
@@ -346,9 +377,27 @@ void ContextualClassifier::ApplyActions(uint32_t sigId,
346377
uint32_t sigType,
347378
pid_t incomingPID,
348379
pid_t incomingTID) {
349-
Request* request = createResourceTuningRequest(sigId, sigType, incomingPID, incomingTID);
380+
381+
if(this->mRestuneHandles[CURR_RESTUNE_SIG_HNDL].mCurHandle != -1) {
382+
untuneRequestHelper(CURR_RESTUNE_SIG_HNDL);
383+
mRestuneHandles[CURR_RESTUNE_SIG_HNDL].mCurHandle = -1;
384+
}
385+
386+
Request* request = createTuneRequestFromSignal(sigId, sigType, incomingPID, incomingTID);
350387
if(request != nullptr) {
351-
submitResProvisionRequest(request, false);
388+
if(request->getResourcesCount() > 0) {
389+
// Record:
390+
this->mRestuneHandles[CURR_RESTUNE_SIG_HNDL].mCurHandle = request->getHandle();
391+
this->mRestuneHandles[CURR_RESTUNE_SIG_HNDL].mCurReqPid = incomingPID;
392+
this->mRestuneHandles[CURR_RESTUNE_SIG_HNDL].mCurReqTid = incomingTID;
393+
394+
// fast path to Request Queue
395+
submitResProvisionRequest(request, true);
396+
397+
} else {
398+
Request::cleanUpRequest(request);
399+
this->mRestuneHandles[CURR_RESTUNE_SIG_HNDL].mCurHandle = -1;
400+
}
352401
}
353402
}
354403

@@ -446,32 +495,16 @@ void ContextualClassifier::MoveAppThreadsToCGroup(pid_t incomingPID,
446495
int32_t cgroupIdentifier) {
447496
try {
448497
// Check for any outstanding request, if found untune it.
449-
if(this->mRestuneHandle.mCurHandle != -1) {
450-
Request* untuneRequest = MPLACED(Request);
451-
452-
untuneRequest->setRequestType(REQ_RESOURCE_UNTUNING);
453-
untuneRequest->setHandle(this->mRestuneHandle.mCurHandle);
454-
untuneRequest->setDuration(-1);
455-
// Passing priority as HIGH_TRANSFER_PRIORITY (= -1)
456-
// - Ensures untune requests are processed before even SERVER_HIGH priority tune requests
457-
// which helps in freeing memory
458-
// - Since this level of priority is only used internally, hence it has been customized to
459-
// not free up the underlying Request object, allowing for reuse.
460-
// Priority Level: -2 is used to force server termination and cleanup so should not be used otherwise.
461-
untuneRequest->setPriority(SYSTEM_HIGH);
462-
untuneRequest->setClientPID(this->mRestuneHandle.mCurReqPid);
463-
untuneRequest->setClientTID(this->mRestuneHandle.mCurReqTid);
464-
465-
// fast path to Request Queue
466-
// Mark verification status as true. Request still goes through RequestManager though.
467-
submitResProvisionRequest(untuneRequest, true);
468-
this->mRestuneHandle.mCurHandle = -1;
498+
if(this->mRestuneHandles[CURR_RESTUNE_CGRP_HNDL].mCurHandle != -1) {
499+
untuneRequestHelper(CURR_RESTUNE_CGRP_HNDL);
500+
mRestuneHandles[CURR_RESTUNE_CGRP_HNDL].mCurHandle = -1;
469501
}
470502

471503
int64_t handleGenerated = -1;
472504
// Issue a tune request for the new pid (and any associated app-config pids)
473505
Request* request = MPLACED(Request);
474506
request->setRequestType(REQ_RESOURCE_TUNING);
507+
475508
// Generate and store the handle for future use
476509
handleGenerated = AuxRoutines::generateUniqueHandle();
477510
request->setHandle(handleGenerated);
@@ -502,22 +535,22 @@ void ContextualClassifier::MoveAppThreadsToCGroup(pid_t incomingPID,
502535
// Anything to issue
503536
if(request->getResourcesCount() > 0) {
504537
// Record:
505-
this->mRestuneHandle.mCurHandle = handleGenerated;
506-
this->mRestuneHandle.mCurReqPid = incomingPID;
507-
this->mRestuneHandle.mCurReqTid = incomingTID;
538+
this->mRestuneHandles[CURR_RESTUNE_CGRP_HNDL].mCurHandle = handleGenerated;
539+
this->mRestuneHandles[CURR_RESTUNE_CGRP_HNDL].mCurReqPid = incomingPID;
540+
this->mRestuneHandles[CURR_RESTUNE_CGRP_HNDL].mCurReqTid = incomingTID;
508541

509542
// fast path to Request Queue
510543
submitResProvisionRequest(request, true);
511544

512545
} else {
513546
Request::cleanUpRequest(request);
514-
this->mRestuneHandle.mCurHandle = -1;
547+
this->mRestuneHandles[CURR_RESTUNE_CGRP_HNDL].mCurHandle = -1;
515548
}
516549

517550
} catch(const std::exception& e) {
518551
LOGE(CLASSIFIER_TAG,
519552
"Failed to move per-app threads to cgroup, Error: " + std::string(e.what()));
520-
this->mRestuneHandle.mCurHandle = -1;
553+
this->mRestuneHandles[CURR_RESTUNE_CGRP_HNDL].mCurHandle = -1;
521554
}
522555
}
523556

contextual-classifier/Include/ContextualClassifier.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class ContextualClassifier {
6363

6464
NetLinkComm mNetLinkComm;
6565
Inference *mInference;
66-
RestuneHandleInfo mRestuneHandle;
66+
RestuneHandleInfo mRestuneHandles[2];
6767

6868
// Event queue for classifier main thread
6969
std::queue<ProcEvent> mPendingEv;
@@ -108,6 +108,8 @@ class ContextualClassifier {
108108
const std::string& comm,
109109
int32_t cgroupIdentifier);
110110

111+
void untuneRequestHelper(int32_t index);
112+
111113
public:
112114
ContextualClassifier();
113115
~ContextualClassifier();

0 commit comments

Comments
 (0)