使用lttng的TRACEPOINT CLASS

发布一下 0 0

接着上篇我们继续学习lttng的功能,本节我们来讲讲lttnp的class功能,废话不多说了,上代码。

[root@ceph-1 test3]# cat tp.h#undef TRACEPOINT_PROVIDER#define TRACEPOINT_PROVIDER self_lttng_ust#undef TRACEPOINT_INCLUDE#define TRACEPOINT_INCLUDE "tp.h"#if !defined(TP_H) || defined(TRACEPOINT_HEADER_MULTI_READ)#define TP_H#include <lttng/tracepoint.h>#include "my-struct.h"TRACEPOINT_EVENT(	self_lttng_ust,	main_tracepoint,	TP_ARGS (		int32_t, grade,		const struct student *, student_info		),	TP_FIELDS (		ctf_string(name, student_info->name)		ctf_integer(int32_t, grade, grade)		ctf_integer(int32_t, age, student_info->age)		ctf_float(float, score, student_info->score)		))TRACEPOINT_EVENT(	self_lttng_ust,	main_tracepoint2,	TP_ARGS (		int32_t, grade,		const struct student *, student_info		),	TP_FIELDS (		ctf_string(name, student_info->name)		ctf_integer(int32_t, grade, grade)		ctf_integer(int32_t, age, student_info->age)		ctf_float(float, score, student_info->score)		))TRACEPOINT_EVENT(	self_lttng_ust,	main_tracepoint3,	TP_ARGS (		int32_t, grade,		const struct student *, student_info		),	TP_FIELDS (		ctf_string(name, student_info->name)		ctf_integer(int32_t, grade, grade)		ctf_integer(int32_t, age, student_info->age)		ctf_float(float, score, student_info->score)		))#endif#include <lttng/tracepoint-event.h>
[root@ceph-1 test3]# cat my-struct.h#ifndef MY_STRUCT_H#define MY_STRUCT_H#include <stdint.h>struct student {    int8_t name[32];    uint8_t age;    float score;};#endif
[root@ceph-1 test3]# cat tp.c#define TRACEPOINT_CREATE_PROBES#define TRACEPOINT_DEFINE#include "tp.h"
[root@ceph-1 test3]# cat main.c#include <stdio.h>#include <stdlib.h>#include <stdint.h>#include <unistd.h>#include "tp.h"#include "my-struct.h"int main(int argc, char *argv[]){    struct student s = {            .name = "sdc",            .age = 18,            .score = 123.5        };    int32_t grade = 1;    while(1) {	grade++;	tracepoint(self_lttng_ust, main_tracepoint, grade, &s);	tracepoint(self_lttng_ust, main_tracepoint2, grade, &s);	tracepoint(self_lttng_ust, main_tracepoint3, grade, &s);	sleep(1);    }    return 0;}
[root@ceph-1 test3]# cat CMakeLists.txtcmake_minimum_required(VERSION 3.12)project(self-ust)include_directories(${CMAKE_CURRENT_LIST_DIR})aux_source_directory(. SRC_FILES)add_executable(${PROJECT_NAME} ${SRC_FILES})target_link_libraries(${PROJECT_NAME} lttng-ust dl)
mkdir build;cmake ..make./self-ust[root@ceph-1 tracing]# lttng create my-user-space-sessionSession my-user-space-session created.Traces will be written in /root/lttng-traces/my-user-space-session-20230623-115058[root@ceph-1 tracing]# lttng enable-event --userspace self_lttng_ust:main_tracepoin*UST event self_lttng_ust:main_tracepoin* created in channel channel0[root@ceph-1 tracing]# lttng startTracing started for session my-user-space-session[root@ceph-1 tracing]# lttng stopWaiting for data availability.Tracing stopped for session my-user-space-session[root@ceph-1 tracing]# lttng destroySession my-user-space-session destroyedbabeltrace2 ~/lttng-traces/my-user-space-session*  


使用lttng的TRACEPOINT CLASS

我们可以将如上的重复代码进行合并,合并结果如下:

[root@ceph-1 build]# cat ../tp.h#undef TRACEPOINT_PROVIDER#define TRACEPOINT_PROVIDER self_lttng_ust#undef TRACEPOINT_INCLUDE#define TRACEPOINT_INCLUDE "tp.h"#if !defined(TP_H) || defined(TRACEPOINT_HEADER_MULTI_READ)#define TP_H#include <lttng/tracepoint.h>#include "my-struct.h"TRACEPOINT_EVENT_CLASS(	self_lttng_ust,	self_lttng_class,	TP_ARGS (		int32_t, grade,		const struct student *, student_info		),	TP_FIELDS (		ctf_string(name, student_info->name)		ctf_integer(int32_t, grade, grade)		ctf_integer(int32_t, age, student_info->age)		ctf_float(float, score, student_info->score)		))TRACEPOINT_EVENT_INSTANCE(	self_lttng_ust,	self_lttng_class,	main_tracepoint,	TP_ARGS (		int32_t, grade,		const struct student *, student_info		))TRACEPOINT_EVENT_INSTANCE(	self_lttng_ust,	self_lttng_class,	main_tracepoint2,	TP_ARGS (		int32_t, grade,		const struct student *, student_info		))TRACEPOINT_EVENT_INSTANCE(	self_lttng_ust,	self_lttng_class,	main_tracepoint3,	TP_ARGS (		int32_t, grade,		const struct student *, student_info		))#endif#include <lttng/tracepoint-event.h>
mkdir build;cmake ..make./self-ust[root@ceph-1 tracing]# lttng create my-user-space-sessionSession my-user-space-session created.Traces will be written in /root/lttng-traces/my-user-space-session-20230623-115058[root@ceph-1 tracing]# lttng enable-event --userspace self_lttng_ust:main_tracepoin*UST event self_lttng_ust:main_tracepoin* created in channel channel0[root@ceph-1 tracing]# lttng startTracing started for session my-user-space-session[root@ceph-1 tracing]# lttng stopWaiting for data availability.Tracing stopped for session my-user-space-session[root@ceph-1 tracing]# lttng destroySession my-user-space-session destroyedbabeltrace2 ~/lttng-traces/my-user-space-session*

可以看到效果是一样的,就是简化了,tracepoint provider的写法

版权声明:内容来源于互联网和用户投稿 如有侵权请联系删除

本文地址:http://0561fc.cn/211947.html