blob: 35c40ab7e669f4fed7e3e06d3a8f6a13efd3d8ae [file] [log] [blame]
Dmitri Plotnikov5effd852021-08-11 14:55:58 -07001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 * Android BPF library - public API
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include "LongArrayMultiStateCounter.h"
19#include <log/log.h>
20
21namespace android {
22namespace battery {
23
Dmitri Plotnikov0a6dc402024-01-19 14:33:20 -080024Uint64ArrayRW::Uint64ArrayRW(const Uint64Array &copy) : Uint64Array(copy.size()) {
25 if (mSize != 0 && copy.data() != nullptr) {
26 mData = new uint64_t[mSize];
27 memcpy(mData, copy.data(), mSize * sizeof(uint64_t));
28 } else {
29 mData = nullptr;
30 }
31}
32
33uint64_t *Uint64ArrayRW::dataRW() {
34 if (mData == nullptr) {
35 mData = new uint64_t[mSize];
36 memset(mData, 0, mSize * sizeof(uint64_t));
37 }
38 return mData;
39}
40
41Uint64ArrayRW &Uint64ArrayRW::operator=(const Uint64Array &t) {
42 if (t.size() != mSize) {
43 delete[] mData;
44 mSize = t.size();
45 mData = nullptr;
46 }
47 if (mSize != 0) {
48 if (t.data() != nullptr) {
49 mData = new uint64_t[mSize];
50 memcpy(mData, t.data(), mSize * sizeof(uint64_t));
51 } else {
52 mData = nullptr;
53 }
54 }
55 return *this;
56}
57
58std::ostream &operator<<(std::ostream &os, const Uint64Array &v) {
59 os << "{";
60 const uint64_t *data = v.data();
61 if (data != nullptr) {
62 bool first = true;
63 for (size_t i = 0; i < v.size(); i++) {
64 if (!first) {
65 os << ", ";
66 }
67 os << data[i];
68 first = false;
69 }
70 }
71 os << "}";
72 return os;
73}
74
75// Convenience constructor for tests
76Uint64ArrayRW::Uint64ArrayRW(std::initializer_list<uint64_t> init) : Uint64Array(init.size()) {
77 mData = new uint64_t[mSize];
78 memcpy(mData, init.begin(), mSize * sizeof(uint64_t));
79}
80
81// Used in tests only.
82bool Uint64Array::operator==(const Uint64Array &other) const {
83 if (size() != other.size()) {
84 return false;
85 }
86 const uint64_t* thisData = data();
87 const uint64_t* thatData = other.data();
88 for (size_t i = 0; i < mSize; i++) {
89 const uint64_t v1 = thisData != nullptr ? thisData[i] : 0;
90 const uint64_t v2 = thatData != nullptr ? thatData[i] : 0;
91 if (v1 != v2) {
92 return false;
93 }
94 }
95 return true;
96}
97
Dmitri Plotnikov5effd852021-08-11 14:55:58 -070098template <>
Dmitri Plotnikov0a6dc402024-01-19 14:33:20 -080099void LongArrayMultiStateCounter::add(Uint64ArrayRW *value1, const Uint64Array &value2,
100 const uint64_t numerator, const uint64_t denominator) const {
101 const uint64_t* data2 = value2.data();
102 if (data2 == nullptr) {
103 return;
104 }
105
106 uint64_t* data1 = value1->dataRW();
107 size_t size = value2.size();
108 if (numerator != denominator) {
109 for (size_t i = 0; i < size; i++) {
110 // The caller ensures that denominator != 0
111 data1[i] += data2[i] * numerator / denominator;
112 }
113 } else {
114 for (size_t i = 0; i < size; i++) {
115 data1[i] += data2[i];
116 }
117 }
118}
119
120template<>
121bool LongArrayMultiStateCounter::delta(const Uint64ArrayRW &previousValue,
122 const Uint64Array &newValue, Uint64ArrayRW *outValue) const {
Dmitri Plotnikov5effd852021-08-11 14:55:58 -0700123 size_t size = previousValue.size();
124 if (newValue.size() != size) {
Dmitri Plotnikov0a6dc402024-01-19 14:33:20 -0800125 ALOGE("Incorrect array size: %d, should be %d", (int) newValue.size(), (int) size);
126 return false;
127 }
128 if (outValue->size() != size) {
129 ALOGE("Incorrect outValue size: %d, should be %d", (int) outValue->size(), (int) size);
Dmitri Plotnikov5effd852021-08-11 14:55:58 -0700130 return false;
131 }
132
133 bool is_delta_valid = true;
Dmitri Plotnikov0a6dc402024-01-19 14:33:20 -0800134 const uint64_t *prevData = previousValue.data();
135 const uint64_t *newData = newValue.data();
136 uint64_t *outData = outValue->dataRW();
137 for (size_t i = 0; i < size; i++) {
138 if (prevData == nullptr) {
139 if (newData == nullptr) {
140 outData[i] = 0;
141 } else {
142 outData[i] = newData[i];
143 }
144 } else if (newData == nullptr || newData[i] < prevData[i]) {
145 outData[i] = 0;
Dmitri Plotnikov5effd852021-08-11 14:55:58 -0700146 is_delta_valid = false;
Dmitri Plotnikov0a6dc402024-01-19 14:33:20 -0800147 } else {
148 outData[i] = newData[i] - prevData[i];
Dmitri Plotnikov5effd852021-08-11 14:55:58 -0700149 }
150 }
151 return is_delta_valid;
152}
153
Dmitri Plotnikov5effd852021-08-11 14:55:58 -0700154} // namespace battery
155} // namespace android