!83 [sync] PR-79: update to v2.0.1-1

From: @openeuler-sync-bot 
Reviewed-by: @Lostwayzxc, @ksana123 
Signed-off-by: @Lostwayzxc, @ksana123
This commit is contained in:
openeuler-ci-bot 2024-12-11 11:57:50 +00:00 committed by Gitee
commit 57b5cfbbd9
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
13 changed files with 60 additions and 6166 deletions

View File

@ -0,0 +1,39 @@
From 2b39cac80efe7327b6d68fe01934919bc41249b8 Mon Sep 17 00:00:00 2001
From: fly_1997 <flylove7@outlook.com>
Date: Tue, 10 Dec 2024 21:46:27 +0800
Subject: [PATCH] change the folder permission to 755, add oeaware group
permission to liboeaware-sdk.so
---
rpm/oeAware.spec | 1 +
src/common/utils.cpp | 2 +-
2 files changed, 2 insertions(+), 1 deletion(-)
diff --git a/rpm/oeAware.spec b/rpm/oeAware.spec
index 5fbaf1f..4c01d0a 100644
--- a/rpm/oeAware.spec
+++ b/rpm/oeAware.spec
@@ -60,6 +60,7 @@ install -D -m 0640 ./build/output/plugin/lib/xcall.yaml %{buildroot}%{
%post
if ! grep -q "oeaware:" /etc/group; then
groupadd oeaware
+ setfacl -m g:oeaware:r /usr/lib64/liboeaware-sdk.so
fi
systemctl start oeaware.service
chcon -t modules_object_t %{_prefix}/lib/smc/smc_acc.ko >/dev/null 2>&1
diff --git a/src/common/utils.cpp b/src/common/utils.cpp
index 8f63a70..b118235 100644
--- a/src/common/utils.cpp
+++ b/src/common/utils.cpp
@@ -167,7 +167,7 @@ bool CreateDir(const std::string &path)
if (stat(subPath.c_str(), &buffer) == 0) {
continue;
}
- if (mkdir(subPath.c_str(), S_IRWXU | S_IRWXG) != 0) {
+ if (mkdir(subPath.c_str(), S_IRWXU | S_IXGRP | S_IRGRP | S_IROTH | S_IXOTH) != 0) {
return false;
}
} while (pos != std::string::npos);
--
2.45.2.windows.1

File diff suppressed because it is too large Load Diff

View File

@ -1,524 +0,0 @@
From 9fb0aa74fece1deea72ef7c8eef5e45da2113b21 Mon Sep 17 00:00:00 2001
From: fly_1997 <flylove7@outlook.com>
Date: Tue, 26 Nov 2024 15:59:15 +0800
Subject: [PATCH 2/5] add data free and fix unsubscribe error
---
src/common/CMakeLists.txt | 7 +
src/common/data_register.cpp | 247 +++++++++++++++----
src/common/data_register.h | 20 +-
src/plugin/collect/include/command_data.h | 2 +-
src/plugin_mgr/event/unsubscribe_handler.cpp | 7 +-
src/plugin_mgr/instance_run_handler.cpp | 2 +-
src/sdk/oe_client.cpp | 1 +
7 files changed, 227 insertions(+), 59 deletions(-)
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index 2d49b3e..46084e5 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -22,6 +22,13 @@ target_link_libraries(${PROJECT_NAME} log4cplus)
target_link_libraries(${PROJECT_NAME} yaml-cpp)
target_link_libraries(${PROJECT_NAME} curl boundscheck)
+if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
+target_link_directories(${PROJECT_NAME} PUBLIC
+ ${LIB_KPERF_LIBPATH}
+)
+target_link_libraries(${PROJECT_NAME} kperf)
+endif()
+
file(COPY
"${CMAKE_CURRENT_SOURCE_DIR}/data_list.h"
DESTINATION "${CMAKE_BINARY_DIR}/output/include")
diff --git a/src/common/data_register.cpp b/src/common/data_register.cpp
index 89e660e..cdf6d97 100644
--- a/src/common/data_register.cpp
+++ b/src/common/data_register.cpp
@@ -16,68 +16,107 @@
namespace oeaware {
-int TopicSerialize(const void *topic, OutStream &out)
+void TopicFree(CTopic *topic)
{
- auto tmpTopic = static_cast<const CTopic*>(topic);
- std::string instanceName(tmpTopic->instanceName);
- std::string topicName(tmpTopic->topicName);
- std::string params(tmpTopic->params);
+ if (topic == nullptr) {
+ return;
+ }
+ if (topic->instanceName != nullptr) {
+ delete[] topic->instanceName;
+ topic->instanceName = nullptr;
+ }
+ if (topic->topicName != nullptr) {
+ delete[] topic->topicName;
+ topic->topicName = nullptr;
+ }
+ if (topic->params != nullptr) {
+ delete[] topic->params;
+ topic->params = nullptr;
+ }
+}
+
+int TopicSerialize(const CTopic *topic, OutStream &out)
+{
+ std::string instanceName(topic->instanceName);
+ std::string topicName(topic->topicName);
+ std::string params(topic->params);
out << instanceName << topicName << params;
return 0;
}
-int TopicDeserialize(void *topic, InStream &in)
+int TopicDeserialize(CTopic *topic, InStream &in)
{
std::string instanceName;
std::string topicName;
std::string params;
in >> instanceName >> topicName >> params;
- ((CTopic*)topic)->instanceName = new char[instanceName.size() + 1];
- ((CTopic*)topic)->topicName = new char[topicName.size() + 1];
- ((CTopic*)topic)->params = new char[params.size() + 1];
+ topic->instanceName = new char[instanceName.size() + 1];
+ topic->topicName = new char[topicName.size() + 1];
+ topic->params = new char[params.size() + 1];
- auto ret = strcpy_s(((CTopic*)topic)->instanceName, instanceName.size() + 1, instanceName.data());
+ auto ret = strcpy_s(topic->instanceName, instanceName.size() + 1, instanceName.data());
if (ret != EOK) return ret;
- ret = strcpy_s(((CTopic*)topic)->topicName, topicName.size() + 1, topicName.data());
+ ret = strcpy_s(topic->topicName, topicName.size() + 1, topicName.data());
if (ret != EOK) return ret;
- ret = strcpy_s(((CTopic*)topic)->params, params.size() + 1, params.data());
+ ret = strcpy_s(topic->params, params.size() + 1, params.data());
if (ret != EOK) return ret;
return 0;
}
-int DataListSerialize(const void *dataList, OutStream &out)
+void DataListFree(DataList *dataList)
+{
+ if (dataList == nullptr) {
+ return;
+ }
+ auto &reg = Register::GetInstance();
+ DataFreeFunc free = reg.GetDataFreeFunc(Concat({dataList->topic.instanceName, dataList->topic.topicName}, "::"));
+ if (free == nullptr) {
+ free = reg.GetDataFreeFunc(dataList->topic.instanceName);
+ }
+ if (free != nullptr) {
+ for (uint64_t i = 0; i < dataList->len; ++i) {
+ free(dataList->data[i]);
+ }
+ }
+ TopicFree(&dataList->topic);
+ if (dataList->data != nullptr) {
+ delete[] dataList->data;
+ dataList->data = nullptr;
+ }
+}
+
+int DataListSerialize(const DataList *dataList, OutStream &out)
{
- auto tmpList = static_cast<const DataList*>(dataList);
- TopicSerialize(&tmpList->topic, out);
- out << tmpList->len;
+ TopicSerialize(&dataList->topic, out);
+ out << dataList->len;
auto &reg = Register::GetInstance();
- auto func = reg.GetDataSerialize(Concat({tmpList->topic.instanceName, tmpList->topic.topicName}, "::"));
+ auto func = reg.GetDataSerialize(Concat({dataList->topic.instanceName, dataList->topic.topicName}, "::"));
if (func == nullptr) {
- func = reg.GetDataSerialize(tmpList->topic.instanceName);
+ func = reg.GetDataSerialize(dataList->topic.instanceName);
}
- for (uint64_t i = 0; i < tmpList->len; ++i) {
- func(tmpList->data[i], out);
+ for (uint64_t i = 0; i < dataList->len; ++i) {
+ func(dataList->data[i], out);
}
return 0;
}
-int DataListDeserialize(void *dataList, InStream &in)
+int DataListDeserialize(DataList *dataList, InStream &in)
{
CTopic topic;
TopicDeserialize(&topic, in);
uint64_t size;
in >> size;
- ((DataList*)dataList)->topic = topic;
- ((DataList*)dataList)->len = size;
- ((DataList*)dataList)->data = new void* [size];
+ dataList->topic = topic;
+ dataList->len = size;
+ dataList->data = new void* [size];
auto &reg = Register::GetInstance();
auto func = reg.GetDataDeserialize(Concat({topic.instanceName, topic.topicName}, "::"));
if (func == nullptr) {
func = reg.GetDataDeserialize(topic.instanceName);
}
for (uint64_t i = 0; i < size; ++i) {
- ((DataList*)dataList)->data[i] = nullptr;
- auto ret = func(&(((DataList*)dataList)->data[i]), in);
+ dataList->data[i] = nullptr;
+ auto ret = func(&(dataList->data[i]), in);
if (ret) {
return ret;
}
@@ -85,6 +124,16 @@ int DataListDeserialize(void *dataList, InStream &in)
return 0;
}
+void ResultFree(Result *result)
+{
+ if (result == nullptr) {
+ return;
+ }
+ if (result->payload != nullptr) {
+ delete[] result->payload;
+ result->payload = nullptr;
+ }
+}
int ResultDeserialize(void *data, InStream &in)
{
@@ -96,6 +145,17 @@ int ResultDeserialize(void *data, InStream &in)
return ret;
}
#if defined(__arm__) || defined(__aarch64__)
+void PmuBaseDataFree(void *data)
+{
+ auto tmpData = static_cast<PmuCountingData*>(data);
+ if (tmpData == nullptr) {
+ return;
+ }
+ PmuDataFree(tmpData->pmuData);
+ tmpData->pmuData = nullptr;
+ tmpData->len = 0;
+}
+
int PmuCountingDataSerialize(const void *data, OutStream &out)
{
auto tmpData = static_cast<const PmuCountingData*>(data);
@@ -456,6 +516,19 @@ int PmuUncoreDataDeserialize(void **data, InStream &in)
}
#endif
+void ThreadInfoFree(void *data)
+{
+ auto threadInfo = static_cast<ThreadInfo*>(data);
+ if (threadInfo == nullptr) {
+ return;
+ }
+ if (threadInfo->name != nullptr) {
+ delete[] threadInfo->name;
+ threadInfo->name = nullptr;
+ }
+ delete threadInfo;
+}
+
int ThreadInfoSerialize(const void *data, OutStream &out)
{
auto threadInfo = static_cast<const ThreadInfo*>(data);
@@ -476,6 +549,32 @@ int ThreadInfoDeserialize(void **data, InStream &in)
return 0;
}
+void KernelDataFree(void *data)
+{
+ auto tmpData = static_cast<const KernelData*>(data);
+ if (tmpData == nullptr) {
+ return;
+ }
+ KernelDataNode *node = tmpData->kernelData;
+ for (int i = 0; i < tmpData->len; ++i) {
+ auto tmp = node->next;
+ if (node == nullptr) {
+ break;
+ }
+ if (node->key != nullptr) {
+ delete[] node->key;
+ node->key = nullptr;
+ }
+ if (node->value != nullptr) {
+ delete[] node->value;
+ node->value = nullptr;
+ }
+ delete node;
+ node = tmp;
+ }
+ delete tmpData;
+}
+
int KernelDataSerialize(const void *data, OutStream &out)
{
auto tmpData = static_cast<const KernelData*>(data);
@@ -523,17 +622,47 @@ int KernelDataDeserialize(void **data, InStream &in)
return 0;
}
+void CommandDataFree(void *data)
+{
+ CommandData *commandData = (CommandData*)data;
+ if (commandData == nullptr) {
+ return;
+ }
+ for (int i = 0; i < commandData->attrLen; ++i) {
+ if (commandData->itemAttr[i] != nullptr) {
+ delete[] commandData->itemAttr[i];
+ commandData->itemAttr[i] = nullptr;
+ }
+ }
+ if (commandData->items == nullptr) {
+ delete commandData;
+ return;
+ }
+ for (int i = 0; i < commandData->itemLen; ++i) {
+ for (int j = 0; j < commandData->attrLen; ++j) {
+ if (commandData->items[i].value[j] != nullptr) {
+ delete[] commandData->items[i].value[j];
+ commandData->items[i].value[j] = nullptr;
+ }
+ }
+ }
+ delete[] commandData->items;
+ commandData->items = nullptr;
+
+ delete commandData;
+}
+
int CommandDataSerialize(const void *data, OutStream &out)
{
- auto sarData = (SarData*)data;
- out << sarData->attrLen << sarData->itemLen;
- for (int i = 0; i < sarData->attrLen; ++i) {
- std::string attr(sarData->itemAttr[i]);
+ auto commandData = (CommandData*)data;
+ out << commandData->attrLen << commandData->itemLen;
+ for (int i = 0; i < commandData->attrLen; ++i) {
+ std::string attr(commandData->itemAttr[i]);
out << attr;
}
- for (int i = 0; i < sarData->itemLen; ++i) {
- for (int j = 0; j < sarData->attrLen; ++j) {
- std::string item(sarData->items[i].value[j]);
+ for (int i = 0; i < commandData->itemLen; ++i) {
+ for (int j = 0; j < commandData->attrLen; ++j) {
+ std::string item(commandData->items[i].value[j]);
out << item;
}
}
@@ -542,8 +671,8 @@ int CommandDataSerialize(const void *data, OutStream &out)
int CommandDataDeserialize(void **data, InStream &in)
{
- *data = new SarData();
- auto sarData = static_cast<SarData*>(*data);
+ *data = new CommandData();
+ auto sarData = static_cast<CommandData*>(*data);
in >> sarData->attrLen >> sarData->itemLen;
int ret;
for (int i = 0; i < sarData->attrLen; ++i) {
@@ -570,6 +699,15 @@ int CommandDataDeserialize(void **data, InStream &in)
return 0;
}
+void AnalysisDataFree(void *data)
+{
+ auto analysisData = static_cast<AdaptData*>(data);
+ if (analysisData == nullptr) {
+ return;
+ }
+ delete analysisData;
+}
+
int AnalysisDataSerialize(const void *data, OutStream &out)
{
auto analysisData = static_cast<const AdaptData*>(data);
@@ -600,44 +738,55 @@ int AnalysisDataDeserialize(void **data, InStream &in)
void Register::RegisterData(const std::string &name, const RegisterEntry &entry)
{
- deserializeFuncs[name] = entry;
+ registerEntry[name] = entry;
}
void Register::InitRegisterData()
{
#if defined(__arm__) || defined(__aarch64__)
- RegisterData("pmu_counting_collector", RegisterEntry(PmuCountingDataSerialize, PmuCountingDataDeserialize));
+ RegisterData("pmu_counting_collector", RegisterEntry(PmuCountingDataSerialize, PmuCountingDataDeserialize,
+ PmuBaseDataFree));
- RegisterData("pmu_sampling_collector", RegisterEntry(PmuSamplingDataSerialize, PmuSamplingDataDeserialize));
+ RegisterData("pmu_sampling_collector", RegisterEntry(PmuSamplingDataSerialize, PmuSamplingDataDeserialize,
+ PmuBaseDataFree));
- RegisterData("pmu_spe_collector", RegisterEntry(PmuSpeDataSerialize, PmuSpeDataDeserialize));
+ RegisterData("pmu_spe_collector", RegisterEntry(PmuSpeDataSerialize, PmuSpeDataDeserialize, PmuBaseDataFree));
- RegisterData("pmu_uncore_collector", RegisterEntry(PmuUncoreDataSerialize, PmuUncoreDataDeserialize));
+ RegisterData("pmu_uncore_collector", RegisterEntry(PmuUncoreDataSerialize, PmuUncoreDataDeserialize,
+ PmuBaseDataFree));
#endif
- RegisterData("thread_collector", RegisterEntry(ThreadInfoSerialize, ThreadInfoDeserialize));
+ RegisterData("thread_collector", RegisterEntry(ThreadInfoSerialize, ThreadInfoDeserialize, ThreadInfoFree));
+
+ RegisterData("kernel_config", RegisterEntry(KernelDataSerialize, KernelDataDeserialize, KernelDataFree));
- RegisterData("kernel_config", RegisterEntry(KernelDataSerialize, KernelDataDeserialize));
+ RegisterData("thread_scenario", RegisterEntry(ThreadInfoSerialize, ThreadInfoDeserialize, ThreadInfoFree));
- RegisterData("thread_scenario", RegisterEntry(ThreadInfoSerialize, ThreadInfoDeserialize));
+ RegisterData("command_collector", RegisterEntry(CommandDataSerialize, CommandDataDeserialize, CommandDataFree));
- RegisterData("command_collector", RegisterEntry(CommandDataSerialize, CommandDataDeserialize));
- RegisterData("command_collector", RegisterEntry(CommandDataSerialize, CommandDataDeserialize));
RegisterData("analysis_aware", RegisterEntry(AnalysisDataSerialize, AnalysisDataDeserialize));
}
SerializeFunc Register::GetDataSerialize(const std::string &name)
{
- if (!deserializeFuncs.count(name)) {
+ if (!registerEntry.count(name)) {
return nullptr;
}
- return deserializeFuncs[name].se;
+ return registerEntry[name].se;
}
DeserializeFunc Register::GetDataDeserialize(const std::string &name)
{
- if (!deserializeFuncs.count(name)) {
+ if (!registerEntry.count(name)) {
+ return nullptr;
+ }
+ return registerEntry[name].de;
+}
+
+DataFreeFunc Register::GetDataFreeFunc(const std::string &name)
+{
+ if (!registerEntry.count(name)) {
return nullptr;
}
- return deserializeFuncs[name].de;
+ return registerEntry[name].free;
}
}
diff --git a/src/common/data_register.h b/src/common/data_register.h
index 5e1ae4a..b72cd97 100644
--- a/src/common/data_register.h
+++ b/src/common/data_register.h
@@ -13,18 +13,21 @@
#define COMMON_DATA_REGISTER_H
#include <unordered_map>
#include "serialize.h"
+#include "data_list.h"
namespace oeaware {
using DeserializeFunc = int(*)(void**, InStream &in);
using SerializeFunc = int(*)(const void*, OutStream &out);
-using FreeData = void(*)(void *);
+using DataFreeFunc = void(*)(void *);
struct RegisterEntry {
RegisterEntry() { }
RegisterEntry(const SerializeFunc &se, const DeserializeFunc &de) : se(se), de(de) { }
+ RegisterEntry(const SerializeFunc &se, const DeserializeFunc &de, const DataFreeFunc &free) : se(se),
+ de(de), free(free) { }
SerializeFunc se;
DeserializeFunc de;
- FreeData free;
+ DataFreeFunc free;
};
class Register {
@@ -40,17 +43,20 @@ public:
void InitRegisterData();
DeserializeFunc GetDataDeserialize(const std::string &name);
SerializeFunc GetDataSerialize(const std::string &name);
+ DataFreeFunc GetDataFreeFunc(const std::string &name);
void RegisterData(const std::string &name, const RegisterEntry &func);
private:
Register() { };
- std::unordered_map<std::string, RegisterEntry> deserializeFuncs;
+ std::unordered_map<std::string, RegisterEntry> registerEntry;
};
-
-int DataListSerialize(const void *data, OutStream &out);
-int DataListDeserialize(void *data, InStream &in);
+void DataListFree(DataList *dataList);
+int DataListSerialize(const DataList *dataList, OutStream &out);
+int DataListDeserialize(DataList *dataList, InStream &in);
int ResultDeserialize(void *data, InStream &in);
-int TopicSerialize(const void *topic, OutStream &out);
+int TopicSerialize(const CTopic *topic, OutStream &out);
+int TopicDeserialize(CTopic *topic, InStream &in);
+void TopicFree(CTopic *topic);
}
#endif
diff --git a/src/plugin/collect/include/command_data.h b/src/plugin/collect/include/command_data.h
index f466bd3..e7a8540 100644
--- a/src/plugin/collect/include/command_data.h
+++ b/src/plugin/collect/include/command_data.h
@@ -26,7 +26,7 @@ typedef struct {
int attrLen;
char *itemAttr[ATTR_MAX_LENGTH];
CommandIter *items;
-} SarData, CommandData;
+} CommandData;
#ifdef __cplusplus
}
#endif
diff --git a/src/plugin_mgr/event/unsubscribe_handler.cpp b/src/plugin_mgr/event/unsubscribe_handler.cpp
index 72b53bf..749a683 100644
--- a/src/plugin_mgr/event/unsubscribe_handler.cpp
+++ b/src/plugin_mgr/event/unsubscribe_handler.cpp
@@ -23,7 +23,12 @@ EventResult UnsubscribeHandler::Handle(const Event &event)
INFO(logger, "sdk " << event.payload[0] << " disconnected and has been unsubscribed related topics.");
return eventResult;
}
- auto msg = std::make_shared<InstanceRunMessage>(RunType::UNSUBSCRIBE, event.payload);
+ CTopic cTopic;
+ InStream in(event.payload[0]);
+ TopicDeserialize(&cTopic, in);
+ Topic topic{cTopic.instanceName, cTopic.topicName, cTopic.params};
+ auto msg = std::make_shared<InstanceRunMessage>(RunType::UNSUBSCRIBE,
+ std::vector<std::string>{topic.GetType(), event.payload[1]});
instanceRunHandler->RecvQueuePush(msg);
msg->Wait();
result = msg->result;
diff --git a/src/plugin_mgr/instance_run_handler.cpp b/src/plugin_mgr/instance_run_handler.cpp
index 2e11d0d..30dc886 100644
--- a/src/plugin_mgr/instance_run_handler.cpp
+++ b/src/plugin_mgr/instance_run_handler.cpp
@@ -181,7 +181,7 @@ void InstanceRunHandler::PublishData(std::shared_ptr<InstanceRunMessage> &msg)
auto instance = memoryStore->GetInstance(subscriber);
instance->interface->UpdateData(msg->dataList);
}
- // free dataList
+ DataListFree(&msg->dataList);
}
bool InstanceRunHandler::HandleMessage()
diff --git a/src/sdk/oe_client.cpp b/src/sdk/oe_client.cpp
index 4eb04ae..9452a37 100644
--- a/src/sdk/oe_client.cpp
+++ b/src/sdk/oe_client.cpp
@@ -70,6 +70,7 @@ void Impl::HandleRecv()
for (auto handle : topicHandle[key]) {
handle(&dataList);
}
+ DataListFree(&dataList);
break;
}
default:
--
2.33.0

View File

@ -1,412 +0,0 @@
From 51355e66be3eeec6ebe79faa88324c788e5a3829 Mon Sep 17 00:00:00 2001
From: fly_1997 <flylove7@outlook.com>
Date: Wed, 27 Nov 2024 05:13:26 +0800
Subject: [PATCH 3/5] fix failed to connect to the sdk and command execution is
stuck
---
src/common/utils.cpp | 16 ++++
src/common/utils.h | 1 +
.../collect/system/command/command_base.cpp | 48 +++++++++++-
.../collect/system/command/command_base.h | 9 +++
.../system/command/command_collector.cpp | 9 ++-
src/plugin/collect/system/kernel_config.cpp | 1 +
src/plugin_mgr/config.cpp | 17 -----
src/plugin_mgr/config.h | 1 -
src/plugin_mgr/message_manager.cpp | 76 +++++++++++++++++--
src/plugin_mgr/message_manager.h | 3 +-
src/sdk/oe_client.cpp | 12 ++-
11 files changed, 160 insertions(+), 33 deletions(-)
diff --git a/src/common/utils.cpp b/src/common/utils.cpp
index 4e12a57..a300a6a 100644
--- a/src/common/utils.cpp
+++ b/src/common/utils.cpp
@@ -156,4 +156,20 @@ std::vector<std::string> SplitString(const std::string &str, const std::string &
}
return tokens;
}
+bool CreateDir(const std::string &path)
+{
+ size_t pos = 0;
+ do {
+ pos = path.find_first_of("/", pos + 1);
+ std::string subPath = path.substr(0, pos);
+ struct stat buffer;
+ if (stat(subPath.c_str(), &buffer) == 0) {
+ continue;
+ }
+ if (mkdir(subPath.c_str(), S_IRWXU | S_IRWXG) != 0) {
+ return false;
+ }
+ } while (pos != std::string::npos);
+ return true;
+}
}
diff --git a/src/common/utils.h b/src/common/utils.h
index ba787fe..48b72bd 100644
--- a/src/common/utils.h
+++ b/src/common/utils.h
@@ -28,6 +28,7 @@ bool EndWith(const std::string &s, const std::string &ending);
std::string Concat(const std::vector<std::string>& strings, const std::string &split);
// Separate "str" with the separator "split"
std::vector<std::string> SplitString(const std::string &str, const std::string &split);
+bool CreateDir(const std::string &path);
}
#endif // !COMMON_UTILS_H
\ No newline at end of file
diff --git a/src/plugin/collect/system/command/command_base.cpp b/src/plugin/collect/system/command/command_base.cpp
index d5a30dd..bf658b8 100644
--- a/src/plugin/collect/system/command/command_base.cpp
+++ b/src/plugin/collect/system/command/command_base.cpp
@@ -10,6 +10,45 @@
* See the Mulan PSL v2 for more details.
******************************************************************************/
#include "command_base.h"
+#include <unistd.h>
+#include <sys/wait.h>
+
+int PopenProcess::Pclose()
+{
+ if (fclose(stream) == EOF) {
+ return -1;
+ }
+ stream = nullptr;
+ if (kill(pid, SIGTERM) == -1) {
+ return -1;
+ }
+ return 0;
+}
+
+void PopenProcess::Popen(const std::string &cmd)
+{
+ int pipeFd[2];
+ if (pipe(pipeFd) == -1) {
+ return;
+ }
+ pid = fork();
+ if (pid == -1) {
+ close(pipeFd[0]);
+ close(pipeFd[1]);
+ } else if (pid == 0) {
+ close(pipeFd[0]);
+ dup2(pipeFd[1], STDOUT_FILENO);
+ close(pipeFd[1]);
+ execl("/bin/bash", "bash", "-c", cmd.data(), nullptr);
+ _exit(1);
+ }
+ close(pipeFd[1]);
+ stream = fdopen(pipeFd[0], "r");
+ if (!stream) {
+ close(pipeFd[0]);
+ return;
+ }
+}
CommandBase::CommandBase()
{
@@ -23,18 +62,19 @@ CommandBase::CommandBase()
bool CommandBase::ValidateArgs(const oeaware::Topic& topic)
{
auto cmd = GetCommand(topic);
- FILE *pipe = popen(cmd.c_str(), "r");
- if (!pipe) {
+ PopenProcess p;
+ p.Popen(cmd);
+ if (!p.stream) {
return false;
}
char buffer[128];
bool isValid = false;
- if (fgets(buffer, sizeof(buffer), pipe) != nullptr) {
+ if (fgets(buffer, sizeof(buffer), p.stream) != nullptr) {
if (strstr(buffer, "Linux") != nullptr || strstr(buffer, "procs") != nullptr) {
isValid = true;
}
}
- pclose(pipe);
+ p.Pclose();
return isValid;
}
diff --git a/src/plugin/collect/system/command/command_base.h b/src/plugin/collect/system/command/command_base.h
index eb75c49..a6d7627 100644
--- a/src/plugin/collect/system/command/command_base.h
+++ b/src/plugin/collect/system/command/command_base.h
@@ -44,4 +44,13 @@ public:
void Close();
};
+class PopenProcess {
+public:
+ int Pclose();
+ void Popen(const std::string &cmd);
+
+ FILE *stream;
+ pid_t pid;
+};
+
#endif
diff --git a/src/plugin/collect/system/command/command_collector.cpp b/src/plugin/collect/system/command/command_collector.cpp
index 6c54555..640a406 100644
--- a/src/plugin/collect/system/command/command_collector.cpp
+++ b/src/plugin/collect/system/command/command_collector.cpp
@@ -32,15 +32,16 @@ CommandCollector::CommandCollector(): oeaware::Interface()
void CommandCollector::CollectThread(const oeaware::Topic &topic, CommandBase* collector)
{
std::string cmd = collector->GetCommand(topic);
- FILE* pipe = popen(cmd.c_str(), "r");
- if (!pipe) {
+ PopenProcess p;
+ p.Popen(cmd);
+ if (!p.stream) {
return;
}
char buffer[256];
- while (collector->isRunning && fgets(buffer, sizeof(buffer), pipe) != nullptr) {
+ while (collector->isRunning && fgets(buffer, sizeof(buffer), p.stream) != nullptr) {
collector->ParseLine(std::string(buffer));
}
- pclose(pipe);
+ p.Pclose();
collector->Close();
}
diff --git a/src/plugin/collect/system/kernel_config.cpp b/src/plugin/collect/system/kernel_config.cpp
index 63aafea..6bdfc8a 100644
--- a/src/plugin/collect/system/kernel_config.cpp
+++ b/src/plugin/collect/system/kernel_config.cpp
@@ -113,6 +113,7 @@ static bool IsSymlink(const std::string &path)
}
return S_ISLNK(st.st_mode);
}
+
void KernelConfig::GetAllEth()
{
const std::string path = "/sys/class/net";
diff --git a/src/plugin_mgr/config.cpp b/src/plugin_mgr/config.cpp
index 341880b..8cd1432 100644
--- a/src/plugin_mgr/config.cpp
+++ b/src/plugin_mgr/config.cpp
@@ -15,23 +15,6 @@
#include "default_path.h"
namespace oeaware {
-bool CreateDir(const std::string &path)
-{
- size_t pos = 0;
- do {
- pos = path.find_first_of("/", pos + 1);
- std::string subPath = path.substr(0, pos);
- struct stat buffer;
- if (stat(subPath.c_str(), &buffer) == 0) {
- continue;
- }
- if (mkdir(subPath.c_str(), S_IRWXU | S_IRWXG) != 0) {
- return false;
- }
- } while (pos != std::string::npos);
- return true;
-}
-
bool CheckPluginList(YAML::Node pluginListItem)
{
if (pluginListItem["name"].IsNull()) {
diff --git a/src/plugin_mgr/config.h b/src/plugin_mgr/config.h
index 640ab00..19e6d33 100644
--- a/src/plugin_mgr/config.h
+++ b/src/plugin_mgr/config.h
@@ -130,7 +130,6 @@ private:
};
std::string GetPath();
-bool CreateDir(const std::string &path);
}
#endif
diff --git a/src/plugin_mgr/message_manager.cpp b/src/plugin_mgr/message_manager.cpp
index 1ffa4cc..bfa5b0c 100644
--- a/src/plugin_mgr/message_manager.cpp
+++ b/src/plugin_mgr/message_manager.cpp
@@ -11,6 +11,7 @@
******************************************************************************/
#include "message_manager.h"
#include <thread>
+#include <pwd.h>
#include <securec.h>
#include "default_path.h"
#include "utils.h"
@@ -41,15 +42,56 @@ int Epoll::EventWait(struct epoll_event *events, int maxEvents, int timeout)
return epoll_wait(epfd, events, maxEvents, timeout);
}
+static std::vector<std::string> GetUserFromGroup(const std::string &groupName)
+{
+ std::vector<std::string> users;
+ std::ifstream file("/etc/group");
+ if (!file.is_open()) {
+ return users;
+ }
+ std::string line;
+ size_t userPartIndex = 3;
+ while (std::getline(file, line)) {
+ std::vector<std::string> parts = SplitString(line, ":");
+ if (parts.size() > userPartIndex && parts[0] == groupName) {
+ std::vector<std::string> userParts = SplitString(parts[userPartIndex], ",");
+ users.insert(users.end(), userParts.begin(), userParts.end());
+ break;
+ }
+ }
+ file.close();
+ return users;
+}
+
+static int GetUid(const std::string &name)
+{
+ struct passwd pwd;
+ struct passwd *result;
+ char buf[1024];
+ int res = getpwnam_r(name.c_str(), &pwd, buf, sizeof(buf), &result);
+ if (res != 0 || result == nullptr) {
+ return -1;
+ }
+ return pwd.pw_uid;
+}
+
void TcpSocket::InitGroups()
{
std::vector<std::string> groupNames{"oeaware", "root"};
+ groups[0].emplace_back(0);
for (auto &groupName : groupNames) {
auto gid = GetGidByGroupName(groupName);
if (gid < 0) {
continue;
}
- groups.emplace_back(gid);
+ auto users = GetUserFromGroup(groupName);
+ for (auto &user : users) {
+ auto uid = GetUid(user);
+ if (uid < 0) {
+ continue;
+ }
+ groups[gid].emplace_back(uid);
+ }
}
}
@@ -68,6 +110,11 @@ bool TcpSocket::StartListen()
ERROR(logger, path << " chmod error!");
return false;
}
+ std::string cmd = "setfacl -m g:oeaware:rw " + path;
+ auto ret = system(cmd.c_str());
+ if (ret) {
+ WARN(logger, "failed to set the communication permission of the oeaware user group.");
+ }
if (domainSocket->Listen() < 0) {
ERROR(logger, "listen error!");
return false;
@@ -136,7 +183,6 @@ static void GetEventResult(Message &msg, EventResultQueue sendMessage)
}
const int DISCONNECTED = -1;
-const int DISCONNECTED_AND_UNSUBCRIBE = -2;
void TcpMessageHandler::Init(EventQueue newRecvMessage, EventResultQueue newSendMessage, EventQueue newRecvData)
{
@@ -240,6 +286,23 @@ void TcpMessageHandler::Start()
}
}
+bool TcpSocket::CheckFileGroups(const std::string &path)
+{
+ struct stat st;
+ if (lstat(path.c_str(), &st) < 0) {
+ return false;
+ }
+ for (auto &p : groups) {
+ bool ok = std::any_of(p.second.begin(), p.second.end(), [&](uid_t uid) {
+ return uid == st.st_uid;
+ });
+ if (ok) {
+ return true;
+ }
+ }
+ return false;
+}
+
void TcpSocket::SaveConnection()
{
struct sockaddr_un un;
@@ -255,12 +318,12 @@ void TcpSocket::SaveConnection()
memcpy_s(name, maxNameLength, un.sun_path, len);
name[len] = 0;
bool isSdk = false;
- if (strcmp(name, DEFAULT_SDK_CONN_PATH.c_str()) == 0) {
+ if (len > 0) {
isSdk = true;
}
// check permission
- if (isSdk && !CheckFileGroups(DEFAULT_SDK_CONN_PATH, groups)) {
- WARN(logger, "sdk permission error");
+ if (isSdk && !CheckFileGroups(name)) {
+ WARN(logger, "sdk permission error, " << name);
return;
}
if (!epoll->EventCtl(EPOLL_CTL_ADD, conn)) {
@@ -274,6 +337,9 @@ void TcpSocket::SaveConnection()
type |= CMD_CONN;
}
tcpMessageHandler.AddConn(conn, type);
+ if (isSdk) {
+ INFO(logger, "a sdk connection is established, " << name);
+ }
DEBUG(logger, "client connected!");
}
diff --git a/src/plugin_mgr/message_manager.h b/src/plugin_mgr/message_manager.h
index 38f544d..3c67096 100644
--- a/src/plugin_mgr/message_manager.h
+++ b/src/plugin_mgr/message_manager.h
@@ -73,12 +73,13 @@ private:
bool StartListen();
void SaveConnection();
void HandleEvents(struct epoll_event *events, int num);
+ bool CheckFileGroups(const std::string &path);
private:
log4cplus::Logger logger;
std::unique_ptr<DomainSocket> domainSocket;
std::unique_ptr<Epoll> epoll;
TcpMessageHandler tcpMessageHandler;
- std::vector<gid_t> groups;
+ std::unordered_map<int, std::vector<uid_t>> groups;
const int maxRequestNum = 20;
const int maxNameLength = 108;
};
diff --git a/src/sdk/oe_client.cpp b/src/sdk/oe_client.cpp
index 9452a37..040433a 100644
--- a/src/sdk/oe_client.cpp
+++ b/src/sdk/oe_client.cpp
@@ -80,7 +80,17 @@ void Impl::HandleRecv()
}
int Impl::Init()
{
- domainSocket = std::make_shared<DomainSocket>(DEFAULT_SDK_CONN_PATH);
+ auto home = getenv("HOME");
+ std::string homeDir;
+ if (home == nullptr) {
+ homeDir = "/var/run/oeAware";
+ } else {
+ homeDir = home;
+ homeDir += "/.oeaware";
+ }
+
+ CreateDir(homeDir);
+ domainSocket = std::make_shared<DomainSocket>(homeDir + "/oeaware-sdk.sock");
domainSocket->SetRemotePath(DEFAULT_SERVER_LISTEN_PATH);
resultQueue = std::make_shared<SafeQueue<Result>>();
int sock = domainSocket->Socket();
--
2.33.0

View File

@ -1,204 +0,0 @@
From eff9e77d5a695f2ef800c54206dbe0ac11dc0272 Mon Sep 17 00:00:00 2001
From: fly_1997 <flylove7@outlook.com>
Date: Wed, 27 Nov 2024 10:18:57 +0800
Subject: [PATCH 4/5] add command verification
---
src/plugin/collect/system/CMakeLists.txt | 2 +-
.../collect/system/command/command_base.cpp | 29 ++++++++++---------
.../collect/system/command/command_base.h | 3 ++
.../system/command/command_collector.h | 3 +-
src/plugin/collect/system/kernel_config.cpp | 9 ++++++
src/plugin/collect/system/kernel_config.h | 1 +
src/plugin_mgr/event/subscribe_handler.cpp | 10 ++-----
src/plugin_mgr/instance_run_handler.cpp | 3 +-
8 files changed, 36 insertions(+), 24 deletions(-)
diff --git a/src/plugin/collect/system/CMakeLists.txt b/src/plugin/collect/system/CMakeLists.txt
index ee6044f..af4a239 100644
--- a/src/plugin/collect/system/CMakeLists.txt
+++ b/src/plugin/collect/system/CMakeLists.txt
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.11)
project(system_collector)
-include_directories(../include)
+include_directories(command)
add_compile_options(-O2 -fPIC -Wall -Wextra)
add_library(system_collector SHARED
thread_collector.cpp
diff --git a/src/plugin/collect/system/command/command_base.cpp b/src/plugin/collect/system/command/command_base.cpp
index bf658b8..e6c0a83 100644
--- a/src/plugin/collect/system/command/command_base.cpp
+++ b/src/plugin/collect/system/command/command_base.cpp
@@ -10,6 +10,7 @@
* See the Mulan PSL v2 for more details.
******************************************************************************/
#include "command_base.h"
+#include <algorithm>
#include <unistd.h>
#include <sys/wait.h>
@@ -59,23 +60,25 @@ CommandBase::CommandBase()
attrsFirst["vmstat"] = {"swpd"};
}
+std::vector<std::string> CommandBase::command{"mpstat", "iostat", "vmstat", "sar", "pidstat"};
+std::vector<std::string> CommandBase::illegal{"|", ";", "&", "$", ">", "<", "`", "\n"};
+
+bool CommandBase::ValidateCmd(const std::string &cmd)
+{
+ for (auto word : illegal) {
+ if (strstr(cmd.c_str(), word.c_str())) {
+ return false;
+ }
+ }
+ return true;
+}
+
bool CommandBase::ValidateArgs(const oeaware::Topic& topic)
{
- auto cmd = GetCommand(topic);
- PopenProcess p;
- p.Popen(cmd);
- if (!p.stream) {
+ if (std::find(command.begin(), command.end(), topic.topicName) == command.end()) {
return false;
}
- char buffer[128];
- bool isValid = false;
- if (fgets(buffer, sizeof(buffer), p.stream) != nullptr) {
- if (strstr(buffer, "Linux") != nullptr || strstr(buffer, "procs") != nullptr) {
- isValid = true;
- }
- }
- p.Pclose();
- return isValid;
+ return ValidateCmd(topic.params);
}
void CommandBase::ParseLine(const std::string& line)
diff --git a/src/plugin/collect/system/command/command_base.h b/src/plugin/collect/system/command/command_base.h
index a6d7627..ef9bd7c 100644
--- a/src/plugin/collect/system/command/command_base.h
+++ b/src/plugin/collect/system/command/command_base.h
@@ -35,9 +35,12 @@ public:
oeaware::Topic topic;
std::unordered_map<std::string, std::vector<std::string>> attrsFirst;
std::vector<std::string> skipLine{"---swap--"};
+ static std::vector<std::string> command;
+ static std::vector<std::string> illegal;
CommandBase();
virtual ~CommandBase() = default;
static bool ValidateArgs(const oeaware::Topic& topic);
+ static bool ValidateCmd(const std::string &cmd);
void ParseLine(const std::string& line);
static std::string GetCommand(const oeaware::Topic& topic);
bool FillDataStruct(void* dataStruct);
diff --git a/src/plugin/collect/system/command/command_collector.h b/src/plugin/collect/system/command/command_collector.h
index 26fc7e7..72553e3 100644
--- a/src/plugin/collect/system/command/command_collector.h
+++ b/src/plugin/collect/system/command/command_collector.h
@@ -27,8 +27,7 @@ public:
void Disable() override;
void Run() override;
private:
- std::vector<std::string> topicStr = {"mpstat", "iostat", "vmstat", "sar", "pidstat", "lscpu", "zone_reclaim_mode",
- "meminfo", "ethtool", "ifconfig", "os-release", "version"};
+ std::vector<std::string> topicStr = {"mpstat", "iostat", "vmstat", "sar", "pidstat"};
std::unordered_map<std::string, std::unique_ptr<CommandBase>> collectors;
std::unordered_map<std::string, std::thread> collectThreads;
std::unordered_map<std::string, std::thread> publishThreads;
diff --git a/src/plugin/collect/system/kernel_config.cpp b/src/plugin/collect/system/kernel_config.cpp
index 6bdfc8a..3d25251 100644
--- a/src/plugin/collect/system/kernel_config.cpp
+++ b/src/plugin/collect/system/kernel_config.cpp
@@ -17,6 +17,7 @@
#include <securec.h>
#include <dirent.h>
#include <sys/stat.h>
+#include "command_base.h"
KernelConfig::KernelConfig(): oeaware::Interface()
{
@@ -258,12 +259,20 @@ void KernelConfig::WriteSysParam(const std::string &path, const std::string &val
INFO(logger, "successfully wrote value{" << value <<"} to " << path << ".");
}
+std::vector<std::string> KernelConfig::cmdGroup{"sysctl", "ifconfig", "/sbin/blockdev"};
+
void KernelConfig::SetKernelConfig()
{
for (auto &p : setSystemParams) {
WriteSysParam(p.first, p.second);
}
for (auto &cmd : cmdRun) {
+ auto cmdParts = oeaware::SplitString(cmd, " ");
+ if (cmdParts.empty() || std::find(cmdGroup.begin(), cmdGroup.end(), cmdParts[0]) == cmdGroup.end() ||
+ !CommandBase::ValidateCmd(cmd)) {
+ WARN(logger, "cmd{" << cmd << "} invalid.");
+ continue;
+ }
FILE *pipe = popen(cmd.data(), "r");
if (!pipe) {
WARN(logger, "{" << cmd << "} run failed.");
diff --git a/src/plugin/collect/system/kernel_config.h b/src/plugin/collect/system/kernel_config.h
index aa96886..32049d4 100644
--- a/src/plugin/collect/system/kernel_config.h
+++ b/src/plugin/collect/system/kernel_config.h
@@ -62,6 +62,7 @@ private:
std::unordered_map<std::string, std::string> kernelParams;
std::vector<std::string> cmdRun;
+ static std::vector<std::string> cmdGroup;
std::vector<std::string> allEths;
};
diff --git a/src/plugin_mgr/event/subscribe_handler.cpp b/src/plugin_mgr/event/subscribe_handler.cpp
index 8697958..f29b455 100644
--- a/src/plugin_mgr/event/subscribe_handler.cpp
+++ b/src/plugin_mgr/event/subscribe_handler.cpp
@@ -14,24 +14,20 @@
namespace oeaware {
Result SubscribeHandler::Subscribe(const std::string &name, const Topic &topic)
{
- Result result;
if (!memoryStore->IsInstanceExist(topic.instanceName)) {
WARN(logger, "The subscribed instance " << topic.instanceName << " does not exist.");
- result.code = -1;
- return result;
+ return Result(FAILED, "instance does not exist.");
}
auto instance = memoryStore->GetInstance(topic.instanceName);
if (!instance->supportTopics.count(topic.topicName)) {
WARN(logger, "The subscribed topic " << topic.topicName << " does not exist.");
- result.code = -1;
- return result;
+ return Result(FAILED, "topic does not exist.");
}
auto msg = std::make_shared<InstanceRunMessage>(RunType::SUBSCRIBE,
std::vector<std::string>{topic.GetType(), name});
instanceRunHandler->RecvQueuePush(msg);
msg->Wait();
- result = msg->result;
- return result;
+ return msg->result;
}
EventResult SubscribeHandler::Handle(const Event &event)
diff --git a/src/plugin_mgr/instance_run_handler.cpp b/src/plugin_mgr/instance_run_handler.cpp
index 30dc886..abee581 100644
--- a/src/plugin_mgr/instance_run_handler.cpp
+++ b/src/plugin_mgr/instance_run_handler.cpp
@@ -61,7 +61,8 @@ Result InstanceRunHandler::Subscribe(const std::vector<std::string> &payload)
if (!topicState[topic.instanceName][topic.topicName][topic.params]) {
result = instance->interface->OpenTopic(topic);
if (result.code < 0) {
- WARN(logger, "topic open failed, " << result.payload);
+ WARN(logger, "topic{" << LogText(topic.instanceName) << ", " << LogText(topic.topicName) << ", " <<
+ LogText(topic.params) << "} open failed, " << result.payload);
DisableInstance(instance->name);
return result;
}
--
2.33.0

View File

@ -1,233 +0,0 @@
From 1a84134fbe63c1a7bea679b5696bafd99a1b6666 Mon Sep 17 00:00:00 2001
From: fly_1997 <flylove7@outlook.com>
Date: Sat, 30 Nov 2024 15:25:03 +0800
Subject: [PATCH 5/5] modify C interface name, add enable count and fix bugs
---
src/client/cmd_handler.cpp | 7 ++++---
src/client/main.cpp | 4 ++--
src/plugin_mgr/config.cpp | 5 +++++
src/plugin_mgr/config.h | 1 +
src/plugin_mgr/event/enable_handler.cpp | 3 +++
src/plugin_mgr/instance_run_handler.cpp | 3 ++-
src/plugin_mgr/plugin.cpp | 2 +-
src/plugin_mgr/plugin.h | 1 +
src/sdk/oe_client.cpp | 14 +++++++++-----
src/sdk/oe_client.h | 10 +++++-----
10 files changed, 33 insertions(+), 17 deletions(-)
diff --git a/src/client/cmd_handler.cpp b/src/client/cmd_handler.cpp
index b1e2ba0..289461b 100644
--- a/src/client/cmd_handler.cpp
+++ b/src/client/cmd_handler.cpp
@@ -33,7 +33,7 @@ void LoadHandler::Handler(Message &msg)
void LoadHandler::ResHandler(Message &msg)
{
if (msg.opt == Opt::RESPONSE_OK) {
- std::cout << "Plugin loaded successfully.";
+ std::cout << "Plugin loaded successfully.\n";
} else {
std::cout << "Plugin loaded failed, because "<< msg.payload[0] << ".\n";
}
@@ -54,9 +54,10 @@ void QueryHandler::PrintFormat()
{
std::cout << "format:\n"
"[plugin]\n"
- "\t[instance]([dependency status], [running status])\n"
+ "\t[instance]([dependency status], [running status], [enable cnt])\n"
"dependency status: available means satisfying dependency, otherwise unavailable.\n"
- "running status: running means that instance is running, otherwise close.\n";
+ "running status: running means that instance is running, otherwise close.\n"
+ "enable cnt: number of instances enabled.\n";
}
void QueryHandler::ResHandler(Message &msg)
diff --git a/src/client/main.cpp b/src/client/main.cpp
index 188d5e2..1c50f1a 100644
--- a/src/client/main.cpp
+++ b/src/client/main.cpp
@@ -41,8 +41,8 @@ int main(int argc, char *argv[])
std::string analysis = argv[1];
if (analysis == "analysis") {
CTopic topic = {"analysis_aware", "analysis_aware", ""};
- Init();
- Subscribe(&topic, AnalysisCallback);
+ OeInit();
+ OeSubscribe(&topic, AnalysisCallback);
std::unique_lock<std::mutex> lock(g_mutex);
g_cv.wait(lock, []{ return g_finish; });
return 0;
diff --git a/src/plugin_mgr/config.cpp b/src/plugin_mgr/config.cpp
index 8cd1432..aee3243 100644
--- a/src/plugin_mgr/config.cpp
+++ b/src/plugin_mgr/config.cpp
@@ -61,6 +61,10 @@ void Config::SetEnableList(const YAML::Node &node)
std::string pluginName = enableList[i]["name"].as<std::string>();
YAML::Node instances = enableList[i]["instances"];
EnableItem enableItem(pluginName);
+ if (!instances.IsSequence()) {
+ WARN(logger, "the format of the enable list is incorrect.");
+ continue;
+ }
if (!instances.IsDefined() || instances.IsNull()) {
enableItem.SetEnabled(true);
} else {
@@ -75,6 +79,7 @@ void Config::SetEnableList(const YAML::Node &node)
bool Config::Load(const std::string &path)
{
+ logger = Logger::GetInstance().Get("Main");
YAML::Node node;
struct stat buffer;
if (stat(path.c_str(), &buffer) != 0) {
diff --git a/src/plugin_mgr/config.h b/src/plugin_mgr/config.h
index 19e6d33..dc02b37 100644
--- a/src/plugin_mgr/config.h
+++ b/src/plugin_mgr/config.h
@@ -127,6 +127,7 @@ private:
std::string logType;
std::unordered_map<std::string, PluginInfo> pluginList;
std::vector<EnableItem> enableList;
+ log4cplus::Logger logger;
};
std::string GetPath();
diff --git a/src/plugin_mgr/event/enable_handler.cpp b/src/plugin_mgr/event/enable_handler.cpp
index 66eba6c..a5d8e1a 100644
--- a/src/plugin_mgr/event/enable_handler.cpp
+++ b/src/plugin_mgr/event/enable_handler.cpp
@@ -29,6 +29,9 @@ ErrorCode EnableHandler::InstanceEnabled(const std::string &name)
instanceRunHandler->RecvQueuePush(msg);
/* Wait for InstanceRunHandler to finsh this task. */
msg->Wait();
+ if (msg->result.code < 0) {
+ return ErrorCode::ENABLE_INSTANCE_ENV;
+ }
return ErrorCode::OK;
}
diff --git a/src/plugin_mgr/instance_run_handler.cpp b/src/plugin_mgr/instance_run_handler.cpp
index abee581..aa428ff 100644
--- a/src/plugin_mgr/instance_run_handler.cpp
+++ b/src/plugin_mgr/instance_run_handler.cpp
@@ -26,6 +26,7 @@ Result InstanceRunHandler::EnableInstance(const std::string &name)
return result;
}
instance->enabled = true;
+ instance->enableCnt++;
if (instance->interface->GetType() & SCENARIO) {
scheduleQueue.push(ScheduleInstance{instance, time + instance->interface->GetPeriod()});
} else if (instance->interface->GetType() & TUNE) {
@@ -196,7 +197,7 @@ bool InstanceRunHandler::HandleMessage()
DEBUG(logger, "handle message " << (int)msg->GetType());
switch (msg->GetType()) {
case RunType::ENABLED: {
- EnableInstance(msg->payload[0]);
+ msg->result = EnableInstance(msg->payload[0]);
break;
}
case RunType::DISABLED: {
diff --git a/src/plugin_mgr/plugin.cpp b/src/plugin_mgr/plugin.cpp
index 5789737..8b894b6 100644
--- a/src/plugin_mgr/plugin.cpp
+++ b/src/plugin_mgr/plugin.cpp
@@ -72,6 +72,6 @@ std::string Instance::GetInfo() const
{
std::string stateText = this->state ? pluginStateOn : pluginStateOff;
std::string runText = this->enabled ? pluginEnabled : pluginDisabled;
- return name + "(" + stateText + ", " + runText + ")";
+ return name + "(" + stateText + ", " + runText + ", count: " + std::to_string(enableCnt) + ")";
}
}
diff --git a/src/plugin_mgr/plugin.h b/src/plugin_mgr/plugin.h
index ef5fc1d..bf63852 100644
--- a/src/plugin_mgr/plugin.h
+++ b/src/plugin_mgr/plugin.h
@@ -23,6 +23,7 @@ struct Instance {
std::string pluginName;
bool state = true;
bool enabled;
+ uint64_t enableCnt = 0;
std::shared_ptr<Interface> interface;
std::unordered_map<std::string, Topic> supportTopics;
const static std::string pluginEnabled;
diff --git a/src/sdk/oe_client.cpp b/src/sdk/oe_client.cpp
index 040433a..a9f82ef 100644
--- a/src/sdk/oe_client.cpp
+++ b/src/sdk/oe_client.cpp
@@ -22,6 +22,7 @@
namespace oeaware {
class Impl {
public:
+ Impl() noexcept : domainSocket(nullptr), socketStream(nullptr) { }
int Init();
int Subscribe(const CTopic &topic, Callback callback);
int Unsubscribe(const CTopic &topic);
@@ -114,6 +115,9 @@ int Impl::Init()
int Impl::HandleRequest(const Opt &opt, const std::vector<std::string> &payload)
{
MessageProtocol protocol(MessageHeader(MessageType::REQUEST), Message(opt, payload));
+ if (socketStream == nullptr) {
+ return -1;
+ }
SendMessage(*socketStream, protocol);
Result result;
if (!resultQueue->WaitTimeAndPop(result)) {
@@ -166,28 +170,28 @@ void Impl::Close()
static oeaware::Impl impl;
-int Init()
+int OeInit()
{
oeaware::Register::GetInstance().InitRegisterData();
return impl.Init();
}
-int Subscribe(const CTopic *topic, Callback callback)
+int OeSubscribe(const CTopic *topic, Callback callback)
{
return impl.Subscribe(*topic, callback);
}
-int Unsubscribe(const CTopic *topic)
+int OeUnsubscribe(const CTopic *topic)
{
return impl.Unsubscribe(*topic);
}
-int Publish(const DataList *dataList)
+int OePublish(const DataList *dataList)
{
return impl.Publish(*dataList);
}
-void Close()
+void OeClose()
{
impl.Close();
}
diff --git a/src/sdk/oe_client.h b/src/sdk/oe_client.h
index 17d8e68..267462c 100644
--- a/src/sdk/oe_client.h
+++ b/src/sdk/oe_client.h
@@ -16,11 +16,11 @@
extern "C" {
#endif
typedef int(*Callback)(const DataList *);
-int Init();
-int Subscribe(const CTopic *topic, Callback callback);
-int Unsubscribe(const CTopic *topic);
-int Publish(const DataList *dataList);
-void Close();
+int OeInit();
+int OeSubscribe(const CTopic *topic, Callback callback);
+int OeUnsubscribe(const CTopic *topic);
+int OePublish(const DataList *dataList);
+void OeClose();
#ifdef __cplusplus
}
#endif
--
2.33.0

View File

@ -1,510 +0,0 @@
From 55a03b46318e4c977b7e84508c998775e9db34ba Mon Sep 17 00:00:00 2001
From: fly_1997 <flylove7@outlook.com>
Date: Tue, 3 Dec 2024 09:13:09 +0800
Subject: [PATCH 1/4] add one-time command collection
---
src/common/utils.cpp | 29 +++
src/common/utils.h | 3 +
src/plugin/collect/system/CMakeLists.txt | 2 +
.../collect/system/command/command_base.h | 2 +-
.../system/command/command_collector.cpp | 5 +-
src/plugin/collect/system/kernel_config.cpp | 223 ++++++++++--------
src/plugin/collect/system/kernel_config.h | 25 +-
tests/CMakeLists.txt | 2 +-
8 files changed, 175 insertions(+), 116 deletions(-)
diff --git a/src/common/utils.cpp b/src/common/utils.cpp
index a300a6a..ddd5235 100644
--- a/src/common/utils.cpp
+++ b/src/common/utils.cpp
@@ -13,6 +13,7 @@
#include <algorithm>
#include <fstream>
#include <regex>
+#include <securec.h>
#include <curl/curl.h>
#include <sys/stat.h>
#include <grp.h>
@@ -172,4 +173,32 @@ bool CreateDir(const std::string &path)
} while (pos != std::string::npos);
return true;
}
+
+bool SetDataListTopic(DataList *dataList, const std::string &instanceName, const std::string &topicName,
+ const std::string &params)
+{
+ dataList->topic.instanceName = new char[instanceName.size() + 1];
+ if (dataList->topic.instanceName == nullptr) {
+ return false;
+ }
+ strcpy_s(dataList->topic.instanceName, instanceName.size() + 1, instanceName.data());
+ dataList->topic.topicName = new char[topicName.size() + 1];
+ if (dataList->topic.topicName == nullptr) {
+ delete[] dataList->topic.instanceName;
+ dataList->topic.instanceName = nullptr;
+ return false;
+ }
+ strcpy_s(dataList->topic.topicName, topicName.size() + 1, topicName.data());
+ dataList->topic.params = new char[params.size() + 1];
+ if (dataList->topic.params == nullptr) {
+ delete[] dataList->topic.instanceName;
+ delete[] dataList->topic.topicName;
+ dataList->topic.instanceName = nullptr;
+ dataList->topic.topicName = nullptr;
+ return false;
+ }
+ strcpy_s(dataList->topic.params, params.size() + 1, params.data());
+ return true;
+}
+
}
diff --git a/src/common/utils.h b/src/common/utils.h
index 48b72bd..ceadf36 100644
--- a/src/common/utils.h
+++ b/src/common/utils.h
@@ -13,6 +13,7 @@
#define COMMON_UTILS_H
#include <string>
#include <vector>
+#include "data_list.h"
namespace oeaware {
bool Download(const std::string &url, const std::string &path);
@@ -29,6 +30,8 @@ std::string Concat(const std::vector<std::string>& strings, const std::string &s
// Separate "str" with the separator "split"
std::vector<std::string> SplitString(const std::string &str, const std::string &split);
bool CreateDir(const std::string &path);
+bool SetDataListTopic(DataList *dataList, const std::string &instanceName, const std::string &topicName,
+ const std::string &params);
}
#endif // !COMMON_UTILS_H
\ No newline at end of file
diff --git a/src/plugin/collect/system/CMakeLists.txt b/src/plugin/collect/system/CMakeLists.txt
index af4a239..b7d2666 100644
--- a/src/plugin/collect/system/CMakeLists.txt
+++ b/src/plugin/collect/system/CMakeLists.txt
@@ -10,5 +10,7 @@ add_library(system_collector SHARED
./command/command_collector.cpp
./command/command_base.cpp
)
+target_include_directories(system_collector PRIVATE src/common)
+target_link_libraries(system_collector common)
set_target_properties(system_collector PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${PLUGIN_OUTPUT_LIBRARY_DIRECTORY})
\ No newline at end of file
diff --git a/src/plugin/collect/system/command/command_base.h b/src/plugin/collect/system/command/command_base.h
index ef9bd7c..8653a60 100644
--- a/src/plugin/collect/system/command/command_base.h
+++ b/src/plugin/collect/system/command/command_base.h
@@ -34,7 +34,7 @@ public:
std::string nowType;
oeaware::Topic topic;
std::unordered_map<std::string, std::vector<std::string>> attrsFirst;
- std::vector<std::string> skipLine{"---swap--"};
+ std::vector<std::string> skipLine{"---swap--", "Average:"};
static std::vector<std::string> command;
static std::vector<std::string> illegal;
CommandBase();
diff --git a/src/plugin/collect/system/command/command_collector.cpp b/src/plugin/collect/system/command/command_collector.cpp
index 640a406..1f6ac1e 100644
--- a/src/plugin/collect/system/command/command_collector.cpp
+++ b/src/plugin/collect/system/command/command_collector.cpp
@@ -9,8 +9,8 @@
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
******************************************************************************/
-
#include "command_collector.h"
+#include <unistd.h>
CommandCollector::CommandCollector(): oeaware::Interface()
{
@@ -41,8 +41,9 @@ void CommandCollector::CollectThread(const oeaware::Topic &topic, CommandBase* c
while (collector->isRunning && fgets(buffer, sizeof(buffer), p.stream) != nullptr) {
collector->ParseLine(std::string(buffer));
}
+ int waitTime = 10 * 1000;
+ usleep(waitTime);
p.Pclose();
-
collector->Close();
}
diff --git a/src/plugin/collect/system/kernel_config.cpp b/src/plugin/collect/system/kernel_config.cpp
index 3d25251..145251c 100644
--- a/src/plugin/collect/system/kernel_config.cpp
+++ b/src/plugin/collect/system/kernel_config.cpp
@@ -17,7 +17,9 @@
#include <securec.h>
#include <dirent.h>
#include <sys/stat.h>
+#include "utils.h"
#include "command_base.h"
+#include "data_register.h"
KernelConfig::KernelConfig(): oeaware::Interface()
{
@@ -35,6 +37,31 @@ KernelConfig::KernelConfig(): oeaware::Interface()
}
}
+bool KernelConfig::InitCmd(std::stringstream &ss, const std::string &topicType)
+{
+ std::string cmd;
+ std::string word;
+ while (ss >> word) {
+ if (word == cmdSeparator) {
+ if (!CommandBase::ValidateCmd(cmd)) {
+ return false;
+ }
+ getCmds[topicType].emplace_back(cmd);
+ cmd = "";
+ continue;
+ }
+ if (!cmd.empty()) {
+ cmd += " ";
+ }
+ cmd += word;
+ }
+ if (!CommandBase::ValidateCmd(cmd)) {
+ return false;
+ }
+ getCmds[topicType].emplace_back(cmd);
+ return true;
+}
+
oeaware::Result KernelConfig::OpenTopic(const oeaware::Topic &topic)
{
if (find(topicStr.begin(), topicStr.end(), topic.topicName) == topicStr.end()) {
@@ -42,9 +69,12 @@ oeaware::Result KernelConfig::OpenTopic(const oeaware::Topic &topic)
}
std::stringstream ss(topic.params);
std::string word;
- while (ss >> word) {
- if (topic.topicName == "get_kernel_config") {
- getTopics[topic.GetType()].insert(word);
+ std::string topicType = topic.GetType();
+ if (topic.topicName == "get_cmd" && !InitCmd(ss, topicType)) {
+ return oeaware::Result(FAILED, "params invalid.");
+ } else if (topic.topicName == "get_kernel_config") {
+ while (ss >> word) {
+ getTopics[topicType].insert(word);
}
}
return oeaware::Result(OK);
@@ -53,87 +83,11 @@ oeaware::Result KernelConfig::OpenTopic(const oeaware::Topic &topic)
void KernelConfig::CloseTopic(const oeaware::Topic &topic)
{
getTopics.erase(topic.GetType());
+ getCmds.erase(topic.GetType());
setSystemParams.clear();
cmdRun.clear();
}
-void KernelConfig::InitFileParam()
-{
- for (auto &v : kernelParamPath) {
- std::string path = v[1];
- std::ifstream file(path);
- if (!file.is_open()) {
- continue;
- }
- std::string key = v[0];
- std::string line;
- std::string value = "";
- while (std::getline(file, line)) {
- if (line.empty()) {
- continue;
- }
- value += line;
- value += '\n';
- }
- kernelParams[key] = value;
- file.close();
- }
-}
-
-void KernelConfig::AddCommandParam(const std::string &cmd)
-{
- FILE *pipe = popen(cmd.data(), "r");
- if (!pipe) {
- return;
- }
- char buffer[1024];
- std::string value;
- while (fgets(buffer, sizeof(buffer), pipe) != nullptr) {
- value += buffer;
- }
- pclose(pipe);
- auto v = oeaware::SplitString(cmd, " ");
- std::vector<std::string> skipSpace;
- for (auto &word : v) {
- if (word.empty()) continue;
- skipSpace.emplace_back(word);
- }
- if (skipSpace.size() > 1 && v[0] == "ethtool") {
- kernelParams[v[0] + "@" + v[1]] = value;
- return;
- }
- kernelParams[cmd] = value;
-}
-
-static bool IsSymlink(const std::string &path)
-{
- struct stat st;
- if (lstat(path.c_str(), &st) != 0) {
- perror("lstat failed");
- return false;
- }
- return S_ISLNK(st.st_mode);
-}
-
-void KernelConfig::GetAllEth()
-{
- const std::string path = "/sys/class/net";
- std::vector<std::string> interfaces;
- DIR* dir = opendir(path.c_str());
- if (dir == nullptr) {
- WARN(logger, "failed to open directory: " << path << ".");
- return;
- }
- struct dirent* entry;
- while ((entry = readdir(dir)) != nullptr) {
- std::string name(entry->d_name);
- if (name != "." && name != ".." && IsSymlink(path + "/" + name)) {
- allEths.push_back(name);
- }
- }
- closedir(dir);
-}
-
oeaware::Result KernelConfig::Enable(const std::string &param)
{
(void)param;
@@ -156,21 +110,12 @@ oeaware::Result KernelConfig::Enable(const std::string &param)
sysctlParams[values[0]] = values[1];
}
pclose(pipe);
- InitFileParam();
- GetAllEth();
- AddCommandParam("lscpu");
- AddCommandParam("ifconfig");
- for (auto &eth : allEths) {
- AddCommandParam("ethtool " + eth);
- }
-
return oeaware::Result(OK);
}
void KernelConfig::Disable()
{
sysctlParams.clear();
- kernelParams.clear();
setSystemParams.clear();
getTopics.clear();
cmdRun.clear();
@@ -196,32 +141,103 @@ void KernelConfig::UpdateData(const DataList &dataList)
return;
}
-void KernelConfig::PublishKernelConfig()
+std::vector<std::string> KernelConfig::getCmdGroup{"cat", "grep", "awk", "pgrep", "ls", "ethtool"};
+
+static void SetKernelData(KernelData *data, const std::string &ret)
{
- if (getTopics.empty()) {
+ data->kernelData = new KernelDataNode();
+ if (data->kernelData == nullptr) {
+ return;
+ }
+ data->len = 1;
+ data->kernelData->key = new char[1];
+ if (data->kernelData->key == nullptr) {
+ delete data->kernelData;
+ data->kernelData = nullptr;
+ return;
+ }
+ data->kernelData->key[0] = 0;
+ data->kernelData->value = new char[ret.size() + 1];
+ if (data->kernelData->value == nullptr) {
+ delete data->kernelData;
+ delete[] data->kernelData->key;
+ data->kernelData->key = nullptr;
+ data->kernelData = nullptr;
return;
}
+ strcpy_s(data->kernelData->value, ret.size() + 1, ret.data());
+ data->kernelData->next = nullptr;
+}
+
+void KernelConfig::PublishCmd()
+{
+ for (auto &p : getCmds) {
+ oeaware::Topic topic = oeaware::Topic::GetTopicFromType(p.first);
+ DataList dataList;
+ if (!oeaware::SetDataListTopic(&dataList, topic.instanceName, topic.topicName, topic.params)) {
+ continue;
+ }
+ KernelData *data = new KernelData();
+ if (data == nullptr) {
+ WARN(logger, "KernelData failed to allocate memory.");
+ continue;
+ }
+ std::string cmd = "";
+ for (auto &cmdPart : p.second) {
+ if (!cmd.empty()) {
+ cmd += " | ";
+ }
+ cmd += cmdPart;
+ }
+ PopenProcess pipe;
+ pipe.Popen(cmd);
+ char buffer[1024];
+ std::string ret = "";
+ while (fgets(buffer, sizeof(buffer), pipe.stream) != nullptr) {
+ ret += buffer;
+ }
+ if (pipe.Pclose() < 0) {
+ WARN(logger, "pipe close error.");
+ }
+ SetKernelData(data, ret);
+ dataList.len = 1;
+ dataList.data = new void* [1];
+ if (dataList.data == nullptr) {
+ oeaware::Register::GetInstance().GetDataFreeFunc("kernel_config")(data);
+ continue;
+ }
+ dataList.data[0] = data;
+ Publish(dataList);
+ }
+}
+
+void KernelConfig::PublishKernelParams()
+{
for (auto &p : getTopics) {
oeaware::Topic topic = oeaware::Topic::GetTopicFromType(p.first);
DataList dataList;
- dataList.topic.instanceName = new char[topic.instanceName.size() + 1];
- strcpy_s(dataList.topic.instanceName, topic.instanceName.size() + 1, topic.instanceName.data());
- dataList.topic.topicName = new char[topic.topicName.size() + 1];
- strcpy_s(dataList.topic.topicName, topic.topicName.size() + 1, topic.topicName.data());
- dataList.topic.params = new char[topic.params.size() + 1];
- strcpy_s(dataList.topic.params, topic.params.size() + 1, topic.params.data());
+ if (!oeaware::SetDataListTopic(&dataList, topic.instanceName, topic.topicName, topic.params)) {
+ continue;
+ }
KernelData *data = new KernelData();
+ if (data == nullptr) {
+ WARN(logger, "KernelData failed to allocate memory.");
+ continue;
+ }
KernelDataNode *tmp = nullptr;
for (auto &name : p.second) {
std::string value = "";
if (sysctlParams.count(name)) {
value = sysctlParams[name];
- } else if (kernelParams.count(name)) {
- value = kernelParams[name];
} else {
+ WARN(logger, "invalid params: " << name << ".");
continue;
}
KernelDataNode *newNode = createNode(name.data(), value.data());
+ if (newNode == nullptr) {
+ WARN(logger, "KernelDataNode failed to allocate memory.");
+ continue;
+ }
if (data->kernelData == NULL) {
data->kernelData = newNode;
tmp = newNode;
@@ -239,6 +255,15 @@ void KernelConfig::PublishKernelConfig()
}
}
+void KernelConfig::PublishKernelConfig()
+{
+ if (getTopics.empty() && getCmds.empty()) {
+ return;
+ }
+ PublishCmd();
+ PublishKernelParams();
+}
+
void KernelConfig::WriteSysParam(const std::string &path, const std::string &value)
{
std::ofstream sysFile(path);
diff --git a/src/plugin/collect/system/kernel_config.h b/src/plugin/collect/system/kernel_config.h
index 32049d4..4c6eb99 100644
--- a/src/plugin/collect/system/kernel_config.h
+++ b/src/plugin/collect/system/kernel_config.h
@@ -20,10 +20,11 @@
/*
* topic: get_kernel_config, obtain the kernel parameter information.
* params: kernel params name, including
- * 1. sysctl -a -N
- * 2. kernel_version, os_release, meminfo, zone_reclaim_mode
- * 3. lscpu, ifconfig, ethtool@{name}.
- * params :
+ * 1. sysctl -a -N
+ *
+ * topic: get_cmd, the trustlist command is supported.
+ * params: each command is seqarated by "@@", include "cat", "grep", "awk", "pgrep", "ls", "ethtool".
+ *
* topic: set_kernel_config, modify kernel parameters.
* DataList:
* data: KernelData, [key, value]:
@@ -41,29 +42,27 @@ public:
void Disable() override;
void Run() override;
private:
+ void PublishCmd();
+ void PublishKernelParams();
void PublishKernelConfig();
void SetKernelConfig();
+ bool InitCmd(std::stringstream &ss, const std::string &topicType);
void InitFileParam();
void AddCommandParam(const std::string &cmd);
void WriteSysParam(const std::string &path, const std::string &value);
void GetAllEth();
- std::vector<std::string> topicStr = {"get_kernel_config", "set_kernel_config"};
-
- const std::vector<std::vector<std::string>> kernelParamPath{{"kernel_version", "/proc/version"},
- {"os_release", "/etc/os-release"}, {"meminfo", "/proc/meminfo"},
- {"zone_reclaim_mode", "/proc/sys/vm/zone_reclaim_mode"}};
+ std::vector<std::string> topicStr = {"get_kernel_config", "get_cmd", "set_kernel_config"};
// key: topic type, value: parameters to be queried.
std::unordered_map<std::string, std::unordered_set<std::string>> getTopics;
+ std::unordered_map<std::string, std::vector<std::string>> getCmds;
std::vector<std::pair<std::string, std::string>> setSystemParams;
std::unordered_map<std::string, std::string> sysctlParams;
- // Stores system parameters, include lscpu, ifconfig, file path.
- std::unordered_map<std::string, std::string> kernelParams;
-
std::vector<std::string> cmdRun;
+ static std::vector<std::string> getCmdGroup;
static std::vector<std::string> cmdGroup;
- std::vector<std::string> allEths;
+ const std::string cmdSeparator = "@@";
};
#endif
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index dec13ac..11079f2 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -25,7 +25,7 @@ add_executable(pmu_count_test
pmu_count_test.cpp
)
-target_link_libraries(serialize_test PRIVATE curl GTest::gtest_main)
+target_link_libraries(serialize_test PRIVATE common GTest::gtest_main)
target_link_libraries(logger_test PRIVATE GTest::gtest_main log4cplus)
target_link_libraries(safe_queue_test PRIVATE GTest::gtest_main)
target_link_libraries(pmu_count_test PRIVATE GTest::gtest_main)
--
2.33.0

View File

@ -1,135 +0,0 @@
From cf12b73ec44cf0c553319d675b780e6e8c84f0bc Mon Sep 17 00:00:00 2001
From: LHesperus <2639350497@qq.com>
Date: Tue, 3 Dec 2024 16:10:46 +0800
Subject: [PATCH 2/4] fix bug : free spe mem after use data
---
src/plugin/collect/pmu/pmu_spe_collector.cpp | 66 ++++++++++++--------
src/plugin/collect/pmu/pmu_spe_collector.h | 5 +-
2 files changed, 45 insertions(+), 26 deletions(-)
diff --git a/src/plugin/collect/pmu/pmu_spe_collector.cpp b/src/plugin/collect/pmu/pmu_spe_collector.cpp
index 114c0ee..8d265eb 100644
--- a/src/plugin/collect/pmu/pmu_spe_collector.cpp
+++ b/src/plugin/collect/pmu/pmu_spe_collector.cpp
@@ -63,15 +63,26 @@ int PmuSpeCollector::OpenSpe()
return pd;
}
-void PmuSpeCollector::DynamicAdjustPeriod(uint64_t interval)
+void PmuSpeCollector::DynamicAdjustPeriod(int interval)
{
+ if (pmuId == -1) {
+ return;
+ }
if (interval > timeout) {
PmuDisable(pmuId);
PmuClose(pmuId);
attrPeriod *= periodThreshold;
+ if (attrPeriod > maxAttrPeriod) {
+ attrPeriod = maxAttrPeriod;
+ }
+ INFO(logger, "PmuSpeCollector dynamic adjust period to " <<
+ attrPeriod << ", PmuRead interval is " << interval << " ms.");
pmuId = OpenSpe();
- PmuEnable(pmuId);
+ if (pmuId != -1) {
+ PmuEnable(pmuId);
+ }
}
+ // later add code to decrease period when interval is too small
}
oeaware::Result PmuSpeCollector::OpenTopic(const oeaware::Topic &topic)
@@ -79,7 +90,8 @@ oeaware::Result PmuSpeCollector::OpenTopic(const oeaware::Topic &topic)
if (topic.instanceName != this->name || topic.topicName != topicStr) {
return oeaware::Result(FAILED, "OpenTopic failed");
}
-
+ attrPeriod = minAttrPeriod;
+ readTimeMs = 0;
if (pmuId == -1) {
pmuId = OpenSpe();
if (pmuId == -1) {
@@ -124,27 +136,31 @@ void PmuSpeCollector::UpdateData(const DataList &dataList)
void PmuSpeCollector::Run()
{
- if (pmuId != -1) {
- PmuSpeData *data = new PmuSpeData();
- PmuDisable(pmuId);
- data->len = PmuRead(pmuId, &(data->pmuData));
- PmuEnable(pmuId);
-
- auto now = std::chrono::high_resolution_clock::now();
- data->interval = std::chrono::duration_cast<std::chrono::milliseconds>(now - timestamp).count();
- DynamicAdjustPeriod(data->interval);
- timestamp = std::chrono::high_resolution_clock::now();
-
- DataList dataList;
- dataList.topic.instanceName = new char[name.size() + 1];
- strcpy_s(dataList.topic.instanceName, name.size() + 1, name.data());
- dataList.topic.topicName = new char[topicStr.size() + 1];
- strcpy_s(dataList.topic.topicName, topicStr.size() + 1, topicStr.data());
- dataList.topic.params = new char[1];
- dataList.topic.params[0] = 0;
- dataList.len = 1;
- dataList.data = new void* [1];
- dataList.data[0] = data;
- Publish(dataList);
+ // adjust period will pmuclose and free spe data
+ // so adjust period should be done after other plugins have finished using SPE data
+ DynamicAdjustPeriod(readTimeMs);
+ if (pmuId == -1) {
+ return;
}
+ PmuSpeData *data = new PmuSpeData();
+ PmuDisable(pmuId);
+ auto readBegin = std::chrono::high_resolution_clock::now();
+ data->len = PmuRead(pmuId, &(data->pmuData));
+ readTimeMs = std::chrono::duration_cast<std::chrono::milliseconds>(
+ std::chrono::high_resolution_clock::now() - readBegin).count();
+ PmuEnable(pmuId);
+ auto now = std::chrono::high_resolution_clock::now();
+ data->interval = std::chrono::duration_cast<std::chrono::milliseconds>(now - timestamp).count();
+ timestamp = std::chrono::high_resolution_clock::now();
+ DataList dataList;
+ dataList.topic.instanceName = new char[name.size() + 1];
+ strcpy_s(dataList.topic.instanceName, name.size() + 1, name.data());
+ dataList.topic.topicName = new char[topicStr.size() + 1];
+ strcpy_s(dataList.topic.topicName, topicStr.size() + 1, topicStr.data());
+ dataList.topic.params = new char[1];
+ dataList.topic.params[0] = 0;
+ dataList.len = 1;
+ dataList.data = new void *[1];
+ dataList.data[0] = data;
+ Publish(dataList);
}
\ No newline at end of file
diff --git a/src/plugin/collect/pmu/pmu_spe_collector.h b/src/plugin/collect/pmu/pmu_spe_collector.h
index 00f0be7..3323e47 100644
--- a/src/plugin/collect/pmu/pmu_spe_collector.h
+++ b/src/plugin/collect/pmu/pmu_spe_collector.h
@@ -28,7 +28,7 @@ public:
void Disable() override;
void Run() override;
private:
- void DynamicAdjustPeriod(uint64_t interval);
+ void DynamicAdjustPeriod(int interval);
void InitSpeAttr(struct PmuAttr &attr);
int OpenSpe();
@@ -38,6 +38,9 @@ private:
std::chrono::time_point<std::chrono::high_resolution_clock> timestamp;
const int timeout = 50;
const int periodThreshold = 2;
+ const int minAttrPeriod = 2048;
+ const int maxAttrPeriod = 2048000;
+ int readTimeMs = 0; // last period PmuRead() time
};
#endif
\ No newline at end of file
--
2.33.0

File diff suppressed because it is too large Load Diff

View File

@ -1,24 +0,0 @@
From 5315e7712951014a238e98ab1613e41edc0e8cb9 Mon Sep 17 00:00:00 2001
From: LHesperus <2639350497@qq.com>
Date: Wed, 4 Dec 2024 19:46:04 +0800
Subject: [PATCH 4/4] cycles sample add elf resolve to support d-fot
---
src/plugin/collect/pmu/pmu_sampling_collector.cpp | 1 +
1 file changed, 1 insertion(+)
diff --git a/src/plugin/collect/pmu/pmu_sampling_collector.cpp b/src/plugin/collect/pmu/pmu_sampling_collector.cpp
index 8f8576f..59ba800 100644
--- a/src/plugin/collect/pmu/pmu_sampling_collector.cpp
+++ b/src/plugin/collect/pmu/pmu_sampling_collector.cpp
@@ -67,6 +67,7 @@ int PmuSamplingCollector::OpenSampling(const oeaware::Topic &topic)
if (topic.topicName == "cycles") {
attr.freq = CYCLES_FREQ;
attr.useFreq = 1;
+ attr.symbolMode = RESOLVE_ELF;
} else {
attr.period = NET_RECEIVE_TRACE_SAMPLE_PERIOD;
}
--
2.33.0

Binary file not shown.

Binary file not shown.

View File

@ -1,35 +1,28 @@
Name: oeAware-manager
Version: v2.0.0
Release: 3
Version: v2.0.1
Release: 1
Summary: OeAware is a framework for implementing low-load collection, sensing, and tuning on openEuler.
License: MulanPSL2
URL: https://gitee.com/openeuler/%{name}
Source0: %{name}-%{version}.tar.gz
Patch1: 0001-remove-old-code.patch
Patch2: 0002-add-data-free-and-fix-unsubscribe-error.patch
Patch3: 0003-fix-failed-to-connect-to-the-sdk-and-command-executi.patch
Patch4: 0004-add-command-verification.patch
Patch5: 0005-modify-C-interface-name-add-enable-count-and-fix-bug.patch
Patch6: 0006-add-one-time-command-collection.patch
Patch7: 0007-fix-bug-free-spe-mem-after-use-data.patch
Patch8: 0008-Adjust-the-header-file-structure.patch
Patch9: 0009-cycles-sample-add-elf-resolve-to-support-d-fot.patch
Patch1: 0001-change-the-folder-permission-to-755-add-oeaware-grou.patch
BuildRequires: cmake make gcc-c++
BuildRequires: boost-devel
BuildRequires: curl-devel
BuildRequires: log4cplus-devel
BuildRequires: yaml-cpp-devel
BuildRequires: gtest-devel gmock-devel
BuildRequires: libboundscheck
%ifarch aarch64
BuildRequires: libkperf libkperf-devel
Requires: libkperf
%endif
BuildRequires: libnl3 libnl3-devel
BuildRequires: numactl-devel
BuildRequires: kernel-devel
Requires: graphviz yaml-cpp curl log4cplus boost systemd libboundscheck
Requires: libnl3 acl
Requires: sysstat
Obsoletes: oeAware-collector < v2.0.0
Obsoletes: oeAware-scenario < v2.0.0
@ -64,17 +57,22 @@ mkdir -p %{buildroot}%{_includedir}/oeaware/data
install -dm 0755 %{buildroot}%{_prefix}/lib/smc
install -b -m740 ./build/output/plugin/lib/*.so %{buildroot}%{_libdir}/oeAware-plugin/
install -b -m740 ./build/output/include/oeaware/*.h %{buildroot}%{_includedir}/oeaware
install -b -m740 ./build/output/include/oeaware/data/*.h %{buildroot}%{_includedir}/oeaware/data
install -b -m740 ./build/output/include/oeaware/*.h %{buildroot}%{_includedir}/oeaware
install -b -m740 ./build/output/include/oeaware/data/*.h %{buildroot}%{_includedir}/oeaware/data
install -b -m740 ./build/output/sdk/liboeaware-sdk.so %{buildroot}%{_libdir}
install -D -m 0640 ./build/output/plugin/lib/thread_scenario.conf %{buildroot}%{_libdir}/oeAware-plugin/
install -D -m 0640 ./build/output/plugin/lib/ub_tune.conf %{buildroot}%{_libdir}/oeAware-plugin/
install -D -m 0640 ./build/output/plugin/lib/xcall.yaml %{buildroot}%{_libdir}/oeAware-plugin/
install -D -m 0400 ./build/output/plugin/ko/smc_acc.ko %{buildroot}%{_prefix}/lib/smc
%preun
%systemd_preun oeaware.service
%post
if ! grep -q "oeaware:" /etc/group; then
groupadd oeaware
setfacl -m g:oeaware:r /usr/lib64/liboeaware-sdk.so
fi
systemctl start oeaware.service
chcon -t modules_object_t %{_prefix}/lib/smc/smc_acc.ko >/dev/null 2>&1
exit 0
@ -85,10 +83,6 @@ if [ "${VERSION}" == "22.03 (LTS-SP4)" ]; then
systemctl enable oeaware.service
fi
if ! grep -q "oeaware:" /etc/group; then
groupadd oeaware
fi
%files
%attr(0750, root, root) %{_bindir}/oeaware
%attr(0750, root, root) %{_bindir}/oeawarectl
@ -96,6 +90,7 @@ fi
%attr(0644, root, root) %{_unitdir}/oeaware.service
%attr(0640, root, root) %{_libdir}/oeAware-plugin/ub_tune.conf
%attr(0640, root, root) %{_libdir}/oeAware-plugin/thread_scenario.conf
%attr(0640, root, root) %{_libdir}/oeAware-plugin/xcall.yaml
%attr(0400, root, root) %{_prefix}/lib/smc/smc_acc.ko
%attr(0440, root, root) %{_libdir}/oeAware-plugin/*.so
%attr(0440, root, root) %{_libdir}/liboeaware-sdk.so
@ -105,8 +100,15 @@ fi
%attr(0644, root, root) %{_includedir}/oeaware/data/*.h
%changelog
* Wed Dec 11 2024 LHesperus <liuchanggeng@huawei.com> -v2.0.1-1
- add some document
- fix bug : spe period dynamic changes
- fix bug : analysis client not work
- fix bug : cpuburst exit error
- adapt : seep tune , steal task tune, xcall tune
* Fri Dec 6 2024 fly_1997 <flylove7@outlook.com> -v2.0.0-3
- adjust the header file structure
- adjust the header file structure
- fix cycles sample, spe memory free
- add one time command collection