@@ -50,14 +50,11 @@ Inference *ContextualClassifier::GetInferenceObject() {
5050#endif
5151
5252ContextualClassifier::ContextualClassifier () {
53+ this ->mRestuneHandle = -1 ;
5354 mInference = GetInferenceObject ();
5455}
5556
5657static ContextualClassifier *g_classifier = nullptr ;
57-
58- // Global Restune handle store, used across threads
59- static int64_t gCurrRestuneHandle = -1 ;
60-
6158static const int32_t pendingQueueControlSize = 30 ;
6259
6360ContextualClassifier::~ContextualClassifier () {
@@ -75,25 +72,24 @@ ErrCode ContextualClassifier::Init() {
7572 this ->mOurPid = getpid ();
7673 this ->mOurTid = gettid ();
7774
78- LoadIgnoredProcesses ();
75+ this -> LoadIgnoredProcesses ();
7976
8077 // Single worker thread for classification
81- mClassifierMain = std::thread (&ContextualClassifier::ClassifierMain, this );
78+ this -> mClassifierMain = std::thread (&ContextualClassifier::ClassifierMain, this );
8279
83- if (mNetLinkComm .connect () == -1 ) {
80+ if (this -> mNetLinkComm .connect () == -1 ) {
8481 LOGE (CLASSIFIER_TAG, " Failed to connect to netlink socket." );
8582 return RC_SOCKET_OP_FAILURE;
8683 }
8784
88- if (mNetLinkComm .set_listen (true ) == -1 ) {
85+ if (this -> mNetLinkComm .set_listen (true ) == -1 ) {
8986 LOGE (CLASSIFIER_TAG, " Failed to set proc event listener." );
9087 mNetLinkComm .close_socket ();
9188 return RC_SOCKET_OP_FAILURE;
9289 }
9390 LOGI (CLASSIFIER_TAG, " Now listening for process events." );
9491
95- mNetlinkThread = std::thread (&ContextualClassifier::HandleProcEv, this );
96-
92+ this ->mNetlinkThread = std::thread (&ContextualClassifier::HandleProcEv, this );
9793 return RC_SUCCESS;
9894}
9995
@@ -165,19 +161,19 @@ void ContextualClassifier::ClassifierMain() {
165161 // Identify if any signal configuration exists
166162 // Will return the sigID based on the workload
167163 // For example: game, browser, multimedia
168- GetSignalDetailsForWorkload (contextType, sigId, sigSubtype);
164+ this -> GetSignalDetailsForWorkload (contextType, sigId, sigSubtype);
169165
170166 // Step 2:
171167 // - Move the process to focused-cgroup, Also involves removing the process
172168 // already there from the cgroup.
173169 // - Move the "threads" from per-app config to appropriate cgroups
174- gCurrRestuneHandle = MoveAppThreadsToCGroup (ev.pid , comm, FOCUSED_CGROUP_IDENTIFIER);
170+ this -> MoveAppThreadsToCGroup (ev.pid , comm, FOCUSED_CGROUP_IDENTIFIER);
175171
176172 // Step 3: If the post processing block exists, call it
177173 // It might provide us a more specific sigSubtype
178174 PostProcessingCallback postCb =
179175 Extensions::getPostProcessingCallback (comm);
180- if (postCb) {
176+ if (postCb != nullptr ) {
181177 PostProcessCBData postProcessData = {
182178 .mPid = ev.pid ,
183179 .mSigId = sigId,
@@ -189,15 +185,15 @@ void ContextualClassifier::ClassifierMain() {
189185 sigSubtype = postProcessData.mSigSubtype ;
190186 }
191187
192- // Step 4: Apply actions, call tuneSignal
188+ // Step 4: Apply actions, call tuneSignal
193189 // Skip
194190 // ApplyActions(comm, sigId, sigSubtype);
195191 }
196192 } else if (ev.type == CC_APP_CLOSE) {
197193 // Step1: move process to original cgroup
198194
199195 // Step2: remove actions, call untune signal
200- RemoveActions (ev.pid , ev.tgid );
196+ // RemoveActions(ev.pid, ev.tgid);
201197 }
202198 }
203199}
@@ -232,7 +228,7 @@ int ContextualClassifier::HandleProcEv() {
232228
233229 switch (rc) {
234230 case CC_APP_OPEN:
235- TYPELOGV (NOTIFY_CLASSIFIER_PROC_EVENT, " CC_APP_OPEN" , ev.pid );
231+ // TYPELOGV(NOTIFY_CLASSIFIER_PROC_EVENT, "CC_APP_OPEN", ev.pid);
236232 if (!this ->isIgnoredProcess (ev.type , ev.pid )) {
237233 const std::lock_guard<std::mutex> lock (mQueueMutex );
238234 this ->mPendingEv .push (ev);
@@ -241,13 +237,13 @@ int ContextualClassifier::HandleProcEv() {
241237 }
242238 this ->mQueueCond .notify_one ();
243239 } else {
244- TYPELOGV (NOTIFY_CLASSIFIER_PROC_IGNORE, ev.pid );
240+ // TYPELOGV(NOTIFY_CLASSIFIER_PROC_IGNORE, ev.pid);
245241 }
246242
247243 break ;
248244
249245 case CC_APP_CLOSE:
250- TYPELOGV (NOTIFY_CLASSIFIER_PROC_EVENT, " CC_APP_CLOSE" , ev.pid );
246+ // TYPELOGV(NOTIFY_CLASSIFIER_PROC_EVENT, "CC_APP_CLOSE", ev.pid);
251247 if (!this ->isIgnoredProcess (ev.type , ev.pid )) {
252248 const std::lock_guard<std::mutex> lock (mQueueMutex );
253249 this ->mPendingEv .push (ev);
@@ -256,7 +252,7 @@ int ContextualClassifier::HandleProcEv() {
256252 }
257253 this ->mQueueCond .notify_one ();
258254 } else {
259- TYPELOGV (NOTIFY_CLASSIFIER_PROC_IGNORE, ev.pid );
255+ // TYPELOGV(NOTIFY_CLASSIFIER_PROC_IGNORE, ev.pid);
260256 }
261257
262258 break ;
@@ -383,7 +379,7 @@ bool ContextualClassifier::isIgnoredProcess(int32_t evType, pid_t pid) {
383379 proc_name = proc_name.substr (first, (last - first + 1 ));
384380 }
385381 if (mIgnoredProcesses .count (proc_name) != 0U ) {
386- LOGD (CLASSIFIER_TAG, " Ignoring process: " + proc_name);
382+ LOGD (CLASSIFIER_TAG, " Ignoring process: " + proc_name);
387383 mIgnoredPids .insert (pid);
388384 ignore = true ;
389385 }
@@ -452,7 +448,7 @@ bool ContextualClassifier::IsNumericString(const std::string& str) {
452448
453449ResIterable* ContextualClassifier::createMovePidResource (int32_t cGroupdId, pid_t pid) {
454450 ResIterable* resIterable = MPLACED (ResIterable);
455- Resource* resource = MPLACEV (Resource);
451+ Resource* resource = MPLACED (Resource);
456452 resource->setResCode (RES_CGRP_MOVE_PID);
457453 resource->setNumValues (2 );
458454 resource->setValueAt (0 , cGroupdId);
@@ -462,61 +458,62 @@ ResIterable* ContextualClassifier::createMovePidResource(int32_t cGroupdId, pid_
462458 return resIterable;
463459}
464460
465- int64_t ContextualClassifier::MoveAppThreadsToCGroup (pid_t incomingPID,
466- const std::string& comm,
467- int32_t cgroupIdentifier) {
468- // Check for any outstanding request, if found untune it.
469- if (gCurrRestuneHandle != -1 ) {
470- static Request* untuneRequest = nullptr ;
471- if (untuneRequest == nullptr ) {
472- untuneRequest = MPLACED (Request);
473- }
474-
475- untuneRequest->setRequestType (REQ_RESOURCE_UNTUNING);
476- untuneRequest->setHandle (gCurrRestuneHandle );
477- untuneRequest->setDuration (-1 );
478-
479- // Passing priority as HIGH_TRANSFER_PRIORITY (= -1)
480- // - Ensures untune requests are processed before even SERVER_HIGH priority tune requests
481- // which helps in freeing memory
482- // - Since this level of priority is only used internally, hence it has been customized to
483- // not free up the underlying Request object, allowing for reuse.
484- // Priority Level: -2 is used to force server termination and cleanup so should not be used otherwise.
485- untuneRequest->setProperties (HIGH_TRANSFER_PRIORITY);
486- untuneRequest->setClientPID (mOurPid );
487- untuneRequest->setClientTID (mOurTid );
488-
489- // fast path to Request Queue
490- // Mark verification status as true. Request still goes through RequestManager though.
491- LOGD (CLASSIFIER_TAG, " Issuing untune request for handle = " + std::to_string (gCurrRestuneHandle ));
492- submitResProvisionRequest (untuneRequest, true );
493- gCurrRestuneHandle = -1 ;
494- }
461+ void ContextualClassifier::MoveAppThreadsToCGroup (pid_t incomingPID,
462+ const std::string& comm,
463+ int32_t cgroupIdentifier) {
464+ (void )comm;
465+ try {
466+ // Check for any outstanding request, if found untune it.
467+ LOGE (CLASSIFIER_TAG, " enter MoveAppThreadsToCGroup" );
468+ if (this ->mRestuneHandle != -1 ) {
469+ LOGE (" Creating untune request for handle = " , std::to_string (this ->mRestuneHandle ));
470+ Request* untuneRequest = MPLACED (Request);
471+
472+ untuneRequest->setRequestType (REQ_RESOURCE_UNTUNING);
473+ untuneRequest->setHandle (this ->mRestuneHandle );
474+ untuneRequest->setDuration (-1 );
475+ // Passing priority as HIGH_TRANSFER_PRIORITY (= -1)
476+ // - Ensures untune requests are processed before even SERVER_HIGH priority tune requests
477+ // which helps in freeing memory
478+ // - Since this level of priority is only used internally, hence it has been customized to
479+ // not free up the underlying Request object, allowing for reuse.
480+ // Priority Level: -2 is used to force server termination and cleanup so should not be used otherwise.
481+ untuneRequest->setPriority (SYSTEM_HIGH);
482+ untuneRequest->setClientPID (mOurPid );
483+ untuneRequest->setClientTID (mOurTid );
495484
496- // Issue a tune request for the new pid (and any associated app-config pids)
497- int64_t handleGenerated = -1 ;
485+ // fast path to Request Queue
486+ // Mark verification status as true. Request still goes through RequestManager though.
487+ LOGE (" Issuing untune request for handle = " , std::to_string (this ->mRestuneHandle ));
488+ submitResProvisionRequest (untuneRequest, true );
489+ this ->mRestuneHandle = -1 ;
490+ LOGE (CLASSIFIER_TAG, " Untune request issued" );
491+ }
498492
499- try {
493+ // Issue a tune request for the new pid (and any associated app-config pids)
494+ LOGE (CLASSIFIER_TAG, " Starting tune request creation" );
500495 Request* request = MPLACED (Request);
501496 request->setRequestType (REQ_RESOURCE_TUNING);
502497 // Generate and store the handle for future use
503- handleGenerated = AuxRoutines::generateUniqueHandle ();
504- request->setHandle (handleGenerated );
498+ this -> mRestuneHandle = AuxRoutines::generateUniqueHandle ();
499+ request->setHandle (this -> mRestuneHandle );
505500 request->setDuration (-1 );
506- request->setProperties (RequestPriority::REQ_PRIORITY_HIGH );
507- request->setClientPID (mOurPid );
508- request->setClientTID (mOurTid );
501+ request->setPriority (SYSTEM_LOW );
502+ request->setClientPID (this -> mOurPid );
503+ request->setClientTID (this -> mOurTid );
509504
510505 // Move the incoming pid
511- request->addResource (this ->createMovePidResource (cgroupIdentifier, incomingPID));
506+ LOGE (CLASSIFIER_TAG, " proceeding with reources creation" );
507+ ResIterable* resIter = this ->createMovePidResource (cgroupIdentifier, incomingPID);
508+ request->addResource (resIter);
512509
513510 AppConfig* appConfig = AppConfigs::getInstance ()->getAppConfig (comm);
514511 if (appConfig != nullptr && appConfig->mThreadNameList != nullptr ) {
515512 int32_t numThreads = appConfig->mNumThreads ;
516513 // Go over the list of proc names (comm) and get their pids
517514 for (int32_t i = 0 ; i < numThreads; i++) {
518515 std::string targetComm = appConfig->mThreadNameList [i];
519- pid_t targetPID = FetchPid (targetComm);
516+ pid_t targetPID = this -> FetchPid (targetComm);
520517 if (targetPID != -1 && targetPID != incomingPID) {
521518 // Get the CGroup
522519 int32_t currCGroupID = appConfig->mCGroupIds [i];
@@ -531,15 +528,14 @@ int64_t ContextualClassifier::MoveAppThreadsToCGroup(pid_t incomingPID,
531528 submitResProvisionRequest (request, true );
532529 } else {
533530 Request::cleanUpRequest (request);
534- return -1 ;
531+ this -> mRestuneHandle = -1 ;
535532 }
536533
537534 } catch (const std::exception& e) {
538535 LOGE (CLASSIFIER_TAG,
539536 " Failed to move per-app threads to cgroup, Error: " + std::string (e.what ()));
537+ this ->mRestuneHandle = -1 ;
540538 }
541-
542- return handleGenerated;
543539}
544540
545541// Public C interface exported from the contextual-classifier shared library.
0 commit comments