Timer creation makes powertop stuck with high CPU usage for a noticeable
     amount of time, due to /proc/timer_stats file parsing.
    
     Move member is_deferred() to static timer_is_deferred(const char*) 
function,
     which gets called during timer() construction.
    
     is_deferred() function returns member bool deferred variable instead,
     preventing CPU consuming file lookup.

Signed-off-by: Sergey Senozhatsky <sergey.senozhat...@gmail.com>

---
 process/timer.cpp |   45 +++++++++++++++++++++++++--------------------
 process/timer.h   |    1 +
 2 files changed, 26 insertions(+), 20 deletions(-)

diff --git a/process/timer.cpp b/process/timer.cpp
index bb7f459..8917490 100644
--- a/process/timer.cpp
+++ b/process/timer.cpp
@@ -35,11 +35,35 @@
 
 using namespace std;
 
+static bool timer_is_deferred(const char *handler)
+{
+       FILE    *file;
+       bool    ret = false;
+       char    line[4096];
+
+       file = fopen("/proc/timer_stats", "r"); 
+       if (!file) {
+               return ret;
+       }
+
+       while (!feof(file)) {
+               if (fgets(line, 4096, file) == NULL)
+                       break;
+               if (strstr(line, handler)) {
+                       ret = (strstr(line, "D,") != NULL);
+                       if (ret == true)
+                               break;
+               }
+       }
+       fclose(file);
+       return ret;
+}
 
 timer::timer(unsigned long address) : power_consumer()
 {
        strncpy(handler, kernel_function(address), 31);
        raw_count = 0;
+       deferred = timer_is_deferred(handler);
 }
 
 
@@ -127,24 +151,5 @@ void clear_timers(void)
 
 bool timer::is_deferred(void)
 {
-       FILE    *file;
-       bool    ret = false;
-       char    line[4096];
-
-       file = fopen("/proc/timer_stats", "r"); 
-       if (!file) {
-               return ret;
-       }
-
-       while (!feof(file)) {
-               if (fgets(line, 4096, file) == NULL)
-                       break;
-               if (strstr(line, handler)) {
-                       ret = (strstr(line, "D,") != NULL);
-                       if (ret == true)
-                               break;
-               }
-       }
-       fclose(file);
-       return ret;
+       return deferred;
 }
diff --git a/process/timer.h b/process/timer.h
index 880fa26..7718c3b 100644
--- a/process/timer.h
+++ b/process/timer.h
@@ -34,6 +34,7 @@ class timer : public power_consumer {
 public:
        char            handler[32];
        int             raw_count;
+       bool            deferred;
 
        timer(unsigned long timer_func);
 

_______________________________________________
Power mailing list
Power@bughost.org
https://bughost.org/mailman/listinfo/power

Reply via email to