• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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 <memory>
18 #include <mutex>
19 
20 #include "flow_controller.h"
21 #include "ftrace_namespace.h"
22 #include "logging.h"
23 #include "plugin_module_api.h"
24 
25 namespace {
26 constexpr uint32_t MAX_BUFFER_SIZE = 4 * 1024 * 1024;
27 
28 std::mutex g_mutex;
29 std::unique_ptr<FTRACE_NS::FlowController> g_mainController;
30 } // namespace
31 
TracePluginRegisterWriter(const WriterStruct * writer)32 static int TracePluginRegisterWriter(const WriterStruct* writer)
33 {
34     std::unique_lock<std::mutex> lock(g_mutex);
35     g_mainController = std::make_unique<FTRACE_NS::FlowController>();
36 
37     int result = g_mainController->SetWriter(const_cast<WriterStructPtr>(writer));
38     PROFILER_LOG_INFO(LOG_CORE, "%s: %d", __func__, __LINE__);
39     return result;
40 }
41 
TracePluginStartSession(const uint8_t configData[],const uint32_t configSize)42 static 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, "%s: no FlowController created!", __func__);
46 
47     PROFILER_LOG_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     PROFILER_LOG_INFO(LOG_CORE, "%s: %d", __func__, __LINE__);
52     int result = g_mainController->StartCapture();
53     PROFILER_LOG_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, "%s: no FlowController created!", __func__);
61 
62     PROFILER_LOG_INFO(LOG_CORE, "%s: %d", __func__, __LINE__);
63     g_mainController->SetReportBasicData(true);
64     PROFILER_LOG_INFO(LOG_CORE, "%s: %d", __func__, __LINE__);
65     return 0;
66 }
67 
TracePluginStopSession()68 static int TracePluginStopSession()
69 {
70     std::unique_lock<std::mutex> lock(g_mutex);
71     CHECK_NOTNULL(g_mainController, -1, "%s: no FlowController created!", __func__);
72 
73     PROFILER_LOG_INFO(LOG_CORE, "%s: %d", __func__, __LINE__);
74     int result = g_mainController->StopCapture();
75     PROFILER_LOG_INFO(LOG_CORE, "%s: %d", __func__, __LINE__);
76 
77     g_mainController.reset();
78     return result;
79 }
80 
TraceReportState()81 static bool TraceReportState()
82 {
83     return g_mainController->IsDataReady();
84 }
85 
86 static PluginModuleCallbacks moduleCallbacks = {
87     .onPluginSessionStart = TracePluginStartSession,
88     .onPluginReportResult = 0,
89     .onPluginSessionStop = TracePluginStopSession,
90     .onRegisterWriterStruct = TracePluginRegisterWriter,
91     .onReportBasicDataCallback = TracePluginReportBasicData, // report ftrace_plugin basic data
92     .onReportStateCallback = TraceReportState,
93 };
94 
95 EXPORT_API PluginModuleStruct g_pluginModule = {
96     .callbacks = &moduleCallbacks,
97     .name = "ftrace-plugin",
98     .version = "1.02",
99     .resultBufferSizeHint = MAX_BUFFER_SIZE,
100 };
101