chromium/third_party/leveldatabase/env_chromium.cc

// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#include "third_party/leveldatabase/env_chromium.h"

#include <atomic>
#include <iterator>
#include <limits>
#include <string>
#include <string_view>
#include <utility>

#include "base/check_op.h"
#include "base/files/file_error_or.h"
#include "base/files/file_util.h"
#include "base/format_macros.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/no_destructor.h"
#include "base/notreached.h"
#include "base/process/process_metrics.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/system/sys_info.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "base/time/time_override.h"
#include "base/trace_event/memory_dump_manager.h"
#include "base/trace_event/memory_dump_provider.h"
#include "base/trace_event/process_memory_dump.h"
#include "base/trace_event/trace_event.h"
#include "base/types/expected_macros.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "components/services/storage/public/cpp/filesystem/filesystem_proxy.h"
#include "third_party/leveldatabase/chromium_logger.h"
#include "third_party/leveldatabase/leveldb_chrome.h"
#include "third_party/leveldatabase/src/include/leveldb/options.h"
#include "third_party/re2/src/re2/re2.h"

FilePath;
MemoryAllocatorDump;
MemoryDumpArgs;
ProcessMemoryDump;
FileLock;
Slice;
Status;

namespace leveldb_env {
namespace {

FileErrorOr;

// After this limit we don't bother doing file eviction for leveldb for speed,
// memory usage, and simplicity.
const constexpr size_t kFileLimitToDisableEviction =;

// The maximum time for the |Retrier| to indicate that an operation should
// be retried.
constexpr auto kMaxRetryDuration =;

const FilePath::CharType table_extension[] =);

static const FilePath::CharType kLevelDBTestDirectoryPrefix[] =);

// This name should not be changed or users involved in a crash might not be
// able to recover data.
static const char kDatabaseNameSuffixForRebuildDB[] =;

DBFactoryMethod& GetDBFactoryOverride() {}

class ChromiumFileLock : public FileLock {};

class Retrier {};

class ChromiumSequentialFile : public leveldb::SequentialFile {};

void RemoveFile(const Slice& key, void* value) {}

Status ReadFromFileToScratch(uint64_t offset,
                             size_t n,
                             Slice* result,
                             char* scratch,
                             base::File* file,
                             const base::FilePath& file_path) {}

// The cache mechanism uses leveldb's LRU cache, which is a threadsafe sharded
// LRU cache. The keys use the pointer value of |this|, and the values are file
// objects.
// Each object will only use its own cache entry, so the |Erase| call in the
// object destructor should synchronously delete the file from the cache. This
// ensures that pointer location re-use won't re-use an entry in the cache as
// the entry at |this| will always have been deleted.
// Files are always cleaned up with |RemoveFile|, which will be called when the
// ChromiumEvictableRandomAccessFile is deleted, the cache is deleted, or the
// file is evicted.
class ChromiumEvictableRandomAccessFile : public leveldb::RandomAccessFile {};

class ChromiumRandomAccessFile : public leveldb::RandomAccessFile {};

class ChromiumWritableFile : public leveldb::WritableFile {};

ChromiumWritableFile::ChromiumWritableFile(const std::string& fname,
                                           base::File f,
                                           storage::FilesystemProxy* filesystem)
    :{}

Status ChromiumWritableFile::SyncParent() {}

Status ChromiumWritableFile::Append(const Slice& data) {}

Status ChromiumWritableFile::Close() {}

Status ChromiumWritableFile::Flush() {}

Status ChromiumWritableFile::Sync() {}

// Return the maximum number of read-only files to keep open.
size_t GetLevelDBFileLimit(size_t max_file_descriptors) {}

std::string GetDumpNameForDB(const leveldb::DB* db) {}

std::string GetDumpNameForCache(DBTracker::SharedReadCacheUse cache) {}

MemoryAllocatorDump* CreateDumpMalloced(ProcessMemoryDump* pmd,
                                        const std::string& name,
                                        size_t size) {}

void RecordCacheUsageInTracing(ProcessMemoryDump* pmd,
                               DBTracker::SharedReadCacheUse cache) {}

}  // namespace

Options::Options() {}

const char* MethodIDToString(MethodID method) {}

Status MakeIOError(Slice filename,
                   const std::string& message,
                   MethodID method,
                   base::File::Error error) {}

ErrorParsingResult ParseMethodAndError(const leveldb::Status& status,
                                       MethodID* method_param,
                                       base::File::Error* error) {}

// Keep in sync with LevelDBCorruptionTypes in histograms.xml. Also, don't
// change the order because indices into this array have been recorded in uma
// histograms.
const char* patterns[] =;

// Returns 1-based index into the above array or 0 if nothing matches.
int GetCorruptionCode(const leveldb::Status& status) {}

int GetNumCorruptionCodes() {}

std::string GetCorruptionMessage(const leveldb::Status& status) {}

bool IndicatesDiskFull(const leveldb::Status& status) {}

std::string DatabaseNameForRewriteDB(const std::string& original_name) {}

// Given the size of the disk, identified by |disk_size| in bytes, determine the
// appropriate write_buffer_size. Ignoring snapshots, if the current set of
// tables in a database contains a set of key/value pairs identified by {A}, and
// a set of key/value pairs identified by {B} has been written and is in the log
// file, then during compaction you will have {A} + {B} + {A, B} = 2A + 2B.
// There is no way to know the size of A, so minimizing the size of B will
// maximize the likelihood of a successful compaction.
size_t WriteBufferSize(int64_t disk_size) {}

ChromiumEnv::ChromiumEnv()
    :{}

ChromiumEnv::ChromiumEnv(bool log_lock_errors) :{}

ChromiumEnv::ChromiumEnv(std::unique_ptr<storage::FilesystemProxy> filesystem)
    :{}

ChromiumEnv::~ChromiumEnv() {}

bool ChromiumEnv::FileExists(const std::string& fname) {}

const char* ChromiumEnv::FileErrorString(base::File::Error error) {}

// Delete unused table backup files - a feature no longer supported.
// TODO(cmumford): Delete this function once found backup files drop below some
//                 very small (TBD) number.
void ChromiumEnv::RemoveBackupFiles(const FilePath& dir) {}

// Test must call this *before* opening any random-access files.
void ChromiumEnv::SetReadOnlyFileLimitForTesting(int max_open_files) {}

Status ChromiumEnv::GetChildren(const std::string& dir,
                                std::vector<std::string>* result) {}

Status ChromiumEnv::RemoveFile(const std::string& fname) {}

Status ChromiumEnv::CreateDir(const std::string& name) {}

Status ChromiumEnv::RemoveDir(const std::string& name) {}

Status ChromiumEnv::GetFileSize(const std::string& fname, uint64_t* size) {}

Status ChromiumEnv::RenameFile(const std::string& src, const std::string& dst) {}

Status ChromiumEnv::LockFile(const std::string& fname, FileLock** lock) {}

Status ChromiumEnv::UnlockFile(FileLock* lock) {}

Status ChromiumEnv::GetTestDirectory(std::string* path) {}

Status ChromiumEnv::NewLogger(const std::string& fname,
                              leveldb::Logger** result) {}

Status ChromiumEnv::NewSequentialFile(const std::string& fname,
                                      leveldb::SequentialFile** result) {}

Status ChromiumEnv::NewRandomAccessFile(const std::string& fname,
                                        leveldb::RandomAccessFile** result) {}

Status ChromiumEnv::NewWritableFile(const std::string& fname,
                                    leveldb::WritableFile** result) {}

Status ChromiumEnv::NewAppendableFile(const std::string& fname,
                                      leveldb::WritableFile** result) {}

uint64_t ChromiumEnv::NowMicros() {}

void ChromiumEnv::SleepForMicroseconds(int micros) {}

class Thread : public base::PlatformThread::Delegate {};

void ChromiumEnv::Schedule(ScheduleFunc* function, void* arg) {}

void ChromiumEnv::StartThread(void (*function)(void* arg), void* arg) {}

LevelDBStatusValue GetLevelDBStatusUMAValue(const leveldb::Status& s) {}

// Forwards all calls to the underlying leveldb::DB instance.
// Adds / removes itself in the DBTracker it's created with.
class DBTracker::TrackedDBImpl : public base::LinkNode<TrackedDBImpl>,
                                 public TrackedDB {};

// Reports live databases and in-memory env's to memory-infra. For each live
// database the following information is reported:
// 1. Instance pointer (to disambiguate databases).
// 2. Memory taken by the database, with the shared cache being attributed
// equally to each database sharing 3. The name of the database (when not in
// BACKGROUND mode to avoid exposing
//    PIIs in slow reports).
//
// Example report (as seen after clicking "leveldatabase" in "Overview" pane
// in Chrome tracing UI):
//
// Component                  effective_size  size        name
// ---------------------------------------------------------------------------
// leveldatabase              390 KiB         490 KiB
//   db_0x7FE70F2040A0        100 KiB         100 KiB     Users/.../Sync
//     block_cache (browser)  40 KiB          40 KiB
//   db_0x7FE70F530D80        150 KiB         150 KiB     Users/.../Data Proxy
//     block_cache (web)      30 KiB          30 KiB
//   db_0x7FE70F530D80        140 KiB         140 KiB     Users/.../Extensions
//     block_cache (web)      30 KiB          30 KiB
//   block_cache              0 KiB           100 KiB
//     browser                0 KiB           40 KiB
//     web                    0 KiB           60 KiB
//   memenv_0x7FE80F2040A0    4 KiB           4 KiB
//   memenv_0x7FE80F3040A0    4 KiB           4 KiB
//
class DBTracker::MemoryDumpProvider
    : public base::trace_event::MemoryDumpProvider {};

void DBTracker::MemoryDumpProvider::DumpAllDatabases(ProcessMemoryDump* pmd) {}

void DBTracker::MemoryDumpProvider::DumpVisitor(ProcessMemoryDump* pmd,
                                                TrackedDB* db) {}

DBTracker::DBTracker() :{}

DBTracker::~DBTracker() {}

// static
DBTracker* DBTracker::GetInstance() {}

// static
MemoryAllocatorDump* DBTracker::GetOrCreateAllocatorDump(
    ProcessMemoryDump* pmd,
    leveldb::DB* tracked_db) {}

// static
MemoryAllocatorDump* DBTracker::GetOrCreateAllocatorDump(
    ProcessMemoryDump* pmd,
    leveldb::Env* tracked_memenv) {}

bool DBTracker::IsTrackedDB(const leveldb::DB* db) const {}

leveldb::Status DBTracker::OpenDatabase(const leveldb_env::Options& options,
                                        const std::string& name,
                                        TrackedDB** dbptr) {}

void DBTracker::VisitDatabases(const DatabaseVisitor& visitor) {}

void DBTracker::DatabaseOpened(TrackedDBImpl* database) {}

void DBTracker::DatabaseDestroyed(TrackedDBImpl* database) {}

leveldb::Status OpenDB(const leveldb_env::Options& options,
                       const std::string& name,
                       std::unique_ptr<leveldb::DB>* dbptr) {}

void SetDBFactoryForTesting(DBFactoryMethod factory) {}

leveldb::Status RewriteDB(const leveldb_env::Options& options,
                          const std::string& name,
                          std::unique_ptr<leveldb::DB>* dbptr) {}

std::string_view MakeStringView(const leveldb::Slice& s) {}

leveldb::Slice MakeSlice(std::string_view s) {}

leveldb::Slice MakeSlice(base::span<const uint8_t> s) {}

}  // namespace leveldb_env

namespace leveldb {

Env* Env::Default() {}

}  // namespace leveldb