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 | |
Bertrand SIMONNET | 4b915ae | 2015-07-28 15:38:14 -0700 | [diff] [blame] | 17 | #include "persistent_integer.h" |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 18 | |
| 19 | #include <fcntl.h> |
| 20 | |
| 21 | #include <base/logging.h> |
| 22 | #include <base/posix/eintr_wrapper.h> |
| 23 | |
Bertrand SIMONNET | bd3505e | 2015-08-04 14:04:51 -0700 | [diff] [blame] | 24 | #include "constants.h" |
Bertrand SIMONNET | e6cfd64 | 2014-07-09 16:35:23 -0700 | [diff] [blame] | 25 | #include "metrics/metrics_library.h" |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 26 | |
Luigi Semenzato | e5c7eb1 | 2014-04-16 17:05:05 -0700 | [diff] [blame] | 27 | |
| 28 | namespace chromeos_metrics { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 29 | |
| 30 | // Static class member instantiation. |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 31 | std::string PersistentInteger::metrics_directory_ = metrics::kMetricsDirectory; |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 32 | |
| 33 | PersistentInteger::PersistentInteger(const std::string& name) : |
| 34 | value_(0), |
| 35 | version_(kVersion), |
| 36 | name_(name), |
| 37 | synced_(false) { |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 38 | backing_file_name_ = metrics_directory_ + name_; |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | PersistentInteger::~PersistentInteger() {} |
| 42 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 43 | void PersistentInteger::Set(int64_t value) { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 44 | value_ = value; |
Luigi Semenzato | a5f4fe6 | 2014-04-15 09:31:47 -0700 | [diff] [blame] | 45 | Write(); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 48 | int64_t PersistentInteger::Get() { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 49 | // If not synced, then read. If the read fails, it's a good idea to write. |
| 50 | if (!synced_ && !Read()) |
Luigi Semenzato | a5f4fe6 | 2014-04-15 09:31:47 -0700 | [diff] [blame] | 51 | Write(); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 52 | return value_; |
| 53 | } |
| 54 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 55 | int64_t PersistentInteger::GetAndClear() { |
| 56 | int64_t v = Get(); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 57 | Set(0); |
| 58 | return v; |
| 59 | } |
| 60 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 61 | void PersistentInteger::Add(int64_t x) { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 62 | Set(Get() + x); |
| 63 | } |
| 64 | |
Luigi Semenzato | a5f4fe6 | 2014-04-15 09:31:47 -0700 | [diff] [blame] | 65 | void PersistentInteger::Write() { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 66 | int fd = HANDLE_EINTR(open(backing_file_name_.c_str(), |
| 67 | O_WRONLY | O_CREAT | O_TRUNC, |
| 68 | S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH)); |
| 69 | PCHECK(fd >= 0) << "cannot open " << backing_file_name_ << " for writing"; |
| 70 | PCHECK((HANDLE_EINTR(write(fd, &version_, sizeof(version_))) == |
| 71 | sizeof(version_)) && |
| 72 | (HANDLE_EINTR(write(fd, &value_, sizeof(value_))) == |
| 73 | sizeof(value_))) |
| 74 | << "cannot write to " << backing_file_name_; |
| 75 | close(fd); |
| 76 | synced_ = true; |
| 77 | } |
| 78 | |
| 79 | bool PersistentInteger::Read() { |
| 80 | int fd = HANDLE_EINTR(open(backing_file_name_.c_str(), O_RDONLY)); |
| 81 | if (fd < 0) { |
| 82 | PLOG(WARNING) << "cannot open " << backing_file_name_ << " for reading"; |
| 83 | return false; |
| 84 | } |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 85 | int32_t version; |
| 86 | int64_t value; |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 87 | bool read_succeeded = false; |
| 88 | if (HANDLE_EINTR(read(fd, &version, sizeof(version))) == sizeof(version) && |
| 89 | version == version_ && |
| 90 | HANDLE_EINTR(read(fd, &value, sizeof(value))) == sizeof(value)) { |
| 91 | value_ = value; |
| 92 | read_succeeded = true; |
| 93 | synced_ = true; |
| 94 | } |
| 95 | close(fd); |
| 96 | return read_succeeded; |
| 97 | } |
| 98 | |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 99 | void PersistentInteger::SetMetricsDirectory(const std::string& directory) { |
| 100 | metrics_directory_ = directory; |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | |
| 104 | } // namespace chromeos_metrics |