From 5986b4ee3980d27d6e8524c4b8f5ad4c9a701288 Mon Sep 17 00:00:00 2001 From: UbitUmarov Date: Sat, 13 Oct 2012 22:30:34 +0100 Subject: add mesh cache expire on region startup. Expires will be relative to previus expire (assumed done only once at startup). File 'cntr' on cache folder stores time. Deleting it will force a skip on expire. Default time is 48hours before previus startup to account for failed ones etc. --- OpenSim/Region/Physics/Manager/IMesher.cs | 1 + OpenSim/Region/Physics/Manager/ZeroMesher.cs | 1 + OpenSim/Region/Physics/Meshing/Meshmerizer.cs | 1 + OpenSim/Region/Physics/UbitMeshing/Meshmerizer.cs | 88 ++++++++++++++++++++++- 4 files changed, 88 insertions(+), 3 deletions(-) (limited to 'OpenSim/Region') diff --git a/OpenSim/Region/Physics/Manager/IMesher.cs b/OpenSim/Region/Physics/Manager/IMesher.cs index 21a5fa0..ecc2918 100644 --- a/OpenSim/Region/Physics/Manager/IMesher.cs +++ b/OpenSim/Region/Physics/Manager/IMesher.cs @@ -41,6 +41,7 @@ namespace OpenSim.Region.Physics.Manager IMesh GetMesh(String primName, PrimitiveBaseShape primShape, Vector3 size, float lod, bool isPhysical, bool convex); void ReleaseMesh(IMesh mesh); void ExpireReleaseMeshs(); + void ExpireFileCache(); } // Values for level of detail to be passed to the mesher. diff --git a/OpenSim/Region/Physics/Manager/ZeroMesher.cs b/OpenSim/Region/Physics/Manager/ZeroMesher.cs index 1411165..16846e6 100644 --- a/OpenSim/Region/Physics/Manager/ZeroMesher.cs +++ b/OpenSim/Region/Physics/Manager/ZeroMesher.cs @@ -87,5 +87,6 @@ namespace OpenSim.Region.Physics.Manager public void ReleaseMesh(IMesh mesh) { } public void ExpireReleaseMeshs() { } + public void ExpireFileCache() { } } } diff --git a/OpenSim/Region/Physics/Meshing/Meshmerizer.cs b/OpenSim/Region/Physics/Meshing/Meshmerizer.cs index fd4ac7f..f629c4d 100644 --- a/OpenSim/Region/Physics/Meshing/Meshmerizer.cs +++ b/OpenSim/Region/Physics/Meshing/Meshmerizer.cs @@ -755,5 +755,6 @@ namespace OpenSim.Region.Physics.Meshing public void ReleaseMesh(IMesh imesh) { } public void ExpireReleaseMeshs() { } + public void ExpireFileCache() { } } } diff --git a/OpenSim/Region/Physics/UbitMeshing/Meshmerizer.cs b/OpenSim/Region/Physics/UbitMeshing/Meshmerizer.cs index 35eabd4..6550b66 100644 --- a/OpenSim/Region/Physics/UbitMeshing/Meshmerizer.cs +++ b/OpenSim/Region/Physics/UbitMeshing/Meshmerizer.cs @@ -76,6 +76,8 @@ namespace OpenSim.Region.Physics.Meshing public bool doMeshFileCache = true; public string cachePath = "MeshCache"; + public TimeSpan CacheExpire; + public bool doCacheExpire = true; // const string baseDir = "rawFiles"; private const string baseDir = null; //"rawFiles"; @@ -92,17 +94,29 @@ namespace OpenSim.Region.Physics.Meshing IConfig start_config = config.Configs["Startup"]; IConfig mesh_config = config.Configs["Mesh"]; + + float fcache = 48.0f; +// float fcache = 0.02f; + if(mesh_config != null) { useMeshiesPhysicsMesh = mesh_config.GetBoolean("UseMeshiesPhysicsMesh", useMeshiesPhysicsMesh); - if(useMeshiesPhysicsMesh) + if (useMeshiesPhysicsMesh) { doMeshFileCache = mesh_config.GetBoolean("MeshFileCache", doMeshFileCache); cachePath = mesh_config.GetString("MeshFileCachePath", cachePath); + fcache = mesh_config.GetFloat("MeshFileCacheExpireHours", fcache); + doCacheExpire = mesh_config.GetBoolean("MeshFileCacheDoExpire", doCacheExpire); } else + { doMeshFileCache = false; + doCacheExpire = false; + } } + + CacheExpire = TimeSpan.FromHours(fcache); + } /// @@ -1273,6 +1287,7 @@ namespace OpenSim.Region.Physics.Meshing BinaryFormatter bformatter = new BinaryFormatter(); mesh = Mesh.FromStream(stream, key); + } catch (Exception e) { @@ -1281,11 +1296,14 @@ namespace OpenSim.Region.Physics.Meshing "[MESH CACHE]: Failed to get file {0}. Exception {1} {2}", filename, e.Message, e.StackTrace); } + if (stream != null) stream.Close(); if (mesh == null || !ok) File.Delete(filename); + else + File.SetLastAccessTimeUtc(filename, DateTime.UtcNow); } } @@ -1326,8 +1344,72 @@ namespace OpenSim.Region.Physics.Meshing if (stream != null) stream.Close(); - if (!ok && File.Exists(filename)) - File.Delete(filename); + if (File.Exists(filename)) + { + if (ok) + File.SetLastAccessTimeUtc(filename, DateTime.UtcNow); + else + File.Delete(filename); + } + } + } + + public void ExpireFileCache() + { + if (!doCacheExpire) + return; + + string controlfile = System.IO.Path.Combine(cachePath, "cntr"); + + lock (diskLock) + { + try + { + if (File.Exists(controlfile)) + { + int ndeleted = 0; + int totalfiles = 0; + int ndirs = 0; + DateTime OlderTime = File.GetLastAccessTimeUtc(controlfile) - CacheExpire; + File.SetLastAccessTimeUtc(controlfile, DateTime.UtcNow); + + foreach (string dir in Directory.GetDirectories(cachePath)) + { + try + { + foreach (string file in Directory.GetFiles(dir)) + { + try + { + if (File.GetLastAccessTimeUtc(file) < OlderTime) + { + File.Delete(file); + ndeleted++; + } + } + catch { } + totalfiles++; + } + } + catch { } + ndirs++; + } + + if (ndeleted == 0) + m_log.InfoFormat("[MESH CACHE]: {0} Files in {1} cache folders, no expires", + totalfiles,ndirs); + else + m_log.InfoFormat("[MESH CACHE]: {0} Files in {1} cache folders, expired {2} files accessed before {3}", + totalfiles,ndirs, ndeleted, OlderTime.ToString()); + } + else + { + m_log.Info("[MESH CACHE]: Expire delayed to next startup"); + FileStream fs = File.Create(controlfile,4096,FileOptions.WriteThrough); + fs.Close(); + } + } + catch { } } } } -- cgit v1.1