📄 cmsmemorymonitor.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/org/opencms/monitor/CmsMemoryMonitor.java,v $
* Date : $Date: 2007-09-06 15:09:27 $
* Version: $Revision: 1.61 $
*
* This library is part of OpenCms -
* the Open Source Content Management System
*
* Copyright (c) 2002 - 2007 Alkacon Software GmbH (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about Alkacon Software GmbH, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.opencms.monitor;
import org.opencms.cache.CmsLruCache;
import org.opencms.cache.CmsMemoryObjectCache;
import org.opencms.cache.CmsVfsMemoryObjectCache;
import org.opencms.configuration.CmsSystemConfiguration;
import org.opencms.db.CmsCacheSettings;
import org.opencms.db.CmsDriverManager;
import org.opencms.db.CmsSecurityManager;
import org.opencms.file.CmsFile;
import org.opencms.file.CmsGroup;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsProject;
import org.opencms.file.CmsProperty;
import org.opencms.file.CmsPropertyDefinition;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsUser;
import org.opencms.flex.CmsFlexCache.CmsFlexCacheVariation;
import org.opencms.i18n.CmsLocaleManager;
import org.opencms.lock.CmsLock;
import org.opencms.lock.CmsLockManager;
import org.opencms.mail.CmsMailTransport;
import org.opencms.mail.CmsSimpleMail;
import org.opencms.main.CmsEvent;
import org.opencms.main.CmsLog;
import org.opencms.main.CmsSessionManager;
import org.opencms.main.I_CmsEventListener;
import org.opencms.main.OpenCms;
import org.opencms.publish.CmsPublishHistory;
import org.opencms.publish.CmsPublishJobInfoBean;
import org.opencms.publish.CmsPublishQueue;
import org.opencms.scheduler.I_CmsScheduledJob;
import org.opencms.security.CmsAccessControlList;
import org.opencms.security.CmsOrganizationalUnit;
import org.opencms.security.CmsPermissionSet;
import org.opencms.security.I_CmsPermissionHandler;
import org.opencms.util.CmsDateUtil;
import org.opencms.util.CmsStringUtil;
import org.opencms.util.CmsUUID;
import org.opencms.util.PrintfFormat;
import org.opencms.xml.CmsXmlContentDefinition;
import org.opencms.xml.CmsXmlEntityResolver;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.mail.internet.InternetAddress;
import org.apache.commons.collections.Buffer;
import org.apache.commons.collections.buffer.SynchronizedBuffer;
import org.apache.commons.collections.map.LRUMap;
import org.apache.commons.logging.Log;
/**
* Monitors OpenCms memory consumtion.<p>
*
* The memory monitor also provides all kind of caches used in the OpenCms core.<p>
*
* @author Carsten Weinholz
* @author Michael Emmerich
* @author Alexander Kandzior
* @author Michael Moossen
*
* @version $Revision: 1.61 $
*
* @since 6.0.0
*/
public class CmsMemoryMonitor implements I_CmsScheduledJob {
/** Set interval for clearing the caches to 10 minutes. */
private static final int INTERVAL_CLEAR = 1000 * 60 * 10;
/** The log object for this class. */
private static final Log LOG = CmsLog.getLog(CmsMemoryMonitor.class);
/** Flag indicating if monitor is currently running. */
private static boolean m_currentlyRunning;
/** Maximum depth for object size recursion. */
private static final int MAX_DEPTH = 5;
/** Cache for access control lists. */
private Map m_accessControlListCache;
/** The memory monitor configuration. */
private CmsMemoryMonitorConfiguration m_configuration;
/** A temporary cache for XML content definitions. */
private Map m_contentDefinitionsCache;
/** Cache for groups. */
private Map m_groupCache;
/** Interval in which emails are send. */
private int m_intervalEmail;
/** Interval in which the log is written. */
private int m_intervalLog;
/** Interval between 2 warnings. */
private int m_intervalWarning;
/** The time the caches were last cleared. */
private long m_lastClearCache;
/** The time the last status email was send. */
private long m_lastEmailStatus;
/** The time the last warning email was send. */
private long m_lastEmailWarning;
/** The time the last status log was written. */
private long m_lastLogStatus;
/** The time the last warning log was written. */
private long m_lastLogWarning;
/** A cache for accelerated locale lookup. */
private Map m_localeCache;
/** Cache for the resource locks. */
private Map m_lockCache;
/** The number of times the log entry was written. */
private int m_logCount;
/** Memory percentage to reach to go to warning level. */
private int m_maxUsagePercent;
/** The memory object cache map. */
private Map m_memObjectCache;
/** The average memory status. */
private CmsMemoryStatus m_memoryAverage;
/** The current memory status. */
private CmsMemoryStatus m_memoryCurrent;
/** Contains the object to be monitored. */
private Map m_monitoredObjects;
/** Cache for organizational units. */
private Map m_orgUnitCache;
/** Cache for permission checks. */
private Map m_permissionCache;
/** Cache for offline projects. */
private Map m_projectCache;
/** Cache for project resources. */
private Map m_projectResourcesCache;
/** Cache for properties. */
private Map m_propertyCache;
/** Cache for property lists. */
private Map m_propertyListCache;
/** Buffer for publish history. */
private Buffer m_publishHistory;
/** Buffer for publish jobs. */
private Buffer m_publishQueue;
/** Cache for resources. */
private Map m_resourceCache;
/** Cache for resource lists. */
private Map m_resourceListCache;
/** Cache for role lists. */
private Map m_roleListsCache;
/** Cache for roles. */
private Map m_rolesCache;
/** Cache for user data. */
private Map m_userCache;
/** Cache for user groups. */
private Map m_userGroupsCache;
/** The vfs memory cache map. */
private Map m_vfsObjectCache;
/** Flag for memory warning mail send. */
private boolean m_warningLoggedSinceLastStatus;
/** Flag for memory warning mail send. */
private boolean m_warningSendSinceLastStatus;
/** A permanent cache to avoid multiple readings of often used files from the VFS. */
private Map m_xmlPermanentEntityCache;
/** A temporary cache to avoid multiple readings of often used files from the VFS. */
private Map m_xmlTemporaryEntityCache;
/**
* Empty constructor, required by OpenCms scheduler.<p>
*/
public CmsMemoryMonitor() {
m_monitoredObjects = new HashMap();
}
/**
* Returns the size of objects that are instances of
* <code>byte[]</code>, <code>String</code>, <code>CmsFile</code>,<code>I_CmsLruCacheObject</code>.<p>
* For other objects, a size of 0 is returned.
*
* @param obj the object
* @return the size of the object
*/
public static int getMemorySize(Object obj) {
if (obj instanceof I_CmsMemoryMonitorable) {
return ((I_CmsMemoryMonitorable)obj).getMemorySize();
}
if (obj instanceof byte[]) {
// will always be a total of 16 + 8
return 8 + (int)(Math.ceil(((byte[])obj).length / 16.0) * 16.0);
}
if (obj instanceof String) {
// will always be a total of 16 + 24
return 24 + (int)(Math.ceil(((String)obj).length() / 8.0) * 16.0);
}
if (obj instanceof CmsFile) {
CmsFile f = (CmsFile)obj;
if (f.getContents() != null) {
return f.getContents().length + 1024;
} else {
return 1024;
}
}
if (obj instanceof CmsUUID) {
return 184; // worst case if UUID String has been generated
}
if (obj instanceof CmsPermissionSet) {
return 16; // two ints
}
if (obj instanceof CmsResource) {
return 1024; // estimated size
}
if (obj instanceof CmsUser) {
return 2048; // estimated size
}
if (obj instanceof CmsGroup) {
return 512; // estimated size
}
if (obj instanceof CmsProject) {
return 512; // estimated size
}
if (obj instanceof Boolean) {
return 8; // one boolean
}
if (obj instanceof CmsProperty) {
int size = 8;
CmsProperty property = (CmsProperty)obj;
size += getMemorySize(property.getName());
if (property.getResourceValue() != null) {
size += getMemorySize(property.getResourceValue());
}
if (property.getStructureValue() != null) {
size += getMemorySize(property.getStructureValue());
}
return size;
}
if (obj instanceof CmsPropertyDefinition) {
int size = 8;
CmsPropertyDefinition propDef = (CmsPropertyDefinition)obj;
size += getMemorySize(propDef.getName());
size += getMemorySize(propDef.getId());
return size;
}
return 8;
}
/**
* Caches the given acl under the given cache key.<p>
*
* @param key the cache key
* @param acl the acl to cache
*/
public void cacheACL(String key, CmsAccessControlList acl) {
m_accessControlListCache.put(key, acl);
}
/**
* Caches the given content definition under the given cache key.<p>
*
* @param key the cache key
* @param contentDefinition the content definition to cache
*/
public void cacheContentDefinition(String key, CmsXmlContentDefinition contentDefinition) {
m_contentDefinitionsCache.put(key, contentDefinition);
}
/**
* Caches the given group under its id AND fully qualified name.<p>
*
* @param group the group to cache
*/
public void cacheGroup(CmsGroup group) {
m_groupCache.put(group.getId().toString(), group);
m_groupCache.put(group.getName(), group);
}
/**
* Caches the given locale under the given cache key.<p>
*
* @param key the cache key
* @param locale the locale to cache
*/
public void cacheLocale(String key, Locale locale) {
if (m_localeCache != null) {
// this may be accessed before initialization
m_localeCache.put(key, locale);
}
}
/**
* Caches the given lock.<p>
*
* The lock is cached by it resource's root path.<p>
*
* @param lock the lock to cache
*/
public void cacheLock(CmsLock lock) {
m_lockCache.put(lock.getResourceName(), lock);
}
/**
* Caches the given object under the given cache key.<p>
*
* @param key the cache key
* @param obj the object to cache
*/
public void cacheMemObject(String key, Object obj) {
m_memObjectCache.put(key, obj);
}
/**
* Caches the given organizational under its id AND the fully qualified name.<p>
*
* @param orgUnit the organizational unit to cache
*/
public void cacheOrgUnit(CmsOrganizationalUnit orgUnit) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -