1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 * Description: ftrace plugin module implements
16 */
17 #include "ftrace_module.h"
18 #include <memory>
19 #include <mutex>
20
21 #include "flow_controller.h"
22 #include "logging.h"
23
24 namespace {
25 constexpr uint32_t MAX_BUFFER_SIZE = 4 * 1024 * 1024;
26
27 std::mutex g_mutex;
28 std::unique_ptr<FTRACE_NS::FlowController> g_mainController;
29 } // namespace
30
TracePluginRegisterWriter(const WriterStruct * writer)31 int TracePluginRegisterWriter(const WriterStruct* writer)
32 {
33 std::unique_lock<std::mutex> lock(g_mutex);
34 g_mainController = std::make_unique<FTRACE_NS::FlowController>();
35
36 HILOG_INFO(LOG_CORE, "%s: %d %p", __func__, __LINE__, writer);
37 int result = g_mainController->SetWriter(const_cast<WriterStructPtr>(writer));
38 HILOG_INFO(LOG_CORE, "%s: %d", __func__, __LINE__);
39 return result;
40 }
41
TracePluginStartSession(const uint8_t configData[],const uint32_t configSize)42 int TracePluginStartSession(const uint8_t configData[], const uint32_t configSize)
43 {
44 std::unique_lock<std::mutex> lock(g_mutex);
45 CHECK_NOTNULL(g_mainController, -1, "no FlowController created!");
46
47 HILOG_INFO(LOG_CORE, "%s: %d", __func__, __LINE__);
48 int retval = g_mainController->LoadConfig(configData, configSize);
49 CHECK_TRUE(retval == 0, retval, "LoadConfig failed!");
50
51 HILOG_INFO(LOG_CORE, "%s: %d", __func__, __LINE__);
52 int result = g_mainController->StartCapture();
53 HILOG_INFO(LOG_CORE, "%s: %d", __func__, __LINE__);
54 return result;
55 }
56
TracePluginReportBasicData()57 static int TracePluginReportBasicData()
58 {
59 std::unique_lock<std::mutex> lock(g_mutex);
60 CHECK_NOTNULL(g_mainController, -1, "no FlowController created!");
61
62 HILOG_INFO(LOG_CORE, "%s: %d", __func__, __LINE__);
63 int result = g_mainController->ParseBasicData();
64 HILOG_INFO(LOG_CORE, "%s: %d", __func__, __LINE__);
65 return result;
66 }
67
TracePluginStopSession()68 int TracePluginStopSession()
69 {
70 std::unique_lock<std::mutex> lock(g_mutex);
71 CHECK_NOTNULL(g_mainController, -1, "no FlowController created!");
72
73 HILOG_INFO(LOG_CORE, "%s: %d", __func__, __LINE__);
74 int result = g_mainController->StopCapture();
75 HILOG_INFO(LOG_CORE, "%s: %d", __func__, __LINE__);
76
77 g_mainController.reset();
78 return result;
79 }
80
81 static PluginModuleCallbacks moduleCallbacks = {
82 .onPluginSessionStart = TracePluginStartSession,
83 .onPluginReportResult = 0,
84 .onPluginSessionStop = TracePluginStopSession,
85 .onRegisterWriterStruct = TracePluginRegisterWriter,
86 .onReportBasicDataCallback = TracePluginReportBasicData, // report ftrace_plugin basic data
87 };
88
89 EXPORT_API PluginModuleStruct g_pluginModule = {
90 .callbacks = &moduleCallbacks,
91 .name = "ftrace-plugin",
92 .version = "1.01",
93 .resultBufferSizeHint = MAX_BUFFER_SIZE,
94 };
95