| // Copyright 2019 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #ifndef SRC_DEVELOPER_MEMORY_METRICS_PRINTER_H_ | |
| #define SRC_DEVELOPER_MEMORY_METRICS_PRINTER_H_ | |
| #include <iostream> | |
| #include <string> | |
| #include "src/developer/memory/metrics/capture.h" | |
| #include "src/developer/memory/metrics/digest.h" | |
| #include "src/developer/memory/metrics/summary.h" | |
| #include "src/lib/fxl/macros.h" | |
| namespace memory { | |
| constexpr size_t kMaxFormattedStringSize = sizeof("1023.5T"); | |
| const char* FormatSize(uint64_t bytes, char* buf); | |
| enum Sorted : uint8_t { UNSORTED, SORTED }; | |
| class JsonPrinter { | |
| public: | |
| explicit JsonPrinter(zx::socket& output_socket) : output_socket(output_socket) {} | |
| /* Outputs capture as JSON. In this format: | |
| { | |
| "Time":28477260758625, | |
| "Kernel":{ | |
| "total":1610612736, | |
| "free":170565632, | |
| "wired":67395584, | |
| "total_heap":30904320, | |
| "free_heap":1873728, | |
| "vmo":1276194816, | |
| "mmu":65294336, | |
| "ipc":196608, | |
| "other":61440, | |
| "zram_compressed_total": 65294336, | |
| "zram_uncompressed": 65294336, | |
| "zram_fragmentation": 65294336, | |
| }, | |
| "Processes":[ | |
| ["koid","name","vmos"], | |
| [47325,"fonts.cm",[47353, ...]], | |
| ... | |
| ], | |
| "VmoNames":["scudo:primary", ...], | |
| "Vmos":[ | |
| ["koid","name","parent_koid","committed_bytes","allocated_bytes","populated_bytes"], | |
| [47440,38,47437,4096,4096,4096], | |
| ... | |
| ] | |
| } | |
| For size reasons: | |
| - Processes and Vmos have an initial entry that contains the names of the the fields in the | |
| rest of the entries. | |
| - The names of the VMOs are an index into the VMONames array. | |
| */ | |
| void PrintCapture(const Capture& capture); | |
| // `bucket_config` must be a valid JSON array, in this format: | |
| // [ | |
| // { | |
| // "event_code": 29, | |
| // "name": "BlobfsInactive", | |
| // "process": "blobfs\\.cm", | |
| // "vmo": "inactive-blob-.*" | |
| // }, | |
| // ... | |
| // ] | |
| // | |
| // | |
| // Outputs JSON in this format: | |
| // {"Capture": | |
| // { | |
| // "Time":28477260758625, | |
| // "Kernel":{ | |
| // "total":1610612736, | |
| // "free":170565632, | |
| // "wired":67395584, | |
| // "total_heap":30904320, | |
| // "free_heap":1873728, | |
| // "vmo":1276194816, | |
| // "mmu":65294336, | |
| // "ipc":196608, | |
| // "other":61440, | |
| // "zram_compressed_total": 65294336, | |
| // "zram_uncompressed": 65294336, | |
| // "zram_fragmentation": 65294336, | |
| // }, | |
| // "Processes":[ | |
| // ["koid","name","vmos"], | |
| // [47325,"fonts.cm",[47353, ...]], | |
| // ... | |
| // ], | |
| // "VmoNames":["scudo:primary", ...], | |
| // "Vmos":[ | |
| // ["koid","name","parent_koid","committed_bytes","allocated_bytes","populated_bytes"], | |
| // [47440,38,47437,4096,4096,4096], | |
| // ... | |
| // ] | |
| // } | |
| // "Buckets": | |
| // [ | |
| // { | |
| // "event_code": 29, | |
| // "name": "BlobfsInactive", | |
| // "process": "blobfs\\.cm", | |
| // "vmo": "inactive-blob-.*" | |
| // }, | |
| // ... | |
| // ] | |
| // } | |
| // | |
| // For size reasons: | |
| // - Processes and Vmos have an initial entry that contains the names of the the fields in the | |
| // rest of the entries. | |
| // - The names of the VMOs are an index into the VMONames array. | |
| void PrintCaptureAndBucketConfig(const Capture& capture, const std::string& bucket_config); | |
| private: | |
| zx::socket& output_socket; | |
| FXL_DISALLOW_COPY_AND_ASSIGN(JsonPrinter); | |
| }; | |
| class TextPrinter { | |
| public: | |
| explicit TextPrinter(std::ostream& os) : os_(os) {} | |
| void PrintSummary(const Summary& summary, CaptureLevel level, Sorted sorted); | |
| void PrintDigest(const Digest& digest); | |
| private: | |
| void OutputSizes(const Sizes& sizes); | |
| std::ostream& os_; | |
| FXL_DISALLOW_COPY_AND_ASSIGN(TextPrinter); | |
| }; | |
| } // namespace memory | |
| #endif // SRC_DEVELOPER_MEMORY_METRICS_PRINTER_H_ |