From: Yong Shen yong.shen@linaro.org
Signed-off-by: Yong Shen yong.shen@linaro.org --- clocks.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++- powerdebug.c | 3 +++ powerdebug.h | 3 +++ 3 files changed, 59 insertions(+), 1 deletions(-)
diff --git a/clocks.c b/clocks.c index 47881c5..a4c54b0 100644 --- a/clocks.c +++ b/clocks.c @@ -16,12 +16,18 @@ #include <stdio.h> #include <mntent.h> #include <sys/stat.h> +#include <sys/inotify.h> +#include <poll.h>
#include "powerdebug.h" #include "clocks.h"
#define MAX_LINES 120
+int inotify_fd; +int inotify_wd; +static struct pollfd fds; + static char clk_dir_path[PATH_MAX]; static int bold[MAX_LINES]; static char clock_lines[MAX_LINES][128]; @@ -58,11 +64,56 @@ int clock_init(void) if (locate_debugfs(clk_dir_path)) return -1;
+ inotify_fd = inotify_init(); + if ( inotify_fd < 0 ) { + fprintf(stderr, "inotify_init error.\n" ); + return -1; + } + + inotify_wd = inotify_add_watch(inotify_fd, clk_dir_path, + IN_ALL_EVENTS ); + + fds.fd = inotify_fd; + fds.events = POLLIN; + sprintf(clk_dir_path, "%s/clock", clk_dir_path);
return access(clk_dir_path, F_OK); }
+#define EVENT_SIZE ( sizeof (struct inotify_event) ) +#define BUF_LEN ( 10 * ( EVENT_SIZE + 16 ) ) + +int debugfs_changed(void) +{ + int length, i = 0; + char buffer[BUF_LEN]; + + if (inotify_fd <= 0) { + return 1; + } + + poll(&fds, 1, 1); + if (fds.revents != POLLIN) { + return 0; + } + + length = read(inotify_fd, buffer, BUF_LEN); + + if (length < 0) + return 0; + + while (i < length) { + struct inotify_event *event = (struct inotify_event *) &buffer[i]; + if (event->mask & IN_ALL_EVENTS) + return 1; + + i += EVENT_SIZE + event->len; + } + + return 0; +} + static int file_read_from_format(char *file, int *value, const char *format) { FILE *f; @@ -379,7 +430,8 @@ static int clk_number_recursive(char *clk_path)
static int get_clk_number(char *clk_path) { - if ((max_clk_num != 0)) /* no nodes have been added */ + /* no nodes have been added */ + if ((max_clk_num != 0) && (!debugfs_changed())) return max_clk_num; else { max_clk_num = 0; diff --git a/powerdebug.c b/powerdebug.c index a26d16f..621052f 100644 --- a/powerdebug.c +++ b/powerdebug.c @@ -14,6 +14,7 @@ *******************************************************************************/
#include <getopt.h> +#include <sys/inotify.h> #include <stdbool.h> #include "regulator.h" #include "display.h" @@ -390,6 +391,8 @@ int main(int argc, char **argv) powerdebug_display(options, regulators_info, numregulators);
release_all_clk_info_mem(); + inotify_rm_watch(inotify_fd, inotify_wd); + close(inotify_fd);
return ret < 0; } diff --git a/powerdebug.h b/powerdebug.h index f97e3b9..4071eaa 100644 --- a/powerdebug.h +++ b/powerdebug.h @@ -54,3 +54,6 @@ extern void create_selectedwindow(int selectedwindow); struct regulator_info; extern void show_regulator_info(struct regulator_info *reg_info, int nr_reg, int verbose); +extern int inotify_fd; +extern int inotify_wd; +extern int need_refresh(void);