Android:dumpsys

    xiaoxiao2021-03-26  16

    比如#dumpsys diskstats会打印系统中内部存储设备的使用情况。 那这个dumpsys diskstats到底是从哪里打印存储设备的情况的呢? 先看一下dumpsys.cpp的main函数实现

    int main(int argc, char* const argv[]) { signal(SIGPIPE, SIG_IGN); sp<IServiceManager> sm = defaultServiceManager(); fflush(stdout); if (sm == NULL) { ALOGE("Unable to get default service manager!"); aerr << "dumpsys: Unable to get default service manager!" << endl; return 20; } Vector<String16> services; Vector<String16> args; bool showListOnly = false; if ((argc == 2) && (strcmp(argv[1], "-l") == 0)) { showListOnly = true; } if ((argc == 1) || showListOnly) { services = sm->listServices(); services.sort(sort_func); args.add(String16("-a")); } else { services.add(String16(argv[1])); for (int i=2; i<argc; i++) { args.add(String16(argv[i])); } } const size_t N = services.size(); if (N > 1) { // first print a list of the current services aout << "Currently running services:" << endl; for (size_t i=0; i<N; i++) { sp<IBinder> service = sm->checkService(services[i]); if (service != NULL) { aout << " " << services[i] << endl; } } } if (showListOnly) { return 0; } for (size_t i=0; i<N; i++) { sp<IBinder> service = sm->checkService(services[i]); if (service != NULL) { if (N > 1) { aout << "------------------------------------------------------------" "-------------------" << endl; aout << "DUMP OF SERVICE " << services[i] << ":" << endl; } int err = service->dump(STDOUT_FILENO, args); if (err != 0) { aerr << "Error dumping service info: (" << strerror(err) << ") " << services[i] << endl; } } else { aerr << "Can't find service: " << services[i] << endl; } } return 0; }

    这里#dumpsys diskstats命令会在dumpsys的main函数中,从所有注册的service里边找到叫diskstats的,然后调用其dump()函数。那diskstats是否已经注册了呢? 可以再SystemServer.java->startOtherServices()函数中找到

    private void startOtherServices() { ... try { Slog.i(TAG, "DiskStats Service"); ServiceManager.addService("diskstats", new DiskStatsService(context)); } catch (Throwable e) { reportWtf("starting DiskStats Service", e); } } ... }

    以下是DiskStatsService类的定义,可以看到已经定义了dump()函数。可以从dump()函数里看到是怎么打印disk信息的

    public class DiskStatsService extends Binder { private static final String TAG = "DiskStatsService"; private final Context mContext; public DiskStatsService(Context context) { mContext = context; } @Override protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); // Run a quick-and-dirty performance test: write 512 bytes byte[] junk = new byte[512]; for (int i = 0; i < junk.length; i++) junk[i] = (byte) i; // Write nonzero bytes File tmp = new File(Environment.getDataDirectory(), "system/perftest.tmp"); FileOutputStream fos = null; IOException error = null; long before = SystemClock.uptimeMillis(); try { fos = new FileOutputStream(tmp); fos.write(junk); } catch (IOException e) { error = e; } finally { try { if (fos != null) fos.close(); } catch (IOException e) {} } long after = SystemClock.uptimeMillis(); if (tmp.exists()) tmp.delete(); if (error != null) { pw.print("Test-Error: "); pw.println(error.toString()); } else { pw.print("Latency: "); pw.print(after - before); pw.println("ms [512B Data Write]"); } reportFreeSpace(Environment.getDataDirectory(), "Data", pw); reportFreeSpace(Environment.getDownloadCacheDirectory(), "Cache", pw); reportFreeSpace(new File("/system"), "System", pw); // TODO: Read /proc/yaffs and report interesting values; // add configurable (through args) performance test parameters. } private void reportFreeSpace(File path, String name, PrintWriter pw) { try { StatFs statfs = new StatFs(path.getPath()); long bsize = statfs.getBlockSize(); long avail = statfs.getAvailableBlocks(); long total = statfs.getBlockCount(); if (bsize <= 0 || total <= 0) { throw new IllegalArgumentException( "Invalid stat: bsize=" + bsize + " avail=" + avail + " total=" + total); } pw.print(name); pw.print("-Free: "); pw.print(avail * bsize / 1024); pw.print("K / "); pw.print(total * bsize / 1024); pw.print("K total = "); pw.print(avail * 100 / total); pw.println("% free"); } catch (IllegalArgumentException e) { pw.print(name); pw.print("-Error: "); pw.println(e.toString()); return; } } }
    转载请注明原文地址: https://ju.6miu.com/read-649998.html

    最新回复(0)