-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreaming.cc
248 lines (215 loc) · 7.79 KB
/
streaming.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include "shared_mutex"
#include "iostream"
#include "sys/socket.h"
#include "arpa/inet.h"
#include "thread"
#include "perfetto.h"
#include <android/log.h>
char *tag = "PERFEETO_STREAMING";
const int flush_period = 2000;
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, tag, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, tag, __VA_ARGS__)
// add surface flinger datasource
void add_surface_flinger_datasource(perfetto::TraceConfig &cfg){
auto* ds = cfg.add_data_sources()->mutable_config();
ds->set_name("android.surfaceflinger.frametimeline");
}
// add process stats datasource
void add_process_stats_ds(perfetto::TraceConfig &cfg){
auto* ds = cfg.add_data_sources()->mutable_config();
ds->set_name("linux.process_stats");
ds->set_target_buffer(0);
}
// add ftrace datasource
void add_ftrace_ds(perfetto::TraceConfig &cfg, const char * app_name){
auto* ds = cfg.add_data_sources()->mutable_config();
ds->set_name("linux.ftrace");
perfetto::protos::gen::FtraceConfig ftrace_cfg;
std::string events[] = {
"sched",
"freq",
"idle",
"am",
"wm",
"gfx",
"view",
"binder_driver",
"hal",
"dalvik",
"camera",
"input",
"res",
"memory"
};
for (const auto &item: events){
ftrace_cfg.add_atrace_categories(item);
}
ftrace_cfg.set_symbolize_ksyms(true);
ftrace_cfg.add_atrace_apps(app_name);
ds->set_ftrace_config_raw(ftrace_cfg.SerializeAsString());
}
// initialize perfetto ,set datasource and fill policy, set ringbuffer and flush period, not file
perfetto::TraceConfig getTraceConfig(const char * app_name){
perfetto::TraceConfig cfg;
auto buffer = cfg.add_buffers();
buffer->set_size_kb(32768);
buffer->set_fill_policy(
perfetto::protos::gen::TraceConfig_BufferConfig_FillPolicy::TraceConfig_BufferConfig_FillPolicy_RING_BUFFER
);
cfg.set_statsd_logging(perfetto::protos::gen::TraceConfig_StatsdLogging::TraceConfig_StatsdLogging_STATSD_LOGGING_DISABLED);
cfg.set_write_into_file(false);
cfg.set_enable_extra_guardrails(false);
cfg.set_flush_period_ms(flush_period);
add_ftrace_ds(cfg, app_name);
add_process_stats_ds(cfg);
add_surface_flinger_datasource(cfg);
return cfg;
}
void flush_handler(bool success){
if (success) {
LOGI("flush success");
} else {
LOGE("flush failed");
}
}
void send_message_to_server(int & sock, perfetto::TracingSession::ReadTraceCallbackArgs arg){
u_long size = arg.size;
auto cd = send(sock, arg.data, arg.size, 0);
if (cd < 0){
LOGE("send data error, code: %s", strerror(cd));
}else{
LOGI("send %zd byte data ", cd);
}
}
// send trace-packet, the first 4 byte is net order length, then the tracepacket body
// this version send method is called druing perfetto running
void send_message_to_server_with_order(int & sock, perfetto::TracingSession::ReadTraceCallbackArgs arg){
uint32_t size = arg.size;
uint32_t header = htonl(size);
auto header_cd = send(sock, &header, sizeof(header), 0);
LOGI("send header %zd byte", header_cd);
auto cd = send(sock, arg.data, arg.size, 0);
if (cd < 0){
LOGE("send data error, code: %s", strerror(cd));
}else{
LOGI("send %zd byte data", cd);
}
}
// send trace-packet, the first 4 byte is net order length, then the tracepacket body
// this version send method is called after perfetto stopped, the all left packet will be send at the same time
void send_message_to_server_with_order(int &sock, std::vector<char> blocking_data){
uint32_t size = blocking_data.size();
uint32_t header = htonl(size);
auto header_cd = send(sock, &header, sizeof(header), 0);
LOGI("send header %zd byte", header_cd);
auto cd = send(sock, &blocking_data[0], static_cast<std::streamsize>(blocking_data.size()), 0);
LOGI("send %zd byte data", cd);
}
class PerfettoStreaming{
private:
mutable std::shared_mutex mutex_;
bool _started = false;
const char *server_host = "127.0.0.1"; // the host of socket will be send
const char * app_name = ""; // trace android appName/ packege Identifier..like com.adnroid.chrome
int server_port; // the porf of socket will be send
int sock_instance;
void initialize_perfetto();
std::unique_ptr<perfetto::TracingSession> session;
bool get_started();
void clear_tracing();
bool set_sock();
// lambda void to async read perfetto trace packet
perfetto::TracingSession::ReadTraceCallback readTraceCallback = [&](perfetto::TracingSession::ReadTraceCallbackArgs arg){
if (arg.size > 0 && arg.data != nullptr){
send_message_to_server_with_order(this->sock_instance, arg);
}else{
LOGE("data is empty, skip send");
}
};
public:
void set_server_host(const char * host);
void set_server_port(int port);
void set_app_name(const char * appname);
void start_tracing();
int stop_tracing();
~PerfettoStreaming();
};
void PerfettoStreaming::set_server_host(const char * host) {
this->server_host=host;
}
void PerfettoStreaming::set_server_port(int port) {
this->server_port = port;
}
bool PerfettoStreaming::set_sock() {
if (this->server_port && this->server_host){
std::cout << "connect to socket server" << std::endl;
this->sock_instance = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server_addr{};
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(this->server_host);
server_addr.sin_port = htons(this->server_port);
auto connect_result = connect(this->sock_instance, (struct sockaddr *)&server_addr, sizeof server_addr);
if (connect_result==0){
LOGI("connect to target socket success");
return true;
}else{
LOGE("connect to target socket error !!! ");
};
}
LOGE("target socket host/port maybe empty! ");
return false;
}
void PerfettoStreaming::start_tracing() {
bool result = this->set_sock();
if (!result){
LOGE("can not connect to target socket, skip tracing");
return;
}
LOGI("initialize perfetto");
this->initialize_perfetto();
LOGI("start PerfEtto tracing");
this->session = perfetto::Tracing::NewTrace();
auto cfg = getTraceConfig(this->app_name);
this->session->Setup(cfg);
this->session->StartBlocking();
this->_started = true;
while (this->get_started()){
std::this_thread::sleep_for(std::chrono::milliseconds(flush_period));
if(this->get_started()){
this->session->Flush(flush_handler, 0);
LOGI("read tracing data");
this->session->ReadTrace(this->readTraceCallback);
}
}
}
void PerfettoStreaming::initialize_perfetto() {
perfetto::TracingInitArgs args;
args.backends = perfetto::kSystemBackend;
perfetto::Tracing::Initialize(args);
}
PerfettoStreaming::~PerfettoStreaming() {
close(this->sock_instance);
}
int PerfettoStreaming::stop_tracing() {
std::shared_lock<std::shared_mutex> lock(mutex_);
_started = false;
this->clear_tracing();
LOGI("stop perfetto success");
return 0;
}
bool PerfettoStreaming::get_started() {
std::shared_lock<std::shared_mutex> lock(mutex_);
return _started;
}
void PerfettoStreaming::set_app_name(const char *appname) {
this->app_name = appname;
}
void PerfettoStreaming::clear_tracing() {
this->session->FlushBlocking();
this->session->StopBlocking();
auto left_data = this->session->ReadTraceBlocking();
send_message_to_server_with_order(this->sock_instance, left_data);
close(this->sock_instance);
this->sock_instance= 0;
}