Bertrand SIMONNET | 52e5b99 | 2015-08-10 15:18:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | */ |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 16 | |
| 17 | #ifndef METRICS_PERSISTENT_INTEGER_H_ |
| 18 | #define METRICS_PERSISTENT_INTEGER_H_ |
| 19 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 22 | #include <string> |
| 23 | |
| 24 | namespace chromeos_metrics { |
| 25 | |
| 26 | // PersistentIntegers is a named 64-bit integer value backed by a file. |
| 27 | // The in-memory value acts as a write-through cache of the file value. |
| 28 | // If the backing file doesn't exist or has bad content, the value is 0. |
| 29 | |
| 30 | class PersistentInteger { |
| 31 | public: |
Bertrand SIMONNET | e6cfd64 | 2014-07-09 16:35:23 -0700 | [diff] [blame] | 32 | explicit PersistentInteger(const std::string& name); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 33 | |
| 34 | // Virtual only because of mock. |
| 35 | virtual ~PersistentInteger(); |
| 36 | |
| 37 | // Sets the value. This writes through to the backing file. |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 38 | void Set(int64_t v); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 39 | |
| 40 | // Gets the value. May sync from backing file first. |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 41 | int64_t Get(); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 42 | |
| 43 | // Returns the name of the object. |
| 44 | std::string Name() { return name_; } |
| 45 | |
| 46 | // Convenience function for Get() followed by Set(0). |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 47 | int64_t GetAndClear(); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 48 | |
| 49 | // Convenience function for v = Get, Set(v + x). |
| 50 | // Virtual only because of mock. |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 51 | virtual void Add(int64_t x); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 52 | |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 53 | // Sets the directory path for all persistent integers. |
| 54 | // This is used in unittests to change where the counters are stored. |
| 55 | static void SetMetricsDirectory(const std::string& directory); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 56 | |
| 57 | private: |
| 58 | static const int kVersion = 1001; |
| 59 | |
Luigi Semenzato | a5f4fe6 | 2014-04-15 09:31:47 -0700 | [diff] [blame] | 60 | // Writes |value_| to the backing file, creating it if necessary. |
| 61 | void Write(); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 62 | |
| 63 | // Reads the value from the backing file, stores it in |value_|, and returns |
Luigi Semenzato | a5f4fe6 | 2014-04-15 09:31:47 -0700 | [diff] [blame] | 64 | // true if the backing file is valid. Returns false otherwise, and creates |
| 65 | // a valid backing file as a side effect. |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 66 | bool Read(); |
| 67 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 68 | int64_t value_; |
| 69 | int32_t version_; |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 70 | std::string name_; |
| 71 | std::string backing_file_name_; |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 72 | static std::string metrics_directory_; |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 73 | bool synced_; |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 74 | }; |
| 75 | |
Bertrand SIMONNET | e6cfd64 | 2014-07-09 16:35:23 -0700 | [diff] [blame] | 76 | } // namespace chromeos_metrics |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 77 | |
| 78 | #endif // METRICS_PERSISTENT_INTEGER_H_ |