On Tue, Apr 5, 2011 at 8:49 PM, Daniel Lezcano <daniel.lezc...@free.fr>wrote:

> On 04/05/2011 10:28 AM, yong.s...@linaro.org wrote:
>
>> From: Yong Shen<yong.s...@linaro.org>
>>
>> Signed-off-by: Yong Shen<yong.s...@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;
>>
>
> Thats wrong, you have to add the inotify_wd to the mainloop code.
>
> Index: dlezcano/work/src/powerdebug/powerdebug.c
> ===================================================================
> --- dlezcano.orig/work/src/powerdebug/powerdebug.c    2011-04-05
> 14:38:40.437379003 +0200
> +++ dlezcano/work/src/powerdebug/powerdebug.c    2011-04-05
> 14:42:48.397379003 +0200
> @@ -291,16 +291,21 @@ int mainloop(struct powerdebug_options *
>
>         FD_ZERO(&readfds);
>         FD_SET(0, &readfds);
> +        FD_SET(inotify_fd, &readfds);
>         tval.tv_sec = options->ticktime;
>         tval.tv_usec = (options->ticktime - tval.tv_sec) * 1000000;
>
> -        key = select(1, &readfds, NULL, NULL, &tval);
> +        key = select(inotify_fd + 1, &readfds, NULL, NULL, &tval);
>         if (!key)
>             continue;
>
> -        if (keystroke_callback(&enter_hit, &findparent_ncurses,
> -                       clkname_str, &refreshwin, options))
> -            break;
> +        if (FD_ISSET(0, &readfds))
> +            if (keystroke_callback(&enter_hit, &findparent_ncurses,
> +                           clkname_str, &refreshwin, options))
> +                break;
> +
> +        if (FD_ISSET(inotify_fd, &readfds))
> +            thecallback();
>
>
>     }
>
>
>         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);
>>
>
> I would suggest you create a clock_info structure where you store the
> inotify_fd and inotify_wd and you use a clock_fini function.
>
> struct clock_info *clock_info;
>
> ...
>
> clock_info = clock_init();
>
> ...
>
>
> clock_fini(clock_info);
>
> ...
>
> That will prevent to add more global variables and extern definitions.
>
I agree to some degree.
The situation is that we are trying to finish some certain working items in
this cycle, so time is tight. About those polishing tasks, I suggest we
could do it after the major functions have been implemented, like what you
did, a serial patches dedicated for such purpose.
_______________________________________________
linaro-dev mailing list
linaro-dev@lists.linaro.org
http://lists.linaro.org/mailman/listinfo/linaro-dev

Reply via email to