storaged: remove protos from storaged class

protobuf is only needed when serializing/deserializing data. Instead of
maintaining a permanent buffer in storaged object, move the container to
stack so that the buffer is released when we don't need it. In addition,
we don't need to clear the buffer before updating it.

Also added a function to clear user io history when the user is removed.

Bug: 63740245
Change-Id: Ia5d19b9a0c3f92a93b061a56be89bb0b958a2a29
diff --git a/storaged/storaged_uid_monitor.cpp b/storaged/storaged_uid_monitor.cpp
index ab1c16e..5745782 100644
--- a/storaged/storaged_uid_monitor.cpp
+++ b/storaged/storaged_uid_monitor.cpp
@@ -257,11 +257,10 @@
 }
 
 std::map<uint64_t, struct uid_records> uid_monitor::dump(
-    double hours, uint64_t threshold, bool force_report,
-    unordered_map<int, StoragedProto>* protos)
+    double hours, uint64_t threshold, bool force_report)
 {
     if (force_report) {
-        report(protos);
+        report(nullptr);
     }
 
     Mutex::Autolock _l(uidm_mutex);
@@ -374,7 +373,9 @@
     update_curr_io_stats_locked();
     add_records_locked(time(NULL));
 
-    update_uid_io_proto(protos);
+    if (protos) {
+        update_uid_io_proto(protos);
+    }
 }
 
 namespace {
@@ -407,10 +408,6 @@
 
 void uid_monitor::update_uid_io_proto(unordered_map<int, StoragedProto>* protos)
 {
-    for (auto& it : *protos) {
-        it.second.mutable_uid_io_usage()->Clear();
-    }
-
     for (const auto& item : io_history) {
         const uint64_t& end_ts = item.first;
         const struct uid_records& recs = item.second;
@@ -449,10 +446,34 @@
     }
 }
 
+void uid_monitor::clear_user_history(userid_t user_id)
+{
+    Mutex::Autolock _l(uidm_mutex);
+
+    for (auto& item : io_history) {
+        vector<uid_record>* entries = &item.second.entries;
+        entries->erase(
+            remove_if(entries->begin(), entries->end(),
+                [user_id](const uid_record& rec) {
+                    return rec.ios.user_id == user_id;}),
+            entries->end());
+    }
+
+    for (auto it = io_history.begin(); it != io_history.end(); ) {
+        if (it->second.entries.empty()) {
+            it = io_history.erase(it);
+        } else {
+            it++;
+        }
+    }
+}
+
 void uid_monitor::load_uid_io_proto(const UidIOUsage& uid_io_proto)
 {
     if (!enabled()) return;
 
+    Mutex::Autolock _l(uidm_mutex);
+
     for (const auto& item_proto : uid_io_proto.uid_io_items()) {
         const UidIORecords& records_proto = item_proto.records();
         struct uid_records* recs = &io_history[item_proto.end_ts()];