LunaSysMgr
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MemoryMonitor.h
Go to the documentation of this file.
1 /* @@@LICENSE
2 *
3 * Copyright (c) 2010-2012 Hewlett-Packard Development Company, L.P.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * LICENSE@@@ */
18 
19 
20 
21 
22 #ifndef MEMORYMONITOR_H
23 #define MEMORYMONITOR_H
24 
25 #include "Common.h"
26 
27 #include <stdint.h>
28 #include <map>
29 #include <QObject>
30 
31 #include "Timer.h"
32 #include "Mutex.h"
33 
34 #if defined(HAS_MEMCHUTE)
35 extern "C" {
36 #include <memchute.h>
37 }
38 #endif
39 
40 class MemoryMonitor : public QObject
41 {
42  Q_OBJECT
43 
44 public:
45 
46  enum MemState {
47  Normal = 0,
49  Low,
51  };
52 
53  static MemoryMonitor* instance();
54 
55  void start();
56 
57  MemState state() const { return m_state; }
58 
59  bool allowNewNativeAppLaunch(int appMemoryRequirement); // appMemoryRequirement in MB
60 
61  void monitorNativeProcessMemory(pid_t pid, int maxMemAllowed, pid_t updateFromPid = 0);
62 
63  bool getMemInfo(int& lowMemoryEntryRem, int& criticalMemoryEntryRem, int& rebootMemoryEntryRem);
64 
65 Q_SIGNALS:
66 
67  void memoryStateChanged(bool critical);
68 
69 private:
70 
71  MemoryMonitor();
72  ~MemoryMonitor();
73 
74  bool timerTicked();
75  int getCurrentRssUsage() const;
76 
77  int getProcessMemInfo(pid_t pid);
78 
79  void adjustOomScore();
80 
81 #if defined(HAS_MEMCHUTE)
82  static void memchuteCallback(MemchuteThreshold threshold);
83  void memchuteStateChanged();
84  int getMonitoredProcessesMemoryOffset();
85  void checkMonitoredProcesses();
86 #endif
87 
88 private:
89 
90  Timer<MemoryMonitor> m_timer;
91  int m_currRssUsage;
92 
93  static const int kFileNameLen = 128;
94  char m_fileName[kFileNameLen];
95 
96  MemState m_state;
97 
98 #if defined(HAS_MEMCHUTE)
99  MemchuteWatcher* m_memWatch;
100 
101  typedef struct
102  {
103  pid_t pid;
104  int maxMemAllowed;
105  int violationNumber;
106  } ProcMemMonitor;
107 
108  typedef std::map<pid_t, ProcMemMonitor*> ProcMemRestrictions;
109 
110  ProcMemRestrictions memRestrict;
111 
112 #endif
113 
114 
115 };
116 
117 #endif /* MEMORYMONITOR_H */
118