[VD] TV customization of Simple Disk Cache 22/320622/3
authorLiu Feifei <feifei08.liu@samsung.com>
Tue, 19 Nov 2024 20:43:42 +0000 (04:43 +0800)
committerBot Blink <blinkbot@samsung.com>
Thu, 21 Nov 2024 05:42:33 +0000 (05:42 +0000)
1.Check the disk life time before Simple Disk Cache created
2.Do eviction based on the overall size control(100M) of webapps' simple disk cache

Change-Id: I2cea02d081ed5ce09304e5b8f2d5a41a783c5908
Signed-off-by: Liu Feifei <feifei08.liu@samsung.com>
net/disk_cache/cache_util.h
net/disk_cache/cache_util_posix.cc
net/disk_cache/simple/simple_backend_impl.cc
net/disk_cache/simple/simple_backend_impl.h
net/disk_cache/simple/simple_entry_impl.cc
net/disk_cache/simple/simple_index_file.h

index 69bf2f52808f061ee2bb27f1534c6a92193cf3ba..c03fdc6beac8c57b2f60a33ebaebb2ee719f8b2e 100644 (file)
@@ -66,6 +66,9 @@ int CalculateWebAppCacheSize(const base::FilePath& path, net::CacheType type);
 // Web apps disk cache path prefix.
 const char kTizenAppCache[] = "WebAppsCache";
 const char kIndexName[] = "index";
+const char kCacheDataDirectoryName[] = "Cache_Data";
+const char kIndexDir[] = "index-dir";
+const char kSimpleIndexName[] = "the-real-index";
 #endif
 
 // The default cache size should not ideally be exposed, but the blockfile
index 024c7a1fe42021f5994228ff760fd1f471a27fdd..962c61bcb15ba93987a5271d04824e366b988583 100644 (file)
@@ -12,6 +12,7 @@
 
 #if BUILDFLAG(IS_TIZEN_TV)
 #include "net/disk_cache/blockfile/disk_format.h"
+#include "net/disk_cache/simple/simple_index_file.h"
 #endif
 
 namespace disk_cache {
@@ -99,16 +100,18 @@ int CalculateWebAppCacheSize(const base::FilePath& path, net::CacheType type) {
     return -1;
 
   int total = 0;
-  base::FileEnumerator iter(path.DirName(), false,
+  base::FileEnumerator iter(path.DirName().DirName(), false,
                             base::FileEnumerator::DIRECTORIES);
   for (base::FilePath app_name = iter.Next(); !app_name.value().empty();
        app_name = iter.Next()) {
-    base::FilePath index_path = app_name.Append(kIndexName);
-    disk_cache::IndexHeader header;
+    base::FilePath cache_path = app_name.Append(kCacheDataDirectoryName);
+    base::FilePath index_dir_path = cache_path.Append(kIndexDir);
+    base::FilePath index_path = index_dir_path.Append(kSimpleIndexName);
+    disk_cache::SimpleIndexHeader header;
     if (!ReadHeader(index_path, reinterpret_cast<char*>(&header),
                     sizeof(header)))
       continue;
-    total += header.num_bytes;
+    total += (header.cache_size >> 32);
   }
 
   return total;
index 41dddb9f4a2e8f4adecacb0fed58c5652bc5505c..9dd45a4a9348f5d65a565c417059301885658714 100644 (file)
@@ -705,6 +705,17 @@ SimpleBackendImpl::DiskStatResult SimpleBackendImpl::InitCacheStructureOnDisk(
   DiskStatResult result;
   result.max_size = suggested_max_size;
   result.net_error = net::OK;
+
+#if BUILDFLAG(IS_TIZEN_TV)
+  int life_time = disk_cache::GetMemoryLifeTime();
+  if (life_time <= 0 || life_time > 5) {
+    LOG(ERROR) << "MemoryLifeTime was dangerous for using cache, life_time : "
+               << life_time << ", path : " << path.value();
+    result.net_error = net::ERR_FAILED;
+    return result;
+  }
+#endif
+
   SimpleCacheConsistencyResult consistency =
       FileStructureConsistent(file_operations.get(), path);
   SIMPLE_CACHE_UMA(ENUMERATION, "ConsistencyResult", cache_type, consistency);
@@ -882,4 +893,25 @@ uint32_t SimpleBackendImpl::GetNewEntryPriority(
          entry_count_++;
 }
 
+#if BUILDFLAG(IS_TIZEN_TV)
+void SimpleBackendImpl::UpdateMaxCacheSize() {
+  int used_size = CalculateWebAppCacheSize(path_, GetCacheType());
+  if (used_size == -1)
+    return;
+  uint64_t cur_app_cache_size = index()->GetCacheSize();
+  if (used_size < orig_max_size_) {
+    orig_max_size_ = orig_max_size_ - used_size + cur_app_cache_size;
+  } else if (cur_app_cache_size * 2 >= orig_max_size_) {
+    orig_max_size_ =
+        (cur_app_cache_size > orig_max_size_ ? orig_max_size_
+                                             : cur_app_cache_size) /
+        2;
+  } else {
+    orig_max_size_ = cur_app_cache_size;
+  }
+  already_update_ = true;
+  index_->SetMaxSize(orig_max_size_);
+}
+#endif
+
 }  // namespace disk_cache
index cde0eeebd8225413cdbf12a2a61bc519fc95646b..8bb1b39989af3e1ac7edc61b5f8dc187ca2f8db6 100644 (file)
@@ -150,6 +150,11 @@ class NET_EXPORT_PRIVATE SimpleBackendImpl : public Backend,
   }
 #endif
 
+#if BUILDFLAG(IS_TIZEN_TV)
+  void UpdateMaxCacheSize();
+  bool AlreadyUpdate() { return already_update_; }
+#endif
+
  private:
   class SimpleIterator;
   friend class SimpleIterator;
@@ -282,6 +287,9 @@ class NET_EXPORT_PRIVATE SimpleBackendImpl : public Backend,
 #if BUILDFLAG(IS_ANDROID)
   ApplicationStatusListenerGetter app_status_listener_getter_;
 #endif
+#if BUILDFLAG(IS_TIZEN_TV)
+  bool already_update_ = false;
+#endif
 };
 
 }  // namespace disk_cache
index 6aaeabbffc55926a09a420db2516d967d404cbcc..3b25e332247818df3ce3542d95e69eea79c5b550 100644 (file)
@@ -1647,6 +1647,11 @@ void SimpleEntryImpl::UpdateDataFromEntryStat(
   sparse_data_size_ = entry_stat.sparse_data_size();
 
   SimpleBackendImpl* backend_ptr = backend_.get();
+#if BUILDFLAG(IS_TIZEN_TV)
+  if (backend_ptr->index()->initialized() && !backend_ptr->AlreadyUpdate()) {
+    backend_ptr->UpdateMaxCacheSize();
+  }
+#endif
   if (doom_state_ == DOOM_NONE && backend_ptr) {
     backend_ptr->index()->UpdateEntrySize(
         entry_hash_, base::checked_cast<uint32_t>(GetDiskUsage()));
index fed23c50c527f59d9c5e12317892b3d7ba18d1fa..efc9cb6ece10ad44bd1e32aae19c3594d77b1171 100644 (file)
@@ -41,6 +41,21 @@ struct NET_EXPORT_PRIVATE SimpleIndexLoadResult {
   bool flush_required = false;
 };
 
+// Header for the master index file.
+struct NET_EXPORT_PRIVATE SimpleIndexHeader {
+  SimpleIndexHeader() {
+    memset(this, 0, sizeof(*this));
+    magic_number = kSimpleIndexMagicNumber;
+    version = kSimpleVersion;
+  }
+
+  uint64_t magic_number;
+  uint32_t version;
+  SimpleIndex::IndexWriteToDiskReason reason;
+  uint64_t entry_count;
+  uint64_t cache_size;  // Total cache storage size in bytes.
+};
+
 // Simple Index File format is a pickle of IndexMetadata and EntryMetadata
 // objects. The file format is as follows: one instance of |IndexMetadata|
 // followed by |EntryMetadata| repeated |entry_count| times. To learn more about