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
TracePluginStopSession()57 int TracePluginStopSession()
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->StopCapture();
64 HILOG_INFO(LOG_CORE, "%s: %d", __func__, __LINE__);
65
66 g_mainController.reset();
67 return result;
68 }
69
70 static PluginModuleCallbacks moduleCallbacks = {
71 .onPluginSessionStart = TracePluginStartSession,
72 .onPluginReportResult = 0,
73 .onPluginSessionStop = TracePluginStopSession,
74 .onRegisterWriterStruct = TracePluginRegisterWriter,
75 };
76
77 PluginModuleStruct g_pluginModule = {
78 .callbacks = &moduleCallbacks,
79 .name = "ftrace-plugin",
80 .resultBufferSizeHint = MAX_BUFFER_SIZE,
81 };
82