summaryrefslogtreecommitdiffstats
path: root/toolkit/components/glean/build_scripts/glean_parser_ext/templates/js.jinja2
blob: b8804b91e55ff94817e26151522091a10fc39285 (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
// -*- mode: C++ -*-

// AUTOGENERATED BY glean_parser.  DO NOT EDIT.
{# The rendered source is autogenerated, but this
Jinja2 template is not. Please file bugs! #}

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_GleanJSMetricsLookup_h
#define mozilla_GleanJSMetricsLookup_h

#include "mozilla/PerfectHash.h"
#include "mozilla/Maybe.h"
#include "mozilla/glean/bindings/MetricTypes.h"

#define GLEAN_INDEX_BITS ({{index_bits}})
#define GLEAN_ID_BITS ({{id_bits}})
#define GLEAN_TYPE_ID(id) ((id) >> GLEAN_ID_BITS)
#define GLEAN_METRIC_ID(id) ((id) & ((1ULL << GLEAN_ID_BITS) - 1))
#define GLEAN_OFFSET(entry) (entry & ((1ULL << GLEAN_INDEX_BITS) - 1))

namespace mozilla::glean {

// The category lookup table's entry type
using category_entry_t = uint32_t;
// The metric lookup table's entry type
using metric_entry_t = uint64_t;

static_assert(GLEAN_INDEX_BITS + GLEAN_ID_BITS < sizeof(metric_entry_t) * 8, "Index and ID bits need to fit into an category_entry_t");
static_assert(GLEAN_ID_BITS < sizeof(uint32_t) * 8, "Metric IDs need to fit into less than 32 bit");
static_assert({{ categories|length }} < UINT32_MAX, "Too many metric categories generated.");
static_assert({{ metric_id_mapping|length }} < {{2 ** id_bits}}, "Too many metrics generated.");
static_assert({{ metric_type_ids|length }} < {{2 ** (entry_width - index_bits - id_bits)}}, "Too many different metric types.");

static already_AddRefed<nsISupports> NewMetricFromId(uint32_t id) {
  uint32_t typeId = GLEAN_TYPE_ID(id);
  uint32_t metricId = GLEAN_METRIC_ID(id);

  switch (typeId) {
    {% for type, type_id in metric_type_ids.items() %}
    {% if type|is_implemented_type %}
    case {{ type_id }}: /* {{ type|Camelize }} */
    {
      return MakeAndAddRef<{{type | type_name}}>(metricId);
    }
    {% endif %}
    {% endfor %}
    default:
    MOZ_ASSERT_UNREACHABLE("Invalid type ID reached when trying to instantiate a new metric");
    return nullptr;
  }
}

static Maybe<uint32_t> category_result_check(const nsACString& aKey, category_entry_t entry);
static Maybe<uint32_t> metric_result_check(const nsACString& aKey, metric_entry_t entry);

{{ category_string_table }}
static_assert(sizeof(gCategoryStringTable) < UINT32_MAX, "Category string table is too large.");

{{ category_by_name_lookup }}

{{ metric_string_table }}
static_assert(sizeof(gMetricStringTable) < {{2 ** index_bits}}, "Metric string table is too large.");

{{ metric_by_name_lookup }}

/**
 * Get a category's name from the string table.
 */
static const char* GetCategoryName(category_entry_t entry) {
  MOZ_ASSERT(entry < sizeof(gCategoryStringTable), "Entry identifier offset larger than string table");
  return &gCategoryStringTable[entry];
}

/**
 * Get a metric's identifier from the string table.
 */
static const char* GetMetricIdentifier(metric_entry_t entry) {
  uint32_t offset = GLEAN_OFFSET(entry);
  MOZ_ASSERT(offset < sizeof(gMetricStringTable), "Entry identifier offset larger than string table");
  return &gMetricStringTable[offset];
}

/**
 * Check that the found entry is pointing to the right key
 * and return it.
 * Or return `Nothing()` if the entry was not found.
 */
static Maybe<uint32_t> category_result_check(const nsACString& aKey, category_entry_t entry) {
  if (MOZ_UNLIKELY(entry > sizeof(gCategoryStringTable))) {
    return Nothing();
  }
  if (aKey.EqualsASCII(gCategoryStringTable + entry)) {
    return Some(entry);
  }
  return Nothing();
}

/**
 * Check if the found entry index is pointing to the right key
 * and return the corresponding metric ID.
 * Or return `Nothing()` if the entry was not found.
 */
static Maybe<uint32_t> metric_result_check(const nsACString& aKey, uint64_t entry) {
  uint32_t metricId = entry >> GLEAN_INDEX_BITS;
  uint32_t offset = GLEAN_OFFSET(entry);

  if (offset > sizeof(gMetricStringTable)) {
    return Nothing();
  }

  if (aKey.EqualsASCII(gMetricStringTable + offset)) {
    return Some(metricId);
  }

  return Nothing();
}


#undef GLEAN_INDEX_BITS
#undef GLEAN_ID_BITS
#undef GLEAN_TYPE_ID
#undef GLEAN_METRIC_ID
#undef GLEAN_OFFSET

} // namespace mozilla::glean
#endif // mozilla_GleanJSMetricsLookup_h