• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2#
3#   Copyright 2017 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17from metrics.metric import Metric
18
19
20class RamMetric(Metric):
21
22    COMMAND = "free -m"
23    # Fields for response dictionary
24    TOTAL = 'total'
25    USED = 'used'
26    FREE = 'free'
27    BUFFERS = 'buffers'
28    CACHED = 'cached'
29
30    def gather_metric(self):
31        """Finds RAM statistics in MB
32
33        Returns:
34            A dict with the following fields:
35                total: int representing total physical RAM available in MB
36                used: int representing total RAM used by system in MB
37                free: int representing total RAM free for new process in MB
38                buffers: total RAM buffered by different applications in MB
39                cached: total RAM for caching of data in MB
40        """
41        # Run shell command
42        result = self._shell.run(self.COMMAND)
43        # Example stdout:
44        #           total       used       free     shared    buffers     cached
45        # Mem:      64350      34633      29717        556       1744      24692
46        # -/+ buffers/cache:     8196      56153
47        # Swap:     65459          0      65459
48
49        # Get only second line
50        output = result.stdout.splitlines()[1]
51        # Split by space
52        fields = output.split()
53        # Create response dictionary
54        response = {
55            self.TOTAL: int(fields[1]),
56            self.USED: int(fields[2]),
57            self.FREE: int(fields[3]),
58            # Skip shared column, since obsolete
59            self.BUFFERS: int(fields[5]),
60            self.CACHED: int(fields[6]),
61        }
62        return (response)
63