webappmanager
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MemoryWatcher.h
Go to the documentation of this file.
1 /* @@@LICENSE
2 *
3 * Copyright (c) 2009-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 MEMORYWATCHER_H
23 #define MEMORYWATCHER_H
24 
25 #include "Common.h"
26 
27 #include <stdint.h>
28 #include <map>
29 
30 #include "SignalSlot.h"
31 #include "Timer.h"
32 
33 #if defined(HAS_MEMCHUTE)
34 extern "C" {
35 #include <memchute.h>
36 }
37 #endif
38 
40 {
41 public:
42 
43  enum MemState {
44  Normal = 0,
46  Low,
48  };
49 
50  static MemoryWatcher* instance();
51 
52  void start();
53 
54  MemState state() const { return m_state; }
55 
56  void doLowMemActions(bool allowExpensive=false);
57 
58  bool allowNewWebAppLaunch();
59 
60  static void dropBufferCaches();
61 
62 private:
63 
64  MemoryWatcher();
65  ~MemoryWatcher();
66 
67  bool timerTicked();
68  int getCurrentRssUsage() const;
69 
70  void adjustOomScore();
71 
72 #if defined(HAS_MEMCHUTE)
73  static void memchuteCallback(MemchuteThreshold threshold);
74 #endif
75 
76 public:
77 
78  Signal<MemState> signalMemoryStateChanged;
79 
80 private:
81 
82  Timer<MemoryWatcher> m_timer;
83  int m_currRssUsage;
84 
85  int m_timeAtLastLowMemAction;
86  int m_timeAtLastExpensiveLowMemAction;
87 
88  static const int kFileNameLen = 128;
89  char m_fileName[kFileNameLen];
90 
91  MemState m_state;
92  MemState m_lastNotifiedState;
93 
94 
95 #if defined(HAS_MEMCHUTE)
96  int m_memchuteCriticalThreshold;
97  MemchuteWatcher* m_memWatch;
98 #endif
99 
100 };
101 
102 #endif /* MEMORYWATCHER_H */
103