[LTTng学习之旅]------在用户程序中简单的添加一个trace点

25 篇文章 6 订阅

编写 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
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小羊苏C

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值