Skip to content

Commit 1bd13ed

Browse files
mhiramatrostedt
authored andcommitted
tracing/hist: Add poll(POLLIN) support on hist file
Add poll syscall support on the `hist` file. The Waiter will be waken up when the histogram is updated with POLLIN. Currently, there is no way to wait for a specific event in userspace. So user needs to peek the `trace` periodicaly, or wait on `trace_pipe`. But it is not a good idea to peek at the `trace` for an event that randomly happens. And `trace_pipe` is not coming back until a page is filled with events. This allows a user to wait for a specific event on the `hist` file. User can set a histogram trigger on the event which they want to monitor and poll() on its `hist` file. Since this poll() returns POLLIN, the next poll() will return soon unless a read() happens on that hist file. NOTE: To read the hist file again, you must set the file offset to 0, but just for monitoring the event, you may not need to read the histogram. Cc: Shuah Khan <shuah@kernel.org> Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Link: https://lore.kernel.org/173527247756.464571.14236296701625509931.stgit@devnote2 Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> Reviewed-by: Tom Zanussi <zanussi@kernel.org> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
1 parent 22bec11 commit 1bd13ed

File tree

3 files changed

+95
-3
lines changed

3 files changed

+95
-3
lines changed

include/linux/trace_events.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,20 @@ struct trace_event_file {
673673
atomic_t tm_ref; /* trigger-mode reference counter */
674674
};
675675

676+
#ifdef CONFIG_HIST_TRIGGERS
677+
extern struct irq_work hist_poll_work;
678+
extern wait_queue_head_t hist_poll_wq;
679+
680+
static inline void hist_poll_wakeup(void)
681+
{
682+
if (wq_has_sleeper(&hist_poll_wq))
683+
irq_work_queue(&hist_poll_work);
684+
}
685+
686+
#define hist_poll_wait(file, wait) \
687+
poll_wait(file, &hist_poll_wq, wait)
688+
#endif
689+
676690
#define __TRACE_EVENT_FLAGS(name, value) \
677691
static int __init trace_init_flags_##name(void) \
678692
{ \

kernel/trace/trace_events.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3094,6 +3094,20 @@ static bool event_in_systems(struct trace_event_call *call,
30943094
return !*p || isspace(*p) || *p == ',';
30953095
}
30963096

3097+
#ifdef CONFIG_HIST_TRIGGERS
3098+
/*
3099+
* Wake up waiter on the hist_poll_wq from irq_work because the hist trigger
3100+
* may happen in any context.
3101+
*/
3102+
static void hist_poll_event_irq_work(struct irq_work *work)
3103+
{
3104+
wake_up_all(&hist_poll_wq);
3105+
}
3106+
3107+
DEFINE_IRQ_WORK(hist_poll_work, hist_poll_event_irq_work);
3108+
DECLARE_WAIT_QUEUE_HEAD(hist_poll_wq);
3109+
#endif
3110+
30973111
static struct trace_event_file *
30983112
trace_create_new_event(struct trace_event_call *call,
30993113
struct trace_array *tr)

kernel/trace/trace_events_hist.c

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5311,6 +5311,8 @@ static void event_hist_trigger(struct event_trigger_data *data,
53115311

53125312
if (resolve_var_refs(hist_data, key, var_ref_vals, true))
53135313
hist_trigger_actions(hist_data, elt, buffer, rec, rbe, key, var_ref_vals);
5314+
5315+
hist_poll_wakeup();
53145316
}
53155317

53165318
static void hist_trigger_stacktrace_print(struct seq_file *m,
@@ -5590,43 +5592,105 @@ static void hist_trigger_show(struct seq_file *m,
55905592
n_entries, (u64)atomic64_read(&hist_data->map->drops));
55915593
}
55925594

5595+
struct hist_file_data {
5596+
struct file *file;
5597+
u64 last_read;
5598+
};
5599+
5600+
static u64 get_hist_hit_count(struct trace_event_file *event_file)
5601+
{
5602+
struct hist_trigger_data *hist_data;
5603+
struct event_trigger_data *data;
5604+
u64 ret = 0;
5605+
5606+
list_for_each_entry(data, &event_file->triggers, list) {
5607+
if (data->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5608+
hist_data = data->private_data;
5609+
ret += atomic64_read(&hist_data->map->hits);
5610+
}
5611+
}
5612+
return ret;
5613+
}
5614+
55935615
static int hist_show(struct seq_file *m, void *v)
55945616
{
5617+
struct hist_file_data *hist_file = m->private;
55955618
struct event_trigger_data *data;
55965619
struct trace_event_file *event_file;
55975620
int n = 0;
55985621

55995622
guard(mutex)(&event_mutex);
56005623

5601-
event_file = event_file_file(m->private);
5624+
event_file = event_file_file(hist_file->file);
56025625
if (unlikely(!event_file))
56035626
return -ENODEV;
56045627

56055628
list_for_each_entry(data, &event_file->triggers, list) {
56065629
if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
56075630
hist_trigger_show(m, data, n++);
56085631
}
5632+
hist_file->last_read = get_hist_hit_count(event_file);
5633+
56095634
return 0;
56105635
}
56115636

5637+
static __poll_t event_hist_poll(struct file *file, struct poll_table_struct *wait)
5638+
{
5639+
struct trace_event_file *event_file;
5640+
struct seq_file *m = file->private_data;
5641+
struct hist_file_data *hist_file = m->private;
5642+
5643+
guard(mutex)(&event_mutex);
5644+
5645+
event_file = event_file_data(file);
5646+
if (!event_file)
5647+
return EPOLLERR;
5648+
5649+
hist_poll_wait(file, wait);
5650+
5651+
if (hist_file->last_read != get_hist_hit_count(event_file))
5652+
return EPOLLIN | EPOLLRDNORM;
5653+
5654+
return 0;
5655+
}
5656+
5657+
static int event_hist_release(struct inode *inode, struct file *file)
5658+
{
5659+
struct seq_file *m = file->private_data;
5660+
struct hist_file_data *hist_file = m->private;
5661+
5662+
kfree(hist_file);
5663+
return tracing_single_release_file_tr(inode, file);
5664+
}
5665+
56125666
static int event_hist_open(struct inode *inode, struct file *file)
56135667
{
5668+
struct hist_file_data *hist_file;
56145669
int ret;
56155670

56165671
ret = tracing_open_file_tr(inode, file);
56175672
if (ret)
56185673
return ret;
56195674

5675+
hist_file = kzalloc(sizeof(*hist_file), GFP_KERNEL);
5676+
if (!hist_file)
5677+
return -ENOMEM;
5678+
hist_file->file = file;
5679+
56205680
/* Clear private_data to avoid warning in single_open() */
56215681
file->private_data = NULL;
5622-
return single_open(file, hist_show, file);
5682+
ret = single_open(file, hist_show, hist_file);
5683+
if (ret)
5684+
kfree(hist_file);
5685+
return ret;
56235686
}
56245687

56255688
const struct file_operations event_hist_fops = {
56265689
.open = event_hist_open,
56275690
.read = seq_read,
56285691
.llseek = seq_lseek,
5629-
.release = tracing_single_release_file_tr,
5692+
.release = event_hist_release,
5693+
.poll = event_hist_poll,
56305694
};
56315695

56325696
#ifdef CONFIG_HIST_TRIGGERS_DEBUG

0 commit comments

Comments
 (0)