autotest: Collect info of usage the host classes.

Classes based on AbstractSSHHost will track info which classes was
created that time. This information will give us statistic unused
classes in autotest.

BUG=chromium:1064804
TEST=run local, presubmit

for labstation during deployment
./site_utils/deployment/prepare/main.py --hostname chromeos1-row4-rack4-labstation --host-info-file /tr/host_info_store/chromeos1-row4-rack4-labstation.store --results-dir /tr/ run-pre-deploy-verification

for DUT during repair
./server/autoserv -s --host-info-subdir host_info_store -m chromeos1-row4-rack1-host5 --lab True --local-only-host-info True -R -r /tr/


Change-Id: I95ca3587cd423f87b9bd925c799e62bc8e315e3e
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/2274679
Tested-by: Otabek Kasimov <otabek@google.com>
Reviewed-by: Gregory Nisbet <gregorynisbet@chromium.org>
Reviewed-by: Gregory Nisbet <gregorynisbet@google.com>
Reviewed-by: Garry Wang <xianuowang@chromium.org>
Commit-Queue: Otabek Kasimov <otabek@google.com>
diff --git a/server/hosts/abstract_ssh.py b/server/hosts/abstract_ssh.py
index c9d9c14..d8e1bda 100644
--- a/server/hosts/abstract_ssh.py
+++ b/server/hosts/abstract_ssh.py
@@ -8,6 +8,7 @@
 
 from autotest_lib.client.bin.result_tools import runner as result_tools_runner
 from autotest_lib.client.common_lib import error
+from autotest_lib.client.common_lib import utils
 from autotest_lib.client.common_lib.cros.network import ping_runner
 from autotest_lib.client.common_lib.global_config import global_config
 from autotest_lib.server import utils, autotest
@@ -16,6 +17,11 @@
 from autotest_lib.server.hosts import rpc_server_tracker
 from autotest_lib.server.hosts import ssh_multiplex
 
+try:
+    from chromite.lib import metrics
+except ImportError:
+    metrics = utils.metrics_mock
+
 # pylint: disable=C0111
 
 get_value = global_config.get_config_value
@@ -66,6 +72,7 @@
         @param connection_pool: ssh_multiplex.ConnectionPool instance to share
                 the master ssh connection across control scripts.
         """
+        self._track_class_usage()
         # IP address is retrieved only on demand. Otherwise the host
         # initialization will fail for host is not online.
         self._ip = None
@@ -1008,3 +1015,24 @@
             self._cached_up_status = self.is_up_fast()
             self._cached_up_status_updated = time.time()
         return self._cached_up_status
+
+
+    def _track_class_usage(self):
+        """Tracking which class was used.
+
+        The idea to identify unused classes to be able clean them up.
+        We skip names with dynamic created classes where the name is
+        hostname of the device.
+        """
+        class_name = None
+        if 'chrome' not in self.__class__.__name__:
+            class_name = self.__class__.__name__
+        else:
+            for base in self.__class__.__bases__:
+                if 'chrome' not in base.__name__:
+                    class_name = base.__name__
+                    break
+        if class_name:
+            data = {'host_class': class_name}
+            metrics.Counter(
+                'chromeos/autotest/used_hosts').increment(fields=data)