aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/benchmark/scenarios/compute/cachestat.py
blob: da4aa754f5a6f167b09ae809a05ecdb390ba0e17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
##############################################################################
# Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

"""cache hit/miss ratio and usage statistics"""

import pkg_resources
import logging
import re
import yardstick.ssh as ssh

from yardstick.benchmark.scenarios import base

LOG = logging.getLogger(__name__)


class CACHEstat(base.Scenario):
    '''Collect cache statistics.

    This scenario reads system cache hit/miss ratio and other statistics on
    a Linux host.

    cache statistics are read using 'cachestat'.
    cachestat - show Linux page cache hit/miss statistics.
                Uses Linux ftrace.

    This is a proof of concept using Linux ftrace capabilities on older
    kernels, and works by using function profiling for in-kernel counters.
    Specifically, four kernel functions are traced:

    mark_page_accessed() for measuring cache accesses
    mark_buffer_dirty() for measuring cache writes
    add_to_page_cache_lru() for measuring page additions
    account_page_dirtied() for measuring page dirties

    It is possible that these functions have been renamed (or are different
    logically) for your kernel version, and this script will not work as-is.
    This script was written on Linux 3.13. This script is a sandcastle: the
    kernel may wash some away, and you'll need to rebuild.

    USAGE: cachestat [-Dht] [interval]
       eg,
         cachestat 5    # show stats every 5 seconds

    Run "cachestat -h" for full usage.

    WARNING: This uses dynamic tracing of kernel functions, and could cause
    kernel panics or freezes. Test, and know what you are doing, before use.
    It also traces cache activity, which can be frequent, and cost some
    overhead. The statistics should be treated as best-effort: there may be
    some error margin depending on unusual workload types.

    REQUIREMENTS: CONFIG_FUNCTION_PROFILER, awk.
    '''
    __scenario_type__ = "CACHEstat"

    TARGET_SCRIPT = "cache_stat.bash"

    def __init__(self, scenario_cfg, context_cfg):
        """Scenario construction."""
        self.scenario_cfg = scenario_cfg
        self.context_cfg = context_cfg
        self.setup_done = False

    def setup(self):
        """Scenario setup."""
        self.target_script = pkg_resources.resource_filename(
            "yardstick.benchmark.scenarios.compute",
            CACHEstat.TARGET_SCRIPT)

        host = self.context_cfg['host']
        user = host.get('user', 'ubuntu')
        ip = host.get('ip', None)
        key_filename = host.get('key_filename', '~/.ssh/id_rsa')

        LOG.info("user:%s, host:%s", user, ip)
        self.client = ssh.SSH(user, ip, key_filename=key_filename)
        self.client.wait(timeout=600)

        # copy scripts to host
        self.client.run("cat > ~/cache_stat.sh",
                        stdin=open(self.target_script, 'rb'))

        self.setup_done = True

    def _execute_command(self, cmd):
        """Execute a command on server."""
        LOG.info("Executing: %s" % cmd)
        status, stdout, stderr = self.client.execute(cmd)
        if status:
            raise RuntimeError("Failed executing command: ",
                               cmd, stderr)
        return stdout

    def _filtrate_result(self, result):
        fields = []
        cachestat = {}
        data_marker = re.compile("\d+")
        ite = 0
        average = {'HITS': 0, 'MISSES': 0, 'DIRTIES': 0, 'RATIO': 0,
                   'BUFFERS_MB': 0, 'CACHE_MB': 0}
        maximum = {'HITS': 0, 'MISSES': 0, 'DIRTIES': 0, 'RATIO': 0,
                   'BUFFERS_MB': 0, 'CACHE_MB': 0}

        # Parse cache stats
        for row in result.split('\n'):
            line = row.split()

            if line and line[0] == 'HITS':
                # header fields
                fields = line[:]
            elif line and re.match(data_marker, line[0]):
                cache = 'cache' + str(ite)
                ite += 1
                values = line[:]
                if values and len(values) == len(fields):
                    cachestat[cache] = dict(zip(fields, values))

        for entry in cachestat:
            for item in average:
                if item != 'RATIO':
                    average[item] += int(cachestat[entry][item])
                else:
                    average[item] += float(cachestat[entry][item][:-1])

            for item in maximum:
                if item != 'RATIO':
                    if int(cachestat[entry][item]) > maximum[item]:
                        maximum[item] = int(cachestat[entry][item])
                else:
                    if float(cachestat[entry][item][:-1]) > maximum[item]:
                        maximum[item] = float(cachestat[entry][item][:-1])

        for item in average:
            if item != 'RATIO':
                average[item] = average[item] / len(cachestat)
            else:
                average[item] = str(average[item] / len(cachestat)) + '%'

        return {'cachestat': cachestat, 'average': average, 'max': maximum}

    def _get_cache_usage(self):
        """Get cache statistics."""
        options = self.scenario_cfg['options']
        interval = options.get("interval", 1)

        cmd = "sudo bash cache_stat.sh %s" % (interval)

        result = self._execute_command(cmd)
        filtrated_result = self._filtrate_result(result)

        return filtrated_result

    def run(self, result):
        if not self.setup_done:
            self.setup()

        result.update(self._get_cache_usage())