aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/codegen/core/gen_stats_data.py
blob: c56e1f260891a247713ffd2f3084c5532779786d (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
#!/usr/bin/env python2.7

# Copyright 2017 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import collections
import ctypes
import math
import sys
import yaml

with open('src/core/lib/debug/stats_data.yaml') as f:
  attrs = yaml.load(f.read())

types = (
  (collections.namedtuple('Counter', 'name'), []),
  (collections.namedtuple('Histogram', 'name max buckets'), []),
)

inst_map = dict((t[0].__name__, t[1]) for t in types)

stats = []

for attr in attrs:
  found = False
  for t, lst in types:
    t_name = t.__name__.lower()
    if t_name in attr:
      name = attr[t_name]
      del attr[t_name]
      lst.append(t(name=name, **attr))
      found = True
      break
  assert found, "Bad decl: %s" % attr

def dbl2u64(d):
  return ctypes.c_ulonglong.from_buffer(ctypes.c_double(d)).value

def shift_works(mapped_bounds, shift_bits):
  for a, b in zip(mapped_bounds, mapped_bounds[1:]):
    if (a >> shift_bits) == (b >> shift_bits):
      return False
  return True

def find_max_shift(mapped_bounds):
  for shift_bits in reversed(range(0,64)):
    if shift_works(mapped_bounds, shift_bits):
      return shift_bits
  return -1

def gen_bucket_code(histogram):
  bounds = [0, 1]
  done_trivial = False
  done_unmapped = False
  first_nontrivial = None
  first_unmapped = None
  while len(bounds) < histogram.buckets:
    mul = math.pow(float(histogram.max) / bounds[-1],
                   1.0 / (histogram.buckets - len(bounds)))
    nextb = bounds[-1] * mul
    if nextb < bounds[-1] + 1:
      nextb = bounds[-1] + 1
    elif not done_trivial:
      done_trivial = True
      first_nontrivial = len(bounds)
    bounds.append(nextb)
  if done_trivial:
    code_bounds = [dbl2u64(x - first_nontrivial) for x in bounds]
    shift_bits = find_max_shift(code_bounds[first_nontrivial:])
  print first_nontrivial, shift_bits, bounds, [hex(x >> shift_bits) for x in code_bounds[first_nontrivial:]]

# utility: print a big comment block into a set of files
def put_banner(files, banner):
  for f in files:
    print >>f, '/*'
    for line in banner:
      print >>f, ' * %s' % line
    print >>f, ' */'
    print >>f

with open('src/core/lib/debug/stats_data.h', 'w') as H:
  # copy-paste copyright notice from this file
  with open(sys.argv[0]) as my_source:
    copyright = []
    for line in my_source:
      if line[0] != '#': break
    for line in my_source:
      if line[0] == '#':
        copyright.append(line)
        break
    for line in my_source:
      if line[0] != '#':
        break
      copyright.append(line)
    put_banner([H], [line[2:].rstrip() for line in copyright])

  put_banner([H], ["Automatically generated by tools/codegen/core/gen_stats_data.py"])

  print >>H, "#ifndef GRPC_CORE_LIB_DEBUG_STATS_DATA_H"
  print >>H, "#define GRPC_CORE_LIB_DEBUG_STATS_DATA_H"
  print >>H

  for typename, instances in sorted(inst_map.items()):
    print >>H, "typedef enum {"
    for inst in instances:
      print >>H, "  GRPC_STATS_%s_%s," % (typename.upper(), inst.name.upper())
    print >>H, "  GRPC_STATS_%s_COUNT" % (typename.upper())
    print >>H, "} grpc_stats_%ss;" % (typename.lower())

  for ctr in inst_map['Counter']:
    print >>H, ("#define GRPC_STATS_INC_%s(exec_ctx) " +
                "GRPC_STATS_INC_COUNTER((exec_ctx), GRPC_STATS_COUNTER_%s)") % (
                ctr.name.upper(), ctr.name.upper())
  for histogram in inst_map['Histogram']:
    print >>H, ("#define GRPC_STATS_INC_%s(exec_ctx, value) " +
                "GRPC_STATS_INC_HISTOGRAM((exec_ctx), " +
                    "GRPC_STATS_HISTOGRAM_%s," +
                    "%s)") % (
                histogram.name.upper(),
                histogram.name.upper(),
                gen_bucket_code(histogram))

  print >>H, "extern const char *grpc_stats_counter_name[GRPC_STATS_COUNTER_COUNT];"

  print >>H
  print >>H, "#endif /* GRPC_CORE_LIB_DEBUG_STATS_DATA_H */"

with open('src/core/lib/debug/stats_data.c', 'w') as C:
  # copy-paste copyright notice from this file
  with open(sys.argv[0]) as my_source:
    copyright = []
    for line in my_source:
      if line[0] != '#': break
    for line in my_source:
      if line[0] == '#':
        copyright.append(line)
        break
    for line in my_source:
      if line[0] != '#':
        break
      copyright.append(line)
    put_banner([C], [line[2:].rstrip() for line in copyright])

  put_banner([C], ["Automatically generated by tools/codegen/core/gen_stats_data.py"])

  print >>C, "#include \"src/core/lib/debug/stats_data.h\""

  print >>C, "const char *grpc_stats_counter_name[GRPC_STATS_COUNTER_COUNT] = {";
  for ctr in inst_map['Counter']:
    print >>C, "  \"%s\"," % ctr.name
  print >>C, "};"