FlotsamAssetCache.cs 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  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. // Uncomment to make asset Get requests for existing
  28. // #define WAIT_ON_INPROGRESS_REQUESTS
  29. using System;
  30. using System.IO;
  31. using System.Collections.Generic;
  32. using System.Linq;
  33. using System.Reflection;
  34. using System.Runtime.Serialization;
  35. using System.Runtime.Serialization.Formatters.Binary;
  36. using System.Threading;
  37. using System.Timers;
  38. using log4net;
  39. using Nini.Config;
  40. using Mono.Addins;
  41. using OpenMetaverse;
  42. using OpenSim.Framework;
  43. using OpenSim.Framework.Console;
  44. using OpenSim.Framework.Monitoring;
  45. using OpenSim.Region.Framework.Interfaces;
  46. using OpenSim.Region.Framework.Scenes;
  47. using OpenSim.Services.Interfaces;
  48. //[assembly: Addin("FlotsamAssetCache", "1.1")]
  49. //[assembly: AddinDependency("OpenSim", "0.8.1")]
  50. namespace OpenSim.Region.CoreModules.Asset
  51. {
  52. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "FlotsamAssetCache")]
  53. public class FlotsamAssetCache : ISharedRegionModule, IAssetCache, IAssetService
  54. {
  55. private static readonly ILog m_log =
  56. LogManager.GetLogger(
  57. MethodBase.GetCurrentMethod().DeclaringType);
  58. private bool m_Enabled;
  59. private bool m_timerRunning;
  60. private bool m_cleanupRunning;
  61. private const string m_ModuleName = "FlotsamAssetCache";
  62. private const string m_DefaultCacheDirectory = "./assetcache";
  63. private string m_CacheDirectory = m_DefaultCacheDirectory;
  64. private readonly List<char> m_InvalidChars = new List<char>();
  65. private int m_LogLevel = 0;
  66. private ulong m_HitRateDisplay = 100; // How often to display hit statistics, given in requests
  67. private static ulong m_Requests;
  68. private static ulong m_RequestsForInprogress;
  69. private static ulong m_DiskHits;
  70. private static ulong m_MemoryHits;
  71. private static ulong m_weakRefHits;
  72. #if WAIT_ON_INPROGRESS_REQUESTS
  73. private Dictionary<string, ManualResetEvent> m_CurrentlyWriting = new Dictionary<string, ManualResetEvent>();
  74. private int m_WaitOnInprogressTimeout = 3000;
  75. #else
  76. private HashSet<string> m_CurrentlyWriting = new HashSet<string>();
  77. #endif
  78. private bool m_FileCacheEnabled = true;
  79. private ExpiringCache<string, AssetBase> m_MemoryCache;
  80. private bool m_MemoryCacheEnabled = false;
  81. private ExpiringCache<string, object> m_negativeCache;
  82. private bool m_negativeCacheEnabled = true;
  83. private bool m_negativeCacheSliding = false;
  84. // Expiration is expressed in hours.
  85. private double m_MemoryExpiration = 0.016;
  86. private const double m_DefaultFileExpiration = 48;
  87. // Negative cache is in seconds
  88. private int m_negativeExpiration = 120;
  89. private TimeSpan m_FileExpiration = TimeSpan.FromHours(m_DefaultFileExpiration);
  90. private TimeSpan m_FileExpirationCleanupTimer = TimeSpan.FromHours(1.0);
  91. private static int m_CacheDirectoryTiers = 1;
  92. private static int m_CacheDirectoryTierLen = 3;
  93. private static int m_CacheWarnAt = 30000;
  94. private System.Timers.Timer m_CacheCleanTimer;
  95. private IAssetService m_AssetService;
  96. private List<Scene> m_Scenes = new List<Scene>();
  97. private object timerLock = new object();
  98. private Dictionary<string,WeakReference> weakAssetReferences = new Dictionary<string, WeakReference>();
  99. private object weakAssetReferencesLock = new object();
  100. private bool m_updateFileTimeOnCacheHit = false;
  101. public FlotsamAssetCache()
  102. {
  103. m_InvalidChars.AddRange(Path.GetInvalidPathChars());
  104. m_InvalidChars.AddRange(Path.GetInvalidFileNameChars());
  105. }
  106. public Type ReplaceableInterface
  107. {
  108. get { return null; }
  109. }
  110. public string Name
  111. {
  112. get { return m_ModuleName; }
  113. }
  114. public void Initialise(IConfigSource source)
  115. {
  116. IConfig moduleConfig = source.Configs["Modules"];
  117. if (moduleConfig != null)
  118. {
  119. string name = moduleConfig.GetString("AssetCaching", String.Empty);
  120. if (name == Name)
  121. {
  122. m_MemoryCache = new ExpiringCache<string, AssetBase>();
  123. m_negativeCache = new ExpiringCache<string, object>();
  124. m_Enabled = true;
  125. m_log.InfoFormat("[FLOTSAM ASSET CACHE]: {0} enabled", this.Name);
  126. IConfig assetConfig = source.Configs["AssetCache"];
  127. if (assetConfig == null)
  128. {
  129. m_log.Debug(
  130. "[FLOTSAM ASSET CACHE]: AssetCache section missing from config (not copied config-include/FlotsamCache.ini.example? Using defaults.");
  131. }
  132. else
  133. {
  134. m_FileCacheEnabled = assetConfig.GetBoolean("FileCacheEnabled", m_FileCacheEnabled);
  135. m_CacheDirectory = assetConfig.GetString("CacheDirectory", m_DefaultCacheDirectory);
  136. m_MemoryCacheEnabled = assetConfig.GetBoolean("MemoryCacheEnabled", m_MemoryCacheEnabled);
  137. m_MemoryExpiration = assetConfig.GetDouble("MemoryCacheTimeout", m_MemoryExpiration);
  138. m_MemoryExpiration *= 3600.0; // config in hours to seconds
  139. m_negativeCacheEnabled = assetConfig.GetBoolean("NegativeCacheEnabled", m_negativeCacheEnabled);
  140. m_negativeExpiration = assetConfig.GetInt("NegativeCacheTimeout", m_negativeExpiration);
  141. m_negativeCacheSliding = assetConfig.GetBoolean("NegativeCacheSliding", m_negativeCacheSliding);
  142. m_updateFileTimeOnCacheHit = assetConfig.GetBoolean("UpdateFileTimeOnCacheHit", m_updateFileTimeOnCacheHit);
  143. #if WAIT_ON_INPROGRESS_REQUESTS
  144. m_WaitOnInprogressTimeout = assetConfig.GetInt("WaitOnInprogressTimeout", 3000);
  145. #endif
  146. m_LogLevel = assetConfig.GetInt("LogLevel", m_LogLevel);
  147. m_HitRateDisplay = (ulong)assetConfig.GetLong("HitRateDisplay", (long)m_HitRateDisplay);
  148. m_FileExpiration = TimeSpan.FromHours(assetConfig.GetDouble("FileCacheTimeout", m_DefaultFileExpiration));
  149. m_FileExpirationCleanupTimer
  150. = TimeSpan.FromHours(
  151. assetConfig.GetDouble("FileCleanupTimer", m_FileExpirationCleanupTimer.TotalHours));
  152. m_CacheDirectoryTiers = assetConfig.GetInt("CacheDirectoryTiers", m_CacheDirectoryTiers);
  153. m_CacheDirectoryTierLen = assetConfig.GetInt("CacheDirectoryTierLength", m_CacheDirectoryTierLen);
  154. m_CacheWarnAt = assetConfig.GetInt("CacheWarnAt", m_CacheWarnAt);
  155. }
  156. m_log.InfoFormat("[FLOTSAM ASSET CACHE]: Cache Directory {0}", m_CacheDirectory);
  157. if (m_CacheDirectoryTiers < 1)
  158. {
  159. m_CacheDirectoryTiers = 1;
  160. }
  161. else if (m_CacheDirectoryTiers > 3)
  162. {
  163. m_CacheDirectoryTiers = 3;
  164. }
  165. if (m_CacheDirectoryTierLen < 1)
  166. {
  167. m_CacheDirectoryTierLen = 1;
  168. }
  169. else if (m_CacheDirectoryTierLen > 4)
  170. {
  171. m_CacheDirectoryTierLen = 4;
  172. }
  173. MainConsole.Instance.Commands.AddCommand("Assets", true, "fcache status", "fcache status", "Display cache status", HandleConsoleCommand);
  174. MainConsole.Instance.Commands.AddCommand("Assets", true, "fcache clear", "fcache clear [file] [memory]", "Remove all assets in the cache. If file or memory is specified then only this cache is cleared.", HandleConsoleCommand);
  175. MainConsole.Instance.Commands.AddCommand("Assets", true, "fcache assets", "fcache assets", "Attempt a deep scan and cache of all assets in all scenes", HandleConsoleCommand);
  176. MainConsole.Instance.Commands.AddCommand("Assets", true, "fcache expire", "fcache expire <datetime>", "Purge cached assets older then the specified date/time", HandleConsoleCommand);
  177. }
  178. }
  179. }
  180. public void PostInitialise()
  181. {
  182. }
  183. public void Close()
  184. {
  185. }
  186. public void AddRegion(Scene scene)
  187. {
  188. if (m_Enabled)
  189. {
  190. scene.RegisterModuleInterface<IAssetCache>(this);
  191. m_Scenes.Add(scene);
  192. }
  193. }
  194. public void RemoveRegion(Scene scene)
  195. {
  196. if (m_Enabled)
  197. {
  198. scene.UnregisterModuleInterface<IAssetCache>(this);
  199. m_Scenes.Remove(scene);
  200. lock(timerLock)
  201. {
  202. if(m_timerRunning && m_Scenes.Count <= 0)
  203. {
  204. m_timerRunning = false;
  205. m_CacheCleanTimer.Stop();
  206. m_CacheCleanTimer.Close();
  207. }
  208. }
  209. }
  210. }
  211. public void RegionLoaded(Scene scene)
  212. {
  213. if (m_Enabled)
  214. {
  215. if(m_AssetService == null)
  216. m_AssetService = scene.RequestModuleInterface<IAssetService>();
  217. lock(timerLock)
  218. {
  219. if(!m_timerRunning)
  220. {
  221. if (m_FileCacheEnabled && (m_FileExpiration > TimeSpan.Zero) && (m_FileExpirationCleanupTimer > TimeSpan.Zero))
  222. {
  223. m_CacheCleanTimer = new System.Timers.Timer(m_FileExpirationCleanupTimer.TotalMilliseconds);
  224. m_CacheCleanTimer.AutoReset = false;
  225. m_CacheCleanTimer.Elapsed += CleanupExpiredFiles;
  226. m_CacheCleanTimer.Start();
  227. m_timerRunning = true;
  228. }
  229. }
  230. }
  231. if (m_MemoryCacheEnabled)
  232. m_MemoryCache = new ExpiringCache<string, AssetBase>();
  233. lock(weakAssetReferencesLock)
  234. weakAssetReferences = new Dictionary<string, WeakReference>();
  235. }
  236. }
  237. ////////////////////////////////////////////////////////////
  238. // IAssetCache
  239. //
  240. private void UpdateWeakReference(string key, AssetBase asset)
  241. {
  242. WeakReference aref = new WeakReference(asset);
  243. lock(weakAssetReferencesLock)
  244. weakAssetReferences[key] = aref;
  245. }
  246. private void UpdateMemoryCache(string key, AssetBase asset)
  247. {
  248. // NOTE DO NOT USE SLIDEEXPIRE option on current libomv
  249. m_MemoryCache.AddOrUpdate(key, asset, m_MemoryExpiration);
  250. }
  251. private void UpdateFileCache(string key, AssetBase asset)
  252. {
  253. string filename = GetFileName(key);
  254. try
  255. {
  256. // If the file is already cached, don't cache it, just touch it so access time is updated
  257. if (File.Exists(filename))
  258. {
  259. UpdateFileLastAccessTime(filename);
  260. }
  261. else
  262. {
  263. // Once we start writing, make sure we flag that we're writing
  264. // that object to the cache so that we don't try to write the
  265. // same file multiple times.
  266. lock (m_CurrentlyWriting)
  267. {
  268. #if WAIT_ON_INPROGRESS_REQUESTS
  269. if (m_CurrentlyWriting.ContainsKey(filename))
  270. {
  271. return;
  272. }
  273. else
  274. {
  275. m_CurrentlyWriting.Add(filename, new ManualResetEvent(false));
  276. }
  277. #else
  278. if (m_CurrentlyWriting.Contains(filename))
  279. {
  280. return;
  281. }
  282. else
  283. {
  284. m_CurrentlyWriting.Add(filename);
  285. }
  286. #endif
  287. }
  288. Util.FireAndForget(
  289. delegate { WriteFileCache(filename, asset); }, null, "FlotsamAssetCache.UpdateFileCache");
  290. }
  291. }
  292. catch (Exception e)
  293. {
  294. m_log.ErrorFormat(
  295. "[FLOTSAM ASSET CACHE]: Failed to update cache for asset {0}. Exception {1} {2}",
  296. asset.ID, e.Message, e.StackTrace);
  297. }
  298. }
  299. public void Cache(AssetBase asset)
  300. {
  301. // TODO: Spawn this off to some seperate thread to do the actual writing
  302. if (asset != null)
  303. {
  304. //m_log.DebugFormat("[FLOTSAM ASSET CACHE]: Caching asset with id {0}", asset.ID);
  305. UpdateWeakReference(asset.ID, asset);
  306. if (m_MemoryCacheEnabled)
  307. UpdateMemoryCache(asset.ID, asset);
  308. if (m_FileCacheEnabled)
  309. UpdateFileCache(asset.ID, asset);
  310. }
  311. }
  312. public void CacheNegative(string id)
  313. {
  314. if (m_negativeCacheEnabled)
  315. {
  316. if (m_negativeCacheSliding)
  317. m_negativeCache.AddOrUpdate(id, null, TimeSpan.FromSeconds(m_negativeExpiration));
  318. else
  319. m_negativeCache.AddOrUpdate(id, null, m_negativeExpiration);
  320. }
  321. }
  322. /// <summary>
  323. /// Updates the cached file with the current time.
  324. /// </summary>
  325. /// <param name="filename">Filename.</param>
  326. /// <returns><c>true</c>, if the update was successful, false otherwise.</returns>
  327. private bool UpdateFileLastAccessTime(string filename)
  328. {
  329. try
  330. {
  331. File.SetLastAccessTime(filename, DateTime.Now);
  332. return true;
  333. }
  334. catch
  335. {
  336. return false;
  337. }
  338. }
  339. private AssetBase GetFromWeakReference(string id)
  340. {
  341. AssetBase asset = null;
  342. WeakReference aref;
  343. lock(weakAssetReferencesLock)
  344. {
  345. if (weakAssetReferences.TryGetValue(id, out aref))
  346. {
  347. asset = aref.Target as AssetBase;
  348. if(asset == null)
  349. weakAssetReferences.Remove(id);
  350. else
  351. m_weakRefHits++;
  352. }
  353. }
  354. return asset;
  355. }
  356. /// <summary>
  357. /// Try to get an asset from the in-memory cache.
  358. /// </summary>
  359. /// <param name="id"></param>
  360. /// <returns></returns>
  361. private AssetBase GetFromMemoryCache(string id)
  362. {
  363. AssetBase asset = null;
  364. if (m_MemoryCache.TryGetValue(id, out asset))
  365. m_MemoryHits++;
  366. return asset;
  367. }
  368. private bool CheckFromMemoryCache(string id)
  369. {
  370. return m_MemoryCache.Contains(id);
  371. }
  372. /// <summary>
  373. /// Try to get an asset from the file cache.
  374. /// </summary>
  375. /// <param name="id"></param>
  376. /// <returns>An asset retrieved from the file cache. null if there was a problem retrieving an asset.</returns>
  377. private AssetBase GetFromFileCache(string id)
  378. {
  379. string filename = GetFileName(id);
  380. #if WAIT_ON_INPROGRESS_REQUESTS
  381. // Check if we're already downloading this asset. If so, try to wait for it to
  382. // download.
  383. if (m_WaitOnInprogressTimeout > 0)
  384. {
  385. m_RequestsForInprogress++;
  386. ManualResetEvent waitEvent;
  387. if (m_CurrentlyWriting.TryGetValue(filename, out waitEvent))
  388. {
  389. waitEvent.WaitOne(m_WaitOnInprogressTimeout);
  390. return Get(id);
  391. }
  392. }
  393. #else
  394. // Track how often we have the problem that an asset is requested while
  395. // it is still being downloaded by a previous request.
  396. if (m_CurrentlyWriting.Contains(filename))
  397. {
  398. m_RequestsForInprogress++;
  399. return null;
  400. }
  401. #endif
  402. AssetBase asset = null;
  403. if (File.Exists(filename))
  404. {
  405. try
  406. {
  407. using (FileStream stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
  408. {
  409. if (stream.Length == 0) // Empty file will trigger exception below
  410. return null;
  411. BinaryFormatter bformatter = new BinaryFormatter();
  412. asset = (AssetBase)bformatter.Deserialize(stream);
  413. m_DiskHits++;
  414. }
  415. }
  416. catch (System.Runtime.Serialization.SerializationException e)
  417. {
  418. m_log.WarnFormat(
  419. "[FLOTSAM ASSET CACHE]: Failed to get file {0} for asset {1}. Exception {2} {3}",
  420. filename, id, e.Message, e.StackTrace);
  421. // If there was a problem deserializing the asset, the asset may
  422. // either be corrupted OR was serialized under an old format
  423. // {different version of AssetBase} -- we should attempt to
  424. // delete it and re-cache
  425. File.Delete(filename);
  426. }
  427. catch (Exception e)
  428. {
  429. m_log.WarnFormat(
  430. "[FLOTSAM ASSET CACHE]: Failed to get file {0} for asset {1}. Exception {2} {3}",
  431. filename, id, e.Message, e.StackTrace);
  432. }
  433. }
  434. return asset;
  435. }
  436. private bool CheckFromFileCache(string id)
  437. {
  438. bool found = false;
  439. string filename = GetFileName(id);
  440. if (File.Exists(filename))
  441. {
  442. try
  443. {
  444. using (FileStream stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
  445. {
  446. if (stream != null)
  447. found = true;
  448. }
  449. }
  450. catch (Exception e)
  451. {
  452. m_log.ErrorFormat(
  453. "[FLOTSAM ASSET CACHE]: Failed to check file {0} for asset {1}. Exception {2} {3}",
  454. filename, id, e.Message, e.StackTrace);
  455. }
  456. }
  457. return found;
  458. }
  459. // For IAssetService
  460. public AssetBase Get(string id)
  461. {
  462. AssetBase asset;
  463. Get(id, out asset);
  464. return asset;
  465. }
  466. public bool Get(string id, out AssetBase asset)
  467. {
  468. asset = null;
  469. m_Requests++;
  470. object dummy;
  471. if (m_negativeCache.TryGetValue(id, out dummy))
  472. {
  473. return false;
  474. }
  475. asset = GetFromWeakReference(id);
  476. if (asset != null && m_updateFileTimeOnCacheHit)
  477. {
  478. string filename = GetFileName(id);
  479. UpdateFileLastAccessTime(filename);
  480. }
  481. if (m_MemoryCacheEnabled && asset == null)
  482. {
  483. asset = GetFromMemoryCache(id);
  484. if(asset != null)
  485. {
  486. UpdateWeakReference(id,asset);
  487. if (m_updateFileTimeOnCacheHit)
  488. {
  489. string filename = GetFileName(id);
  490. UpdateFileLastAccessTime(filename);
  491. }
  492. }
  493. }
  494. if (asset == null && m_FileCacheEnabled)
  495. {
  496. asset = GetFromFileCache(id);
  497. if(asset != null)
  498. UpdateWeakReference(id,asset);
  499. }
  500. if (m_MemoryCacheEnabled && asset != null)
  501. UpdateMemoryCache(id, asset);
  502. if (((m_LogLevel >= 1)) && (m_HitRateDisplay != 0) && (m_Requests % m_HitRateDisplay == 0))
  503. {
  504. m_log.InfoFormat("[FLOTSAM ASSET CACHE]: Cache Get :: {0} :: {1}", id, asset == null ? "Miss" : "Hit");
  505. GenerateCacheHitReport().ForEach(l => m_log.InfoFormat("[FLOTSAM ASSET CACHE]: {0}", l));
  506. }
  507. return true;
  508. }
  509. public bool Check(string id)
  510. {
  511. if (m_MemoryCacheEnabled && CheckFromMemoryCache(id))
  512. return true;
  513. if (m_FileCacheEnabled && CheckFromFileCache(id))
  514. return true;
  515. return false;
  516. }
  517. public AssetBase GetCached(string id)
  518. {
  519. AssetBase asset;
  520. Get(id, out asset);
  521. return asset;
  522. }
  523. public void Expire(string id)
  524. {
  525. if (m_LogLevel >= 2)
  526. m_log.DebugFormat("[FLOTSAM ASSET CACHE]: Expiring Asset {0}", id);
  527. try
  528. {
  529. if (m_FileCacheEnabled)
  530. {
  531. string filename = GetFileName(id);
  532. if (File.Exists(filename))
  533. {
  534. File.Delete(filename);
  535. }
  536. }
  537. if (m_MemoryCacheEnabled)
  538. m_MemoryCache.Remove(id);
  539. lock(weakAssetReferencesLock)
  540. weakAssetReferences.Remove(id);
  541. }
  542. catch (Exception e)
  543. {
  544. m_log.WarnFormat(
  545. "[FLOTSAM ASSET CACHE]: Failed to expire cached file {0}. Exception {1} {2}",
  546. id, e.Message, e.StackTrace);
  547. }
  548. }
  549. public void Clear()
  550. {
  551. if (m_LogLevel >= 2)
  552. m_log.Debug("[FLOTSAM ASSET CACHE]: Clearing caches.");
  553. if (m_FileCacheEnabled && Directory.Exists(m_CacheDirectory))
  554. {
  555. foreach (string dir in Directory.GetDirectories(m_CacheDirectory))
  556. {
  557. Directory.Delete(dir);
  558. }
  559. }
  560. if (m_MemoryCacheEnabled)
  561. m_MemoryCache = new ExpiringCache<string, AssetBase>();
  562. if (m_negativeCacheEnabled)
  563. m_negativeCache = new ExpiringCache<string, object>();
  564. lock(weakAssetReferencesLock)
  565. weakAssetReferences = new Dictionary<string, WeakReference>();
  566. }
  567. private void CleanupExpiredFiles(object source, ElapsedEventArgs e)
  568. {
  569. if (m_LogLevel >= 2)
  570. m_log.DebugFormat("[FLOTSAM ASSET CACHE]: Checking for expired files older then {0}.", m_FileExpiration);
  571. lock(timerLock)
  572. {
  573. if(!m_timerRunning || m_cleanupRunning)
  574. return;
  575. m_cleanupRunning = true;
  576. }
  577. // Purge all files last accessed prior to this point
  578. DateTime purgeLine = DateTime.Now - m_FileExpiration;
  579. // An asset cache may contain local non-temporary assets that are not in the asset service. Therefore,
  580. // before cleaning up expired files we must scan the objects in the scene to make sure that we retain
  581. // such local assets if they have not been recently accessed.
  582. TouchAllSceneAssets(false);
  583. if(Directory.Exists(m_CacheDirectory))
  584. {
  585. foreach (string dir in Directory.GetDirectories(m_CacheDirectory))
  586. CleanExpiredFiles(dir, purgeLine);
  587. }
  588. lock(timerLock)
  589. {
  590. if(m_timerRunning)
  591. m_CacheCleanTimer.Start();
  592. m_cleanupRunning = false;
  593. }
  594. }
  595. /// <summary>
  596. /// Recurses through specified directory checking for asset files last
  597. /// accessed prior to the specified purge line and deletes them. Also
  598. /// removes empty tier directories.
  599. /// </summary>
  600. /// <param name="dir"></param>
  601. /// <param name="purgeLine"></param>
  602. private void CleanExpiredFiles(string dir, DateTime purgeLine)
  603. {
  604. try
  605. {
  606. if(!Directory.Exists(dir))
  607. return;
  608. foreach (string file in Directory.GetFiles(dir))
  609. {
  610. if (File.GetLastAccessTime(file) < purgeLine)
  611. {
  612. File.Delete(file);
  613. }
  614. }
  615. // Recurse into lower tiers
  616. foreach (string subdir in Directory.GetDirectories(dir))
  617. {
  618. CleanExpiredFiles(subdir, purgeLine);
  619. }
  620. // Check if a tier directory is empty, if so, delete it
  621. int dirSize = Directory.GetFiles(dir).Length + Directory.GetDirectories(dir).Length;
  622. if (dirSize == 0)
  623. {
  624. Directory.Delete(dir);
  625. }
  626. else if (dirSize >= m_CacheWarnAt)
  627. {
  628. m_log.WarnFormat(
  629. "[FLOTSAM ASSET CACHE]: Cache folder exceeded CacheWarnAt limit {0} {1}. Suggest increasing tiers, tier length, or reducing cache expiration",
  630. dir, dirSize);
  631. }
  632. }
  633. catch (DirectoryNotFoundException)
  634. {
  635. // If we get here, another node on the same box has
  636. // already removed the directory. Continue with next.
  637. }
  638. catch (Exception e)
  639. {
  640. m_log.Warn(
  641. string.Format("[FLOTSAM ASSET CACHE]: Could not complete clean of expired files in {0}, exception ", dir), e);
  642. }
  643. }
  644. /// <summary>
  645. /// Determines the filename for an AssetID stored in the file cache
  646. /// </summary>
  647. /// <param name="id"></param>
  648. /// <returns></returns>
  649. private string GetFileName(string id)
  650. {
  651. // Would it be faster to just hash the darn thing?
  652. foreach (char c in m_InvalidChars)
  653. {
  654. id = id.Replace(c, '_');
  655. }
  656. string path = m_CacheDirectory;
  657. for (int p = 1; p <= m_CacheDirectoryTiers; p++)
  658. {
  659. string pathPart = id.Substring((p - 1) * m_CacheDirectoryTierLen, m_CacheDirectoryTierLen);
  660. path = Path.Combine(path, pathPart);
  661. }
  662. return Path.Combine(path, id);
  663. }
  664. /// <summary>
  665. /// Writes a file to the file cache, creating any nessesary
  666. /// tier directories along the way
  667. /// </summary>
  668. /// <param name="filename"></param>
  669. /// <param name="asset"></param>
  670. private void WriteFileCache(string filename, AssetBase asset)
  671. {
  672. Stream stream = null;
  673. // Make sure the target cache directory exists
  674. string directory = Path.GetDirectoryName(filename);
  675. // Write file first to a temp name, so that it doesn't look
  676. // like it's already cached while it's still writing.
  677. string tempname = Path.Combine(directory, Path.GetRandomFileName());
  678. try
  679. {
  680. try
  681. {
  682. if (!Directory.Exists(directory))
  683. {
  684. Directory.CreateDirectory(directory);
  685. }
  686. stream = File.Open(tempname, FileMode.Create);
  687. BinaryFormatter bformatter = new BinaryFormatter();
  688. bformatter.Serialize(stream, asset);
  689. }
  690. catch (IOException e)
  691. {
  692. m_log.WarnFormat(
  693. "[FLOTSAM ASSET CACHE]: Failed to write asset {0} to temporary location {1} (final {2}) on cache in {3}. Exception {4} {5}.",
  694. asset.ID, tempname, filename, directory, e.Message, e.StackTrace);
  695. return;
  696. }
  697. catch (UnauthorizedAccessException e)
  698. {
  699. }
  700. finally
  701. {
  702. if (stream != null)
  703. stream.Close();
  704. }
  705. try
  706. {
  707. // Now that it's written, rename it so that it can be found.
  708. //
  709. // File.Copy(tempname, filename, true);
  710. // File.Delete(tempname);
  711. //
  712. // For a brief period, this was done as a separate copy and then temporary file delete operation to
  713. // avoid an IOException caused by move if some competing thread had already written the file.
  714. // However, this causes exceptions on Windows when other threads attempt to read a file
  715. // which is still being copied. So instead, go back to moving the file and swallow any IOException.
  716. //
  717. // This situation occurs fairly rarely anyway. We assume in this that moves are atomic on the
  718. // filesystem.
  719. File.Move(tempname, filename);
  720. if (m_LogLevel >= 2)
  721. m_log.DebugFormat("[FLOTSAM ASSET CACHE]: Cache Stored :: {0}", asset.ID);
  722. }
  723. catch (IOException)
  724. {
  725. // If we see an IOException here it's likely that some other competing thread has written the
  726. // cache file first, so ignore. Other IOException errors (e.g. filesystem full) should be
  727. // signally by the earlier temporary file writing code.
  728. }
  729. }
  730. finally
  731. {
  732. // Even if the write fails with an exception, we need to make sure
  733. // that we release the lock on that file, otherwise it'll never get
  734. // cached
  735. lock (m_CurrentlyWriting)
  736. {
  737. #if WAIT_ON_INPROGRESS_REQUESTS
  738. ManualResetEvent waitEvent;
  739. if (m_CurrentlyWriting.TryGetValue(filename, out waitEvent))
  740. {
  741. m_CurrentlyWriting.Remove(filename);
  742. waitEvent.Set();
  743. }
  744. #else
  745. m_CurrentlyWriting.Remove(filename);
  746. #endif
  747. }
  748. }
  749. }
  750. /// <summary>
  751. /// Scan through the file cache, and return number of assets currently cached.
  752. /// </summary>
  753. /// <param name="dir"></param>
  754. /// <returns></returns>
  755. private int GetFileCacheCount(string dir)
  756. {
  757. if(!Directory.Exists(dir))
  758. return 0;
  759. int count = Directory.GetFiles(dir).Length;
  760. foreach (string subdir in Directory.GetDirectories(dir))
  761. {
  762. count += GetFileCacheCount(subdir);
  763. }
  764. return count;
  765. }
  766. /// <summary>
  767. /// This notes the last time the Region had a deep asset scan performed on it.
  768. /// </summary>
  769. /// <param name="regionID"></param>
  770. private void StampRegionStatusFile(UUID regionID)
  771. {
  772. string RegionCacheStatusFile = Path.Combine(m_CacheDirectory, "RegionStatus_" + regionID.ToString() + ".fac");
  773. try
  774. {
  775. if (File.Exists(RegionCacheStatusFile))
  776. {
  777. File.SetLastWriteTime(RegionCacheStatusFile, DateTime.Now);
  778. }
  779. else
  780. {
  781. File.WriteAllText(
  782. RegionCacheStatusFile,
  783. "Please do not delete this file unless you are manually clearing your Flotsam Asset Cache.");
  784. }
  785. }
  786. catch (Exception e)
  787. {
  788. m_log.Warn(
  789. string.Format(
  790. "[FLOTSAM ASSET CACHE]: Could not stamp region status file for region {0}. Exception ",
  791. regionID),
  792. e);
  793. }
  794. }
  795. /// <summary>
  796. /// Iterates through all Scenes, doing a deep scan through assets
  797. /// to update the access time of all assets present in the scene or referenced by assets
  798. /// in the scene.
  799. /// </summary>
  800. /// <param name="storeUncached">
  801. /// If true, then assets scanned which are not found in cache are added to the cache.
  802. /// </param>
  803. /// <returns>Number of distinct asset references found in the scene.</returns>
  804. private int TouchAllSceneAssets(bool storeUncached)
  805. {
  806. UuidGatherer gatherer = new UuidGatherer(m_AssetService);
  807. Dictionary<UUID, bool> assetsFound = new Dictionary<UUID, bool>();
  808. foreach (Scene s in m_Scenes)
  809. {
  810. StampRegionStatusFile(s.RegionInfo.RegionID);
  811. s.ForEachSOG(delegate(SceneObjectGroup e)
  812. {
  813. if(!m_timerRunning && !storeUncached)
  814. return;
  815. gatherer.AddForInspection(e);
  816. gatherer.GatherAll();
  817. if(!m_timerRunning && !storeUncached)
  818. return;
  819. foreach (UUID assetID in gatherer.GatheredUuids.Keys)
  820. {
  821. if (!assetsFound.ContainsKey(assetID))
  822. {
  823. string filename = GetFileName(assetID.ToString());
  824. if (File.Exists(filename))
  825. {
  826. UpdateFileLastAccessTime(filename);
  827. assetsFound[assetID] = true;
  828. }
  829. else if (storeUncached)
  830. {
  831. AssetBase cachedAsset = m_AssetService.Get(assetID.ToString());
  832. if (cachedAsset == null && gatherer.GatheredUuids[assetID] != (sbyte)AssetType.Unknown)
  833. assetsFound[assetID] = false;
  834. else
  835. assetsFound[assetID] = true;
  836. }
  837. }
  838. else if (!assetsFound[assetID])
  839. {
  840. m_log.DebugFormat(
  841. "[FLOTSAM ASSET CACHE]: Could not find asset {0}, type {1} referenced by object {2} at {3} in scene {4} when pre-caching all scene assets",
  842. assetID, gatherer.GatheredUuids[assetID], e.Name, e.AbsolutePosition, s.Name);
  843. }
  844. }
  845. gatherer.GatheredUuids.Clear();
  846. if(!m_timerRunning && !storeUncached)
  847. return;
  848. if(!storeUncached)
  849. Thread.Sleep(50);
  850. });
  851. if(!m_timerRunning && !storeUncached)
  852. break;
  853. }
  854. return assetsFound.Count;
  855. }
  856. /// <summary>
  857. /// Deletes all cache contents
  858. /// </summary>
  859. private void ClearFileCache()
  860. {
  861. if(!Directory.Exists(m_CacheDirectory))
  862. return;
  863. foreach (string dir in Directory.GetDirectories(m_CacheDirectory))
  864. {
  865. try
  866. {
  867. Directory.Delete(dir, true);
  868. }
  869. catch (Exception e)
  870. {
  871. m_log.WarnFormat(
  872. "[FLOTSAM ASSET CACHE]: Couldn't clear asset cache directory {0} from {1}. Exception {2} {3}",
  873. dir, m_CacheDirectory, e.Message, e.StackTrace);
  874. }
  875. }
  876. foreach (string file in Directory.GetFiles(m_CacheDirectory))
  877. {
  878. try
  879. {
  880. File.Delete(file);
  881. }
  882. catch (Exception e)
  883. {
  884. m_log.WarnFormat(
  885. "[FLOTSAM ASSET CACHE]: Couldn't clear asset cache file {0} from {1}. Exception {1} {2}",
  886. file, m_CacheDirectory, e.Message, e.StackTrace);
  887. }
  888. }
  889. }
  890. private List<string> GenerateCacheHitReport()
  891. {
  892. List<string> outputLines = new List<string>();
  893. double invReq = 100.0 / m_Requests;
  894. double weakHitRate = m_weakRefHits * invReq;
  895. int weakEntries = weakAssetReferences.Count;
  896. double fileHitRate = m_DiskHits * invReq;
  897. double TotalHitRate = weakHitRate + fileHitRate;
  898. outputLines.Add(
  899. string.Format("Total requests: {0}", m_Requests));
  900. outputLines.Add(
  901. string.Format("unCollected Hit Rate: {0}% ({1} entries)", weakHitRate.ToString("0.00"),weakEntries));
  902. outputLines.Add(
  903. string.Format("File Hit Rate: {0}%", fileHitRate.ToString("0.00")));
  904. if (m_MemoryCacheEnabled)
  905. {
  906. double HitRate = m_MemoryHits * invReq;
  907. outputLines.Add(
  908. string.Format("Memory Hit Rate: {0}%", HitRate.ToString("0.00")));
  909. TotalHitRate += HitRate;
  910. }
  911. outputLines.Add(
  912. string.Format("Total Hit Rate: {0}%", TotalHitRate.ToString("0.00")));
  913. outputLines.Add(
  914. string.Format(
  915. "Requests overlap during file writing: {0}", m_RequestsForInprogress));
  916. return outputLines;
  917. }
  918. #region Console Commands
  919. private void HandleConsoleCommand(string module, string[] cmdparams)
  920. {
  921. ICommandConsole con = MainConsole.Instance;
  922. if (cmdparams.Length >= 2)
  923. {
  924. string cmd = cmdparams[1];
  925. switch (cmd)
  926. {
  927. case "status":
  928. if (m_MemoryCacheEnabled)
  929. con.OutputFormat("Memory Cache: {0} assets", m_MemoryCache.Count);
  930. else
  931. con.OutputFormat("Memory cache disabled");
  932. if (m_FileCacheEnabled)
  933. {
  934. int fileCount = GetFileCacheCount(m_CacheDirectory);
  935. con.OutputFormat("File Cache: {0} assets", fileCount);
  936. }
  937. else
  938. {
  939. con.Output("File cache disabled");
  940. }
  941. GenerateCacheHitReport().ForEach(l => con.Output(l));
  942. if (m_FileCacheEnabled)
  943. {
  944. con.Output("Deep scans have previously been performed on the following regions:");
  945. foreach (string s in Directory.GetFiles(m_CacheDirectory, "*.fac"))
  946. {
  947. string RegionID = s.Remove(0,s.IndexOf("_")).Replace(".fac","");
  948. DateTime RegionDeepScanTMStamp = File.GetLastWriteTime(s);
  949. con.OutputFormat("Region: {0}, {1}", RegionID, RegionDeepScanTMStamp.ToString("MM/dd/yyyy hh:mm:ss"));
  950. }
  951. }
  952. break;
  953. case "clear":
  954. if (cmdparams.Length < 2)
  955. {
  956. con.Output("Usage is fcache clear [file] [memory]");
  957. break;
  958. }
  959. bool clearMemory = false, clearFile = false;
  960. if (cmdparams.Length == 2)
  961. {
  962. clearMemory = true;
  963. clearFile = true;
  964. }
  965. foreach (string s in cmdparams)
  966. {
  967. if (s.ToLower() == "memory")
  968. clearMemory = true;
  969. else if (s.ToLower() == "file")
  970. clearFile = true;
  971. }
  972. if (clearMemory)
  973. {
  974. if (m_MemoryCacheEnabled)
  975. {
  976. m_MemoryCache.Clear();
  977. con.Output("Memory cache cleared.");
  978. }
  979. else
  980. {
  981. con.Output("Memory cache not enabled.");
  982. }
  983. }
  984. if (clearFile)
  985. {
  986. if (m_FileCacheEnabled)
  987. {
  988. ClearFileCache();
  989. con.Output("File cache cleared.");
  990. }
  991. else
  992. {
  993. con.Output("File cache not enabled.");
  994. }
  995. }
  996. break;
  997. case "assets":
  998. lock(timerLock)
  999. {
  1000. if(m_cleanupRunning)
  1001. {
  1002. con.OutputFormat("FloatSam assets check already running");
  1003. return;
  1004. }
  1005. m_cleanupRunning = true;
  1006. }
  1007. con.Output("FloatSam Ensuring assets are cached for all scenes.");
  1008. WorkManager.RunInThreadPool(delegate
  1009. {
  1010. bool wasRunning= false;
  1011. lock(timerLock)
  1012. {
  1013. if(m_timerRunning)
  1014. {
  1015. m_CacheCleanTimer.Stop();
  1016. m_timerRunning = false;
  1017. wasRunning = true;
  1018. Thread.Sleep(100);
  1019. }
  1020. }
  1021. int assetReferenceTotal = TouchAllSceneAssets(true);
  1022. GC.Collect();
  1023. lock(timerLock)
  1024. {
  1025. if(wasRunning)
  1026. {
  1027. m_CacheCleanTimer.Start();
  1028. m_timerRunning = true;
  1029. }
  1030. m_cleanupRunning = false;
  1031. }
  1032. con.OutputFormat("Completed check with {0} assets.", assetReferenceTotal);
  1033. }, null, "TouchAllSceneAssets");
  1034. break;
  1035. case "expire":
  1036. if (cmdparams.Length < 3)
  1037. {
  1038. con.OutputFormat("Invalid parameters for Expire, please specify a valid date & time", cmd);
  1039. break;
  1040. }
  1041. string s_expirationDate = "";
  1042. DateTime expirationDate;
  1043. if (cmdparams.Length > 3)
  1044. {
  1045. s_expirationDate = string.Join(" ", cmdparams, 2, cmdparams.Length - 2);
  1046. }
  1047. else
  1048. {
  1049. s_expirationDate = cmdparams[2];
  1050. }
  1051. if (!DateTime.TryParse(s_expirationDate, out expirationDate))
  1052. {
  1053. con.OutputFormat("{0} is not a valid date & time", cmd);
  1054. break;
  1055. }
  1056. if (m_FileCacheEnabled)
  1057. CleanExpiredFiles(m_CacheDirectory, expirationDate);
  1058. else
  1059. con.OutputFormat("File cache not active, not clearing.");
  1060. break;
  1061. default:
  1062. con.OutputFormat("Unknown command {0}", cmd);
  1063. break;
  1064. }
  1065. }
  1066. else if (cmdparams.Length == 1)
  1067. {
  1068. con.Output("fcache assets - Attempt a deep cache of all assets in all scenes");
  1069. con.Output("fcache expire <datetime> - Purge assets older then the specified date & time");
  1070. con.Output("fcache clear [file] [memory] - Remove cached assets");
  1071. con.Output("fcache status - Display cache status");
  1072. }
  1073. }
  1074. #endregion
  1075. #region IAssetService Members
  1076. public AssetMetadata GetMetadata(string id)
  1077. {
  1078. AssetBase asset;
  1079. Get(id, out asset);
  1080. return asset.Metadata;
  1081. }
  1082. public byte[] GetData(string id)
  1083. {
  1084. AssetBase asset;
  1085. Get(id, out asset);
  1086. return asset.Data;
  1087. }
  1088. public bool Get(string id, object sender, AssetRetrieved handler)
  1089. {
  1090. AssetBase asset;
  1091. if (!Get(id, out asset))
  1092. return false;
  1093. handler(id, sender, asset);
  1094. return true;
  1095. }
  1096. public bool[] AssetsExist(string[] ids)
  1097. {
  1098. bool[] exist = new bool[ids.Length];
  1099. for (int i = 0; i < ids.Length; i++)
  1100. {
  1101. exist[i] = Check(ids[i]);
  1102. }
  1103. return exist;
  1104. }
  1105. public string Store(AssetBase asset)
  1106. {
  1107. if (asset.FullID == UUID.Zero)
  1108. {
  1109. asset.FullID = UUID.Random();
  1110. }
  1111. Cache(asset);
  1112. return asset.ID;
  1113. }
  1114. public bool UpdateContent(string id, byte[] data)
  1115. {
  1116. AssetBase asset;
  1117. if (!Get(id, out asset))
  1118. return false;
  1119. asset.Data = data;
  1120. Cache(asset);
  1121. return true;
  1122. }
  1123. public bool Delete(string id)
  1124. {
  1125. Expire(id);
  1126. return true;
  1127. }
  1128. #endregion
  1129. }
  1130. }