| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <cstdlib> | 5 #include <cstdlib> |
| 6 #include <fstream> | 6 #include <fstream> |
| 7 #include <iostream> | 7 #include <iostream> |
| 8 #include <memory> |
| 8 #include <string> | 9 #include <string> |
| 9 #include <vector> | 10 #include <vector> |
| 10 | 11 |
| 11 #include "base/at_exit.h" | 12 #include "base/at_exit.h" |
| 12 #include "base/bind.h" | 13 #include "base/bind.h" |
| 13 #include "base/callback.h" | 14 #include "base/callback.h" |
| 14 #include "base/command_line.h" | 15 #include "base/command_line.h" |
| 15 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
| 16 #include "base/logging.h" | 17 #include "base/logging.h" |
| 17 #include "base/memory/scoped_ptr.h" | |
| 18 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/string_piece.h" | 20 #include "base/strings/string_piece.h" |
| 21 #include "base/strings/string_split.h" | 21 #include "base/strings/string_split.h" |
| 22 #include "base/strings/string_util.h" | 22 #include "base/strings/string_util.h" |
| 23 #include "base/strings/stringprintf.h" | 23 #include "base/strings/stringprintf.h" |
| 24 #include "base/thread_task_runner_handle.h" | 24 #include "base/thread_task_runner_handle.h" |
| 25 #include "net/base/cache_type.h" | 25 #include "net/base/cache_type.h" |
| 26 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
| 27 #include "net/disk_cache/disk_cache.h" | 27 #include "net/disk_cache/disk_cache.h" |
| 28 #include "net/disk_cache/simple/simple_backend_impl.h" | 28 #include "net/disk_cache/simple/simple_backend_impl.h" |
| 29 #include "net/disk_cache/simple/simple_index.h" | 29 #include "net/disk_cache/simple/simple_index.h" |
| 30 | 30 |
| 31 namespace disk_cache { | 31 namespace disk_cache { |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 const char kBlockFileBackendType[] = "block_file"; | 34 const char kBlockFileBackendType[] = "block_file"; |
| 35 const char kSimpleBackendType[] = "simple"; | 35 const char kSimpleBackendType[] = "simple"; |
| 36 | 36 |
| 37 const char kDiskCacheType[] = "disk_cache"; | 37 const char kDiskCacheType[] = "disk_cache"; |
| 38 const char kAppCacheType[] = "app_cache"; | 38 const char kAppCacheType[] = "app_cache"; |
| 39 | 39 |
| 40 const char kPrivateDirty[] = "Private_Dirty:"; | 40 const char kPrivateDirty[] = "Private_Dirty:"; |
| 41 const char kReadWrite[] = "rw-"; | 41 const char kReadWrite[] = "rw-"; |
| 42 const char kHeap[] = "[heap]"; | 42 const char kHeap[] = "[heap]"; |
| 43 const char kKb[] = "kB"; | 43 const char kKb[] = "kB"; |
| 44 | 44 |
| 45 struct CacheSpec { | 45 struct CacheSpec { |
| 46 public: | 46 public: |
| 47 static scoped_ptr<CacheSpec> Parse(const std::string& spec_string) { | 47 static std::unique_ptr<CacheSpec> Parse(const std::string& spec_string) { |
| 48 std::vector<std::string> tokens = base::SplitString( | 48 std::vector<std::string> tokens = base::SplitString( |
| 49 spec_string, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 49 spec_string, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 50 if (tokens.size() != 3) | 50 if (tokens.size() != 3) |
| 51 return scoped_ptr<CacheSpec>(); | 51 return std::unique_ptr<CacheSpec>(); |
| 52 if (tokens[0] != kBlockFileBackendType && tokens[0] != kSimpleBackendType) | 52 if (tokens[0] != kBlockFileBackendType && tokens[0] != kSimpleBackendType) |
| 53 return scoped_ptr<CacheSpec>(); | 53 return std::unique_ptr<CacheSpec>(); |
| 54 if (tokens[1] != kDiskCacheType && tokens[1] != kAppCacheType) | 54 if (tokens[1] != kDiskCacheType && tokens[1] != kAppCacheType) |
| 55 return scoped_ptr<CacheSpec>(); | 55 return std::unique_ptr<CacheSpec>(); |
| 56 return scoped_ptr<CacheSpec>(new CacheSpec( | 56 return std::unique_ptr<CacheSpec>(new CacheSpec( |
| 57 tokens[0] == kBlockFileBackendType ? net::CACHE_BACKEND_BLOCKFILE | 57 tokens[0] == kBlockFileBackendType ? net::CACHE_BACKEND_BLOCKFILE |
| 58 : net::CACHE_BACKEND_SIMPLE, | 58 : net::CACHE_BACKEND_SIMPLE, |
| 59 tokens[1] == kDiskCacheType ? net::DISK_CACHE : net::APP_CACHE, | 59 tokens[1] == kDiskCacheType ? net::DISK_CACHE : net::APP_CACHE, |
| 60 base::FilePath(tokens[2]))); | 60 base::FilePath(tokens[2]))); |
| 61 } | 61 } |
| 62 | 62 |
| 63 const net::BackendType backend_type; | 63 const net::BackendType backend_type; |
| 64 const net::CacheType cache_type; | 64 const net::CacheType cache_type; |
| 65 const base::FilePath path; | 65 const base::FilePath path; |
| 66 | 66 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 78 bool* succeeded, | 78 bool* succeeded, |
| 79 int net_error) { | 79 int net_error) { |
| 80 if (net_error == net::OK) { | 80 if (net_error == net::OK) { |
| 81 *succeeded = true; | 81 *succeeded = true; |
| 82 } else { | 82 } else { |
| 83 *succeeded = false; | 83 *succeeded = false; |
| 84 } | 84 } |
| 85 run_loop->Quit(); | 85 run_loop->Quit(); |
| 86 } | 86 } |
| 87 | 87 |
| 88 scoped_ptr<Backend> CreateAndInitBackend(const CacheSpec& spec) { | 88 std::unique_ptr<Backend> CreateAndInitBackend(const CacheSpec& spec) { |
| 89 scoped_ptr<Backend> result; | 89 std::unique_ptr<Backend> result; |
| 90 scoped_ptr<Backend> backend; | 90 std::unique_ptr<Backend> backend; |
| 91 bool succeeded = false; | 91 bool succeeded = false; |
| 92 base::RunLoop run_loop; | 92 base::RunLoop run_loop; |
| 93 const net::CompletionCallback callback = base::Bind( | 93 const net::CompletionCallback callback = base::Bind( |
| 94 &SetSuccessCodeOnCompletion, | 94 &SetSuccessCodeOnCompletion, |
| 95 base::Unretained(&run_loop), | 95 base::Unretained(&run_loop), |
| 96 base::Unretained(&succeeded)); | 96 base::Unretained(&succeeded)); |
| 97 const int net_error = CreateCacheBackend( | 97 const int net_error = CreateCacheBackend( |
| 98 spec.cache_type, spec.backend_type, spec.path, 0, false, | 98 spec.cache_type, spec.backend_type, spec.path, 0, false, |
| 99 base::ThreadTaskRunnerHandle::Get(), NULL, &backend, callback); | 99 base::ThreadTaskRunnerHandle::Get(), NULL, &backend, callback); |
| 100 if (net_error == net::OK) | 100 if (net_error == net::OK) |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 total_size += size; | 216 total_size += size; |
| 217 is_private_dirty = false; | 217 is_private_dirty = false; |
| 218 } | 218 } |
| 219 if (!std::getline(maps_file, line) || line.empty()) | 219 if (!std::getline(maps_file, line) || line.empty()) |
| 220 return total_size; | 220 return total_size; |
| 221 } | 221 } |
| 222 } | 222 } |
| 223 return total_size; | 223 return total_size; |
| 224 } | 224 } |
| 225 | 225 |
| 226 bool CacheMemTest(const std::vector<scoped_ptr<CacheSpec>>& specs) { | 226 bool CacheMemTest(const std::vector<std::unique_ptr<CacheSpec>>& specs) { |
| 227 std::vector<scoped_ptr<Backend>> backends; | 227 std::vector<std::unique_ptr<Backend>> backends; |
| 228 for (const auto& it : specs) { | 228 for (const auto& it : specs) { |
| 229 scoped_ptr<Backend> backend = CreateAndInitBackend(*it); | 229 std::unique_ptr<Backend> backend = CreateAndInitBackend(*it); |
| 230 if (!backend) | 230 if (!backend) |
| 231 return false; | 231 return false; |
| 232 std::cout << "Number of entries in " << it->path.LossyDisplayName() << " : " | 232 std::cout << "Number of entries in " << it->path.LossyDisplayName() << " : " |
| 233 << backend->GetEntryCount() << std::endl; | 233 << backend->GetEntryCount() << std::endl; |
| 234 backends.push_back(std::move(backend)); | 234 backends.push_back(std::move(backend)); |
| 235 } | 235 } |
| 236 const uint64_t memory_consumption = GetMemoryConsumption(); | 236 const uint64_t memory_consumption = GetMemoryConsumption(); |
| 237 std::cout << "Private dirty memory: " << memory_consumption << " kB" | 237 std::cout << "Private dirty memory: " << memory_consumption << " kB" |
| 238 << std::endl; | 238 << std::endl; |
| 239 return true; | 239 return true; |
| 240 } | 240 } |
| 241 | 241 |
| 242 void PrintUsage(std::ostream* stream) { | 242 void PrintUsage(std::ostream* stream) { |
| 243 *stream << "Usage: disk_cache_mem_test " | 243 *stream << "Usage: disk_cache_mem_test " |
| 244 << "--spec-1=<spec> " | 244 << "--spec-1=<spec> " |
| 245 << "[--spec-2=<spec>]" | 245 << "[--spec-2=<spec>]" |
| 246 << std::endl | 246 << std::endl |
| 247 << " with <cache_spec>=<backend_type>:<cache_type>:<cache_path>" | 247 << " with <cache_spec>=<backend_type>:<cache_type>:<cache_path>" |
| 248 << std::endl | 248 << std::endl |
| 249 << " <backend_type>='block_file'|'simple'" << std::endl | 249 << " <backend_type>='block_file'|'simple'" << std::endl |
| 250 << " <cache_type>='disk_cache'|'app_cache'" << std::endl | 250 << " <cache_type>='disk_cache'|'app_cache'" << std::endl |
| 251 << " <cache_path>=file system path" << std::endl; | 251 << " <cache_path>=file system path" << std::endl; |
| 252 } | 252 } |
| 253 | 253 |
| 254 bool ParseAndStoreSpec(const std::string& spec_str, | 254 bool ParseAndStoreSpec(const std::string& spec_str, |
| 255 std::vector<scoped_ptr<CacheSpec>>* specs) { | 255 std::vector<std::unique_ptr<CacheSpec>>* specs) { |
| 256 scoped_ptr<CacheSpec> spec = CacheSpec::Parse(spec_str); | 256 std::unique_ptr<CacheSpec> spec = CacheSpec::Parse(spec_str); |
| 257 if (!spec) { | 257 if (!spec) { |
| 258 PrintUsage(&std::cerr); | 258 PrintUsage(&std::cerr); |
| 259 return false; | 259 return false; |
| 260 } | 260 } |
| 261 specs->push_back(std::move(spec)); | 261 specs->push_back(std::move(spec)); |
| 262 return true; | 262 return true; |
| 263 } | 263 } |
| 264 | 264 |
| 265 bool Main(int argc, char** argv) { | 265 bool Main(int argc, char** argv) { |
| 266 base::AtExitManager at_exit_manager; | 266 base::AtExitManager at_exit_manager; |
| 267 base::MessageLoopForIO message_loop; | 267 base::MessageLoopForIO message_loop; |
| 268 base::CommandLine::Init(argc, argv); | 268 base::CommandLine::Init(argc, argv); |
| 269 const base::CommandLine& command_line = | 269 const base::CommandLine& command_line = |
| 270 *base::CommandLine::ForCurrentProcess(); | 270 *base::CommandLine::ForCurrentProcess(); |
| 271 if (command_line.HasSwitch("help")) { | 271 if (command_line.HasSwitch("help")) { |
| 272 PrintUsage(&std::cout); | 272 PrintUsage(&std::cout); |
| 273 return true; | 273 return true; |
| 274 } | 274 } |
| 275 if ((command_line.GetSwitches().size() != 1 && | 275 if ((command_line.GetSwitches().size() != 1 && |
| 276 command_line.GetSwitches().size() != 2) || | 276 command_line.GetSwitches().size() != 2) || |
| 277 !command_line.HasSwitch("spec-1") || | 277 !command_line.HasSwitch("spec-1") || |
| 278 (command_line.GetSwitches().size() == 2 && | 278 (command_line.GetSwitches().size() == 2 && |
| 279 !command_line.HasSwitch("spec-2"))) { | 279 !command_line.HasSwitch("spec-2"))) { |
| 280 PrintUsage(&std::cerr); | 280 PrintUsage(&std::cerr); |
| 281 return false; | 281 return false; |
| 282 } | 282 } |
| 283 std::vector<scoped_ptr<CacheSpec>> specs; | 283 std::vector<std::unique_ptr<CacheSpec>> specs; |
| 284 const std::string spec_str_1 = command_line.GetSwitchValueASCII("spec-1"); | 284 const std::string spec_str_1 = command_line.GetSwitchValueASCII("spec-1"); |
| 285 if (!ParseAndStoreSpec(spec_str_1, &specs)) | 285 if (!ParseAndStoreSpec(spec_str_1, &specs)) |
| 286 return false; | 286 return false; |
| 287 if (command_line.HasSwitch("spec-2")) { | 287 if (command_line.HasSwitch("spec-2")) { |
| 288 const std::string spec_str_2 = command_line.GetSwitchValueASCII("spec-2"); | 288 const std::string spec_str_2 = command_line.GetSwitchValueASCII("spec-2"); |
| 289 if (!ParseAndStoreSpec(spec_str_2, &specs)) | 289 if (!ParseAndStoreSpec(spec_str_2, &specs)) |
| 290 return false; | 290 return false; |
| 291 } | 291 } |
| 292 return CacheMemTest(specs); | 292 return CacheMemTest(specs); |
| 293 } | 293 } |
| 294 | 294 |
| 295 } // namespace | 295 } // namespace |
| 296 } // namespace disk_cache | 296 } // namespace disk_cache |
| 297 | 297 |
| 298 int main(int argc, char** argv) { | 298 int main(int argc, char** argv) { |
| 299 return !disk_cache::Main(argc, argv); | 299 return !disk_cache::Main(argc, argv); |
| 300 } | 300 } |
| OLD | NEW |