File: Counter.cpp

package info (click to toggle)
firefox 143.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,617,328 kB
  • sloc: cpp: 7,478,492; javascript: 6,417,157; ansic: 3,720,058; python: 1,396,372; xml: 627,523; asm: 438,677; java: 186,156; sh: 63,477; makefile: 19,171; objc: 13,059; perl: 12,983; yacc: 4,583; cs: 3,846; pascal: 3,405; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (237 lines) | stat: -rw-r--r-- 8,359 bytes parent folder | download | duplicates (3)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

#include "mozilla/glean/bindings/Counter.h"

#include "nsITelemetry.h"
#include "nsString.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/ResultVariant.h"
#include "mozilla/dom/GleanMetricsBinding.h"
#include "mozilla/glean/bindings/HistogramGIFFTMap.h"
#include "mozilla/glean/bindings/ScalarGIFFTMap.h"
#include "mozilla/glean/fog_ffi_generated.h"
#include "GIFFTFwd.h"

using mozilla::Telemetry::HistogramID;

/* static */
void accumulateToBoolean(HistogramID aId, const nsACString& aLabel,
                         int32_t aAmount) {
  MOZ_ASSERT(aAmount == 1,
             "When mirroring to boolean histograms, we only support "
             "accumulating one sample at a time.");
  if (aLabel.EqualsASCII("true")) {
    TelemetryHistogram::Accumulate(aId, true);
  } else if (aLabel.EqualsASCII("false")) {
    TelemetryHistogram::Accumulate(aId, false);
  } else {
    MOZ_ASSERT_UNREACHABLE(
        "When mirroring to boolean histograms, we only support labels 'true' "
        "and 'false'");
  }
}

/* static */
void accumulateToKeyedBoolean(HistogramID aId, const nsACString& aKey,
                              const nsACString& aCategory, int32_t aAmount) {
  MOZ_ASSERT(aAmount == 1,
             "When mirroring to keyed boolean histograms, we only support "
             "accumulating one sample at a time.");
  if (aCategory.EqualsASCII("true")) {
    TelemetryHistogram::Accumulate(aId, PromiseFlatCString(aKey), true);
  } else if (aCategory.EqualsASCII("false")) {
    TelemetryHistogram::Accumulate(aId, PromiseFlatCString(aKey), false);
  } else {
    MOZ_ASSERT_UNREACHABLE(
        "When mirroring to keyed boolean histograms, we only support labels "
        "'true' and 'false'");
  }
}

/* static */
void accumulateToKeyedCount(HistogramID aId, const nsCString& aLabel,
                            int32_t aAmount) {
  TelemetryHistogram::Accumulate(aId, aLabel, aAmount);
}

/* static */
void accumulateToCategorical(HistogramID aId, const nsCString& aLabel,
                             int32_t aAmount) {
  MOZ_ASSERT(aAmount == 1,
             "When mirroring to categorical histograms, we only support "
             "accumulating one sample at a time.");
  TelemetryHistogram::AccumulateCategorical(aId, aLabel);
}

/* static */
void accumulateToKeyedCategorical(HistogramID aId, const nsCString& aKey,
                                  const nsCString& aCategory, int32_t aAmount) {
  MOZ_ASSERT(aAmount == 1,
             "When mirroring to keyed categorical histograms, we only support "
             "accumulating one sample at a time.");
  TelemetryHistogram::AccumulateCategorical(aId, aKey, aCategory);
}

namespace mozilla::glean {

namespace impl {

template <>
void CounterMetric<CounterType::eBaseOrLabeled>::Add(int32_t aAmount) const {
  auto scalarId = ScalarIdForMetric(mId);
  if (aAmount >= 0) {
    if (scalarId) {
      TelemetryScalar::Add(scalarId.extract(), aAmount);
    } else if (IsSubmetricId(mId)) {
      bool mirrorsToKeyedScalar = false;
      GetLabeledMirrorLock().apply([&](const auto& lock) {
        auto tuple = lock.ref()->MaybeGet(mId);
        mirrorsToKeyedScalar = !!tuple;
        if (tuple && aAmount > 0) {
          TelemetryScalar::Add(std::get<0>(tuple.ref()),
                               std::get<1>(tuple.ref()), (uint32_t)aAmount);
        }
      });
      if (!mirrorsToKeyedScalar) {
        GetLabeledDistributionMirrorLock().apply([&](const auto& lock) {
          auto tuple = lock.ref()->MaybeGet(mId);
          if (tuple) {
            HistogramID hId = std::get<0>(tuple.ref());
            switch (TelemetryHistogram::GetHistogramType(hId)) {
              case nsITelemetry::HISTOGRAM_BOOLEAN:
                accumulateToBoolean(hId, std::get<1>(tuple.ref()), aAmount);
                break;
              case nsITelemetry::HISTOGRAM_COUNT:
                accumulateToKeyedCount(hId, std::get<1>(tuple.ref()), aAmount);
                break;
              case nsITelemetry::HISTOGRAM_CATEGORICAL:
                accumulateToCategorical(hId, std::get<1>(tuple.ref()), aAmount);
                break;
              default:
                MOZ_ASSERT_UNREACHABLE(
                    "Asked to mirror labeled_counter to unsupported histogram "
                    "type.");
                break;
            }
          }
        });
      }
    } else {
      auto hgramId = HistogramIdForMetric(mId);
      if (hgramId) {
        TelemetryHistogram::Accumulate(hgramId.extract(), aAmount);
      }
    }
  }
  fog_counter_add(mId, aAmount);
}

template <>
void CounterMetric<CounterType::eDualLabeled>::Add(int32_t aAmount) const {
  if (IsSubmetricId(mId)) {
    GetDualLabeledDistributionMirrorLock().apply([&](const auto& lock) {
      auto tuple = lock.ref()->MaybeGet(mId);
      if (tuple) {
        HistogramID hId = std::get<0>(tuple.ref());
        switch (TelemetryHistogram::GetHistogramType(hId)) {
          case nsITelemetry::HISTOGRAM_BOOLEAN:
            accumulateToKeyedBoolean(hId, std::get<1>(tuple.ref()),
                                     std::get<2>(tuple.ref()), aAmount);
            break;
          case nsITelemetry::HISTOGRAM_CATEGORICAL:
            accumulateToKeyedCategorical(hId, std::get<1>(tuple.ref()),
                                         std::get<2>(tuple.ref()), aAmount);
            break;
          default:
            MOZ_ASSERT_UNREACHABLE(
                "Asked to mirror dual_labeled_counter to unsupported "
                "histogram type.");
            break;
        }
      }
    });
  }
  fog_dual_labeled_counter_add(mId, aAmount);
}

template <>
Result<Maybe<int32_t>, nsCString>
CounterMetric<CounterType::eBaseOrLabeled>::TestGetValue(
    const nsACString& aPingName) const {
  nsCString err;
  if (fog_counter_test_get_error(mId, &err)) {
    return Err(err);
  }
  if (!fog_counter_test_has_value(mId, &aPingName)) {
    return Maybe<int32_t>();  // can't use Nothing() or templates will fail.
  }
  return Some(fog_counter_test_get_value(mId, &aPingName));
}

template <>
Result<Maybe<int32_t>, nsCString>
CounterMetric<CounterType::eDualLabeled>::TestGetValue(
    const nsACString& aPingName) const {
  nsCString err;
  if (fog_dual_labeled_counter_test_get_error(mId, &err)) {
    return Err(err);
  }
  if (!fog_dual_labeled_counter_test_has_value(mId, &aPingName)) {
    return Maybe<int32_t>();  // can't use Nothing() or templates will fail.
  }
  return Some(fog_dual_labeled_counter_test_get_value(mId, &aPingName));
}

}  // namespace impl

/* virtual */
JSObject* GleanCounter::WrapObject(JSContext* aCx,
                                   JS::Handle<JSObject*> aGivenProto) {
  return dom::GleanCounter_Binding::Wrap(aCx, this, aGivenProto);
}

void GleanCounter::Add(int32_t aAmount) {
  switch (mType) {
    case impl::CounterType::eBaseOrLabeled:
      impl::CounterMetric<impl::CounterType::eBaseOrLabeled>(mId).Add(aAmount);
      break;
    case impl::CounterType::eDualLabeled:
      impl::CounterMetric<impl::CounterType::eDualLabeled>(mId).Add(aAmount);
      break;
    default:
      MOZ_ASSERT_UNREACHABLE();
  }
}

dom::Nullable<int32_t> GleanCounter::TestGetValue(const nsACString& aPingName,
                                                  ErrorResult& aRv) {
  dom::Nullable<int32_t> ret;
  Result<Maybe<int32_t>, nsCString> result(Err(VoidCString()));
  switch (mType) {
    case impl::CounterType::eBaseOrLabeled:
      result = impl::CounterMetric<impl::CounterType::eBaseOrLabeled>(mId)
                   .TestGetValue(aPingName);
      break;
    case impl::CounterType::eDualLabeled:
      result = impl::CounterMetric<impl::CounterType::eDualLabeled>(mId)
                   .TestGetValue(aPingName);
      break;
    default:
      MOZ_ASSERT_UNREACHABLE();
  }
  if (result.isErr()) {
    aRv.ThrowDataError(result.unwrapErr());
    return ret;
  }
  auto optresult = result.unwrap();
  if (!optresult.isNothing()) {
    ret.SetValue(optresult.value());
  }
  return ret;
}

}  // namespace mozilla::glean