RegionInfoCache.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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.Threading;
  29. using System.Runtime.InteropServices;
  30. using System.Collections.Generic;
  31. using OpenSim.Framework;
  32. using OpenMetaverse;
  33. using log4net;
  34. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  35. using Timer = System.Threading.Timer;
  36. using ReaderWriterLockSlim = System.Threading.ReaderWriterLockSlim;
  37. namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid
  38. {
  39. public class RegionInfoCache
  40. {
  41. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. private const int CACHE_EXPIRATION_SECONDS = 120; // 2 minutes opensim regions change a lot
  43. private const int CACHE_PURGE_TIME = 60000; // milliseconds
  44. public const ulong HANDLEMASK = 0xffffff00ffffff00ul;
  45. public const ulong HANDLECOORDMASK = 0xffffff00ul;
  46. private static readonly object m_creationLock = new object();
  47. private static readonly Dictionary<ulong, int> m_expireControl = new Dictionary<ulong, int>();
  48. private static readonly Dictionary<ulong, GridRegion> m_byHandler = new Dictionary<ulong, GridRegion>();
  49. private static readonly Dictionary<string, GridRegion> m_byName = new Dictionary<string, GridRegion>();
  50. private static readonly Dictionary<UUID, GridRegion> m_byUUID = new Dictionary<UUID, GridRegion>();
  51. // includes handles to the inside of large regions
  52. private static readonly Dictionary<ulong, GridRegion> m_innerHandles = new Dictionary<ulong, GridRegion>();
  53. //private static bool disposed;
  54. private static ReaderWriterLockSlim m_rwLock;
  55. private static Timer m_timer;
  56. private static double starttimeS;
  57. public RegionInfoCache()
  58. {
  59. lock(m_creationLock)
  60. {
  61. if(m_rwLock == null)
  62. {
  63. starttimeS = Util.GetTimeStamp();
  64. m_rwLock = new ReaderWriterLockSlim();
  65. if (m_timer == null)
  66. m_timer = new Timer(PurgeCache, null, CACHE_PURGE_TIME, Timeout.Infinite);
  67. }
  68. }
  69. }
  70. /* not if static
  71. ~RegionInfoCache()
  72. {
  73. Dispose(false);
  74. }
  75. public void Dispose()
  76. {
  77. Dispose(true);
  78. GC.SuppressFinalize(this);
  79. }
  80. private void Dispose(bool disposing)
  81. {
  82. if (!disposed)
  83. {
  84. disposed = true;
  85. if (m_rwLock != null)
  86. {
  87. m_rwLock.Dispose();
  88. m_rwLock = null;
  89. }
  90. if (m_timer != null)
  91. {
  92. m_timer.Dispose();
  93. m_timer = null;
  94. }
  95. }
  96. }
  97. */
  98. public bool AddOrUpdate(GridRegion rinfo)
  99. {
  100. return AddOrUpdate(rinfo, CACHE_EXPIRATION_SECONDS);
  101. }
  102. public bool AddOrUpdate(GridRegion rinfo, int expire)
  103. {
  104. //if (rinfo == null || disposed)
  105. if (rinfo == null)
  106. return false;
  107. bool gotLock = false;
  108. try
  109. {
  110. try { }
  111. finally
  112. {
  113. m_rwLock.EnterWriteLock();
  114. gotLock = true;
  115. }
  116. int newexpire = (int)(Util.GetTimeStamp() - starttimeS) + expire;
  117. ulong handle = rinfo.RegionHandle & HANDLEMASK;
  118. if (m_expireControl.ContainsKey(handle))
  119. {
  120. if (m_expireControl[handle] < newexpire)
  121. m_expireControl[handle] = newexpire;
  122. }
  123. else
  124. {
  125. m_expireControl[handle] = newexpire;
  126. }
  127. if (m_innerHandles.TryGetValue(handle, out GridRegion oldr))
  128. removeFromInner(oldr);
  129. addToInner(rinfo);
  130. m_byHandler[handle] = rinfo;
  131. m_byName[rinfo.RegionName.ToLowerInvariant()] = rinfo;
  132. m_byUUID[rinfo.RegionID] = rinfo;
  133. return true;
  134. }
  135. finally
  136. {
  137. if (gotLock)
  138. m_rwLock.ExitWriteLock();
  139. }
  140. }
  141. public void Cache(GridRegion rinfo)
  142. {
  143. AddOrUpdate(rinfo, CACHE_EXPIRATION_SECONDS);
  144. }
  145. public void Cache(GridRegion rinfo, int expireSeconds)
  146. {
  147. AddOrUpdate(rinfo, expireSeconds);
  148. }
  149. public void Cache(GridRegion rinfo, float expireSeconds)
  150. {
  151. AddOrUpdate(rinfo, (int)expireSeconds);
  152. }
  153. public void Cache(UUID scopeID, GridRegion rinfo)
  154. {
  155. AddOrUpdate(rinfo, CACHE_EXPIRATION_SECONDS);
  156. }
  157. public void CacheLocal(GridRegion rinfo)
  158. {
  159. AddOrUpdate(rinfo, 100000);
  160. }
  161. public void CacheNearNeighbour(UUID scopeID, GridRegion rinfo)
  162. {
  163. AddOrUpdate(rinfo, CACHE_EXPIRATION_SECONDS);
  164. }
  165. public void Cache(UUID scopeID, GridRegion rinfo, int expireSeconds)
  166. {
  167. AddOrUpdate(rinfo, expireSeconds);
  168. }
  169. public void Cache(UUID scopeID, GridRegion rinfo, float expireSeconds)
  170. {
  171. AddOrUpdate(rinfo, (int)expireSeconds);
  172. }
  173. public void Clear()
  174. {
  175. //if (disposed)
  176. // return;
  177. bool gotLock = false;
  178. try
  179. {
  180. try { }
  181. finally
  182. {
  183. m_rwLock.EnterWriteLock();
  184. gotLock = true;
  185. }
  186. m_expireControl.Clear();
  187. m_byHandler.Clear();
  188. m_byName.Clear();
  189. m_byUUID.Clear();
  190. m_innerHandles.Clear();
  191. }
  192. finally
  193. {
  194. if (gotLock)
  195. m_rwLock.ExitWriteLock();
  196. }
  197. }
  198. public bool Contains(ulong handle)
  199. {
  200. //if (disposed)
  201. // return false;
  202. bool gotLock = false;
  203. try
  204. {
  205. try { }
  206. finally
  207. {
  208. m_rwLock.EnterReadLock();
  209. gotLock = true;
  210. }
  211. return m_byHandler.ContainsKey(handle & HANDLEMASK);
  212. }
  213. finally
  214. {
  215. if (gotLock)
  216. m_rwLock.ExitReadLock();
  217. }
  218. }
  219. public bool Contains(GridRegion rinfo)
  220. {
  221. //if (disposed)
  222. // return false;
  223. bool gotLock = false;
  224. try
  225. {
  226. try { }
  227. finally
  228. {
  229. m_rwLock.EnterReadLock();
  230. gotLock = true;
  231. }
  232. if(!m_byHandler.TryGetValue(rinfo.RegionHandle & HANDLEMASK, out GridRegion rcur))
  233. return false;
  234. return rcur.RegionID == rinfo.RegionID &&
  235. rcur.RegionSizeX == rinfo.RegionSizeX &&
  236. rcur.RegionSizeY == rinfo.RegionSizeY;
  237. }
  238. finally
  239. {
  240. if (gotLock)
  241. m_rwLock.ExitReadLock();
  242. }
  243. }
  244. public bool Contains(UUID scope, ulong handle)
  245. {
  246. return Contains(handle);
  247. }
  248. public bool Contains(UUID scope, GridRegion rinfo)
  249. {
  250. return Contains(rinfo);
  251. }
  252. public int Count()
  253. {
  254. //if (disposed)
  255. // return 0;
  256. bool gotLock = false;
  257. try
  258. {
  259. try { }
  260. finally
  261. {
  262. m_rwLock.EnterReadLock();
  263. gotLock = true;
  264. }
  265. return m_byName.Count;
  266. }
  267. finally
  268. {
  269. if (gotLock)
  270. m_rwLock.ExitReadLock();
  271. }
  272. }
  273. public void Remove(GridRegion rinfo)
  274. {
  275. if (rinfo == null)
  276. return;
  277. bool gotLock = false;
  278. try
  279. {
  280. try { }
  281. finally
  282. {
  283. m_rwLock.EnterWriteLock();
  284. gotLock = true;
  285. }
  286. m_byName.Remove(rinfo.RegionName.ToLowerInvariant());
  287. m_byUUID.Remove(rinfo.RegionID);
  288. ulong handle = rinfo.RegionHandle & HANDLEMASK;
  289. m_byHandler.Remove(handle);
  290. removeFromInner(rinfo);
  291. if (m_expireControl.ContainsKey(handle))
  292. {
  293. m_expireControl.Remove(handle);
  294. if (m_expireControl.Count == 0)
  295. {
  296. m_byHandler.Clear();
  297. m_byName.Clear();
  298. m_byUUID.Clear();
  299. m_innerHandles.Clear();
  300. }
  301. }
  302. }
  303. finally
  304. {
  305. if (gotLock)
  306. m_rwLock.ExitWriteLock();
  307. }
  308. }
  309. public void Remove(ulong regionHandle)
  310. {
  311. //if(disposed)
  312. // return;
  313. bool gotLock = false;
  314. try
  315. {
  316. try { }
  317. finally
  318. {
  319. m_rwLock.EnterWriteLock();
  320. gotLock = true;
  321. }
  322. regionHandle &= HANDLEMASK;
  323. if (m_byHandler.TryGetValue(regionHandle, out GridRegion r))
  324. {
  325. m_byName.Remove(r.RegionName.ToLowerInvariant());
  326. m_byUUID.Remove(r.RegionID);
  327. removeFromInner(r);
  328. m_byHandler.Remove(regionHandle);
  329. }
  330. if (m_expireControl.ContainsKey(regionHandle))
  331. {
  332. m_expireControl.Remove(regionHandle);
  333. if (m_expireControl.Count == 0)
  334. {
  335. m_byHandler.Clear();
  336. m_byName.Clear();
  337. m_byUUID.Clear();
  338. m_innerHandles.Clear();
  339. }
  340. }
  341. }
  342. finally
  343. {
  344. if (gotLock)
  345. m_rwLock.ExitWriteLock();
  346. }
  347. }
  348. public void Remove(UUID scopeID, GridRegion rinfo)
  349. {
  350. Remove(rinfo);
  351. }
  352. public void Remove(UUID scopeID, ulong regionHandle)
  353. {
  354. Remove(regionHandle);
  355. }
  356. public GridRegion Get(UUID scopeID, UUID regionID, out bool inCache)
  357. {
  358. inCache = TryGet(regionID, out GridRegion rinfo);
  359. return rinfo;
  360. }
  361. public GridRegion Get(UUID scopeID, ulong handle, out bool inCache)
  362. {
  363. inCache = TryGet(handle, out GridRegion rinfo);
  364. return rinfo;
  365. }
  366. public GridRegion Get(UUID scopeID, string name, out bool inCache)
  367. {
  368. inCache = TryGet(name, out GridRegion rinfo);
  369. return rinfo;
  370. }
  371. public GridRegion Get(UUID scopeID, uint x, uint y, out bool inCache)
  372. {
  373. inCache = TryGet(x, y, out GridRegion rinfo);
  374. return rinfo;
  375. }
  376. public bool TryGet(UUID regionID, out GridRegion rinfo)
  377. {
  378. /*
  379. if (disposed)
  380. {
  381. rinfo = null;
  382. return false;
  383. }
  384. */
  385. bool gotLock = false;
  386. try
  387. {
  388. try { }
  389. finally
  390. {
  391. m_rwLock.EnterReadLock();
  392. gotLock = true;
  393. }
  394. return m_byUUID.TryGetValue(regionID, out rinfo);
  395. }
  396. finally
  397. {
  398. if (gotLock)
  399. m_rwLock.ExitReadLock();
  400. }
  401. }
  402. public bool TryGet(ulong handle, out GridRegion rinfo)
  403. {
  404. /*
  405. if (disposed)
  406. {
  407. rinfo = null;
  408. return false;
  409. }
  410. */
  411. bool gotLock = false;
  412. try
  413. {
  414. try { }
  415. finally
  416. {
  417. m_rwLock.EnterReadLock();
  418. gotLock = true;
  419. }
  420. handle &= HANDLEMASK;
  421. if (m_byHandler.TryGetValue(handle, out rinfo))
  422. return true;
  423. return m_innerHandles.TryGetValue(handle, out rinfo);
  424. }
  425. finally
  426. {
  427. if (gotLock)
  428. m_rwLock.ExitReadLock();
  429. }
  430. }
  431. public bool TryGet(string name, out GridRegion rinfo)
  432. {
  433. /*
  434. if (disposed)
  435. {
  436. rinfo = null;
  437. return false;
  438. }
  439. */
  440. bool gotLock = false;
  441. try
  442. {
  443. try { }
  444. finally
  445. {
  446. m_rwLock.EnterReadLock();
  447. gotLock = true;
  448. }
  449. return m_byName.TryGetValue(name.ToLowerInvariant(), out rinfo);
  450. }
  451. finally
  452. {
  453. if (gotLock)
  454. m_rwLock.ExitReadLock();
  455. }
  456. }
  457. public bool TryGet(uint x, uint y, out GridRegion rinfo)
  458. {
  459. /*
  460. if (disposed)
  461. {
  462. rinfo = null;
  463. return false;
  464. }
  465. */
  466. bool gotLock = false;
  467. try
  468. {
  469. try { }
  470. finally
  471. {
  472. m_rwLock.EnterReadLock();
  473. gotLock = true;
  474. }
  475. ulong handle = x & HANDLECOORDMASK;
  476. handle <<= 32;
  477. handle |= y & HANDLECOORDMASK;
  478. if (m_byHandler.TryGetValue(handle, out rinfo))
  479. return true;
  480. if (!m_innerHandles.TryGetValue(handle, out rinfo))
  481. return false;
  482. // extra check, possible redundant
  483. int test = rinfo.RegionLocX;
  484. if (x < test)
  485. {
  486. rinfo = null;
  487. return false;
  488. }
  489. test += rinfo.RegionSizeX;
  490. if (x >= test)
  491. {
  492. rinfo = null;
  493. return false;
  494. }
  495. test = rinfo.RegionLocY;
  496. if (y < test)
  497. {
  498. rinfo = null;
  499. return false;
  500. }
  501. test += rinfo.RegionSizeY;
  502. if (y < test)
  503. return true;
  504. rinfo = null;
  505. return false;
  506. }
  507. finally
  508. {
  509. if (gotLock)
  510. m_rwLock.ExitReadLock();
  511. }
  512. }
  513. private void PurgeCache(object ignored)
  514. {
  515. //if(disposed || m_expireControl.Count == 0)
  516. if (m_expireControl.Count == 0)
  517. return;
  518. bool gotLock = false;
  519. try
  520. {
  521. try { }
  522. finally
  523. {
  524. m_rwLock.EnterWriteLock();
  525. gotLock = true;
  526. }
  527. int now = (int)(Util.GetTimeStamp() - starttimeS);
  528. List<ulong> toexpire = new List<ulong>(m_expireControl.Count);
  529. foreach (KeyValuePair<ulong, int> kvp in m_expireControl)
  530. {
  531. if (kvp.Value < now)
  532. toexpire.Add(kvp.Key);
  533. }
  534. if (toexpire.Count == 0)
  535. return;
  536. ulong h;
  537. for (int i = 0; i < toexpire.Count; i++)
  538. {
  539. h = toexpire[i];
  540. if (m_byHandler.TryGetValue(h, out GridRegion r))
  541. {
  542. m_byName.Remove(r.RegionName.ToLowerInvariant());
  543. m_byUUID.Remove(r.RegionID);
  544. removeFromInner(r);
  545. m_byHandler.Remove(h);
  546. }
  547. m_expireControl.Remove(h);
  548. }
  549. }
  550. finally
  551. {
  552. if (gotLock)
  553. m_rwLock.ExitWriteLock();
  554. if (m_timer != null)
  555. m_timer.Change(CACHE_PURGE_TIME, Timeout.Infinite);
  556. }
  557. }
  558. private void addToInner(GridRegion region)
  559. {
  560. int rsx = region.RegionSizeX;
  561. int rsy = region.RegionSizeY;
  562. if (rsx < 257 && rsy < 257)
  563. return;
  564. rsx >>= 8;
  565. rsy >>= 8;
  566. ulong handle = region.RegionHandle & HANDLEMASK;
  567. fastRegionHandle fh = new fastRegionHandle(handle);
  568. uint startY = fh.y;
  569. for (int i = 0; i < rsx; i++)
  570. {
  571. for (int j = 0; j < rsy; j++)
  572. {
  573. m_innerHandles[fh.toHandle()] = region;
  574. fh.y += 256;
  575. }
  576. fh.y = startY;
  577. fh.x += 256;
  578. }
  579. }
  580. private void removeFromInner(GridRegion region)
  581. {
  582. int rsx = region.RegionSizeX;
  583. int rsy = region.RegionSizeY;
  584. if (rsx < 257 && rsy < 257)
  585. return;
  586. rsx >>= 8;
  587. rsy >>= 8;
  588. ulong handle = region.RegionHandle & HANDLEMASK;
  589. fastRegionHandle fh = new fastRegionHandle(handle);
  590. uint startY = fh.y;
  591. for (int i = 0; i < rsx; i++)
  592. {
  593. for (int j = 0; j < rsy; j++)
  594. {
  595. m_innerHandles.Remove(fh.toHandle());
  596. fh.y += 256;
  597. }
  598. fh.y = startY;
  599. fh.x += 256;
  600. }
  601. }
  602. }
  603. [StructLayout(LayoutKind.Explicit, Size = 8, Pack = 8)]
  604. public class fastRegionHandle
  605. {
  606. [FieldOffset(0)] public ulong handle;
  607. [FieldOffset(0)] public uint y;
  608. [FieldOffset(4)] public uint x;
  609. public fastRegionHandle(ulong h)
  610. {
  611. handle = h;
  612. if (!BitConverter.IsLittleEndian)
  613. {
  614. x = (uint)(handle >> 32);
  615. y = (uint)handle;
  616. }
  617. y &= 0xffffff00;
  618. x &= 0xffffff00;
  619. }
  620. public fastRegionHandle(uint px, uint py)
  621. {
  622. y = py & 0xffffff00;
  623. x = px & 0xffffff00;
  624. }
  625. public ulong toHandle()
  626. {
  627. if (BitConverter.IsLittleEndian)
  628. return handle;
  629. else
  630. return (ulong)x << 32 | y ;
  631. }
  632. public static bool operator ==(fastRegionHandle value1, fastRegionHandle value2)
  633. {
  634. return value1.handle == value2.handle;
  635. }
  636. public static bool operator !=(fastRegionHandle value1, fastRegionHandle value2)
  637. {
  638. return value1.handle != value2.handle;
  639. }
  640. public override int GetHashCode()
  641. {
  642. return handle.GetHashCode();
  643. }
  644. public override bool Equals(Object obj)
  645. {
  646. if(obj == null)
  647. return false;
  648. fastRegionHandle p = obj as fastRegionHandle;
  649. return p.handle == handle;
  650. }
  651. }
  652. }