编写 trace provider
在头文件中定义tracepoint event.
#undef LTTNG_UST_TRACEPOINT_PROVIDER
#define LTTNG_UST_TRACEPOINT_PROVIDER provider_my
#undef LTTNG_UST_TRACEPOINT_INCLUDE
#define LTTNG_UST_TRACEPOINT_INCLUDE "./my_lttng_usr.h"
#if !defined(_MY_LTTNG_USR_H) || defined(LTTNG_UST_TRACEPOINT_HEADER_MULTI_READ)
#define _MY_LTTNG_USR_H
#include <lttng/tracepoint.h>
/*
* Use LTTNG_UST_TRACEPOINT_EVENT(), LTTNG_UST_TRACEPOINT_EVENT_CLASS(),
* LTTNG_UST_TRACEPOINT_EVENT_INSTANCE(), and
* LTTNG_UST_TRACEPOINT_LOGLEVEL() here.
*/
LTTNG_UST_TRACEPOINT_EVENT(
/* Tracepoint provider name */
provider_my,
/* Tracepoint name */
enter_app,
/* Input arguments */
LTTNG_UST_TP_ARGS(
int, index,
double,start_time_record
),
/* Output event fields */
LTTNG_UST_TP_FIELDS(
lttng_ust_field_integer(int,app_run_index,index)
lttng_ust_field_float(double, period_record, start_time_record)
)
)
#endif /* _MY_LTTNG_USR_H */
#include <lttng/tracepoint-event.h>
在C文件中简单包含头文件即可,如果使用多组tracepoint event。 可以在C 文件中包含多个头文件。
#define LTTNG_UST_TRACEPOINT_CREATE_PROBES
#include "my_lttng_usr.h"
编译为.o文件:
gcc -I. -c my_lttng_usr.c
在代码中包含tracepoint
#include<pthread.h>
#include<stdio.h>
#include<chrono>
#include <iostream>
#include<ratio>
#include<thread>
#define LTTNG_UST_TRACEPOINT_DEFINE
#include"my_lttng_usr.h"
using namespace std;
void my_print_thread(void){
}
int main(){
//get the start time
auto time_point_start = chrono::steady_clock::now();
cout<<"start--------"<<endl;
int i = 0;
auto last_time = time_point_start;
chrono::milliseconds ms10(10);
while(i<=100){
auto this_time = chrono::steady_clock::now();
chrono::duration<double,milli>init_spend_time= this_time-time_point_start;
chrono::duration<double,milli>period_spend_time=this_time-last_time;
last_time = this_time;
cout<<"run "<<i<<" times, now is"<<init_spend_time.count()<<"ms, period is "<<period_spend_time.count()<<"ms..."<<endl;
lttng_ust_tracepoint(provider_my,enter_app,i,period_spend_time.count());
i++;
if(i>50) i=1;
this_thread::sleep_until(this_time+ms10);
}
if(chrono::steady_clock::is_steady) cout<<"steady clock is steady"<<endl;
auto end_time_high = chrono::steady_clock::now()-time_point_start;
cout<<"total time spend:"<<chrono::duration_cast<chrono::nanoseconds>(end_time_high).count()<<endl;
cout<<"exit..."<<endl;
return 0;
}
app也编译成.o
g++ -lpthread -c multi.cpp
最后编译链接成app运行
g++ -o app multi.o my_lttng_usr.o -llttng-ust -ldl