-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FXIOS-7616 [v121] Debug utility to help identify bloat in app con…
…tainer (#16975) (#17047) * [7616] Add debug utility to help identify problems with excessive bloat in app container or cache storage. WIP * [7616] Fix SwiftLint warning * [7616] Provide nicer output for report by ordering by size * [7616] Some general cleanup and minor refactors * [7616] Line spacing (cherry picked from commit 6906199) Co-authored-by: mattreaganmozilla <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
df2eccb
commit 62d6b25
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
Client/Frontend/Settings/Main/Debug/AppDataUsageReportSetting.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
|
||
import UIKit | ||
|
||
class AppDataUsageReportSetting: HiddenSetting { | ||
override var title: NSAttributedString? { | ||
// Not localized for now. | ||
return NSAttributedString(string: "App Data Usage Report", | ||
attributes: [NSAttributedString.Key.foregroundColor: theme.colors.textPrimary]) | ||
} | ||
|
||
override func onClick(_ navigationController: UINavigationController?) { | ||
let results = generateAppDataSummary() | ||
UIPasteboard.general.string = results | ||
|
||
// Hidden debug utility not localized for now. | ||
showSimpleAlert("Summary generated. Text has been copied to the clipboard.") | ||
} | ||
|
||
// MARK: - Internal Utilities | ||
|
||
private func showSimpleAlert(_ message: String) { | ||
let alert = UIAlertController(title: "App Data Usage", | ||
message: message, | ||
preferredStyle: .alert) | ||
alert.addAction(UIAlertAction(title: "OK", style: .default)) | ||
settings.present(alert, animated: true) | ||
} | ||
|
||
private func generateAppDataSummary() -> String { | ||
var directoriesAndSizes: [String: UInt64] = [:] | ||
var largeFileWarnings: [String: UInt64] = [:] | ||
let fileManager = FileManager.default | ||
let warningSize = 100 * 1024 * 1024 // (100MB) File size threshold to log for report | ||
|
||
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask).first | ||
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first | ||
let directories: [URL] = [cachesDirectory, documentsDirectory].compactMap({ $0 }) | ||
|
||
for baseDirectory in directories { | ||
guard let enumerator = fileManager.enumerator(at: baseDirectory, | ||
includingPropertiesForKeys: [URLResourceKey.fileSizeKey], | ||
options: [], | ||
errorHandler: nil) else { continue } | ||
for case let fileURL as URL in enumerator { | ||
var isDir: ObjCBool = false | ||
let path = fileURL.path | ||
if fileManager.fileExists(atPath: path, isDirectory: &isDir) && !isDir.boolValue { | ||
let parentDir = fileURL.deletingLastPathComponent().path | ||
if directoriesAndSizes[parentDir] == nil { directoriesAndSizes[parentDir] = 0 } | ||
do { | ||
let values = try fileURL.resourceValues(forKeys: [URLResourceKey.fileSizeKey]) | ||
let size = UInt64(values.fileSize ?? 0) | ||
|
||
if size >= warningSize { largeFileWarnings[path] = size } | ||
|
||
// Find any directory whose path is a valid prefix for the file path | ||
// This allows us to tally the total sizes for parent directories | ||
// along with nested children (if needed) at the same time. | ||
for dir in directoriesAndSizes.keys where path.hasPrefix(dir) { | ||
let newSize = (directoriesAndSizes[dir] ?? 0) + size | ||
directoriesAndSizes[dir] = newSize | ||
} | ||
} catch { | ||
print("Error checking file size: \(error)") | ||
} | ||
} else { | ||
if directoriesAndSizes[path] == nil { directoriesAndSizes[path] = 0 } | ||
} | ||
} | ||
} | ||
|
||
let directoriesAndSizesSorted = directoriesAndSizes | ||
.map({ return ($0, $1) }) | ||
.sorted(by: { return $0.1 > $1.1 }) | ||
|
||
var result = "FireFox Debug Utility: App Data Summary" | ||
result += "\n=======================================" | ||
result += "\n" | ||
for (dir, size) in directoriesAndSizesSorted { | ||
result += "\nSize: \(size / 1024) kb \t\tDirectory: \t\(dir)" | ||
} | ||
result += "\n\n=======================================" | ||
result += "\nLarge files detected: \(largeFileWarnings.count)" | ||
for (file, size) in largeFileWarnings { | ||
result += "\nSize: \(size / 1024) kb \t\tFile: \t\(file)" | ||
} | ||
return result | ||
} | ||
} |