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 <array>
24 #include <iomanip>
25 #include <mutex>
26 #include <sstream>
27 #include <utility>
28
29 namespace bluetooth {
30 namespace common {
31
32 static const int LOG_BUFFER_LENGTH = 10;
33 static std::array<StopWatchLog, LOG_BUFFER_LENGTH> stopwatch_logs;
34 static int current_buffer_index;
35 static std::recursive_mutex stopwatch_log_mutex;
36
RecordLog(StopWatchLog log)37 void StopWatchLegacy::RecordLog(StopWatchLog log) {
38 std::unique_lock<std::recursive_mutex> lock(stopwatch_log_mutex, std::defer_lock);
39 if (!lock.try_lock()) {
40 log::info(
41 "try_lock fail. log content: {}, took {} us", log.message,
42 static_cast<size_t>(
43 std::chrono::duration_cast<std::chrono::microseconds>(
44 stopwatch_logs[current_buffer_index % LOG_BUFFER_LENGTH].end_timestamp -
45 stopwatch_logs[current_buffer_index % LOG_BUFFER_LENGTH]
46 .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 =
73 std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
74 auto now_time_t = std::chrono::system_clock::to_time_t(now);
75 ss << std::put_time(std::localtime(&now_time_t), "%Y-%m-%d %H:%M:%S");
76 ss << '.' << std::setfill('0') << std::setw(3) << millis.count();
77 std::string start_timestamp = ss.str();
78 log::info("{}: {}: took {} us", start_timestamp, stopwatch_logs[current_buffer_index].message,
79 static_cast<size_t>(std::chrono::duration_cast<std::chrono::microseconds>(
80 stopwatch_logs[current_buffer_index].end_timestamp -
81 stopwatch_logs[current_buffer_index].start_timestamp)
82 .count()));
83 current_buffer_index++;
84 }
85 log::info("=-----------------------------------=");
86 }
87
StopWatchLegacy(std::string text)88 StopWatchLegacy::StopWatchLegacy(std::string text)
89 : text_(std::move(text)),
90 timestamp_(std::chrono::system_clock::now()),
91 start_timestamp_(std::chrono::high_resolution_clock::now()) {}
92
~StopWatchLegacy()93 StopWatchLegacy::~StopWatchLegacy() {
94 StopWatchLog sw_log;
95 sw_log.timestamp = timestamp_;
96 sw_log.start_timestamp = start_timestamp_;
97 sw_log.end_timestamp = std::chrono::high_resolution_clock::now();
98 sw_log.message = std::move(text_);
99
100 RecordLog(std::move(sw_log));
101 }
102
103 } // namespace common
104 } // namespace bluetooth
105