2014-05-05 21:30:39 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2000-06-03 11:02:20 +04:00
|
|
|
|
|
|
|
#include "nsMemoryImpl.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2004-11-12 22:26:37 +03:00
|
|
|
|
2009-09-15 07:11:30 +04:00
|
|
|
#include "nsIObserver.h"
|
2004-11-12 22:26:37 +03:00
|
|
|
#include "nsIObserverService.h"
|
2013-04-08 22:35:31 +04:00
|
|
|
#include "nsISimpleEnumerator.h"
|
2004-11-12 22:26:37 +03:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 20:59:13 +04:00
|
|
|
#include "mozilla/Services.h"
|
2000-06-03 11:02:20 +04:00
|
|
|
|
2012-10-31 22:59:55 +04:00
|
|
|
#ifdef ANDROID
|
|
|
|
# include <stdio.h>
|
2013-10-01 17:22:15 +04:00
|
|
|
|
|
|
|
// Minimum memory threshold for a device to be considered
|
|
|
|
// a low memory platform. This value has be in sync with
|
|
|
|
// Java's equivalent threshold, defined in
|
|
|
|
// mobile/android/base/util/HardwareUtils.java
|
2013-01-09 20:12:36 +04:00
|
|
|
# define LOW_MEMORY_THRESHOLD_KB (384 * 1024)
|
2012-10-31 22:59:55 +04:00
|
|
|
#endif
|
|
|
|
|
2004-11-12 22:26:37 +03:00
|
|
|
static nsMemoryImpl sGlobalMemory;
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE(nsMemoryImpl, nsIMemory)
|
2004-11-12 22:26:37 +03:00
|
|
|
|
2000-06-03 11:02:20 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsMemoryImpl::HeapMinimize(bool aImmediate) {
|
2016-07-21 08:03:25 +03:00
|
|
|
return FlushMemory(u"heap-minimize", aImmediate);
|
2000-06-03 11:02:20 +04:00
|
|
|
}
|
|
|
|
|
2012-10-31 22:59:55 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-13 21:41:38 +04:00
|
|
|
nsMemoryImpl::IsLowMemoryPlatform(bool* aResult) {
|
2012-10-31 22:59:55 +04:00
|
|
|
#ifdef ANDROID
|
2014-05-05 21:30:39 +04:00
|
|
|
static int sLowMemory =
|
|
|
|
-1; // initialize to unknown, lazily evaluate to 0 or 1
|
|
|
|
if (sLowMemory == -1) {
|
|
|
|
sLowMemory = 0; // assume "not low memory" in case file operations fail
|
2014-05-13 21:41:38 +04:00
|
|
|
*aResult = false;
|
2014-05-05 21:30:39 +04:00
|
|
|
|
|
|
|
// check if MemTotal from /proc/meminfo is less than LOW_MEMORY_THRESHOLD_KB
|
|
|
|
FILE* fd = fopen("/proc/meminfo", "r");
|
|
|
|
if (!fd) {
|
|
|
|
return NS_OK;
|
2012-10-31 22:59:55 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
uint64_t mem = 0;
|
2017-05-17 20:06:23 +03:00
|
|
|
int rv = fscanf(fd, "MemTotal: %" PRIu64 " kB", &mem);
|
2014-05-05 21:30:39 +04:00
|
|
|
if (fclose(fd)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (rv != 1) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
sLowMemory = (mem < LOW_MEMORY_THRESHOLD_KB) ? 1 : 0;
|
|
|
|
}
|
2014-05-13 21:41:38 +04:00
|
|
|
*aResult = (sLowMemory == 1);
|
2012-10-31 22:59:55 +04:00
|
|
|
#else
|
2014-05-13 21:41:38 +04:00
|
|
|
*aResult = false;
|
2012-10-31 22:59:55 +04:00
|
|
|
#endif
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_OK;
|
2012-10-31 22:59:55 +04:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:14:01 +03:00
|
|
|
/*static*/
|
|
|
|
nsresult nsMemoryImpl::Create(nsISupports* aOuter, const nsIID& aIID,
|
|
|
|
void** aResult) {
|
2014-05-13 21:41:38 +04:00
|
|
|
if (NS_WARN_IF(aOuter)) {
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
return sGlobalMemory.QueryInterface(aIID, aResult);
|
2004-11-12 22:26:37 +03:00
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
nsresult nsMemoryImpl::FlushMemory(const char16_t* aReason, bool aImmediate) {
|
2014-05-05 21:30:39 +04:00
|
|
|
if (aImmediate) {
|
|
|
|
// They've asked us to run the flusher *immediately*. We've
|
|
|
|
// got to be on the UI main thread for us to be able to do
|
|
|
|
// that...are we?
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
NS_ERROR("can't synchronously flush memory: not on UI thread");
|
|
|
|
return NS_ERROR_FAILURE;
|
2000-09-20 09:44:19 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2000-09-20 09:44:19 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
bool lastVal = sIsFlushing.exchange(true);
|
2014-05-13 21:41:38 +04:00
|
|
|
if (lastVal) {
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_OK;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2009-09-15 07:11:30 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
PRIntervalTime now = PR_IntervalNow();
|
|
|
|
|
|
|
|
// Run the flushers immediately if we can; otherwise, proxy to the
|
2019-11-28 01:06:46 +03:00
|
|
|
// UI thread and run 'em asynchronously.
|
2019-11-27 16:46:12 +03:00
|
|
|
nsresult rv = NS_OK;
|
2014-05-05 21:30:39 +04:00
|
|
|
if (aImmediate) {
|
2019-11-27 16:46:12 +03:00
|
|
|
RunFlushers(aReason);
|
2014-05-13 21:41:38 +04:00
|
|
|
} else {
|
2014-05-05 21:30:39 +04:00
|
|
|
// Don't broadcast more than once every 1000ms to avoid being noisy
|
|
|
|
if (PR_IntervalToMicroseconds(now - sLastFlushTime) > 1000) {
|
|
|
|
sFlushEvent.mReason = aReason;
|
2014-05-23 23:53:17 +04:00
|
|
|
rv = NS_DispatchToMainThread(&sFlushEvent);
|
2000-09-20 09:44:19 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2000-09-20 09:44:19 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
sLastFlushTime = now;
|
|
|
|
return rv;
|
2000-09-20 09:44:19 +04:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:46:12 +03:00
|
|
|
void nsMemoryImpl::RunFlushers(const char16_t* aReason) {
|
2014-05-05 21:30:39 +04:00
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
if (os) {
|
|
|
|
// Instead of:
|
|
|
|
// os->NotifyObservers(this, "memory-pressure", aReason);
|
|
|
|
// we are going to do this manually to see who/what is
|
|
|
|
// deallocating.
|
2009-09-15 07:11:30 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
nsCOMPtr<nsISimpleEnumerator> e;
|
|
|
|
os->EnumerateObservers("memory-pressure", getter_AddRefs(e));
|
2009-09-15 07:11:30 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
if (e) {
|
2014-05-05 21:30:39 +04:00
|
|
|
nsCOMPtr<nsIObserver> observer;
|
|
|
|
bool loop = true;
|
2009-09-15 07:11:30 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
while (NS_SUCCEEDED(e->HasMoreElements(&loop)) && loop) {
|
2014-05-05 21:30:39 +04:00
|
|
|
nsCOMPtr<nsISupports> supports;
|
|
|
|
e->GetNext(getter_AddRefs(supports));
|
2009-09-15 07:11:30 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!supports) {
|
2014-05-05 21:30:39 +04:00
|
|
|
continue;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2009-09-15 07:11:30 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
observer = do_QueryInterface(supports);
|
|
|
|
observer->Observe(observer, "memory-pressure", aReason);
|
|
|
|
}
|
2000-06-03 11:02:20 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2000-09-20 09:44:19 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
sIsFlushing = false;
|
2000-06-03 11:02:20 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
// XXX need NS_IMPL_STATIC_ADDREF/RELEASE
|
2014-05-13 21:41:38 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
nsMemoryImpl::FlushEvent::AddRef() { return 2; }
|
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
nsMemoryImpl::FlushEvent::Release() { return 1; }
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE(nsMemoryImpl::FlushEvent, nsIRunnable)
|
2000-06-03 11:02:20 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMemoryImpl::FlushEvent::Run() {
|
2014-05-05 21:30:39 +04:00
|
|
|
sGlobalMemory.RunFlushers(mReason);
|
|
|
|
return NS_OK;
|
2000-06-03 11:02:20 +04:00
|
|
|
}
|
|
|
|
|
2013-08-22 19:14:42 +04:00
|
|
|
mozilla::Atomic<bool> nsMemoryImpl::sIsFlushing;
|
2004-11-12 22:26:37 +03:00
|
|
|
|
2009-09-15 07:11:30 +04:00
|
|
|
PRIntervalTime nsMemoryImpl::sLastFlushTime = 0;
|
|
|
|
|
2004-11-12 22:26:37 +03:00
|
|
|
nsMemoryImpl::FlushEvent nsMemoryImpl::sFlushEvent;
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
nsresult NS_GetMemoryManager(nsIMemory** aResult) {
|
|
|
|
return sGlobalMemory.QueryInterface(NS_GET_IID(nsIMemory), (void**)aResult);
|
2000-06-03 11:02:20 +04:00
|
|
|
}
|