Re: [PATCH idlestat] Add -o option to save output report to a file

2014-08-20 Thread Pi-Cheng Chen
On 21 August 2014 09:43, Daniel Lezcano  wrote:
> On 08/19/2014 09:22 AM, pi-cheng.chen wrote:
>>
>> Currently the serial terminal connected to the boards running idlestat are
>> restricted to be at least 80 characters wide to output the report.
>> Otherwise
>> idlestat quits with message "The terminal must be at least 80 columns
>> wide".
>>
>> Fix it by adding a "-o" option to save report output to a file.
>
>
> Yes, but please do it in the unix way.
>
> 1. open file
> 2. close stdout
> 3. dup[2] opened file to stdout (fd 1)
> 4. close opened file
>
> So no need to change all the code around.
>
> Thanks
>   -- Daniel

Thanks for your comments.
I'll do it in next version.

Pi-Cheng

___
linaro-dev mailing list
linaro-dev@lists.linaro.org
http://lists.linaro.org/mailman/listinfo/linaro-dev


Re: [PATCH idlestat] Add -o option to save output report to a file

2014-08-20 Thread Daniel Lezcano

On 08/19/2014 09:22 AM, pi-cheng.chen wrote:

Currently the serial terminal connected to the boards running idlestat are
restricted to be at least 80 characters wide to output the report. Otherwise
idlestat quits with message "The terminal must be at least 80 columns wide".

Fix it by adding a "-o" option to save report output to a file.


Yes, but please do it in the unix way.

1. open file
2. close stdout
3. dup[2] opened file to stdout (fd 1)
4. close opened file

So no need to change all the code around.

Thanks
  -- Daniel


Signed-off-by: Pi-Cheng Chen 
---
  idlestat.c | 136 -
  idlestat.h |   1 +
  2 files changed, 90 insertions(+), 47 deletions(-)

diff --git a/idlestat.c b/idlestat.c
index bba8951..fb0ae8f 100644
--- a/idlestat.c
+++ b/idlestat.c
@@ -51,6 +51,7 @@
  #define USEC_PER_SEC 100

  static char buffer[BUFSIZE];
+static FILE *output;

  static inline int error(const char *str)
  {
@@ -68,19 +69,40 @@ static void charrep(char c, int count)
  {
int i;
for (i = 0; i < count; i++)
-   printf("%c", c);
+   fprintf(output, "%c", c);
+}
+
+static int open_report_file(const char *path)
+{
+   if (path) {
+   output = fopen(path, "w+");
+
+   if (!output) {
+   fprintf(stderr, "%s: failed to open '%s'\n", __func__, 
path);
+   return -1;
+   }
+   } else
+   output = stdout;
+
+   return 0;
+}
+
+static void close_report_file(void)
+{
+   if (output != stdout)
+   fclose(output);
  }

  static void display_cpu_header(char *cpu, int length)
  {
charrep('-', length);
-   printf("\n");
+   fprintf(output, "\n");

if (strstr(cpu, "cluster"))
-   printf("| %-*s |\n", length - 4, cpu);
+   fprintf(output, "| %-*s |\n", length - 4, cpu);
else if (strstr(cpu, "core"))
-   printf("|  %-*s |\n", length - 9, cpu);
-   else printf("| %-*s |\n", length - 16, cpu);
+   fprintf(output, "|  %-*s |\n", length - 9, cpu);
+   else fprintf(output, "| %-*s |\n", length - 16, cpu);
  }

  static void display_factored_time(double time, int align)
@@ -89,15 +111,15 @@ static void display_factored_time(double time, int align)

if (time < 1000) {
sprintf(buffer, "%.0lfus", time);
-   printf("%*s", align, buffer);
+   fprintf(output, "%*s", align, buffer);
}
else if (time < 100) {
sprintf(buffer, "%.2lfms", time / 1000.0);
-   printf("%*s", align, buffer);
+   fprintf(output, "%*s", align, buffer);
}
else {
sprintf(buffer, "%.2lfs", time / 100.0);
-   printf("%*s", align, buffer);
+   fprintf(output, "%*s", align, buffer);
}
  }

@@ -107,28 +129,29 @@ static void display_factored_freq(int freq, int align)

if (freq < 1000) {
sprintf(buffer, "%dHz", freq);
-   printf("%*s", align, buffer);
+   fprintf(output, "%*s", align, buffer);
} else if (freq < 100) {
sprintf(buffer, "%.2fMHz", (float)freq / 1000.0);
-   printf("%*s", align, buffer);
+   fprintf(output, "%*s", align, buffer);
} else {
sprintf(buffer, "%.2fGHz", (float)freq / 100.0);
-   printf("%*s", align, buffer);
+   fprintf(output, "%*s", align, buffer);
}
  }

  static void display_cstates_header(void)
  {
charrep('-', 80);
-   printf("\n");
+   fprintf(output, "\n");

-   printf("| C-state  |   min|   max|   avg|   total  | hits  |  
over | under |\n");
+   fprintf(output, "| C-state  |   min|   max|   avg|   total"
+   "  | hits  |  over | under |\n");
  }

  static void display_cstates_footer(void)
  {
charrep('-', 80);
-   printf("\n\n");
+   fprintf(output, "\n\n");
  }

  static int display_cstates(void *arg, char *cpu)
@@ -148,23 +171,23 @@ static int display_cstates(void *arg, char *cpu)
display_cpu_header(cpu, 80);
cpu_header = true;
charrep('-', 80);
-   printf("\n");
+   fprintf(output, "\n");
}

-   printf("| %8s | ", c->name);
+   fprintf(output, "| %8s | ", c->name);
display_factored_time(c->min_time == DBL_MAX ? 0. :
  c->min_time, 8);
-   printf(" | ");
+   fprintf(output, " | ");
display_factored_time(c->max_time, 8);
-   printf(" | ");
+   fprintf(output, " | ");
display_factored_time(c->avg_time, 8);
-  

[PATCH idlestat] Add -o option to save output report to a file

2014-08-19 Thread pi-cheng.chen
Currently the serial terminal connected to the boards running idlestat are 
restricted to be at least 80 characters wide to output the report. Otherwise
idlestat quits with message "The terminal must be at least 80 columns wide".

Fix it by adding a "-o" option to save report output to a file. 

Signed-off-by: Pi-Cheng Chen 
---
 idlestat.c | 136 -
 idlestat.h |   1 +
 2 files changed, 90 insertions(+), 47 deletions(-)

diff --git a/idlestat.c b/idlestat.c
index bba8951..fb0ae8f 100644
--- a/idlestat.c
+++ b/idlestat.c
@@ -51,6 +51,7 @@
 #define USEC_PER_SEC 100
 
 static char buffer[BUFSIZE];
+static FILE *output;
 
 static inline int error(const char *str)
 {
@@ -68,19 +69,40 @@ static void charrep(char c, int count)
 {
int i;
for (i = 0; i < count; i++)
-   printf("%c", c);
+   fprintf(output, "%c", c);
+}
+
+static int open_report_file(const char *path)
+{
+   if (path) {
+   output = fopen(path, "w+");
+
+   if (!output) {
+   fprintf(stderr, "%s: failed to open '%s'\n", __func__, 
path);
+   return -1;
+   }
+   } else
+   output = stdout;
+
+   return 0;
+}
+
+static void close_report_file(void)
+{
+   if (output != stdout)
+   fclose(output);
 }
 
 static void display_cpu_header(char *cpu, int length)
 {
charrep('-', length);
-   printf("\n");
+   fprintf(output, "\n");
 
if (strstr(cpu, "cluster"))
-   printf("| %-*s |\n", length - 4, cpu);
+   fprintf(output, "| %-*s |\n", length - 4, cpu);
else if (strstr(cpu, "core"))
-   printf("|  %-*s |\n", length - 9, cpu);
-   else printf("| %-*s |\n", length - 16, cpu);
+   fprintf(output, "|  %-*s |\n", length - 9, cpu);
+   else fprintf(output, "| %-*s |\n", length - 16, cpu);
 }
 
 static void display_factored_time(double time, int align)
@@ -89,15 +111,15 @@ static void display_factored_time(double time, int align)
 
if (time < 1000) {
sprintf(buffer, "%.0lfus", time);
-   printf("%*s", align, buffer);
+   fprintf(output, "%*s", align, buffer);
}
else if (time < 100) {
sprintf(buffer, "%.2lfms", time / 1000.0);
-   printf("%*s", align, buffer);
+   fprintf(output, "%*s", align, buffer);
}
else {
sprintf(buffer, "%.2lfs", time / 100.0);
-   printf("%*s", align, buffer);
+   fprintf(output, "%*s", align, buffer);
}
 }
 
@@ -107,28 +129,29 @@ static void display_factored_freq(int freq, int align)
 
if (freq < 1000) {
sprintf(buffer, "%dHz", freq);
-   printf("%*s", align, buffer);
+   fprintf(output, "%*s", align, buffer);
} else if (freq < 100) {
sprintf(buffer, "%.2fMHz", (float)freq / 1000.0);
-   printf("%*s", align, buffer);
+   fprintf(output, "%*s", align, buffer);
} else {
sprintf(buffer, "%.2fGHz", (float)freq / 100.0);
-   printf("%*s", align, buffer);
+   fprintf(output, "%*s", align, buffer);
}
 }
 
 static void display_cstates_header(void)
 {
charrep('-', 80);
-   printf("\n");
+   fprintf(output, "\n");
 
-   printf("| C-state  |   min|   max|   avg|   total  | hits  
|  over | under |\n");
+   fprintf(output, "| C-state  |   min|   max|   avg|   total"
+   "  | hits  |  over | under |\n");
 }
 
 static void display_cstates_footer(void)
 {
charrep('-', 80);
-   printf("\n\n");
+   fprintf(output, "\n\n");
 }
 
 static int display_cstates(void *arg, char *cpu)
@@ -148,23 +171,23 @@ static int display_cstates(void *arg, char *cpu)
display_cpu_header(cpu, 80);
cpu_header = true;
charrep('-', 80);
-   printf("\n");
+   fprintf(output, "\n");
}
 
-   printf("| %8s | ", c->name);
+   fprintf(output, "| %8s | ", c->name);
display_factored_time(c->min_time == DBL_MAX ? 0. :
  c->min_time, 8);
-   printf(" | ");
+   fprintf(output, " | ");
display_factored_time(c->max_time, 8);
-   printf(" | ");
+   fprintf(output, " | ");
display_factored_time(c->avg_time, 8);
-   printf(" | ");
+   fprintf(output, " | ");
display_factored_time(c->duration, 8);
-   printf(" | ");
-   printf("%5d | %5d | %5d |", c->nrdata,
+   fprintf(output, " | ");