Android DropBoxManagerService解析

本文详细解析了Android系统中的DropBoxManagerService组件的工作原理及其实现细节,包括服务的启动流程、日志文件管理和异常场景处理等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文字说明部分转载自http://gityuan.com/2016/06/12/DropBoxManagerService/

最后的源码文件注释自己完成

DropBoxManagerService(简称DBMS) 记录着系统关键log信息,主要功能用于Debug调试。 Android系统启动过程SystemServer进程时,在startOtherServices()过程会启动DBMS服务,如下:

1.1 启动DBMS

[-> SystemServer.java]

private void startOtherServices() {
    //初始化DBMS,并登记该服务【见小节1.2】
    ServiceManager.addService(Context.DROPBOX_SERVICE,
            new DropBoxManagerService(context, new File("/data/system/dropbox")));
    ...
}

其中DROPBOX_SERVICE = “dropbox”, DBMS工作目录位于”/data/system/dropbox”,这个过程向ServiceManager 登记名为“dropbox”的服务。那么可通过dumpsys dropbox来查看该dropbox服务信息。

1.2 初始化DBMS

[-> DropBoxManagerService.java]

public final class DropBoxManagerService extends IDropBoxManagerService.Stub {

    public DropBoxManagerService(final Context context, File path) {
        mDropBoxDir = path;  // 目录/data/system/dropbox
        mContext = context;
        mContentResolver = context.getContentResolver();

        IntentFilter filter = new IntentFilter();
        // 监听存储设备可用空间低的广播
        filter.addAction(Intent.ACTION_DEVICE_STORAGE_LOW);
        // 监听开机完毕的广播
        filter.addAction(Intent.ACTION_BOOT_COMPLETED);
        context.registerReceiver(mReceiver, filter);

        // Settings数据库变化时则回调广播接收者的onReceive方法,此处CONTENT_URI=content://settings/global"
        mContentResolver.registerContentObserver(
            Settings.Global.CONTENT_URI, true,
            new ContentObserver(new Handler()) {
                public void onChange(boolean selfChange) {
                    mReceiver.onReceive(context, (Intent) null);
                }
            });

        mHandler = new Handler() {
            public void handleMessage(Message msg) {
                // 发送广播
                if (msg.what == MSG_SEND_BROADCAST) {
                    mContext.sendBroadcastAsUser((Intent)msg.obj, UserHandle.OWNER,
                            android.Manifest.permission.READ_LOGS);
                }
            }
        };
    }
}

该方法主要功能是给dropbox目录所对应的存储空间进行瘦身:

  • 存储设备可用空间低;
  • 开机完毕;
  • Settings数据库变化;

当发生任一以上情况都会触发触发执行mReceiver的onReceive方法,接下来看看该onReceive()过程.

1.3 mReceiver.onReceive

[-> DropBoxManagerService.java]

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        if (intent != null && Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            mBooted = true;
            return;
        }

        //收到ACTION_DEVICE_STORAGE_LOW,则强制重新check存储空间
        mCachedQuotaUptimeMillis = 0; 

        //创建工作线程来执行init和trim操作
        new Thread() {
            public void run() {
                try {
                    init(); //【见小节1.3.1】
                    trimToFit(); //【见小节1.3.2】
                } catch (IOException e) {
                    ...
                }
            }
        }.start();
    }
};
1.3.1 init
private synchronized void init() throws IOException {
    if (mStatFs == null) {
        if (!mDropBoxDir.isDirectory() && !mDropBoxDir.mkdirs()) {
            ...
        }
        mStatFs = new StatFs(mDropBoxDir.getPath());
        mBlockSize = mStatFs.getBlockSize(); //mBlockSize=4096
    }

    if (mAllFiles == null) {
        File[] files = mDropBoxDir.listFiles();
        // 列举所有的dropbox文件
        mAllFiles = new FileList();
        mFilesByTag = new HashMap<String, FileList>();

        for (File file : files) {
            if (file.getName().endsWith(".tmp")) {
                file.delete(); //删除后缀为.tmp文件
                continue;
            }
            // 创建dropbox的实体文件对象, 根据文件名来获取相应的时间戳
            EntryFile entry = new EntryFile(file, mBlockSize);
            if (entry.tag == null) {
                continue; //忽略tag为空的文件
            } else if (entry.timestampMillis == 0) {
                file.delete(); //删除时间戳为0的文件
                continue;
            }
            //将entry加入到mAllFiles对象
            enrollEntry(entry);
        }
    }
}

该方法主要功能:

  • 创建目录/data/system/dropbox;
  • 列举该目录下所有文件,并对其进行:
    • 将每一个dropbox文件都对应于一个EntryFile对象,根据文件名来获取相应的时间戳
    • 删除后缀为.tmp的文件;
    • 删除时间戳为0的文件.
1.3.2 trimToFit
private synchronized long trimToFit() {

    int ageSeconds = Settings.Global.getInt(mContentResolver,
            Settings.Global.DROPBOX_AGE_SECONDS, DEFAULT_AGE_SECONDS);
    int maxFiles = Settings.Global.getInt(mContentResolver,
            Settings.Global.DROPBOX_MAX_FILES, DEFAULT_MAX_FILES);
    long cutoffMillis = System.currentTimeMillis() - ageSeconds * 1000;

    while (!mAllFiles.contents.isEmpty()) {
        EntryFile entry = mAllFiles.contents.first();
        //当最老的文件时间戳在3天之内,且文件个数低于1000,则跳出循环
        if (entry.timestampMillis > cutoffMillis 
            && mAllFiles.contents.size() < maxFiles) break;
        FileList tag = mFilesByTag.get(entry.tag);
        if (tag != null && tag.contents.remove(entry)) tag.blocks -= entry.blocks;
        if (mAllFiles.contents.remove(entry)) mAllFiles.blocks -= entry.blocks;
        if (entry.file != null) entry.file.delete(); //删除文件
    }

    long uptimeMillis = SystemClock.uptimeMillis();
    //除非接收设备存储低的广播,否则间隔5s才能再次执行restat
    if (uptimeMillis > mCachedQuotaUptimeMillis + QUOTA_RESCAN_MILLIS) {
        int quotaPercent = Settings.Global.getInt(mContentResolver,
                Settings.Global.DROPBOX_QUOTA_PERCENT, DEFAULT_QUOTA_PERCENT);
        int reservePercent = Settings.Global.getInt(mContentResolver,
                Settings.Global.DROPBOX_RESERVE_PERCENT, DEFAULT_RESERVE_PERCENT);
        int quotaKb = Settings.Global.getInt(mContentResolver,
                Settings.Global.DROPBOX_QUOTA_KB, DEFAULT_QUOTA_KB);
        //重新统计文件
        mStatFs.restat(mDropBoxDir.getPath());
        int available = mStatFs.getAvailableBlocks();
        int nonreserved = available - mStatFs.getBlockCount() * reservePercent / 100;
        int maximum = quotaKb * 1024 / mBlockSize;
        //可用的块数量
        mCachedQuotaBlocks = Math.min(maximum, Math.max(0, nonreserved * quotaPercent / 100));
        mCachedQuotaUptimeMillis = uptimeMillis;
    }

    if (mAllFiles.blocks > mCachedQuotaBlocks) {
        //公平地限制所有tag的空间
        int unsqueezed = mAllFiles.blocks, squeezed = 0;
        TreeSet<FileList> tags = new TreeSet<FileList>(mFilesByTag.values());
        for (FileList tag : tags) {
            if (squeezed > 0 && tag.blocks <= (mCachedQuotaBlocks - unsqueezed) / squeezed) {
                break;
            }
            unsqueezed -= tag.blocks;
            squeezed++;
        }
        int tagQuota = (mCachedQuotaBlocks - unsqueezed) / squeezed;

        //移除每个tags中的旧items
        for (FileList tag : tags) {
            if (mAllFiles.blocks < mCachedQuotaBlocks) break;
            while (tag.blocks > tagQuota && !tag.contents.isEmpty()) {
                EntryFile entry = tag.contents.first();
                if (tag.contents.remove(entry)) tag.blocks -= entry.blocks;
                if (mAllFiles.contents.remove(entry)) mAllFiles.blocks -= entry.blocks;

                try {
                    if (entry.file != null) entry.file.delete();
                    enrollEntry(new EntryFile(mDropBoxDir, entry.tag, entry.timestampMillis));
                } catch (IOException e) {
                    Slog.e(TAG, "Can't write tombstone file", e);
                }
            }
        }
    }

    return mCachedQuotaBlocks * mBlockSize;
}

trimToFit过程中触发条件是:当文件有效时长超过3天,或者最大文件数超过1000,再或者剩余可用存储设备过低;

DBMS有很多常量参数:

  • DEFAULT_AGE_SECONDS = 3 * 86400:文件最长可存活时长为3天
  • DEFAULT_MAX_FILES = 1000:最大dropbox文件个数为1000
  • DEFAULT_QUOTA_KB = 5 * 1024:分配dropbox空间的最大值5M
  • DEFAULT_QUOTA_PERCENT = 10:是指dropbox目录最多可占用空间比例10%
  • DEFAULT_RESERVE_PERCENT = 10:是指dropbox不可使用的存储空间比例10%
  • QUOTA_RESCAN_MILLIS = 5000:重新扫描retrim时长为5s

当然上面这些都是默认值,完全可以通过设置content://settings/global数据库中相应项来设定值。

二、DropBox工作

当发生以下任一场景,都会调用AMS.addErrorToDropBox()来触发DBMS工作。

  • crash: 文章理解Android Crash处理流程 [小节4]的AMS.handleApplicationCrashInner过程
  • anr: 文章android ANR原理分析[小节3.1]的AMS.appNotResponding()过程;
  • watchdog: 文章WatchDog工作原理 [小节3.1]的Watchdog.run()过程;
  • native_crash: 当调用NativeCrashReporter.run()的过程;
  • wtf: 当调用Log.wtf()或者Log.wtfQuiet()的过程;
  • lowmem: 当内存较低时,触发AMS.reportMemUsage()过程;

2.1 AMS.addErrorToDropBox

[–>ActivityManagerService.java]

public void addErrorToDropBox(String eventType,
        ProcessRecord process, String processName, ActivityRecord activity,
        ActivityRecord parent, String subject,
        final String report, final File logFile,
        final ApplicationErrorReport.CrashInfo crashInfo) {

    //创建dropbox标签名【见小节2.1.1】
    final String dropboxTag = processClass(process) + "_" + eventType;
    //获取dropbox服务的代理端
    final DropBoxManager dbox = (DropBoxManager)
            mContext.getSystemService(Context.DROPBOX_SERVICE);

    //当不需要输出dropbox报告则直接返回
    if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return;

    final StringBuilder sb = new StringBuilder(1024);
    //输出Process,flags,以及进程中所有package 【见小节2.1.2】
    appendDropBoxProcessHeaders(process, processName, sb);
    ...
    if (subject != null) {
        sb.append("Subject: ").append(subject).append("\n");
    }
    sb.append("Build: ").append(Build.FINGERPRINT).append("\n");
    sb.append("\n");

    //创建新线程,避免将调用者阻塞在I/O
    Thread worker = new Thread("Error dump: " + dropboxTag) {
        @Override
        public void run() {
            if (report != null) {
                //比如ANR时输出Cpuinfo,或者lowmem时输出的内存信息
                sb.append(report); 
            }
            if (logFile != null) {
                //比如anr或者Watchdog时输出的traces文件(kill -3),最大上限为256KB
                sb.append(FileUtils.readTextFile(logFile, DROPBOX_MAX_SIZE,
                            "\n\n[[TRUNCATED]]"));
            }
            if (crashInfo != null && crashInfo.stackTrace != null) {
                // 比如crash时输出的调用栈
                sb.append(crashInfo.stackTrace);
            }

            String setting = Settings.Global.ERROR_LOGCAT_PREFIX + dropboxTag;
            int lines = Settings.Global.getInt(mContext.getContentResolver(), setting, 0);
            //当dropboxTag所对应的settings项不等于0,则输出logcat
            if (lines > 0) {
              //输出evets/system/main/crash这些log信息
              java.lang.Process logcat = new ProcessBuilder("/system/bin/logcat",
                      "-v", "time", "-b", "events", "-b", "system", "-b", "main",
                      "-b", "crash",
                      "-t", String.valueOf(lines)).redirectErrorStream(true).start();

              input = new InputStreamReader(logcat.getInputStream());

              int num;
              char[] buf = new char[8192];
              //不断读取input中的log内容,并添加到sb
              while ((num = input.read(buf)) > 0) sb.append(buf, 0, num);
              ...
            }
            //将log信息输出到DropBox 【见小节2.2】
            dbox.addText(dropboxTag, sb.toString());
        }
    };

    if (process == null) {
        //当进程为空,意味着system_server进程崩溃,系统可能很快就要挂了,
        //那么不再创建新线程,而是直接在system_server进程中同步运行
        worker.run();
    } else {
        //启动新线程
        worker.start();
    }
}

该方法主要功能是输出以下内容项:

  1. Process,flags, package等头信息;
  2. 当report不为空,则比如ANR时输出Cpuinfo,或者lowmem时输出的内存信息
  3. 当logFile不为空,则比如anr或者Watchdog时输出的traces文件(kill -3),最大上限为256KB;
  4. 当stack不为空,则比如crash时输出的调用栈;
  5. 输出logcat的events/system/main/crash信息。
2.1.1 AMS.processClass
private static String processClass(ProcessRecord process) {
    //MY_PID代表的是当前进程pid,正是system_server进程
    if (process == null || process.pid == MY_PID) {
        return "system_server";
    } else if ((process.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
        return "system_app";
    } else {
        return "data_app";
    }
}

dropbox文件名格式为dropboxTag@xxx.txt xxx代表时间戳,例如system_server_crash@1465650845355.txt,则记录该文件时间戳为1465650845355. 文件后缀除了.txt,还有压缩格式.txt.gz. 对于dropboxTag是由processClass + eventType组合而成.

  • processClass分为system_server, system_app, data_app;
  • eventType:分为crash,anr,wtf,native_cras,lowmem, watchdog

列举部分常见tags以及含义:

dropboxTag含义
system_server_anrsystem进程无响应
system_server_watchdogsystem进程发生watchdog
system_server_crashsystem进程崩溃
system_server_native_crashsystem进程native出现崩溃
system_server_wtfsystem进程发生严重错误
system_server_lowmemsystem进程内存不足

当然除了system_server进程, 还有system_app, data_app类型的进程, 以上所有类型都适用,列举部分:

system_app_crash系统app崩溃
system_app_anr系统app无响应
data_app_crash普通app崩溃
data_app_anr普通app无响应
2.1.2 AMS.appendDropBoxProcessHeaders
private void appendDropBoxProcessHeaders(ProcessRecord process, String processName,
        StringBuilder sb) {
    if (process == null) {
        sb.append("Process: ").append(processName).append("\n");
        return;
    }

    synchronized (this) {
        sb.append("Process: ").append(processName).append("\n");
        int flags = process.info.flags;
        IPackageManager pm = AppGlobals.getPackageManager();
        sb.append("Flags: 0x").append(Integer.toString(flags, 16)).append("\n");
        for (int ip=0; ip<process.pkgList.size(); ip++) {
            String pkg = process.pkgList.keyAt(ip);
            sb.append("Package: ").append(pkg);
            try {
                PackageInfo pi = pm.getPackageInfo(pkg, 0, UserHandle.getCallingUserId());
                if (pi != null) {
                    sb.append(" v").append(pi.versionCode);
                    if (pi.versionName != null) {
                        sb.append(" (").append(pi.versionName).append(")");
                    }
                }
            } catch (RemoteException e) {
                ...
            }
            sb.append("\n");
        }
    }
}

该方法输出的信息:

  • 进程名;
  • 进程的ApplicationInfo的flags信息;
  • 进程中所有的包名以及版本信息;

这里列举头信息实例:

2016-11-11 22:22:22 system_app_anr (compressed text, 26165 bytes)
Process: com.android.systemui
Flags: 0x40d83e0d
Package: com.android.systemui v21 (5.0.2)
Subject: Broadcast of Intent { act=android.intent.action.TIME_TICK flg=0x50000014 (has extras) }

2.2 DBM.addText

[-> DropBoxManager.java]

public void addText(String tag, String data) {
    try { 
        //data数据封装到Entry对象实例 【见小节2.3】
        mService.add(new Entry(tag, 0, data)); 
    } catch (RemoteException e) {
        ...
    }
}

在DropBoxManager中有addText, addData, addFile方法,三分归一统,对应于DBMS的add()方法。

2.3 DBMS.add

[ -> DropBoxManagerService.java]

public void add(DropBoxManager.Entry entry) {
    File temp = null;
    OutputStream output = null;
    final String tag = entry.getTag();
    try {
        int flags = entry.getFlags();
        ...
        init(); // 初始化【见小节1.3.1】
        long max = trimToFit(); // 压缩空间【见小节1.3.2】
        long lastTrim = System.currentTimeMillis();
        byte[] buffer = new byte[mBlockSize];
        InputStream input = entry.getInputStream();

        int read = 0;
        while (read < buffer.length) {
            int n = input.read(buffer, read, buffer.length - read);
            if (n <= 0) break;
            read += n;
        }
        //创建临时文件,例如tid=1234的对应文件drop1234.tmp
        temp = new File(mDropBoxDir, "drop" + Thread.currentThread().getId() + ".tmp");
        int bufferSize = mBlockSize;
        if (bufferSize > 4096) bufferSize = 4096;
        if (bufferSize < 512) bufferSize = 512;
        FileOutputStream foutput = new FileOutputStream(temp);
        output = new BufferedOutputStream(foutput, bufferSize);
        //创建gzip压缩文件
        if (read == buffer.length && ((flags & DropBoxManager.IS_GZIPPED) == 0)) {
            output = new GZIPOutputStream(output);
            flags = flags | DropBoxManager.IS_GZIPPED;
        }

        //不断将temp文件数据写入buffer
        do {
            output.write(buffer, 0, read);
            long now = System.currentTimeMillis();
            if (now - lastTrim > 30 * 1000) {
                max = trimToFit(); //执行时间超过30s则执行trim
                lastTrim = now;
            }

            read = input.read(buffer);
            if (read <= 0) {
                FileUtils.sync(foutput);
                output.close();
                output = null;
            } else {
                output.flush();
            }

            long len = temp.length();
            if (len > max) {
                temp.delete();
                temp = null;
                break;
            }
        } while (read > 0);

        //[见小节2.3.1]
        long time = createEntry(temp, tag, flags);
        temp = null;

        final Intent dropboxIntent = new Intent(DropBoxManager.ACTION_DROPBOX_ENTRY_ADDED);
        dropboxIntent.putExtra(DropBoxManager.EXTRA_TAG, tag);
        dropboxIntent.putExtra(DropBoxManager.EXTRA_TIME, time);
        if (!mBooted) {
            dropboxIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
        }
        //发送广播MSG_SEND_BROADCAST
        mHandler.sendMessage(mHandler.obtainMessage(MSG_SEND_BROADCAST, dropboxIntent));
    } catch (IOException e) {
        ...
    } finally {
        if (output != null) output.close();
        entry.close();
        if (temp != null) temp.delete();
    }
}
2.3.1 DBMS.createEntry

[ -> DropBoxManagerService.java]

private synchronized long createEntry(File temp, String tag, int flags) throws IOException {
    long t = System.currentTimeMillis(); //当当前时间作为dropbox文件的时间戳
    ...
    if (temp == null) {
        enrollEntry(new EntryFile(mDropBoxDir, tag, t));
    } else {
        enrollEntry(new EntryFile(temp, mDropBoxDir, tag, t, flags, mBlockSize));
    }
    return t;
}

关于时间戳问题:

  1. EntryFile(File file, int blockSize) : 从file文件获取时间戳,并保存到EntryFile.timestampMillis. init()过程使用.
  2. 其他的构造方法则都会在创建时,将当前时间保存到EntryFile.timestampMillis.比如EntryFile(File dir, String tag, long timestampMillis)

三. 总结

DBMS服务的数据保存目录为/data/system/dropbox

当出现crash, anr, wtf,lowmem,以及开机完成时都会通过DropBoxManager, 收集系统的重要信息: Process,flags, package等头信息和logcat信息。 另外就是根据不同场景输出相应的信息,例如:

  1. CRASH:输出发生crash时的当前线程的调用栈信息;
  2. ANR:输出Cpuinfo,以及重要进程的各个线程的traces文件(kill -3);
  3. Watchdog: 也输出重要进程的各个线程的traces文件(kill -3)

DropBoxManagerService源码注释

/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Binder;
import android.os.Debug;
import android.os.DropBoxManager;
import android.os.FileUtils;
import android.os.Handler;
import android.os.Message;
import android.os.StatFs;
import android.os.SystemClock;
import android.os.UserHandle;
import android.provider.Settings;
import android.text.format.Time;
import android.util.Slog;

import libcore.io.IoUtils;

import com.android.internal.os.IDropBoxManagerService;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.zip.GZIPOutputStream;

/**
 * Implementation of {@link IDropBoxManagerService} using the filesystem.
 * Clients use {@link DropBoxManager} to access this service.
 * DBMS工作目录位于”/data/system/dropbox”,这个过程向ServiceManager 登记名为“dropbox”的服务。
 * 那么可通过dumpsys dropbox来查看该dropbox服务信息
 */
public final class DropBoxManagerService extends SystemService {
    private static final String TAG = "DropBoxManagerService";
    private static final int DEFAULT_AGE_SECONDS = 3 * 86400;//72小时,3天
    private static final int DEFAULT_MAX_FILES = 1000;
    private static final int DEFAULT_QUOTA_KB = 5 * 1024;//分配dropbox空间的最大值5M
    private static final int DEFAULT_QUOTA_PERCENT = 10;//dropbox目录最多可占用空间比例10%
    private static final int DEFAULT_RESERVE_PERCENT = 10;//dropbox不可使用的存储空间比例10%
    private static final int QUOTA_RESCAN_MILLIS = 5000;

    // mHandler 'what' value.
    private static final int MSG_SEND_BROADCAST = 1;

    private static final boolean PROFILE_DUMP = false;

    // TODO: This implementation currently uses one file per entry, which is
    // inefficient for smallish entries -- consider using a single queue file
    // per tag (or even globally) instead.

    // The cached context and derived objects

    private final ContentResolver mContentResolver;
    private final File mDropBoxDir;

    // Accounting of all currently written log files (set in init()).

    private FileList mAllFiles = null;
    private HashMap<String, FileList> mFilesByTag = null;

    // Various bits of disk information
    //保存文件系统的信息,这个类是对Unix statvfs()的封装
    private StatFs mStatFs = null;
    private int mBlockSize = 0;
    private int mCachedQuotaBlocks = 0;  // Space we can use: computed from free space, etc.
    private long mCachedQuotaUptimeMillis = 0;

    private volatile boolean mBooted = false;

    // Provide a way to perform sendBroadcast asynchronously to avoid deadlocks.
    private final Handler mHandler;

    /** Receives events that might indicate a need to clean up files. */
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // For ACTION_DEVICE_STORAGE_LOW:
            mCachedQuotaUptimeMillis = 0;  // Force a re-check of quota size

            // Run the initialization in the background (not this main thread).
            // The init() and trimToFit() methods are synchronized, so they still
            // block other users -- but at least the onReceive() call can finish.
            //在设备存储空间不足时,异步清理文件
            new Thread() {
                public void run() {
                    try {
                        init();
                        trimToFit();
                    } catch (IOException e) {
                        Slog.e(TAG, "Can't init", e);
                    }
                }
            }.start();
        }
    };

    private final IDropBoxManagerService.Stub mStub = new IDropBoxManagerService.Stub() {
        @Override
        public void add(DropBoxManager.Entry entry) {
            DropBoxManagerService.this.add(entry);
        }

        @Override
        public boolean isTagEnabled(String tag) {
            return DropBoxManagerService.this.isTagEnabled(tag);
        }

        @Override
        public DropBoxManager.Entry getNextEntry(String tag, long millis) {
            return DropBoxManagerService.this.getNextEntry(tag, millis);
        }

        @Override
        public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
            DropBoxManagerService.this.dump(fd, pw, args);
        }
    };

    /**
     * Creates an instance of managed drop box storage using the default dropbox
     * directory.
     *
     * @param context to use for receiving free space & gservices intents
     * /data/system/dropbox目录是dropbox的超作目录,文件都保存在这个目录下
     */
    public DropBoxManagerService(final Context context) {
        this(context, new File("/data/system/dropbox"));
    }

    /**
     * Creates an instance of managed drop box storage.  Normally there is one of these
     * run by the system, but others can be created for testing and other purposes.
     *
     * @param context to use for receiving free space & gservices intents
     * @param path to store drop box entries in
     */
    public DropBoxManagerService(final Context context, File path) {
        super(context);
        mDropBoxDir = path;
        mContentResolver = getContext().getContentResolver();
        /*DropBoxManagerService在SystemServer进程中的线程中运行,而此线程已经调用了Looper的prepaer()方法*/
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == MSG_SEND_BROADCAST) {
                    getContext().sendBroadcastAsUser((Intent)msg.obj, UserHandle.SYSTEM,
                            android.Manifest.permission.READ_LOGS);
                }
            }
        };
    }
    /* onStart在DropBoxManagerService构造函数后调用*/
    @Override
    public void onStart() {
        // Set up intent receivers
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_DEVICE_STORAGE_LOW);
        //接收设备存储空间不足广播
        getContext().registerReceiver(mReceiver, filter);

        //监听settings的uri,一旦有变化调用onReceive
        mContentResolver.registerContentObserver(
            Settings.Global.CONTENT_URI, true,
            new ContentObserver(new Handler()) {
                @Override
                public void onChange(boolean selfChange) {
                    mReceiver.onReceive(getContext(), (Intent) null);
                }
            });
        //发布binder可以被调用
        publishBinderService(Context.DROPBOX_SERVICE, mStub);

        // The real work gets done lazily in init() -- that way service creation always
        // succeeds, and things like disk problems cause individual method failures.
    }

    @Override
    public void onBootPhase(int phase) {
        switch (phase) {
            //系统启动完成
            case PHASE_BOOT_COMPLETED:
                mBooted = true;
                break;
        }
    }

    /** Retrieves the binder stub -- for test instances */
    public IDropBoxManagerService getServiceStub() {
        return mStub;
    }

    /**
     * Entry存放在drop box日志的一个索引
     * @param entry
     */
    public void add(DropBoxManager.Entry entry) {
        File temp = null;
        InputStream input = null;
        OutputStream output = null;
        final String tag = entry.getTag();
        try {
            int flags = entry.getFlags();
            //如果entry是空,抛出异常
            if ((flags & DropBoxManager.IS_EMPTY) != 0) throw new IllegalArgumentException();
            //方法里有判断,基本上只被初始化一次
            init();
            //从settings.globle判断tag对应的drop box是否使能
            if (!isTagEnabled(tag)) return;
            long max = trimToFit();//获取目录可用的空间
            long lastTrim = System.currentTimeMillis();

            byte[] buffer = new byte[mBlockSize];
            input = entry.getInputStream();

            // First, accumulate up to one block worth of data in memory before
            // deciding whether to compress the data or not.
            //读取一个块大小的数据,当文件数据小于一个块大小时,全部读取
            int read = 0;
            while (read < buffer.length) {
                int n = input.read(buffer, read, buffer.length - read);
                if (n <= 0) break;
                read += n;
            }

            // If we have at least one block, compress it -- otherwise, just write
            // the data in uncompressed form.
            //只有写入文件的数据大于块的长度时,才压缩,要不然,直接写入数据
            //先写入临时文件"drop"+线程id+".tmp"
            temp = new File(mDropBoxDir, "drop" + Thread.currentThread().getId() + ".tmp");
            int bufferSize = mBlockSize;
            if (bufferSize > 4096) bufferSize = 4096;
            if (bufferSize < 512) bufferSize = 512;
            FileOutputStream foutput = new FileOutputStream(temp);
            output = new BufferedOutputStream(foutput, bufferSize);
            if (read == buffer.length && ((flags & DropBoxManager.IS_GZIPPED) == 0)) {
                output = new GZIPOutputStream(output);
                flags = flags | DropBoxManager.IS_GZIPPED;
            }

            do {
                output.write(buffer, 0, read);

                long now = System.currentTimeMillis();
                if (now - lastTrim > 30 * 1000) {//写文件的时间超过30s,执行trimToFit()
                    max = trimToFit();  // In case data dribbles in slowly
                    lastTrim = now;
                }

                read = input.read(buffer);
                if (read <= 0) {
                    FileUtils.sync(foutput);
                    output.close();  // Get a final size measurement
                    output = null;
                } else {
                    output.flush();  // So the size measurement is pseudo-reasonable
                }

                long len = temp.length();
                //如果临时文件大于可用的size,删除临时文件,cteat entry时只创建一个空文件
                if (len > max) {
                    Slog.w(TAG, "Dropping: " + tag + " (" + temp.length() + " > " + max + " bytes)");
                    temp.delete();
                    temp = null;  // Pass temp = null to createEntry() to leave a tombstone
                    break;
                }
            } while (read > 0);

            long time = createEntry(temp, tag, flags);
            temp = null;

            final Intent dropboxIntent = new Intent(DropBoxManager.ACTION_DROPBOX_ENTRY_ADDED);
            dropboxIntent.putExtra(DropBoxManager.EXTRA_TAG, tag);
            dropboxIntent.putExtra(DropBoxManager.EXTRA_TIME, time);
            if (!mBooted) {
                dropboxIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
            }
            // Call sendBroadcast after returning from this call to avoid deadlock. In particular
            // the caller may be holding the WindowManagerService lock but sendBroadcast requires a
            // lock in ActivityManagerService. ActivityManagerService has been caught holding that
            // very lock while waiting for the WindowManagerService lock.
            mHandler.sendMessage(mHandler.obtainMessage(MSG_SEND_BROADCAST, dropboxIntent));
        } catch (IOException e) {
            Slog.e(TAG, "Can't write: " + tag, e);
        } finally {
            IoUtils.closeQuietly(output);
            IoUtils.closeQuietly(input);
            entry.close();
            if (temp != null) temp.delete();
        }
    }

    public boolean isTagEnabled(String tag) {
        final long token = Binder.clearCallingIdentity();
        try {
            return !"disabled".equals(Settings.Global.getString(
                    mContentResolver, Settings.Global.DROPBOX_TAG_PREFIX + tag));
        } finally {
            Binder.restoreCallingIdentity(token);
        }
    }

    public synchronized DropBoxManager.Entry getNextEntry(String tag, long millis) {
        if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.READ_LOGS)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("READ_LOGS permission required");
        }

        try {
            init();
        } catch (IOException e) {
            Slog.e(TAG, "Can't init", e);
            return null;
        }

        FileList list = tag == null ? mAllFiles : mFilesByTag.get(tag);
        if (list == null) return null;

        for (EntryFile entry : list.contents.tailSet(new EntryFile(millis + 1))) {
            if (entry.tag == null) continue;
            if ((entry.flags & DropBoxManager.IS_EMPTY) != 0) {
                return new DropBoxManager.Entry(entry.tag, entry.timestampMillis);
            }
            try {
                return new DropBoxManager.Entry(
                        entry.tag, entry.timestampMillis, entry.file, entry.flags);
            } catch (IOException e) {
                Slog.e(TAG, "Can't read: " + entry.file, e);
                // Continue to next file
            }
        }

        return null;
    }

    public synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
        if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
                != PackageManager.PERMISSION_GRANTED) {
            pw.println("Permission Denial: Can't dump DropBoxManagerService");
            return;
        }

        try {
            init();
        } catch (IOException e) {
            pw.println("Can't initialize: " + e);
            Slog.e(TAG, "Can't init", e);
            return;
        }

        if (PROFILE_DUMP) Debug.startMethodTracing("/data/trace/dropbox.dump");

        StringBuilder out = new StringBuilder();
        boolean doPrint = false, doFile = false;
        ArrayList<String> searchArgs = new ArrayList<String>();
        for (int i = 0; args != null && i < args.length; i++) {
            if (args[i].equals("-p") || args[i].equals("--print")) {
                doPrint = true;
            } else if (args[i].equals("-f") || args[i].equals("--file")) {
                doFile = true;
            } else if (args[i].startsWith("-")) {
                out.append("Unknown argument: ").append(args[i]).append("\n");
            } else {
                searchArgs.add(args[i]);
            }
        }

        out.append("Drop box contents: ").append(mAllFiles.contents.size()).append(" entries\n");

        if (!searchArgs.isEmpty()) {
            out.append("Searching for:");
            for (String a : searchArgs) out.append(" ").append(a);
            out.append("\n");
        }

        int numFound = 0, numArgs = searchArgs.size();
        Time time = new Time();
        out.append("\n");
        for (EntryFile entry : mAllFiles.contents) {
            time.set(entry.timestampMillis);
            String date = time.format("%Y-%m-%d %H:%M:%S");
            boolean match = true;
            for (int i = 0; i < numArgs && match; i++) {
                String arg = searchArgs.get(i);
                match = (date.contains(arg) || arg.equals(entry.tag));
            }
            if (!match) continue;

            numFound++;
            if (doPrint) out.append("========================================\n");
            out.append(date).append(" ").append(entry.tag == null ? "(no tag)" : entry.tag);
            if (entry.file == null) {
                out.append(" (no file)\n");
                continue;
            } else if ((entry.flags & DropBoxManager.IS_EMPTY) != 0) {
                out.append(" (contents lost)\n");
                continue;
            } else {
                out.append(" (");
                if ((entry.flags & DropBoxManager.IS_GZIPPED) != 0) out.append("compressed ");
                out.append((entry.flags & DropBoxManager.IS_TEXT) != 0 ? "text" : "data");
                out.append(", ").append(entry.file.length()).append(" bytes)\n");
            }

            if (doFile || (doPrint && (entry.flags & DropBoxManager.IS_TEXT) == 0)) {
                if (!doPrint) out.append("    ");
                out.append(entry.file.getPath()).append("\n");
            }

            if ((entry.flags & DropBoxManager.IS_TEXT) != 0 && (doPrint || !doFile)) {
                DropBoxManager.Entry dbe = null;
                InputStreamReader isr = null;
                try {
                    dbe = new DropBoxManager.Entry(
                             entry.tag, entry.timestampMillis, entry.file, entry.flags);

                    if (doPrint) {
                        isr = new InputStreamReader(dbe.getInputStream());
                        char[] buf = new char[4096];
                        boolean newline = false;
                        for (;;) {
                            int n = isr.read(buf);
                            if (n <= 0) break;
                            out.append(buf, 0, n);
                            newline = (buf[n - 1] == '\n');

                            // Flush periodically when printing to avoid out-of-memory.
                            if (out.length() > 65536) {
                                pw.write(out.toString());
                                out.setLength(0);
                            }
                        }
                        if (!newline) out.append("\n");
                    } else {
                        String text = dbe.getText(70);
                        out.append("    ");
                        if (text == null) {
                            out.append("[null]");
                        } else {
                            boolean truncated = (text.length() == 70);
                            out.append(text.trim().replace('\n', '/'));
                            if (truncated) out.append(" ...");
                        }
                        out.append("\n");
                    }
                } catch (IOException e) {
                    out.append("*** ").append(e.toString()).append("\n");
                    Slog.e(TAG, "Can't read: " + entry.file, e);
                } finally {
                    if (dbe != null) dbe.close();
                    if (isr != null) {
                        try {
                            isr.close();
                        } catch (IOException unused) {
                        }
                    }
                }
            }

            if (doPrint) out.append("\n");
        }

        if (numFound == 0) out.append("(No entries found.)\n");

        if (args == null || args.length == 0) {
            if (!doPrint) out.append("\n");
            out.append("Usage: dumpsys dropbox [--print|--file] [YYYY-mm-dd] [HH:MM:SS] [tag]\n");
        }

        pw.write(out.toString());
        if (PROFILE_DUMP) Debug.stopMethodTracing();
    }

    ///////////////////////////////////////////////////////////////////////////

    /**
     * Chronologically sorted list of {@link EntryFile}
     * */
    private static final class FileList implements Comparable<FileList> {
        public int blocks = 0;
        public final TreeSet<EntryFile> contents = new TreeSet<EntryFile>();

        /** Sorts bigger FileList instances before smaller ones. */
        public final int compareTo(FileList o) {
            if (blocks != o.blocks) return o.blocks - blocks;
            if (this == o) return 0;
            if (hashCode() < o.hashCode()) return -1;
            if (hashCode() > o.hashCode()) return 1;
            return 0;
        }
    }

    /** Metadata describing an on-disk log file. */
    private static final class EntryFile implements Comparable<EntryFile> {
        public final String tag;
        /**
         * 1,从文件获取时间戳
         * 2,构造方法在创建时,将当前时间保存为时间戳
         */
        public final long timestampMillis;
        public final int flags;
        public final File file;
        public final int blocks;

        /** Sorts earlier EntryFile instances before later ones. */
        public final int compareTo(EntryFile o) {
            if (timestampMillis < o.timestampMillis) return -1;
            if (timestampMillis > o.timestampMillis) return 1;
            if (file != null && o.file != null) return file.compareTo(o.file);
            if (o.file != null) return -1;
            if (file != null) return 1;
            if (this == o) return 0;
            if (hashCode() < o.hashCode()) return -1;
            if (hashCode() > o.hashCode()) return 1;
            return 0;
        }

        /**
         * Moves an existing temporary file to a new log filename.
         * @param temp file to rename
         * @param dir to store file in
         * @param tag to use for new log file name
         * @param timestampMillis of log entry
         * @param flags for the entry data
         * @param blockSize to use for space accounting
         * @throws IOException if the file can't be moved
         */
        public EntryFile(File temp, File dir, String tag,long timestampMillis,
                         int flags, int blockSize) throws IOException {
            if ((flags & DropBoxManager.IS_EMPTY) != 0) throw new IllegalArgumentException();

            this.tag = tag;
            this.timestampMillis = timestampMillis;
            this.flags = flags;
            this.file = new File(dir, Uri.encode(tag) + "@" + timestampMillis +
                    ((flags & DropBoxManager.IS_TEXT) != 0 ? ".txt" : ".dat") +
                    ((flags & DropBoxManager.IS_GZIPPED) != 0 ? ".gz" : ""));

            if (!temp.renameTo(this.file)) {
                throw new IOException("Can't rename " + temp + " to " + this.file);
            }
            this.blocks = (int) ((this.file.length() + blockSize - 1) / blockSize);
        }

        /**
         * Creates a zero-length tombstone for a file whose contents were lost.
         * @param dir to store file in
         * @param tag to use for new log file name
         * @param timestampMillis of log entry
         * @throws IOException if the file can't be created.
         */
        public EntryFile(File dir, String tag, long timestampMillis) throws IOException {
            this.tag = tag;
            this.timestampMillis = timestampMillis;
            this.flags = DropBoxManager.IS_EMPTY;
            this.file = new File(dir, Uri.encode(tag) + "@" + timestampMillis + ".lost");
            this.blocks = 0;
            new FileOutputStream(this.file).close();
        }

        /**
         * Extracts metadata from an existing on-disk log filename.
         * @param file name of existing log file
         * @param blockSize to use for space accounting
         */
        public EntryFile(File file, int blockSize) {
            this.file = file;
            this.blocks = (int) ((this.file.length() + blockSize - 1) / blockSize);

            String name = file.getName();
            int at = name.lastIndexOf('@');
            if (at < 0) {
                this.tag = null;
                this.timestampMillis = 0;
                this.flags = DropBoxManager.IS_EMPTY;
                return;
            }

            int flags = 0;
            this.tag = Uri.decode(name.substring(0, at));
            if (name.endsWith(".gz")) {
                flags |= DropBoxManager.IS_GZIPPED;
                name = name.substring(0, name.length() - 3);
            }
            if (name.endsWith(".lost")) {
                flags |= DropBoxManager.IS_EMPTY;
                name = name.substring(at + 1, name.length() - 5);
            } else if (name.endsWith(".txt")) {
                flags |= DropBoxManager.IS_TEXT;
                name = name.substring(at + 1, name.length() - 4);
            } else if (name.endsWith(".dat")) {
                name = name.substring(at + 1, name.length() - 4);
            } else {
                this.flags = DropBoxManager.IS_EMPTY;
                this.timestampMillis = 0;
                return;
            }
            this.flags = flags;

            long millis;
            try { millis = Long.valueOf(name); } catch (NumberFormatException e) { millis = 0; }
            this.timestampMillis = millis;
        }

        /**
         * Creates a EntryFile object with only a timestamp for comparison purposes.
         * @param millis to compare with.
         */
        public EntryFile(long millis) {
            this.tag = null;
            this.timestampMillis = millis;
            this.flags = DropBoxManager.IS_EMPTY;
            this.file = null;
            this.blocks = 0;
        }
    }

    ///////////////////////////////////////////////////////////////////////////

    /** If never run before, scans disk contents to build in-memory tracking data. */
    private synchronized void init() throws IOException {
        if (mStatFs == null) {
            //目录/data/system/dropbox不存在并且创建失败,抛出异常
            if (!mDropBoxDir.isDirectory() && !mDropBoxDir.mkdirs()) {
                throw new IOException("Can't mkdir: " + mDropBoxDir);
            }
            try {
                mStatFs = new StatFs(mDropBoxDir.getPath());
                mBlockSize = mStatFs.getBlockSize();
            } catch (IllegalArgumentException e) {  // StatFs throws this on error
                throw new IOException("Can't statfs: " + mDropBoxDir);
            }
        }

        if (mAllFiles == null) {
            //listFiles()返回的是目录下面的文件和目录数组,返回的是文件和目录的绝对路径
            //如果mDropBoxDir目录下没有文件或目录,则files不为null,files.length为0
            File[] files = mDropBoxDir.listFiles();
            if (files == null) throw new IOException("Can't list files: " + mDropBoxDir);

            mAllFiles = new FileList();
            mFilesByTag = new HashMap<String, FileList>();

            // Scan pre-existing files.
            for (File file : files) {
                //清除.tmp结尾的文件
                if (file.getName().endsWith(".tmp")) {
                    Slog.i(TAG, "Cleaning temp file: " + file);
                    file.delete();
                    continue;
                }
                //为每个日志文件创建一个entry对象进行跟踪
                EntryFile entry = new EntryFile(file, mBlockSize);
                if (entry.tag == null) {
                    Slog.w(TAG, "Unrecognized file: " + file);
                    continue;
                } else if (entry.timestampMillis == 0) {
                    Slog.w(TAG, "Invalid filename: " + file);
                    //删除时间戳为0的文件
                    file.delete();
                    continue;
                }

                enrollEntry(entry);
            }
        }
    }

    /**
     * Adds a disk log file to in-memory tracking for accounting and enumeration.
     *
     * 把日志文件对应的对象加入到内存集合中,以方便计数和查询
     * */
    private synchronized void enrollEntry(EntryFile entry) {
        //所有文件的集合
        mAllFiles.contents.add(entry);
        mAllFiles.blocks += entry.blocks;

        // mFilesByTag is used for trimming, so don't list empty files.
        // (Zero-length/lost files are trimmed by date from mAllFiles.)

        if (entry.tag != null && entry.file != null && entry.blocks > 0) {
            FileList tagFiles = mFilesByTag.get(entry.tag);
            if (tagFiles == null) {
                tagFiles = new FileList();
                mFilesByTag.put(entry.tag, tagFiles);
            }
            //以tag为Key,FileList为value的map;把相同tag的文件作为一个集合
            tagFiles.contents.add(entry);
            tagFiles.blocks += entry.blocks;
        }
    }

    /** Moves a temporary file to a final log filename and enrolls it. */
    private synchronized long createEntry(File temp, String tag, int flags) throws IOException {
        long t = System.currentTimeMillis();

        // Require each entry to have a unique timestamp; if there are entries
        // >10sec in the future (due to clock skew), drag them back to avoid
        // keeping them around forever.
        //每个entry有唯一的时间戳,如果在未来的10s内已经有entry,则删除此,重新加入

        //tailSet : 返回此 set 的部分视图,其元素大于等于 fromElement
        SortedSet<EntryFile> tail = mAllFiles.contents.tailSet(new EntryFile(t + 10000));
        EntryFile[] future = null;
        if (!tail.isEmpty()) {
            future = tail.toArray(new EntryFile[tail.size()]);
            tail.clear();  // Remove from mAllFiles
        }

        if (!mAllFiles.contents.isEmpty()) {
            t = Math.max(t, mAllFiles.contents.last().timestampMillis + 1);
        }

        if (future != null) {
            for (EntryFile late : future) {
                mAllFiles.blocks -= late.blocks;
                FileList tagFiles = mFilesByTag.get(late.tag);
                if (tagFiles != null && tagFiles.contents.remove(late)) {
                    tagFiles.blocks -= late.blocks;
                }
                if ((late.flags & DropBoxManager.IS_EMPTY) == 0) {
                    enrollEntry(new EntryFile(
                            late.file, mDropBoxDir, late.tag, t++, late.flags, mBlockSize));
                } else {
                    enrollEntry(new EntryFile(mDropBoxDir, late.tag, t++));
                }
            }
        }

        if (temp == null) {
            enrollEntry(new EntryFile(mDropBoxDir, tag, t));
        } else {
            enrollEntry(new EntryFile(temp, mDropBoxDir, tag, t, flags, mBlockSize));
        }
        return t;
    }

    /**
     * Trims the files on disk to make sure they aren't using too much space.
     * 整理日志文件,节省空间
     * @return the overall quota for storage (in bytes)
     */
    private synchronized long trimToFit() {
        // Expunge aged items (including tombstones marking deleted data).
        //删除时间长的文件

        //获取过期时间
        int ageSeconds = Settings.Global.getInt(mContentResolver,
                Settings.Global.DROPBOX_AGE_SECONDS, DEFAULT_AGE_SECONDS);
        //获取文件最大数
        int maxFiles = Settings.Global.getInt(mContentResolver,
                Settings.Global.DROPBOX_MAX_FILES, DEFAULT_MAX_FILES);
        //截止点,也就是和entry的时间戳进行比较,
        long cutoffMillis = System.currentTimeMillis() - ageSeconds * 1000;
        while (!mAllFiles.contents.isEmpty()) {
            EntryFile entry = mAllFiles.contents.first();
            if (entry.timestampMillis > cutoffMillis && mAllFiles.contents.size() < maxFiles) break;
            //entry的时间戳小于等于截止点,或者是文件书大于等于1000,删除日志文件
            FileList tag = mFilesByTag.get(entry.tag);
            if (tag != null && tag.contents.remove(entry)) tag.blocks -= entry.blocks;
            if (mAllFiles.contents.remove(entry)) mAllFiles.blocks -= entry.blocks;
            if (entry.file != null) entry.file.delete();
        }

        // Compute overall quota (a fraction of available free space) in blocks.
        // The quota changes dynamically based on the amount of free space;
        // that way when lots of data is available we can use it, but we'll get
        // out of the way if storage starts getting tight.
        //获取开机到现在的毫秒
        long uptimeMillis = SystemClock.uptimeMillis();
        //除非接收到了低存储空间的广播,否者隔5s才能执行一次此动作
        if (uptimeMillis > mCachedQuotaUptimeMillis + QUOTA_RESCAN_MILLIS) {
            int quotaPercent = Settings.Global.getInt(mContentResolver,
                    Settings.Global.DROPBOX_QUOTA_PERCENT, DEFAULT_QUOTA_PERCENT);
            int reservePercent = Settings.Global.getInt(mContentResolver,
                    Settings.Global.DROPBOX_RESERVE_PERCENT, DEFAULT_RESERVE_PERCENT);
            int quotaKb = Settings.Global.getInt(mContentResolver,
                    Settings.Global.DROPBOX_QUOTA_KB, DEFAULT_QUOTA_KB);
            //重启统计文件
            mStatFs.restat(mDropBoxDir.getPath());
            int available = mStatFs.getAvailableBlocks();
            int nonreserved = available - mStatFs.getBlockCount() * reservePercent / 100;
            int maximum = quotaKb * 1024 / mBlockSize;
            //可用的块数量
            mCachedQuotaBlocks = Math.min(maximum, Math.max(0, nonreserved * quotaPercent / 100));
            mCachedQuotaUptimeMillis = uptimeMillis;
        }

        // If we're using too much space, delete old items to make room.
        //
        // We trim each tag independently (this is why we keep per-tag lists).
        // Space is "fairly" shared between tags -- they are all squeezed
        // equally until enough space is reclaimed.
        //
        // A single circular buffer (a la logcat) would be simpler, but this
        // way we can handle fat/bursty data (like 1MB+ bugreports, 300KB+
        // kernel crash dumps, and 100KB+ ANR reports) without swamping small,
        // well-behaved data streams (event statistics, profile data, etc).
        //
        // Deleted files are replaced with zero-length tombstones to mark what
        // was lost.  Tombstones are expunged by age (see above).

        //日志文件占用的块数大于可用的块数量
        if (mAllFiles.blocks > mCachedQuotaBlocks) {
            // Find a fair share amount of space to limit each tag
            int unsqueezed = mAllFiles.blocks,/*未压缩块数*/ squeezed = 0;//已压缩tag的个数
            TreeSet<FileList> tags = new TreeSet<FileList>(mFilesByTag.values());
            for (FileList tag : tags) {
                //算法???
                if (squeezed > 0 && tag.blocks <= (mCachedQuotaBlocks - unsqueezed) / squeezed) {
                    break;
                }
                unsqueezed -= tag.blocks;
                squeezed++;
            }
            int tagQuota = (mCachedQuotaBlocks - unsqueezed) / squeezed;

            // Remove old items from each tag until it meets the per-tag quota.
            for (FileList tag : tags) {
                if (mAllFiles.blocks < mCachedQuotaBlocks) break;
                while (tag.blocks > tagQuota && !tag.contents.isEmpty()) {
                    EntryFile entry = tag.contents.first();
                    if (tag.contents.remove(entry)) tag.blocks -= entry.blocks;
                    if (mAllFiles.contents.remove(entry)) mAllFiles.blocks -= entry.blocks;

                    try {
                        if (entry.file != null) entry.file.delete();
                        enrollEntry(new EntryFile(mDropBoxDir, entry.tag, entry.timestampMillis));
                    } catch (IOException e) {
                        Slog.e(TAG, "Can't write tombstone file", e);
                    }
                }
            }
        }

        return mCachedQuotaBlocks * mBlockSize;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值