1 /*
2 * Copyright 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_TAG "BtStopWatchLegacy"
18
19 #include "common/stop_watch_legacy.h"
20
21 #include <bluetooth/log.h>
22
23 #include <iomanip>
24 #include <mutex>
25 #include <sstream>
26 #include <utility>
27
28 #include "common/init_flags.h"
29 #include "os/log.h"
30
31 namespace bluetooth {
32 namespace common {
33
34 static const int LOG_BUFFER_LENGTH = 10;
35 static std::array<StopWatchLog, LOG_BUFFER_LENGTH> stopwatch_logs;
36 static int current_buffer_index;
37 static std::recursive_mutex stopwatch_log_mutex;
38
RecordLog(StopWatchLog log)39 void StopWatchLegacy::RecordLog(StopWatchLog log) {
40 std::unique_lock<std::recursive_mutex> lock(stopwatch_log_mutex, std::defer_lock);
41 if (!lock.try_lock()) {
42 log::info("try_lock fail. log content: {}, took {} us", log.message,
43 static_cast<size_t>(
44 std::chrono::duration_cast<std::chrono::microseconds>(
45 stopwatch_logs[current_buffer_index].end_timestamp -
46 stopwatch_logs[current_buffer_index].start_timestamp)
47 .count()));
48 return;
49 }
50 if (current_buffer_index >= LOG_BUFFER_LENGTH) {
51 current_buffer_index = 0;
52 }
53 stopwatch_logs[current_buffer_index] = std::move(log);
54 current_buffer_index++;
55 lock.unlock();
56 }
57
DumpStopWatchLog()58 void StopWatchLegacy::DumpStopWatchLog() {
59 std::lock_guard<std::recursive_mutex> lock(stopwatch_log_mutex);
60 log::info("=-----------------------------------=");
61 log::info("bluetooth stopwatch log history:");
62 for (int i = 0; i < LOG_BUFFER_LENGTH; i++) {
63 if (current_buffer_index >= LOG_BUFFER_LENGTH) {
64 current_buffer_index = 0;
65 }
66 if (stopwatch_logs[current_buffer_index].message.empty()) {
67 current_buffer_index++;
68 continue;
69 }
70 std::stringstream ss;
71 auto now = stopwatch_logs[current_buffer_index].timestamp;
72 auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(
73 now.time_since_epoch()) %
74 1000;
75 auto now_time_t = std::chrono::system_clock::to_time_t(now);
76 ss << std::put_time(std::localtime(&now_time_t), "%Y-%m-%d %H:%M:%S");
77 ss << '.' << std::setfill('0') << std::setw(3) << millis.count();
78 std::string start_timestamp = ss.str();
79 log::info("{}: {}: took {} us", start_timestamp,
80 stopwatch_logs[current_buffer_index].message,
81 static_cast<size_t>(
82 std::chrono::duration_cast<std::chrono::microseconds>(
83 stopwatch_logs[current_buffer_index].end_timestamp -
84 stopwatch_logs[current_buffer_index].start_timestamp)
85 .count()));
86 current_buffer_index++;
87 }
88 log::info("=-----------------------------------=");
89 }
90
StopWatchLegacy(std::string text)91 StopWatchLegacy::StopWatchLegacy(std::string text)
92 : text_(std::move(text)),
93 timestamp_(std::chrono::system_clock::now()),
94 start_timestamp_(std::chrono::high_resolution_clock::now()) {}
95
~StopWatchLegacy()96 StopWatchLegacy::~StopWatchLegacy() {
97 StopWatchLog sw_log;
98 sw_log.timestamp = timestamp_;
99 sw_log.start_timestamp = start_timestamp_;
100 sw_log.end_timestamp = std::chrono::high_resolution_clock::now();
101 sw_log.message = std::move(text_);
102
103 RecordLog(std::move(sw_log));
104 }
105
106 } // namespace common
107 } // namespace bluetooth
108