Code cleanup in is_deferred().

- No need to check for !handler, since it's allocated member array
- "total events" substrig search is redundant -- EOF should do the same
- replace strchr() with "D," substring search.

Alos I really don't like fopen()/fclose() syscall storm, fix it later.

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

---

 process/timer.cpp |   35 +++++++++++++----------------------
 1 files changed, 13 insertions(+), 22 deletions(-)

diff --git a/process/timer.cpp b/process/timer.cpp
index 5c1da31..221d6d0 100644
--- a/process/timer.cpp
+++ b/process/timer.cpp
@@ -139,36 +139,27 @@ bool get_timerstats(void)
 
 bool timer::is_deferred(void)
 {
-       FILE *file;
-       char line[4096];
+       FILE    *file;
+       bool    ret = false;
+       char    line[4096];
 
        if (!get_timerstats()){
                return false;
        }
-       file = fopen("/proc/timer_stats", "r");
+       file = fopen("/proc/timer_stats", "r"); 
        if (!file) {
-               return false;
+               return ret;
        }
 
-       while (file && !feof(file)) {
-               char *c;
-               if (fgets(line, 4096,file)== NULL)
-                       break;
-               if (strstr(line, "total events"))
+       while (!feof(file)) {
+               if (fgets(line, 4096, file) == NULL)
                        break;
-               if (!handler)
-                       break;
-               if (strstr(line, handler)){
-                       c = strchr(line, ',');
-                       if (!c)
-                               continue;
-                       c--;
-                       if (*c == 'D') {
-                               fclose(file);
-                               return true;
-                       }
+               if (strstr(line, handler)) {
+                       ret = (strstr(line, "D,") != NULL);
+                       if (ret == true)
+                               break;
                }
        }
        fclose(file);
-       return false;
-}
\ No newline at end of file
+       return ret;
+}

_______________________________________________
Discuss mailing list
Discuss@lesswatts.org
http://lists.lesswatts.org/listinfo/discuss

Reply via email to