How can I know if fstrim was run when I put my phone on charging over night?


Question

Is there any logging where I can confirm this?


Answer

A charging, idle device over night is not the only way to run fstrim, there are other ways too.



What this old article quotes about scheduled fstrim on Android:




The Android framework will send out a “start idle maintenance window” event that the MountService listens for, and then invokes vold to fstrim filesystems when a few conditions have been met – the device hasn’t been touched for over an hour, no idle maintenance window event has been sent in 24 hours, and the device is either off-charger with 80% battery or on-charger with 30% battery.




But IdleMaintenanceService was removed in Lollipop, things have changed. On Pie, scheduled fstrim is triggered from StorageManagerService (1) to MountService (2):




if the device is alive at midnight + idle, fstrim gets run at the next available charging + idle time




* (3)

Mount service calls StorageManager which in turn calls Vold to do fstrim.



If the schedule is being missed because the conditions don't meet and fstrim has not run for last 3 days (4), it will be forced during device startup if the device is restarted. SystemServer (5), PackageManager (6), StorageManager (7) and Vold (8) are involved.



In each case, the file /data/system/last-fstrim (9) (since Lollipop) gets updated (10), so you can check its timestamp (requires root) to find out when fstrim was last run.



If you want to do TRIM manually, use Android's commandline tool sm fstrim (requires root or adb shell). Previously this was vdc fstrim dotrim but added to Storage Manager in Oreo (11) and removed from vold in Pie (12).



In order to send FITRIM ioctl to selected filesystem(s) only, use fstrim tool (requires root); a busybox applet.






What if you don't have root?

And you suspect that the scheduled fstrim is being missed for 3+ days, restart device, fstrim will be forced (since Lollipop 5.1 (13)). Simple.



Apps can't TRIM without root; see this answer.






PS: HOW OFTEN TRIM SHOULD BE RUN?



Flash memory (including SSDs, eMMCs, SD Cards, USB sticks etc.) has limited number of Program/Erase cycles before it dies. But unlike HDDs, data can't be just overwritten on flash memory, it has to be Erased first, which is achieved through Garbage Collection.



Now if we do too much fstrim, this will cause frequent GC in background consuming precious P/E cycles and hence reducing life. If we don't do fstrim at all, there could be unnecessary GC (of already deleted data), plus the write operations will get too slow with time because Erase will have to be performed before overwriting data, which has very higher latency as compared to Program operation.



So we need a balance between both.



Also there are other factors e.g. whether eMMC controller supports over provisioning, how effective it is in background GC scheduling, wear-leveling and managing write amplification etc.



So unless you have the data sheet of your phone's eMMC and you are sure what you are doing, better is to leave the fstrim to OS. On PCs' (14):




Running fstrim frequently, or even using mount -o discard, might negatively affect the lifetime of poor-quality SSD devices. For most desktop and server systems a sufficient trimming frequency is once a week.




But this may vary with situations.



RELATED:




Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes