Skip to content

Commit 45d84d1

Browse files
committed
Added option to re-open files to poller.
Some times a sysfs/debug fs will only generate a value on open. Subsequent seek/read will not vield any new values. This patch adds the option to reopen all files on each read.
1 parent 5670e57 commit 45d84d1

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

wa/instruments/poller/__init__.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ class FilePoller(Instrument):
5959
Whether or not the poller will be run as root. This should be
6060
used when the file you need to poll can only be accessed by root.
6161
"""),
62+
Parameter('reopen', kind=bool, default=False,
63+
description="""
64+
When enabled files will be re-opened with each read. This is
65+
useful for some sysfs/debugfs entries that only generate a
66+
value when opened.
67+
"""),
6268
]
6369

6470
def validate(self):
@@ -91,13 +97,17 @@ def initialize(self, context):
9197
if self.align_with_ftrace:
9298
marker_option = '-m'
9399
signal.connect(self._adjust_timestamps, signal.AFTER_JOB_OUTPUT_PROCESSED)
94-
self.command = '{} -t {} {} -l {} {} > {} 2>{}'.format(target_poller,
95-
self.sample_interval * 1000,
96-
marker_option,
97-
','.join(self.labels),
98-
' '.join(self.files),
99-
self.target_output_path,
100-
self.target_log_path)
100+
reopen_option = ''
101+
if self.reopen:
102+
reopen_option = '-r'
103+
self.command = '{} {} -t {} {} -l {} {} > {} 2>{}'.format(target_poller,
104+
reopen_option,
105+
self.sample_interval * 1000,
106+
marker_option,
107+
','.join(self.labels),
108+
' '.join(self.files),
109+
self.target_output_path,
110+
self.target_log_path)
101111

102112
def start(self, context):
103113
self.target.kick_off(self.command, as_root=self.as_root)
2.86 MB
Binary file not shown.
2.9 MB
Binary file not shown.

wa/instruments/poller/poller.c

+22-4
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,26 @@ int main(int argc, char ** argv) {
7777
char *labels;
7878
int labelCount = 0;
7979
int should_write_marker = 0;
80+
int reopen_files = 0;
8081
int ret;
8182

82-
static char usage[] = "usage: %s [-h] [-m] [-t INTERVAL] FILE [FILE ...]\n"
83+
static char usage[] = "usage: %s [-h] [-m] [-r] [-t INTERVAL] FILE [FILE ...]\n"
8384
"polls FILE(s) every INTERVAL microseconds and outputs\n"
8485
"the results in CSV format including a timestamp to STDOUT\n"
8586
"\n"
8687
" -h Display this message\n"
8788
" -m Insert a marker into ftrace at the time of the first\n"
8889
" sample. This marker may be used to align the timestamps\n"
8990
" produced by the poller with those of ftrace events.\n"
91+
" -r Reopen files on each read (needed for some sysfs/debugfs files)\n"
9092
" -t The polling sample interval in microseconds\n"
9193
" Defaults to 1000000 (1 second)\n"
9294
" -l Comma separated list of labels to use in the CSV\n"
9395
" output. This should match the number of files\n";
9496

9597

9698
//Handling command line arguments
97-
while ((c = getopt(argc, argv, "hmt:l:")) != -1)
99+
while ((c = getopt(argc, argv, "hmrt:l:")) != -1)
98100
{
99101
switch(c) {
100102
case 'h':
@@ -104,7 +106,10 @@ int main(int argc, char ** argv) {
104106
break;
105107
case 'm':
106108
should_write_marker = 1;
107-
break;
109+
break;
110+
case 'r':
111+
reopen_files = 1;
112+
break;
108113
case 't':
109114
interval = (useconds_t)atoi(optarg);
110115
break;
@@ -184,7 +189,20 @@ int main(int argc, char ** argv) {
184189
time_float += ((double)current_time.tv_nsec)/1000/1000/1000;
185190
printf("%f", time_float);
186191
for (i = 0; i < num_files; i++) {
187-
lseek(files_to_poll[i].fd, 0, SEEK_SET);
192+
if (reopen_files) {
193+
// Close and reopen the file to get fresh data
194+
close(files_to_poll[i].fd);
195+
files_to_poll[i].fd = open(files_to_poll[i].path, O_RDONLY);
196+
if (files_to_poll[i].fd == -1) {
197+
fprintf(stderr, "WARNING: Could not reopen \"%s\", got: %s\n",
198+
files_to_poll[i].path, strerror(errno));
199+
printf(",");
200+
continue;
201+
}
202+
} else {
203+
lseek(files_to_poll[i].fd, 0, SEEK_SET);
204+
}
205+
188206
bytes_read = read(files_to_poll[i].fd, buf, 1024);
189207

190208
if (bytes_read < 0) {

0 commit comments

Comments
 (0)