Ray Essick | 5a55729 | 2020-06-10 21:31:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "MetricsCleaner" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include "cleaner.h" |
| 22 | |
| 23 | namespace android::mediametrics { |
| 24 | |
| 25 | // place time into buckets at 0,1,2,4,8,16,32 seconds and then at minute boundaries. |
| 26 | // time is rounded up to the next boundary. |
| 27 | // |
| 28 | int64_t bucket_time_minutes(int64_t in_millis) { |
| 29 | |
| 30 | const int64_t SEC_TO_MS = 1000; |
| 31 | const int64_t MIN_TO_MS = (60 * SEC_TO_MS); |
| 32 | |
| 33 | if (in_millis <= 0) { |
| 34 | return 0; |
| 35 | } |
| 36 | if (in_millis <= 32 * SEC_TO_MS) { |
| 37 | for (int sec = 1; sec <= 32; sec *= 2) { |
| 38 | if (in_millis <= sec * SEC_TO_MS) { |
| 39 | return sec * SEC_TO_MS; |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | /* up to next 1 minute boundary */ |
| 44 | int64_t minutes = (in_millis + MIN_TO_MS - 1) / MIN_TO_MS; |
| 45 | in_millis = minutes * MIN_TO_MS; |
| 46 | return in_millis; |
| 47 | } |
| 48 | |
| 49 | } // namespace android::mediametrics |