tizen beta release
[framework/web/wrt-plugins-common.git] / src / modules / tizen / Cpu / Unit.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 #include <unistd.h>
17 #include <stdio.h>
18 #include <fstream>
19 #include <sstream>
20 #include <pcrecpp.h>
21 #include <Commons/Exception.h>
22 #include "Unit.h"
23
24 namespace {
25 const unsigned int GET_STATS_TIMEOUT = 1; // in seconds
26 const char* STAT_FILE = "/proc/stat";
27 const char* CPU_PATTERN_PREFIX = "^cpu";
28 const char* CPU_PATTERN_POSTFIX = "\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)";
29 }
30
31 namespace WrtDeviceApis {
32 namespace Cpu {
33
34 Unit::Unit(std::size_t index) : m_index(index)
35 {
36     std::ostringstream oss(CPU_PATTERN_PREFIX);
37     if (m_index > 0) {
38         oss << m_index - 1;
39     }
40     oss << CPU_PATTERN_POSTFIX;
41     m_utilizationPattern = oss.str();
42 }
43
44 unsigned int Unit::getUtilizationPercent() const
45 {
46     UtilizationStats usage0 = getUtilization();
47     sleep(GET_STATS_TIMEOUT);
48     UtilizationStats usage1 = getUtilization();
49
50     unsigned sum0 = usage0.m_user + usage0.m_nice + usage0.m_system +
51         usage0.m_idle;
52     unsigned sum1 = usage1.m_user + usage1.m_nice + usage1.m_system +
53         usage1.m_idle;
54
55     unsigned percent =
56         (100 -
57          ((usage1.m_idle -
58            usage0.m_idle) / (double) (sum1 - sum0)) * 100);
59
60     return percent;
61 }
62
63 Unit::UtilizationStats Unit::getUtilization() const
64 {
65     std::ifstream file(STAT_FILE);
66     if (!file) {
67         ThrowMsg(Commons::UnsupportedException,
68                  "Could not open statistics file.");
69     }
70
71     UtilizationStats result;
72     memset(&result, 0, sizeof(UtilizationStats));
73     bool matched = false;
74     std::string line;
75     while (!file.eof() && !!std::getline(file, line)) {
76         if (pcrecpp::RE(m_utilizationPattern.c_str()).FullMatch(
77                 line,
78                 &result.m_user,
79                 &result.m_nice,
80                 &result.m_system,
81                 &result.m_idle))
82         {
83             matched = true;
84             break;
85         }
86     }
87     file.close();
88
89     if (!matched) {
90         ThrowMsg(Commons::UnsupportedException, "Total memory data not found.");
91     }
92
93     return result;
94 }
95
96 } // Cpu
97 } // WrtDeviceApis