Skip to content

Commit e1368e1

Browse files
authored
Adding journal options and Refactoring dbus api calls (#175)
--- Signed-off-by: Kartik Nema <kartnema@qti.qualcomm.com>
1 parent 0838a2c commit e1368e1

15 files changed

Lines changed: 355 additions & 11 deletions

File tree

contextual-classifier/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ include_directories(${CMAKE_SOURCE_DIR}/modula/CoreModules/Include)
66
include_directories(${CMAKE_SOURCE_DIR}/extensions/Include)
77
include_directories(${CMAKE_SOURCE_DIR}/resource-tuner/core/Include)
88
include_directories(${CMAKE_SOURCE_DIR}/resource-tuner/signals/Include/)
9+
include_directories(${CMAKE_SOURCE_DIR}/resource-tuner/dbus-modules/Include/)
910

1011
# Single shared library that contains parser + classifier + netlink glue
1112
add_library(ContextualClassifier SHARED

modula/Common/Include/ErrCodes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ enum ErrCode {
4545
RC_WORKER_THREAD_ASSIGNMENT_FAILURE,
4646
RC_LOGICAL_TO_PHYSICAL_GEN_FAILED,
4747
RC_CGROUP_CREATION_FAILURE,
48+
RC_DBUS_COMM_FAIL,
4849
};
4950

5051
#define RC_IS_OK(rc) ({ \

modula/Components/Include/Logger.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ enum CommonMessageTypes {
113113
NOTIFY_CLASSIFIER_PROC_EVENT,
114114
NOTIFY_CLASSIFIER_PROC_IGNORE,
115115
NOTIFY_MODEL_PREDICTION,
116+
SYSTEM_BUS_CONN_FAILED,
116117
};
117118

118119
/**

modula/Components/Logger.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,13 @@ void Logger::typeLog(CommonMessageTypes type, const std::string& funcName, ...)
746746
Logger::log(LOG_INFO, "CONTEXTUAL_CLASSIFIER", funcName, buffer);
747747
break;
748748

749+
case CommonMessageTypes::SYSTEM_BUS_CONN_FAILED:
750+
vsnprintf(buffer, sizeof(buffer),
751+
"Failed to establish connection with system bus, Error: %s", args);
752+
753+
Logger::log(LOG_ERR, "RESTUNE_COCO_TABLE", funcName, std::string(buffer));
754+
break;
755+
749756
default:
750757
break;
751758
}

resource-tuner/CMakeLists.txt

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
# Mandatory Dependencies
22
pkg_check_modules(LIBYAML REQUIRED yaml-0.1)
33

4+
# Optional Dependency: libsystemd
5+
set(LIB_SYSTEMD_DEP_FOUND FALSE)
6+
pkg_check_modules(PC_SYSTEMD QUIET systemd)
7+
if(PC_SYSTEMD_FOUND)
8+
pkg_check_modules(SYSTEMD QUIET libsystemd)
9+
if(SYSTEMD_FOUND)
10+
set(LIB_SYSTEMD_DEP_FOUND TRUE)
11+
endif()
12+
endif()
13+
414
# Optional Modules
515
option(BUILD_TESTS "Testing" OFF)
616
option(BUILD_STATE_DETECTOR "Display Detector" OFF)
@@ -18,19 +28,33 @@ list(APPEND LINK_LIBS
1828
${LIBYAML_LIBRARIES}
1929
)
2030

21-
if(BUILD_STATE_DETECTOR)
22-
pkg_check_modules(SYSTEMD REQUIRED libsystemd)
23-
list(APPEND SOURCES extensions/StateDetector.cpp)
31+
if(LIB_SYSTEMD_DEP_FOUND)
2432
list(APPEND LINK_LIBS ${SYSTEMD_LIBRARIES})
33+
list(APPEND SOURCES dbus-modules/RestuneDBusInternal.cpp)
34+
if(BUILD_STATE_DETECTOR)
35+
list(APPEND SOURCES dbus-modules/StateDetector.cpp)
36+
endif()
37+
else()
38+
list(APPEND SOURCES dbus-modules/RestuneDBusStubs.cpp)
2539
endif()
2640

2741
add_library(RestuneCore ${SOURCES})
2842
set_target_properties(RestuneCore PROPERTIES VERSION 1.0.0 SOVERSION 1)
29-
target_include_directories(RestuneCore PRIVATE ${LIBYAML_INCLUDE_DIRS})
3043
target_link_libraries(RestuneCore PUBLIC ${LINK_LIBS})
3144

3245
target_include_directories(RestuneCore PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/core/Include)
3346
target_include_directories(RestuneCore PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/core/Server/Include)
3447
target_include_directories(RestuneCore PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/signals/Include)
3548
target_include_directories(RestuneCore PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/init/Include)
49+
target_include_directories(RestuneCore PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/dbus-modules/Include)
50+
51+
# libyaml headers
52+
target_include_directories(RestuneCore PRIVATE ${LIBYAML_INCLUDE_DIRS})
53+
54+
# libsystemd headers
55+
if(LIB_SYSTEMD_DEP_FOUND)
56+
target_include_directories(RestuneCore PRIVATE ${SYSTEMD_INCLUDE_DIRS})
57+
endif()
58+
59+
# Install RestuneCore
3660
install(TARGETS RestuneCore LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

resource-tuner/core/CocoTable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static int8_t comparLBetter(DLRootNode* newNode, DLRootNode* targetNode) {
4747
return newValue < targetValue;
4848
}
4949

50-
int8_t CocoTable::needAllocation(Resource* res) {
50+
int8_t CocoTable::needsAllocation(Resource* res) {
5151
ResConfInfo* rConf = this->mResourceRegistry->getResConf(res->getResCode());
5252
return (rConf->mPolicy != Policy::PASS_THROUGH);
5353
}

resource-tuner/core/Include/CocoTable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class CocoTable {
126126

127127
void fastPathApply(Resource* resource);
128128
void fastPathReset(Resource* resource);
129-
int8_t needAllocation(Resource* res);
129+
int8_t needsAllocation(Resource* res);
130130

131131
public:
132132
~CocoTable();

resource-tuner/core/Include/TargetRegistry.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
#include <regex>
2222
#include <memory>
2323

24+
#include "Logger.h"
25+
#include "ErrCodes.h"
2426
#include "UrmSettings.h"
2527
#include "AuxRoutines.h"
26-
#include "ErrCodes.h"
27-
#include "Logger.h"
28+
#include "RestuneDBus.h"
2829

2930
#define POLICY_DIR_PATH "/sys/devices/system/cpu/cpufreq/"
3031
#define ONLINE_CPU_FILE_PATH "/sys/devices/system/cpu/online"
@@ -166,6 +167,7 @@ class TargetRegistry {
166167

167168
ErrCode addIrqAffine(std::vector<std::string>& values,
168169
int8_t areClusterValues = false);
170+
ErrCode addLogLimit(std::vector<std::string>& values);
169171

170172
static std::shared_ptr<TargetRegistry> getInstance() {
171173
if(targetRegistryInstance == nullptr) {

resource-tuner/core/TargetRegistry.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ void TargetRegistry::generatePolicyBasedMapping(std::vector<std::string>& policy
162162
}
163163
}
164164

165+
static std::string trimStr(const std::string &s) {
166+
size_t start = s.find_first_not_of(" \t");
167+
size_t end = s.find_last_not_of(" \t\r");
168+
return (start == std::string::npos) ? "" : s.substr(start, end - start + 1);
169+
}
170+
165171
void TargetRegistry::getClusterIdBasedMapping() {
166172
const std::string cpuDir = "/sys/devices/system/cpu";
167173
const std::regex cpuRegex("^cpu([0-9]+)$");
@@ -551,6 +557,95 @@ ErrCode TargetRegistry::addIrqAffine(std::vector<std::string>& values,
551557
return RC_SUCCESS;
552558
}
553559

560+
ErrCode TargetRegistry::addLogLimit(std::vector<std::string>& values) {
561+
if(values.size() < 1) {
562+
return RC_INVALID_VALUE;
563+
}
564+
565+
std::string logLevel = values[0];
566+
if(logLevel != "minimal") {
567+
return RC_SUCCESS;
568+
}
569+
570+
const std::string journaldConfFile = "/etc/systemd/journald.conf";
571+
const std::unordered_map<std::string, std::string> configOptions = {
572+
{"RuntimeMaxUse", "20M"},
573+
{"RuntimeMaxFileSize", "128K"},
574+
{"MaxLevelStore", "notice"},
575+
{"MaxLevelSyslog", "notice"},
576+
{"MaxLevelKMsg", "notice"},
577+
{"MaxLevelConsole", "notice"},
578+
{"ForwardToSyslog", "no"}
579+
};
580+
581+
std::ifstream confInStream(journaldConfFile);
582+
if(!confInStream) {
583+
return RC_SUCCESS;
584+
}
585+
586+
std::ostringstream oldContent;
587+
std::ostringstream newContent;
588+
std::string line;
589+
int8_t journalSectionFound = false;
590+
591+
std::unordered_map<std::string, int8_t> keyUpdated;
592+
for(auto &entry : configOptions) {
593+
keyUpdated[entry.first] = false;
594+
}
595+
596+
while(std::getline(confInStream, line)) {
597+
std::string trimmedLine = trimStr(line);
598+
int8_t replaced = false;
599+
600+
if(trimmedLine == "[Journal]") {
601+
journalSectionFound = true;
602+
}
603+
604+
for(auto &entry : configOptions) {
605+
if(trimmedLine.find(entry.first + "=") == 0 ||
606+
trimmedLine.find("#" + entry.first + "=") == 0) {
607+
newContent << entry.first << "=" << entry.second << "\n";
608+
keyUpdated[entry.first] = true;
609+
replaced = true;
610+
break;
611+
}
612+
}
613+
if(!replaced) {
614+
newContent << line << "\n";
615+
}
616+
oldContent << line << "\n";
617+
}
618+
confInStream.close();
619+
620+
if(!journalSectionFound) {
621+
newContent << "\n[Journal]\n";
622+
}
623+
624+
for(auto &entry : configOptions) {
625+
if(!keyUpdated[entry.first]) {
626+
newContent << entry.first << "=" << entry.second << "\n";
627+
}
628+
}
629+
630+
std::ofstream confOutStream(journaldConfFile);
631+
confOutStream << newContent.str();
632+
confOutStream.close();
633+
634+
// Set printk kernel logging to minimal
635+
const std::string printkPath = "/proc/sys/kernel/printk";
636+
const std::string newLevels = "3 4 1 3";
637+
638+
std::ofstream printkFile(printkPath);
639+
if(printkFile.is_open()) {
640+
printkFile << newLevels;
641+
printkFile.close();
642+
}
643+
644+
// Restart journald
645+
RestuneSDBus::getInstance()->restartService("systemd-journald.service");
646+
return RC_SUCCESS;
647+
}
648+
554649
// Methods for Building CGroup Config from InitConfigs.yaml
555650
CGroupConfigInfoBuilder::CGroupConfigInfoBuilder() {
556651
this->mCGroupConfigInfo = new(std::nothrow) CGroupConfigInfo;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
2+
// SPDX-License-Identifier: BSD-3-Clause-Clear
3+
4+
#ifndef RESTUNE_DBUS_INTERNAL_H
5+
#define RESTUNE_DBUS_INTERNAL_H
6+
7+
#include <memory>
8+
9+
#include "Logger.h"
10+
#include "ErrCodes.h"
11+
12+
class RestuneSDBus {
13+
private:
14+
static std::shared_ptr<RestuneSDBus> restuneSDBusInstance;
15+
void* mBus;
16+
17+
RestuneSDBus();
18+
19+
public:
20+
~RestuneSDBus();
21+
22+
ErrCode stopService(const std::string& unitName);
23+
ErrCode startService(const std::string& unitName);
24+
ErrCode restartService(const std::string& unitName);
25+
26+
// For custom use-cases, like StateDetector while still keeping
27+
// a single open connection to system-bus.
28+
void* getBus();
29+
30+
static std::shared_ptr<RestuneSDBus> getInstance() {
31+
if(restuneSDBusInstance == nullptr) {
32+
restuneSDBusInstance = std::shared_ptr<RestuneSDBus>(new RestuneSDBus());
33+
}
34+
return restuneSDBusInstance;
35+
}
36+
};
37+
38+
#endif

0 commit comments

Comments
 (0)