Skip to content

Commit 2e33af0

Browse files
Steven Rostedtrostedt
authored andcommitted
tracing: Move fields from event to class structure
Move the defined fields from the event to the class structure. Since the fields of the event are defined by the class they belong to, it makes sense to have the class hold the information instead of the individual events. The events of the same class would just hold duplicate information. After this change the size of the kernel dropped another 3K: text data bss dec hex filename 4913961 1088356 861512 6863829 68bbd5 vmlinux.orig 4900252 1057412 861512 6819176 680d68 vmlinux.regs 4900375 1053380 861512 6815267 67fe23 vmlinux.fields Although the text increased, this was mainly due to the C files having to adapt to the change. This is a constant increase, where new tracepoints will not increase the Text. But the big drop is in the data size (as well as needed allocations to hold the fields). This will give even more savings as more tracepoints are created. Note, if just TRACE_EVENT()s are used and not DECLARE_EVENT_CLASS() with several DEFINE_EVENT()s, then the savings will be lost. But we are pushing developers to consolidate events with DEFINE_EVENT() so this should not be an issue. The kprobes define a unique class to every new event, but are dynamic so it should not be a issue. The syscalls however have a single class but the fields for the individual events are different. The syscalls use a metadata to define the fields. I moved the fields list from the event to the metadata and added a "get_fields()" function to the class. This function is used to find the fields. For normal events and kprobes, get_fields() just returns a pointer to the fields list_head in the class. For syscall events, it returns the fields list_head in the metadata for the event. v2: Fixed the syscall fields. The syscall metadata needs a list of fields for both enter and exit. Acked-by: Frederic Weisbecker <fweisbec@gmail.com> Acked-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Acked-by: Masami Hiramatsu <mhiramat@redhat.com> Cc: Tom Zanussi <tzanussi@gmail.com> Cc: Peter Zijlstra <peterz@infradead.org> Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
1 parent 2239291 commit 2e33af0

10 files changed

Lines changed: 102 additions & 46 deletions

File tree

‎include/linux/ftrace_event.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ struct ftrace_event_class {
130130
#endif
131131
int (*reg)(struct ftrace_event_call *event,
132132
enum trace_reg type);
133+
int (*define_fields)(struct ftrace_event_call *);
134+
struct list_head *(*get_fields)(struct ftrace_event_call *);
135+
struct list_head fields;
133136
};
134137

135138
struct ftrace_event_call {
@@ -142,8 +145,6 @@ struct ftrace_event_call {
142145
int id;
143146
const char *print_fmt;
144147
int (*raw_init)(struct ftrace_event_call *);
145-
int (*define_fields)(struct ftrace_event_call *);
146-
struct list_head fields;
147148
int filter_active;
148149
struct event_filter *filter;
149150
void *mod;

‎include/linux/syscalls.h‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ extern struct ftrace_event_class event_class_syscall_enter;
122122
extern struct ftrace_event_class event_class_syscall_exit;
123123

124124
#define SYSCALL_TRACE_ENTER_EVENT(sname) \
125-
static const struct syscall_metadata __syscall_meta_##sname; \
125+
static struct syscall_metadata __syscall_meta_##sname; \
126126
static struct ftrace_event_call \
127127
__attribute__((__aligned__(4))) event_enter_##sname; \
128128
static struct trace_event enter_syscall_print_##sname = { \
@@ -136,12 +136,11 @@ extern struct ftrace_event_class event_class_syscall_exit;
136136
.class = &event_class_syscall_enter, \
137137
.event = &enter_syscall_print_##sname, \
138138
.raw_init = init_syscall_trace, \
139-
.define_fields = syscall_enter_define_fields, \
140139
.data = (void *)&__syscall_meta_##sname,\
141140
}
142141

143142
#define SYSCALL_TRACE_EXIT_EVENT(sname) \
144-
static const struct syscall_metadata __syscall_meta_##sname; \
143+
static struct syscall_metadata __syscall_meta_##sname; \
145144
static struct ftrace_event_call \
146145
__attribute__((__aligned__(4))) event_exit_##sname; \
147146
static struct trace_event exit_syscall_print_##sname = { \
@@ -155,14 +154,13 @@ extern struct ftrace_event_class event_class_syscall_exit;
155154
.class = &event_class_syscall_exit, \
156155
.event = &exit_syscall_print_##sname, \
157156
.raw_init = init_syscall_trace, \
158-
.define_fields = syscall_exit_define_fields, \
159157
.data = (void *)&__syscall_meta_##sname,\
160158
}
161159

162160
#define SYSCALL_METADATA(sname, nb) \
163161
SYSCALL_TRACE_ENTER_EVENT(sname); \
164162
SYSCALL_TRACE_EXIT_EVENT(sname); \
165-
static const struct syscall_metadata __used \
163+
static struct syscall_metadata __used \
166164
__attribute__((__aligned__(4))) \
167165
__attribute__((section("__syscalls_metadata"))) \
168166
__syscall_meta_##sname = { \
@@ -172,19 +170,23 @@ extern struct ftrace_event_class event_class_syscall_exit;
172170
.args = args_##sname, \
173171
.enter_event = &event_enter_##sname, \
174172
.exit_event = &event_exit_##sname, \
173+
.enter_fields = LIST_HEAD_INIT(__syscall_meta_##sname.enter_fields), \
174+
.exit_fields = LIST_HEAD_INIT(__syscall_meta_##sname.exit_fields), \
175175
};
176176

177177
#define SYSCALL_DEFINE0(sname) \
178178
SYSCALL_TRACE_ENTER_EVENT(_##sname); \
179179
SYSCALL_TRACE_EXIT_EVENT(_##sname); \
180-
static const struct syscall_metadata __used \
180+
static struct syscall_metadata __used \
181181
__attribute__((__aligned__(4))) \
182182
__attribute__((section("__syscalls_metadata"))) \
183183
__syscall_meta__##sname = { \
184184
.name = "sys_"#sname, \
185185
.nb_args = 0, \
186186
.enter_event = &event_enter__##sname, \
187187
.exit_event = &event_exit__##sname, \
188+
.enter_fields = LIST_HEAD_INIT(__syscall_meta__##sname.enter_fields), \
189+
.exit_fields = LIST_HEAD_INIT(__syscall_meta__##sname.exit_fields), \
188190
}; \
189191
asmlinkage long sys_##sname(void)
190192
#else

‎include/trace/ftrace.h‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,9 @@ static inline notrace int ftrace_get_offsets_##call( \
430430
*
431431
* static struct ftrace_event_class __used event_class_<template> = {
432432
* .system = "<system>",
433+
* .define_fields = ftrace_define_fields_<call>,
434+
* .fields = LIST_HEAD_INIT(event_class_##call.fields), \
435+
* .probe = ftrace_raw_event_##call, \
433436
* };
434437
*
435438
* static struct ftrace_event_call __used
@@ -438,10 +441,8 @@ static inline notrace int ftrace_get_offsets_##call( \
438441
* .name = "<call>",
439442
* .class = event_class_<template>,
440443
* .raw_init = trace_event_raw_init,
441-
* .regfunc = ftrace_raw_reg_event_<call>,
442-
* .unregfunc = ftrace_raw_unreg_event_<call>,
444+
* .event = &ftrace_event_type_<call>,
443445
* .print_fmt = print_fmt_<call>,
444-
* .define_fields = ftrace_define_fields_<call>,
445446
* };
446447
*
447448
*/
@@ -563,6 +564,8 @@ _TRACE_PERF_PROTO(call, PARAMS(proto)); \
563564
static const char print_fmt_##call[] = print; \
564565
static struct ftrace_event_class __used event_class_##call = { \
565566
.system = __stringify(TRACE_SYSTEM), \
567+
.define_fields = ftrace_define_fields_##call, \
568+
.fields = LIST_HEAD_INIT(event_class_##call.fields),\
566569
.probe = ftrace_raw_event_##call, \
567570
_TRACE_PERF_INIT(call) \
568571
};
@@ -578,7 +581,6 @@ __attribute__((section("_ftrace_events"))) event_##call = { \
578581
.event = &ftrace_event_type_##call, \
579582
.raw_init = trace_event_raw_init, \
580583
.print_fmt = print_fmt_##template, \
581-
.define_fields = ftrace_define_fields_##template, \
582584
};
583585

584586
#undef DEFINE_EVENT_PRINT
@@ -594,7 +596,6 @@ __attribute__((section("_ftrace_events"))) event_##call = { \
594596
.event = &ftrace_event_type_##call, \
595597
.raw_init = trace_event_raw_init, \
596598
.print_fmt = print_fmt_##call, \
597-
.define_fields = ftrace_define_fields_##template, \
598599
}
599600

600601
#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)

‎include/trace/syscall.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ struct syscall_metadata {
2525
int nb_args;
2626
const char **types;
2727
const char **args;
28+
struct list_head enter_fields;
29+
struct list_head exit_fields;
2830

2931
struct ftrace_event_call *enter_event;
3032
struct ftrace_event_call *exit_event;
@@ -34,8 +36,6 @@ struct syscall_metadata {
3436
extern unsigned long arch_syscall_addr(int nr);
3537
extern int init_syscall_trace(struct ftrace_event_call *call);
3638

37-
extern int syscall_enter_define_fields(struct ftrace_event_call *call);
38-
extern int syscall_exit_define_fields(struct ftrace_event_call *call);
3939
extern int reg_event_syscall_enter(struct ftrace_event_call *call);
4040
extern void unreg_event_syscall_enter(struct ftrace_event_call *call);
4141
extern int reg_event_syscall_exit(struct ftrace_event_call *call);

‎kernel/trace/trace.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,9 @@ extern void print_subsystem_event_filter(struct event_subsystem *system,
794794
struct trace_seq *s);
795795
extern int filter_assign_type(const char *type);
796796

797+
struct list_head *
798+
trace_get_fields(struct ftrace_event_call *event_call);
799+
797800
static inline int
798801
filter_check_discard(struct ftrace_event_call *call, void *rec,
799802
struct ring_buffer *buffer,

‎kernel/trace/trace_events.c‎

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,23 @@ DEFINE_MUTEX(event_mutex);
2929

3030
LIST_HEAD(ftrace_events);
3131

32+
struct list_head *
33+
trace_get_fields(struct ftrace_event_call *event_call)
34+
{
35+
if (!event_call->class->get_fields)
36+
return &event_call->class->fields;
37+
return event_call->class->get_fields(event_call);
38+
}
39+
3240
int trace_define_field(struct ftrace_event_call *call, const char *type,
3341
const char *name, int offset, int size, int is_signed,
3442
int filter_type)
3543
{
3644
struct ftrace_event_field *field;
45+
struct list_head *head;
46+
47+
if (WARN_ON(!call->class))
48+
return 0;
3749

3850
field = kzalloc(sizeof(*field), GFP_KERNEL);
3951
if (!field)
@@ -56,7 +68,8 @@ int trace_define_field(struct ftrace_event_call *call, const char *type,
5668
field->size = size;
5769
field->is_signed = is_signed;
5870

59-
list_add(&field->link, &call->fields);
71+
head = trace_get_fields(call);
72+
list_add(&field->link, head);
6073

6174
return 0;
6275

@@ -94,8 +107,10 @@ static int trace_define_common_fields(struct ftrace_event_call *call)
94107
void trace_destroy_fields(struct ftrace_event_call *call)
95108
{
96109
struct ftrace_event_field *field, *next;
110+
struct list_head *head;
97111

98-
list_for_each_entry_safe(field, next, &call->fields, link) {
112+
head = trace_get_fields(call);
113+
list_for_each_entry_safe(field, next, head, link) {
99114
list_del(&field->link);
100115
kfree(field->type);
101116
kfree(field->name);
@@ -111,7 +126,6 @@ int trace_event_raw_init(struct ftrace_event_call *call)
111126
if (!id)
112127
return -ENODEV;
113128
call->id = id;
114-
INIT_LIST_HEAD(&call->fields);
115129

116130
return 0;
117131
}
@@ -537,6 +551,7 @@ event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
537551
{
538552
struct ftrace_event_call *call = filp->private_data;
539553
struct ftrace_event_field *field;
554+
struct list_head *head;
540555
struct trace_seq *s;
541556
int common_field_count = 5;
542557
char *buf;
@@ -555,7 +570,8 @@ event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
555570
trace_seq_printf(s, "ID: %d\n", call->id);
556571
trace_seq_printf(s, "format:\n");
557572

558-
list_for_each_entry_reverse(field, &call->fields, link) {
573+
head = trace_get_fields(call);
574+
list_for_each_entry_reverse(field, head, link) {
559575
/*
560576
* Smartly shows the array type(except dynamic array).
561577
* Normal:
@@ -931,6 +947,7 @@ event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
931947
const struct file_operations *filter,
932948
const struct file_operations *format)
933949
{
950+
struct list_head *head;
934951
int ret;
935952

936953
/*
@@ -957,14 +974,21 @@ event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
957974
id);
958975
#endif
959976

960-
if (call->define_fields) {
961-
ret = trace_define_common_fields(call);
962-
if (!ret)
963-
ret = call->define_fields(call);
964-
if (ret < 0) {
965-
pr_warning("Could not initialize trace point"
966-
" events/%s\n", call->name);
967-
return ret;
977+
if (call->class->define_fields) {
978+
/*
979+
* Other events may have the same class. Only update
980+
* the fields if they are not already defined.
981+
*/
982+
head = trace_get_fields(call);
983+
if (list_empty(head)) {
984+
ret = trace_define_common_fields(call);
985+
if (!ret)
986+
ret = call->class->define_fields(call);
987+
if (ret < 0) {
988+
pr_warning("Could not initialize trace point"
989+
" events/%s\n", call->name);
990+
return ret;
991+
}
968992
}
969993
trace_create_file("filter", 0644, call->dir, call,
970994
filter);

‎kernel/trace/trace_events_filter.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,10 @@ static struct ftrace_event_field *
500500
find_event_field(struct ftrace_event_call *call, char *name)
501501
{
502502
struct ftrace_event_field *field;
503+
struct list_head *head;
503504

504-
list_for_each_entry(field, &call->fields, link) {
505+
head = trace_get_fields(call);
506+
list_for_each_entry(field, head, link) {
505507
if (!strcmp(field->name, name))
506508
return field;
507509
}
@@ -625,7 +627,7 @@ static int init_subsystem_preds(struct event_subsystem *system)
625627
int err;
626628

627629
list_for_each_entry(call, &ftrace_events, list) {
628-
if (!call->define_fields)
630+
if (!call->class || !call->class->define_fields)
629631
continue;
630632

631633
if (strcmp(call->class->system, system->name) != 0)
@@ -644,7 +646,7 @@ static void filter_free_subsystem_preds(struct event_subsystem *system)
644646
struct ftrace_event_call *call;
645647

646648
list_for_each_entry(call, &ftrace_events, list) {
647-
if (!call->define_fields)
649+
if (!call->class || !call->class->define_fields)
648650
continue;
649651

650652
if (strcmp(call->class->system, system->name) != 0)
@@ -1249,7 +1251,7 @@ static int replace_system_preds(struct event_subsystem *system,
12491251
list_for_each_entry(call, &ftrace_events, list) {
12501252
struct event_filter *filter = call->filter;
12511253

1252-
if (!call->define_fields)
1254+
if (!call->class || !call->class->define_fields)
12531255
continue;
12541256

12551257
if (strcmp(call->class->system, system->name) != 0)

‎kernel/trace/trace_export.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@
1818
#undef TRACE_SYSTEM
1919
#define TRACE_SYSTEM ftrace
2020

21-
struct ftrace_event_class event_class_ftrace = {
22-
.system = __stringify(TRACE_SYSTEM),
23-
};
24-
2521
/* not needed for this file */
2622
#undef __field_struct
2723
#define __field_struct(type, item)
@@ -131,7 +127,7 @@ ftrace_define_fields_##name(struct ftrace_event_call *event_call) \
131127

132128
static int ftrace_raw_init_event(struct ftrace_event_call *call)
133129
{
134-
INIT_LIST_HEAD(&call->fields);
130+
INIT_LIST_HEAD(&call->class->fields);
135131
return 0;
136132
}
137133

@@ -159,15 +155,19 @@ static int ftrace_raw_init_event(struct ftrace_event_call *call)
159155
#undef FTRACE_ENTRY
160156
#define FTRACE_ENTRY(call, struct_name, type, tstruct, print) \
161157
\
158+
struct ftrace_event_class event_class_ftrace_##call = { \
159+
.system = __stringify(TRACE_SYSTEM), \
160+
.define_fields = ftrace_define_fields_##call, \
161+
}; \
162+
\
162163
struct ftrace_event_call __used \
163164
__attribute__((__aligned__(4))) \
164165
__attribute__((section("_ftrace_events"))) event_##call = { \
165166
.name = #call, \
166167
.id = type, \
167-
.class = &event_class_ftrace, \
168+
.class = &event_class_ftrace_##call, \
168169
.raw_init = ftrace_raw_init_event, \
169170
.print_fmt = print, \
170-
.define_fields = ftrace_define_fields_##call, \
171171
}; \
172172

173173
#include "trace_entries.h"

‎kernel/trace/trace_kprobe.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,8 +1112,6 @@ static void probe_event_disable(struct ftrace_event_call *call)
11121112

11131113
static int probe_event_raw_init(struct ftrace_event_call *event_call)
11141114
{
1115-
INIT_LIST_HEAD(&event_call->fields);
1116-
11171115
return 0;
11181116
}
11191117

@@ -1362,11 +1360,13 @@ static int register_probe_event(struct trace_probe *tp)
13621360
if (probe_is_return(tp)) {
13631361
tp->event.trace = print_kretprobe_event;
13641362
call->raw_init = probe_event_raw_init;
1365-
call->define_fields = kretprobe_event_define_fields;
1363+
INIT_LIST_HEAD(&call->class->fields);
1364+
call->class->define_fields = kretprobe_event_define_fields;
13661365
} else {
13671366
tp->event.trace = print_kprobe_event;
13681367
call->raw_init = probe_event_raw_init;
1369-
call->define_fields = kprobe_event_define_fields;
1368+
INIT_LIST_HEAD(&call->class->fields);
1369+
call->class->define_fields = kprobe_event_define_fields;
13701370
}
13711371
if (set_print_fmt(tp) < 0)
13721372
return -ENOMEM;

0 commit comments

Comments
 (0)