summaryrefslogtreecommitdiffstats
path: root/src/tools/histogram_dump.py
blob: cc22fef5e964c31867d3ec917dba52506e2176fb (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
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python3
# coding: utf-8
#
# Ceph - scalable distributed file system
#
# Copyright (C) 2017 OVH
# Copyright (C) 2020 Marc Schöchlin <ms-github@256bit.org>
#
# This is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License version 2, as published by the Free Software
# Foundation.  See file COPYING.
#

import json
import subprocess
import time
import os
import argparse
import glob
import sys
import textwrap
import datetime


def shorten(val):
    if isinstance(val, str):
        return val
    for u in ((3, ''), (6, 'k'), (9, 'M'), (12, 'G'), (15, 'T')):
        if val < 10**u[0]:
            return "{}{}".format(int(val / (10 ** (u[0]-3))), u[1])
    return val


def create_histogram(sockets, counter, last, seconds, batch):

    current_datasets = {}
    json_d = {}
    for socket in sockets:
       try:
           out = subprocess.check_output(
               "ceph --admin-daemon {} perf histogram dump".format(socket),
               shell=True)
           json_d = json.loads(out.decode('utf-8'))
       except Exception as e:
           return (last,
                   "Couldn't connect to admin socket, result: \n{}".format(e))
       current_datasets[socket] = json_d['osd'][counter]['values']


    axes = json_d['osd'][counter]['axes']
   
    if batch:
        content = "{} : Counter: {} for {}\n\n\n".format(
                datetime.datetime.now().isoformat(), counter,", ".join(sockets))
    else:
        content = "Counter: {} for {}\n(create statistics every {} seconds)\n\n".format(
                counter,", ".join(sockets),seconds)

    content += "{}:\n".format(axes[1]['name'])
    for r in axes[1]['ranges']:
        content += "{0: >4} ".format(
            shorten(r['min']) if 'min' in r else '')
    content += "\n"
    for r in axes[1]['ranges']:
        content += "{0: >4} ".format(
            shorten(r['max']) if 'max' in r else '')
    content += "\n"

    content += ("{0: >"+str(len(axes[1]['ranges'])*5+14)+"}:\n").format(
        axes[0]['name'])

    if batch:
        COL = ''
        ENDC = ''
    else:
        COL = '\033[91m'
        ENDC = '\033[0m'

    current = []

    # initalize with zeros
    for i in range(len(current_datasets[socket])):
       current.append([])
       for j in range(len(current_datasets[socket][i])):
              current[i].append(0)

    # combine data
    for socket, data in current_datasets.items():
       for i in range(len(data)):
           for j in range(len(data[i])):
              current[i][j] += data[i][j]

    for i in range(len(current)):
        for j in range(len(current[i])):
            try:
                diff = current[i][j] - last[i][j]
            except IndexError:
                diff = '-'

            if diff != "-" and diff != 0:
                content += "{0}{1: >4}{2} ".format(COL,shorten(diff),ENDC)
            else:
                content += "{0: >4} ".format(shorten(diff))

        r = axes[0]['ranges'][i]
        content += "{0: >6} : {1}\n".format(
            shorten(r['min']) if 'min' in r else '',
            shorten(r['max']) if 'max' in r else '')
    return (current, content)


def loop_print(sockets, counter, loop_seconds, batch):
    last = []

    try:
       while True:
           last, content = create_histogram(sockets, counter, last, loop_seconds, batch)
           if not batch:
               print(chr(27) + "[2J")
           print(content)
           time.sleep(loop_seconds)
    except KeyboardInterrupt:
       print("...interupted")
       sys.exit(0)


def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        description='Continuously display ceph performance histogram for selected osd operations')
    parser.add_argument(
        '--asok',
        type=str,
        default=['/var/run/ceph/*.asok'],
        nargs='+',
        help='Path to asok file, you can use wildcards')
    parser.add_argument(
        '--counter',
        type=str,
	help=textwrap.dedent('''\
         Specify name of the counter to calculate statistics
         see "ceph --admin-daemon /var/run/ceph/<osd>.asok  perf histogram dump"
         '''),
        default='op_w_latency_in_bytes_histogram')
    parser.add_argument(
	'--batch',
	help='Disable colors and add timestamps',
	action='store_true',
    )
    parser.add_argument(
        '--loop_seconds',
        type=int,
	help='Cycle time in seconds for statistics generation',
        default=5)

    args = parser.parse_args()

    if not sys.stdout.isatty():
      print("Not running with a tty, automatically switching to batch mode")
      args.batch = True
    
    sockets = []
    for asok in args.asok: 
      sockets = glob.glob(asok) + sockets

    if len(sockets) == 0:
      print("no suitable socket at {}".format(args.asok))
      sys.exit(1)
 
    loop_print(sockets, args.counter, args.loop_seconds, args.batch)

if __name__ == '__main__':
    main()