CenomeAssetCache.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Reflection;
  29. using log4net;
  30. using Mono.Addins;
  31. using Nini.Config;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.Framework.Interfaces;
  34. using OpenSim.Region.Framework.Scenes;
  35. namespace OpenSim.Region.CoreModules.Asset
  36. {
  37. /// <summary>
  38. /// Cenome memory asset cache.
  39. /// </summary>
  40. /// <remarks>
  41. /// <para>
  42. /// Cache is enabled by setting "AssetCaching" configuration to value "CenomeMemoryAssetCache".
  43. /// When cache is successfully enable log should have message
  44. /// "[ASSET CACHE]: Cenome asset cache enabled (MaxSize = XXX bytes, MaxCount = XXX, ExpirationTime = XXX)".
  45. /// </para>
  46. /// <para>
  47. /// Cache's size is limited by two parameters:
  48. /// maximal allowed size in bytes and maximal allowed asset count. When new asset
  49. /// is added to cache that have achieved either size or count limitation, cache
  50. /// will automatically remove less recently used assets from cache. Additionally
  51. /// asset's lifetime is controlled by expiration time.
  52. /// </para>
  53. /// <para>
  54. /// <list type="table">
  55. /// <listheader>
  56. /// <term>Configuration</term>
  57. /// <description>Description</description>
  58. /// </listheader>
  59. /// <item>
  60. /// <term>MaxSize</term>
  61. /// <description>Maximal size of the cache in bytes. Default value: 128MB (134 217 728 bytes).</description>
  62. /// </item>
  63. /// <item>
  64. /// <term>MaxCount</term>
  65. /// <description>Maximal count of assets stored to cache. Default value: 4096 assets.</description>
  66. /// </item>
  67. /// <item>
  68. /// <term>ExpirationTime</term>
  69. /// <description>Asset's expiration time in minutes. Default value: 30 minutes.</description>
  70. /// </item>
  71. /// </list>
  72. /// </para>
  73. /// </remarks>
  74. /// <example>
  75. /// Enabling Cenome Asset Cache:
  76. /// <code>
  77. /// [Modules]
  78. /// AssetCaching = "CenomeMemoryAssetCache"
  79. /// </code>
  80. /// Setting size and expiration time limitations:
  81. /// <code>
  82. /// [AssetCache]
  83. /// ; 256 MB (default: 134217728)
  84. /// MaxSize = 268435456
  85. /// ; How many assets it is possible to store cache (default: 4096)
  86. /// MaxCount = 16384
  87. /// ; Expiration time - 1 hour (default: 30 minutes)
  88. /// ExpirationTime = 60
  89. /// </code>
  90. /// </example>
  91. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "CenomeMemoryAssetCache")]
  92. public class CenomeMemoryAssetCache : IImprovedAssetCache, ISharedRegionModule
  93. {
  94. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  95. /// <summary>
  96. /// Cache's default maximal asset count.
  97. /// </summary>
  98. /// <remarks>
  99. /// <para>
  100. /// Assuming that average asset size is about 32768 bytes.
  101. /// </para>
  102. /// </remarks>
  103. public const int DefaultMaxCount = 4096;
  104. /// <summary>
  105. /// Default maximal size of the cache in bytes
  106. /// </summary>
  107. /// <remarks>
  108. /// <para>
  109. /// 128MB = 128 * 1024^2 = 134 217 728 bytes.
  110. /// </para>
  111. /// </remarks>
  112. public const long DefaultMaxSize = 134217728;
  113. /// <summary>
  114. /// Asset's default expiration time in the cache.
  115. /// </summary>
  116. public static readonly TimeSpan DefaultExpirationTime = TimeSpan.FromMinutes(30.0);
  117. /// <summary>
  118. /// Cache object.
  119. /// </summary>
  120. private ICnmCache<string, AssetBase> m_cache;
  121. /// <summary>
  122. /// Count of cache commands
  123. /// </summary>
  124. private int m_cachedCount;
  125. /// <summary>
  126. /// How many gets before dumping statistics
  127. /// </summary>
  128. /// <remarks>
  129. /// If 0 or less, then disabled.
  130. /// </remarks>
  131. private int m_debugEpoch;
  132. /// <summary>
  133. /// Is Cenome asset cache enabled.
  134. /// </summary>
  135. private bool m_enabled;
  136. /// <summary>
  137. /// Count of get requests
  138. /// </summary>
  139. private int m_getCount;
  140. /// <summary>
  141. /// How many hits
  142. /// </summary>
  143. private int m_hitCount;
  144. /// <summary>
  145. /// Initialize asset cache module, with custom parameters.
  146. /// </summary>
  147. /// <param name="maximalSize">
  148. /// Cache's maximal size in bytes.
  149. /// </param>
  150. /// <param name="maximalCount">
  151. /// Cache's maximal count of assets.
  152. /// </param>
  153. /// <param name="expirationTime">
  154. /// Asset's expiration time.
  155. /// </param>
  156. protected void Initialize(long maximalSize, int maximalCount, TimeSpan expirationTime)
  157. {
  158. if (maximalSize <= 0 || maximalCount <= 0)
  159. {
  160. //m_log.Debug("[ASSET CACHE]: Cenome asset cache is not enabled.");
  161. m_enabled = false;
  162. return;
  163. }
  164. if (expirationTime <= TimeSpan.Zero)
  165. {
  166. // Disable expiration time
  167. expirationTime = TimeSpan.MaxValue;
  168. }
  169. // Create cache and add synchronization wrapper over it
  170. m_cache =
  171. CnmSynchronizedCache<string, AssetBase>.Synchronized(new CnmMemoryCache<string, AssetBase>(
  172. maximalSize, maximalCount, expirationTime));
  173. m_enabled = true;
  174. m_log.DebugFormat(
  175. "[ASSET CACHE]: Cenome asset cache enabled (MaxSize = {0} bytes, MaxCount = {1}, ExpirationTime = {2})",
  176. maximalSize,
  177. maximalCount,
  178. expirationTime);
  179. }
  180. #region IImprovedAssetCache Members
  181. public bool Check(string id)
  182. {
  183. AssetBase asset;
  184. // XXX:This is probably not an efficient implementation.
  185. return m_cache.TryGetValue(id, out asset);
  186. }
  187. /// <summary>
  188. /// Cache asset.
  189. /// </summary>
  190. /// <param name="asset">
  191. /// The asset that is being cached.
  192. /// </param>
  193. public void Cache(AssetBase asset)
  194. {
  195. if (asset != null)
  196. {
  197. // m_log.DebugFormat("[CENOME ASSET CACHE]: Caching asset {0}", asset.ID);
  198. long size = asset.Data != null ? asset.Data.Length : 1;
  199. m_cache.Set(asset.ID, asset, size);
  200. m_cachedCount++;
  201. }
  202. }
  203. /// <summary>
  204. /// Clear asset cache.
  205. /// </summary>
  206. public void Clear()
  207. {
  208. m_cache.Clear();
  209. }
  210. /// <summary>
  211. /// Expire (remove) asset stored to cache.
  212. /// </summary>
  213. /// <param name="id">
  214. /// The expired asset's id.
  215. /// </param>
  216. public void Expire(string id)
  217. {
  218. m_cache.Remove(id);
  219. }
  220. /// <summary>
  221. /// Get asset stored
  222. /// </summary>
  223. /// <param name="id">
  224. /// The asset's id.
  225. /// </param>
  226. /// <returns>
  227. /// Asset if it is found from cache; otherwise <see langword="null"/>.
  228. /// </returns>
  229. /// <remarks>
  230. /// <para>
  231. /// Caller should always check that is return value <see langword="null"/>.
  232. /// Cache doesn't guarantee in any situation that asset is stored to it.
  233. /// </para>
  234. /// </remarks>
  235. public AssetBase Get(string id)
  236. {
  237. m_getCount++;
  238. AssetBase assetBase;
  239. if (m_cache.TryGetValue(id, out assetBase))
  240. m_hitCount++;
  241. if (m_getCount == m_debugEpoch)
  242. {
  243. m_log.DebugFormat(
  244. "[ASSET CACHE]: Cached = {0}, Get = {1}, Hits = {2}%, Size = {3} bytes, Avg. A. Size = {4} bytes",
  245. m_cachedCount,
  246. m_getCount,
  247. ((double) m_hitCount / m_getCount) * 100.0,
  248. m_cache.Size,
  249. m_cache.Size / m_cache.Count);
  250. m_getCount = 0;
  251. m_hitCount = 0;
  252. m_cachedCount = 0;
  253. }
  254. // if (null == assetBase)
  255. // m_log.DebugFormat("[CENOME ASSET CACHE]: Asset {0} not in cache", id);
  256. return assetBase;
  257. }
  258. #endregion
  259. #region ISharedRegionModule Members
  260. /// <summary>
  261. /// Gets region module's name.
  262. /// </summary>
  263. public string Name
  264. {
  265. get { return "CenomeMemoryAssetCache"; }
  266. }
  267. public Type ReplaceableInterface
  268. {
  269. get { return null; }
  270. }
  271. /// <summary>
  272. /// New region is being added to server.
  273. /// </summary>
  274. /// <param name="scene">
  275. /// Region's scene.
  276. /// </param>
  277. public void AddRegion(Scene scene)
  278. {
  279. if (m_enabled)
  280. scene.RegisterModuleInterface<IImprovedAssetCache>(this);
  281. }
  282. /// <summary>
  283. /// Close region module.
  284. /// </summary>
  285. public void Close()
  286. {
  287. m_enabled = false;
  288. m_cache.Clear();
  289. m_cache = null;
  290. }
  291. /// <summary>
  292. /// Initialize region module.
  293. /// </summary>
  294. /// <param name="source">
  295. /// Configuration source.
  296. /// </param>
  297. public void Initialise(IConfigSource source)
  298. {
  299. m_cache = null;
  300. m_enabled = false;
  301. IConfig moduleConfig = source.Configs[ "Modules" ];
  302. if (moduleConfig == null)
  303. return;
  304. string name = moduleConfig.GetString("AssetCaching");
  305. //m_log.DebugFormat("[XXX] name = {0} (this module's name: {1}", name, Name);
  306. if (name != Name)
  307. return;
  308. long maxSize = DefaultMaxSize;
  309. int maxCount = DefaultMaxCount;
  310. TimeSpan expirationTime = DefaultExpirationTime;
  311. IConfig assetConfig = source.Configs["AssetCache"];
  312. if (assetConfig != null)
  313. {
  314. // Get optional configurations
  315. maxSize = assetConfig.GetLong("MaxSize", DefaultMaxSize);
  316. maxCount = assetConfig.GetInt("MaxCount", DefaultMaxCount);
  317. expirationTime =
  318. TimeSpan.FromMinutes(assetConfig.GetInt("ExpirationTime", (int)DefaultExpirationTime.TotalMinutes));
  319. // Debugging purposes only
  320. m_debugEpoch = assetConfig.GetInt("DebugEpoch", 0);
  321. }
  322. Initialize(maxSize, maxCount, expirationTime);
  323. }
  324. /// <summary>
  325. /// Initialization post handling.
  326. /// </summary>
  327. /// <remarks>
  328. /// <para>
  329. /// Modules can use this to initialize connection with other modules.
  330. /// </para>
  331. /// </remarks>
  332. public void PostInitialise()
  333. {
  334. }
  335. /// <summary>
  336. /// Region has been loaded.
  337. /// </summary>
  338. /// <param name="scene">
  339. /// Region's scene.
  340. /// </param>
  341. /// <remarks>
  342. /// <para>
  343. /// This is needed for all module types. Modules will register
  344. /// Interfaces with scene in AddScene, and will also need a means
  345. /// to access interfaces registered by other modules. Without
  346. /// this extra method, a module attempting to use another modules'
  347. /// interface would be successful only depending on load order,
  348. /// which can't be depended upon, or modules would need to resort
  349. /// to ugly kludges to attempt to request interfaces when needed
  350. /// and unnecessary caching logic repeated in all modules.
  351. /// The extra function stub is just that much cleaner.
  352. /// </para>
  353. /// </remarks>
  354. public void RegionLoaded(Scene scene)
  355. {
  356. }
  357. /// <summary>
  358. /// Region is being removed.
  359. /// </summary>
  360. /// <param name="scene">
  361. /// Region scene that is being removed.
  362. /// </param>
  363. public void RemoveRegion(Scene scene)
  364. {
  365. }
  366. #endregion
  367. }
  368. }